<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[46197] trunk: Menus: add a position argument to `add_submenu_page` and the helper functions that use it.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/46197">46197</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/46197","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>adamsilverstein</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-09-20 14:59:04 +0000 (Fri, 20 Sep 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Menus: add a position argument to `add_submenu_page` and the helper functions that use it.
Add a position argument to the `add_submenu_page` function similar to the one already in `add_menu_page`. When adding sub menus enables setting the position in the sub menu where the item should appear.
In addition, add the position argument to functions that call `add_submenu_page` under the hood: `add_management_page`, `add_options_page`, `add_theme_page`, `add_plugins_page`, `add_users_page`, `add_dashboard_page`, `add_posts_page`, `add_media_page`, `add_links_page`, `add_pages_page` and `add_comments_page`.
Props welcher, birgire, alexvorn2.
Fixes <a href="https://core.trac.wordpress.org/ticket/39776">#39776</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludespluginphp">trunk/src/wp-admin/includes/plugin.php</a></li>
<li><a href="#trunktestsphpunittestsadminincludesPluginphp">trunk/tests/phpunit/tests/admin/includesPlugin.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludespluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/plugin.php 2019-09-20 13:04:50 UTC (rev 46196)
+++ trunk/src/wp-admin/includes/plugin.php 2019-09-20 14:59:04 UTC (rev 46197)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1267,7 +1267,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * * Pass the name of a Dashicons helper class to use a font icon,
</span><span class="cx" style="display: block; padding: 0 10px"> * e.g. 'dashicons-chart-pie'.
</span><span class="cx" style="display: block; padding: 0 10px"> * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $position The position in the menu order this one should appear.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string The resulting page's hook_suffix.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1338,9 +1338,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * and only include lowercase alphanumeric, dashes, and underscores characters
</span><span class="cx" style="display: block; padding: 0 10px"> * to be compatible with sanitize_key().
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
</span><span class="cx" style="display: block; padding: 0 10px"> $_registered_pages, $_parent_pages;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1370,7 +1372,33 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $submenu[ $parent_slug ][] = array( $menu_title, $capability, $menu_slug, $page_title );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title );
+ if ( null === $position ) {
+ $submenu[ $parent_slug ][] = $new_sub_menu;
+ } else {
+ // If position is equal or higher than the number of items in the array, append the submenu.
+ if ( $position >= count( $submenu[ $parent_slug ] ) ) {
+ $submenu[ $parent_slug ][] = $new_sub_menu;
+ } else {
+ // Test for a negative position.
+ $position = max( $position, 0 );
+ if ( 0 === $position ) {
+ // For negative or `0` positions, prepend the submenu.
+ array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
+ } else {
+ // Grab all of the items before the insertion point.
+ $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
+ // Grab all of the items after the insertion point.
+ $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
+ // Add the new item.
+ $before_items[] = $new_sub_menu;
+ // Merge the items.
+ $submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
+ }
+ }
+ }
+ // Sort the parent array
+ ksort( $submenu[ $parent_slug ] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $function ) && ! empty( $hookname ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1409,10 +1437,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1431,10 +1460,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1453,10 +1483,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1475,10 +1506,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1497,15 +1529,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( current_user_can( 'edit_users' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $parent = 'users.php';
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $parent = 'profile.php';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Add submenu page to the Dashboard main menu.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1523,10 +1557,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1545,10 +1580,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1567,10 +1603,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1589,10 +1626,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1611,10 +1649,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1633,10 +1672,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability The capability required for this menu to be displayed to the user.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function The function to be called to output the content for this page.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $position The position in the menu order this item should appear.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
- return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
+ return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsadminincludesPluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/admin/includesPlugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/admin/includesPlugin.php 2019-09-20 13:04:50 UTC (rev 46196)
+++ trunk/tests/phpunit/tests/admin/includesPlugin.php 2019-09-20 14:59:04 UTC (rev 46197)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -57,6 +57,208 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( $current_user );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Tests the priority parameter.
+ *
+ * @ticket 39776
+ *
+ * @covers ::add_submenu_page
+ *
+ * @param int $priority The position of the new item.
+ * @param int $expected_position Where the new item is expected to appear.
+ *
+ * @dataProvider data_submenu_priority
+ */
+ function test_submenu_priority( $priority, $expected_position ) {
+ global $submenu;
+ global $menu;
+ $current_user = get_current_user_id();
+ $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ wp_set_current_user( $admin_user );
+ set_current_screen( 'dashboard' );
+
+ // Setup a menu with some items.
+ $parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );
+ foreach ( $this->submenus_to_add() as $menu_to_add ) {
+ add_submenu_page( $parent, $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $menu_to_add[3], $menu_to_add[4] );
+ }
+
+ // Insert the new page.
+ add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos', $priority );
+ wp_set_current_user( $current_user );
+
+ // Clean up the temporary user.
+ wp_delete_user( $admin_user );
+
+ // Verify the menu was inserted at the expected position.
+ $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] );
+ }
+
+ /**
+ * Tests the priority parameter for menu helper functions.
+ *
+ * @ticket 39776
+ *
+ * @covers ::add_management_page
+ * @covers ::add_options_page
+ * @covers ::add_theme_page
+ * @covers ::add_plugins_page
+ * @covers ::add_users_page
+ * @covers ::add_dashboard_page
+ * @covers ::add_posts_page
+ * @covers ::add_media_page
+ * @covers ::add_links_page
+ * @covers ::add_pages_page
+ * @covers ::add_comments_page
+ *
+ * @param int $priority The position of the new item.
+ * @param int $expected_position Where the new item is expected to appear.
+ *
+ * @dataProvider data_submenu_priority
+ */
+ function test_submenu_helpers_priority( $priority, $expected_position ) {
+ global $submenu;
+ global $menu;
+
+ // Skip for multisite.
+ if ( is_multisite() ) {
+ return;
+ }
+
+ // Reset menus.
+ $submenu = array();
+ $menu = array();
+
+ $current_user = get_current_user_id();
+ $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ wp_set_current_user( $admin_user );
+ set_current_screen( 'dashboard' );
+
+ // Test the helper functions that use `add_submenu_page`. Each helper adds to a specific menu root.
+ $helper_functions = array(
+ array(
+ 'callback' => 'add_management_page',
+ 'menu_root' => 'tools.php',
+ ),
+ array(
+ 'callback' => 'add_options_page',
+ 'menu_root' => 'options-general.php',
+ ),
+ array(
+ 'callback' => 'add_theme_page',
+ 'menu_root' => 'themes.php',
+ ),
+ array(
+ 'callback' => 'add_plugins_page',
+ 'menu_root' => 'plugins.php',
+ ),
+ array(
+ 'callback' => 'add_users_page',
+ 'menu_root' => 'users.php',
+ ),
+ array(
+ 'callback' => 'add_dashboard_page',
+ 'menu_root' => 'index.php',
+ ),
+ array(
+ 'callback' => 'add_posts_page',
+ 'menu_root' => 'edit.php',
+ ),
+ array(
+ 'callback' => 'add_media_page',
+ 'menu_root' => 'upload.php',
+ ),
+ array(
+ 'callback' => 'add_links_page',
+ 'menu_root' => 'link-manager.php',
+ ),
+ array(
+ 'callback' => 'add_pages_page',
+ 'menu_root' => 'edit.php?post_type=page',
+ ),
+ array(
+ 'callback' => 'add_comments_page',
+ 'menu_root' => 'edit-comments.php',
+ ),
+ );
+
+ $actual_positions = array();
+
+ foreach ( $helper_functions as $helper_function ) {
+
+ // Build up demo pages on the menu root.
+ foreach ( $this->submenus_to_add() as $menu_to_add ) {
+ add_menu_page( $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $helper_function['menu_root'], $helper_function['menu_root'] );
+ }
+
+ $test = 'test_' . $helper_function['callback'];
+
+ // Call the helper function, passing the desired priority.
+ call_user_func_array( $helper_function['callback'], array( $test, $test, 'manage_options', 'custom-position', '', $priority ) );
+
+ $actual_positions[ $test ] = $submenu[ $helper_function['menu_root'] ][ $expected_position ][2];
+ }
+
+ // Clean up the temporary user.
+ wp_delete_user( $admin_user );
+
+ foreach ( $actual_positions as $test => $actual_position ) {
+ // Verify the menu was inserted at the expected position.
+ $this->assertSame( 'custom-position', $actual_position, 'Menu not inserted at the expected position with ' . $test );
+ }
+ }
+
+ /**
+ * Helper to store the menus that are to be added, so getting the length is programmatically done.
+ *
+ * @since 5.3.0
+ *
+ * @return array {
+ * @type array {
+ * @type string Page title.
+ * @type string Menu_title.
+ * @type string Capability.
+ * @type string Menu slug.
+ * @type string Function.
+ * }
+ * }
+ */
+ function submenus_to_add() {
+ return array(
+ array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ),
+ array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ),
+ array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ),
+ array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ),
+ array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ),
+ );
+ }
+
+ /**
+ * Data provider for test_submenu_helpers_priority().
+ *
+ * @since 5.3.0
+ *
+ * @return array {
+ * @type array {
+ * @type int|null Priority.
+ * @type int Expected position.
+ * }
+ * }
+ */
+ function data_submenu_priority() {
+ $menu_count = count( $this->submenus_to_add() );
+ return array(
+ array( null, $menu_count ), // Insert at the end of the menu if null is passed. Default behavior.
+ array( 0, 0 ), // Insert at the beginning of the menu if 0 is passed.
+ array( -1, 0 ), // Negative numbers are treated the same as passing 0.
+ array( -7, 0 ), // Negative numbers are treated the same as passing 0.
+ array( 1, 1 ), // Insert as the second item.
+ array( 3, 3 ), // Insert as the 4th item.
+ array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end.
+ array( 123456, $menu_count ), // Numbers higher than the number of items are added at the end.
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> function test_is_plugin_active_true() {
</span><span class="cx" style="display: block; padding: 0 10px"> activate_plugin( 'hello.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> $test = is_plugin_active( 'hello.php' );
</span></span></pre>
</div>
</div>
</body>
</html>