Ticket #21760: 21760.patch
File 21760.patch, 7.7 KB (added by , 8 years ago) |
---|
-
taxonomy.php
873 873 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not 874 874 * exist then WP_Error will be returned. 875 875 */ 876 function get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw') {876 function get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw' ) { 877 877 global $wpdb; 878 $null = null;879 878 880 879 if ( empty($term) ) { 881 $error = new WP_Error('invalid_term', __('Empty Term')); 882 return $error; 880 return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 883 881 } 884 882 885 if ( ! taxonomy_exists($taxonomy) ) { 886 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); 887 return $error; 883 if ( ! taxonomy_exists( $taxonomy ) ) { 884 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 888 885 } 889 886 890 if ( is_object( $term) && empty($term->filter) ) {891 wp_cache_add( $term->term_id, $term, $taxonomy);887 if ( is_object( $term ) && empty( $term->filter ) ) { 888 wp_cache_add( $term->term_id, $term, $taxonomy ); 892 889 $_term = $term; 893 890 } else { 894 if ( is_object( $term) )891 if ( is_object( $term ) ) { 895 892 $term = $term->term_id; 896 if ( !$term = (int) $term )897 return $null;898 if ( ! $_term = wp_cache_get($term, $taxonomy) ) {899 $_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) );900 if ( ! $_term )901 return $null;902 wp_cache_add($term, $_term, $taxonomy);903 893 } 894 895 if ( empty( $term ) ) { 896 return null; 897 } 898 899 if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) { 900 $_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, (int) $term ) ); 901 if ( empty( $_term ) ) { 902 return null; 903 } 904 wp_cache_add( $term, $_term, $taxonomy ); 905 } 904 906 } 905 907 906 $_term = apply_filters( 'get_term', $_term, $taxonomy);907 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy);908 $_term = sanitize_term( $_term, $taxonomy, $filter);908 $_term = apply_filters( 'get_term', $_term, $taxonomy ); 909 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 910 $_term = sanitize_term( $_term, $taxonomy, $filter ); 909 911 910 if ( $output == OBJECT ) {912 if ( $output === OBJECT ) { 911 913 return $_term; 912 } elseif ( $output == ARRAY_A ) {913 $__term = get_object_vars( $_term);914 } elseif ( $output === ARRAY_A ) { 915 $__term = get_object_vars( $_term ); 914 916 return $__term; 915 } elseif ( $output == ARRAY_N ) {916 $__term = array_values( get_object_vars($_term));917 } elseif ( $output === ARRAY_N ) { 918 $__term = array_values( get_object_vars( $_term ) ); 917 919 return $__term; 918 920 } else { 919 921 return $_term; … … 937 939 * @since 2.3.0 938 940 * 939 941 * @uses $wpdb 942 * @uses _get_term_by() To query the database directly if no cached data exists 943 * @uses get_term() To get the term if it's already cached 944 * 945 * @param string $field Either 'slug', 'name', or 'id' 946 * @param string|int $value Search for this term value 947 * @param string $taxonomy Taxonomy Name 948 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 949 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 950 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found. 951 */ 952 function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) { 953 954 // Allow plugins to short-circuit get_term_by() 955 $pre = apply_filters( 'pre_get_term_by_' . $field . '_' . $value . '_' . $taxonomy, false ); 956 if ( false !== $pre ) 957 return $pre; 958 959 // ID lookups are already cached 960 if ( 'id' == $field ) { 961 return _get_term_by( $field, $value, $taxonomy, $output, $filter ); 962 } 963 964 // Build the key and check the cache 965 $cache_key = $field . '|' . $taxonomy . '|' . md5( $value ); 966 $term_id = wp_cache_get( $cache_key, 'get_term_by' ); 967 968 // Not cached so hit the DB 969 if ( false === $term_id ) { 970 $term = _get_term_by( $field, $value, $taxonomy ); 971 $term_id = !empty( $term ) && ! is_wp_error( $term ) ? $term->term_id : 0; 972 973 wp_cache_set( $cache_key, $term_id, 'get_term_by' ); 974 975 // Cached, so call get_term() to get the term 976 } else { 977 $term = get_term( $term_id, $taxonomy, $output, $filter ); 978 } 979 980 // Set the return value to false if an error occurred 981 if ( is_wp_error( $term ) ) { 982 $term = false; 983 } 984 985 return $term; 986 } 987 988 /** 989 * Used internally by get_term_by(), this function is responsible for querying 990 * the database directly and priming the cache if it's needed. 991 * 992 * @package WordPress 993 * @subpackage Taxonomy 994 * @since 3.7.0 995 * @internal Used by get_term_by() 996 * 997 * @uses $wpdb 940 998 * @uses sanitize_term() Cleanses the term based on $filter context before returning. 941 999 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. 942 1000 * … … 947 1005 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 948 1006 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found. 949 1007 */ 950 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {1008 function _get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) { 951 1009 global $wpdb; 952 1010 953 if ( ! taxonomy_exists($taxonomy) ) 1011 // Taxonomy is currently a required part of the query below, so bail if it's 1012 // missing, not registered, etc... 1013 if ( ! taxonomy_exists( $taxonomy ) ) 954 1014 return false; 955 1015 1016 // Querying by slug 956 1017 if ( 'slug' == $field ) { 957 1018 $field = 't.slug'; 958 $value = sanitize_title($value); 959 if ( empty($value) ) 1019 $value = sanitize_title( $value ); 1020 1021 // Bail if no value to check 1022 if ( empty( $value ) ) { 960 1023 return false; 1024 } 961 1025 } else if ( 'name' == $field ) { 962 // Assume already escaped 963 $value = wp_unslash($value); 1026 $value = wp_unslash( $value ); // Assume already escaped 964 1027 $field = 't.name'; 965 1028 } else { 966 $term = get_term( (int) $value, $taxonomy, $output, $filter );967 if ( is_wp_error( $term ) ) 1029 $term = get_term( (int) $value, $taxonomy, $output, $filter ); 1030 if ( is_wp_error( $term ) ) { 968 1031 $term = false; 1032 } 969 1033 return $term; 970 1034 } 971 1035 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 ) 1036 // Query for the term 1037 $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 ) ); 1038 if ( empty( $term ) ) { 974 1039 return false; 1040 } 975 1041 976 wp_cache_add($term->term_id, $term, $taxonomy); 1042 // Prime the term cache in case it's used elsewhere (like a loop of posts) 1043 wp_cache_add( $term->term_id, $term, $taxonomy ); 977 1044 978 $term = apply_filters('get_term', $term, $taxonomy); 979 $term = apply_filters("get_$taxonomy", $term, $taxonomy); 980 $term = sanitize_term($term, $taxonomy, $filter); 1045 // Filter and sanitize the term 1046 $term = apply_filters( 'get_term', $term, $taxonomy ); 1047 $term = apply_filters( "get_$taxonomy", $term, $taxonomy ); 1048 $term = sanitize_term( $term, $taxonomy, $filter ); 981 1049 1050 // Adjust the return value based on the $output type 982 1051 if ( $output == OBJECT ) { 983 1052 return $term; 984 1053 } elseif ( $output == ARRAY_A ) { 985 return get_object_vars( $term);1054 return get_object_vars( $term ); 986 1055 } elseif ( $output == ARRAY_N ) { 987 return array_values( get_object_vars($term));1056 return array_values( get_object_vars( $term ) ); 988 1057 } else { 989 1058 return $term; 990 1059 }