/** * Set the terms for a post. * * @since 0.0.1 * * @see hq_set_object_terms() * * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. * @param string $tags Optional. The tags to set for the post, separated by commas. Default empty. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. * @param bool $append Optional. If true, don't delete existing tags, just add on. If false, * replace the tags with the new tags. Default false. * @return array|false|HQ_Error Array of affected term IDs. HQ_Error or false on failure. */ function hq_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false) { $post_id = (int) $post_id; if (!$post_id) { return false; } if (empty($tags)) { $tags = array(); } if (!is_array($tags)) { $comma = _x(',', 'tag delimiter'); if (',' !== $comma) { $tags = str_replace($comma, ',', $tags); } $tags = explode(',', trim($tags, " \n\t\r\v,")); } /* * Hierarchical taxonomies must always pass IDs rather than names so that * children with the same names but different parents aren't confused. */ if (is_taxonomy_hierarchical($taxonomy)) { $tags = array_unique(array_map('intval', $tags)); } return hq_set_object_terms($post_id, $tags, $taxonomy, $append); }
/** * Save the properties of a menu item or create a new one. * * @since 0.0.1 * * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. * @param array $menu_item_data The menu item's data. * @return int|WP_Error The menu item's database ID or WP_Error object on failure. */ function hq_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.')); } $menu = hq_get_nav_menu_object($menu_id); if (!$menu && 0 !== $menu_id) { return new WP_Error('invalid_menu_id', __('Invalid menu ID.')); } if (is_hq_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-status' => ''); $args = hq_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) hq_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 hq 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; // 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 = hq_insert_post($post); if (!$menu_item_db_id || is_hq_error($menu_item_db_id)) { return $menu_item_db_id; } } // Associate the menu item with the menu term // Only set the menu term if it isn't set to avoid unnecessary hq_get_object_terms() if ($menu_id && (!$update || !is_object_in_term($menu_item_db_id, 'nav_menu', (int) $menu->term_id))) { hq_set_object_terms($menu_item_db_id, array($menu->term_id), 'nav_menu'); } 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_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'; hq_update_post($post); } /** * Fires after a navigation menu item has been updated. * * @since 0.0.1 * * @see hq_update_nav_menu_item() * * @param int $menu_id ID of the updated menu. * @param int $menu_item_db_id ID of the updated menu item. * @param array $args An array of arguments used to update a menu item. */ do_action('hq_update_nav_menu_item', $menu_id, $menu_item_db_id, $args); return $menu_item_db_id; }
/** * Update link with the specified link categories. * * @since 0.0.1 * * @param int $link_id ID of link to update * @param array $link_categories Array of categories to */ function hq_set_link_cats($link_id = 0, $link_categories = array()) { // If $link_categories isn't already an array, make it one: if (!is_array($link_categories) || 0 == count($link_categories)) { $link_categories = array(get_option('default_link_category')); } $link_categories = array_map('intval', $link_categories); $link_categories = array_unique($link_categories); hq_set_object_terms($link_id, $link_categories, 'link_category'); clean_bookmark_cache($link_id); }