<!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>[18792] trunk/wp-admin/menu-header.php: _wp_menu_output() speed up and clean up.</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 { 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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/18792">18792</a></dd>
<dt>Author</dt> <dd>duck_</dd>
<dt>Date</dt> <dd>2011-09-27 10:22:58 +0000 (Tue, 27 Sep 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>_wp_menu_output() speed up and clean up. Props dragoonis. Fixes <a href="http://core.trac.wordpress.org/ticket/13662">#13662</a>.

Attempt file_exists() last and cache an array index lookup for speed up. Whitespace and appropriate parentheses for readability.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminmenuheaderphp">trunk/wp-admin/menu-header.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminmenuheaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/menu-header.php (18791 => 18792)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/menu-header.php        2011-09-27 07:36:36 UTC (rev 18791)
+++ trunk/wp-admin/menu-header.php        2011-09-27 10:22:58 UTC (rev 18792)
</span><span class="lines">@@ -44,63 +44,60 @@
</span><span class="cx">         foreach ( $menu as $key =&gt; $item ) {
</span><span class="cx">                 $admin_is_parent = false;
</span><span class="cx">                 $class = array();
</span><ins>+
</ins><span class="cx">                 if ( $first ) {
</span><span class="cx">                         $class[] = 'wp-first-item';
</span><span class="cx">                         $first = false;
</span><span class="cx">                 }
</span><del>-                if ( !empty($submenu[$item[2]]) ) {
</del><ins>+
+                $submenu_items = false;
+                if ( ! empty( $submenu[$item[2]] ) ) {
</ins><span class="cx">                         $class[] = 'wp-has-submenu';
</span><ins>+                        $submenu_items = $submenu[$item[2]];
</ins><span class="cx">                         $menu_setting_increment++;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if ( ( $parent_file &amp;&amp; $item[2] == $parent_file ) || ( empty($typenow) &amp;&amp; $self == $item[2] ) ) {
</span><del>-                        if ( !empty($submenu[$item[2]]) )
-                                $class[] = 'wp-has-current-submenu wp-menu-open';
-                        else
-                                $class[] = 'current';
</del><ins>+                        $class[] = ! empty( $submenu_items ) ? 'wp-has-current-submenu wp-menu-open' : 'current';
</ins><span class="cx">                 } else {
</span><span class="cx">                         $class[] = 'wp-not-current-submenu';
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( ! empty($item[4]) )
</del><ins>+                if ( ! empty( $item[4] ) )
</ins><span class="cx">                         $class[] = $item[4];
</span><span class="cx"> 
</span><span class="cx">                 $class = $class ? ' class=&quot;' . join( ' ', $class ) . '&quot;' : '';
</span><span class="cx">                 $tabindex = ' tabindex=&quot;1&quot;';
</span><del>-                $id = ! empty($item[5]) ? ' id=&quot;' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '&quot;' : '';
</del><ins>+                $id = ! empty( $item[5] ) ? ' id=&quot;' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '&quot;' : '';
</ins><span class="cx">                 $img = '';
</span><del>-                if ( ! empty($item[6]) ) {
-                        if ( 'div' === $item[6] )
-                                $img = '&lt;br /&gt;';
-                        else
-                                $img = '&lt;img src=&quot;' . $item[6] . '&quot; alt=&quot;&quot; /&gt;';
-                }
</del><ins>+                if ( ! empty( $item[6] ) )
+                        $img = ( 'div' === $item[6] ) ? '&lt;br /&gt;' : '&lt;img src=&quot;' . $item[6] . '&quot; alt=&quot;&quot; /&gt;';
</ins><span class="cx">                 $arrow = '&lt;div class=&quot;wp-menu-arrow&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;';
</span><span class="cx"> 
</span><del>-                $title = wptexturize($item[0]);
</del><ins>+                $title = wptexturize( $item[0] );
</ins><span class="cx"> 
</span><span class="cx">                 echo &quot;\n\t&lt;li$class$id&gt;&quot;;
</span><span class="cx"> 
</span><span class="cx">                 if ( false !== strpos( $class, 'wp-menu-separator' ) ) {
</span><span class="cx">                         echo '&lt;div class=&quot;separator&quot;&gt;&lt;/div&gt;';
</span><del>-                } elseif ( $submenu_as_parent &amp;&amp; !empty($submenu[$item[2]]) ) {
-                        $submenu[$item[2]] = array_values($submenu[$item[2]]);  // Re-index.
-                        $menu_hook = get_plugin_page_hook($submenu[$item[2]][0][2], $item[2]);
-                        $menu_file = $submenu[$item[2]][0][2];
-                        if ( false !== $pos = strpos($menu_file, '?') )
-                                $menu_file = substr($menu_file, 0, $pos);
-                        if ( ( ('index.php' != $submenu[$item[2]][0][2]) &amp;&amp; file_exists(WP_PLUGIN_DIR . &quot;/$menu_file&quot;) ) || !empty($menu_hook)) {
</del><ins>+                } elseif ( $submenu_as_parent &amp;&amp; ! empty( $submenu_items ) ) {
+                        $submenu_items = array_values( $submenu_items );  // Re-index.
+                        $menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
+                        $menu_file = $submenu_items[0][2];
+                        if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
+                                $menu_file = substr( $menu_file, 0, $pos );
+                        if ( ! empty( $menu_hook ) || ( ('index.php' != $submenu_items[0][2]) &amp;&amp; file_exists( WP_PLUGIN_DIR . &quot;/$menu_file&quot; ) ) ) {
</ins><span class="cx">                                 $admin_is_parent = true;
</span><del>-                                echo &quot;&lt;div class='wp-menu-image'&gt;&lt;a href='admin.php?page={$submenu[$item[2]][0][2]}'&gt;$img&lt;/a&gt;&lt;/div&gt;$arrow&lt;a href='admin.php?page={$submenu[$item[2]][0][2]}'$class$tabindex&gt;$title&lt;/a&gt;&quot;;
</del><ins>+                                echo &quot;&lt;div class='wp-menu-image'&gt;&lt;a href='admin.php?page={$submenu_items[0][2]}'&gt;$img&lt;/a&gt;&lt;/div&gt;$arrow&lt;a href='admin.php?page={$submenu_items[0][2]}'$class$tabindex&gt;$title&lt;/a&gt;&quot;;
</ins><span class="cx">                         } else {
</span><del>-                                echo &quot;\n\t&lt;div class='wp-menu-image'&gt;&lt;a href='{$submenu[$item[2]][0][2]}'&gt;$img&lt;/a&gt;&lt;/div&gt;$arrow&lt;a href='{$submenu[$item[2]][0][2]}'$class$tabindex&gt;$title&lt;/a&gt;&quot;;
</del><ins>+                                echo &quot;\n\t&lt;div class='wp-menu-image'&gt;&lt;a href='{$submenu_items[0][2]}'&gt;$img&lt;/a&gt;&lt;/div&gt;$arrow&lt;a href='{$submenu_items[0][2]}'$class$tabindex&gt;$title&lt;/a&gt;&quot;;
</ins><span class="cx">                         }
</span><del>-                } else if ( !empty($item[2]) &amp;&amp; current_user_can($item[1]) ) {
-                        $menu_hook = get_plugin_page_hook($item[2], 'admin.php');
</del><ins>+                } elseif ( ! empty( $item[2] ) &amp;&amp; current_user_can( $item[1] ) ) {
+                        $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
</ins><span class="cx">                         $menu_file = $item[2];
</span><del>-                        if ( false !== $pos = strpos($menu_file, '?') )
-                                $menu_file = substr($menu_file, 0, $pos);
-                        if ( ('index.php' != $item[2]) &amp;&amp; file_exists(WP_PLUGIN_DIR . &quot;/$menu_file&quot;) || !empty($menu_hook) ) {
</del><ins>+                        if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
+                                $menu_file = substr( $menu_file, 0, $pos );
+                        if ( ! empty( $menu_hook ) || ( ('index.php' != $item[2]) &amp;&amp; file_exists( WP_PLUGIN_DIR . &quot;/$menu_file&quot; ) ) ) {
</ins><span class="cx">                                 $admin_is_parent = true;
</span><span class="cx">                                 echo &quot;\n\t&lt;div class='wp-menu-image'&gt;&lt;a href='admin.php?page={$item[2]}'&gt;$img&lt;/a&gt;&lt;/div&gt;$arrow&lt;a href='admin.php?page={$item[2]}'$class$tabindex&gt;{$item[0]}&lt;/a&gt;&quot;;
</span><span class="cx">                         } else {
</span><span class="lines">@@ -108,12 +105,12 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( !empty($submenu[$item[2]]) ) {
</del><ins>+                if ( ! empty( $submenu_items ) ) {
</ins><span class="cx">                         echo &quot;\n\t&lt;div class='wp-submenu'&gt;&lt;div class='wp-submenu-wrap'&gt;&quot;;
</span><span class="cx">                         echo &quot;&lt;div class='wp-submenu-head'&gt;{$item[0]}&lt;/div&gt;&lt;ul&gt;&quot;;
</span><span class="cx">                         $first = true;
</span><del>-                        foreach ( $submenu[$item[2]] as $sub_key =&gt; $sub_item ) {
-                                if ( !current_user_can($sub_item[1]) )
</del><ins>+                        foreach ( $submenu_items as $sub_key =&gt; $sub_item ) {
+                                if ( ! current_user_can( $sub_item[1] ) )
</ins><span class="cx">                                         continue;
</span><span class="cx"> 
</span><span class="cx">                                 $class = array();
</span><span class="lines">@@ -124,21 +121,20 @@
</span><span class="cx"> 
</span><span class="cx">                                 $menu_file = $item[2];
</span><span class="cx"> 
</span><del>-                                if ( false !== $pos = strpos($menu_file, '?') )
-                                        $menu_file = substr($menu_file, 0, $pos);
</del><ins>+                                if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
+                                        $menu_file = substr( $menu_file, 0, $pos );
</ins><span class="cx"> 
</span><span class="cx">                                 // Handle current for post_type=post|page|foo pages, which won't match $self.
</span><del>-                                if ( !empty($typenow) )
-                                        $self_type = $self . '?post_type=' . $typenow;
-                                else
-                                        $self_type = 'nothing';
</del><ins>+                                $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
</ins><span class="cx"> 
</span><del>-                                if ( isset($submenu_file) ) {
-                                        if ( $submenu_file == $sub_item[2] )
-                                                $class[] = 'current';
</del><ins>+                                if ( isset( $submenu_file ) &amp;&amp; $submenu_file == $sub_item[2] ) {
+                                        $class[] = 'current';
</ins><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) || ($item[2] == $self_type))) || (!isset($plugin_page) &amp;&amp; $self == $sub_item[2]) ) {
</del><ins>+                                } else if (
+                                        ( ! isset( $plugin_page ) &amp;&amp; $self == $sub_item[2] ) ||
+                                        ( isset( $plugin_page ) &amp;&amp; $plugin_page == $sub_item[2] &amp;&amp; ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) )
+                                ) {
</ins><span class="cx">                                         $class[] = 'current';
</span><span class="cx">                                 }
</span><span class="cx"> 
</span><span class="lines">@@ -146,18 +142,19 @@
</span><span class="cx"> 
</span><span class="cx">                                 $menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
</span><span class="cx">                                 $sub_file = $sub_item[2];
</span><del>-                                if ( false !== $pos = strpos($sub_file, '?') )
</del><ins>+                                if ( false !== ( $pos = strpos( $sub_file, '?' ) ) )
</ins><span class="cx">                                         $sub_file = substr($sub_file, 0, $pos);
</span><span class="cx"> 
</span><span class="cx">                                 $title = wptexturize($sub_item[0]);
</span><span class="cx"> 
</span><del>-                                if ( ( ('index.php' != $sub_item[2]) &amp;&amp; file_exists(WP_PLUGIN_DIR . &quot;/$sub_file&quot;) ) || ! empty($menu_hook) ) {
</del><ins>+                                if ( ! empty( $menu_hook ) || ( ('index.php' != $sub_item[2]) &amp;&amp; file_exists( WP_PLUGIN_DIR . &quot;/$sub_file&quot; ) ) ) {
</ins><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><span class="cx">                                         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) )
</span><span class="cx">                                                 $sub_item_url = add_query_arg( array('page' =&gt; $sub_item[2]), $item[2] );
</span><span class="cx">                                         else
</span><span class="cx">                                                 $sub_item_url = add_query_arg( array('page' =&gt; $sub_item[2]), 'admin.php' );
</span><del>-                                        $sub_item_url = esc_url($sub_item_url);
</del><ins>+
+                                        $sub_item_url = esc_url( $sub_item_url );
</ins><span class="cx">                                         echo &quot;&lt;li$class&gt;&lt;a href='$sub_item_url'$class$tabindex&gt;$title&lt;/a&gt;&lt;/li&gt;&quot;;
</span><span class="cx">                                 } else {
</span><span class="cx">                                         echo &quot;&lt;li$class&gt;&lt;a href='{$sub_item[2]}'$class$tabindex&gt;$title&lt;/a&gt;&lt;/li&gt;&quot;;
</span></span></pre>
</div>
</div>

</body>
</html>