Ticket #39776: 39776.9.diff
File 39776.9.diff, 17.4 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..e247d24e57 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, append the submenu. 1380 if ( $position >= count( $submenu[ $parent_slug ] ) ) { 1381 $submenu[ $parent_slug ][] = $new_sub_menu; 1382 } else { 1383 // Test for a negative position. 1384 $position = max( $position, 0 ); 1385 if ( 0 === $position ) { 1386 // For negative or `0` positions, prepend the submenu. 1387 array_unshift( $submenu[ $parent_slug ], $new_sub_menu ); 1388 } else { 1389 // Grab all of the items before the insertion point. 1390 $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true ); 1391 // Grab all of the items after the insertion point. 1392 $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true ); 1393 // Add the new item. 1394 $before_items[] = $new_sub_menu; 1395 // Merge the items. 1396 $submenu[ $parent_slug ] = array_merge( $before_items, $after_items ); 1397 } 1398 } 1399 } 1400 // Sort the parent array 1401 ksort( $submenu[ $parent_slug ] ); 1374 1402 1375 1403 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug ); 1376 1404 if ( ! empty( $function ) && ! empty( $hookname ) ) { … … function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 1409 1437 * @param string $capability The capability required for this menu to be displayed to the user. 1410 1438 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1411 1439 * @param callable $function The function to be called to output the content for this page. 1440 * @param int $position The position in the menu order this one should appear. 1412 1441 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1413 1442 */ 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 );1443 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1444 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1416 1445 } 1417 1446 1418 1447 /** … … function add_management_page( $page_title, $menu_title, $capability, $menu_slug, 1431 1460 * @param string $capability The capability required for this menu to be displayed to the user. 1432 1461 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1433 1462 * @param callable $function The function to be called to output the content for this page. 1463 * @param int $position The position in the menu order this one should appear. 1434 1464 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1435 1465 */ 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 );1466 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1467 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1438 1468 } 1439 1469 1440 1470 /** … … function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $f 1453 1483 * @param string $capability The capability required for this menu to be displayed to the user. 1454 1484 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1455 1485 * @param callable $function The function to be called to output the content for this page. 1486 * @param int $position The position in the menu order this one should appear. 1456 1487 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1457 1488 */ 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 );1489 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1490 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1460 1491 } 1461 1492 1462 1493 /** … … function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1475 1506 * @param string $capability The capability required for this menu to be displayed to the user. 1476 1507 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1477 1508 * @param callable $function The function to be called to output the content for this page. 1509 * @param int $position The position in the menu order this one should appear. 1478 1510 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1479 1511 */ 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 );1512 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1513 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1482 1514 } 1483 1515 1484 1516 /** … … function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $f 1497 1529 * @param string $capability The capability required for this menu to be displayed to the user. 1498 1530 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1499 1531 * @param callable $function The function to be called to output the content for this page. 1532 * @param int $position The position in the menu order this one should appear. 1500 1533 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1501 1534 */ 1502 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 1503 if ( current_user_can( 'edit_users' ) ) { 1535 1536 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1537 if ( current_user_can('edit_users') ) { 1504 1538 $parent = 'users.php'; 1505 1539 } else { 1506 1540 $parent = 'profile.php'; 1507 1541 } 1508 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );1542 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1509 1543 } 1510 1544 /** 1511 1545 * Add submenu page to the Dashboard main menu. … … function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1523 1557 * @param string $capability The capability required for this menu to be displayed to the user. 1524 1558 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1525 1559 * @param callable $function The function to be called to output the content for this page. 1560 * @param int $position The position in the menu order this one should appear. 1526 1561 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1527 1562 */ 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 );1563 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1564 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1530 1565 } 1531 1566 1532 1567 /** … … function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, 1545 1580 * @param string $capability The capability required for this menu to be displayed to the user. 1546 1581 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1547 1582 * @param callable $function The function to be called to output the content for this page. 1583 * @param int $position The position in the menu order this one should appear. 1548 1584 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1549 1585 */ 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 );1586 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1587 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1552 1588 } 1553 1589 1554 1590 /** … … function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1567 1603 * @param string $capability The capability required for this menu to be displayed to the user. 1568 1604 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1569 1605 * @param callable $function The function to be called to output the content for this page. 1606 * @param int $position The position in the menu order this one should appear. 1570 1607 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1571 1608 */ 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 );1609 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1610 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1574 1611 } 1575 1612 1576 1613 /** … … function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1589 1626 * @param string $capability The capability required for this menu to be displayed to the user. 1590 1627 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1591 1628 * @param callable $function The function to be called to output the content for this page. 1629 * @param int $position The position in the menu order this one should appear. 1592 1630 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1593 1631 */ 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 );1632 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1633 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1596 1634 } 1597 1635 1598 1636 /** … … function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1611 1649 * @param string $capability The capability required for this menu to be displayed to the user. 1612 1650 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1613 1651 * @param callable $function The function to be called to output the content for this page. 1652 * @param int $position The position in the menu order this one should appear. 1614 1653 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1615 1654 */ 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 1655 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1656 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position); 1618 1657 } 1619 1658 1620 1659 /** … … function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $fun 1633 1672 * @param string $capability The capability required for this menu to be displayed to the user. 1634 1673 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). 1635 1674 * @param callable $function The function to be called to output the content for this page. 1675 * @param int $position The position in the menu order this one should appear. 1636 1676 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. 1637 1677 */ 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 );1678 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { 1679 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); 1640 1680 } 1641 1681 1642 1682 /** -
tests/phpunit/tests/admin/includesPlugin.php
diff --git tests/phpunit/tests/admin/includesPlugin.php tests/phpunit/tests/admin/includesPlugin.php index 916ca755b7..714c60e97a 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 * @ticket 39776 64 * @covers ::add_submenu_page 65 * 66 * @param int $priority The position of the new item. 67 * @param int $expected_position Where the new item is expected to appear. 68 * @dataProvider data_submenu_priority 69 */ 70 function test_submenu_priority( $priority, $expected_position ) { 71 global $submenu; 72 $current_user = get_current_user_id(); 73 $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) ); 74 wp_set_current_user( $admin_user ); 75 set_current_screen( 'dashboard' ); 76 77 // Setup a menu with some items. 78 $parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' ); 79 foreach ( $this->submenus_to_add() as $menu ) { 80 add_submenu_page( $parent, $menu[0], $menu[1], $menu[2], $menu[3], $menu[4] ); 81 } 82 83 // Insert the new page. 84 add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos' , $priority ); 85 wp_set_current_user( $current_user ); 86 87 // Clean up the temporary user. 88 wp_delete_user( $admin_user ); 89 90 $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] ); 91 } 92 93 /** 94 * Helper to store the menus to add so getting the length is programmatically done. 95 * @since 5.3.0 96 * 97 * @return array 98 */ 99 function submenus_to_add() { 100 return array( 101 array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ), 102 array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ), 103 array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ), 104 array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ), 105 array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ), 106 ); 107 } 108 /** 109 * Data provider for the above priority parameter tests. 110 * @return array 111 */ 112 function data_submenu_priority() { 113 $menu_count = count( $this->submenus_to_add() ); 114 return array( 115 array( null, $menu_count ), // Insert at the end of the menu if null is passed. Default behaviour. 116 array( 0, 0 ), // Insert at the beginning of the menu if 0 is passed. 117 array( -1, 0 ), // Negative numbers are treated the same as passing 0. 118 array( -7, 0 ), // Negative numbers are treated the same as passing 0. 119 array( 1, 1 ), // Insert as the second item. 120 array( 3, 3 ), // Insert as the 4th item. 121 array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end. 122 array( 123456, $menu_count ) // Numbers higher than the number of items are added at the end. 123 ); 124 } 125 60 126 function test_is_plugin_active_true() { 61 127 activate_plugin( 'hello.php' ); 62 128 $test = is_plugin_active( 'hello.php' );