Changeset 34997 for trunk/src/wp-includes/taxonomy-functions.php
- Timestamp:
- 10/10/2015 01:58:37 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy-functions.php
r34891 r34997 709 709 * 710 710 * @since 2.3.0 711 * @since 4.4.0 Converted to return a WP_Term object if `$output` is `OBJECT`. 712 * The `$taxonomy` parameter was made optional. 711 713 * 712 714 * @global wpdb $wpdb WordPress database abstraction object. 713 715 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. 714 716 * 715 * @param int|object $term If integer, will get from database. If object will apply filters and return $term. 716 * @param string $taxonomy Taxonomy name that $term is part of. 717 * @param int|WP_Term|object $term If integer, term data will be fetched from the database, or from the cache if 718 * available. If stdClass object (as in the results of a database query), will apply 719 * filters and return a `WP_Term` object corresponding to the `$term` data. If `WP_Term`, 720 * will return `$term`. 721 * @param string $taxonomy Optional. Taxonomy name that $term is part of. 717 722 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 718 723 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 719 * @return object|array|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not 720 * exist then WP_Error will be returned. 721 */ 722 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 723 global $wpdb; 724 724 * @return mixed Type corresponding to `$output` on success or null on failure. When `$output` is `OBJECT`, 725 * a WP_Term instance is returned. If taxonomy does not exist then WP_Error will be returned. 726 */ 727 function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 725 728 if ( empty( $term ) ) { 726 729 return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 727 730 } 728 731 729 if ( ! taxonomy_exists( $taxonomy ) ) {732 if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) { 730 733 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 731 734 } 732 735 733 if ( is_object($term) && empty($term->filter) ) { 734 wp_cache_add( $term->term_id, $term, $taxonomy ); 736 if ( $term instanceof WP_Term ) { 735 737 $_term = $term; 738 } elseif ( is_object( $term ) ) { 739 if ( empty( $term->filter ) || 'raw' === $term->filter ) { 740 $_term = sanitize_term( $term, $taxonomy, 'raw' ); 741 $_term = new WP_Term( $_term ); 742 } else { 743 $_term = WP_Term::get_instance( $term->term_id ); 744 } 736 745 } else { 737 if ( is_object($term) ) 738 $term = $term->term_id; 739 if ( !$term = (int) $term ) 740 return null; 741 if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) { 742 $_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) ); 743 if ( ! $_term ) 744 return null; 745 wp_cache_add( $term, $_term, $taxonomy ); 746 } 746 $_term = WP_Term::get_instance( $term ); 747 } 748 749 // If `$taxonomy` was provided, make sure it matches the taxonomy of the located term. 750 if ( $_term && $taxonomy && $taxonomy !== $_term->taxonomy ) { 751 // If there are two terms with the same ID, split the other one to a new term. 752 $new_term_id = _split_shared_term( $_term->term_id, $_term->term_taxonomy_id ); 753 754 // If no split occurred, this is an invalid request. 755 if ( $new_term_id === $_term->term_id ) { 756 return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 757 758 // The term has been split. Refetch the term from the proper taxonomy. 759 } else { 760 return get_term( $_term->term_id, $taxonomy, $output, $filter ); 761 } 762 } 763 764 if ( ! $_term ) { 765 return null; 747 766 } 748 767 … … 751 770 * 752 771 * @since 2.3.0 753 * 754 * @param int|object $_term Term object or ID. 755 * @param string $taxonomy The taxonomy slug. 772 * @since 4.4.0 `$_term` can now also be a WP_Term object. 773 * 774 * @param int|WP_Term $_term Term object or ID. 775 * @param string $taxonomy The taxonomy slug. 756 776 */ 757 777 $_term = apply_filters( 'get_term', $_term, $taxonomy ); … … 764 784 * 765 785 * @since 2.3.0 766 * 767 * @param int|object $_term Term object or ID. 768 * @param string $taxonomy The taxonomy slug. 786 * @since 4.4.0 `$_term` can now also be a WP_Term object. 787 * 788 * @param int|WP_Term $_term Term object or ID. 789 * @param string $taxonomy The taxonomy slug. 769 790 */ 770 791 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 771 $_term = sanitize_term($_term, $taxonomy, $filter); 772 773 if ( $output == OBJECT ) { 774 return $_term; 775 } elseif ( $output == ARRAY_A ) { 776 $__term = get_object_vars($_term); 777 return $__term; 792 793 // Sanitize term, according to the specified filter. 794 $_term->filter( $filter ); 795 796 if ( $output == ARRAY_A ) { 797 return $_term->to_array(); 778 798 } elseif ( $output == ARRAY_N ) { 779 $__term = array_values(get_object_vars($_term)); 780 return $__term; 781 } else { 782 return $_term; 783 } 799 return array_values( $_term->to_array() ); 800 } 801 802 return $_term; 784 803 } 785 804 … … 799 818 * 800 819 * @since 2.3.0 801 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. 820 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return 821 * a WP_Term object if `$output` is `OBJECT`. 802 822 * 803 823 * @global wpdb $wpdb WordPress database abstraction object. … … 809 829 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 810 830 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 811 * @return object|array|null|WP_Error|false Term Row from database.812 * Will return false if $taxonomy does not exist or $termwas not found.831 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist 832 * or `$term` was not found. 813 833 */ 814 834 function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { … … 823 843 824 844 if ( 'slug' == $field ) { 825 $ field = 't.slug';845 $_field = 't.slug'; 826 846 $value = sanitize_title($value); 827 847 if ( empty($value) ) … … 830 850 // Assume already escaped 831 851 $value = wp_unslash($value); 832 $ field = 't.name';852 $_field = 't.name'; 833 853 } elseif ( 'term_taxonomy_id' == $field ) { 834 854 $value = (int) $value; 835 $ field = 'tt.term_taxonomy_id';855 $_field = 'tt.term_taxonomy_id'; 836 856 837 857 // No `taxonomy` clause when searching by 'term_taxonomy_id'. … … 845 865 } 846 866 847 $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 $ field = %s $tax_clause LIMIT 1", $value ) );867 $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 $_field = %s $tax_clause LIMIT 1", $value ) ); 848 868 if ( ! $term ) 849 869 return false; … … 854 874 } 855 875 856 wp_cache_add( $term->term_id, $term, $taxonomy ); 857 858 /** This filter is documented in wp-includes/taxonomy-functions.php */ 859 $term = apply_filters( 'get_term', $term, $taxonomy ); 860 861 /** This filter is documented in wp-includes/taxonomy-functions.php */ 862 $term = apply_filters( "get_$taxonomy", $term, $taxonomy ); 863 864 $term = sanitize_term($term, $taxonomy, $filter); 865 866 if ( $output == OBJECT ) { 867 return $term; 868 } elseif ( $output == ARRAY_A ) { 869 return get_object_vars($term); 870 } elseif ( $output == ARRAY_N ) { 871 return array_values(get_object_vars($term)); 872 } else { 873 return $term; 874 } 876 wp_cache_add( $term->term_id, $term, 'terms' ); 877 878 return get_term( $term, $taxonomy, $output, $filter ); 875 879 } 876 880 … … 3423 3427 $taxonomies[] = $term->taxonomy; 3424 3428 $ids[] = $term->term_id; 3425 wp_cache_delete( $term->term_id, $term->taxonomy);3429 wp_cache_delete( $term->term_id, 'terms' ); 3426 3430 } 3427 3431 $taxonomies = array_unique($taxonomies); … … 3430 3434 foreach ( $taxonomies as $taxonomy ) { 3431 3435 foreach ( $ids as $id ) { 3432 wp_cache_delete( $id, $taxonomy);3436 wp_cache_delete( $id, 'terms' ); 3433 3437 } 3434 3438 } … … 3553 3557 $term_taxonomy = $term->taxonomy; 3554 3558 3555 wp_cache_add( $term->term_id, $term, $term_taxonomy);3559 wp_cache_add( $term->term_id, $term, 'terms' ); 3556 3560 } 3557 3561 }
Note: See TracChangeset
for help on using the changeset viewer.