/**
  * When menu item removed remove menu image metadata.
  */
 public function delete_menu_item_image_action()
 {
     $menu_item_id = (int) $_REQUEST['menu-item'];
     check_admin_referer('delete-menu_item_image_' . $menu_item_id);
     if (is_nav_menu_item($menu_item_id) && has_post_thumbnail($menu_item_id)) {
         delete_post_thumbnail($menu_item_id);
         delete_post_meta($menu_item_id, 'menu_item_icon');
         delete_post_meta($menu_item_id, 'menu-item-bg-img');
     }
 }
 /**
  *
  * @param type $post_id
  */
 public function save_post($post_id = false)
 {
     // Get the post object
     $post = get_post($post_id);
     // Bail if post is an autosave, revision, or menu item
     if (wp_is_post_autosave($post) || wp_is_post_revision($post) || is_nav_menu_item($post)) {
         return;
     }
     // Run autolink callbacks on post content
     parent::input($post->post_content, $post);
 }
 private function create_page()
 {
     $page = array('post_type' => 'page', 'post_status' => 'publish', 'post_name' => 'wordlift-entity', 'post_content' => '[wordlift.entity]', 'post_title' => 'WordLift Entity');
     $error = null;
     $page_id = wp_insert_post($page, $error);
     // add the entity page option and remove the page from menus.
     if (is_numeric($page_id) && 0 !== $page_id) {
         update_option('_wordlift_entity_page_id', $page_id);
         $args = array('meta_key' => '_menu_item_object_id', 'meta_value' => $page_id, 'post_type' => 'nav_menu_item');
         $menu_items = get_posts($args);
         foreach ($menu_items as $menu_item) {
             if (is_nav_menu_item($menu_item->ID)) {
                 wp_delete_post($menu_item->ID, true);
             }
         }
     }
     $this->logger->info(var_export($error, true));
 }
 /**
  * Add icon before menu item title and wrap them with <span class="iconized-hover-trigger"> tag if needed ( if function attached to "iconize_menu_items_with" filter is returning "title" string ).
  *
  * @since 1.1.2
  *
  * @param string  $title
  * @param string  $id
  * @return string $title
  */
 public function iconize_menu_item_title_all($title, $id)
 {
     if (is_nav_menu_item($id)) {
         $icon_args['icon_name'] = get_post_meta($id, '_menu_item_icon_name', true);
         $icon_args['icon_set'] = get_post_meta($id, '_menu_item_icon_set', true);
         $icon_args['icon_transform'] = get_post_meta($id, '_menu_item_icon_transform', true);
         $icon_args['icon_color'] = get_post_meta($id, '_menu_item_icon_color', true);
         $icon_args['icon_size'] = get_post_meta($id, '_menu_item_icon_size', true);
         $icon_args['icon_align'] = get_post_meta($id, '_menu_item_icon_align', true);
         $icon_args['icon_custom_classes'] = get_post_meta($id, '_menu_item_icon_custom_classes', true);
         $icon_args['icon_position'] = get_post_meta($id, '_menu_item_icon_position', true);
         $icon = iconize_get_icon($icon_args, 'menu_item');
         $hovers = $this->get_iconize_dialog_dropdown_options_for('hover');
         $hovers = array_keys($hovers);
         if (!empty($icon_args['icon_transform']) && in_array($icon_args['icon_transform'], $hovers)) {
             $title = '<span class="iconized-hover-trigger">' . $icon . $title . '</span>';
         } else {
             if ('after' === $icon_args['icon_position']) {
                 $title = $title . $icon;
             } else {
                 $title = $icon . $title;
             }
         }
     }
     return $title;
 }
Exemple #5
0
/**
 * Updates the order for and publishes an existing menu item.
 *
 * Skips the mess that is wp_update_nav_menu_item() and avoids
 * handling menu item fields that are not changed.
 *
 * Based on the parts of wp_update_nav_menu_item() that are needed here.
 * $menu_id must already be validated before running this function (to avoid re-validating for each item in the menu).
 *
 * @param int $menu_id The valid ID of the menu.
 * @param int $item_id The ID of the (existing) menu item.
 * @param int $order   The menu item's new order/position.
 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
 */
function menu_customizer_update_menu_item_order($menu_id, $item_id, $order)
{
    $item_id = (int) $item_id;
    // Make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects.
    if (!is_nav_menu_item($item_id)) {
        return new WP_Error('update_nav_menu_item_failed', __('The given object ID is not that of a menu item.'));
    }
    // Associate the menu item with the menu term.
    // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms().
    if ($menu_id && !is_object_in_term($item_id, 'nav_menu', (int) $menu_id)) {
        wp_set_object_terms($item_id, array($menu_id), 'nav_menu');
    }
    // Populate the potentially-changing fields of the menu item object.
    $post = array('ID' => $item_id, 'menu_order' => $order, 'post_status' => 'publish');
    // Update the menu item object.
    wp_update_post($post);
    return $item_id;
}
Exemple #6
0
 /**
  * Saves the current nav menu.
  *
  * Taken and modified for Client Dash needs from wp-admin/includes/nav-menu.php:~1257
  *
  * @param int $menu_ID The ID of the nav menu to save to.
  *
  * @since Client Dash 1.6
  */
 public static function update_menu_items($menu_ID)
 {
     // Get our old menu items
     $unsorted_menu_items = wp_get_nav_menu_items($menu_ID, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID'));
     // Index menu items by db ID
     $menu_items = array();
     foreach ($unsorted_menu_items as $_item) {
         $menu_items[$_item->db_id] = $_item;
     }
     wp_defer_term_counting(true);
     // Loop through all the menu items' POST variables
     if (!empty($_POST['menu-item-db-id'])) {
         foreach ((array) $_POST['menu-item-db-id'] as $db_ID) {
             // Setup args
             $args = array();
             foreach (self::$menu_item_defaults as $field => $default) {
                 // Skip some static properties
                 if (in_array($field, array('original-title', 'cd-type', 'cd-page-title', 'cd-submenu-parent', 'db-id'))) {
                     continue;
                 }
                 $args[$field] = isset($_POST["menu-item-{$field}"][$db_ID]) ? $_POST["menu-item-{$field}"][$db_ID] : '';
             }
             $menu_item_db_id = self::update_menu_item($menu_ID, $_POST['menu-item-db-id'][$db_ID] != $db_ID ? 0 : $db_ID, $args);
             // Saved it, now remove it (for comparison later) and move on
             unset($menu_items[$menu_item_db_id]);
         }
     }
     // Remove menu items from the menu that weren't in $_POST
     if (!empty($menu_items)) {
         foreach (array_keys($menu_items) as $menu_item_id) {
             if (is_nav_menu_item($menu_item_id)) {
                 wp_delete_post($menu_item_id);
             }
         }
     }
     wp_defer_term_counting(false);
 }
/**
 * Saves nav menu items
 *
 * @since 3.6.0
 *
 * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
 * @param string $nav_menu_selected_title Title of the currently-selected menu
 * @return array $messages The menu updated message
 */
function wp_nav_menu_update_menu_items($nav_menu_selected_id, $nav_menu_selected_title)
{
    $unsorted_menu_items = wp_get_nav_menu_items($nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish'));
    $messages = array();
    $menu_items = array();
    // Index menu items by db ID
    foreach ($unsorted_menu_items as $_item) {
        $menu_items[$_item->db_id] = $_item;
    }
    $post_fields = array('menu-item-db-id', 'menu-item-object-id', 'menu-item-object', 'menu-item-parent-id', 'menu-item-position', 'menu-item-type', 'menu-item-title', 'menu-item-url', 'menu-item-description', 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn');
    wp_defer_term_counting(true);
    // Loop through all the menu items' POST variables
    if (!empty($_POST['menu-item-db-id'])) {
        foreach ((array) $_POST['menu-item-db-id'] as $_key => $k) {
            // Menu item title can't be blank
            if (!isset($_POST['menu-item-title'][$_key]) || '' == $_POST['menu-item-title'][$_key]) {
                continue;
            }
            $args = array();
            foreach ($post_fields as $field) {
                $args[$field] = isset($_POST[$field][$_key]) ? $_POST[$field][$_key] : '';
            }
            $menu_item_db_id = wp_update_nav_menu_item($nav_menu_selected_id, $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key, $args);
            if (is_wp_error($menu_item_db_id)) {
                $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
            } else {
                unset($menu_items[$menu_item_db_id]);
            }
        }
    }
    // Remove menu items from the menu that weren't in $_POST
    if (!empty($menu_items)) {
        foreach (array_keys($menu_items) as $menu_item_id) {
            if (is_nav_menu_item($menu_item_id)) {
                wp_delete_post($menu_item_id);
            }
        }
    }
    // Store 'auto-add' pages.
    $auto_add = !empty($_POST['auto-add-pages']);
    $nav_menu_option = (array) get_option('nav_menu_options');
    if (!isset($nav_menu_option['auto_add'])) {
        $nav_menu_option['auto_add'] = array();
    }
    if ($auto_add) {
        if (!in_array($nav_menu_selected_id, $nav_menu_option['auto_add'])) {
            $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
        }
    } else {
        if (false !== ($key = array_search($nav_menu_selected_id, $nav_menu_option['auto_add']))) {
            unset($nav_menu_option['auto_add'][$key]);
        }
    }
    // Remove nonexistent/deleted menus
    $nav_menu_option['auto_add'] = array_intersect($nav_menu_option['auto_add'], wp_get_nav_menus(array('fields' => 'ids')));
    update_option('nav_menu_options', $nav_menu_option);
    wp_defer_term_counting(false);
    /** This action is documented in wp-includes/nav-menu.php */
    do_action('wp_update_nav_menu', $nav_menu_selected_id);
    $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . sprintf(__('<strong>%1$s</strong> has been updated.'), $nav_menu_selected_title) . '</p></div>';
    unset($menu_items, $unsorted_menu_items);
    return $messages;
}
Exemple #8
0
 /**
  * Remove the _menu_item_visibility meta when the menu item is removed
  *
  * @since 0.2.2
  */
 function remove_visibility_meta($post_id)
 {
     if (is_nav_menu_item($post_id)) {
         delete_post_meta($post_id, '_menu_item_visibility');
     }
 }
/**
 * Clean up the icon meta field when a menu item is deleted
 *
 * @param int $post_id
 *
 * @since 1.6.8
 */
function themify_remove_menu_icon_meta($post_id)
{
    if (is_nav_menu_item($post_id)) {
        delete_post_meta($post_id, '_menu_item_icon');
    }
}
 function update_nav_menu($options, $clean)
 {
     $eo_options = get_option('eventorganiser_options');
     $menu = $clean['addtomenu'];
     $menu_item_db_id = isset($eo_options['menu_item_db_id']) ? (int) $eo_options['menu_item_db_id'] : 0;
     $current = isset($eo_options['addtomenu']) ? $eo_options['addtomenu'] : 0;
     if (!is_nav_menu_item($menu_item_db_id)) {
         $menu_item_db_id = 0;
         $current = '';
     }
     if ((empty($menu) || $menu == '1') && is_nav_menu_item($menu_item_db_id)) {
         wp_delete_post($menu_item_db_id, true);
         $menu_item_db_id = 0;
     }
     if (!empty($menu) && $menu != '1') {
         $menu_item_data = array();
         $menu_obj = wp_get_nav_menu_object($menu);
         $menu_id = $menu_obj ? $menu_obj->term_id : 0;
         $status = $menu_id == 0 ? '' : 'publish';
         $menu_item_data = array('menu-item-title' => $clean['navtitle'], 'menu-item-url' => get_post_type_archive_link('event'), 'menu-item-object' => 'event', 'menu-item-status' => $status, 'menu-item-type' => 'post_type_archive');
         $menu_item_db_id = wp_update_nav_menu_item($menu_id, $menu_item_db_id, $menu_item_data);
     }
     $clean['menu_item_db_id'] = $menu_item_db_id;
     return $clean;
 }
 function mltlngg_the_title_filter($title, $id)
 {
     global $mltlngg_options, $wpdb, $mltlngg_table_translate, $mltlngg_current_language, $mltlngg_active_language, $post;
     if (!is_nav_menu_item($id)) {
         /* Do not filter, if a navigation menu */
         $mltlngg_post_type = get_post_type($id);
         /* If current post type enabled to translation */
         if ($mltlngg_post_type == 'post' || $mltlngg_post_type == 'page') {
             if (is_admin()) {
                 $mltlngg_current_language = !empty($_SESSION['current_language']) ? $_SESSION['current_language'] : (isset($_GET['lang']) ? $_GET['lang'] : (!isset($_GET['message']) ? $mltlngg_options['default_language'] : $mltlngg_active_language['locale']));
             }
             $new_title = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$mltlngg_table_translate} WHERE `post_ID` = %d AND `language` = '%s'", $id, $mltlngg_current_language), ARRAY_A);
             /* If translation is exist and not empty, filter title */
             if (isset($new_title['post_title']) && "" != $new_title['post_title']) {
                 $title = $new_title['post_title'];
                 if (!is_admin()) {
                     if (!empty($post->post_password)) {
                         $protected_title_format = apply_filters('protected_title_format', __('Protected: %s', 'multilanguage'), $post);
                         $title = sprintf($protected_title_format, $title);
                     } else {
                         if (isset($post->post_status) && 'private' == $post->post_status) {
                             $private_title_format = apply_filters('private_title_format', __('Private: %s', 'multilanguage'), $post);
                             $title = sprintf($private_title_format, $title);
                         }
                     }
                 }
             }
         }
     }
     return $title;
 }
 /**
  * Get the ID of a menu item from a post ID.
  *
  * @since     1.0.0
  *
  * @param     int    $post_id    Post ID.
  *
  * @return    int                Corresponding menu item ID.
  */
 public function get_page_auto_menu_item_id($page_id)
 {
     // Check if page already has assigned menu item.
     $menu_item_id = get_post_meta($page_id, '_amfp_menu_item_id', true);
     // Get object id (post it points to) of menu item.
     $menu_item_object_id = get_post_meta($menu_item_id, '_menu_item_object_id', true);
     /**
      * If we already have a valid menu item ID, and it's points to
      * this post, then return it.
      */
     if ($menu_item_id && is_nav_menu_item($menu_item_id) && $menu_item_object_id == $page_id) {
         return $menu_item_id;
     }
     /**
      * If no menu item is set or the value isn't good, generate a new menu item
      * ID by incrementing the highest post ID in the database by one.
      */
     // Make sure ID isn't already taken.
     while (FALSE !== get_post_status($this->highest_db_post_id)) {
         $this->highest_db_post_id++;
     }
     $menu_item_id = ++$this->highest_db_post_id;
     // Add post meta to hold ID of associated menu item.
     update_post_meta($page_id, '_amfp_menu_item_id', $menu_item_id);
     return $menu_item_id;
 }
function hocwp_get_top_parent_menu_item($menu_item)
{
    $item = hocwp_get_parent_menu_item($menu_item);
    if (is_nav_menu_item($item)) {
        while ($item->menu_item_parent && hocwp_id_number_valid($item->menu_item_parent)) {
            $item = hocwp_get_parent_menu_item($item);
        }
    }
    return $item;
}
 /**
  * Adds new menu item to specified menu or removes it
  */
 public function update_menu($menu_id = NULL, $menu_item_title = '')
 {
     $menu_item_id = $this->options['general']['event_nav_menu']['item_id'];
     if (is_nav_menu_item($menu_item_id)) {
         $nav_menu_item = true;
         if ($menu_id === NULL) {
             wp_delete_post($menu_item_id, true);
             $menu_item_id = 0;
         }
     } else {
         $nav_menu_item = false;
         $menu_item_id = 0;
     }
     if (is_int($menu_id) && !empty($menu_id)) {
         if (($menu = wp_get_nav_menu_object($menu_id)) === false) {
             return false;
         }
         $menu_id = $menu->term_id;
         $menu_item_data = array('menu-item-title' => $menu_item_title, 'menu-item-url' => em_get_event_date_link(), 'menu-item-object' => 'event', 'menu-item-status' => $menu_id == 0 ? '' : 'publish', 'menu-item-type' => 'custom');
         if ($nav_menu_item === true) {
             $menu_item = wp_setup_nav_menu_item(get_post($menu_item_id));
             $menu_item_data['menu-item-parent-id'] = $menu_item->menu_item_parent;
             $menu_item_data['menu-item-position'] = $menu_item->menu_order;
         }
         $menu_item_id = wp_update_nav_menu_item($menu_id, $menu_item_id, $menu_item_data);
     }
     return $menu_item_id;
 }
Exemple #15
0
 public function delete_menu_item()
 {
     $menu_item_id = isset($_POST['menu_item_id']) ? intval($_POST['menu_item_id']) : false;
     $menu_items = isset($_POST['new_menu_order']) ? $_POST['new_menu_order'] : false;
     if ($menu_item_id) {
         if (is_nav_menu_item($menu_item_id) && wp_delete_post($menu_item_id, true)) {
             $messages[] = Upfront_UnewnavigationView::_get_l10n('delete_success');
         }
         //			update_post_meta($child['menu-item-db-id'], '_menu_item_menu_item_parent', 0);
         //echo $menu_items;
         //die(0);
         if ($menu_items) {
             $this->update_menu_order($menu_items);
             /*
             				foreach($menu_items as $menu_item) {
             	if(isset($menu_item['refresh-parent']) && $menu_item['refresh-parent'] == 1) {
             						echo "yes this happend";
             						return;
             	//				     $current_menu_item = get_post( $menu_item['menu-item-db-id'], 'ARRAY_A' );
             //						$current_menu_item['menu_order'] = $count;
             						delete_post_meta($menu_item['menu-item-db-id'], '_menu_item_menu_item_parent');
             		//				wp_update_post($current_menu_item);
             					}
             					$count++;
             				}
             */
         } else {
             $this->_out(new Upfront_JsonResponse_Success($messages));
         }
     }
     $this->_out(new Upfront_JsonResponse_Error(Upfront_UnewnavigationView::_get_l10n('cant_delete')));
 }
Exemple #16
0
 /**
  * When menu item removed remove menu image metadata.
  */
 public function menu_image_delete_menu_item_image_action()
 {
     $menu_item_id = (int) $_REQUEST['menu-item'];
     check_admin_referer('delete-menu_item_image_' . $menu_item_id);
     if (is_nav_menu_item($menu_item_id) && has_post_thumbnail($menu_item_id)) {
         delete_post_thumbnail($menu_item_id);
         delete_post_meta($menu_item_id, '_thumbnail_hover_id');
         delete_post_meta($menu_item_id, '_menu_item_image_size');
         delete_post_meta($menu_item_id, '_menu_item_image_title_position');
     }
 }
 function ut_wp_update_nav_menu_item($menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array())
 {
     $menu_id = (int) $menu_id;
     $menu_item_db_id = (int) $menu_item_db_id;
     // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
     if (!empty($menu_item_db_id) && !is_nav_menu_item($menu_item_db_id)) {
         return new WP_Error('update_nav_menu_item_failed', __('The given object ID is not that of a menu item.', 'unitedthemes'));
     }
     $menu = wp_get_nav_menu_object($menu_id);
     if (!$menu && 0 !== $menu_id || is_wp_error($menu)) {
         return $menu;
     }
     $defaults = array('menu-item-db-id' => $menu_item_db_id, 'menu-item-object-id' => 0, 'menu-item-object' => '', 'menu-item-parent-id' => 0, 'menu-item-position' => 0, 'menu-item-type' => 'custom', 'menu-item-title' => '', 'menu-item-url' => '', 'menu-item-description' => '', 'menu-item-attr-title' => '', 'menu-item-target' => '', 'menu-item-classes' => '', 'menu-item-xfn' => '', 'menu-item-menutype' => '', 'menu-item-status' => '');
     $args = wp_parse_args($menu_item_data, $defaults);
     if (0 == $menu_id) {
         $args['menu-item-position'] = 1;
     } elseif (0 == (int) $args['menu-item-position']) {
         $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items($menu_id, array('post_status' => 'publish,draft'));
         $last_item = array_pop($menu_items);
         $args['menu-item-position'] = $last_item && isset($last_item->menu_order) ? 1 + $last_item->menu_order : count($menu_items);
     }
     $original_parent = 0 < $menu_item_db_id ? get_post_field('post_parent', $menu_item_db_id) : 0;
     if ('custom' != $args['menu-item-type']) {
         /* if non-custom menu item, then:
          * use original object's URL
          * blank default title to sync with original object's
          */
         $args['menu-item-url'] = '';
         $original_title = '';
         if ('taxonomy' == $args['menu-item-type']) {
             $original_parent = get_term_field('parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw');
             $original_title = get_term_field('name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw');
         } elseif ('post_type' == $args['menu-item-type']) {
             $original_object = get_post($args['menu-item-object-id']);
             $original_parent = (int) $original_object->post_parent;
             $original_title = $original_object->post_title;
         }
         if ($args['menu-item-title'] == $original_title) {
             $args['menu-item-title'] = '';
         }
         // hack to get wp to create a post object when too many properties are empty
         if ('' == $args['menu-item-title'] && '' == $args['menu-item-description']) {
             $args['menu-item-description'] = ' ';
         }
     }
     // Populate the menu item object
     $post = array('menu_order' => $args['menu-item-position'], 'ping_status' => 0, 'post_content' => $args['menu-item-description'], 'post_excerpt' => $args['menu-item-attr-title'], 'post_parent' => $original_parent, 'post_title' => $args['menu-item-title'], 'post_type' => 'nav_menu_item');
     $update = 0 != $menu_item_db_id;
     // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
     if ($menu_id && (!$update || !is_object_in_term($menu_item_db_id, 'nav_menu', (int) $menu->term_id))) {
         $post['tax_input'] = array('nav_menu' => array(intval($menu->term_id)));
     }
     // New menu item. Default is draft status
     if (!$update) {
         $post['ID'] = 0;
         $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
         $menu_item_db_id = wp_insert_post($post);
         if (!$menu_item_db_id || is_wp_error($menu_item_db_id)) {
             return $menu_item_db_id;
         }
     }
     if ('custom' == $args['menu-item-type']) {
         $args['menu-item-object-id'] = $menu_item_db_id;
         $args['menu-item-object'] = 'custom';
     }
     $menu_item_db_id = (int) $menu_item_db_id;
     update_post_meta($menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']));
     update_post_meta($menu_item_db_id, '_menu_item_menu_item_parent', strval((int) $args['menu-item-parent-id']));
     update_post_meta($menu_item_db_id, '_menu_item_object_id', strval((int) $args['menu-item-object-id']));
     update_post_meta($menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']));
     update_post_meta($menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']));
     $args['menu-item-classes'] = array_map('sanitize_html_class', explode(' ', $args['menu-item-classes']));
     $args['menu-item-xfn'] = implode(' ', array_map('sanitize_html_class', explode(' ', $args['menu-item-xfn'])));
     update_post_meta($menu_item_db_id, '_menu_item_classes', $args['menu-item-classes']);
     update_post_meta($menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn']);
     update_post_meta($menu_item_db_id, '_menu_item_menutype', $args['menu_item_menutype']);
     update_post_meta($menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']));
     if (0 == $menu_id) {
         update_post_meta($menu_item_db_id, '_menu_item_orphaned', (string) time());
     } elseif (get_post_meta($menu_item_db_id, '_menu_item_orphaned')) {
         delete_post_meta($menu_item_db_id, '_menu_item_orphaned');
     }
     // Update existing menu item. Default is publish status
     if ($update) {
         $post['ID'] = $menu_item_db_id;
         $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
         wp_update_post($post);
     }
     do_action('ut_wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args);
     return $menu_item_db_id;
 }
 /**
  *
  *@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;
 }
Exemple #19
0
/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int $object_id The ID of the original object.
 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 * @return array The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type')
{
    $object_id = (int) $object_id;
    $menu_item_ids = array();
    $query = new WP_Query();
    $menu_items = $query->query(array('meta_key' => '_menu_item_object_id', 'meta_value' => $object_id, 'post_status' => 'any', 'post_type' => 'nav_menu_item', 'showposts' => -1));
    foreach ((array) $menu_items as $menu_item) {
        if (isset($menu_item->ID) && is_nav_menu_item($menu_item->ID)) {
            if (get_post_meta($menu_item->ID, '_menu_item_type', true) != $object_type) {
                continue;
            }
            $menu_item_ids[] = (int) $menu_item->ID;
        }
    }
    return array_unique($menu_item_ids);
}
 public function importMenu($menu)
 {
     wp_delete_nav_menu($menu['menu-name']);
     $newMenu = wp_update_nav_menu_object(0, $menu);
     if (is_wp_error($newTerm)) {
         return $newTerm;
     }
     $this->registry->set('term', $menu['id'], (int) $newMenu);
     foreach ($menu['items'] as $menuItem) {
         $existing = $this->registry->get('post', $menuItem['id']);
         if (is_nav_menu_item($existing)) {
             wp_delete_post($existing, true);
         }
         if ('taxonomy' == $menuItem['menu-item-type']) {
             $menuItem['menu-item-object-id'] = $this->registry->get('term', $menuItem['menu-item-object-id']);
         } elseif ('post_type' == $menuItem['menu-item-type']) {
             $menuItem['menu-item-object-id'] = $this->registry->get('post', $menuItem['menu-item-object-id']);
         }
         if (0 != (int) $menuItem['menu-item-parent-id']) {
             $menuItem['menu-item-parent-id'] = $this->registry->get('post', $menuItem['menu-item-parent-id']);
             if (is_null($menuItem['menu-item-parent-id'])) {
                 continue;
             }
         }
         $newMenuItem = wp_update_nav_menu_item($newMenu, 0, $menuItem);
         if (is_wp_error($newMenuItem)) {
             return $newMenuItem;
         }
         $this->registry->set('post', $menuItem['id'], (int) $newMenuItem);
     }
     $this->message = sprintf(__('Creating menus...', '__x__'), $menu['menu-name']);
     return true;
 }
         $args = array();
         foreach ($post_fields as $field) {
             $args[$field] = isset($_POST[$field][$_key]) ? $_POST[$field][$_key] : '';
         }
         $menu_item_db_id = wp_update_nav_menu_item($nav_menu_selected_id, $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key, $args);
         if (is_wp_error($menu_item_db_id)) {
             $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
         } elseif (isset($menu_items[$menu_item_db_id])) {
             unset($menu_items[$menu_item_db_id]);
         }
     }
 }
 // Remove menu items from the menu that weren't in $_POST
 if (!empty($menu_items)) {
     foreach (array_keys($menu_items) as $menu_item_id) {
         if (is_nav_menu_item($menu_item_id)) {
             wp_delete_post($menu_item_id);
         }
     }
 }
 // Store 'auto-add' pages.
 $auto_add = !empty($_POST['auto-add-pages']);
 $nav_menu_option = (array) get_option('nav_menu_options');
 if (!isset($nav_menu_option['auto_add'])) {
     $nav_menu_option['auto_add'] = array();
 }
 if ($auto_add) {
     if (!in_array($nav_menu_selected_id, $nav_menu_option['auto_add'])) {
         $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
     }
 } else {
 /**
  * Sync auto menu if certain actions have fired.
  *
  * @since  1.0.0
  */
 public function maybe_sync_auto_menu()
 {
     // Define actions that warrant rebuilding the menu.
     $trigger_actions = array('amfp_force_update', 'amfp_force_sync', 'save_post', 'updated_option', 'load-pages_page_mypageorder');
     // Check if any have been triggered.
     $update_menu = false;
     foreach ($trigger_actions as $action) {
         if (did_action($action)) {
             $update_menu = true;
             break;
         }
     }
     // Only run if something changed on this load.
     if (!$update_menu) {
         return;
     }
     // Get auto menu ID.
     $auto_menu_id = $this->get_auto_menu_id();
     // Get saved auto menu items.
     $menu = wp_get_nav_menu_object($auto_menu_id);
     // Exit if the menu doesn't exist (edge case after deactivation)
     if (empty($menu->term_id)) {
         return false;
     }
     $menu_item_ids = get_objects_in_term($menu->term_id, 'nav_menu');
     // Get array of non-excluded pages.
     $args = array('post_type' => 'page', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'menu_order post_modified', 'meta_query' => array('relation' => 'OR', array('key' => '_amfp_exclude_from_menu', 'compare' => '!=', 'value' => 1), array('key' => '_amfp_exclude_from_menu', 'compare' => 'NOT EXISTS', 'value' => 1)));
     $pages = get_posts($args);
     $i = 1;
     foreach ($pages as $index => $page) {
         // Exclude this page if it has an excluded ancestor.
         if ($this->has_excluded_ancestor($page->ID)) {
             unset($pages[$index]);
             continue;
         }
         // Setup menu item database ID based on post ID.
         $menu_item_db_id = $this->get_page_auto_menu_item_id($page->ID);
         // Set up menu item parent database ID.
         $parent_menu_item_db_id = 0;
         if ($page->post_parent) {
             $parent_menu_item_db_id = $this->get_page_auto_menu_item_id($page->post_parent);
         }
         // Set up menu item args from page.
         $args = array('menu-item-object-id' => $page->ID, 'menu-item-object' => $page->post_type, 'menu-item-type' => 'post_type', 'menu-item-parent-id' => $parent_menu_item_db_id, 'menu-item-position' => $i, 'menu-item-status' => 'publish');
         if (!is_nav_menu_item($menu_item_db_id)) {
             $temp_menu_item_post = array('import_id' => $menu_item_db_id, 'post_type' => 'nav_menu_item');
             wp_insert_post($temp_menu_item_post);
         }
         $item = wp_update_nav_menu_item($auto_menu_id, $menu_item_db_id, $args);
         $i++;
     }
     // Get all non-excluded page ID's.
     $included_menu_item_ids = array_map(function ($page) {
         return $this->get_page_auto_menu_item_id($page->ID);
     }, $pages);
     // Remove any items that aren't published or are otherwise excluded.
     foreach ($menu_item_ids as $menu_item_id) {
         $object_id = get_post_meta($menu_item_id, '_menu_item_object_id', true);
         $post_status = get_post_status($object_id);
         if (!in_array($menu_item_id, $included_menu_item_ids) || 'publish' != $post_status) {
             wp_delete_post($menu_item_id);
         }
     }
     // Die properly if called via AJAX.
     if (defined('DOING_AJAX') && DOING_AJAX) {
         wp_die();
     }
 }
                     }
                     // else this menu item is not a child of the previous
                 } elseif (empty($menu_item_data['menu_order']) || empty($menu_item_data['menu_item_parent']) || !in_array($menu_item_data['menu_item_parent'], array_keys($dbids_to_orders)) || empty($orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1]) || $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent']) {
                     // just make it a child of the previous; keep the order
                     $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1];
                     update_post_meta($menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent']);
                     wp_update_post($menu_item_data);
                 }
             }
         }
     }
     break;
 case 'delete-menu-item':
     $menu_item_id = (int) $_REQUEST['menu-item'];
     check_admin_referer('delete-menu_item_' . $menu_item_id);
     if (is_nav_menu_item($menu_item_id) && wp_delete_post($menu_item_id, true)) {
         $messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
     }
     break;
 case 'delete':
     check_admin_referer('delete-nav_menu-' . $nav_menu_selected_id);
     if (is_nav_menu($nav_menu_selected_id)) {
         $deletion = wp_delete_nav_menu($nav_menu_selected_id);
     } else {
         // Reset the selected menu
         $nav_menu_selected_id = 0;
         unset($_REQUEST['menu']);
     }
     if (!isset($deletion)) {
         break;
     }
 /**
  * Clean up the icon meta field when a menu item is deleted
  *
  * @param int $post_id
  */
 function delete_post($post_id)
 {
     if (is_nav_menu_item($post_id)) {
         delete_post_meta($post_id, '_menu_item_icon');
     }
 }
/**
 * Get the menu items associated with a particular object.
 *
 * @since 3.0.0
 *
 * @param int $object_id The ID of the original object.
 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
 * @return array The array of menu item IDs; empty array if none;
 */
function wp_get_associated_nav_menu_items($object_id = 0, $object_type = 'post_type', $taxonomy = '')
{
    $object_id = (int) $object_id;
    $menu_item_ids = array();
    $query = new WP_Query();
    $menu_items = $query->query(array('meta_key' => '_menu_item_object_id', 'meta_value' => $object_id, 'post_status' => 'any', 'post_type' => 'nav_menu_item', 'posts_per_page' => -1));
    foreach ((array) $menu_items as $menu_item) {
        if (isset($menu_item->ID) && is_nav_menu_item($menu_item->ID)) {
            $menu_item_type = get_post_meta($menu_item->ID, '_menu_item_type', true);
            if ('post_type' == $object_type && 'post_type' == $menu_item_type) {
                $menu_item_ids[] = (int) $menu_item->ID;
            } else {
                if ('taxonomy' == $object_type && 'taxonomy' == $menu_item_type && get_post_meta($menu_item->ID, '_menu_item_object', true) == $taxonomy) {
                    $menu_item_ids[] = (int) $menu_item->ID;
                }
            }
        }
    }
    return array_unique($menu_item_ids);
}
 public function fix_wp_menu()
 {
     if (!current_user_can('edit_theme_options')) {
         wp_die(-1);
     }
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     require_once ABSPATH . 'wp-includes/post.php';
     global $wpdb;
     // $wpdb->show_errors(); // uncomment if need to debug query
     $menus = get_nav_menu_locations();
     $menu_id = $_POST['theme_location'];
     if (!in_array($menu_id, $menus)) {
         throw new Exception('Menu location not configured: ' . $_POST['theme_location']);
     }
     // delete existing menu items (they be b0rk3d)
     $deleted_menu_ids = array();
     $unsorted_menu_items = wp_get_nav_menu_items($menu_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish'));
     $menu_items = array();
     // Index menu items by db ID
     foreach ($unsorted_menu_items as $_item) {
         $menu_items[$_item->db_id] = $_item;
     }
     // Remove menu items from the menu that weren't in $_POST
     if (!empty($menu_items)) {
         foreach (array_keys($menu_items) as $menu_item_id) {
             if (is_nav_menu_item($menu_item_id)) {
                 wp_delete_post($menu_item_id);
                 $deleted_menu_ids[] = $menu_item_id;
             }
         }
     }
     echo 'Deleted ' . count($deleted_menu_ids) . ' menu items<br />';
     $table_name = $wpdb->prefix . Mega_Menu::$table_name;
     $menu_items = $wpdb->get_results($wpdb->prepare("SELECT `{$table_name}`.`ID`, `{$table_name}`.`post_id`, `{$table_name}`.`parent_id`, `{$table_name}`.`position`, `{$table_name}`.`data`\n\t\t\t\t\tFROM `{$table_name}`\n\t\t\t\t\tWHERE `{$table_name}`.`menu_id`=%d\n\t\t\t\t\tORDER BY `{$table_name}`.`position`", $menu_id));
     // stores a map of old menu item IDs to new menu item IDs (for parents)
     $map_menu_item_ids = array();
     foreach ($menu_items as $menu_item) {
         $data = json_decode($menu_item->data, true);
         // return as assoc array
         foreach ($data as $key => $value) {
             $key = str_replace('menu-item-', '', $key);
             $key = str_replace('-', '_', $key);
             if ($value && !isset($menu_item->{$key})) {
                 $menu_item->{$key} = $value;
             }
         }
         if ($menu_item->type == 'column') {
             $title = json_encode(array('url' => $menu_item->url ?: '', 'title' => $menu_item->title ?: ''));
         } else {
             if ($menu_item->type == 'menu') {
                 $title = json_encode(array('menu' => $menu_item->menu, 'title' => $menu_item->title ?: ''));
             } else {
                 $title = $menu_item->title ?: '';
             }
         }
         $menu_item_data = array('menu-item-db-id' => 0, 'menu-item-object-id' => $menu_item->object_id, 'menu-item-object' => $menu_item->object, 'menu-item-parent-id' => $map_menu_item_ids[$menu_item->parent_id], 'menu-item-position' => $menu_item->position, 'menu-item-type' => $menu_item->type, 'menu-item-title' => $title, 'menu-item-url' => isset($menu_item->url) ? $menu_item->url : '', 'menu-item-description' => $menu_item->description, 'menu-item-attr-title' => $menu_item->attr_title, 'menu-item-target' => isset($menu_item->target) ? $menu_item->target : '', 'menu-item-classes' => implode(' ', $menu_item->classes), 'menu-item-xfn' => isset($menu_item->xfn) ? $menu_item->xfn : '', 'menu-item-status' => 'publish');
         $id = wp_update_nav_menu_item($menu_id, 0, $menu_item_data);
         $map_menu_item_ids[$menu_item->db_id] = $id;
     }
     echo 'Inserted ' . count($map_menu_item_ids) . ' menu items<br />';
     die;
 }