<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[13579] trunk/wp-admin: Fix submenus for post types.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13579">13579</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-03-04 00:15:55 +0000 (Thu, 04 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Fix submenus for post types. Props TobiasBg. see <a href="http://trac.wordpress.org/ticket/12453">#12453</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminadminphp">trunk/wp-admin/admin.php</a></li>
<li><a href="#trunkwpadminincludespluginphp">trunk/wp-admin/includes/plugin.php</a></li>
<li><a href="#trunkwpadminincludestemplatephp">trunk/wp-admin/includes/template.php</a></li>
<li><a href="#trunkwpadminmenuheaderphp">trunk/wp-admin/menu-header.php</a></li>
<li><a href="#trunkwpadminmenuphp">trunk/wp-admin/menu.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminadminphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/admin.php (13578 => 13579)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/admin.php        2010-03-03 23:07:23 UTC (rev 13578)
+++ trunk/wp-admin/admin.php        2010-03-04 00:15:55 UTC (rev 13579)
</span><span class="lines">@@ -72,17 +72,26 @@
</span><span class="cx"> 
</span><span class="cx"> $editing = false;
</span><span class="cx"> 
</span><del>-if (isset($_GET['page'])) {
</del><ins>+if ( isset($_GET['page']) ) {
</ins><span class="cx">         $plugin_page = stripslashes($_GET['page']);
</span><span class="cx">         $plugin_page = plugin_basename($plugin_page);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+if ( isset($_GET['post_type']) )
+        $typenow = sanitize_user($_GET['post_type'], true);
+else
+        $typenow = '';
+
</ins><span class="cx"> require(ABSPATH . 'wp-admin/menu.php');
</span><span class="cx"> 
</span><span class="cx"> do_action('admin_init');
</span><span class="cx"> 
</span><del>-if (isset($plugin_page) ) {
-        if ( ! $page_hook = get_plugin_page_hook($plugin_page, $pagenow) ) {
</del><ins>+if ( isset($plugin_page) ) {
+        if ( !empty($typenow) )
+                $the_parent = $pagenow . '?post_type=' . $typenow;
+        else
+                $the_parent = $pagenow;
+        if ( ! $page_hook = get_plugin_page_hook($plugin_page, $the_parent) ) {
</ins><span class="cx">                 $page_hook = get_plugin_page_hook($plugin_page, $plugin_page);
</span><span class="cx">                 // backwards compatibility for plugins using add_management_page
</span><span class="cx">                 if ( empty( $page_hook ) &amp;&amp; 'edit.php' == $pagenow &amp;&amp; '' != get_plugin_page_hook($plugin_page, 'tools.php') ) {
</span><span class="lines">@@ -95,6 +104,7 @@
</span><span class="cx">                         exit;
</span><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+        unset($the_parent);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> $hook_suffix = '';
</span><span class="lines">@@ -105,12 +115,6 @@
</span><span class="cx"> else if ( isset($pagenow) )
</span><span class="cx">         $hook_suffix = $pagenow;
</span><span class="cx"> 
</span><del>-if ( isset($_GET['post_type']) )
-        $typenow = $_GET['post_type'];
-else
-        $typenow = '';
-// @todo validate typenow against post types.
-
</del><span class="cx"> set_current_screen();
</span><span class="cx"> 
</span><span class="cx"> // Handle plugin admin pages.
</span></span></pre></div>
<a id="trunkwpadminincludespluginphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/plugin.php (13578 => 13579)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/plugin.php        2010-03-03 23:07:23 UTC (rev 13578)
+++ trunk/wp-admin/includes/plugin.php        2010-03-04 00:15:55 UTC (rev 13579)
</span><span class="lines">@@ -817,6 +817,7 @@
</span><span class="cx">         $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
</span><span class="cx"> 
</span><span class="cx">         $hookname = get_plugin_page_hookname( $menu_slug, '' );
</span><ins>+
</ins><span class="cx">         if (!empty ( $function ) &amp;&amp; !empty ( $hookname ) &amp;&amp; current_user_can( $capability ) )
</span><span class="cx">                 add_action( $hookname, $function );
</span><span class="cx"> 
</span><span class="lines">@@ -1167,6 +1168,7 @@
</span><span class="cx">         global $menu;
</span><span class="cx">         global $submenu;
</span><span class="cx">         global $pagenow;
</span><ins>+        global $typenow;
</ins><span class="cx">         global $plugin_page;
</span><span class="cx">         global $_wp_real_parent_file;
</span><span class="cx">         global $_wp_menu_nopriv;
</span><span class="lines">@@ -1215,9 +1217,12 @@
</span><span class="cx">                 foreach ( $submenu[$parent] as $submenu_array ) {
</span><span class="cx">                         if ( isset( $_wp_real_parent_file[$parent] ) )
</span><span class="cx">                                 $parent = $_wp_real_parent_file[$parent];
</span><del>-                        if ( $submenu_array[2] == $pagenow &amp;&amp; ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
</del><ins>+                        if ( !empty($typenow) &amp;&amp; ($submenu_array[2] == &quot;$pagenow?post_type=$typenow&quot;) ) {
</ins><span class="cx">                                 $parent_file = $parent;
</span><span class="cx">                                 return $parent;
</span><ins>+                        } elseif ( $submenu_array[2] == $pagenow &amp;&amp; empty($typenow) &amp;&amp; ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
+                                $parent_file = $parent;
+                                return $parent;
</ins><span class="cx">                         } else
</span><span class="cx">                                 if ( isset( $plugin_page ) &amp;&amp; ($plugin_page == $submenu_array[2] ) ) {
</span><span class="cx">                                         $parent_file = $parent;
</span><span class="lines">@@ -1326,6 +1331,7 @@
</span><span class="cx">                 $page_type = $admin_page_hooks[$parent];
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx">         $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
</span><span class="cx"> 
</span><span class="cx">         return $page_type.'_page_'.$plugin_name;
</span><span class="lines">@@ -1350,6 +1356,7 @@
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><span class="cx">                 $hookname = get_plugin_page_hookname($plugin_page, $parent);
</span><ins>+
</ins><span class="cx">                 if ( !isset($_registered_pages[$hookname]) )
</span><span class="cx">                         return false;
</span><span class="cx">         }
</span></span></pre></div>
<a id="trunkwpadminincludestemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/template.php (13578 => 13579)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/template.php        2010-03-03 23:07:23 UTC (rev 13578)
+++ trunk/wp-admin/includes/template.php        2010-03-04 00:15:55 UTC (rev 13579)
</span><span class="lines">@@ -3947,8 +3947,6 @@
</span><span class="cx">                         $typenow = 'post';
</span><span class="cx">                 $current_screen-&gt;id = $typenow;
</span><span class="cx">                 $current_screen-&gt;post_type = $typenow;
</span><del>-        } else {
-                $typenow = '';
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         $current_screen = apply_filters('current_screen', $current_screen);
</span></span></pre></div>
<a id="trunkwpadminmenuheaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/menu-header.php (13578 => 13579)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/menu-header.php        2010-03-03 23:07:23 UTC (rev 13578)
+++ trunk/wp-admin/menu-header.php        2010-03-04 00:15:55 UTC (rev 13579)
</span><span class="lines">@@ -33,7 +33,7 @@
</span><span class="cx">  * @param bool $submenu_as_parent
</span><span class="cx">  */
</span><span class="cx"> function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
</span><del>-        global $self, $parent_file, $submenu_file, $plugin_page, $pagenow;
</del><ins>+        global $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $typenow;
</ins><span class="cx"> 
</span><span class="cx">         $first = true;
</span><span class="cx">         // 0 = name, 1 = capability, 2 = file, 3 = class, 4 = id, 5 = icon src
</span><span class="lines">@@ -47,7 +47,7 @@
</span><span class="cx">                 if ( !empty($submenu[$item[2]]) )
</span><span class="cx">                         $class[] = 'wp-has-submenu';
</span><span class="cx"> 
</span><del>-                if ( ( $parent_file &amp;&amp; $item[2] == $parent_file ) || ( false === strpos($parent_file, '?') &amp;&amp; strcmp($self, $item[2]) == 0 ) ) {
</del><ins>+                if ( ( $parent_file &amp;&amp; $item[2] == $parent_file ) || ( false === strpos($parent_file, '?') &amp;&amp; $self == $item[2] ) ) {
</ins><span class="cx">                         if ( !empty($submenu[$item[2]]) )
</span><span class="cx">                                 $class[] = 'wp-has-current-submenu wp-menu-open';
</span><span class="cx">                         else
</span><span class="lines">@@ -112,15 +112,22 @@
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><span class="cx">                                 $menu_file = $item[2];
</span><ins>+
</ins><span class="cx">                                 if ( false !== $pos = strpos($menu_file, '?') )
</span><span class="cx">                                         $menu_file = substr($menu_file, 0, $pos);
</span><span class="cx"> 
</span><ins>+                                // Handle current for post_type=post|page|foo pages, which won't match $self.
+                                if ( !empty($typenow) )
+                                        $self_type = $self . '?post_type=' . $typenow;
+                                else
+                                        $self_type = 'nothing';
+
</ins><span class="cx">                                 if ( isset($submenu_file) ) {
</span><span class="cx">                                         if ( $submenu_file == $sub_item[2] )
</span><span class="cx">                                                 $class[] = 'current';
</span><span class="cx">                                 // If plugin_page is set the parent must either match the current page or not physically exist.
</span><span class="cx">                                 // This allows plugin pages with the same hook to exist under different parents.
</span><del>-                                } else if ( (isset($plugin_page) &amp;&amp; $plugin_page == $sub_item[2] &amp;&amp; (!file_exists($menu_file) || ($item[2] == $self))) || (!isset($plugin_page) &amp;&amp; $self == $sub_item[2]) ) {
</del><ins>+                                } else if ( (isset($plugin_page) &amp;&amp; $plugin_page == $sub_item[2] &amp;&amp; (!file_exists($menu_file) || ($item[2] == $self) || ($item[2] == $self_type))) || (!isset($plugin_page) &amp;&amp; $self == $sub_item[2]) ) {
</ins><span class="cx">                                         $class[] = 'current';
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><span class="lines">@@ -133,14 +140,11 @@
</span><span class="cx"> 
</span><span class="cx">                                 if ( ( ('index.php' != $sub_item[2]) &amp;&amp; file_exists(WP_PLUGIN_DIR . &quot;/$sub_file&quot;) ) || ! empty($menu_hook) ) {
</span><span class="cx">                                         // If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
</span><del>-
-                                        $parent_exists = (!$admin_is_parent &amp;&amp; file_exists(WP_PLUGIN_DIR . &quot;/$menu_file&quot;) &amp;&amp; !is_dir(WP_PLUGIN_DIR . &quot;/{$item[2]}&quot;) ) || file_exists($menu_file);
-                                        if ( $parent_exists )
-                                                echo &quot;&lt;li$class&gt;&lt;a href='{$item[2]}?page={$sub_item[2]}'$class$tabindex&gt;{$sub_item[0]}&lt;/a&gt;&lt;/li&gt;&quot;;
-                                        elseif ( 'admin.php' == $pagenow || !$parent_exists )
-                                                echo &quot;&lt;li$class&gt;&lt;a href='admin.php?page={$sub_item[2]}'$class$tabindex&gt;{$sub_item[0]}&lt;/a&gt;&lt;/li&gt;&quot;;
</del><ins>+                                        if ( (!$admin_is_parent &amp;&amp; file_exists(WP_PLUGIN_DIR . &quot;/$menu_file&quot;) &amp;&amp; !is_dir(WP_PLUGIN_DIR . &quot;/{$item[2]}&quot;)) || file_exists($menu_file) )
+                                                $sub_item_url = add_query_arg( array('page' =&gt; $sub_item[2]), $item[2] );
</ins><span class="cx">                                         else
</span><del>-                                                echo &quot;&lt;li$class&gt;&lt;a href='{$item[2]}?page={$sub_item[2]}'$class$tabindex&gt;{$sub_item[0]}&lt;/a&gt;&lt;/li&gt;&quot;;
</del><ins>+                                                $sub_item_url = add_query_arg( array('page' =&gt; $sub_item[2]), 'admin.php' );
+                                        echo &quot;&lt;li$class&gt;&lt;a href='$sub_item_url'$class$tabindex&gt;{$sub_item[0]}&lt;/a&gt;&lt;/li&gt;&quot;;
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         echo &quot;&lt;li$class&gt;&lt;a href='{$sub_item[2]}'$class$tabindex&gt;{$sub_item[0]}&lt;/a&gt;&lt;/li&gt;&quot;;
</span><span class="cx">                                 }
</span></span></pre></div>
<a id="trunkwpadminmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/menu.php (13578 => 13579)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/menu.php        2010-03-03 23:07:23 UTC (rev 13578)
+++ trunk/wp-admin/menu.php        2010-03-04 00:15:55 UTC (rev 13579)
</span><span class="lines">@@ -177,15 +177,31 @@
</span><span class="cx"> 
</span><span class="cx"> // Create list of page plugin hook names.
</span><span class="cx"> foreach ($menu as $menu_page) {
</span><del>-        $hook_name = sanitize_title(basename($menu_page[2], '.php'));
</del><ins>+        if ( false !== $pos = strpos($menu_page[2], '?') ) {
+                // Handle post_type=post|page|foo pages.
+                $hook_name = substr($menu_page[2], 0, $pos);
+                $hook_args = substr($menu_page[2], $pos + 1);
+                wp_parse_str($hook_args, $hook_args);
+                // Set the hook name to be the post type.
+                if ( isset($hook_args['post_type']) )
+                        $hook_name = $hook_args['post_type'];
+                else
+                        $hook_name = basename($hook_name, '.php');
+                unset($hook_args);
+        } else {
+                $hook_name = basename($menu_page[2], '.php');
+        }
+        $hook_name = sanitize_title($hook_name);
</ins><span class="cx"> 
</span><span class="cx">         // ensure we're backwards compatible
</span><span class="cx">         $compat = array(
</span><span class="cx">                 'index' =&gt; 'dashboard',
</span><span class="cx">                 'edit' =&gt; 'posts',
</span><ins>+                'post' =&gt; 'posts',
</ins><span class="cx">                 'upload' =&gt; 'media',
</span><span class="cx">                 'link-manager' =&gt; 'links',
</span><span class="cx">                 'edit-pages' =&gt; 'pages',
</span><ins>+                'page' =&gt; 'pages',
</ins><span class="cx">                 'edit-comments' =&gt; 'comments',
</span><span class="cx">                 'options-general' =&gt; 'settings',
</span><span class="cx">                 'themes' =&gt; 'appearance',
</span></span></pre>
</div>
</div>

</body>
</html>