WordPress.org

Make WordPress Core

Ticket #21760: get-term-mixed.diff

File get-term-mixed.diff, 11.5 KB (added by wonderboymusic, 8 years ago)
  • wp-includes/taxonomy.php

     
    852852 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
    853853 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    854854 *
    855  * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
     855 * @param mixed $term If integer or string, will get from database or cache. If object, will apply filters, cache, and return $term.
    856856 * @param string $taxonomy Taxonomy name that $term is part of.
    857857 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
    858858 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
    859859 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
    860860 * exist then WP_Error will be returned.
    861861 */
    862 function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
     862function &get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
    863863        global $wpdb;
    864864        $null = null;
    865865
    866         if ( empty($term) ) {
    867                 $error = new WP_Error('invalid_term', __('Empty Term'));
     866        if ( empty( $term ) ) {
     867                $error = new WP_Error( 'invalid_term', __( 'Empty Term' ) );
    868868                return $error;
    869869        }
    870870
    871         if ( ! taxonomy_exists($taxonomy) ) {
    872                 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     871        if ( ! taxonomy_exists( $taxonomy ) ) {
     872                $error = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    873873                return $error;
    874874        }
    875875
    876         if ( is_object($term) && empty($term->filter) ) {
    877                 wp_cache_add($term->term_id, $term, $taxonomy);
     876        if ( is_object( $term ) && empty( $term->filter ) ) {
     877                wp_cache_add( $term->term_id, $term, $taxonomy );
    878878                $_term = $term;
    879879        } else {
    880                 if ( is_object($term) )
    881                         $term = $term->term_id;
    882                 if ( !$term = (int) $term )
    883                         return $null;
    884                 if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
    885                         $_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) );
     880                if ( is_numeric( $term ) ) {
     881                        $term = (int) $term;
     882                } elseif ( is_object( $term ) ) {
     883                        $term = (int) $term->term_id;
     884                }
     885               
     886                if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
     887                        $select = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
     888                       
     889                        if ( is_string( $term ) )
     890                                $query = $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND (t.name = %s OR t.slug = %s) LIMIT 1",
     891                                        $taxonomy,
     892                                        stripslashes( $term ),
     893                                        sanitize_title( $term )
     894                                );
     895                        else
     896                                $query = $wpdb->prepare( "$select WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term );
     897                       
     898                        $_term = $wpdb->get_row( $query );
     899                       
    886900                        if ( ! $_term )
    887901                                return $null;
    888                         wp_cache_add($term, $_term, $taxonomy);
     902                        wp_cache_add( $term, $_term, $taxonomy );
    889903                }
    890904        }
    891905
    892         $_term = apply_filters('get_term', $_term, $taxonomy);
    893         $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
    894         $_term = sanitize_term($_term, $taxonomy, $filter);
     906        $_term = apply_filters( 'get_term', $_term, $taxonomy );
     907        $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
     908        $_term = sanitize_term( $_term, $taxonomy, $filter );
    895909
    896910        if ( $output == OBJECT ) {
    897911                return $_term;
    898912        } elseif ( $output == ARRAY_A ) {
    899                 $__term = get_object_vars($_term);
     913                $__term = get_object_vars( $_term );
    900914                return $__term;
    901915        } elseif ( $output == ARRAY_N ) {
    902                 $__term = array_values(get_object_vars($_term));
     916                $__term = array_values( get_object_vars( $_term ) );
    903917                return $__term;
    904918        } else {
    905919                return $_term;
     
    907921}
    908922
    909923/**
    910  * Get all Term data from database by Term field and data.
    911  *
    912  * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
    913  * required.
    914  *
    915  * The default $field is 'id', therefore it is possible to also use null for
    916  * field, but not recommended that you do so.
    917  *
    918  * If $value does not exist, the return value will be false. If $taxonomy exists
    919  * and $field and $value combinations exist, the Term will be returned.
    920  *
    921  * @package WordPress
    922  * @subpackage Taxonomy
    923  * @since 2.3.0
    924  *
    925  * @uses $wpdb
    926  * @uses sanitize_term() Cleanses the term based on $filter context before returning.
    927  * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    928  *
    929  * @param string $field Either 'slug', 'name', or 'id'
    930  * @param string|int $value Search for this term value
    931  * @param string $taxonomy Taxonomy Name
    932  * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
    933  * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
    934  * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
    935  */
    936 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
    937         global $wpdb;
    938 
    939         if ( ! taxonomy_exists($taxonomy) )
    940                 return false;
    941 
    942         if ( 'slug' == $field ) {
    943                 $field = 't.slug';
    944                 $value = sanitize_title($value);
    945                 if ( empty($value) )
    946                         return false;
    947         } else if ( 'name' == $field ) {
    948                 // Assume already escaped
    949                 $value = stripslashes($value);
    950                 $field = 't.name';
    951         } else {
    952                 $term = get_term( (int) $value, $taxonomy, $output, $filter);
    953                 if ( is_wp_error( $term ) )
    954                         $term = false;
    955                 return $term;
    956         }
    957 
    958         $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) );
    959         if ( !$term )
    960                 return false;
    961 
    962         wp_cache_add($term->term_id, $term, $taxonomy);
    963 
    964         $term = apply_filters('get_term', $term, $taxonomy);
    965         $term = apply_filters("get_$taxonomy", $term, $taxonomy);
    966         $term = sanitize_term($term, $taxonomy, $filter);
    967 
    968         if ( $output == OBJECT ) {
    969                 return $term;
    970         } elseif ( $output == ARRAY_A ) {
    971                 return get_object_vars($term);
    972         } elseif ( $output == ARRAY_N ) {
    973                 return array_values(get_object_vars($term));
    974         } else {
    975                 return $term;
    976         }
    977 }
    978 
    979 /**
    980924 * Merge all term children into a single array of their IDs.
    981925 *
    982926 * This recursive function will merge all of the children of $term into the same
     
    25692513 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
    25702514 * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.
    25712515 */
    2572 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
     2516function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
    25732517        global $wpdb;
    25742518        static $cleaned = array();
    25752519
    2576         if ( !is_array($ids) )
    2577                 $ids = array($ids);
     2520        if ( ! is_array( $ids ) )
     2521                $ids = array( $ids );
    25782522
    25792523        $taxonomies = array();
    25802524        // If no taxonomy, assume tt_ids.
    2581         if ( empty($taxonomy) ) {
    2582                 $tt_ids = array_map('intval', $ids);
    2583                 $tt_ids = implode(', ', $tt_ids);
    2584                 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
     2525        if ( empty( $taxonomy ) ) {
     2526                $tt_ids = array_map( 'intval', $ids );
     2527                $tt_ids = implode( ', ', $tt_ids );
     2528               
     2529                $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.name, t.slug, t.term_id, tt.taxonomy FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt WHERE tt.term_taxonomy_id IN ($tt_ids)" ) );
    25852530                $ids = array();
    25862531                foreach ( (array) $terms as $term ) {
    25872532                        $taxonomies[] = $term->taxonomy;
    25882533                        $ids[] = $term->term_id;
    2589                         wp_cache_delete($term->term_id, $term->taxonomy);
     2534                        wp_cache_delete( $term->term_id, $term->taxonomy );
     2535                        wp_cache_delete( $term->name, $term->taxonomy );
     2536                        wp_cache_delete( $term->slug, $term->taxonomy );
    25902537                }
    2591                 $taxonomies = array_unique($taxonomies);
     2538                $taxonomies = array_unique( $taxonomies );
    25922539        } else {
    2593                 $taxonomies = array($taxonomy);
    2594                 foreach ( $taxonomies as $taxonomy ) {
    2595                         foreach ( $ids as $id ) {
    2596                                 wp_cache_delete($id, $taxonomy);
     2540                foreach ( $ids as $id ) {
     2541                        $found = false;
     2542                        $term = wp_cache_get( $id, $taxonomy );
     2543                        wp_cache_delete( $id, $taxonomy );
     2544                       
     2545                        if ( ! empty( $term ) ) {
     2546                                wp_cache_delete( $term->name, $term->taxonomy );
     2547                                wp_cache_delete( $term->slug, $term->taxonomy );
    25972548                        }
    25982549                }
     2550                $taxonomies = array( $taxonomy );
    25992551        }
    26002552
    26012553        foreach ( $taxonomies as $taxonomy ) {
    2602                 if ( isset($cleaned[$taxonomy]) )
     2554                if ( isset( $cleaned[$taxonomy] ) )
    26032555                        continue;
    26042556                $cleaned[$taxonomy] = true;
    26052557
    26062558                if ( $clean_taxonomy ) {
    2607                         wp_cache_delete('all_ids', $taxonomy);
    2608                         wp_cache_delete('get', $taxonomy);
    2609                         delete_option("{$taxonomy}_children");
     2559                        wp_cache_delete( 'get', $taxonomy );
     2560                        delete_option( "{$taxonomy}_children" );
    26102561                        // Regenerate {$taxonomy}_children
    2611                         _get_term_hierarchy($taxonomy);
     2562                        _get_term_hierarchy( $taxonomy );
    26122563                }
    26132564
    2614                 do_action('clean_term_cache', $ids, $taxonomy);
     2565                do_action( 'clean_term_cache', $ids, $taxonomy );
    26152566        }
    26162567
    2617         wp_cache_set('last_changed', time(), 'terms');
     2568        wp_cache_set( 'last_changed', time(), 'terms' );
    26182569}
    26192570
    26202571/**
  • wp-includes/deprecated.php

     
    32243224        return get_post( $postid, $mode, 'edit' );
    32253225}
    32263226
     3227/**
     3228 * Get all Term data from database by Term field and data.
     3229 *
     3230 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
     3231 * required.
     3232 *
     3233 * The default $field is 'id', therefore it is possible to also use null for
     3234 * field, but not recommended that you do so.
     3235 *
     3236 * If $value does not exist, the return value will be false. If $taxonomy exists
     3237 * and $field and $value combinations exist, the Term will be returned.
     3238 *
     3239 * @package WordPress
     3240 * @subpackage Taxonomy
     3241 * @since 3.5.0
     3242 *
     3243 * @uses $wpdb
     3244 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
     3245 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
     3246 *
     3247 * @param string $field Either 'slug', 'name', or 'id'
     3248 * @param string|int $value Search for this term value
     3249 * @param string $taxonomy Taxonomy Name
     3250 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
     3251 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
     3252 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
     3253 */
     3254function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
     3255        _deprecated_function( __FUNCTION__, '3.5', 'get_term()' );
     3256       
     3257        return get_term( $value, $taxonomy, $output = OBJECT, $filter = 'raw' );
     3258}
     3259 No newline at end of file
  • wp-content/themes/twentyeleven/functions.php

     
    11<?php
     2add_filter( 'query', function ( $sql ) { error_log( $sql ); return $sql; } );
     3
     4$term = get_term( 'under-under', 'category' );
     5var_dump( $term );
     6$term = get_term_by( 'slug', 'under-under', 'category' );
     7var_dump( $term );
     8clean_term_cache( $term->term_id, 'category' );
     9$term = get_term( 'under-under', 'category' );
     10var_dump( $term );
     11$term = get_term_by( 'slug', 'under-under', 'category' );
     12var_dump( $term );
     13exit();
    214/**
    315 * Twenty Eleven functions and definitions
    416 *