if ('link_category' == $tax->name) { $parent_file = 'link-manager.php'; $submenu_file = 'edit-tags.php?taxonomy=link_category'; } else { $parent_file = 'edit.php'; $submenu_file = "edit-tags.php?taxonomy={$taxonomy}"; } } add_screen_option('per_page', array('label' => $title, 'default' => 20, 'option' => 'edit_' . $tax->name . '_per_page')); switch ($nxt_list_table->current_action()) { case 'add-tag': check_admin_referer('add-tag', '_nxtnonce_add-tag'); if (!current_user_can($tax->cap->edit_terms)) { nxt_die(__('Cheatin’ uh?')); } $ret = nxt_insert_term($_POST['tag-name'], $taxonomy, $_POST); $location = 'edit-tags.php?taxonomy=' . $taxonomy; if ('post' != $post_type) { $location .= '&post_type=' . $post_type; } if ($referer = nxt_get_original_referer()) { if (false !== strpos($referer, 'edit-tags.php')) { $location = $referer; } } if ($ret && !is_nxt_error($ret)) { $location = add_query_arg('message', 1, $location); } else { $location = add_query_arg('message', 4, $location); } nxt_redirect($location);
/** * Create Term and Taxonomy Relationships. * * Relates an object (post, link etc) to a term and taxonomy type. Creates the * term and taxonomy relationship if it doesn't already exist. Creates a term if * it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. * A term has no meaning until it is given context by defining which taxonomy it * exists under. * * @package NXTClass * @subpackage Taxonomy * @since 2.3.0 * @uses $nxtdb * * @param int $object_id The object to relate to. * @param array|int|string $terms The slug or id of the term, will replace all existing * related terms in this taxonomy. * @param array|string $taxonomy The context in which to relate the term to the object. * @param bool $append If false will delete difference of terms. * @return array|nxt_Error Affected Term IDs */ function nxt_set_object_terms($object_id, $terms, $taxonomy, $append = false) { global $nxtdb; $object_id = (int) $object_id; if (!taxonomy_exists($taxonomy)) { return new nxt_Error('invalid_taxonomy', __('Invalid Taxonomy')); } if (!is_array($terms)) { $terms = array($terms); } if (!$append) { $old_tt_ids = nxt_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none')); } else { $old_tt_ids = array(); } $tt_ids = array(); $term_ids = array(); $new_tt_ids = array(); foreach ((array) $terms as $term) { if (!strlen(trim($term))) { continue; } if (!($term_info = term_exists($term, $taxonomy))) { // Skip if a non-existent term ID is passed. if (is_int($term)) { continue; } $term_info = nxt_insert_term($term, $taxonomy); } if (is_nxt_error($term_info)) { return $term_info; } $term_ids[] = $term_info['term_id']; $tt_id = $term_info['term_taxonomy_id']; $tt_ids[] = $tt_id; if ($nxtdb->get_var($nxtdb->prepare("SELECT term_taxonomy_id FROM {$nxtdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id))) { continue; } do_action('add_term_relationship', $object_id, $tt_id); $nxtdb->insert($nxtdb->term_relationships, array('object_id' => $object_id, 'term_taxonomy_id' => $tt_id)); do_action('added_term_relationship', $object_id, $tt_id); $new_tt_ids[] = $tt_id; } if ($new_tt_ids) { nxt_update_term_count($new_tt_ids, $taxonomy); } if (!$append) { $delete_terms = array_diff($old_tt_ids, $tt_ids); if ($delete_terms) { $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; do_action('delete_term_relationships', $object_id, $delete_terms); $nxtdb->query($nxtdb->prepare("DELETE FROM {$nxtdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ({$in_delete_terms})", $object_id)); do_action('deleted_term_relationships', $object_id, $delete_terms); nxt_update_term_count($delete_terms, $taxonomy); } } $t = get_taxonomy($taxonomy); if (!$append && isset($t->sort) && $t->sort) { $values = array(); $term_order = 0; $final_tt_ids = nxt_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); foreach ($tt_ids as $tt_id) { if (in_array($tt_id, $final_tt_ids)) { $values[] = $nxtdb->prepare("(%d, %d, %d)", $object_id, $tt_id, ++$term_order); } } if ($values) { $nxtdb->query("INSERT INTO {$nxtdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); } } do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); return $tt_ids; }
/** * {@internal Missing Short Description}} * * @since 2.8.0 * * @param unknown_type $tag_name * @return unknown */ function nxt_create_term($tag_name, $taxonomy = 'post_tag') { if ($id = term_exists($tag_name, $taxonomy)) { return $id; } return nxt_insert_term($tag_name, $taxonomy); }
function _nxt_ajax_add_hierarchical_term() { $action = $_POST['action']; $taxonomy = get_taxonomy(substr($action, 4)); check_ajax_referer($action, '_ajax_nonce-add-' . $taxonomy->name); if (!current_user_can($taxonomy->cap->edit_terms)) { die('-1'); } $names = explode(',', $_POST['new' . $taxonomy->name]); $parent = isset($_POST['new' . $taxonomy->name . '_parent']) ? (int) $_POST['new' . $taxonomy->name . '_parent'] : 0; if (0 > $parent) { $parent = 0; } if ($taxonomy->name == 'category') { $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array(); } else { $post_category = isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ? (array) $_POST['tax_input'][$taxonomy->name] : array(); } $checked_categories = array_map('absint', (array) $post_category); $popular_ids = nxt_popular_terms_checklist($taxonomy->name, 0, 10, false); foreach ($names as $cat_name) { $cat_name = trim($cat_name); $category_nicename = sanitize_title($cat_name); if ('' === $category_nicename) { continue; } if (!($cat_id = term_exists($cat_name, $taxonomy->name, $parent))) { $new_term = nxt_insert_term($cat_name, $taxonomy->name, array('parent' => $parent)); $cat_id = $new_term['term_id']; } $checked_categories[] = $cat_id; if ($parent) { // Do these all at once in a second continue; } $category = get_term($cat_id, $taxonomy->name); ob_start(); nxt_terms_checklist(0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); $data = ob_get_contents(); ob_end_clean(); $add = array('what' => $taxonomy->name, 'id' => $cat_id, 'data' => str_replace(array("\n", "\t"), '', $data), 'position' => -1); } if ($parent) { // Foncy - replace the parent and all its children $parent = get_term($parent, $taxonomy->name); $term_id = $parent->term_id; while ($parent->parent) { // get the top parent $parent =& get_term($parent->parent, $taxonomy->name); if (is_nxt_error($parent)) { break; } $term_id = $parent->term_id; } ob_start(); nxt_terms_checklist(0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); $data = ob_get_contents(); ob_end_clean(); $add = array('what' => $taxonomy->name, 'id' => $term_id, 'data' => str_replace(array("\n", "\t"), '', $data), 'position' => -1); } ob_start(); nxt_dropdown_categories(array('taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new' . $taxonomy->name . '_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —')); $sup = ob_get_contents(); ob_end_clean(); $add['supplemental'] = array('newcat_parent' => $sup); $x = new nxt_Ajax_Response($add); $x->send(); }
/** * Save the properties of a menu or create a new menu with those properties. * * @since 3.0.0 * * @param int $menu_id The ID of the menu or "0" to create a new menu. * @param array $menu_data The array of menu data. * @return int|error object The menu's ID or nxt_Error object. */ function nxt_update_nav_menu_object($menu_id = 0, $menu_data = array()) { $menu_id = (int) $menu_id; $_menu = nxt_get_nav_menu_object($menu_id); $args = array('description' => isset($menu_data['description']) ? $menu_data['description'] : '', 'name' => isset($menu_data['menu-name']) ? $menu_data['menu-name'] : '', 'parent' => isset($menu_data['parent']) ? (int) $menu_data['parent'] : 0, 'slug' => null); // double-check that we're not going to have one menu take the name of another $_possible_existing = get_term_by('name', $menu_data['menu-name'], 'nav_menu'); if ($_possible_existing && !is_nxt_error($_possible_existing) && isset($_possible_existing->term_id) && $_possible_existing->term_id != $menu_id) { return new nxt_Error('menu_exists', sprintf(__('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html($menu_data['menu-name']))); } // menu doesn't already exist, so create a new menu if (!$_menu || is_nxt_error($_menu)) { $menu_exists = get_term_by('name', $menu_data['menu-name'], 'nav_menu'); if ($menu_exists) { return new nxt_Error('menu_exists', sprintf(__('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html($menu_data['menu-name']))); } $_menu = nxt_insert_term($menu_data['menu-name'], 'nav_menu', $args); if (is_nxt_error($_menu)) { return $_menu; } do_action('nxt_create_nav_menu', $_menu['term_id'], $menu_data); return (int) $_menu['term_id']; } if (!$_menu || !isset($_menu->term_id)) { return 0; } $menu_id = (int) $_menu->term_id; $update_response = nxt_update_term($menu_id, 'nav_menu', $args); if (is_nxt_error($update_response)) { return $update_response; } do_action('nxt_update_nav_menu', $menu_id, $menu_data); return $menu_id; }
/** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_posts() { foreach ($this->posts as $post) { if (!post_type_exists($post['post_type'])) { printf(__('Failed to import “%s”: Invalid post type %s', 'nxtclass-importer'), esc_html($post['post_title']), esc_html($post['post_type'])); echo '<br />'; continue; } if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { $this->process_menu_item($post); continue; } $post_type_object = get_post_type_object($post['post_type']); $post_exists = post_exists($post['post_title'], '', $post['post_date']); if ($post_exists) { printf(__('%s “%s” already exists.', 'nxtclass-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); echo '<br />'; $comment_post_ID = $post_id = $post_exists; } else { $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } // map the post author $author = sanitize_user($post['post_author'], true); if (isset($this->author_mapping[$author])) { $author = $this->author_mapping[$author]; } else { $author = (int) get_current_user_id(); } $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); if ('attachment' == $postdata['post_type']) { $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; // try to use _nxt_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_nxt_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); } else { $comment_post_ID = $post_id = nxt_insert_post($postdata, true); } if (is_nxt_error($post_id)) { printf(__('Failed to import %s “%s”', 'nxtclass-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $post_id->get_error_message(); } echo '<br />'; continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; // add categories, tags and other terms if (!empty($post['terms'])) { $terms_to_set = array(); foreach ($post['terms'] as $term) { // back compat with WXR 1.0 map 'tag' to 'post_tag' $taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain']; $term_exists = term_exists($term['slug'], $taxonomy); $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; if (!$term_id) { $t = nxt_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); if (!is_nxt_error($t)) { $term_id = $t['term_id']; } else { printf(__('Failed to import %s %s', 'nxtclass-importer'), esc_html($taxonomy), esc_html($term['name'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $t->get_error_message(); } echo '<br />'; continue; } } $terms_to_set[$taxonomy][] = intval($term_id); } foreach ($terms_to_set as $tax => $ids) { $tt_ids = nxt_set_post_terms($post_id, $ids, $tax); } unset($post['terms'], $terms_to_set); } // add/update comments if (!empty($post['comments'])) { $num_comments = 0; $inserted_comments = array(); foreach ($post['comments'] as $comment) { $comment_id = $comment['comment_id']; $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); if (isset($this->processed_authors[$comment['comment_user_id']])) { $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; } } ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = nxt_filter_comment($comment); $inserted_comments[$key] = nxt_insert_comment($comment); foreach ($comment['commentmeta'] as $meta) { $value = maybe_unserialize($meta['value']); add_comment_meta($inserted_comments[$key], $meta['key'], $value); } $num_comments++; } } unset($newcomments, $inserted_comments, $post['comments']); } // add/update post meta if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('import_post_meta_key', $meta['key']); $value = false; if ('_edit_last' == $key) { if (isset($this->processed_authors[intval($meta['value'])])) { $value = $this->processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } add_post_meta($post_id, $key, $value); do_action('import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $this->featured_images[$post_id] = (int) $value; } } } } } unset($this->posts); }