WordPress.org

Make WordPress Core

Ticket #21760: 21760.2.diff

File 21760.2.diff, 9.8 KB (added by ryan, 5 years ago)

Refresh patch, don't deprecate list_terms_exclusions, back compat cache keys

  • wp-includes/category.php

     
    1414 * @return object List of all of the category IDs.
    1515 */
    1616function get_all_category_ids() {
    17         if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) {
    18                 $cat_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') );
    19                 wp_cache_add( 'all_category_ids', $cat_ids, 'category' );
    20         }
    21 
    22         return $cat_ids;
     17        // this call returns from cache if present
     18        return get_terms( 'category', array( 'fields' => 'ids', 'get' => 'all' ) );
    2319}
    2420
    2521/**
  • wp-includes/taxonomy.php

     
    867867 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    868868 *
    869869 * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
    870  * @param string $taxonomy Taxonomy name that $term is part of.
     870 * @param string $taxonomy Optional if object is passed for $term. Taxonomy name that $term is part of.
    871871 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
    872872 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
    873873 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
    874874 * exist then WP_Error will be returned.
    875875 */
    876 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
     876function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
    877877        global $wpdb;
    878878        $null = null;
    879879
    880         if ( empty($term) ) {
    881                 $error = new WP_Error('invalid_term', __('Empty Term'));
     880        if ( empty( $term ) ) {
     881                $error = new WP_Error( 'invalid_term', __( 'Empty Term' ) );
    882882                return $error;
    883883        }
    884884
    885         if ( ! taxonomy_exists($taxonomy) ) {
    886                 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    887                 return $error;
    888         }
     885        if ( is_object( $term ) )
     886                $taxonomy = $term->taxonomy;
    889887
    890         if ( is_object($term) && empty($term->filter) ) {
    891                 wp_cache_add($term->term_id, $term, $taxonomy);
     888        if ( ! taxonomy_exists( $taxonomy ) )
     889                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
     890
     891        if ( is_object( $term ) && empty( $term->filter ) ) {
     892                update_term_cache( $term, $taxonomy );
    892893                $_term = $term;
    893894        } else {
    894895                if ( is_object($term) )
    895896                        $term = $term->term_id;
    896897                if ( !$term = (int) $term )
    897898                        return $null;
    898                 if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
     899                if ( ! $_term = wp_cache_get( $term, term_cache_group( $taxonomy ) ) ) {
    899900                        $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) );
    900901                        if ( ! $_term )
    901902                                return $null;
    902                         wp_cache_add($term, $_term, $taxonomy);
     903
     904                        update_term_cache( $_term, $taxonomy );
    903905                }
    904906        }
    905907
     
    954956                return false;
    955957
    956958        if ( 'slug' == $field ) {
     959                $value = sanitize_title( $value );
     960                if ( empty( $value ) )
     961                        return false;
     962
     963                $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) );
    957964                $field = 't.slug';
    958                 $value = sanitize_title($value);
    959                 if ( empty($value) )
    960                         return false;
    961965        } else if ( 'name' == $field ) {
    962966                // Assume already escaped
    963                 $value = wp_unslash($value);
     967                $value = wp_unslash( $value );
     968                $term_id = wp_cache_get( $value, term_cache_group( $taxonomy, $field ) );
    964969                $field = 't.name';
    965970        } else {
    966                 $term = get_term( (int) $value, $taxonomy, $output, $filter);
     971                $term_id = $value;
     972
     973                return get_term( $term_id, $taxonomy );
     974        }
     975
     976        if ( ! empty( $term_id ) ) {
     977                $term = get_term( (int) $term_id, $taxonomy, $output, $filter );
     978
    967979                if ( is_wp_error( $term ) )
    968980                        $term = false;
     981
    969982                return $term;
    970983        }
    971984
    972         $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
    973         if ( !$term )
     985        $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) );
     986        if ( ! $term )
    974987                return false;
    975988
    976         wp_cache_add($term->term_id, $term, $taxonomy);
    977 
    978989        $term = apply_filters('get_term', $term, $taxonomy);
    979990        $term = apply_filters("get_$taxonomy", $term, $taxonomy);
    980991        $term = sanitize_term($term, $taxonomy, $filter);
     
    26542665
    26552666        foreach ( $object_ids as $id )
    26562667                foreach ( $taxonomies as $taxonomy )
    2657                         wp_cache_delete($id, "{$taxonomy}_relationships");
     2668                        wp_cache_delete( $id, term_cache_group( $taxonomy, 'relationships' ) );
    26582669
    26592670        do_action('clean_object_term_cache', $object_ids, $object_type);
    26602671}
     
    26752686        global $wpdb;
    26762687        static $cleaned = array();
    26772688
    2678         if ( !is_array($ids) )
    2679                 $ids = array($ids);
     2689        if ( ! is_array( $ids ) )
     2690                $ids = array( $ids );
    26802691
    26812692        $taxonomies = array();
     2693
     2694        $select = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
     2695
     2696        $ids = array_map( 'intval', $ids );
     2697
    26822698        // If no taxonomy, assume tt_ids.
    2683         if ( empty($taxonomy) ) {
    2684                 $tt_ids = array_map('intval', $ids);
    2685                 $tt_ids = implode(', ', $tt_ids);
    2686                 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
     2699        if ( empty( $taxonomy ) ) {
     2700                $terms = $wpdb->get_results( "$select WHERE tt.term_taxonomy_id IN (" . join( ',', $ids ) . ")" );
    26872701                $ids = array();
    2688                 foreach ( (array) $terms as $term ) {
     2702                foreach ( $terms as $term ) {
    26892703                        $taxonomies[] = $term->taxonomy;
    26902704                        $ids[] = $term->term_id;
    2691                         wp_cache_delete($term->term_id, $term->taxonomy);
    26922705                }
    26932706                $taxonomies = array_unique($taxonomies);
    26942707        } else {
    2695                 $taxonomies = array($taxonomy);
    2696                 foreach ( $taxonomies as $taxonomy ) {
    2697                         foreach ( $ids as $id ) {
    2698                                 wp_cache_delete($id, $taxonomy);
    2699                         }
    2700                 }
     2708                $terms = $wpdb->get_results( $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND tt.term_id IN (" . join( ',', $ids ) . ")", $taxonomy ) );
     2709                $taxonomies = array( $taxonomy );
    27012710        }
    27022711
     2712        foreach ( $terms as $term ) {
     2713                wp_cache_delete( $term->term_id, term_cache_group( $term->taxonomy ) );
     2714                wp_cache_delete( $term->name, term_cache_group( $term->taxonomy, 'names' ) );
     2715                wp_cache_delete( $term->slug, term_cache_group( $term->taxonomy, 'slugs' ) );
     2716        }
     2717
    27032718        foreach ( $taxonomies as $taxonomy ) {
    27042719                if ( isset($cleaned[$taxonomy]) )
    27052720                        continue;
    27062721                $cleaned[$taxonomy] = true;
    27072722
    27082723                if ( $clean_taxonomy ) {
    2709                         wp_cache_delete('all_ids', $taxonomy);
    2710                         wp_cache_delete('get', $taxonomy);
    27112724                        delete_option("{$taxonomy}_children");
    27122725                        // Regenerate {$taxonomy}_children
    27132726                        _get_term_hierarchy($taxonomy);
     
    27332746 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
    27342747 */
    27352748function get_object_term_cache($id, $taxonomy) {
    2736         $cache = wp_cache_get($id, "{$taxonomy}_relationships");
     2749        $cache = wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) );
    27372750        return $cache;
    27382751}
    27392752
     
    27732786        $ids = array();
    27742787        foreach ( (array) $object_ids as $id ) {
    27752788                foreach ( $taxonomies as $taxonomy ) {
    2776                         if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
     2789                        if ( false === wp_cache_get( $id, term_cache_group( $taxonomy, 'relationships' ) ) ) {
    27772790                                $ids[] = $id;
    27782791                                break;
    27792792                        }
     
    28012814
    28022815        foreach ( $object_terms as $id => $value ) {
    28032816                foreach ( $value as $taxonomy => $terms ) {
    2804                         wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
     2817                        wp_cache_add( $id, $terms, term_cache_group( $taxonomy, 'relationships' ) );
    28052818                }
    28062819        }
    28072820}
     
    28162829 * @param array $terms List of Term objects to change
    28172830 * @param string $taxonomy Optional. Update Term to this taxonomy in cache
    28182831 */
    2819 function update_term_cache($terms, $taxonomy = '') {
    2820         foreach ( (array) $terms as $term ) {
    2821                 $term_taxonomy = $taxonomy;
    2822                 if ( empty($term_taxonomy) )
    2823                         $term_taxonomy = $term->taxonomy;
     2832function update_term_cache( $terms, $taxonomy = '' ) {
     2833        if ( ! is_array( $terms ) )
     2834                $terms = array( $terms );
    28242835
    2825                 wp_cache_add($term->term_id, $term, $term_taxonomy);
     2836        foreach ( $terms as $term ) {
     2837                $tax = $taxonomy;
     2838                if ( empty( $tax ) )
     2839                        $tax = $term->taxonomy;
     2840
     2841                wp_cache_add( $term->term_id, $term, term_cache_group( $tax ) );
     2842                wp_cache_add( $term->name, $term->term_id, term_cache_group( $tax, 'names' ) );
     2843                wp_cache_add( $term->slug, $term->term_id, term_cache_group( $tax, 'slugs' ) );
    28262844        }
    28272845}
    28282846
     2847/**
     2848 *
     2849 * @package WordPress
     2850 * @subpackage Taxonomy
     2851 * @since 3.6.0
     2852 *
     2853 * @param string $taxonomy Taxonomy
     2854 * @param string $group Optional. ids, names, slugs, or relationships.
     2855 */
     2856function term_cache_group( $taxonomy, $group = 'ids' ) {
     2857        $tax = get_taxonomy( $taxonomy );
     2858
     2859        if ( ! $tax )
     2860                return false;
     2861
     2862        switch ( $group ) {
     2863                case 'names':
     2864                case 'slugs':
     2865                        $key = "$taxonomy:$group";
     2866                        break;
     2867                case 'relationships':
     2868                        $key = "{$taxonomy}_relationships";
     2869                        break;
     2870                default:
     2871                        $key = $taxonomy;
     2872                        break;
     2873        }
     2874
     2875        return $key;
     2876}
     2877
    28292878//
    28302879// Private
    28312880//
  • wp-includes/category-template.php

     
    10651065        $terms = get_object_term_cache( $post->ID, $taxonomy );
    10661066        if ( false === $terms ) {
    10671067                $terms = wp_get_object_terms( $post->ID, $taxonomy );
    1068                 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
     1068                wp_cache_add( $post->ID, $terms, term_cache_group( $taxonomy, 'relationships' ) );
    10691069        }
    10701070
    10711071        $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );