function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '')); // vars $r = array(); $args = array('hide_empty' => false); // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // search if ($options['s']) { $args['search'] = $options['s']; } // filters $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']); // get terms $terms = get_terms($field['taxonomy'], $args); // sort into hierachial order! if (is_taxonomy_hierarchical($field['taxonomy'])) { // get parent $parent = acf_maybe_get($args, 'parent', 0); $parent = acf_maybe_get($args, 'child_of', $parent); // this will fail if a search has taken place because parents wont exist if (empty($args['search'])) { $terms = _get_term_children($parent, $terms, $field['taxonomy']); } } /// append to r foreach ($terms as $term) { // add to json $r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id'])); } // return return $r; }
public function isParentOf($query) { $this->setupBaseInfo(); if (!$query->is_archive || !is_taxonomy_hierarchical($this->type)) { return false; } $taxonomy = null; $term_id = null; if ($query->is_tax) { $taxonomy = $query->query_vars['taxonomy']; $term = get_term_by('slug', $query->query['term'], $query->query_vars['taxonomy'], OBJECT, 'raw'); if ($term === false) { return false; } $term_id = $term->term_id; } else { if ($query->is_category) { $taxonomy = "category"; $term_id = $query->query_vars['cat']; } } if ($this->type != $taxonomy) { return false; } $depth = 0; return $this->findChild($this->id, $term_id, $this->children, $depth); }
/** * Plugin Name: Site Breadcrumbs * Plugin URI: https://wordpress.com * Description: Quickly add breadcrumbs to the single view of a hierarchical post type or a hierarchical taxonomy. * Author: Automattic * Version: 1.0 * Author URI: https://wordpress.com * License: GPL2 or later */ function jetpack_breadcrumbs() { $taxonomy = is_category() ? 'category' : get_query_var('taxonomy'); $is_taxonomy_hierarchical = is_taxonomy_hierarchical($taxonomy); $post_type = is_page() ? 'page' : get_query_var('post_type'); $is_post_type_hierarchical = is_post_type_hierarchical($post_type); if (!($is_post_type_hierarchical || $is_taxonomy_hierarchical) || is_front_page()) { return; } $breadcrumb = ''; if ($is_post_type_hierarchical) { $post_id = get_queried_object_id(); $ancestors = array_reverse(get_post_ancestors($post_id)); if ($ancestors) { foreach ($ancestors as $ancestor) { $breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="' . esc_url(get_permalink($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_html(get_the_title($ancestor)) . '</span></a></span>'; } } $breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">' . esc_html(get_the_title($post_id)) . '</span></span>'; } elseif ($is_taxonomy_hierarchical) { $current = get_term(get_queried_object_id(), $taxonomy); if (is_wp_error($current)) { return; } if ($current->parent) { $breadcrumb = jetpack_get_term_parents($current->parent, $taxonomy); } $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">' . esc_html($current->name) . '</span></span>'; } $home = '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="' . esc_url(home_url('/')) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__('Home', 'jetpack') . '</span></a></span>'; echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; }
function start_el(&$output, $term, $depth = 0, $args = array(), $id = 0) { global $post; /* $args array includes: taxonomy disabled selected_cats popular_cats has_children */ //echo '<pre>'; var_dump( $args ); echo '</pre>'; extract($args); if (empty($taxonomy)) { $taxonomy = 'category'; } $name = 'radio_tax_input[' . $taxonomy . ']'; //get first term object $current_term = !empty($selected_cats) && !is_wp_error($selected_cats) ? array_pop($selected_cats) : false; // if no term, match the 0 "no term" option $current_id = $current_term ? $current_term : 0; //small tweak so that it works for both hierarchical and non-hierarchical tax $value = is_taxonomy_hierarchical($taxonomy) ? $term->term_id : $term->slug; $class = in_array($term->term_id, $popular_cats) ? ' class="popular-category"' : ''; $output .= sprintf("\n" . '<li id="%1$s-%2$s" %3$s><label class="selectit"><input id="%4$s" type="radio" name="%5$s" value="%6$s" %7$s %8$s/> %9$s</label>', $taxonomy, $value, $class, "in-{$taxonomy}-{$term->term_id}", $name . '[]', esc_attr(trim($value)), checked($current_id, $term->term_id, false), disabled(empty($args['disabled']), false, false), esc_html(apply_filters('the_category', $term->name))); }
/** * put your comment there... * */ protected function isHierarchical() { // Initialize. $typeParams =& $this->getTypeParams(); // Check if post_type hierarchical. return is_taxonomy_hierarchical($typeParams['type']); }
/** * @param $filters * @param $operand * * @return array|null */ public static function form_tax_query($filters, $operand) { if (empty($filters)) { return null; } $tax_query = array(); foreach ($filters as $tax => $terms) { if (empty($terms)) { continue; } $tax_operand = 'AND'; if ($operand == 'OR') { $tax_operand = 'IN'; } if ('AND' === $tax_operand && is_taxonomy_hierarchical($tax)) { /* * When making and AND query on a hierarchical taxonomy where 'include_children' * is true (the default), WP requires all matches to have ALL child terms, * not just one child of each of the supplied terms. By breaking this up * into multiple tax queries ANDed together, you get the results that * are more naturally expected. */ foreach ($terms as $term) { $tax_query[] = array('taxonomy' => $tax, 'field' => 'id', 'terms' => array($term)); } } else { $tax_query[] = array('taxonomy' => $tax, 'field' => 'id', 'operator' => $tax_operand, 'terms' => $terms); } } if (count($tax_query) > 1) { $tax_query['relation'] = $operand; } return $tax_query; }
function wpcf7_flamingo_add_channel($slug, $name = '') { if (!class_exists('Flamingo_Inbound_Message')) { return false; } $parent = term_exists('contact-form-7', Flamingo_Inbound_Message::channel_taxonomy); if (!$parent) { $parent = wp_insert_term(__('Contact Form 7', 'contact-form-7'), Flamingo_Inbound_Message::channel_taxonomy, array('slug' => 'contact-form-7')); if (is_wp_error($parent)) { return false; } } $parent = (int) $parent['term_id']; if (!is_taxonomy_hierarchical(Flamingo_Inbound_Message::channel_taxonomy)) { // backward compat for Flamingo 1.0.4 and lower return $parent; } if (empty($name)) { $name = $slug; } $channel = term_exists($slug, Flamingo_Inbound_Message::channel_taxonomy, $parent); if (!$channel) { $channel = wp_insert_term($name, Flamingo_Inbound_Message::channel_taxonomy, array('slug' => $slug, 'parent' => $parent)); if (is_wp_error($channel)) { return false; } } return (int) $channel['term_id']; }
/** * Return the terms for the given type. * * @param int $site_id Blog ID. * * @return array */ public function get_terms_for_site($site_id) { $out = []; switch_to_blog($site_id); $taxonomy_object = get_taxonomy($this->taxonomy_name); if (!current_user_can($taxonomy_object->cap->edit_terms)) { $terms = []; } else { $terms = get_terms($this->taxonomy_name, ['hide_empty' => FALSE]); } foreach ($terms as $term) { if (is_taxonomy_hierarchical($this->taxonomy_name)) { $ancestors = get_ancestors($term->term_id, $this->taxonomy_name); if (!empty($ancestors)) { foreach ($ancestors as $ancestor) { $parent_term = get_term($ancestor, $this->taxonomy_name); $term->name = $parent_term->name . '/' . $term->name; } } } $out[$term->term_taxonomy_id] = esc_html($term->name); } restore_current_blog(); uasort($out, 'strcasecmp'); return $out; }
function widget($args, $instance) { global $wp_query; extract($args); $current_taxonomy = $this->_get_current_taxonomy($instance); if (!empty($instance['title'])) { $title = $instance['title']; } else { if ('post_tag' == $current_taxonomy) { $title = __('Tags'); } else { $tax = get_taxonomy($current_taxonomy); $title = $tax->labels->name; } } $use_desc_for_title = isset($instance['use_desc_for_title']) ? $instance['use_desc_for_title'] : true; $count = isset($instance['count']) ? $instance['count'] : false; $title = apply_filters('widget_title', $title, $instance, $this->id_base); $current_term = false; $current_term_id = $current_post_id = 0; if (suwp::is_tax($current_taxonomy)) { $current_term = $wp_query->get_queried_object(); $current_term_id = $wp_query->get_queried_object_id(); $title = $current_term->name; } elseif (is_singular()) { $current_post_id = $wp_query->get_queried_object_id(); $post_terms = get_the_terms($current_post_id, $current_taxonomy); if (is_array($post_terms) && count($post_terms)) { $current_term = reset($post_terms); $current_term_id = $current_term->term_id; $title = $current_term->name; } } $term_args = array('taxonomy' => $current_taxonomy, 'orderby' => 'name', 'show_count' => $count ? '1' : '0', 'hierarchical' => '0', 'title_li' => '', 'parent' => $current_term_id, 'show_option_none' => false, 'use_desc_for_title' => $use_desc_for_title ? '1' : '0', 'echo' => false); $category_output = $post_output = ''; if (!$current_term || is_taxonomy_hierarchical($current_taxonomy)) { $category_output = wp_list_categories($term_args); } if ($current_term) { $child_posts = get_posts(array('taxonomy' => $current_taxonomy, 'term' => $current_term->slug, 'numberposts' => 5)); foreach ($child_posts as $child_post) { $css_class = ''; if ($child_post->ID == $current_post_id) { $css_class = 'current_post_item'; } $post_output .= "\n\t\t\t<li class=\"" . $css_class . '"><a href="' . get_permalink($child_post->ID) . '" title="' . esc_attr(wp_strip_all_tags(apply_filters('the_title', $child_post->post_title, $child_post->ID))) . '">' . apply_filters('the_title', $child_post->post_title, $child_post->ID) . "</a></li>\n"; } } if ($category_output || $post_output) { echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } echo "\n\t\t<ul>\n"; echo $category_output; echo $post_output; echo "\n\t\t</ul>\n"; echo $after_widget; } }
public function get_markup() { $walker = new WCMF_walker(); foreach ($this->taxonomies as $tax) { wp_dropdown_categories(array('taxonomy' => $tax, 'hide_if_empty' => true, 'show_option_all' => sprintf(get_taxonomy($tax)->labels->all_items), 'hide_empty' => true, 'hierarchical' => is_taxonomy_hierarchical($tax), 'show_count' => true, 'orderby' => 'name', 'selected' => '0' !== get_query_var($tax) ? get_query_var($tax) : false, 'name' => $tax, 'id' => $tax, 'walker' => $walker)); } }
function wplb_get_taxonomies_by_post_type($post_types = array('post')) { if (!empty($post_types)) { if (is_string($post_types)) { $post_types = explode(',', $post_types); } foreach ($post_types as $post_type) { $post_type_object = get_post_type_object($post_type); $taxonomies = get_object_taxonomies($post_type); if ($taxonomies) { foreach ($taxonomies as $taxonomy) { if (!is_taxonomy_hierarchical($taxonomy)) { continue; } $terms = get_terms($taxonomy, array('hide_empty' => false)); if ($terms) { foreach ($terms as $term) { $returns[$term->term_id] = $term->name; } } } } } } return $returns; }
public static function act_save_item($term_id, $tt_id, $taxonomy) { if (!in_array($taxonomy, pp_get_enabled_taxonomies())) { if (!empty($_REQUEST['pp_enable_taxonomy'])) { $enabled_taxonomies = get_option('pp_enabled_taxonomies'); $enabled_taxonomies[$taxonomy] = '1'; update_option('pp_enabled_taxonomies', $enabled_taxonomies); } return; } static $saved_terms; if (!isset($saved_terms)) { $saved_terms = array(); } // so this filter doesn't get called by hook AND internally if (isset($saved_terms[$taxonomy][$tt_id])) { return; } // parent settings can affect the auto-assignment of propagating exceptions $set_parent = is_taxonomy_hierarchical($taxonomy) && !empty($_REQUEST['parent']) ? (int) $_REQUEST['parent'] : 0; if ($set_parent < 0) { $set_parent = 0; } $saved_terms[$taxonomy][$tt_id] = 1; // Determine whether this object is new (first time this PP filter has run for it, though the object may already be inserted into db) $last_parent = 0; if (!($last_parents = get_option("pp_last_{$taxonomy}_parents"))) { $last_parents = array(); } if (!isset($last_parents[$tt_id])) { $is_new = true; $last_parents = array(); } else { $is_new = false; } if (isset($last_parents[$tt_id])) { $last_parent = $last_parents[$tt_id]; } if ($set_parent != $last_parent && ($set_parent || $last_parent)) { $last_parents[$tt_id] = $set_parent; update_option("pp_last_{$taxonomy}_parents", $last_parents); } $exceptions_customized = false; if (!$is_new) { if ($custom_exc_objects = get_option("pp_custom_{$taxonomy}")) { $exceptions_customized = isset($custom_exc_objects[$tt_id]); } } global $typenow; require_once dirname(__FILE__) . '/item-save_pp.php'; $args = compact('is_new', 'set_parent', 'last_parent', 'disallow_manual_entry'); $args['via_item_type'] = $taxonomy; PP_ItemSave::item_update_process_exceptions('term', 'post', $tt_id, $args); do_action('pp_update_item_exceptions', 'term', $tt_id, $args); }
public function __construct($element_type, $root = false) { global $sitepress; $this->taxonomy = $element_type; $this->root_trid = $root; $this->tree = false; /* If accidentally passed a non-hierarchical taxonomy, we cannot create a tree for it. */ if (is_taxonomy_hierarchical($element_type) && $sitepress->get_option('sync_taxonomy_parents')) { $this->tree = $this->get_all_elements($element_type); } }
/** * Get supported taxonomies * * @since 0.4.0 */ function of_cme_supported_taxonomies() { $taxes = get_taxonomies(); $results = array(); foreach ($taxes as $tax) { if (is_taxonomy_hierarchical($tax)) { $results[] = $tax; } } return $results; }
function get_terms_html($id, $taxonomy, $args = []) { if (empty($id) || !is_int($id) || empty($taxonomy) || !is_taxonomy_hierarchical($taxonomy) || empty($args)) { return; } $html = []; if ($terms = get_terms($args)) { foreach ($terms as $term) { $html[] = sprintf('<span class="p-category"><a href="%s" rel="tag">%s</a></span>', get_term_link($term), $term->name); } } return $html; }
public function build() { $terms = wp_get_object_terms($this->args['post_id'], $this->args['taxonomy']); if (is_array($terms) && $terms !== array()) { $deepest_term = $this->find_deepest_term($terms); if (is_taxonomy_hierarchical($this->args['taxonomy']) && $deepest_term->parent != 0) { $parent_terms = $this->get_term_parents($deepest_term); foreach ($parent_terms as $parent_term) { $this->add_term_crumb($parent_term); } } $this->add_term_crumb($deepest_term); } }
public function add_meta_boxes($post_type) { if ($this->model->is_translated_post_type($post_type)) { add_meta_box('ml_box', __('Languages', 'polylang'), array(&$this, 'post_language'), $post_type, 'side', 'high'); } // replace tag metabox by our own foreach (get_object_taxonomies($post_type) as $tax_name) { $taxonomy = get_taxonomy($tax_name); if ($taxonomy->show_ui && !is_taxonomy_hierarchical($tax_name)) { remove_meta_box('tagsdiv-' . $tax_name, null, 'side'); add_meta_box('pll-tagsdiv-' . $tax_name, $taxonomy->labels->name, 'post_tags_meta_box', null, 'side', 'core', array('taxonomy' => $tax_name)); } } }
public function isParentOf($query) { $this->setupBaseInfo(); if (!is_taxonomy_hierarchical($this->type)) { return false; } $item_terms = wp_get_post_terms($query->post->ID, $this->type, array('fields' => 'ids')); foreach ($item_terms as $item_term) { $depth = 0; if ($this->findChild($this->id, $item_term, $this->children, $depth)) { return true; } } }
public function duplicates_require_sync($post_ids, $duplicates_only = true) { $taxonomies = $this->sitepress->get_translatable_taxonomies(true); foreach ($taxonomies as $key => $tax) { if (!is_taxonomy_hierarchical($tax)) { unset($taxonomies[$key]); } } if ((bool) $post_ids === true) { $need_sync_taxonomies = $duplicates_only === true ? $this->get_need_sync_new_dupl($post_ids, $taxonomies) : $this->get_need_sync_all_terms($taxonomies, $post_ids); } else { $need_sync_taxonomies = array(); } return array_values(array_unique($need_sync_taxonomies)); }
/** * Get parent terms * @return array Holds term parent 'names'. */ private function getParentTerms($oTerm) { if (!is_taxonomy_hierarchical($oTerm->taxonomy)) { return array(); } if (!$oTerm->parent) { return array(); } $_oTaxonomy = $oTerm->taxonomy; $_aParents = array(); while (0 != $oTerm->parent) { $oTerm = get_term($oTerm->parent, $_oTaxonomy); $_aParents[] = $oTerm; } return array_reverse($_aParents); }
public function __construct($termId, $taxonomy = 'category', $childArgs = array()) { $termId = intval($termId); if (is_integer($termId)) { $this->termId = $termId; } else { return new WP_Error('no post id', __("Post ID must be an integer", "dion")); } $this->taxonomy = $taxonomy; //additional wpquery args $this->childArgs = $childArgs; $this->hierachical = is_taxonomy_hierarchical($this->taxonomy); if ($this->hierachical) { $this->buildTheTree(); } }
function wrap($content, $taxonomy, $before, $sep, $after) { global $post; if (!in_the_loop()) { return $content; } $post_id = $post->ID; if (!$post_id) { return $content; } $data = compact('post_id', 'taxonomy', 'before', 'sep', 'after'); if (!$this->check($data)) { return $content; } $content = $this->placehold($content); $data['type'] = is_taxonomy_hierarchical($taxonomy) ? FEE_Core::$options->taxonomy_ui : 'terminput'; return FEE_Field_Base::wrap($content, $data); }
/** * Add Flat Taxonomy IDs */ private function addFlatTaxonomies() { $taxonomies = $this->data['tax_input']; foreach ($taxonomies as $key => $tax_terms) { $tax = get_taxonomy($key); if (!is_taxonomy_hierarchical($tax->name) && !empty($tax_terms)) { unset($this->data['tax_input'][$key]); // remove taxonomy from returned tax input $terms = explode(',', $tax_terms); foreach ($terms as $i => $term) { if ($term !== "") { $term_obj = get_term_by('name', $term, $tax->name); $this->data['flat_tax'][$key][$i] = $term_obj->term_id; // add the new flat_tax returned object } } } } }
function ajax_query() { // options $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die; } // vars $r = array(); $args = array('hide_empty' => false); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // search if ($options['s']) { $args['search'] = $options['s']; } // filters $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']); // get terms $terms = get_terms($field['taxonomy'], $args); // sort into hierachial order! if (is_taxonomy_hierarchical($field['taxonomy'])) { // this will fail if a search has taken place because parents wont exist if (empty($args['search'])) { $terms = _get_term_children(0, $terms, $field['taxonomy']); } } /// append to r foreach ($terms as $term) { // add to json $r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id'])); } // return JSON echo json_encode($r); die; }
function get_terms_children($taxonomy, $option_value = '') { if (!is_taxonomy_hierarchical($taxonomy)) { return array(); } $children = get_option("{$taxonomy}_children_rs"); if (is_array($children)) { // caused non-refresh from empty array for custom taxonomies in some situations //if ( $children ) return $children; } $children = array(); $terms = $GLOBALS['scoper']->get_terms($taxonomy, UNFILTERED_RS); foreach ($terms as $term) { if ($term->parent) { $children[$term->parent][] = $term->term_id; } } update_option("{$taxonomy}_children_rs", $children); return $children; }
public static function get_term_path($term_id, $taxonomy) { $title_caption = ''; if ($term = get_term($term_id, $taxonomy)) { if (isset($term->name)) { if (is_taxonomy_hierarchical($taxonomy) && ($ancestors = self::get_term_ancestors($taxonomy, $term_id))) { $arr = array(); foreach ($ancestors as $id) { if ($_ancestor = get_term($id, $taxonomy)) { $arr[] = $_ancestor->name; } } $arr = array_reverse($arr); $arr[] = $term->name; $title_caption = implode(' / ', $arr); } else { $title_caption = $term->name; } } } return $title_caption; }
function ubik_terms_ancestors($term = '', $taxonomy = '') { // Check query variables for term and taxonomy data and exit early if nothing works or the taxonomy isn't hierarchical if (empty($term) || $term == '') { $term = get_query_var('term'); } if (empty($taxonomy) || $taxonomy == '') { $taxonomy = get_query_var('taxonomy'); } if (!term_exists($term) || !taxonomy_exists($taxonomy) || !is_taxonomy_hierarchical($taxonomy)) { return; } // Get initial term data if (is_int($term)) { $term = get_term_by('id', $term, $taxonomy); } else { $term = get_term_by('slug', $term, $taxonomy); } // Something went wrong or this term has no parents if (empty($term) || empty($term->parent)) { return; } // Initialize array with the initial term ID $parent = $term->parent; // Loop through all ancestors; this will end when it reaches a term with no parent while ($parent) { $ancestors[] = $parent; $parent = get_term_by('id', $parent, $taxonomy); // This gets all the term data $parent = $parent->parent; // We only want the parent of the parent } // Reverse the array so it's parent-child $ancestors = array_reverse($ancestors); // Add the original term to the end of the inheritance chain $ancestors[] = $term->term_id; // Return the array with an opportunity to filter the results return apply_filters('ubik_terms_ancestors', $ancestors); }
/** * Set the terms for a post. * * @since 2.8.0 * @uses wp_set_object_terms() Sets the tags for the post. * * @param int $post_id Post ID. * @param string $tags The tags to set for the post, separated by commas. * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags. * @return mixed Array of affected term IDs. WP_Error or false on failure. */ function wp_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(); } $tags = is_array($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_map('intval', $tags); $tags = array_unique($tags); } return wp_set_object_terms($post_id, $tags, $taxonomy, $append); }
/** * Helper method for wp_newPost and wp_editPost, containing shared logic. * * @since 3.4.0 * @uses wp_insert_post() * * @param WP_User $user The post author if post_author isn't set in $content_struct. * @param array|IXR_Error $content_struct Post data to insert. */ protected function _insert_post($user, $content_struct) { $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0, 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => ''); $post_data = wp_parse_args($content_struct, $defaults); $post_type = get_post_type_object($post_data['post_type']); if (!$post_type) { return new IXR_Error(403, __('Invalid post type')); } $update = !empty($post_data['ID']); if ($update) { if (!get_post($post_data['ID'])) { return new IXR_Error(401, __('Invalid post ID.')); } if (!current_user_can('edit_post', $post_data['ID'])) { return new IXR_Error(401, __('Sorry, you are not allowed to edit this post.')); } if ($post_data['post_type'] != get_post_type($post_data['ID'])) { return new IXR_Error(401, __('The post type may not be changed.')); } } else { if (!current_user_can($post_type->cap->create_posts) || !current_user_can($post_type->cap->edit_posts)) { return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.')); } } switch ($post_data['post_status']) { case 'draft': case 'pending': break; case 'private': if (!current_user_can($post_type->cap->publish_posts)) { return new IXR_Error(401, __('Sorry, you are not allowed to create private posts in this post type')); } break; case 'publish': case 'future': if (!current_user_can($post_type->cap->publish_posts)) { return new IXR_Error(401, __('Sorry, you are not allowed to publish posts in this post type')); } break; default: if (!get_post_status_object($post_data['post_status'])) { $post_data['post_status'] = 'draft'; } break; } if (!empty($post_data['post_password']) && !current_user_can($post_type->cap->publish_posts)) { return new IXR_Error(401, __('Sorry, you are not allowed to create password protected posts in this post type')); } $post_data['post_author'] = absint($post_data['post_author']); if (!empty($post_data['post_author']) && $post_data['post_author'] != $user->ID) { if (!current_user_can($post_type->cap->edit_others_posts)) { return new IXR_Error(401, __('You are not allowed to create posts as this user.')); } $author = get_userdata($post_data['post_author']); if (!$author) { return new IXR_Error(404, __('Invalid author ID.')); } } else { $post_data['post_author'] = $user->ID; } if (isset($post_data['comment_status']) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed') { unset($post_data['comment_status']); } if (isset($post_data['ping_status']) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed') { unset($post_data['ping_status']); } // Do some timestamp voodoo if (!empty($post_data['post_date_gmt'])) { // We know this is supposed to be GMT, so we're going to slap that Z on there by force $dateCreated = rtrim($post_data['post_date_gmt']->getIso(), 'Z') . 'Z'; } elseif (!empty($post_data['post_date'])) { $dateCreated = $post_data['post_date']->getIso(); } if (!empty($dateCreated)) { $post_data['post_date'] = get_date_from_gmt(iso8601_to_datetime($dateCreated)); $post_data['post_date_gmt'] = iso8601_to_datetime($dateCreated, 'GMT'); } if (!isset($post_data['ID'])) { $post_data['ID'] = get_default_post_to_edit($post_data['post_type'], true)->ID; } $post_ID = $post_data['ID']; if ($post_data['post_type'] == 'post') { // Private and password-protected posts cannot be stickied. if ($post_data['post_status'] == 'private' || !empty($post_data['post_password'])) { // Error if the client tried to stick the post, otherwise, silently unstick. if (!empty($post_data['sticky'])) { return new IXR_Error(401, __('Sorry, you cannot stick a private post.')); } if ($update) { unstick_post($post_ID); } } elseif (isset($post_data['sticky'])) { if (!current_user_can($post_type->cap->edit_others_posts)) { return new IXR_Error(401, __('Sorry, you are not allowed to stick this post.')); } if ($post_data['sticky']) { stick_post($post_ID); } else { unstick_post($post_ID); } } } if (isset($post_data['post_thumbnail'])) { // empty value deletes, non-empty value adds/updates if (!$post_data['post_thumbnail']) { delete_post_thumbnail($post_ID); } elseif (!get_post(absint($post_data['post_thumbnail']))) { return new IXR_Error(404, __('Invalid attachment ID.')); } set_post_thumbnail($post_ID, $post_data['post_thumbnail']); unset($content_struct['post_thumbnail']); } if (isset($post_data['custom_fields'])) { $this->set_custom_fields($post_ID, $post_data['custom_fields']); } if (isset($post_data['terms']) || isset($post_data['terms_names'])) { $post_type_taxonomies = get_object_taxonomies($post_data['post_type'], 'objects'); // accumulate term IDs from terms and terms_names $terms = array(); // first validate the terms specified by ID if (isset($post_data['terms']) && is_array($post_data['terms'])) { $taxonomies = array_keys($post_data['terms']); // validating term ids foreach ($taxonomies as $taxonomy) { if (!array_key_exists($taxonomy, $post_type_taxonomies)) { return new IXR_Error(401, __('Sorry, one of the given taxonomies is not supported by the post type.')); } if (!current_user_can($post_type_taxonomies[$taxonomy]->cap->assign_terms)) { return new IXR_Error(401, __('Sorry, you are not allowed to assign a term to one of the given taxonomies.')); } $term_ids = $post_data['terms'][$taxonomy]; $terms[$taxonomy] = array(); foreach ($term_ids as $term_id) { $term = get_term_by('id', $term_id, $taxonomy); if (!$term) { return new IXR_Error(403, __('Invalid term ID')); } $terms[$taxonomy][] = (int) $term_id; } } } // now validate terms specified by name if (isset($post_data['terms_names']) && is_array($post_data['terms_names'])) { $taxonomies = array_keys($post_data['terms_names']); foreach ($taxonomies as $taxonomy) { if (!array_key_exists($taxonomy, $post_type_taxonomies)) { return new IXR_Error(401, __('Sorry, one of the given taxonomies is not supported by the post type.')); } if (!current_user_can($post_type_taxonomies[$taxonomy]->cap->assign_terms)) { return new IXR_Error(401, __('Sorry, you are not allowed to assign a term to one of the given taxonomies.')); } // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name $ambiguous_terms = array(); if (is_taxonomy_hierarchical($taxonomy)) { $tax_term_names = get_terms($taxonomy, array('fields' => 'names', 'hide_empty' => false)); // count the number of terms with the same name $tax_term_names_count = array_count_values($tax_term_names); // filter out non-ambiguous term names $ambiguous_tax_term_counts = array_filter($tax_term_names_count, array($this, '_is_greater_than_one')); $ambiguous_terms = array_keys($ambiguous_tax_term_counts); } $term_names = $post_data['terms_names'][$taxonomy]; foreach ($term_names as $term_name) { if (in_array($term_name, $ambiguous_terms)) { return new IXR_Error(401, __('Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.')); } $term = get_term_by('name', $term_name, $taxonomy); if (!$term) { // term doesn't exist, so check that the user is allowed to create new terms if (!current_user_can($post_type_taxonomies[$taxonomy]->cap->edit_terms)) { return new IXR_Error(401, __('Sorry, you are not allowed to add a term to one of the given taxonomies.')); } // create the new term $term_info = wp_insert_term($term_name, $taxonomy); if (is_wp_error($term_info)) { return new IXR_Error(500, $term_info->get_error_message()); } $terms[$taxonomy][] = (int) $term_info['term_id']; } else { $terms[$taxonomy][] = (int) $term->term_id; } } } } $post_data['tax_input'] = $terms; unset($post_data['terms'], $post_data['terms_names']); } else { // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names' unset($post_data['tax_input'], $post_data['post_category'], $post_data['tags_input']); } if (isset($post_data['post_format'])) { $format = set_post_format($post_ID, $post_data['post_format']); if (is_wp_error($format)) { return new IXR_Error(500, $format->get_error_message()); } unset($post_data['post_format']); } // Handle enclosures $enclosure = isset($post_data['enclosure']) ? $post_data['enclosure'] : null; $this->add_enclosure_if_new($post_ID, $enclosure); $this->attach_uploads($post_ID, $post_data['post_content']); /** * Filter post data array to be inserted via XML-RPC. * * @since 3.4.0 * * @param array $post_data Parsed array of post data. * @param array $content_struct Post data array. */ $post_data = apply_filters('xmlrpc_wp_insert_post_data', $post_data, $content_struct); $post_ID = $update ? wp_update_post($post_data, true) : wp_insert_post($post_data, true); if (is_wp_error($post_ID)) { return new IXR_Error(500, $post_ID->get_error_message()); } if (!$post_ID) { return new IXR_Error(401, __('Sorry, your entry could not be posted. Something wrong happened.')); } return strval($post_ID); }
/** * Ajax handler to add a tag. * * @since 3.1.0 * * @global WP_List_Table $wp_list_table */ function wp_ajax_add_tag() { global $wp_list_table; check_ajax_referer('add-tag', '_wpnonce_add-tag'); $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; $tax = get_taxonomy($taxonomy); if (!current_user_can($tax->cap->edit_terms)) { wp_die(-1); } $x = new WP_Ajax_Response(); $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST); if (!$tag || is_wp_error($tag) || !($tag = get_term($tag['term_id'], $taxonomy))) { $message = __('An error has occurred. Please reload the page and try again.'); if (is_wp_error($tag) && $tag->get_error_message()) { $message = $tag->get_error_message(); } $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message))); $x->send(); } $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen'])); $level = 0; if (is_taxonomy_hierarchical($taxonomy)) { $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy')); ob_start(); $wp_list_table->single_row($tag, $level); $noparents = ob_get_clean(); } ob_start(); $wp_list_table->single_row($tag); $parents = ob_get_clean(); $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents'))); $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag)); $x->send(); }