Esempio n. 1
0
 public function __construct($menu_term_slug)
 {
     parent::__construct();
     $menu = wp_get_nav_menu_object($menu_term_slug);
     if (!empty($menu)) {
         $this->menu = $menu;
         $nav_menu_items = wp_get_nav_menu_items($this->menu->term_id);
         cfd_tmp_dbg('nav_menu_items_raw.txt', $nav_menu_items, 'print');
         foreach ($nav_menu_items as $item) {
             $menu_item = wp_setup_nav_menu_item($item);
             $menu_item->metadata = get_metadata('post', $item->ID);
             foreach ($menu_item->metadata as $key => &$value) {
                 $value[0] = maybe_unserialize($value[0]);
             }
             if ($menu_item->type == 'post_type') {
                 $menu_item->parent = get_post($menu_item->metadata['_menu_item_object_id'][0]);
             } elseif ($menu_item->type == 'taxonomy' && (!property_exists($menu, 'object') || $menu->object != 'custom')) {
                 $menu_item->term = get_term($menu_item->metadata['_menu_item_object_id'][0], $menu_item->metadata['_menu_item_object'][0]);
             }
             $this->items[] = $menu_item;
         }
     } else {
         throw new Exception(__('Invalid menu id', 'cf-deploy') . ': ' . esc_attr($menu_term_slug));
     }
 }
Esempio n. 2
0
 /**
  * Ajax callback for our menu items
  *
  * @wordpress-action wp_ajax_buggypress_add_to_menu
  * @return void Exits the program on completion
  */
 public function ajax_add_to_menu()
 {
     check_ajax_referer('buggypress-menu', 'buggypress_nonce');
     if (empty($_POST['menu_items'])) {
         die('-1');
     }
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     $menu_items = $_POST['menu_items'];
     $item_ids = array();
     foreach ($menu_items as $item) {
         $item_ids[] = $this->add_menu_item($item);
     }
     if (is_wp_error($item_ids)) {
         die('-1');
     }
     // Set up menu items
     $output_menu_items = array();
     foreach ($item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_obj->type_label = 'BuggyPress';
             $output_menu_items[] = $menu_obj;
         }
     }
     // build the HTML output
     if (!empty($output_menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
         echo walk_nav_menu_tree($output_menu_items, 0, (object) $args);
     }
     exit;
 }
Esempio n. 3
0
 /**
  * @ticket 27113
  */
 function test_orphan_nav_menu_item()
 {
     // Create an orphan nav menu item
     $custom_item_id = wp_update_nav_menu_item(0, 0, array('menu-item-type' => 'custom', 'menu-item-title' => 'Wordpress.org', 'menu-item-link' => 'http://wordpress.org', 'menu-item-status' => 'publish'));
     // Confirm it saved properly
     $custom_item = wp_setup_nav_menu_item(get_post($custom_item_id));
     $this->assertEquals('Wordpress.org', $custom_item->title);
     // Update the orphan with an associated nav menu
     wp_update_nav_menu_item($this->menu_id, $custom_item_id, array('menu-item-title' => 'WordPress.org'));
     $menu_items = wp_get_nav_menu_items($this->menu_id);
     $custom_item = wp_filter_object_list($menu_items, array('db_id' => $custom_item_id));
     $custom_item = array_pop($custom_item);
     $this->assertEquals('WordPress.org', $custom_item->title);
 }
Esempio n. 4
0
function theme_get_list_menu($args = array())
{
    global $wp_query;
    $menu_items = wp_get_nav_menu_items($args['menu']->term_id);
    if (empty($menu_items)) {
        return '';
    }
    $home_page_id = (int) get_option('page_for_posts');
    $queried_object = $wp_query->get_queried_object();
    $queried_object_id = (int) $wp_query->queried_object_id;
    $active_ID = null;
    $IdToKey = array();
    foreach ((array) $menu_items as $key => $menu_item) {
        $IdToKey[$menu_item->ID] = $key;
        if ($menu_item->object_id == $queried_object_id && (!empty($home_page_id) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id || 'post_type' == $menu_item->type && $wp_query->is_singular || 'taxonomy' == $menu_item->type && ($wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax))) {
            $active_ID = $menu_item->ID;
        } elseif ('custom' == $menu_item->object) {
            if (theme_is_current_url($menu_item->url)) {
                $active_ID = $menu_item->ID;
            }
        }
    }
    $current_ID = $active_ID;
    while ($current_ID && isset($IdToKey[$current_ID])) {
        $activeIDs[] = $current_ID;
        $current_item =& $menu_items[$IdToKey[$current_ID]];
        $current_item->classes[] = 'active';
        $current_ID = $current_item->menu_item_parent;
    }
    $sorted_menu_items = array();
    foreach ((array) $menu_items as $key => $menu_item) {
        $sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item);
    }
    $items = array();
    foreach ($sorted_menu_items as $el) {
        $id = $el->db_id;
        $title = $el->title;
        //Jordi
        //$classes = empty($el->classes) ? array() : (array) $el->classes;
        $classes = empty($el->classes) ? array('nav') : array_merge((array) $el->classes, array('nav'));
        $active = in_array('active', $classes);
        $items[] = new theme_MenuItem(array('id' => $id, 'active' => $active, 'attr' => array('title' => strip_tags(empty($el->attr_title) ? $title : $el->attr_title), 'target' => $el->target, 'rel' => $el->xfn, 'href' => $el->url, 'class' => join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $el))), 'title' => $title, 'parent' => $el->menu_item_parent));
    }
    $walker = new theme_MenuWalker();
    $items = apply_filters('wp_nav_menu_objects', $items, $args);
    $items = $walker->walk($items, $args);
    $items = apply_filters('wp_nav_menu_items', $items, $args);
    return apply_filters('wp_nav_menu', $items, $args);
}
Esempio n. 5
0
function sugarforms_menu_items($items)
{
    //  query sugarforms
    $args = array('post_type' => 'sugarform', 'meta_key' => 'sugarform_display_menu', 'meta_value' => '1', 'meta_compare' => '=');
    $loop = new WP_Query($args);
    //  add to menu
    if ($loop->found_posts > 0) {
        foreach ($loop->posts as $post) {
            $new_item = wp_setup_nav_menu_item($post);
            $items[] = $new_item;
            //print_r($new_item);
        }
    }
    return $items;
}
Esempio n. 6
0
/**
 * Ajax handler for adding a menu item. Based on wp_ajax_add_menu_item().
 *
 * @since Menu Customizer 0.0.
 */
function menu_customizer_add_item_ajax()
{
    check_ajax_referer('customize-menus', 'customize-menu-item-nonce');
    if (!current_user_can('edit_theme_options')) {
        wp_die(-1);
    }
    require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
    $menu_item_data = (array) $_POST['menu-item'];
    $menu_id = absint($_POST['menu']);
    // Used only for display, new item is created as an orphan - menu id of 0.
    $id = 0;
    // For performance reasons, we omit some object properties from the checklist.
    // The following is a hacky way to restore them when adding non-custom items.
    // @todo: do we really need this - do we need to populate the description field here?
    if (!empty($menu_item_data['obj_type']) && 'custom' != $menu_item_data['obj_type'] && !empty($menu_item_data['id'])) {
        switch ($menu_item_data['obj_type']) {
            case 'post_type':
                $id = absint(str_replace('post-', '', $menu_item_data['id']));
                $_object = get_post($id);
                break;
            case 'taxonomy':
                $id = absint(str_replace('term-', '', $menu_item_data['id']));
                $_object = get_term($id, $menu_item_data['type']);
                break;
        }
        $_menu_items = array_map('wp_setup_nav_menu_item', array($_object));
        $_menu_item = array_shift($_menu_items);
        // Restore the missing menu item properties
        $menu_item_data['menu-item-description'] = $_menu_item->description;
    }
    // Make the "Home" item into the custom link that it actually is.
    if ('page' == $menu_item_data['type'] && 'custom' == $menu_item_data['obj_type']) {
        $menu_item_data['type'] = 'custom';
        $menu_item_data['url'] = home_url('/');
    }
    // Map data from menu customizer keys to nav-menus.php keys.
    $item_data = array('menu-item-db-id' => 0, 'menu-item-object-id' => $id, 'menu-item-object' => isset($menu_item_data['type']) ? $menu_item_data['type'] : '', 'menu-item-type' => isset($menu_item_data['obj_type']) ? $menu_item_data['obj_type'] : '', 'menu-item-title' => isset($menu_item_data['name']) ? $menu_item_data['name'] : '', 'menu-item-url' => isset($menu_item_data['url']) ? $menu_item_data['url'] : '', 'menu-item-description' => isset($menu_item_data['menu-item-description']) ? $menu_item_data['menu-item-description'] : '');
    // `wp_save_nav_menu_items` requires `menu-item-db-id` to not be set for custom items.
    if ('custom' == $item_data['menu-item-type']) {
        unset($item_data['menu-item-db-id']);
    }
    $item_ids = wp_save_nav_menu_items(0, array(0 => $item_data));
    if (is_wp_error($item_ids) || empty($item_ids)) {
        wp_die(0);
    }
    $item = get_post($item_ids[0]);
    if (!empty($item->ID)) {
        $item = wp_setup_nav_menu_item($item);
        $item->label = $item->title;
        // Don't show "(pending)" in ajax-added items.
    }
    // Output the markup for this item.
    menu_customizer_render_item_control($item, $menu_id, 0);
    wp_die();
}
 /**
  * Ajax handler is triggered when
  * something is added to the nav menu
  * @ajax
  */
 public function ajax_add_menu_item()
 {
     if (!tf_current_user_can(array('manage_options', 'edit_theme_options'), false)) {
         die('-1');
     }
     check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     // For performance reasons, we omit some object properties from the checklist.
     // The following is a hacky way to restore them when adding non-custom items.
     $menu_items_data = array();
     if ($this->request->POST('menu-item')) {
         foreach ((array) $this->request->POST('menu-item') as $menu_item_data) {
             if (!empty($menu_item_data['menu-item-type']) && 'custom' != $menu_item_data['menu-item-type'] && !empty($menu_item_data['menu-item-object-id'])) {
                 switch ($menu_item_data['menu-item-type']) {
                     case 'post_type':
                         $_object = get_post($menu_item_data['menu-item-object-id']);
                         break;
                     case 'taxonomy':
                         $_object = get_term($menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object']);
                         break;
                 }
                 $_menu_items = array_map('wp_setup_nav_menu_item', array($_object));
                 $_menu_item = array_shift($_menu_items);
                 // Restore the missing menu item properties
                 $menu_item_data['menu-item-description'] = $_menu_item->description;
             }
             $menu_items_data[] = $menu_item_data;
         }
     }
     $item_ids = wp_save_nav_menu_items(0, $menu_items_data);
     if (is_wp_error($item_ids)) {
         die('-1');
     }
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_items[] = $menu_obj;
         }
     }
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new TF_ADMIN_MENU_WALKER());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     die;
 }
Esempio n. 8
0
 function wp_ajax_add_menu_item()
 {
     check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
     if (!current_user_can('edit_theme_options')) {
         wp_die(-1);
     }
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     $menu_items_data = array();
     $search_keys = array();
     foreach ((array) $_POST['menu-item'] as $k => $menu_item_data) {
         if (!isset($menu_item_data['menu-item-type']) || $menu_item_data['menu-item-type'] !== 'search') {
             continue;
         }
         $menu_item_data['menu-item-description'] = _x('Search box', 'menu-item-description', 'bop-nav-search-box-item');
         $menu_items_data[] = $menu_item_data;
         $search_keys[] = $k;
     }
     foreach ($search_keys as $k) {
         unset($_POST['menu-item'][$k]);
     }
     if (!$menu_items_data) {
         return;
     }
     $item_ids = wp_save_nav_menu_items(0, $menu_items_data);
     if (is_wp_error($item_ids)) {
         wp_die(0);
     }
     $menu_items = array();
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_items[] = $menu_obj;
         }
     }
     /**
      * This filter is defined in wp-admin/includes/nav-menu.php
      * 
      * @since 1.0.0
      */
     $walker_class_name = apply_filters('wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu']);
     if (!class_exists($walker_class_name)) {
         wp_die(0);
     }
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new $walker_class_name());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     if (!$_POST['menu-item']) {
         wp_die();
     }
 }
 $dbids_to_orders = array();
 $orders_to_dbids = array();
 foreach ((array) $ordered_menu_items as $ordered_menu_item_object) {
     if (isset($ordered_menu_item_object->ID)) {
         if (isset($ordered_menu_item_object->menu_order)) {
             $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
             $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
         }
     }
 }
 // if this menu item is not first
 if (!empty($dbids_to_orders[$menu_item_id]) && !empty($orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1])) {
     // if this menu item is a child of the previous
     if (!empty($menu_item_data['menu_item_parent']) && in_array($menu_item_data['menu_item_parent'], array_keys($dbids_to_orders)) && isset($orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1]) && $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1]) {
         $parent_db_id = in_array($menu_item_data['menu_item_parent'], $orders_to_dbids) ? (int) $menu_item_data['menu_item_parent'] : 0;
         $parent_object = wp_setup_nav_menu_item(get_post($parent_db_id));
         if (!is_wp_error($parent_object)) {
             $parent_data = (array) $parent_object;
             // if there is something before the parent and parent a child of it, make menu item a child also of it
             if (!empty($dbids_to_orders[$parent_db_id]) && !empty($orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1]) && !empty($parent_data['menu_item_parent'])) {
                 $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
                 // else if there is something before parent and parent not a child of it, make menu item a child of that something's parent
             } elseif (!empty($dbids_to_orders[$parent_db_id]) && !empty($orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1])) {
                 $_possible_parent_id = (int) get_post_meta($orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
                 if (in_array($_possible_parent_id, array_keys($dbids_to_orders))) {
                     $menu_item_data['menu_item_parent'] = $_possible_parent_id;
                 } else {
                     $menu_item_data['menu_item_parent'] = 0;
                 }
                 // else there isn't something before the parent
             } else {
 /**
  * 
  */
 function ajax_add_post_type()
 {
     if (!current_user_can('edit_theme_options')) {
         die('-1');
     }
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     if (empty($_POST['post_types'])) {
         exit;
     }
     // Create menu items and store IDs in array
     $item_ids = array();
     foreach ((array) $_POST['post_types'] as $post_type) {
         $post_type_obj = get_post_type_object($post_type);
         if (!$post_type_obj) {
             continue;
         }
         $menu_item_data = array('menu-item-title' => esc_attr($post_type_obj->labels->name), 'menu-item-type' => 'post_type_archive', 'menu-item-object' => esc_attr($post_type), 'menu-item-url' => get_post_type_archive_link($post_type));
         //Collect the items' IDs.
         $item_ids[] = wp_update_nav_menu_item(0, 0, $menu_item_data);
     }
     // If there was an error die here
     if (is_wp_error($item_ids)) {
         die('-1');
     }
     // Set up menu items
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_items[] = $menu_obj;
         }
     }
     // This gets the HTML to returns it to the menu
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     // Finally don't forget to exit
     exit;
 }
Esempio n. 11
0
 public function ajax_add_multisite_page()
 {
     switch_to_blog($_POST['blogId']);
     $post = get_post($_POST['ids'][0]);
     $url = get_permalink($post->ID);
     restore_current_blog();
     $item_ids = wp_update_nav_menu_item(0, 0, array('menu-item-title' => esc_attr($post->post_title), 'menu-item-type' => 'custom', 'menu-item-url' => $url));
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             // don't show "(pending)" in ajax-added items
             $menu_obj->label = $menu_obj->title;
             $menu_items[] = $menu_obj;
         }
     }
     // Needed to get the Walker up and running
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     // This gets the HTML to returns it to the menu
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     exit;
 }
Esempio n. 12
0
 /**
  * Test sanitize method.
  *
  * @see WP_Customize_Nav_Menu_Item_Setting::sanitize()
  */
 function test_sanitize()
 {
     do_action('customize_register', $this->wp_customize);
     $menu_id = wp_create_nav_menu('Primary');
     $setting = new WP_Customize_Nav_Menu_Item_Setting($this->wp_customize, 'nav_menu_item[123]');
     $this->assertNull($setting->sanitize('not an array'));
     $this->assertNull($setting->sanitize(123));
     $unsanitized = array('object_id' => 'bad', 'object' => '<b>hello</b>', 'menu_item_parent' => 'asdasd', 'position' => -123, 'type' => 'custom<b>', 'title' => '\\o/ o\'o Hi<script>unfilteredHtml()</script>', 'url' => 'javascript:alert(1)', 'target' => '" onclick="', 'attr_title' => '\\o/ o\'o <b>bolded</b><script>unfilteredHtml()</script>', 'description' => '\\o/ o\'o <b>Hello world</b><script>unfilteredHtml()</script>', 'classes' => 'hello " inject="', 'xfn' => 'hello " inject="', 'status' => 'forbidden', 'original_title' => 'Hi<script>unfilteredHtml()</script>', 'nav_menu_term_id' => 'heilo', '_invalid' => false);
     $expected_sanitized = array('object_id' => 0, 'object' => 'bhellob', 'menu_item_parent' => 0, 'position' => -123, 'type' => 'customb', 'title' => current_user_can('unfiltered_html') ? '\\o/ o\'o Hi<script>unfilteredHtml()</script>' : '\\o/ o\'o HiunfilteredHtml()', 'url' => '', 'target' => 'onclick', 'attr_title' => current_user_can('unfiltered_html') ? '\\o/ o\'o <b>bolded</b><script>unfilteredHtml()</script>' : '\\o/ o\'o <b>bolded</b>unfilteredHtml()', 'description' => current_user_can('unfiltered_html') ? '\\o/ o\'o <b>Hello world</b><script>unfilteredHtml()</script>' : '\\o/ o\'o <b>Hello world</b>unfilteredHtml()', 'classes' => 'hello  inject', 'xfn' => 'hello  inject', 'status' => 'draft', 'original_title' => 'Hi', 'nav_menu_term_id' => 0);
     $sanitized = $setting->sanitize($unsanitized);
     $this->assertEqualSets(array_keys($unsanitized), array_keys($sanitized));
     foreach ($expected_sanitized as $key => $value) {
         $this->assertEquals($value, $sanitized[$key], "Expected {$key} to be sanitized.");
     }
     $nav_menu_item_id = wp_update_nav_menu_item($menu_id, 0, wp_slash(array('menu-item-object-id' => $unsanitized['object_id'], 'menu-item-object' => $unsanitized['object'], 'menu-item-parent-id' => $unsanitized['menu_item_parent'], 'menu-item-position' => $unsanitized['position'], 'menu-item-type' => $unsanitized['type'], 'menu-item-title' => $unsanitized['title'], 'menu-item-url' => $unsanitized['url'], 'menu-item-description' => $unsanitized['description'], 'menu-item-attr-title' => $unsanitized['attr_title'], 'menu-item-target' => $unsanitized['target'], 'menu-item-classes' => $unsanitized['classes'], 'menu-item-xfn' => $unsanitized['xfn'], 'menu-item-status' => $unsanitized['status'])));
     $post = get_post($nav_menu_item_id);
     $nav_menu_item = wp_setup_nav_menu_item(clone $post);
     $this->assertEquals($expected_sanitized['object_id'], $nav_menu_item->object_id);
     $this->assertEquals($expected_sanitized['object'], $nav_menu_item->object);
     $this->assertEquals($expected_sanitized['menu_item_parent'], $nav_menu_item->menu_item_parent);
     $this->assertEquals($expected_sanitized['position'], $post->menu_order);
     $this->assertEquals($expected_sanitized['type'], $nav_menu_item->type);
     $this->assertEquals($expected_sanitized['title'], $post->post_title);
     $this->assertEquals($expected_sanitized['url'], $nav_menu_item->url);
     $this->assertEquals($expected_sanitized['description'], $post->post_content);
     $this->assertEquals($expected_sanitized['attr_title'], $post->post_excerpt);
     $this->assertEquals($expected_sanitized['target'], $nav_menu_item->target);
     $this->assertEquals($expected_sanitized['classes'], implode(' ', $nav_menu_item->classes));
     $this->assertEquals($expected_sanitized['xfn'], $nav_menu_item->xfn);
     $this->assertEquals($expected_sanitized['status'], $post->post_status);
 }
Esempio n. 13
0
 /**
  * @ticket 35324
  */
 function test_wp_setup_nav_menu_item_for_unknown_post_type_archive_no_description()
 {
     $post_type_slug = rand_str(12);
     $post_type_archive_item_id = wp_update_nav_menu_item($this->menu_id, 0, array('menu-item-type' => 'post_type_archive', 'menu-item-object' => $post_type_slug, 'menu-item-status' => 'publish'));
     $post_type_archive_item = wp_setup_nav_menu_item(get_post($post_type_archive_item_id));
     $this->assertEmpty($post_type_archive_item->description);
 }
 /**
  * AJAX Callback to create the menu item and add it to menu
  * @return string $HTML built with walk_nav_menu_tree()
  * use \Post_Type_Archive_Links::is_allowed() Check request and return choosen post types
  */
 public function ajax_add_year()
 {
     $years = $this->is_allowed();
     // Create menu items and store IDs in array
     $item_ids = array();
     $menu_item_data = array('menu-item-title' => esc_attr($years[0]), 'menu-item-type' => 'cyear_archive', 'menu-item-object' => esc_attr($years[0]), 'menu-item-url' => get_year_link($years[0]));
     // Collect the items' IDs.
     $item_ids[] = wp_update_nav_menu_item(0, 0, $menu_item_data);
     // If there was an error die here
     is_wp_error($item_ids) and die('-1');
     // Set up menu items
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             // don't show "(pending)" in ajax-added items
             $menu_obj->label = $menu_obj->title;
             $menu_items[] = $menu_obj;
         }
     }
     // Needed to get the Walker up and running
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     // This gets the HTML to returns it to the menu
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     // Finally don't forget to exit
     exit;
 }
 /**
  * Test value method returns zero for nav_menu_term_id when previewing a new menu.
  *
  * @see WP_Customize_Nav_Menu_Item_Setting::value()
  */
 function test_value_nav_menu_term_id_returns_zero()
 {
     do_action('customize_register', $this->wp_customize);
     $menu_id = -123;
     $post_value = array('name' => 'Secondary', 'description' => '', 'parent' => 0, 'auto_add' => false);
     $setting_id = "nav_menu[{$menu_id}]";
     $menu = new WP_Customize_Nav_Menu_Setting($this->wp_customize, $setting_id);
     $this->wp_customize->set_post_value($menu->id, $post_value);
     $menu->preview();
     $value = $menu->value();
     $this->assertEquals($post_value, $value);
     $post_id = $this->factory->post->create(array('post_title' => 'Hello World'));
     $item_id = wp_update_nav_menu_item($menu_id, 0, array('menu-item-type' => 'post_type', 'menu-item-object' => 'post', 'menu-item-object-id' => $post_id, 'menu-item-title' => 'Hello World', 'menu-item-status' => 'publish'));
     $post = get_post($item_id);
     $menu_item = wp_setup_nav_menu_item($post);
     $setting_id = "nav_menu_item[{$item_id}]";
     $setting = new WP_Customize_Nav_Menu_Item_Setting($this->wp_customize, $setting_id);
     $value = $setting->value();
     $this->assertEquals(0, $value['nav_menu_term_id']);
 }
 /**
  * Set item meta data.
  *
  * @param  WP_Post $menu_item
  * @param  int     $blog_id
  * @return WP_Post
  */
 private function set_menu_item_meta($menu_item, $blog_id)
 {
     // don't show "(pending)" in ajax-added items
     $menu_item->post_type = 'nav_menu_item';
     $menu_item->url = get_home_url($blog_id, '/');
     $menu_item->object = 'mlp_language';
     $menu_item->xfn = 'alternate';
     $menu_item = wp_setup_nav_menu_item($menu_item);
     $menu_item->label = $menu_item->title;
     // Replace the "Custom" in the management screen
     $menu_item->type_label = esc_html__('Language', 'multilingualpress');
     $menu_item->classes[] = "blog-id-{$blog_id}";
     $menu_item->classes[] = "mlp-language-nav-item";
     $menu_item->url = get_home_url($blog_id, '/');
     update_post_meta($menu_item->ID, $this->meta_key, $blog_id);
     $url = esc_url_raw(get_home_url($blog_id, '/'));
     update_post_meta($menu_item->ID, '_menu_item_url', $url);
     return $menu_item;
 }
Esempio n. 17
0
 /**
  * Called when generating a menu via `wp_nav_menu`
  *
  * You can have submenus automatically generated by passing 'auto_show_children'
  * in the menu args.
  *
  * ```
  * wp_get_nav_menu_items(
  *   $locations['main'],
  *   array('auto_show_children' => true)
  * );
  * ```
  *
  * @param array $items Items from `wp_get_nav_menu_items`
  * @param array $menu Menu object
  * @param array $args Args used in getting menu items
  * @return array
  * @see `wp_get_nav_menu_items`
  */
 public function getNavMenu($items = array(), $menu = null, $args = array())
 {
     if (isset($args['auto_show_children']) && $args['auto_show_children']) {
         $subMenus = array();
         foreach ($items as $index => $item) {
             if ($item->menu_item_parent) {
                 unset($items[$index]);
                 continue;
             }
             $children = get_children(array('post_parent' => $item->object_id, 'post_status' => 'publish', 'post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));
             foreach ($children as &$child) {
                 $child = wp_setup_nav_menu_item($child);
                 $child->menu_item_parent = $item->ID;
                 $child->post_type = 'nav_menu_item';
                 $subMenus[] = $child;
             }
         }
         $items = array_merge($items, $subMenus);
         // restructure the menu based on the new items
         foreach ($items as $index => &$item) {
             $item->menu_order = $index;
         }
     }
     return $items;
 }
 function avia_ajax_switch_menu_walker()
 {
     if (!current_user_can('edit_theme_options')) {
         die('-1');
     }
     check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     $item_ids = wp_save_nav_menu_items(0, $_POST['menu-item']);
     if (is_wp_error($item_ids)) {
         die('-1');
     }
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_items[] = $menu_obj;
         }
     }
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new avia_backend_walker());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     die('end');
 }
Esempio n. 19
0
             }
             $_menu_items = array_map('wp_setup_nav_menu_item', array($_object));
             $_menu_item = array_shift($_menu_items);
             // Restore the missing menu item properties
             $menu_item_data['menu-item-description'] = $_menu_item->description;
         }
         $menu_items_data[] = $menu_item_data;
     }
     $item_ids = wp_save_nav_menu_items(0, $menu_items_data);
     if (is_wp_error($item_ids)) {
         die('-1');
     }
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_items[] = $menu_obj;
         }
     }
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     break;
 case 'add-meta':
     check_ajax_referer('add-meta', '_ajax_nonce-add-meta');
     $c = 0;
     $pid = (int) $_POST['post_id'];
     $post = get_post($pid);
Esempio n. 20
0
/**
 * Displays a navigation menu.
 *
 * Optional $args contents:
 *
 * id - The menu id. Defaults to blank.
 * slug - The menu slug. Defaults to blank.
 * menu_class - CSS class to use for the div container of the menu list. Defaults to 'menu'.
 * format - Whether to format the ul. Defaults to 'div'.
 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
 * before - Text before the link text.
 * after - Text after the link text.
 * link_before - Text before the link.
 * link_after - Text after the link.
 * echo - Whether to echo the menu or return it. Defaults to echo.
 *
 * @todo show_home - If you set this argument, then it will display the link to the home page. The show_home argument really just needs to be set to the value of the text of the link.
 *
 * @since 3.0.0
 *
 * @param array $args Arguments
 */
function wp_nav_menu($args = array())
{
    $defaults = array('menu' => '', 'container' => 'div', 'container_class' => '', 'menu_class' => 'menu', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'depth' => 0, 'walker' => '', 'context' => 'frontend');
    $args = wp_parse_args($args, $defaults);
    $args = apply_filters('wp_nav_menu_args', $args);
    $args = (object) $args;
    // Get the nav menu
    $menu = wp_get_nav_menu_object($args->menu);
    // If we couldn't find a menu based off the name, id or slug,
    // get the first menu that has items.
    if (!$menu) {
        $menus = wp_get_nav_menus();
        foreach ($menus as $menu_maybe) {
            if (wp_get_nav_menu_items($menu_maybe->term_id)) {
                $menu = $menu_maybe;
                break;
            }
        }
    }
    // If the menu exists, get it's items.
    if ($menu && !is_wp_error($menu)) {
        $menu_items = wp_get_nav_menu_items($menu->term_id, $args->context);
    }
    // If no menu was found or if the menu has no items, call the fallback_cb
    if (!$menu || is_wp_error($menu) || isset($menu_items) && empty($menu_items)) {
        if ('frontend' == $args->context && (function_exists($args->fallback_cb) || is_callable($args->fallback_cb))) {
            return call_user_func($args->fallback_cb, (array) $args);
        }
    }
    $nav_menu = '';
    $items = '';
    $container_allowedtags = apply_filters('wp_nav_menu_container_allowedtags', array('div', 'p', 'nav'));
    if (in_array($args->container, $container_allowedtags)) {
        $class = $args->container_class ? ' class="' . esc_attr($args->container_class) . '"' : ' class="menu-' . $menu->slug . '-container"';
        $nav_menu .= '<' . $args->container . $class . '>';
    }
    // Set up the $menu_item variables
    foreach ((array) $menu_items as $key => $menu_item) {
        $menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item, 'frontend');
    }
    $items .= walk_nav_menu_tree($menu_items, $args->depth, $args);
    // Attributes
    $attributes = ' id="menu-' . $menu->slug . '"';
    $attributes .= $args->menu_class ? ' class="' . $args->menu_class . '"' : '';
    $nav_menu .= '<ul' . $attributes . '>';
    // Allow plugins to hook into the menu to add their own <li>'s
    if ('frontend' == $args->context) {
        $items = apply_filters('wp_nav_menu_items', $items, $args);
        $items = apply_filters("wp_nav_menu_{$menu->slug}_items", $items, $args);
        $nav_menu .= $items;
    } else {
        $nav_menu .= $items;
    }
    $nav_menu .= '</ul>';
    if (in_array($args->container, $container_allowedtags)) {
        $nav_menu .= '</' . $args->container . '>';
    }
    $nav_menu = apply_filters('wp_nav_menu', $nav_menu, $args);
    if ($args->echo) {
        echo $nav_menu;
    } else {
        return $nav_menu;
    }
}
 /**
  * Get the instance data for a given nav_menu_item setting.
  *
  * @since 4.3.0
  * @access public
  *
  * @see wp_setup_nav_menu_item()
  *
  * @return array|false Instance data array, or false if the item is marked for deletion.
  */
 public function value()
 {
     if ($this->is_previewed && $this->_previewed_blog_id === get_current_blog_id()) {
         $undefined = new stdClass();
         // Symbol.
         $post_value = $this->post_value($undefined);
         if ($undefined === $post_value) {
             $value = $this->_original_value;
         } else {
             $value = $post_value;
         }
     } else {
         if (isset($this->value)) {
             $value = $this->value;
         } else {
             $value = false;
             // Note that a ID of less than one indicates a nav_menu not yet inserted.
             if ($this->post_id > 0) {
                 $post = get_post($this->post_id);
                 if ($post && self::POST_TYPE === $post->post_type) {
                     $value = (array) wp_setup_nav_menu_item($post);
                 }
             }
             if (!is_array($value)) {
                 $value = $this->default;
             }
             // Cache the value for future calls to avoid having to re-call wp_setup_nav_menu_item().
             $this->value = $value;
             $this->populate_value();
             $value = $this->value;
         }
     }
     return $value;
 }
	/**
	 * Get the value emulated into a WP_Post and set up as a nav_menu_item.
	 *
	 * @since 4.3.0
	 *
	 * @return WP_Post With {@see wp_setup_nav_menu_item()} applied.
	 */
	public function value_as_wp_post_nav_menu_item() {
		$item = (object) $this->value();
		unset( $item->nav_menu_term_id );

		$item->post_status = $item->status;
		unset( $item->status );

		$item->post_type = 'nav_menu_item';
		$item->menu_order = $item->position;
		unset( $item->position );

		$item->post_author = get_current_user_id();

		if ( $item->title ) {
			$item->post_title = $item->title;
		}

		$item->ID = $this->post_id;
		$post = new WP_Post( (object) $item );
		$post = wp_setup_nav_menu_item( $post );

		return $post;
	}
Esempio n. 23
0
function mystique_list_menu($args = array())
{
    $locations = get_nav_menu_locations();
    $menu = wp_get_nav_menu_object($locations['primary']);
    $menu_items = wp_get_nav_menu_items($menu->term_id);
    if (empty($menu_items)) {
        printf('<li><a><span class="error">' . __("Empty menu (%s)", "mystique") . '</span></a></li>', $menu->slug);
        return;
    }
    $nav_menu = '';
    $items = '';
    _mystique_menu_item_classes_by_context($menu_items);
    $sorted_menu_items = array();
    foreach ((array) $menu_items as $key => $menu_item) {
        $sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item);
    }
    $walker = new mystique_MenuWalker();
    $items .= $walker->walk($sorted_menu_items, 0, array());
    // Allow plugins to hook into the menu to add their own <li>'s
    $items = apply_filters('wp_nav_menu_items', $items, $args);
    $items = apply_filters("wp_nav_menu_{$menu->slug}_items", $items, $args);
    $nav_menu .= $items;
    $nav_menu = apply_filters('wp_nav_menu', $nav_menu, $args);
    echo $nav_menu;
}
 /**
  * @ticket 19038
  */
 function test_wp_setup_nav_menu_item_for_trashed_post()
 {
     $post_id = self::factory()->post->create(array('post_status' => 'trash'));
     $menu_item_id = wp_update_nav_menu_item($this->menu_id, 0, array('menu-item-type' => 'post_type', 'menu-item-object' => 'post', 'menu-item-object-id' => $post_id, 'menu-item-status' => 'publish'));
     $menu_item = wp_setup_nav_menu_item(get_post($menu_item_id));
     $this->assertTrue(!_is_valid_nav_menu_item($menu_item));
 }
 function cpcm_replace_taxonomy_by_posts($sorted_menu_items, $args)
 {
     $this->getOptions();
     $result = array();
     $inc = 0;
     $menu_item_parent_map = array();
     // Holds, for each menu item I that was removed, a link to the item that should become the new parent P of menu items under I
     foreach ((array) $sorted_menu_items as $key => $menu_item) {
         $menu_item->menu_order = $menu_item->menu_order + $inc;
         // Augment taxonomy object with a list of its posts: Append posts to $result
         // Optional: Remove the taxonomy object/original menu item itself.
         if ($menu_item->type == 'taxonomy' && get_post_meta($menu_item->db_id, "_cpcm-unfold", true) == '1') {
             $query_arr = array();
             // Example:  Array ( [0] => Array ( [taxonomy] => category [field] => id [terms] => 3 ) ), i.e. get a category by id, where id = 3
             $query_arr['tax_query'] = array('relation' => 'AND', array('taxonomy' => $menu_item->object, 'field' => 'id', 'terms' => $menu_item->object_id));
             $subcategory_behavior = get_post_meta($menu_item->db_id, "_cpcm-subcategories", true);
             switch ($subcategory_behavior) {
                 case "exclude":
                     // Subcategories (subtaxonomies) should be excluded, so append a query to tax_query that does exactly that
                     $category_children = array_diff(get_term_children($menu_item->object_id, $menu_item->object), array(""));
                     if (!empty($category_children)) {
                         $query_arr['tax_query'][] = array('taxonomy' => $menu_item->object, 'terms' => $category_children, 'field' => 'id', 'operator' => 'NOT IN');
                     }
                     break;
                 case "flatten":
                     /* No additional filtering */
                 /* No additional filtering */
                 default:
                     break;
             }
             // If _cpcm-unfold is true, the following custom fields exist:
             $query_arr['order'] = get_post_meta($menu_item->db_id, "_cpcm-order", true);
             $query_arr['orderby'] = get_post_meta($menu_item->db_id, "_cpcm-orderby", true);
             $query_arr['numberposts'] = get_post_meta($menu_item->db_id, "_cpcm-item-count", true);
             // default value of -1 returns all posts
             $query_arr['offset'] = get_post_meta($menu_item->db_id, "_cpcm-item-skip", true);
             // default value of 0 skips no posts
             $query_arr['posts_per_page'] = $query_arr['numberposts'] != '-1' ? get_post_meta($menu_item->db_id, "_cpcm-item-count", true) : 100000;
             // http://wordpress.stackexchange.com/a/13376 posts_per_page does not accept -1, but is a required argument to make offset work. So, whenever numberposts is positive, use that. Otherwise, use the proposed workaround on the SO topic.
             // Support for custom post types
             $tag = get_taxonomy($menu_item->object);
             $query_arr['post_type'] = $tag->object_type;
             // Allow plugin extensions that further modify the query
             if (has_filter('cpcm_filter_posts_query')) {
                 $query_arr = apply_filters('cpcm_filter_posts_query', $query_arr, $menu_item);
             }
             $posts = get_posts($query_arr);
             // Decide whether the original item needs to be preserved.
             $remove_original_item = get_post_meta($menu_item->db_id, "_cpcm-remove-original-item", true);
             $menu_item_parent = $menu_item->menu_item_parent;
             switch ($remove_original_item) {
                 case "always":
                     if (empty($posts)) {
                         $inc -= 1;
                         $menu_item_parent_map[$menu_item->db_id] = $menu_item->menu_item_parent;
                     } else {
                         if (count($posts) == 1) {
                             // If the menu-item should be removed, but it has exactly one post, then use this post as new parent for any menu items down the line.
                             // Because we can't use posts as menu items (they don't have a db_id), reuse the menu_item object and transfer the post properties to the menu_item in the foreach loop
                             // See {note 1} in foreach
                             array_push($result, $menu_item);
                         } else {
                             $inc -= 1;
                             $menu_item_parent_map[$menu_item->db_id] = $menu_item->menu_item_parent;
                         }
                     }
                     break;
                 case "only if empty":
                     if (empty($posts)) {
                         $inc -= 1;
                         $menu_item_parent_map[$menu_item->db_id] = $menu_item->menu_item_parent;
                     } else {
                         array_push($result, $menu_item);
                     }
                     break;
                 case "never":
                     array_push($result, $menu_item);
                     break;
             }
             if (is_numeric($query_arr['numberposts']) && $query_arr['numberposts'] == '0') {
                 continue;
             }
             // Set the menu_item_parent for the menu_item: If the parent item was removed, go up a level
             $current_parent_id = $menu_item->menu_item_parent;
             while (array_key_exists(strval($current_parent_id), $menu_item_parent_map) == 1) {
                 $current_parent_id = $menu_item_parent_map[$current_parent_id];
             }
             $menu_item->menu_item_parent = $current_parent_id;
             foreach ((array) $posts as $pkey => $post) {
                 $post = wp_setup_nav_menu_item($post);
                 // Set the menu_item_parent for the post: If the parent item was removed, go up a level
                 $current_parent_id = $menu_item->db_id;
                 while (array_key_exists(strval($current_parent_id), $menu_item_parent_map) == 1) {
                     $current_parent_id = $menu_item_parent_map[$current_parent_id];
                 }
                 $post->menu_item_parent = $current_parent_id;
                 // Transfer properties from the old menu item to the new one
                 $post->target = $menu_item->target;
                 //$post->classes = $menu_item->classes; // Don't copy the classes, because this will also copy the 'active' CSS class too all siblings of the selected menu item. http://wordpress.org/support/topic/active-css-class
                 $post->classes = array_merge($post->classes, (array) get_post_meta($menu_item->db_id, "_menu_item_classes", true));
                 // copy custom css classes that the user specified under "CSS Classes (optional)"
                 $post->xfn = $menu_item->xfn;
                 $post->description = $menu_item->description;
                 // Set the title of the new menu item
                 $post->title = get_post_meta($menu_item->db_id, "_cpcm-item-titles", true);
                 // Replace the placeholders in the title by the properties of the post
                 $post->title = $this->replace_placeholders($post, $post->title);
                 $inc += 1;
                 $post->menu_order = $menu_item->menu_order + $inc;
             }
             // Solve https://wordpress.org/support/topic/works-with-41-as-far-as-i-can-tell?replies=5, regenerate all classes for the posts, and copy those classes to the menu_item that we're reusing.
             // Extend the items with classes.
             _wp_menu_item_classes_by_context($posts);
             // Decorate the posts with the required data for a menu-item.
             if (count($posts) == 1 && $remove_original_item == "always") {
                 // {note 1}
                 // Do not use the post, but re-use the menu item instead.
                 $menu_item->title = get_post_meta($menu_item->db_id, "_cpcm-item-titles", true);
                 $menu_item->title = $this->replace_placeholders($post, $menu_item->title);
                 $menu_item->url = get_permalink($post->ID);
                 $menu_item->classes = $posts[0]->classes;
             } else {
                 // Append the new menu_items to the menu array that we're building.
                 $result = array_merge($result, $posts);
             }
         } else {
             // Other objects may have a parent that has been removed by cpcm. Fix that here.
             // Set the menu_item_parent for the menu_item: If the parent item was removed, go up a level
             $current_parent_id = $menu_item->menu_item_parent;
             while (array_key_exists(strval($current_parent_id), $menu_item_parent_map) == 1) {
                 $current_parent_id = $menu_item_parent_map[$current_parent_id];
             }
             $menu_item->menu_item_parent = $current_parent_id;
             // Treat other objects as usual, but note that the position
             // of elements in the array changes.
             array_push($result, $menu_item);
         }
     }
     unset($sorted_menu_items);
     unset($menu_item_parent_map);
     // Apply _wp_menu_item_classes_by_context not only to the $posts array, but to the whole result array so that the classes for the original menu items are regenerated as well. Solves: http://wordpress.org/support/topic/issue-with-default-wordpress-sidebar-menu and http://wordpress.org/support/topic/menu-do-not-include-the-current-menu-parent-class
     _wp_menu_item_classes_by_context($result);
     return $result;
 }
 /**
  *
  *@param $menu_databse_id int 0 for no menu, 1 for 'fallback', term ID for menu otherwise
  *
  */
 function update_nav_menu($menu_id, $menu_item_title)
 {
     //Get existing menu item
     $menu_item_db_id = (int) eventorganiser_get_option('menu_item_db_id');
     //Validate exiting menu item ID
     if (!is_nav_menu_item($menu_item_db_id)) {
         $menu_item_db_id = 0;
     }
     //If Menu is not selected, or 'page list' fallback is, and we have an 'events' item added to some menu, remove it
     if ((empty($menu_id) || $menu_id == '1') && is_nav_menu_item($menu_item_db_id)) {
         //Remove menu item
         wp_delete_post($menu_item_db_id, true);
         $menu_item_db_id = 0;
     }
     //If the $menu is an int > 1, we are adding/updating an item (post type) so that it has term set to $menu_id
     if (!empty($menu_id) && $menu_id != '1') {
         $menu_item_data = array();
         //Validate menu ID
         $menu_obj = wp_get_nav_menu_object($menu_id);
         $menu_id = $menu_obj ? $menu_obj->term_id : 0;
         //Set status
         $status = $menu_id == 0 ? '' : 'publish';
         $menu_item_data = array('menu-item-title' => $menu_item_title, 'menu-item-url' => get_post_type_archive_link('event'), 'menu-item-object' => 'event', 'menu-item-status' => $status, 'menu-item-type' => 'post_type_archive');
         //If we're updating preserve parent and position
         if (is_nav_menu_item($menu_item_db_id)) {
             $menu_item = wp_setup_nav_menu_item(get_post($menu_item_db_id));
             $menu_item_data += array('menu-item-parent-id' => $menu_item->menu_item_parent, 'menu-item-position' => $menu_item->menu_order);
         }
         //Update menu item (post type) to have taxonom term $menu_id
         $menu_item_db_id = wp_update_nav_menu_item($menu_id, $menu_item_db_id, $menu_item_data);
     }
     //Return the menu item (post type) ID. 0 For no item added, or item removed.
     return $menu_item_db_id;
 }
Esempio n. 27
0
 /**
  * Worker method to create new items or update existing ones.
  */
 private function add_or_update_item($method, $type, $args, $assoc_args)
 {
     $menu = $args[0];
     $menu_item_db_id = \WP_CLI\Utils\get_flag_value($args, 1, 0);
     $menu = wp_get_nav_menu_object($menu);
     if (!$menu || is_wp_error($menu)) {
         WP_CLI::error("Invalid menu.");
     }
     // `url` is protected in WP-CLI, so we use `link` instead
     $assoc_args['url'] = \WP_CLI\Utils\get_flag_value($assoc_args, 'link');
     // Need to persist the menu item data. See https://core.trac.wordpress.org/ticket/28138
     if ('update' == $method) {
         $menu_item_obj = get_post($menu_item_db_id);
         $menu_item_obj = wp_setup_nav_menu_item($menu_item_obj);
         // Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
         $position = 0 === $menu_item_obj->menu_order ? 1 : $menu_item_obj->menu_order;
         $default_args = array('position' => $position, 'title' => $menu_item_obj->title, 'url' => $menu_item_obj->url, 'description' => $menu_item_obj->description, 'object' => $menu_item_obj->object, 'object-id' => $menu_item_obj->object_id, 'parent-id' => $menu_item_obj->menu_item_parent, 'attr-title' => $menu_item_obj->attr_title, 'target' => $menu_item_obj->target, 'classes' => implode(' ', $menu_item_obj->classes), 'xfn' => $menu_item_obj->xfn, 'status' => $menu_item_obj->post_status);
     } else {
         $default_args = array('position' => 0, 'title' => '', 'url' => '', 'description' => '', 'object' => '', 'object-id' => 0, 'parent-id' => 0, 'attr-title' => '', 'target' => '', 'classes' => '', 'xfn' => '', 'status' => 'publish');
     }
     $menu_item_args = array();
     foreach ($default_args as $key => $default_value) {
         // wp_update_nav_menu_item() has a weird argument prefix
         $new_key = 'menu-item-' . $key;
         $menu_item_args[$new_key] = \WP_CLI\Utils\get_flag_value($assoc_args, $key, $default_value);
     }
     $menu_item_args['menu-item-type'] = $type;
     $ret = wp_update_nav_menu_item($menu->term_id, $menu_item_db_id, $menu_item_args);
     if (is_wp_error($ret)) {
         WP_CLI::error($ret->get_error_message());
     } else {
         if (!$ret) {
             if ('add' == $method) {
                 WP_CLI::error("Couldn't add menu item.");
             } else {
                 if ('update' == $method) {
                     WP_CLI::error("Couldn't update menu item.");
                 }
             }
         } else {
             /**
              * Set the menu
              *
              * wp_update_nav_menu_item() *should* take care of this, but
              * depends on wp_insert_post()'s "tax_input" argument, which
              * is ignored if the user can't edit the taxonomy
              *
              * @see https://core.trac.wordpress.org/ticket/27113
              */
             if (!is_object_in_term($ret, 'nav_menu', (int) $menu->term_id)) {
                 wp_set_object_terms($ret, array((int) $menu->term_id), 'nav_menu');
             }
             if ('add' == $method && !empty($assoc_args['porcelain'])) {
                 WP_CLI::line($ret);
             } else {
                 if ('add' == $method) {
                     WP_CLI::success("Menu item added.");
                 } else {
                     if ('update' == $method) {
                         WP_CLI::success("Menu item updated.");
                     }
                 }
             }
         }
     }
 }
Esempio n. 28
0
 /**
  * Replaces wp_ajax_add_menu_item() (wp-admin/includes/ajax-actions.php:~1056).
  *
  * @since Client Dash 1.6
  */
 public function add_menu_item()
 {
     global $ClientDash;
     // Security
     check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
     if (!current_user_can('edit_theme_options')) {
         wp_die(-1);
     }
     // Save the new items
     $item_ids = ClientDash_Core_Page_Settings_Tab_Menus::save_menu_items(0, $_POST['menu-item']);
     // Setup the new items
     $menu_items = array();
     foreach ((array) $item_ids as $menu_item_id) {
         $menu_obj = get_post($menu_item_id);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_items[] = $menu_obj;
         }
     }
     // Include the custom CD walker class
     include_once $ClientDash->path . '/core/tabs/settings/menus/walkerclass.php';
     // Output the newly populated nav menu
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit_CD());
         echo walk_nav_menu_tree($menu_items, 0, (object) $args);
     }
     wp_die();
 }
Esempio n. 29
0
/**
 * Ajax handler for adding a menu item.
 *
 * @since 3.1.0
 */
function wp_ajax_add_menu_item()
{
    check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
    if (!current_user_can('edit_theme_options')) {
        wp_die(-1);
    }
    require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
    // For performance reasons, we omit some object properties from the checklist.
    // The following is a hacky way to restore them when adding non-custom items.
    $menu_items_data = array();
    foreach ((array) $_POST['menu-item'] as $menu_item_data) {
        if (!empty($menu_item_data['menu-item-type']) && 'custom' != $menu_item_data['menu-item-type'] && !empty($menu_item_data['menu-item-object-id'])) {
            switch ($menu_item_data['menu-item-type']) {
                case 'post_type':
                    $_object = get_post($menu_item_data['menu-item-object-id']);
                    break;
                case 'post_type_archive':
                    $_object = get_post_type_object($menu_item_data['menu-item-object']);
                    break;
                case 'taxonomy':
                    $_object = get_term($menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object']);
                    break;
            }
            $_menu_items = array_map('wp_setup_nav_menu_item', array($_object));
            $_menu_item = reset($_menu_items);
            // Restore the missing menu item properties
            $menu_item_data['menu-item-description'] = $_menu_item->description;
        }
        $menu_items_data[] = $menu_item_data;
    }
    $item_ids = wp_save_nav_menu_items(0, $menu_items_data);
    if (is_wp_error($item_ids)) {
        wp_die(0);
    }
    $menu_items = array();
    foreach ((array) $item_ids as $menu_item_id) {
        $menu_obj = get_post($menu_item_id);
        if (!empty($menu_obj->ID)) {
            $menu_obj = wp_setup_nav_menu_item($menu_obj);
            $menu_obj->label = $menu_obj->title;
            // don't show "(pending)" in ajax-added items
            $menu_items[] = $menu_obj;
        }
    }
    /** This filter is documented in wp-admin/includes/nav-menu.php */
    $walker_class_name = apply_filters('wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu']);
    if (!class_exists($walker_class_name, false)) {
        wp_die(0);
    }
    if (!empty($menu_items)) {
        $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new $walker_class_name());
        echo walk_nav_menu_tree($menu_items, 0, (object) $args);
    }
    wp_die();
}
Esempio n. 30
0
/**
 * Update properties of a nav menu item, with the option to create a clone of the item.
 *
 * Wrapper for wp_update_nav_menu_item() that only requires passing changed properties.
 *
 * @link https://core.trac.wordpress.org/ticket/28138
 *
 * @since Menu Customizer 0.0
 *
 * @param int   $menu_id The ID of the menu. If "0", makes the menu item a draft orphan.
 * @param int   $item_id The ID of the menu item. If "0", creates a new menu item.
 * @param array $data    The new data for the menu item.
 * @param bool  $clone   If true, creates a copy of the item and only changes the copy.
 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
 */
function menu_customizer_update_menu_item($menu_id, $item_id, $data, $clone = false)
{
    $item = get_post($item_id);
    $item = wp_setup_nav_menu_item($item);
    $defaults = array('menu-item-db-id' => $item_id, 'menu-item-object-id' => $item->object_id, 'menu-item-object' => $item->object, 'menu-item-parent-id' => $item->menu_item_parent, 'menu-item-position' => $item->menu_order, 'menu-item-type' => $item->type, 'menu-item-title' => $item->title, 'menu-item-url' => $item->url, 'menu-item-description' => $item->description, 'menu-item-attr-title' => $item->attr_title, 'menu-item-target' => $item->target, 'menu-item-classes' => implode(' ', $item->classes), 'menu-item-xfn' => $item->xfn, 'menu-item-status' => $item->publish);
    $args = wp_parse_args($data, $defaults);
    if ($clone) {
        $item_id = 0;
    }
    return wp_update_nav_menu_item($menu_id, $item_id, $args);
}