Ticket #39776: 39776.6.diff
File 39776.6.diff, 17.2 KB (added by , 5 years ago) |
---|
-
src/wp-admin/includes/plugin.php
diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php index 683cb7c215..5ea6f2c1c5 100644
function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $func 1338 1338 * and only include lowercase alphanumeric, dashes, and underscores characters 1339 1339 * to be compatible with sanitize_key(). 1340 1340 * @param callable $function The function to be called to output the content for this page. 1341 * @param int $position The position order in the menu where this item should appear. 1342 * 1341 1343 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1342 1344 */ 1343 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1345 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1344 1346 global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, 1345 1347 $_registered_pages, $_parent_pages; 1346 1348 … … function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 1370 1372 } 1371 1373 } 1372 1374 1373 $submenu[ $parent_slug ][] = array( $menu_title, $capability, $menu_slug, $page_title ); 1375 $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title ); 1376 if ( null === $position ) { 1377 $submenu[ $parent_slug ][] = $new_sub_menu; 1378 } else { 1379 // If position is equal or higher than the number of items in the array, just append it. 1380 if ( $position >= count( $submenu[ $parent_slug ] ) ) { 1381 $submenu[ $parent_slug ][] = $new_sub_menu; 1382 } elseif( 0 === $position) { 1383 array_unshift( $submenu[ $parent_slug ], $new_sub_menu ); 1384 } else { 1385 // Ensure we don't have a negative position. 1386 $position = max( $position, 0 ); 1387 // Grab all of the items before the insertion point. 1388 $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true ); 1389 // Grab all of the items after the insertion point 1390 $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true ); 1391 // Add the new item 1392 $before_items[] = $new_sub_menu; 1393 // Merge the items. 1394 $submenu[ $parent_slug ] = array_merge( $before_items, $after_items ); 1395 } 1396 } 1397 // Sort the parent array 1398 ksort( $submenu[ $parent_slug ] ); 1374 1399 1375 1400 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug ); 1376 1401 if ( ! empty( $function ) && ! empty( $hookname ) ) { … … function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 1409 1434 * @param string $capability The capability required for this menu to be displayed to the user. 1410 1435 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1411 1436 * @param callable $function The function to be called to output the content for this page. 1437 * @param int $position The position in the menu order this one should appear. 1412 1438 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1413 1439 */ 1414 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1415 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );1440 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1441 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1416 1442 } 1417 1443 1418 1444 /** … … function add_management_page( $page_title, $menu_title, $capability, $menu_slug, 1431 1457 * @param string $capability The capability required for this menu to be displayed to the user. 1432 1458 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1433 1459 * @param callable $function The function to be called to output the content for this page. 1460 * @param int $position The position in the menu order this one should appear. 1434 1461 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1435 1462 */ 1436 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1437 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );1463 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1464 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1438 1465 } 1439 1466 1440 1467 /** … … function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $f 1453 1480 * @param string $capability The capability required for this menu to be displayed to the user. 1454 1481 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1455 1482 * @param callable $function The function to be called to output the content for this page. 1483 * @param int $position The position in the menu order this one should appear. 1456 1484 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1457 1485 */ 1458 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1459 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );1486 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1487 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1460 1488 } 1461 1489 1462 1490 /** … … function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1475 1503 * @param string $capability The capability required for this menu to be displayed to the user. 1476 1504 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1477 1505 * @param callable $function The function to be called to output the content for this page. 1506 * @param int $position The position in the menu order this one should appear. 1478 1507 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1479 1508 */ 1480 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1481 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );1509 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1510 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1482 1511 } 1483 1512 1484 1513 /** … … function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $f 1497 1526 * @param string $capability The capability required for this menu to be displayed to the user. 1498 1527 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1499 1528 * @param callable $function The function to be called to output the content for this page. 1529 * @param int $position The position in the menu order this one should appear. 1500 1530 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1501 1531 */ 1502 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1503 if ( current_user_can( 'edit_users' ) ) { 1532 1533 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1534 if ( current_user_can('edit_users') ) { 1504 1535 $parent = 'users.php'; 1505 1536 } else { 1506 1537 $parent = 'profile.php'; 1507 1538 } 1508 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );1539 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1509 1540 } 1510 1541 /** 1511 1542 * Add submenu page to the Dashboard main menu. … … function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1523 1554 * @param string $capability The capability required for this menu to be displayed to the user. 1524 1555 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1525 1556 * @param callable $function The function to be called to output the content for this page. 1557 * @param int $position The position in the menu order this one should appear. 1526 1558 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1527 1559 */ 1528 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1529 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );1560 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1561 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1530 1562 } 1531 1563 1532 1564 /** … … function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, 1545 1577 * @param string $capability The capability required for this menu to be displayed to the user. 1546 1578 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1547 1579 * @param callable $function The function to be called to output the content for this page. 1580 * @param int $position The position in the menu order this one should appear. 1548 1581 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1549 1582 */ 1550 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1551 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );1583 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1584 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1552 1585 } 1553 1586 1554 1587 /** … … function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1567 1600 * @param string $capability The capability required for this menu to be displayed to the user. 1568 1601 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1569 1602 * @param callable $function The function to be called to output the content for this page. 1603 * @param int $position The position in the menu order this one should appear. 1570 1604 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1571 1605 */ 1572 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1573 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );1606 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1607 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1574 1608 } 1575 1609 1576 1610 /** … … function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1589 1623 * @param string $capability The capability required for this menu to be displayed to the user. 1590 1624 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1591 1625 * @param callable $function The function to be called to output the content for this page. 1626 * @param int $position The position in the menu order this one should appear. 1592 1627 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1593 1628 */ 1594 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1595 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );1629 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1630 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1596 1631 } 1597 1632 1598 1633 /** … … function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1611 1646 * @param string $capability The capability required for this menu to be displayed to the user. 1612 1647 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1613 1648 * @param callable $function The function to be called to output the content for this page. 1649 * @param int $position The position in the menu order this one should appear. 1614 1650 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1615 1651 */ 1616 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1617 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function 1652 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1653 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position); 1618 1654 } 1619 1655 1620 1656 /** … … function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1633 1669 * @param string $capability The capability required for this menu to be displayed to the user. 1634 1670 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1635 1671 * @param callable $function The function to be called to output the content for this page. 1672 * @param int $position The position in the menu order this one should appear. 1636 1673 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1637 1674 */ 1638 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {1639 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );1675 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1676 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1640 1677 } 1641 1678 1642 1679 /** -
tests/phpunit/tests/admin/includesPlugin.php
diff --git tests/phpunit/tests/admin/includesPlugin.php tests/phpunit/tests/admin/includesPlugin.php index 916ca755b7..ce090d1643 100644
class Tests_Admin_includesPlugin extends WP_UnitTestCase { 57 57 wp_set_current_user( $current_user ); 58 58 } 59 59 60 /** 61 * Tests the priority parameter. 62 * 63 * @param int $priority The position of the new item. 64 * @param int $expected_position Where the new item is expected to appear. 65 * 66 * @dataProvider data_submenu_priority 67 */ 68 function test_submenu_priority( $priority, $expected_position ) { 69 global $submenu; 70 $current_user = get_current_user_id(); 71 wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); 72 set_current_screen( 'dashboard' ); 73 74 // Setup a menu with some items. 75 $parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' ); 76 foreach ( $this->submenus_to_add() as $menu ) { 77 add_submenu_page( $parent, $menu[0], $menu[1], $menu[2], $menu[3], $menu[4] ); 78 } 79 80 // Insert the new page. 81 add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos' , $priority ); 82 83 $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] ); 84 85 wp_set_current_user( $current_user ); 86 } 87 88 /** 89 * Helper to store the menus to add so getting the length is programtically done. 90 * 91 * @return array 92 */ 93 function submenus_to_add() { 94 return array( 95 array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ), 96 array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ), 97 array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ), 98 array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ), 99 array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ), 100 ); 101 } 102 /** 103 * Data provider for the above tests 104 * @return array 105 */ 106 function data_submenu_priority() { 107 $menu_count = count( $this->submenus_to_add() ); 108 return array( 109 array( null, $menu_count ), // Insert at the end of the menu is null is passed. Default behaviour. 110 array( 0, 0 ), // Insert at the beginning of the menu if 0 is passed. 111 array( -1, 0 ), // Negative numbers are treated the same as passing 0. 112 array( -7, 0 ), // Negative numbers are treated the same as passing 0. 113 array( 1, 1 ), // Insert as the second item. 114 array( 3, 3 ), // Insert as the 4th item. 115 array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end. 116 array( 123456, $menu_count ) // Numbers higher than the number of items are added at the end. 117 ); 118 } 119 60 120 function test_is_plugin_active_true() { 61 121 activate_plugin( 'hello.php' ); 62 122 $test = is_plugin_active( 'hello.php' );