X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/af50974463450c98503e763a7836a50e260461a9..899389d1e4043331309c0433543419258b230b60:/wp-includes/taxonomy.php diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 1d93f690..7e386695 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -127,7 +127,6 @@ function create_initial_taxonomies() { 'show_in_nav_menus' => current_theme_supports( 'post-formats' ), ) ); } -add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority /** * Get a list of registered taxonomy objects. @@ -283,6 +282,8 @@ function is_taxonomy_hierarchical($taxonomy) { * * If not set, the default is inherited from public. * - show_tagcloud - Whether to list the taxonomy in the Tag Cloud Widget. * * If not set, the default is inherited from show_ui. + * - show_in_quick_edit - Whether to show the taxonomy in the quick/bulk edit panel. + * * It not set, the default is inherited from show_ui. * - show_admin_column - Whether to display a column for the taxonomy on its post type listing screens. * * Defaults to false. * - meta_box_cb - Provide a callback function for the meta box display. @@ -309,8 +310,10 @@ function is_taxonomy_hierarchical($taxonomy) { * - _builtin - true if this taxonomy is a native or "built-in" taxonomy. THIS IS FOR INTERNAL USE ONLY! * * @since 2.3.0 - * @uses $wp_taxonomies Inserts new taxonomy object into the list - * @uses $wp Adds query vars + * @since 4.2.0 Introduced `show_in_quick_edit` argument. + * + * @global array $wp_taxonomies Registered taxonomies. + * @global WP $wp WP instance. * * @param string $taxonomy Taxonomy key, must not exceed 32 characters. * @param array|string $object_type Name of the object type for the taxonomy object. @@ -332,6 +335,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 'show_in_menu' => null, 'show_in_nav_menus' => null, 'show_tagcloud' => null, + 'show_in_quick_edit' => null, 'show_admin_column' => false, 'meta_box_cb' => null, 'capabilities' => array(), @@ -342,9 +346,9 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { ); $args = wp_parse_args( $args, $defaults ); - if ( strlen( $taxonomy ) > 32 ) { - _doing_it_wrong( __FUNCTION__, __( 'Taxonomies cannot exceed 32 characters in length' ), '4.0' ); - return new WP_Error( 'taxonomy_too_long', __( 'Taxonomies cannot exceed 32 characters in length' ) ); + if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) { + _doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2' ); + return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) ); } if ( false !== $args['query_var'] && ! empty( $wp ) ) { @@ -390,6 +394,11 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { if ( null === $args['show_tagcloud'] ) $args['show_tagcloud'] = $args['show_ui']; + // If not set, default to the setting for show_ui. + if ( null === $args['show_in_quick_edit'] ) { + $args['show_in_quick_edit'] = $args['show_ui']; + } + $default_caps = array( 'manage_terms' => 'manage_categories', 'edit_terms' => 'manage_categories', @@ -450,7 +459,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas", used in the meta box. * - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags", used in the meta box when JavaScript is disabled. * - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags", used in the meta box. - * - not_found - This string isn't used on hierarchical taxonomies. Default is "No tags found", used in the meta box. + * - not_found - Default is "No tags found"/"No categories found", used in the meta box and taxonomy list table. * * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories). * @@ -484,7 +493,7 @@ function get_taxonomy_labels( $tax ) { 'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ), 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ), 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ), - 'not_found' => array( __( 'No tags found.' ), null ), + 'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ), ); $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; @@ -765,7 +774,7 @@ class WP_Tax_Query { $cleaned_query['relation'] = $this->sanitize_relation( $query ); // First-order clause. - } else if ( self::is_first_order_clause( $query ) ) { + } elseif ( self::is_first_order_clause( $query ) ) { $cleaned_clause = array_merge( $defaults, $query ); $cleaned_clause['terms'] = (array) $cleaned_clause['terms']; @@ -795,7 +804,7 @@ class WP_Tax_Query { } // Otherwise, it's a nested query, so we recurse. - } else if ( is_array( $query ) ) { + } elseif ( is_array( $query ) ) { $cleaned_subquery = $this->sanitize_query( $query ); if ( ! empty( $cleaned_subquery ) ) { @@ -939,7 +948,7 @@ class WP_Tax_Query { foreach ( $query as $key => &$clause ) { if ( 'relation' === $key ) { $relation = $query['relation']; - } else if ( is_array( $clause ) ) { + } elseif ( is_array( $clause ) ) { // This is a first-order clause. if ( $this->is_first_order_clause( $clause ) ) { @@ -948,7 +957,7 @@ class WP_Tax_Query { $where_count = count( $clause_sql['where'] ); if ( ! $where_count ) { $sql_chunks['where'][] = ''; - } else if ( 1 === $where_count ) { + } elseif ( 1 === $where_count ) { $sql_chunks['where'][] = $clause_sql['where'][0]; } else { $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )'; @@ -1218,7 +1227,17 @@ class WP_Tax_Query { switch ( $query['field'] ) { case 'slug': case 'name': - $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'"; + foreach ( $query['terms'] as &$term ) { + /* + * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't + * matter because `sanitize_term_field()` ignores the $term_id param when the + * context is 'db'. + */ + $term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'"; + } + + $terms = implode( ",", $query['terms'] ); + $terms = $wpdb->get_col( " SELECT $wpdb->term_taxonomy.$resulting_field FROM $wpdb->term_taxonomy @@ -1393,11 +1412,11 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw $value = sanitize_title($value); if ( empty($value) ) return false; - } else if ( 'name' == $field ) { + } elseif ( 'name' == $field ) { // Assume already escaped $value = wp_unslash($value); $field = 't.name'; - } else if ( 'term_taxonomy_id' == $field ) { + } elseif ( 'term_taxonomy_id' == $field ) { $value = (int) $value; $field = 'tt.term_taxonomy_id'; } else { @@ -1544,10 +1563,8 @@ function get_term_to_edit( $id, $taxonomy ) { * The 'get_terms_orderby' filter passes the ORDER BY clause for the query * along with the $args array. * - * The 'get_terms_fields' filter passes the fields for the SELECT query - * along with the $args array. - * * @since 2.3.0 + * @since 4.2.0 Introduced 'name' and 'childless' parameters. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -1555,11 +1572,11 @@ function get_term_to_edit( $id, $taxonomy ) { * @param array|string $args { * Optional. Array or string of arguments to get terms. * - * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug', - * 'term_group', 'term_id', 'id'), 'count' for term taxonomy count, - * 'include' to match the 'order' of the $include param, or 'none' - * to skip ORDER BY. Defaults to 'name'. - * @type string $order Whether to order terms in ascending or descending order. + * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug', + * 'term_group', 'term_id', 'id', 'description'), 'count' for term + * taxonomy count, 'include' to match the 'order' of the $include param, + * or 'none' to skip ORDER BY. Defaults to 'name'. + * @type string $order Whether to order terms in ascending or descending order. * Accepts 'ASC' (ascending) or 'DESC' (descending). * Default 'ASC'. * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts @@ -1572,12 +1589,13 @@ function get_term_to_edit( $id, $taxonomy ) { * @type array|string $exclude_tree Array or comma/space-separated string of term ids to exclude * along with all of their descendant terms. If $include is * non-empty, $exclude_tree is ignored. Default empty array. - * @type int $number Maximum number of terms to return. Accepts 1+ or -1 (all). - * Default -1. + * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any + * positive number. Default ''|0 (all). * @type int $offset The number by which to offset the terms query. Default empty. * @type string $fields Term fields to query for. Accepts 'all' (returns an array of * term objects), 'ids' or 'names' (returns an array of integers * or strings, respectively. Default 'all'. + * @type string|array $name Optional. Name or array of names to return term(s) for. Default empty. * @type string|array $slug Optional. Slug or array of slugs to return term(s) for. Default empty. * @type bool $hierarchical Whether to include terms that have non-empty descendants (even * if $hide_empty is set to true). Default true. @@ -1594,6 +1612,8 @@ function get_term_to_edit( $id, $taxonomy ) { * @type int $child_of Term ID to retrieve child terms of. If multiple taxonomies * are passed, $child_of is ignored. Default 0. * @type int|string $parent Parent term ID to retrieve direct-child terms of. Default empty. + * @type bool $childless True to limit results to terms that have no children. This parameter has + * no effect on non-hierarchical taxonomies. Default false. * @type string $cache_domain Unique cache key to be produced when this query is stored in an * object cache. Default is 'core'. * } @@ -1618,7 +1638,7 @@ function get_terms( $taxonomies, $args = '' ) { $defaults = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), - 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', + 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false, 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); $args = wp_parse_args( $args, $defaults ); @@ -1626,7 +1646,14 @@ function get_terms( $taxonomies, $args = '' ) { $args['offset'] = absint( $args['offset'] ); // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. - if ( ! $single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ) { + $has_hierarchical_tax = false; + foreach ( $taxonomies as $_tax ) { + if ( is_taxonomy_hierarchical( $_tax ) ) { + $has_hierarchical_tax = true; + } + } + + if ( ! $has_hierarchical_tax ) { $args['hierarchical'] = false; $args['pad_counts'] = false; } @@ -1637,6 +1664,7 @@ function get_terms( $taxonomies, $args = '' ) { } if ( 'all' == $args['get'] ) { + $args['childless'] = false; $args['child_of'] = 0; $args['hide_empty'] = 0; $args['hierarchical'] = false; @@ -1648,23 +1676,34 @@ function get_terms( $taxonomies, $args = '' ) { * * @since 3.1.0 * - * @param array $args An array of arguments. - * @param string|array $taxonomies A taxonomy or array of taxonomies. + * @param array $args An array of arguments. + * @param array $taxonomies An array of taxonomies. */ $args = apply_filters( 'get_terms_args', $args, $taxonomies ); + // Avoid the query if the queried parent/child_of term has no descendants. $child_of = $args['child_of']; + $parent = $args['parent']; + if ( $child_of ) { - $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); - if ( ! isset( $hierarchy[ $child_of ] ) ) { - return $empty_array; - } + $_parent = $child_of; + } elseif ( $parent ) { + $_parent = $parent; + } else { + $_parent = false; } - $parent = $args['parent']; - if ( $parent ) { - $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); - if ( ! isset( $hierarchy[ $parent ] ) ) { + if ( $_parent ) { + $in_hierarchy = false; + foreach ( $taxonomies as $_tax ) { + $hierarchy = _get_term_hierarchy( $_tax ); + + if ( isset( $hierarchy[ $_parent ] ) ) { + $in_hierarchy = true; + } + } + + if ( ! $in_hierarchy ) { return $empty_array; } } @@ -1686,9 +1725,9 @@ function get_terms( $taxonomies, $args = '' ) { * * @since 2.3.0 * - * @param array $cache Cached array of terms for the given taxonomy. - * @param string|array $taxonomies A taxonomy or array of taxonomies. - * @param array $args An array of arguments to get terms. + * @param array $cache Cached array of terms for the given taxonomy. + * @param array $taxonomies An array of taxonomies. + * @param array $args An array of arguments to get terms. */ $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args ); return $cache; @@ -1697,16 +1736,18 @@ function get_terms( $taxonomies, $args = '' ) { $_orderby = strtolower( $args['orderby'] ); if ( 'count' == $_orderby ) { $orderby = 'tt.count'; - } else if ( 'name' == $_orderby ) { + } elseif ( 'name' == $_orderby ) { $orderby = 't.name'; - } else if ( 'slug' == $_orderby ) { + } elseif ( 'slug' == $_orderby ) { $orderby = 't.slug'; - } else if ( 'include' == $_orderby && ! empty( $args['include'] ) ) { + } elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) { $include = implode( ',', array_map( 'absint', $args['include'] ) ); $orderby = "FIELD( t.term_id, $include )"; - } else if ( 'term_group' == $_orderby ) { + } elseif ( 'term_group' == $_orderby ) { $orderby = 't.term_group'; - } else if ( 'none' == $_orderby ) { + } elseif ( 'description' == $_orderby ) { + $orderby = 'tt.description'; + } elseif ( 'none' == $_orderby ) { $orderby = ''; } elseif ( empty($_orderby) || 'id' == $_orderby ) { $orderby = 't.term_id'; @@ -1718,9 +1759,9 @@ function get_terms( $taxonomies, $args = '' ) { * * @since 2.8.0 * - * @param string $orderby ORDERBY clause of the terms query. - * @param array $args An array of terms query arguments. - * @param string|array $taxonomies A taxonomy or array of taxonomies. + * @param string $orderby ORDERBY clause of the terms query. + * @param array $args An array of terms query arguments. + * @param array $taxonomies An array of taxonomies. */ $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies ); @@ -1753,6 +1794,7 @@ function get_terms( $taxonomies, $args = '' ) { $where .= $inclusions; } + $exclusions = array(); if ( ! empty( $exclude_tree ) ) { $exclude_tree = wp_parse_id_list( $exclude_tree ); $excluded_children = $exclude_tree; @@ -1762,22 +1804,26 @@ function get_terms( $taxonomies, $args = '' ) { (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) ) ); } - $exclusions = implode( ',', array_map( 'intval', $excluded_children ) ); - } else { - $exclusions = ''; + $exclusions = array_merge( $excluded_children, $exclusions ); } if ( ! empty( $exclude ) ) { - $exterms = wp_parse_id_list( $exclude ); - if ( empty( $exclusions ) ) { - $exclusions = implode( ',', $exterms ); - } else { - $exclusions .= ', ' . implode( ',', $exterms ); + $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions ); + } + + // 'childless' terms are those without an entry in the flattened term hierarchy. + $childless = (bool) $args['childless']; + if ( $childless ) { + foreach ( $taxonomies as $_tax ) { + $term_hierarchy = _get_term_hierarchy( $_tax ); + $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions ); } } if ( ! empty( $exclusions ) ) { - $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')'; + $exclusions = ' AND t.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')'; + } else { + $exclusions = ''; } /** @@ -1785,9 +1831,9 @@ function get_terms( $taxonomies, $args = '' ) { * * @since 2.3.0 * - * @param string $exclusions NOT IN clause of the terms query. - * @param array $args An array of terms query arguments. - * @param string|array $taxonomies A taxonomy or array of taxonomies. + * @param string $exclusions NOT IN clause of the terms query. + * @param array $args An array of terms query arguments. + * @param array $taxonomies An array of taxonomies. */ $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies ); @@ -1795,6 +1841,16 @@ function get_terms( $taxonomies, $args = '' ) { $where .= $exclusions; } + if ( ! empty( $args['name'] ) ) { + if ( is_array( $args['name'] ) ) { + $name = array_map( 'sanitize_text_field', $args['name'] ); + $where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $name ) ) . "')"; + } else { + $name = sanitize_text_field( $args['name'] ); + $where .= $wpdb->prepare( " AND t.name = %s", $name ); + } + } + if ( ! empty( $args['slug'] ) ) { if ( is_array( $args['slug'] ) ) { $slug = array_map( 'sanitize_title', $args['slug'] ); @@ -1852,10 +1908,10 @@ function get_terms( $taxonomies, $args = '' ) { break; case 'ids': case 'id=>parent': - $selects = array( 't.term_id', 'tt.parent', 'tt.count' ); + $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' ); break; case 'names': - $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' ); + $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' ); break; case 'count': $orderby = ''; @@ -1863,10 +1919,10 @@ function get_terms( $taxonomies, $args = '' ) { $selects = array( 'COUNT(*)' ); break; case 'id=>name': - $selects = array( 't.term_id', 't.name', 'tt.count' ); + $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' ); break; case 'id=>slug': - $selects = array( 't.term_id', 't.slug', 'tt.count' ); + $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' ); break; } @@ -1875,11 +1931,18 @@ function get_terms( $taxonomies, $args = '' ) { /** * Filter the fields to select in the terms query. * + * Field lists modified using this filter will only modify the term fields returned + * by the function when the `$fields` parameter set to 'count' or 'all'. In all other + * cases, the term fields in the results array will be determined by the `$fields` + * parameter alone. + * + * Use of this filter can result in unpredictable behavior, and is not recommended. + * * @since 2.8.0 * - * @param array $selects An array of fields to select for the terms query. - * @param array $args An array of term query arguments. - * @param string|array $taxonomies A taxonomy or array of taxonomies. + * @param array $selects An array of fields to select for the terms query. + * @param array $args An array of term query arguments. + * @param array $taxonomies An array of taxonomies. */ $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) ); @@ -1892,9 +1955,9 @@ function get_terms( $taxonomies, $args = '' ) { * * @since 3.1.0 * - * @param array $pieces Terms query SQL clauses. - * @param string|array $taxonomies A taxonomy or array of taxonomies. - * @param array $args An array of terms query arguments. + * @param array $pieces Terms query SQL clauses. + * @param array $taxonomies An array of taxonomies. + * @param array $args An array of terms query arguments. */ $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; @@ -1925,24 +1988,29 @@ function get_terms( $taxonomies, $args = '' ) { } if ( $child_of ) { - $children = _get_term_hierarchy( reset( $taxonomies ) ); - if ( ! empty( $children ) ) { - $terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) ); + foreach ( $taxonomies as $_tax ) { + $children = _get_term_hierarchy( $_tax ); + if ( ! empty( $children ) ) { + $terms = _get_term_children( $child_of, $terms, $_tax ); + } } } // Update term counts to include children. if ( $args['pad_counts'] && 'all' == $_fields ) { - _pad_term_counts( $terms, reset( $taxonomies ) ); + foreach ( $taxonomies as $_tax ) { + _pad_term_counts( $terms, $_tax ); + } } + // Make sure we show empty categories that have children. if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) { foreach ( $terms as $k => $term ) { if ( ! $term->count ) { - $children = get_term_children( $term->term_id, reset( $taxonomies ) ); + $children = get_term_children( $term->term_id, $term->taxonomy ); if ( is_array( $children ) ) { foreach ( $children as $child_id ) { - $child = get_term( $child_id, reset( $taxonomies ) ); + $child = get_term( $child_id, $term->taxonomy ); if ( $child->count ) { continue 2; } @@ -1954,28 +2022,27 @@ function get_terms( $taxonomies, $args = '' ) { } } } - reset( $terms ); $_terms = array(); if ( 'id=>parent' == $_fields ) { - while ( $term = array_shift( $terms ) ) { - $_terms[$term->term_id] = $term->parent; + foreach ( $terms as $term ) { + $_terms[ $term->term_id ] = $term->parent; } } elseif ( 'ids' == $_fields ) { - while ( $term = array_shift( $terms ) ) { + foreach ( $terms as $term ) { $_terms[] = $term->term_id; } } elseif ( 'names' == $_fields ) { - while ( $term = array_shift( $terms ) ) { + foreach ( $terms as $term ) { $_terms[] = $term->name; } } elseif ( 'id=>name' == $_fields ) { - while ( $term = array_shift( $terms ) ) { - $_terms[$term->term_id] = $term->name; + foreach ( $terms as $term ) { + $_terms[ $term->term_id ] = $term->name; } } elseif ( 'id=>slug' == $_fields ) { - while ( $term = array_shift( $terms ) ) { - $_terms[$term->term_id] = $term->slug; + foreach ( $terms as $term ) { + $_terms[ $term->term_id ] = $term->slug; } } @@ -2194,7 +2261,7 @@ function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { $value = esc_html($value); // textarea_escaped else $value = esc_attr($value); - } else if ( 'db' == $context ) { + } elseif ( 'db' == $context ) { /** * Filter a term field value before it is sanitized. @@ -2233,7 +2300,7 @@ function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { $value = apply_filters( 'pre_category_nicename', $value ); } - } else if ( 'rss' == $context ) { + } elseif ( 'rss' == $context ) { /** * Filter the term field for use in RSS. @@ -2290,11 +2357,11 @@ function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { $value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context ); } - if ( 'attribute' == $context ) + if ( 'attribute' == $context ) { $value = esc_attr($value); - else if ( 'js' == $context ) + } elseif ( 'js' == $context ) { $value = esc_js($value); - + } return $value; } @@ -2542,33 +2609,27 @@ function wp_delete_category( $cat_ID ) { /** * Retrieves the terms associated with the given object(s), in the supplied taxonomies. * - * The following information has to do the $args parameter and for what can be - * contained in the string or array of that parameter, if it exists. - * - * The first argument is called, 'orderby' and has the default value of 'name'. - * The other value that is supported is 'count'. - * - * The second argument is called, 'order' and has the default value of 'ASC'. - * The only other value that will be acceptable is 'DESC'. - * - * The final argument supported is called, 'fields' and has the default value of - * 'all'. There are multiple other options that can be used instead. Supported - * values are as follows: 'all', 'ids', 'names', and finally - * 'all_with_object_id'. - * - * The fields argument also decides what will be returned. If 'all' or - * 'all_with_object_id' is chosen or the default kept intact, then all matching - * terms objects will be returned. If either 'ids' or 'names' is used, then an - * array of all matching term ids or term names will be returned respectively. - * * @since 2.3.0 + * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`. + * Introduced `$parent` argument. * * @global wpdb $wpdb WordPress database abstraction object. * - * @param int|array $object_ids The ID(s) of the object(s) to retrieve. + * @param int|array $object_ids The ID(s) of the object(s) to retrieve. * @param string|array $taxonomies The taxonomies to retrieve terms from. - * @param array|string $args Change what is returned - * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist. + * @param array|string $args { + * Array of arguments. + * @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group', + * 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'. + * @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'. + * @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and + * 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of + * term objects being returned, 'ids' will return an array of integers, and 'names' an array + * of strings. + * @type int $parent Optional. Limit results to the direct children of a given term ID. + * } + * @return array|WP_Error The requested term data or empty array if no terms found. + * WP_Error if any of the $taxonomies don't exist. */ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { global $wpdb; @@ -2588,7 +2649,12 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $object_ids = array($object_ids); $object_ids = array_map('intval', $object_ids); - $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); + $defaults = array( + 'orderby' => 'name', + 'order' => 'ASC', + 'fields' => 'all', + 'parent' => '', + ); $args = wp_parse_args( $args, $defaults ); $terms = array(); @@ -2610,17 +2676,13 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $order = $args['order']; $fields = $args['fields']; - if ( 'count' == $orderby ) - $orderby = 'tt.count'; - else if ( 'name' == $orderby ) - $orderby = 't.name'; - else if ( 'slug' == $orderby ) - $orderby = 't.slug'; - else if ( 'term_group' == $orderby ) - $orderby = 't.term_group'; - else if ( 'term_order' == $orderby ) + if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) { + $orderby = "t.$orderby"; + } else if ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) { + $orderby = "tt.$orderby"; + } else if ( 'term_order' === $orderby ) { $orderby = 'tr.term_order'; - else if ( 'none' == $orderby ) { + } else if ( 'none' === $orderby ) { $orderby = ''; $order = ''; } else { @@ -2638,22 +2700,36 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) $order = 'ASC'; + $taxonomy_array = $taxonomies; + $object_id_array = $object_ids; $taxonomies = "'" . implode("', '", $taxonomies) . "'"; $object_ids = implode(', ', $object_ids); $select_this = ''; - if ( 'all' == $fields ) + if ( 'all' == $fields ) { $select_this = 't.*, tt.*'; - else if ( 'ids' == $fields ) + } elseif ( 'ids' == $fields ) { $select_this = 't.term_id'; - else if ( 'names' == $fields ) + } elseif ( 'names' == $fields ) { $select_this = 't.name'; - else if ( 'slugs' == $fields ) + } elseif ( 'slugs' == $fields ) { $select_this = 't.slug'; - else if ( 'all_with_object_id' == $fields ) + } elseif ( 'all_with_object_id' == $fields ) { $select_this = 't.*, tt.*, tr.object_id'; + } + + $where = array( + "tt.taxonomy IN ($taxonomies)", + "tr.object_id IN ($object_ids)", + ); + + if ( '' !== $args['parent'] ) { + $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] ); + } - $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order"; + $where = implode( ' AND ', $where ); + + $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE $where $orderby $order"; $objects = false; if ( 'all' == $fields || 'all_with_object_id' == $fields ) { @@ -2664,14 +2740,14 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $terms = array_merge( $terms, $_terms ); update_term_cache( $terms ); $objects = true; - } else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { + } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { $_terms = $wpdb->get_col( $query ); $_field = ( 'ids' == $fields ) ? 'term_id' : 'name'; foreach ( $_terms as $key => $term ) { $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' ); } $terms = array_merge( $terms, $_terms ); - } else if ( 'tt_ids' == $fields ) { + } elseif ( 'tt_ids' == $fields ) { $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order"); foreach ( $terms as $key => $tt_id ) { $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context. @@ -2695,16 +2771,33 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { } elseif ( ! $objects ) { $terms = array_values( array_unique( $terms ) ); } + /** * Filter the terms for a given object or objects. * + * @since 4.2.0 + * + * @param array $terms An array of terms for the given object or objects. + * @param array $object_id_array Array of object IDs for which `$terms` were retrieved. + * @param array $taxonomy_array Array of taxonomies from which `$terms` were retrieved. + * @param array $args An array of arguments for retrieving terms for the given + * object(s). See wp_get_object_terms() for details. + */ + $terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args ); + + /** + * Filter the terms for a given object or objects. + * + * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The + * {@see 'get_object_terms'} filter is recommended as an alternative. + * * @since 2.8.0 * - * @param array $terms An array of terms for the given object or objects. - * @param array|int $object_ids Object ID or array of IDs. - * @param array|string $taxonomies A taxonomy or array of taxonomies. - * @param array $args An array of arguments for retrieving terms for - * the given object(s). + * @param array $terms An array of terms for the given object or objects. + * @param int|array $object_ids Object ID or array of IDs. + * @param string $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names. + * @param array $args An array of arguments for retrieving terms for the given object(s). + * See {@see wp_get_object_terms()} for details. */ return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); } @@ -2793,13 +2886,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { $slug_provided = ! empty( $args['slug'] ); if ( ! $slug_provided ) { - $_name = trim( $name ); - $existing_term = get_term_by( 'name', $_name, $taxonomy ); - if ( $existing_term ) { - $slug = $existing_term->slug; - } else { - $slug = sanitize_title( $name ); - } + $slug = sanitize_title( $name ); } else { $slug = $args['slug']; } @@ -2810,7 +2897,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { if ( ! empty( $alias->term_group ) ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; - } else if ( ! empty( $alias->term_id ) ) { + } elseif ( ! empty( $alias->term_id ) ) { /* * The alias is not in a group, so we create a new one * and add the alias to it. @@ -2823,20 +2910,28 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { } } - // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy. - if ( $exists = term_exists( $slug, $taxonomy ) ) { - $existing_term = get_term( $exists['term_id'], $taxonomy ); - - if ( $name === $existing_term->name ) { - + /* + * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy, + * unless a unique slug has been explicitly provided. + */ + if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) { + $slug_match = get_term_by( 'slug', $slug, $taxonomy ); + if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) { if ( is_taxonomy_hierarchical( $taxonomy ) ) { - $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) ); - if ( in_array( $name, $siblings ) ) { - return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists with this parent.' ), $exists['term_id'] ); + $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) ); + + $existing_term = null; + if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) { + $existing_term = $name_match; + } elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) { + $existing_term = $slug_match; } + if ( $existing_term ) { + return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id ); + } } else { - return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists in this taxonomy.' ), $exists['term_id'] ); + return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $name_match->term_id ); } } } @@ -2960,7 +3055,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { * @param int $object_id The object to relate to. * @param array|int|string $terms A single term slug, single term id, or array of either term slugs or ids. * 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 string $taxonomy The context in which to relate the term to the object. * @param bool $append Optional. If false will delete difference of terms. Default false. * @return array|WP_Error Affected Term IDs. */ @@ -3270,16 +3365,22 @@ function wp_unique_term_slug($slug, $term) { function wp_update_term( $term_id, $taxonomy, $args = array() ) { global $wpdb; - if ( ! taxonomy_exists($taxonomy) ) - return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); + if ( ! taxonomy_exists( $taxonomy ) ) { + return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); + } $term_id = (int) $term_id; // First, get all of the original args - $term = get_term ($term_id, $taxonomy, ARRAY_A); + $term = get_term( $term_id, $taxonomy, ARRAY_A ); - if ( is_wp_error( $term ) ) + if ( is_wp_error( $term ) ) { return $term; + } + + if ( ! $term ) { + return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); + } // Escape data pulled from DB. $term = wp_slash($term); @@ -3322,7 +3423,7 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) { if ( ! empty( $alias->term_group ) ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; - } else if ( ! empty( $alias->term_id ) ) { + } elseif ( ! empty( $alias->term_id ) ) { /* * The alias is not in a group, so we create a new one * and add the alias to it. @@ -3353,8 +3454,8 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) { $parent = apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args ); // Check for duplicate slug - $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); - if ( $id && ($id != $term_id) ) { + $duplicate = get_term_by( 'slug', $slug, $taxonomy ); + if ( $duplicate && $duplicate->term_id != $term_id ) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ( $empty_slug || ( $parent != $term['parent']) ) @@ -3365,6 +3466,12 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) { $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); + // Check whether this is a shared term that needs splitting. + $_term_id = _split_shared_term( $term_id, $tt_id ); + if ( ! is_wp_error( $_term_id ) ) { + $term_id = $_term_id; + } + /** * Fires immediately before the given terms are edited. * @@ -3682,7 +3789,7 @@ function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { * * @since 2.3.0 * - * @param int|array $id Term object ID + * @param int $id Term object ID * @param string $taxonomy Taxonomy Name * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. */ @@ -3692,23 +3799,21 @@ function get_object_term_cache($id, $taxonomy) { } /** - * Updates the cache for Term ID(s). - * - * Will only update the cache for terms not already cached. + * Updates the cache for the given term object ID(s). * - * The $object_ids expects that the ids be separated by commas, if it is a - * string. + * Note: Due to performance concerns, great care should be taken to only update + * term caches when necessary. Processing time can increase exponentially depending + * on both the number of passed term IDs and the number of taxonomies those terms + * belong to. * - * It should be noted that update_object_term_cache() is very time extensive. It - * is advised that the function is not called very often or at least not for a - * lot of terms that exist in a lot of taxonomies. The amount of time increases - * for each term and it also increases for each taxonomy the term belongs to. + * Caches will only be updated for terms not already cached. * * @since 2.3.0 * - * @param string|array $object_ids Single or list of term object ID(s) - * @param array|string $object_type The taxonomy object type - * @return null|false Null value is given with empty $object_ids. False if + * @param string|array $object_ids Comma-separated list or array of term object IDs.. + * @param array|string $object_type The taxonomy object type. + * @return null|false Null if `$object_ids` is empty, false if all of the terms in + * `$object_ids` are already cached. */ function update_object_term_cache($object_ids, $object_type) { if ( empty($object_ids) ) @@ -3738,7 +3843,7 @@ function update_object_term_cache($object_ids, $object_type) { $object_terms = array(); foreach ( (array) $terms as $term ) - $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term; + $object_terms[$term->object_id][$term->taxonomy][] = $term; foreach ( $ids as $id ) { foreach ( $taxonomies as $taxonomy ) { @@ -3816,11 +3921,15 @@ function _get_term_hierarchy($taxonomy) { * @since 2.3.0 * * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id. - * @param array $terms The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen. - * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. + * @param array $terms The set of terms - either an array of term objects or term IDs - from which those that + * are descendants of $term_id will be chosen. + * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. + * @param array $ancestors Term ancestors that have already been identified. Passed by reference, to keep track of + * found terms when recursing the hierarchy. The array of located ancestors is used to prevent + * infinite recursion loops. For performance, term_ids are used as array keys, with 1 as value. * @return array The subset of $terms that are descendants of $term_id. */ -function _get_term_children($term_id, $terms, $taxonomy) { +function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { $empty_array = array(); if ( empty($terms) ) return $empty_array; @@ -3831,6 +3940,11 @@ function _get_term_children($term_id, $terms, $taxonomy) { if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) return $empty_array; + // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. + if ( empty( $ancestors ) ) { + $ancestors[ $term_id ] = 1; + } + foreach ( (array) $terms as $term ) { $use_id = false; if ( !is_object($term) ) { @@ -3840,7 +3954,8 @@ function _get_term_children($term_id, $terms, $taxonomy) { $use_id = true; } - if ( $term->term_id == $term_id ) { + // Don't recurse if we've already identified the term as a child - this indicates a loop. + if ( isset( $ancestors[ $term->term_id ] ) ) { continue; } @@ -3853,7 +3968,9 @@ function _get_term_children($term_id, $terms, $taxonomy) { if ( !isset($has_children[$term->term_id]) ) continue; - if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) ) + $ancestors[ $term->term_id ] = 1; + + if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) ) $term_list = array_merge($term_list, $children); } } @@ -3889,6 +4006,8 @@ function _pad_term_counts(&$terms, $taxonomy) { return; $term_items = array(); + $terms_by_id = array(); + $term_ids = array(); foreach ( (array) $terms as $key => $term ) { $terms_by_id[$term->term_id] = & $terms[$key]; @@ -3907,12 +4026,18 @@ function _pad_term_counts(&$terms, $taxonomy) { // Touch every ancestor's lookup row for each post in each term foreach ( $term_ids as $term_id ) { $child = $term_id; + $ancestors = array(); while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) { + $ancestors[] = $child; if ( !empty( $term_items[$term_id] ) ) foreach ( $term_items[$term_id] as $item_id => $touches ) { $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1; } $child = $parent; + + if ( in_array( $parent, $ancestors ) ) { + break; + } } } @@ -3969,11 +4094,11 @@ function _update_post_term_count( $terms, $taxonomy ) { $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); /** This action is documented in wp-includes/taxonomy.php */ - do_action( 'edit_term_taxonomy', $term, $taxonomy ); + do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); /** This action is documented in wp-includes/taxonomy.php */ - do_action( 'edited_term_taxonomy', $term, $taxonomy ); + do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); } } @@ -3996,12 +4121,206 @@ function _update_generic_term_count( $terms, $taxonomy ) { $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); /** This action is documented in wp-includes/taxonomy.php */ - do_action( 'edit_term_taxonomy', $term, $taxonomy ); + do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); /** This action is documented in wp-includes/taxonomy.php */ - do_action( 'edited_term_taxonomy', $term, $taxonomy ); + do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); + } +} + +/** + * Create a new term for a term_taxonomy item that currently shares its term with another term_taxonomy. + * + * @ignore + * @since 4.2.0 + * + * @param int $term_id ID of the shared term. + * @param int $term_taxonomy_id ID of the term_taxonomy item to receive a new term. + * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current + * database schema), `$term_id` is returned. When the term is successfully split, the + * new term_id is returned. A WP_Error is returned for miscellaneous errors. + */ +function _split_shared_term( $term_id, $term_taxonomy_id ) { + global $wpdb; + + // Don't try to split terms if database schema does not support shared slugs. + $current_db_version = get_option( 'db_version' ); + if ( $current_db_version < 30133 ) { + return $term_id; + } + + // If there are no shared term_taxonomy rows, there's nothing to do here. + $shared_tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) ); + if ( ! $shared_tt_count ) { + return $term_id; + } + + // Pull up data about the currently shared slug, which we'll use to populate the new one. + $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) ); + + $new_term_data = array( + 'name' => $shared_term->name, + 'slug' => $shared_term->slug, + 'term_group' => $shared_term->term_group, + ); + + if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) { + return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error ); + } + + $new_term_id = (int) $wpdb->insert_id; + + // Update the existing term_taxonomy to point to the newly created term. + $wpdb->update( $wpdb->term_taxonomy, + array( 'term_id' => $new_term_id ), + array( 'term_taxonomy_id' => $term_taxonomy_id ) + ); + + // Reassign child terms to the new parent. + $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); + $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) ); + + if ( ! empty( $children_tt_ids ) ) { + foreach ( $children_tt_ids as $child_tt_id ) { + $wpdb->update( $wpdb->term_taxonomy, + array( 'parent' => $new_term_id ), + array( 'term_taxonomy_id' => $child_tt_id ) + ); + clean_term_cache( $term_id, $term_taxonomy->taxonomy ); + } + } else { + // If the term has no children, we must force its taxonomy cache to be rebuilt separately. + clean_term_cache( $new_term_id, $term_taxonomy->taxonomy ); + } + + // Clean the cache for term taxonomies formerly shared with the current term. + $shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); + if ( $shared_term_taxonomies ) { + foreach ( $shared_term_taxonomies as $shared_term_taxonomy ) { + clean_term_cache( $term_id, $shared_term_taxonomy ); + } + } + + // Keep a record of term_ids that have been split, keyed by old term_id. See {@see wp_get_split_term()}. + $split_term_data = get_option( '_split_terms', array() ); + if ( ! isset( $split_term_data[ $term_id ] ) ) { + $split_term_data[ $term_id ] = array(); + } + + $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id; + + update_option( '_split_terms', $split_term_data ); + + /** + * Fires after a previously shared taxonomy term is split into two separate terms. + * + * @since 4.2.0 + * + * @param int $term_id ID of the formerly shared term. + * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. + * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. + * @param string $taxonomy Taxonomy for the split term. + */ + do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy ); + + return $new_term_id; +} + +/** + * Check default categories when a term gets split to see if any of them need to be updated. + * + * @ignore + * @since 4.2.0 + * + * @param int $term_id ID of the formerly shared term. + * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. + * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. + * @param string $taxonomy Taxonomy for the split term. + */ +function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { + if ( 'category' != $taxonomy ) { + return; } + + foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) { + if ( $term_id == get_option( $option, -1 ) ) { + update_option( $option, $new_term_id ); + } + } +} + +/** + * Check menu items when a term gets split to see if any of them need to be updated. + * + * @ignore + * @since 4.2.0 + * + * @param int $term_id ID of the formerly shared term. + * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. + * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. + * @param string $taxonomy Taxonomy for the split term. + */ +function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { + global $wpdb; + $post_ids = $wpdb->get_col( $wpdb->prepare( + "SELECT m1.post_id + FROM {$wpdb->postmeta} AS m1 + INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id ) + INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id ) + WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' ) + AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' ) + AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )", + $taxonomy, + $term_id + ) ); + + if ( $post_ids ) { + foreach ( $post_ids as $post_id ) { + update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id ); + } + } +} + +/** + * Get data about terms that previously shared a single term_id, but have since been split. + * + * @since 4.2.0 + * + * @param int $old_term_id Term ID. This is the old, pre-split term ID. + * @return array Array of new term IDs, keyed by taxonomy. + */ +function wp_get_split_terms( $old_term_id ) { + $split_terms = get_option( '_split_terms', array() ); + + $terms = array(); + if ( isset( $split_terms[ $old_term_id ] ) ) { + $terms = $split_terms[ $old_term_id ]; + } + + return $terms; +} + +/** + * Get the new term ID corresponding to a previously split term. + * + * @since 4.2.0 + * + * @param int $old_term_id Term ID. This is the old, pre-split term ID. + * @param string $taxonomy Taxonomy that the term belongs to. + * @return bool|int If a previously split term is found corresponding to the old term_id and taxonomy, + * the new term_id will be returned. If no previously split term is found matching + * the parameters, returns false. + */ +function wp_get_split_term( $old_term_id, $taxonomy ) { + $split_terms = wp_get_split_terms( $old_term_id ); + + $term_id = false; + if ( isset( $split_terms[ $taxonomy ] ) ) { + $term_id = (int) $split_terms[ $taxonomy ]; + } + + return $term_id; } /** @@ -4224,7 +4543,7 @@ function get_post_taxonomies( $post = 0 ) { * @param int $object_id ID of the object (post ID, link ID, ...) * @param string $taxonomy Single taxonomy name * @param int|string|array $terms Optional. Term term_id, name, slug or array of said - * @return bool|WP_Error. WP_Error on input error. + * @return bool|WP_Error WP_Error on input error. */ function is_object_in_term( $object_id, $taxonomy, $terms = null ) { if ( !$object_id = (int) $object_id ) @@ -4317,7 +4636,7 @@ function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) if ( ! $resource_type ) { if ( is_taxonomy_hierarchical( $object_type ) ) { $resource_type = 'taxonomy'; - } else if ( post_type_exists( $object_type ) ) { + } elseif ( post_type_exists( $object_type ) ) { $resource_type = 'post_type'; } } @@ -4336,13 +4655,14 @@ function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) * Filter a given object's ancestors. * * @since 3.1.0 + * @since 4.1.0 Introduced the `$resource_type` parameter. * * @param array $ancestors An array of object ancestors. * @param int $object_id Object ID. * @param string $object_type Type of object. * @param string $resource_type Type of resource $object_type is. */ - return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type ); + return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type ); } /**