<!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>[13344] trunk: Some nav menu API.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13344">13344</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-02-23 22:06:21 +0000 (Tue, 23 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Some nav menu API. see <a href="http://trac.wordpress.org/ticket/11817">#11817</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincustomnavigationphp">trunk/wp-admin/custom-navigation.php</a></li>
<li><a href="#trunkwpincludescustomnavigationphp">trunk/wp-includes/custom-navigation.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincustomnavigationphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/custom-navigation.php (13343 => 13344)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/custom-navigation.php        2010-02-23 22:04:29 UTC (rev 13343)
+++ trunk/wp-admin/custom-navigation.php        2010-02-23 22:06:21 UTC (rev 13344)
</span><span class="lines">@@ -29,7 +29,7 @@
</span><span class="cx"> require_once('admin-header.php');
</span><span class="cx"> require_once (ABSPATH . WPINC . '/custom-navigation.php');
</span><span class="cx">
</span><del>-function wp_custom_nav_reset() {
</del><ins>+function wp_reset_nav_menu() {
</ins><span class="cx">         wp_custom_navigation_setup(true);
</span><span class="cx">
</span><span class="cx">         return true;
</span><span class="lines">@@ -77,20 +77,15 @@
</span><span class="cx">         $insert_menu_name = $_POST['add_menu_name'];
</span><span class="cx">
</span><span class="cx">         if ( $insert_menu_name != '' ) {
</span><del>-                $existing_term = get_term_by( 'name', $insert_menu_name, 'nav_menu' );
-                if ( $existing_term ) {
-                        $messagesdiv = '<div id="message" class="error fade below-h2"><p>' . esc_html( sprintf( e__('A menu named "%s" already exists; please try another name.'), $existing_term->name ) ) . '</p></div>';
</del><ins>+                $menu = wp_create_nav_menu($insert_menu_name);
+                if ( is_wp_error($menu) ) {
+                        $messagesdiv = '<div id="message" class="error fade below-h2"><p>' . esc_html( $menu->get_error_message() ) . '</p></div>';
</ins><span class="cx">                 } else {
</span><del>-                        $term = wp_insert_term( $insert_menu_name, 'nav_menu' );
-                        if ( !is_wp_error($term) ) {
-                                $term = get_term($term['term_id'], 'nav_menu');
-                                $custom_menus[$term->term_id] = $term;
-                                $menu_selected_id = $term->term_id;
-                                $menu_id_in_edit = $menu_selected_id;
-                                $messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $term->name ) ) . '</p></div>';
-
-                                $postCounter = 0;
-                        }
</del><ins>+                        $custom_menus[$menu->term_id] = $menu;
+                        $menu_selected_id = $menu->term_id;
+                        $menu_id_in_edit = $menu_selected_id;
+                        $messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . esc_html( sprintf( __('"%s" menu has been created!'), $menu->name ) ) . '</p></div>';
+                        $postCounter = 0;
</ins><span class="cx">                 }
</span><span class="cx">         } else {
</span><span class="cx">                 $messagesdiv = '<div id="message" class="error fade below-h2"><p>' . __('Please enter a valid menu name.') . '</p></div>';
</span><span class="lines">@@ -98,7 +93,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> if ( isset($_POST['reset_wp_menu']) ) {
</span><del>-        $success = wp_custom_nav_reset();
</del><ins>+        $success = wp_reset_nav_menu();
</ins><span class="cx">         if ( $success ) {
</span><span class="cx">                 // DISPLAY SUCCESS MESSAGE IF Menu Reset Correctly
</span><span class="cx">                 $messagesdiv = '<div id="message" class="updated fade below-h2"><p>' . __('The menu has been reset.') . '</p></div>';
</span><span class="lines">@@ -110,8 +105,7 @@
</span><span class="cx">                 $messagesdiv = '<div id="message" class="error fade below-h2"><p>' . __('The menu could not be reset. Please try again.') . '</p></div>';
</span><span class="cx">         }
</span><span class="cx"> } elseif ( $postCounter > 0 && $menu_selected_id > 0 ) {
</span><del>-        $menu_objects = get_objects_in_term( $menu_selected_id, 'nav_menu' );
-        $menu_items = wp_custom_navigation_get_menu_items( $menu_objects );
</del><ins>+        $menu_items = wp_get_nav_menu_items( $menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID') );
</ins><span class="cx">
</span><span class="cx">         // Loop through all POST variables
</span><span class="cx">         for ( $k = 1; $k <= $postCounter; $k++ ) {
</span></span></pre></div>
<a id="trunkwpincludescustomnavigationphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/custom-navigation.php (13343 => 13344)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/custom-navigation.php        2010-02-23 22:04:29 UTC (rev 13343)
+++ trunk/wp-includes/custom-navigation.php        2010-02-23 22:06:21 UTC (rev 13344)
</span><span class="lines">@@ -10,53 +10,74 @@
</span><span class="cx"> * @subpackage Administration
</span><span class="cx"> */
</span><span class="cx">
</span><del>-function wp_custom_navigation_get_menu_items( $menu_objects, $key = 'ID' ) {
-        $menu_items = array();
-        if ( !empty( $menu_objects ) && !empty( $key ) ) {
-                $args = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish' );
-                if ( count( $menu_objects ) > 1 )
-                        $args['include'] = implode( ',', $menu_objects );
-                else
-                        $args['include'] = $menu_objects[0];
-                $posts = get_posts( $args );
-                if ( ! empty( $posts ) ) {
-                        foreach ( $posts as $post ) {
-                                $menu_items[ $post->$key ] = $post;
-                        }
-                }
-                unset( $posts );
-                ksort( $menu_items );
-        }
-        return $menu_items;
-}
-
</del><span class="cx"> function wp_custom_navigation_setup($override = false) {
</span><span class="cx">         // Custom Navigation Menu Setup
</span><span class="cx">
</span><span class="cx">         // Override for menu descriptions
</span><span class="cx">         update_option('wp_settings_custom_nav_advanced_options', 'yes');
</span><span class="cx">
</span><del>-        $custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
-         if ( !empty( $custom_menus ) ) {
-                foreach ( $custom_menus as $menu ) {
-                        wp_custom_navigation_delete_menu( $menu->term_id );
</del><ins>+        $menus = wp_get_nav_menus();
+         if ( !empty( $menus ) ) {
+                foreach ( $menus as $menu ) {
+                        wp_delete_nav_menu( $menu->term_id );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+
+        wp_create_nav_menu( __('Main') );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-function wp_custom_navigation_delete_menu( $menu_term_id ) {
-        $term_id = (int) $menu_term_id;
-        if ( $term_id > 0 ) {
-                $menu_objects = get_objects_in_term( $term_id, 'nav_menu' );
-                if ( !empty( $menu_objects ) ) {
-                        foreach ( $menu_objects as $item ) {
-                                wp_delete_post( $item );
-                        }
</del><ins>+function wp_delete_nav_menu( $menu_id ) {
+        $menu_id = (int) $menu_id;
+        if ( !$menu_id )
+                return false;
+
+        $menu_objects = get_objects_in_term( $menu_id, 'nav_menu' );
+        if ( !empty( $menu_objects ) ) {
+                foreach ( $menu_objects as $item ) {
+                        wp_delete_post( $item );
</ins><span class="cx">                 }
</span><del>-                wp_delete_term( $term_id, 'nav_menu' );
</del><span class="cx">         }
</span><ins>+        wp_delete_term( $menu_id, 'nav_menu' );
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+function wp_create_nav_menu( $menu_name ) {
+        $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
+
+        if ( $menu_exists )
+                return WP_Error('menu_exists', sprintf( __('A menu named "%s" already exists; please try another name.'), $menu_exists->name ));
+
+        return wp_insert_term( $menu_name, 'nav_menu' );
+}
+
+function wp_get_nav_menus() {
+        return get_terms( 'nav_menu', array( 'hide_empty' => false ) );
+}
+
+function wp_get_nav_menu_items( $menu, $args = array() ) {
+        $items = get_objects_in_term( (int) $menu, 'nav_menu' );
+
+        $defaults = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order' );
+        $args = wp_parse_args($args, $defaults);
+        if ( count( $items ) > 1 )        
+                $args['include'] = implode( ',', $items );
+        else
+                $args['include'] = $items[0];
+
+        $items = get_posts( $args );
+
+        if ( ARRAY_A == $args['output'] ) {
+                $output = array();
+                foreach ( $items as $item ) {
+                        $output[$item->$args['output_key']] = $item;
+                }
+                unset($items);
+                ksort($output);
+                return $output;
+        } else {
+                return $items;
+        }
+}
+
</ins><span class="cx"> function setup_menu_item($menu_item, $type = 'item', $position = 0) {
</span><span class="cx">         global $parent_menu_order;
</span><span class="cx">
</span><span class="lines">@@ -263,14 +284,13 @@
</span><span class="cx">                 $args = wp_parse_args($args, $defaults);
</span><span class="cx">                 extract($args, EXTR_SKIP);
</span><span class="cx">
</span><del>-                $menu_objects = get_objects_in_term( $id, 'nav_menu' );
-                $menu_items = wp_custom_navigation_get_menu_items( $menu_objects, 'menu_order' );
</del><ins>+                $menu_items = wp_get_nav_menu_items( $id );
</ins><span class="cx">
</span><span class="cx">                 $parent_stack = array();
</span><span class="cx">                 $current_parent = 0;
</span><span class="cx">                 $parent_menu_order = array();
</span><span class="cx">                 // Setup parentage
</span><del>-                foreach ( $menu_items as $key => $menu_item ) {
</del><ins>+                foreach ( $menu_items as $menu_item ) {
</ins><span class="cx">                         $parent_menu_order[ $menu_item->ID ] = $menu_item->menu_order;
</span><span class="cx">                 }
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>