-
+ C0D76FC9BA2D84013AE0E7219374087D545C1B20E92758B5C7816CFBA55BB1F6B0EE10AE394D1D980AFE132E6E898C07DB8425A68DD732F2129028BA7E10A8C3
mp-wp/wp-includes/taxonomy.php
(0 . 0)(1 . 2277)
151571 <?php
151572 /**
151573 * Taxonomy API
151574 *
151575 * @package WordPress
151576 * @subpackage Taxonomy
151577 * @since 2.3.0
151578 */
151579
151580 //
151581 // Taxonomy Registration
151582 //
151583
151584 /**
151585 * Default Taxonomy Objects
151586 * @since 2.3.0
151587 * @global array $wp_taxonomies
151588 */
151589 $wp_taxonomies = array();
151590 $wp_taxonomies['category'] = (object) array('name' => 'category', 'object_type' => 'post', 'hierarchical' => true, 'update_count_callback' => '_update_post_term_count');
151591 $wp_taxonomies['post_tag'] = (object) array('name' => 'post_tag', 'object_type' => 'post', 'hierarchical' => false, 'update_count_callback' => '_update_post_term_count');
151592 $wp_taxonomies['link_category'] = (object) array('name' => 'link_category', 'object_type' => 'link', 'hierarchical' => false);
151593
151594 /**
151595 * Return all of the taxonomy names that are of $object_type.
151596 *
151597 * It appears that this function can be used to find all of the names inside of
151598 * $wp_taxonomies global variable.
151599 *
151600 * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should
151601 * result in <code>Array('category', 'post_tag')</code>
151602 *
151603 * @package WordPress
151604 * @subpackage Taxonomy
151605 * @since 2.3.0
151606 *
151607 * @uses $wp_taxonomies
151608 *
151609 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
151610 * @return array The names of all taxonomy of $object_type.
151611 */
151612 function get_object_taxonomies($object) {
151613 global $wp_taxonomies;
151614
151615 if ( is_object($object) ) {
151616 if ( $object->post_type == 'attachment' )
151617 return get_attachment_taxonomies($object);
151618 $object = $object->post_type;
151619 }
151620
151621 $object = (array) $object;
151622
151623 $taxonomies = array();
151624 foreach ( (array) $wp_taxonomies as $taxonomy ) {
151625 if ( array_intersect($object, (array) $taxonomy->object_type) )
151626 $taxonomies[] = $taxonomy->name;
151627 }
151628
151629 return $taxonomies;
151630 }
151631
151632 /**
151633 * Retrieves the taxonomy object of $taxonomy.
151634 *
151635 * The get_taxonomy function will first check that the parameter string given
151636 * is a taxonomy object and if it is, it will return it.
151637 *
151638 * @package WordPress
151639 * @subpackage Taxonomy
151640 * @since 2.3.0
151641 *
151642 * @uses $wp_taxonomies
151643 * @uses is_taxonomy() Checks whether taxonomy exists
151644 *
151645 * @param string $taxonomy Name of taxonomy object to return
151646 * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist
151647 */
151648 function get_taxonomy( $taxonomy ) {
151649 global $wp_taxonomies;
151650
151651 if ( ! is_taxonomy($taxonomy) )
151652 return false;
151653
151654 return $wp_taxonomies[$taxonomy];
151655 }
151656
151657 /**
151658 * Checks that the taxonomy name exists.
151659 *
151660 * @package WordPress
151661 * @subpackage Taxonomy
151662 * @since 2.3.0
151663 *
151664 * @uses $wp_taxonomies
151665 *
151666 * @param string $taxonomy Name of taxonomy object
151667 * @return bool Whether the taxonomy exists or not.
151668 */
151669 function is_taxonomy( $taxonomy ) {
151670 global $wp_taxonomies;
151671
151672 return isset($wp_taxonomies[$taxonomy]);
151673 }
151674
151675 /**
151676 * Whether the taxonomy object is hierarchical.
151677 *
151678 * Checks to make sure that the taxonomy is an object first. Then Gets the
151679 * object, and finally returns the hierarchical value in the object.
151680 *
151681 * A false return value might also mean that the taxonomy does not exist.
151682 *
151683 * @package WordPress
151684 * @subpackage Taxonomy
151685 * @since 2.3.0
151686 *
151687 * @uses is_taxonomy() Checks whether taxonomy exists
151688 * @uses get_taxonomy() Used to get the taxonomy object
151689 *
151690 * @param string $taxonomy Name of taxonomy object
151691 * @return bool Whether the taxonomy is hierarchical
151692 */
151693 function is_taxonomy_hierarchical($taxonomy) {
151694 if ( ! is_taxonomy($taxonomy) )
151695 return false;
151696
151697 $taxonomy = get_taxonomy($taxonomy);
151698 return $taxonomy->hierarchical;
151699 }
151700
151701 /**
151702 * Create or modify a taxonomy object. Do not use before init.
151703 *
151704 * A simple function for creating or modifying a taxonomy object based on the
151705 * parameters given. The function will accept an array (third optional
151706 * parameter), along with strings for the taxonomy name and another string for
151707 * the object type.
151708 *
151709 * Nothing is returned, so expect error maybe or use is_taxonomy() to check
151710 * whether taxonomy exists.
151711 *
151712 * Optional $args contents:
151713 *
151714 * hierarachical - has some defined purpose at other parts of the API and is a
151715 * boolean value.
151716 *
151717 * update_count_callback - works much like a hook, in that it will be called
151718 * when the count is updated.
151719 *
151720 * rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize
151721 * permastruct; default will use $taxonomy as slug.
151722 *
151723 * query_var - false to prevent queries, or string to customize query var
151724 * (?$query_var=$term); default will use $taxonomy as query var.
151725 *
151726 * @package WordPress
151727 * @subpackage Taxonomy
151728 * @since 2.3.0
151729 * @uses $wp_taxonomies Inserts new taxonomy object into the list
151730 * @uses $wp_rewrite Adds rewrite tags and permastructs
151731 * @uses $wp Adds query vars
151732 *
151733 * @param string $taxonomy Name of taxonomy object
151734 * @param array|string $object_type Name of the object type for the taxonomy object.
151735 * @param array|string $args See above description for the two keys values.
151736 */
151737 function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
151738 global $wp_taxonomies, $wp_rewrite, $wp;
151739
151740 $defaults = array('hierarchical' => false, 'update_count_callback' => '', 'rewrite' => true, 'query_var' => true);
151741 $args = wp_parse_args($args, $defaults);
151742
151743 if ( false !== $args['query_var'] && !empty($wp) ) {
151744 if ( true === $args['query_var'] )
151745 $args['query_var'] = $taxonomy;
151746 $args['query_var'] = sanitize_title_with_dashes($args['query_var']);
151747 $wp->add_query_var($args['query_var']);
151748 }
151749
151750 if ( false !== $args['rewrite'] && !empty($wp_rewrite) ) {
151751 if ( !is_array($args['rewrite']) )
151752 $args['rewrite'] = array();
151753 if ( !isset($args['rewrite']['slug']) )
151754 $args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy);
151755 $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=$term");
151756 $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%");
151757 }
151758
151759 $args['name'] = $taxonomy;
151760 $args['object_type'] = $object_type;
151761 $wp_taxonomies[$taxonomy] = (object) $args;
151762 }
151763
151764 //
151765 // Term API
151766 //
151767
151768 /**
151769 * Retrieve object_ids of valid taxonomy and term.
151770 *
151771 * The strings of $taxonomies must exist before this function will continue. On
151772 * failure of finding a valid taxonomy, it will return an WP_Error class, kind
151773 * of like Exceptions in PHP 5, except you can't catch them. Even so, you can
151774 * still test for the WP_Error class and get the error message.
151775 *
151776 * The $terms aren't checked the same as $taxonomies, but still need to exist
151777 * for $object_ids to be returned.
151778 *
151779 * It is possible to change the order that object_ids is returned by either
151780 * using PHP sort family functions or using the database by using $args with
151781 * either ASC or DESC array. The value should be in the key named 'order'.
151782 *
151783 * @package WordPress
151784 * @subpackage Taxonomy
151785 * @since 2.3.0
151786 *
151787 * @uses $wpdb
151788 * @uses wp_parse_args() Creates an array from string $args.
151789 *
151790 * @param string|array $terms String of term or array of string values of terms that will be used
151791 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names
151792 * @param array|string $args Change the order of the object_ids, either ASC or DESC
151793 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success
151794 * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
151795 */
151796 function get_objects_in_term( $terms, $taxonomies, $args = array() ) {
151797 global $wpdb;
151798
151799 if ( !is_array( $terms) )
151800 $terms = array($terms);
151801
151802 if ( !is_array($taxonomies) )
151803 $taxonomies = array($taxonomies);
151804
151805 foreach ( (array) $taxonomies as $taxonomy ) {
151806 if ( ! is_taxonomy($taxonomy) )
151807 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
151808 }
151809
151810 $defaults = array('order' => 'ASC');
151811 $args = wp_parse_args( $args, $defaults );
151812 extract($args, EXTR_SKIP);
151813
151814 $order = ( 'desc' == strtolower($order) ) ? 'DESC' : 'ASC';
151815
151816 $terms = array_map('intval', $terms);
151817
151818 $taxonomies = "'" . implode("', '", $taxonomies) . "'";
151819 $terms = "'" . implode("', '", $terms) . "'";
151820
151821 $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($terms) ORDER BY tr.object_id $order");
151822
151823 if ( ! $object_ids )
151824 return array();
151825
151826 return $object_ids;
151827 }
151828
151829 /**
151830 * Get all Term data from database by Term ID.
151831 *
151832 * The usage of the get_term function is to apply filters to a term object. It
151833 * is possible to get a term object from the database before applying the
151834 * filters.
151835 *
151836 * $term ID must be part of $taxonomy, to get from the database. Failure, might
151837 * be able to be captured by the hooks. Failure would be the same value as $wpdb
151838 * returns for the get_row method.
151839 *
151840 * There are two hooks, one is specifically for each term, named 'get_term', and
151841 * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
151842 * term object, and the taxonomy name as parameters. Both hooks are expected to
151843 * return a Term object.
151844 *
151845 * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.
151846 * Must return term object. Used in get_term() as a catch-all filter for every
151847 * $term.
151848 *
151849 * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
151850 * name. Must return term object. $taxonomy will be the taxonomy name, so for
151851 * example, if 'category', it would be 'get_category' as the filter name. Useful
151852 * for custom taxonomies or plugging into default taxonomies.
151853 *
151854 * @package WordPress
151855 * @subpackage Taxonomy
151856 * @since 2.3.0
151857 *
151858 * @uses $wpdb
151859 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
151860 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
151861 *
151862 * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
151863 * @param string $taxonomy Taxonomy name that $term is part of.
151864 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
151865 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
151866 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
151867 * exist then WP_Error will be returned.
151868 */
151869 function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
151870 global $wpdb;
151871
151872 if ( empty($term) ) {
151873 $error = new WP_Error('invalid_term', __('Empty Term'));
151874 return $error;
151875 }
151876
151877 if ( ! is_taxonomy($taxonomy) ) {
151878 $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
151879 return $error;
151880 }
151881
151882 if ( is_object($term) && empty($term->filter) ) {
151883 wp_cache_add($term->term_id, $term, $taxonomy);
151884 $_term = $term;
151885 } else {
151886 if ( is_object($term) )
151887 $term = $term->term_id;
151888 $term = (int) $term;
151889 if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
151890 $_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 = %s LIMIT 1", $taxonomy, $term) );
151891 wp_cache_add($term, $_term, $taxonomy);
151892 }
151893 }
151894
151895 $_term = apply_filters('get_term', $_term, $taxonomy);
151896 $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
151897 $_term = sanitize_term($_term, $taxonomy, $filter);
151898
151899 if ( $output == OBJECT ) {
151900 return $_term;
151901 } elseif ( $output == ARRAY_A ) {
151902 $__term = get_object_vars($_term);
151903 return $__term;
151904 } elseif ( $output == ARRAY_N ) {
151905 $__term = array_values(get_object_vars($_term));
151906 return $__term;
151907 } else {
151908 return $_term;
151909 }
151910 }
151911
151912 /**
151913 * Get all Term data from database by Term field and data.
151914 *
151915 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
151916 * required.
151917 *
151918 * The default $field is 'id', therefore it is possible to also use null for
151919 * field, but not recommended that you do so.
151920 *
151921 * If $value does not exist, the return value will be false. If $taxonomy exists
151922 * and $field and $value combinations exist, the Term will be returned.
151923 *
151924 * @package WordPress
151925 * @subpackage Taxonomy
151926 * @since 2.3.0
151927 *
151928 * @uses $wpdb
151929 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
151930 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
151931 *
151932 * @param string $field Either 'slug', 'name', or 'id'
151933 * @param string|int $value Search for this term value
151934 * @param string $taxonomy Taxonomy Name
151935 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
151936 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
151937 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
151938 */
151939 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
151940 global $wpdb;
151941
151942 if ( ! is_taxonomy($taxonomy) )
151943 return false;
151944
151945 if ( 'slug' == $field ) {
151946 $field = 't.slug';
151947 $value = sanitize_title($value);
151948 if ( empty($value) )
151949 return false;
151950 } else if ( 'name' == $field ) {
151951 // Assume already escaped
151952 $value = stripslashes($value);
151953 $field = 't.name';
151954 } else {
151955 $field = 't.term_id';
151956 $value = (int) $value;
151957 }
151958
151959 $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) );
151960 if ( !$term )
151961 return false;
151962
151963 wp_cache_add($term->term_id, $term, $taxonomy);
151964
151965 $term = sanitize_term($term, $taxonomy, $filter);
151966
151967 if ( $output == OBJECT ) {
151968 return $term;
151969 } elseif ( $output == ARRAY_A ) {
151970 return get_object_vars($term);
151971 } elseif ( $output == ARRAY_N ) {
151972 return array_values(get_object_vars($term));
151973 } else {
151974 return $term;
151975 }
151976 }
151977
151978 /**
151979 * Merge all term children into a single array.
151980 *
151981 * This recursive function will merge all of the children of $term into the same
151982 * array. Only useful for taxonomies which are hierarchical.
151983 *
151984 * Will return an empty array if $term does not exist in $taxonomy.
151985 *
151986 * @package WordPress
151987 * @subpackage Taxonomy
151988 * @since 2.3.0
151989 *
151990 * @uses $wpdb
151991 * @uses _get_term_hierarchy()
151992 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
151993 *
151994 * @param string $term Name of Term to get children
151995 * @param string $taxonomy Taxonomy Name
151996 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
151997 */
151998 function get_term_children( $term, $taxonomy ) {
151999 if ( ! is_taxonomy($taxonomy) )
152000 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
152001
152002 $terms = _get_term_hierarchy($taxonomy);
152003
152004 if ( ! isset($terms[$term]) )
152005 return array();
152006
152007 $children = $terms[$term];
152008
152009 foreach ( (array) $terms[$term] as $child ) {
152010 if ( isset($terms[$child]) )
152011 $children = array_merge($children, get_term_children($child, $taxonomy));
152012 }
152013
152014 return $children;
152015 }
152016
152017 /**
152018 * Get sanitized Term field.
152019 *
152020 * Does checks for $term, based on the $taxonomy. The function is for contextual
152021 * reasons and for simplicity of usage. See sanitize_term_field() for more
152022 * information.
152023 *
152024 * @package WordPress
152025 * @subpackage Taxonomy
152026 * @since 2.3.0
152027 *
152028 * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.
152029 *
152030 * @param string $field Term field to fetch
152031 * @param int $term Term ID
152032 * @param string $taxonomy Taxonomy Name
152033 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
152034 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
152035 */
152036 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
152037 $term = (int) $term;
152038 $term = get_term( $term, $taxonomy );
152039 if ( is_wp_error($term) )
152040 return $term;
152041
152042 if ( !is_object($term) )
152043 return '';
152044
152045 if ( !isset($term->$field) )
152046 return '';
152047
152048 return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
152049 }
152050
152051 /**
152052 * Sanitizes Term for editing.
152053 *
152054 * Return value is sanitize_term() and usage is for sanitizing the term for
152055 * editing. Function is for contextual and simplicity.
152056 *
152057 * @package WordPress
152058 * @subpackage Taxonomy
152059 * @since 2.3.0
152060 *
152061 * @uses sanitize_term() Passes the return value on success
152062 *
152063 * @param int|object $id Term ID or Object
152064 * @param string $taxonomy Taxonomy Name
152065 * @return mixed|null|WP_Error Will return empty string if $term is not an object.
152066 */
152067 function get_term_to_edit( $id, $taxonomy ) {
152068 $term = get_term( $id, $taxonomy );
152069
152070 if ( is_wp_error($term) )
152071 return $term;
152072
152073 if ( !is_object($term) )
152074 return '';
152075
152076 return sanitize_term($term, $taxonomy, 'edit');
152077 }
152078
152079 /**
152080 * Retrieve the terms in a given taxonomy or list of taxonomies.
152081 *
152082 * You can fully inject any customizations to the query before it is sent, as
152083 * well as control the output with a filter.
152084 *
152085 * The 'get_terms' filter will be called when the cache has the term and will
152086 * pass the found term along with the array of $taxonomies and array of $args.
152087 * This filter is also called before the array of terms is passed and will pass
152088 * the array of terms, along with the $taxonomies and $args.
152089 *
152090 * The 'list_terms_exclusions' filter passes the compiled exclusions along with
152091 * the $args.
152092 *
152093 * The list of arguments that $args can contain, which will overwrite the defaults:
152094 *
152095 * orderby - Default is 'name'. Can be name, count, or nothing (will use
152096 * term_id).
152097 *
152098 * order - Default is ASC. Can use DESC.
152099 *
152100 * hide_empty - Default is true. Will not return empty terms, which means
152101 * terms whose count is 0 according to the given taxonomy.
152102 *
152103 * exclude - Default is an empty string. A comma- or space-delimited string
152104 * of term ids to exclude from the return array. If 'include' is non-empty,
152105 * 'exclude' is ignored.
152106 *
152107 * include - Default is an empty string. A comma- or space-delimited string
152108 * of term ids to include in the return array.
152109 *
152110 * number - The maximum number of terms to return. Default is empty.
152111 *
152112 * offset - The number by which to offset the terms query.
152113 *
152114 * fields - Default is 'all', which returns an array of term objects.
152115 * If 'fields' is 'ids' or 'names', returns an array of
152116 * integers or strings, respectively.
152117 *
152118 * slug - Returns terms whose "slug" matches this value. Default is empty string.
152119 *
152120 * hierarchical - Whether to include terms that have non-empty descendants
152121 * (even if 'hide_empty' is set to true).
152122 *
152123 * search - Returned terms' names will contain the value of 'search',
152124 * case-insensitive. Default is an empty string.
152125 *
152126 * name__like - Returned terms' names will begin with the value of 'name__like',
152127 * case-insensitive. Default is empty string.
152128 *
152129 * The argument 'pad_counts', if set to true will include the quantity of a term's
152130 * children in the quantity of each term's "count" object variable.
152131 *
152132 * The 'get' argument, if set to 'all' instead of its default empty string,
152133 * returns terms regardless of ancestry or whether the terms are empty.
152134 *
152135 * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default
152136 * is 0. If set to a non-zero value, all returned terms will be descendants
152137 * of that term according to the given taxonomy. Hence 'child_of' is set to 0
152138 * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies
152139 * make term ancestry ambiguous.
152140 *
152141 * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is
152142 * the empty string '', which has a different meaning from the integer 0.
152143 * If set to an integer value, all returned terms will have as an immediate
152144 * ancestor the term whose ID is specified by that integer according to the given taxonomy.
152145 * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent'
152146 * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
152147 *
152148 * @package WordPress
152149 * @subpackage Taxonomy
152150 * @since 2.3.0
152151 *
152152 * @uses $wpdb
152153 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
152154 *
152155 * @param string|array Taxonomy name or list of Taxonomy names
152156 * @param string|array $args The values of what to search for when returning terms
152157 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
152158 */
152159 function &get_terms($taxonomies, $args = '') {
152160 global $wpdb;
152161 $empty_array = array();
152162
152163 $single_taxonomy = false;
152164 if ( !is_array($taxonomies) ) {
152165 $single_taxonomy = true;
152166 $taxonomies = array($taxonomies);
152167 }
152168
152169 foreach ( (array) $taxonomies as $taxonomy ) {
152170 if ( ! is_taxonomy($taxonomy) )
152171 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
152172 }
152173
152174 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
152175
152176 $defaults = array('orderby' => 'name', 'order' => 'ASC',
152177 'hide_empty' => true, 'exclude' => '', 'include' => '',
152178 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
152179 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
152180 'pad_counts' => false, 'offset' => '', 'search' => '');
152181 $args = wp_parse_args( $args, $defaults );
152182 $args['number'] = absint( $args['number'] );
152183 $args['offset'] = absint( $args['offset'] );
152184 if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||
152185 '' != $args['parent'] ) {
152186 $args['child_of'] = 0;
152187 $args['hierarchical'] = false;
152188 $args['pad_counts'] = false;
152189 }
152190
152191 if ( 'all' == $args['get'] ) {
152192 $args['child_of'] = 0;
152193 $args['hide_empty'] = 0;
152194 $args['hierarchical'] = false;
152195 $args['pad_counts'] = false;
152196 }
152197 extract($args, EXTR_SKIP);
152198
152199 if ( $child_of ) {
152200 $hierarchy = _get_term_hierarchy($taxonomies[0]);
152201 if ( !isset($hierarchy[$child_of]) )
152202 return $empty_array;
152203 }
152204
152205 if ( $parent ) {
152206 $hierarchy = _get_term_hierarchy($taxonomies[0]);
152207 if ( !isset($hierarchy[$parent]) )
152208 return $empty_array;
152209 }
152210
152211 // $args can be whatever, only use the args defined in defaults to compute the key
152212 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
152213 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
152214 $last_changed = wp_cache_get('last_changed', 'terms');
152215 if ( !$last_changed ) {
152216 $last_changed = time();
152217 wp_cache_set('last_changed', $last_changed, 'terms');
152218 }
152219 $cache_key = "get_terms:$key:$last_changed";
152220
152221 if ( $cache = wp_cache_get( $cache_key, 'terms' ) ) {
152222 $terms = apply_filters('get_terms', $cache, $taxonomies, $args);
152223 return $terms;
152224 }
152225
152226 if ( 'count' == $orderby )
152227 $orderby = 'tt.count';
152228 else if ( 'name' == $orderby )
152229 $orderby = 't.name';
152230 else if ( 'slug' == $orderby )
152231 $orderby = 't.slug';
152232 else if ( 'term_group' == $orderby )
152233 $orderby = 't.term_group';
152234 else
152235 $orderby = 't.term_id';
152236
152237 $where = '';
152238 $inclusions = '';
152239 if ( !empty($include) ) {
152240 $exclude = '';
152241 $interms = preg_split('/[\s,]+/',$include);
152242 if ( count($interms) ) {
152243 foreach ( (array) $interms as $interm ) {
152244 if (empty($inclusions))
152245 $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
152246 else
152247 $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
152248 }
152249 }
152250 }
152251
152252 if ( !empty($inclusions) )
152253 $inclusions .= ')';
152254 $where .= $inclusions;
152255
152256 $exclusions = '';
152257 if ( !empty($exclude) ) {
152258 $exterms = preg_split('/[\s,]+/',$exclude);
152259 if ( count($exterms) ) {
152260 foreach ( (array) $exterms as $exterm ) {
152261 if (empty($exclusions))
152262 $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
152263 else
152264 $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
152265 }
152266 }
152267 }
152268
152269 if ( !empty($exclusions) )
152270 $exclusions .= ')';
152271 $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
152272 $where .= $exclusions;
152273
152274 if ( !empty($slug) ) {
152275 $slug = sanitize_title($slug);
152276 $where .= " AND t.slug = '$slug'";
152277 }
152278
152279 if ( !empty($name__like) )
152280 $where .= " AND t.name LIKE '{$name__like}%'";
152281
152282 if ( '' != $parent ) {
152283 $parent = (int) $parent;
152284 $where .= " AND tt.parent = '$parent'";
152285 }
152286
152287 if ( $hide_empty && !$hierarchical )
152288 $where .= ' AND tt.count > 0';
152289
152290 if ( !empty($number) ) {
152291 if( $offset )
152292 $number = 'LIMIT ' . $offset . ',' . $number;
152293 else
152294 $number = 'LIMIT ' . $number;
152295
152296 } else
152297 $number = '';
152298
152299 if ( !empty($search) ) {
152300 $search = like_escape($search);
152301 $where .= " AND (t.name LIKE '%$search%')";
152302 }
152303
152304 $select_this = '';
152305 if ( 'all' == $fields )
152306 $select_this = 't.*, tt.*';
152307 else if ( 'ids' == $fields )
152308 $select_this = 't.term_id, tt.parent, tt.count';
152309 else if ( 'names' == $fields )
152310 $select_this = 't.term_id, tt.parent, tt.count, t.name';
152311
152312 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number";
152313
152314 if ( 'all' == $fields ) {
152315 $terms = $wpdb->get_results($query);
152316 update_term_cache($terms);
152317 } else if ( ('ids' == $fields) || ('names' == $fields) ) {
152318 $terms = $wpdb->get_results($query);
152319 }
152320
152321 if ( empty($terms) ) {
152322 $cache[ $key ] = array();
152323 wp_cache_set( 'get_terms', $cache, 'terms' );
152324 $terms = apply_filters('get_terms', array(), $taxonomies, $args);
152325 return $terms;
152326 }
152327
152328 if ( $child_of ) {
152329 $children = _get_term_hierarchy($taxonomies[0]);
152330 if ( ! empty($children) )
152331 $terms = & _get_term_children($child_of, $terms, $taxonomies[0]);
152332 }
152333
152334 // Update term counts to include children.
152335 if ( $pad_counts && 'all' == $fields )
152336 _pad_term_counts($terms, $taxonomies[0]);
152337
152338 // Make sure we show empty categories that have children.
152339 if ( $hierarchical && $hide_empty && is_array($terms) ) {
152340 foreach ( $terms as $k => $term ) {
152341 if ( ! $term->count ) {
152342 $children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
152343 if( is_array($children) )
152344 foreach ( $children as $child )
152345 if ( $child->count )
152346 continue 2;
152347
152348 // It really is empty
152349 unset($terms[$k]);
152350 }
152351 }
152352 }
152353 reset ( $terms );
152354
152355 $_terms = array();
152356 if ( 'ids' == $fields ) {
152357 while ( $term = array_shift($terms) )
152358 $_terms[] = $term->term_id;
152359 $terms = $_terms;
152360 } elseif ( 'names' == $fields ) {
152361 while ( $term = array_shift($terms) )
152362 $_terms[] = $term->name;
152363 $terms = $_terms;
152364 }
152365
152366 wp_cache_add( $cache_key, $terms, 'terms' );
152367
152368 $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
152369 return $terms;
152370 }
152371
152372 /**
152373 * Check if Term exists.
152374 *
152375 * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
152376 *
152377 * @package WordPress
152378 * @subpackage Taxonomy
152379 * @since 2.3.0
152380 *
152381 * @uses $wpdb
152382 *
152383 * @param int|string $term The term to check
152384 * @param string $taxonomy The taxonomy name to use
152385 * @return mixed Get the term id or Term Object, if exists.
152386 */
152387 function is_term($term, $taxonomy = '') {
152388 global $wpdb;
152389
152390 $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
152391 $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";
152392
152393 if ( is_int($term) ) {
152394 if ( 0 == $term )
152395 return 0;
152396 $where = 't.term_id = %d';
152397 if ( !empty($taxonomy) )
152398 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
152399 else
152400 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
152401 }
152402
152403 if ( '' === $slug = sanitize_title($term) )
152404 return 0;
152405
152406 $where = 't.slug = %s';
152407 $else_where = 't.name = %s';
152408
152409 if ( !empty($taxonomy) ) {
152410 if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $slug, $taxonomy), ARRAY_A) )
152411 return $result;
152412
152413 return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
152414 }
152415
152416 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $slug) ) )
152417 return $result;
152418
152419 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $term) );
152420 }
152421
152422 /**
152423 * Sanitize Term all fields.
152424 *
152425 * Relys on sanitize_term_field() to sanitize the term. The difference is that
152426 * this function will sanitize <strong>all</strong> fields. The context is based
152427 * on sanitize_term_field().
152428 *
152429 * The $term is expected to be either an array or an object.
152430 *
152431 * @package WordPress
152432 * @subpackage Taxonomy
152433 * @since 2.3.0
152434 *
152435 * @uses sanitize_term_field Used to sanitize all fields in a term
152436 *
152437 * @param array|object $term The term to check
152438 * @param string $taxonomy The taxonomy name to use
152439 * @param string $context Default is 'display'.
152440 * @return array|object Term with all fields sanitized
152441 */
152442 function sanitize_term($term, $taxonomy, $context = 'display') {
152443
152444 if ( 'raw' == $context )
152445 return $term;
152446
152447 $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
152448
152449 $do_object = false;
152450 if ( is_object($term) )
152451 $do_object = true;
152452
152453 $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
152454
152455 foreach ( (array) $fields as $field ) {
152456 if ( $do_object ) {
152457 if ( isset($term->$field) )
152458 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
152459 } else {
152460 if ( isset($term[$field]) )
152461 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
152462 }
152463 }
152464
152465 if ( $do_object )
152466 $term->filter = $context;
152467 else
152468 $term['filter'] = $context;
152469
152470 return $term;
152471 }
152472
152473 /**
152474 * Cleanse the field value in the term based on the context.
152475 *
152476 * Passing a term field value through the function should be assumed to have
152477 * cleansed the value for whatever context the term field is going to be used.
152478 *
152479 * If no context or an unsupported context is given, then default filters will
152480 * be applied.
152481 *
152482 * There are enough filters for each context to support a custom filtering
152483 * without creating your own filter function. Simply create a function that
152484 * hooks into the filter you need.
152485 *
152486 * @package WordPress
152487 * @subpackage Taxonomy
152488 * @since 2.3.0
152489 *
152490 * @uses $wpdb
152491 *
152492 * @param string $field Term field to sanitize
152493 * @param string $value Search for this term value
152494 * @param int $term_id Term ID
152495 * @param string $taxonomy Taxonomy Name
152496 * @param string $context Either edit, db, display, attribute, or js.
152497 * @return mixed sanitized field
152498 */
152499 function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
152500 if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {
152501 $value = (int) $value;
152502 if ( $value < 0 )
152503 $value = 0;
152504 }
152505
152506 if ( 'raw' == $context )
152507 return $value;
152508
152509 if ( 'edit' == $context ) {
152510 $value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy);
152511 $value = apply_filters("edit_${taxonomy}_$field", $value, $term_id);
152512 if ( 'description' == $field )
152513 $value = format_to_edit($value);
152514 else
152515 $value = attribute_escape($value);
152516 } else if ( 'db' == $context ) {
152517 $value = apply_filters("pre_term_$field", $value, $taxonomy);
152518 $value = apply_filters("pre_${taxonomy}_$field", $value);
152519 // Back compat filters
152520 if ( 'slug' == $field )
152521 $value = apply_filters('pre_category_nicename', $value);
152522
152523 } else if ( 'rss' == $context ) {
152524 $value = apply_filters("term_${field}_rss", $value, $taxonomy);
152525 $value = apply_filters("${taxonomy}_${field}_rss", $value);
152526 } else {
152527 // Use display filters by default.
152528 $value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context);
152529 $value = apply_filters("${taxonomy}_$field", $value, $term_id, $context);
152530 }
152531
152532 if ( 'attribute' == $context )
152533 $value = attribute_escape($value);
152534 else if ( 'js' == $context )
152535 $value = js_escape($value);
152536
152537 return $value;
152538 }
152539
152540 /**
152541 * Count how many terms are in Taxonomy.
152542 *
152543 * Default $args is 'ignore_empty' which can be <code>'ignore_empty=true'</code>
152544 * or <code>array('ignore_empty' => true);</code>.
152545 *
152546 * @package WordPress
152547 * @subpackage Taxonomy
152548 * @since 2.3.0
152549 *
152550 * @uses $wpdb
152551 * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
152552 *
152553 * @param string $taxonomy Taxonomy name
152554 * @param array|string $args Overwrite defaults
152555 * @return int How many terms are in $taxonomy
152556 */
152557 function wp_count_terms( $taxonomy, $args = array() ) {
152558 global $wpdb;
152559
152560 $defaults = array('ignore_empty' => false);
152561 $args = wp_parse_args($args, $defaults);
152562 extract($args, EXTR_SKIP);
152563
152564 $where = '';
152565 if ( $ignore_empty )
152566 $where = 'AND count > 0';
152567
152568 return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = %s $where", $taxonomy) );
152569 }
152570
152571 /**
152572 * Will unlink the term from the taxonomy.
152573 *
152574 * Will remove the term's relationship to the taxonomy, not the term or taxonomy
152575 * itself. The term and taxonomy will still exist. Will require the term's
152576 * object ID to perform the operation.
152577 *
152578 * @package WordPress
152579 * @subpackage Taxonomy
152580 * @since 2.3.0
152581 * @uses $wpdb
152582 *
152583 * @param int $object_id The term Object Id that refers to the term
152584 * @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name.
152585 */
152586 function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
152587 global $wpdb;
152588
152589 $object_id = (int) $object_id;
152590
152591 if ( !is_array($taxonomies) )
152592 $taxonomies = array($taxonomies);
152593
152594 foreach ( (array) $taxonomies as $taxonomy ) {
152595 $tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
152596 $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
152597 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
152598 wp_update_term_count($tt_ids, $taxonomy);
152599 }
152600 }
152601
152602 /**
152603 * Removes a term from the database.
152604 *
152605 * If the term is a parent of other terms, then the children will be updated to
152606 * that term's parent.
152607 *
152608 * The $args 'default' will only override the terms found, if there is only one
152609 * term found. Any other and the found terms are used.
152610 *
152611 * @package WordPress
152612 * @subpackage Taxonomy
152613 * @since 2.3.0
152614 *
152615 * @uses $wpdb
152616 * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action
152617 * hooks, passing term object, term id. 'delete_term' gets an additional
152618 * parameter with the $taxonomy parameter.
152619 *
152620 * @param int $term Term ID
152621 * @param string $taxonomy Taxonomy Name
152622 * @param array|string $args Optional. Change 'default' term id and override found term ids.
152623 * @return bool|WP_Error Returns false if not term; true if completes delete action.
152624 */
152625 function wp_delete_term( $term, $taxonomy, $args = array() ) {
152626 global $wpdb;
152627
152628 $term = (int) $term;
152629
152630 if ( ! $ids = is_term($term, $taxonomy) )
152631 return false;
152632 if ( is_wp_error( $ids ) )
152633 return $ids;
152634
152635 $tt_id = $ids['term_taxonomy_id'];
152636
152637 $defaults = array();
152638 $args = wp_parse_args($args, $defaults);
152639 extract($args, EXTR_SKIP);
152640
152641 if ( isset($default) ) {
152642 $default = (int) $default;
152643 if ( ! is_term($default, $taxonomy) )
152644 unset($default);
152645 }
152646
152647 // Update children to point to new parent
152648 if ( is_taxonomy_hierarchical($taxonomy) ) {
152649 $term_obj = get_term($term, $taxonomy);
152650 if ( is_wp_error( $term_obj ) )
152651 return $term_obj;
152652 $parent = $term_obj->parent;
152653
152654 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
152655 }
152656
152657 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
152658
152659 foreach ( (array) $objects as $object ) {
152660 $terms = wp_get_object_terms($object, $taxonomy, 'fields=ids');
152661 if ( 1 == count($terms) && isset($default) )
152662 $terms = array($default);
152663 else
152664 $terms = array_diff($terms, array($term));
152665 $terms = array_map('intval', $terms);
152666 wp_set_object_terms($object, $terms, $taxonomy);
152667 }
152668
152669 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
152670
152671 // Delete the term if no taxonomies use it.
152672 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
152673 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
152674
152675 clean_term_cache($term, $taxonomy);
152676
152677 do_action('delete_term', $term, $tt_id, $taxonomy);
152678 do_action("delete_$taxonomy", $term, $tt_id);
152679
152680 return true;
152681 }
152682
152683 /**
152684 * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
152685 *
152686 * The following information has to do the $args parameter and for what can be
152687 * contained in the string or array of that parameter, if it exists.
152688 *
152689 * The first argument is called, 'orderby' and has the default value of 'name'.
152690 * The other value that is supported is 'count'.
152691 *
152692 * The second argument is called, 'order' and has the default value of 'ASC'.
152693 * The only other value that will be acceptable is 'DESC'.
152694 *
152695 * The final argument supported is called, 'fields' and has the default value of
152696 * 'all'. There are multiple other options that can be used instead. Supported
152697 * values are as follows: 'all', 'ids', 'names', and finally
152698 * 'all_with_object_id'.
152699 *
152700 * The fields argument also decides what will be returned. If 'all' or
152701 * 'all_with_object_id' is choosen or the default kept intact, then all matching
152702 * terms objects will be returned. If either 'ids' or 'names' is used, then an
152703 * array of all matching term ids or term names will be returned respectively.
152704 *
152705 * @package WordPress
152706 * @subpackage Taxonomy
152707 * @since 2.3.0
152708 * @uses $wpdb
152709 *
152710 * @param int|array $object_id The id of the object(s) to retrieve.
152711 * @param string|array $taxonomies The taxonomies to retrieve terms from.
152712 * @param array|string $args Change what is returned
152713 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.
152714 */
152715 function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
152716 global $wpdb;
152717
152718 if ( !is_array($taxonomies) )
152719 $taxonomies = array($taxonomies);
152720
152721 foreach ( (array) $taxonomies as $taxonomy ) {
152722 if ( ! is_taxonomy($taxonomy) )
152723 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
152724 }
152725
152726 if ( !is_array($object_ids) )
152727 $object_ids = array($object_ids);
152728 $object_ids = array_map('intval', $object_ids);
152729
152730 $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
152731 $args = wp_parse_args( $args, $defaults );
152732
152733 $terms = array();
152734 if ( count($taxonomies) > 1 ) {
152735 foreach ( $taxonomies as $index => $taxonomy ) {
152736 $t = get_taxonomy($taxonomy);
152737 if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
152738 unset($taxonomies[$index]);
152739 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
152740 }
152741 }
152742 } else {
152743 $t = get_taxonomy($taxonomies[0]);
152744 if ( isset($t->args) && is_array($t->args) )
152745 $args = array_merge($args, $t->args);
152746 }
152747
152748 extract($args, EXTR_SKIP);
152749
152750 if ( 'count' == $orderby )
152751 $orderby = 'tt.count';
152752 else if ( 'name' == $orderby )
152753 $orderby = 't.name';
152754 else if ( 'slug' == $orderby )
152755 $orderby = 't.slug';
152756 else if ( 'term_group' == $orderby )
152757 $orderby = 't.term_group';
152758 else if ( 'term_order' == $orderby )
152759 $orderby = 'tr.term_order';
152760 else
152761 $orderby = 't.term_id';
152762
152763 $taxonomies = "'" . implode("', '", $taxonomies) . "'";
152764 $object_ids = implode(', ', $object_ids);
152765
152766 $select_this = '';
152767 if ( 'all' == $fields )
152768 $select_this = 't.*, tt.*';
152769 else if ( 'ids' == $fields )
152770 $select_this = 't.term_id';
152771 else if ( 'names' == $fields )
152772 $select_this = 't.name';
152773 else if ( 'all_with_object_id' == $fields )
152774 $select_this = 't.*, tt.*, tr.object_id';
152775
152776 $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) ORDER BY $orderby $order";
152777
152778 if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
152779 $terms = array_merge($terms, $wpdb->get_results($query));
152780 update_term_cache($terms);
152781 } else if ( 'ids' == $fields || 'names' == $fields ) {
152782 $terms = array_merge($terms, $wpdb->get_col($query));
152783 } else if ( 'tt_ids' == $fields ) {
152784 $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) ORDER BY tr.term_taxonomy_id $order");
152785 }
152786
152787 if ( ! $terms )
152788 return array();
152789
152790 return $terms;
152791 }
152792
152793 /**
152794 * Adds a new term to the database. Optionally marks it as an alias of an existing term.
152795 *
152796 * Error handling is assigned for the nonexistance of the $taxonomy and $term
152797 * parameters before inserting. If both the term id and taxonomy exist
152798 * previously, then an array will be returned that contains the term id and the
152799 * contents of what is returned. The keys of the array are 'term_id' and
152800 * 'term_taxonomy_id' containing numeric values.
152801 *
152802 * It is assumed that the term does not yet exist or the above will apply. The
152803 * term will be first added to the term table and then related to the taxonomy
152804 * if everything is well. If everything is correct, then several actions will be
152805 * run prior to a filter and then several actions will be run after the filter
152806 * is run.
152807 *
152808 * The arguments decide how the term is handled based on the $args parameter.
152809 * The following is a list of the available overrides and the defaults.
152810 *
152811 * 'alias_of'. There is no default, but if added, expected is the slug that the
152812 * term will be an alias of. Expected to be a string.
152813 *
152814 * 'description'. There is no default. If exists, will be added to the database
152815 * along with the term. Expected to be a string.
152816 *
152817 * 'parent'. Expected to be numeric and default is 0 (zero). Will assign value
152818 * of 'parent' to the term.
152819 *
152820 * 'slug'. Expected to be a string. There is no default.
152821 *
152822 * If 'slug' argument exists then the slug will be checked to see if it is not
152823 * a valid term. If that check succeeds (it is not a valid term), then it is
152824 * added and the term id is given. If it fails, then a check is made to whether
152825 * the taxonomy is hierarchical and the parent argument is not empty. If the
152826 * second check succeeds, the term will be inserted and the term id will be
152827 * given.
152828 *
152829 * @package WordPress
152830 * @subpackage Taxonomy
152831 * @since 2.3.0
152832 * @uses $wpdb
152833 *
152834 * @uses do_action() Calls 'create_term' hook with the term id and taxonomy id as parameters.
152835 * @uses do_action() Calls 'create_$taxonomy' hook with term id and taxonomy id as parameters.
152836 * @uses apply_filters() Calls 'term_id_filter' hook with term id and taxonomy id as parameters.
152837 * @uses do_action() Calls 'created_term' hook with the term id and taxonomy id as parameters.
152838 * @uses do_action() Calls 'created_$taxonomy' hook with term id and taxonomy id as parameters.
152839 *
152840 * @param int|string $term The term to add or update.
152841 * @param string $taxonomy The taxonomy to which to add the term
152842 * @param array|string $args Change the values of the inserted term
152843 * @return array|WP_Error The Term ID and Term Taxonomy ID
152844 */
152845 function wp_insert_term( $term, $taxonomy, $args = array() ) {
152846 global $wpdb;
152847
152848 if ( ! is_taxonomy($taxonomy) )
152849 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
152850
152851 if ( is_int($term) && 0 == $term )
152852 return new WP_Error('invalid_term_id', __('Invalid term ID'));
152853
152854 if ( '' == trim($term) )
152855 return new WP_Error('empty_term_name', __('A name is required for this term'));
152856
152857 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
152858 $args = wp_parse_args($args, $defaults);
152859 $args['name'] = $term;
152860 $args['taxonomy'] = $taxonomy;
152861 $args = sanitize_term($args, $taxonomy, 'db');
152862 extract($args, EXTR_SKIP);
152863
152864 // expected_slashed ($name)
152865 $name = stripslashes($name);
152866 $description = stripslashes($description);
152867
152868 if ( empty($slug) )
152869 $slug = sanitize_title($name);
152870
152871 $term_group = 0;
152872 if ( $alias_of ) {
152873 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
152874 if ( $alias->term_group ) {
152875 // The alias we want is already in a group, so let's use that one.
152876 $term_group = $alias->term_group;
152877 } else {
152878 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
152879 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
152880 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $alias->term_id ) );
152881 }
152882 }
152883
152884 if ( ! $term_id = is_term($slug) ) {
152885 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
152886 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
152887 $term_id = (int) $wpdb->insert_id;
152888 } else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) {
152889 // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
152890 // by incorporating parent slugs.
152891 $slug = wp_unique_term_slug($slug, (object) $args);
152892 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
152893 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
152894 $term_id = (int) $wpdb->insert_id;
152895 }
152896
152897 if ( empty($slug) ) {
152898 $slug = sanitize_title($slug, $term_id);
152899 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
152900 }
152901
152902 $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 ) );
152903
152904 if ( !empty($tt_id) )
152905 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
152906
152907 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
152908 $tt_id = (int) $wpdb->insert_id;
152909
152910 do_action("create_term", $term_id, $tt_id);
152911 do_action("create_$taxonomy", $term_id, $tt_id);
152912
152913 $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
152914
152915 clean_term_cache($term_id, $taxonomy);
152916
152917 do_action("created_term", $term_id, $tt_id);
152918 do_action("created_$taxonomy", $term_id, $tt_id);
152919
152920 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
152921 }
152922
152923 /**
152924 * Create Term and Taxonomy Relationships.
152925 *
152926 * Relates an object (post, link etc) to a term and taxonomy type. Creates the
152927 * term and taxonomy relationship if it doesn't already exist. Creates a term if
152928 * it doesn't exist (using the slug).
152929 *
152930 * A relationship means that the term is grouped in or belongs to the taxonomy.
152931 * A term has no meaning until it is given context by defining which taxonomy it
152932 * exists under.
152933 *
152934 * @package WordPress
152935 * @subpackage Taxonomy
152936 * @since 2.3.0
152937 * @uses $wpdb
152938 *
152939 * @param int $object_id The object to relate to.
152940 * @param array|int|string $term The slug or id of the term.
152941 * @param array|string $taxonomy The context in which to relate the term to the object.
152942 * @param bool $append If false will delete difference of terms.
152943 * @return array|WP_Error Affected Term IDs
152944 */
152945 function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {
152946 global $wpdb;
152947
152948 $object_id = (int) $object_id;
152949
152950 if ( ! is_taxonomy($taxonomy) )
152951 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
152952
152953 if ( !is_array($terms) )
152954 $terms = array($terms);
152955
152956 if ( ! $append )
152957 $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
152958
152959 $tt_ids = array();
152960 $term_ids = array();
152961
152962 foreach ( (array) $terms as $term) {
152963 if ( !strlen(trim($term)) )
152964 continue;
152965
152966 if ( !$term_info = is_term($term, $taxonomy) )
152967 $term_info = wp_insert_term($term, $taxonomy);
152968 if ( is_wp_error($term_info) )
152969 return $term_info;
152970 $term_ids[] = $term_info['term_id'];
152971 $tt_id = $term_info['term_taxonomy_id'];
152972 $tt_ids[] = $tt_id;
152973
152974 if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )
152975 continue;
152976 $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
152977 }
152978
152979 wp_update_term_count($tt_ids, $taxonomy);
152980
152981 if ( ! $append ) {
152982 $delete_terms = array_diff($old_tt_ids, $tt_ids);
152983 if ( $delete_terms ) {
152984 $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
152985 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
152986 wp_update_term_count($delete_terms, $taxonomy);
152987 }
152988 }
152989
152990 $t = get_taxonomy($taxonomy);
152991 if ( ! $append && isset($t->sort) && $t->sort ) {
152992 $values = array();
152993 $term_order = 0;
152994 $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
152995 foreach ( $tt_ids as $tt_id )
152996 if ( in_array($tt_id, $final_tt_ids) )
152997 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
152998 if ( $values )
152999 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
153000 }
153001
153002 return $tt_ids;
153003 }
153004
153005 /**
153006 * Will make slug unique, if it isn't already.
153007 *
153008 * The $slug has to be unique global to every taxonomy, meaning that one
153009 * taxonomy term can't have a matching slug with another taxonomy term. Each
153010 * slug has to be globally unique for every taxonomy.
153011 *
153012 * The way this works is that if the taxonomy that the term belongs to is
153013 * heirarchical and has a parent, it will append that parent to the $slug.
153014 *
153015 * If that still doesn't return an unique slug, then it try to append a number
153016 * until it finds a number that is truely unique.
153017 *
153018 * The only purpose for $term is for appending a parent, if one exists.
153019 *
153020 * @package WordPress
153021 * @subpackage Taxonomy
153022 * @since 2.3.0
153023 * @uses $wpdb
153024 *
153025 * @param string $slug The string that will be tried for a unique slug
153026 * @param object $term The term object that the $slug will belong too
153027 * @return string Will return a true unique slug.
153028 */
153029 function wp_unique_term_slug($slug, $term) {
153030 global $wpdb;
153031
153032 // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
153033 // by incorporating parent slugs.
153034 if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
153035 $the_parent = $term->parent;
153036 while ( ! empty($the_parent) ) {
153037 $parent_term = get_term($the_parent, $term->taxonomy);
153038 if ( is_wp_error($parent_term) || empty($parent_term) )
153039 break;
153040 $slug .= '-' . $parent_term->slug;
153041 if ( empty($parent_term->parent) )
153042 break;
153043 $the_parent = $parent_term->parent;
153044 }
153045 }
153046
153047 // If we didn't get a unique slug, try appending a number to make it unique.
153048 if ( !empty($args['term_id']) )
153049 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] );
153050 else
153051 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
153052
153053 if ( $wpdb->get_var( $query ) ) {
153054 $num = 2;
153055 do {
153056 $alt_slug = $slug . "-$num";
153057 $num++;
153058 $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
153059 } while ( $slug_check );
153060 $slug = $alt_slug;
153061 }
153062
153063 return $slug;
153064 }
153065
153066 /**
153067 * Update term based on arguments provided.
153068 *
153069 * The $args will indiscriminately override all values with the same field name.
153070 * Care must be taken to not override important information need to update or
153071 * update will fail (or perhaps create a new term, neither would be acceptable).
153072 *
153073 * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
153074 * defined in $args already.
153075 *
153076 * 'alias_of' will create a term group, if it doesn't already exist, and update
153077 * it for the $term.
153078 *
153079 * If the 'slug' argument in $args is missing, then the 'name' in $args will be
153080 * used. It should also be noted that if you set 'slug' and it isn't unique then
153081 * a WP_Error will be passed back. If you don't pass any slug, then a unique one
153082 * will be created for you.
153083 *
153084 * For what can be overrode in $args, check the term scheme can contain and stay
153085 * away from the term keys.
153086 *
153087 * @package WordPress
153088 * @subpackage Taxonomy
153089 * @since 2.3.0
153090 *
153091 * @uses $wpdb
153092 * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice.
153093 * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term
153094 * id and taxonomy id.
153095 *
153096 * @param int $term The ID of the term
153097 * @param string $taxonomy The context in which to relate the term to the object.
153098 * @param array|string $args Overwrite term field values
153099 * @return array|WP_Error Returns Term ID and Taxonomy Term ID
153100 */
153101 function wp_update_term( $term, $taxonomy, $args = array() ) {
153102 global $wpdb;
153103
153104 if ( ! is_taxonomy($taxonomy) )
153105 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
153106
153107 $term_id = (int) $term;
153108
153109 // First, get all of the original args
153110 $term = get_term ($term_id, $taxonomy, ARRAY_A);
153111
153112 // Escape data pulled from DB.
153113 $term = add_magic_quotes($term);
153114
153115 // Merge old and new args with new args overwriting old ones.
153116 $args = array_merge($term, $args);
153117
153118 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
153119 $args = wp_parse_args($args, $defaults);
153120 $args = sanitize_term($args, $taxonomy, 'db');
153121 extract($args, EXTR_SKIP);
153122
153123 // expected_slashed ($name)
153124 $name = stripslashes($name);
153125 $description = stripslashes($description);
153126
153127 if ( '' == trim($name) )
153128 return new WP_Error('empty_term_name', __('A name is required for this term'));
153129
153130 $empty_slug = false;
153131 if ( empty($slug) ) {
153132 $empty_slug = true;
153133 $slug = sanitize_title($name);
153134 }
153135
153136 if ( $alias_of ) {
153137 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
153138 if ( $alias->term_group ) {
153139 // The alias we want is already in a group, so let's use that one.
153140 $term_group = $alias->term_group;
153141 } else {
153142 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
153143 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
153144 $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
153145 }
153146 }
153147
153148 // Check for duplicate slug
153149 $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
153150 if ( $id && ($id != $term_id) ) {
153151 // If an empty slug was passed or the parent changed, reset the slug to something unique.
153152 // Otherwise, bail.
153153 if ( $empty_slug || ( $parent != $term->parent) )
153154 $slug = wp_unique_term_slug($slug, (object) $args);
153155 else
153156 return new WP_Error('duplicate_term_slug', sprintf(__('The slug "%s" is already in use by another term'), $slug));
153157 }
153158
153159 $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
153160
153161 if ( empty($slug) ) {
153162 $slug = sanitize_title($name, $term_id);
153163 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
153164 }
153165
153166 $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) );
153167
153168 $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
153169
153170 do_action("edit_term", $term_id, $tt_id);
153171 do_action("edit_$taxonomy", $term_id, $tt_id);
153172
153173 $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
153174
153175 clean_term_cache($term_id, $taxonomy);
153176
153177 do_action("edited_term", $term_id, $tt_id);
153178 do_action("edited_$taxonomy", $term_id, $tt_id);
153179
153180 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
153181 }
153182
153183 /**
153184 * Enable or disable term counting.
153185 *
153186 * @since 2.5.0
153187 *
153188 * @param bool $defer Optional. Enable if true, disable if false.
153189 * @return bool Whether term counting is enabled or disabled.
153190 */
153191 function wp_defer_term_counting($defer=null) {
153192 static $_defer = false;
153193
153194 if ( is_bool($defer) ) {
153195 $_defer = $defer;
153196 // flush any deferred counts
153197 if ( !$defer )
153198 wp_update_term_count( null, null, true );
153199 }
153200
153201 return $_defer;
153202 }
153203
153204 /**
153205 * Updates the amount of terms in taxonomy.
153206 *
153207 * If there is a taxonomy callback applyed, then it will be called for updating
153208 * the count.
153209 *
153210 * The default action is to count what the amount of terms have the relationship
153211 * of term ID. Once that is done, then update the database.
153212 *
153213 * @package WordPress
153214 * @subpackage Taxonomy
153215 * @since 2.3.0
153216 * @uses $wpdb
153217 *
153218 * @param int|array $terms The term_taxonomy_id of the terms
153219 * @param string $taxonomy The context of the term.
153220 * @return bool If no terms will return false, and if successful will return true.
153221 */
153222 function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) {
153223 static $_deferred = array();
153224
153225 if ( $do_deferred ) {
153226 foreach ( (array) array_keys($_deferred) as $tax ) {
153227 wp_update_term_count_now( $_deferred[$tax], $tax );
153228 unset( $_deferred[$tax] );
153229 }
153230 }
153231
153232 if ( empty($terms) )
153233 return false;
153234
153235 if ( !is_array($terms) )
153236 $terms = array($terms);
153237
153238 if ( wp_defer_term_counting() ) {
153239 if ( !isset($_deferred[$taxonomy]) )
153240 $_deferred[$taxonomy] = array();
153241 $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
153242 return true;
153243 }
153244
153245 return wp_update_term_count_now( $terms, $taxonomy );
153246 }
153247
153248 /**
153249 * Perform term count update immediately.
153250 *
153251 * @since 2.5.0
153252 *
153253 * @param array $terms The term_taxonomy_id of terms to update.
153254 * @param string $taxonomy The context of the term.
153255 * @return bool Always true when complete.
153256 */
153257 function wp_update_term_count_now( $terms, $taxonomy ) {
153258 global $wpdb;
153259
153260 $terms = array_map('intval', $terms);
153261
153262 $taxonomy = get_taxonomy($taxonomy);
153263 if ( !empty($taxonomy->update_count_callback) ) {
153264 call_user_func($taxonomy->update_count_callback, $terms);
153265 } else {
153266 // Default count updater
153267 foreach ( (array) $terms as $term) {
153268 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term) );
153269 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
153270 }
153271
153272 }
153273
153274 clean_term_cache($terms);
153275
153276 return true;
153277 }
153278
153279 //
153280 // Cache
153281 //
153282
153283
153284 /**
153285 * Removes the taxonomy relationship to terms from the cache.
153286 *
153287 * Will remove the entire taxonomy relationship containing term $object_id. The
153288 * term IDs have to exist within the taxonomy $object_type for the deletion to
153289 * take place.
153290 *
153291 * @package WordPress
153292 * @subpackage Taxonomy
153293 * @since 2.3.0
153294 *
153295 * @see get_object_taxonomies() for more on $object_type
153296 * @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion.
153297 * Passes, function params in same order.
153298 *
153299 * @param int|array $object_ids Single or list of term object ID(s)
153300 * @param array|string $object_type The taxonomy object type
153301 */
153302 function clean_object_term_cache($object_ids, $object_type) {
153303 if ( !is_array($object_ids) )
153304 $object_ids = array($object_ids);
153305
153306 foreach ( $object_ids as $id )
153307 foreach ( get_object_taxonomies($object_type) as $taxonomy )
153308 wp_cache_delete($id, "{$taxonomy}_relationships");
153309
153310 do_action('clean_object_term_cache', $object_ids, $object_type);
153311 }
153312
153313
153314 /**
153315 * Will remove all of the term ids from the cache.
153316 *
153317 * @package WordPress
153318 * @subpackage Taxonomy
153319 * @since 2.3.0
153320 * @uses $wpdb
153321 *
153322 * @param int|array $ids Single or list of Term IDs
153323 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
153324 */
153325 function clean_term_cache($ids, $taxonomy = '') {
153326 global $wpdb;
153327 static $cleaned = array();
153328
153329 if ( !is_array($ids) )
153330 $ids = array($ids);
153331
153332 $taxonomies = array();
153333 // If no taxonomy, assume tt_ids.
153334 if ( empty($taxonomy) ) {
153335 $tt_ids = implode(', ', $ids);
153336 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
153337 foreach ( (array) $terms as $term ) {
153338 $taxonomies[] = $term->taxonomy;
153339 wp_cache_delete($term->term_id, $term->taxonomy);
153340 }
153341 $taxonomies = array_unique($taxonomies);
153342 } else {
153343 foreach ( $ids as $id ) {
153344 wp_cache_delete($id, $taxonomy);
153345 }
153346 $taxonomies = array($taxonomy);
153347 }
153348
153349 foreach ( $taxonomies as $taxonomy ) {
153350 if ( isset($cleaned[$taxonomy]) )
153351 continue;
153352 $cleaned[$taxonomy] = true;
153353 wp_cache_delete('all_ids', $taxonomy);
153354 wp_cache_delete('get', $taxonomy);
153355 delete_option("{$taxonomy}_children");
153356 }
153357
153358 wp_cache_set('last_changed', time(), 'terms');
153359
153360 do_action('clean_term_cache', $ids, $taxonomy);
153361 }
153362
153363
153364 /**
153365 * Retrieves the taxonomy relationship to the term object id.
153366 *
153367 * @package WordPress
153368 * @subpackage Taxonomy
153369 * @since 2.3.0
153370 *
153371 * @uses wp_cache_get() Retrieves taxonomy relationship from cache
153372 *
153373 * @param int|array $id Term object ID
153374 * @param string $taxonomy Taxonomy Name
153375 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
153376 */
153377 function &get_object_term_cache($id, $taxonomy) {
153378 $cache = wp_cache_get($id, "{$taxonomy}_relationships");
153379 return $cache;
153380 }
153381
153382
153383 /**
153384 * Updates the cache for Term ID(s).
153385 *
153386 * Will only update the cache for terms not already cached.
153387 *
153388 * The $object_ids expects that the ids be separated by commas, if it is a
153389 * string.
153390 *
153391 * It should be noted that update_object_term_cache() is very time extensive. It
153392 * is advised that the function is not called very often or at least not for a
153393 * lot of terms that exist in a lot of taxonomies. The amount of time increases
153394 * for each term and it also increases for each taxonomy the term belongs to.
153395 *
153396 * @package WordPress
153397 * @subpackage Taxonomy
153398 * @since 2.3.0
153399 * @uses wp_get_object_terms() Used to get terms from the database to update
153400 *
153401 * @param string|array $object_ids Single or list of term object ID(s)
153402 * @param array|string $object_type The taxonomy object type
153403 * @return null|bool Null value is given with empty $object_ids. False if
153404 */
153405 function update_object_term_cache($object_ids, $object_type) {
153406 if ( empty($object_ids) )
153407 return;
153408
153409 if ( !is_array($object_ids) )
153410 $object_ids = explode(',', $object_ids);
153411
153412 $object_ids = array_map('intval', $object_ids);
153413
153414 $taxonomies = get_object_taxonomies($object_type);
153415
153416 $ids = array();
153417 foreach ( (array) $object_ids as $id ) {
153418 foreach ( $taxonomies as $taxonomy ) {
153419 if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
153420 $ids[] = $id;
153421 break;
153422 }
153423 }
153424 }
153425
153426 if ( empty( $ids ) )
153427 return false;
153428
153429 $terms = wp_get_object_terms($ids, $taxonomies, 'fields=all_with_object_id');
153430
153431 $object_terms = array();
153432 foreach ( (array) $terms as $term )
153433 $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;
153434
153435 foreach ( $ids as $id ) {
153436 foreach ( $taxonomies as $taxonomy ) {
153437 if ( ! isset($object_terms[$id][$taxonomy]) ) {
153438 if ( !isset($object_terms[$id]) )
153439 $object_terms[$id] = array();
153440 $object_terms[$id][$taxonomy] = array();
153441 }
153442 }
153443 }
153444
153445 foreach ( $object_terms as $id => $value ) {
153446 foreach ( $value as $taxonomy => $terms ) {
153447 wp_cache_set($id, $terms, "{$taxonomy}_relationships");
153448 }
153449 }
153450 }
153451
153452
153453 /**
153454 * Updates Terms to Taxonomy in cache.
153455 *
153456 * @package WordPress
153457 * @subpackage Taxonomy
153458 * @since 2.3.0
153459 *
153460 * @param array $terms List of Term objects to change
153461 * @param string $taxonomy Optional. Update Term to this taxonomy in cache
153462 */
153463 function update_term_cache($terms, $taxonomy = '') {
153464 foreach ( (array) $terms as $term ) {
153465 $term_taxonomy = $taxonomy;
153466 if ( empty($term_taxonomy) )
153467 $term_taxonomy = $term->taxonomy;
153468
153469 wp_cache_add($term->term_id, $term, $term_taxonomy);
153470 }
153471 }
153472
153473 //
153474 // Private
153475 //
153476
153477
153478 /**
153479 * Retrieves children of taxonomy.
153480 *
153481 * @package WordPress
153482 * @subpackage Taxonomy
153483 * @access private
153484 * @since 2.3.0
153485 *
153486 * @uses update_option() Stores all of the children in "$taxonomy_children"
153487 * option. That is the name of the taxonomy, immediately followed by '_children'.
153488 *
153489 * @param string $taxonomy Taxonomy Name
153490 * @return array Empty if $taxonomy isn't hierarachical or returns children.
153491 */
153492 function _get_term_hierarchy($taxonomy) {
153493 if ( !is_taxonomy_hierarchical($taxonomy) )
153494 return array();
153495 $children = get_option("{$taxonomy}_children");
153496 if ( is_array($children) )
153497 return $children;
153498
153499 $children = array();
153500 $terms = get_terms($taxonomy, 'get=all');
153501 foreach ( $terms as $term ) {
153502 if ( $term->parent > 0 )
153503 $children[$term->parent][] = $term->term_id;
153504 }
153505 update_option("{$taxonomy}_children", $children);
153506
153507 return $children;
153508 }
153509
153510
153511 /**
153512 * Get the subset of $terms that are descendants of $term_id.
153513 *
153514 * If $terms is an array of objects, then _get_term_children returns an array of objects.
153515 * If $terms is an array of IDs, then _get_term_children returns an array of IDs.
153516 *
153517 * @package WordPress
153518 * @subpackage Taxonomy
153519 * @access private
153520 * @since 2.3.0
153521 *
153522 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id.
153523 * @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.
153524 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
153525 * @return array The subset of $terms that are descendants of $term_id.
153526 */
153527 function &_get_term_children($term_id, $terms, $taxonomy) {
153528 $empty_array = array();
153529 if ( empty($terms) )
153530 return $empty_array;
153531
153532 $term_list = array();
153533 $has_children = _get_term_hierarchy($taxonomy);
153534
153535 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
153536 return $empty_array;
153537
153538 foreach ( (array) $terms as $term ) {
153539 $use_id = false;
153540 if ( !is_object($term) ) {
153541 $term = get_term($term, $taxonomy);
153542 if ( is_wp_error( $term ) )
153543 return $term;
153544 $use_id = true;
153545 }
153546
153547 if ( $term->term_id == $term_id )
153548 continue;
153549
153550 if ( $term->parent == $term_id ) {
153551 if ( $use_id )
153552 $term_list[] = $term->term_id;
153553 else
153554 $term_list[] = $term;
153555
153556 if ( !isset($has_children[$term->term_id]) )
153557 continue;
153558
153559 if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )
153560 $term_list = array_merge($term_list, $children);
153561 }
153562 }
153563
153564 return $term_list;
153565 }
153566
153567
153568 /**
153569 * Add count of children to parent count.
153570 *
153571 * Recalculates term counts by including items from child terms. Assumes all
153572 * relevant children are already in the $terms argument.
153573 *
153574 * @package WordPress
153575 * @subpackage Taxonomy
153576 * @access private
153577 * @since 2.3.0
153578 * @uses $wpdb
153579 *
153580 * @param array $terms List of Term IDs
153581 * @param string $taxonomy Term Context
153582 * @return null Will break from function if conditions are not met.
153583 */
153584 function _pad_term_counts(&$terms, $taxonomy) {
153585 global $wpdb;
153586
153587 // This function only works for post categories.
153588 if ( 'category' != $taxonomy )
153589 return;
153590
153591 $term_hier = _get_term_hierarchy($taxonomy);
153592
153593 if ( empty($term_hier) )
153594 return;
153595
153596 $term_items = array();
153597
153598 foreach ( (array) $terms as $key => $term ) {
153599 $terms_by_id[$term->term_id] = & $terms[$key];
153600 $term_ids[$term->term_taxonomy_id] = $term->term_id;
153601 }
153602
153603 // Get the object and term ids and stick them in a lookup table
153604 $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (".join(',', array_keys($term_ids)).") AND post_type = 'post' AND post_status = 'publish'");
153605 foreach ( $results as $row ) {
153606 $id = $term_ids[$row->term_taxonomy_id];
153607 $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
153608 }
153609
153610 // Touch every ancestor's lookup row for each post in each term
153611 foreach ( $term_ids as $term_id ) {
153612 $child = $term_id;
153613 while ( $parent = $terms_by_id[$child]->parent ) {
153614 if ( !empty($term_items[$term_id]) )
153615 foreach ( $term_items[$term_id] as $item_id => $touches ) {
153616 $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
153617 }
153618 $child = $parent;
153619 }
153620 }
153621
153622 // Transfer the touched cells
153623 foreach ( (array) $term_items as $id => $items )
153624 if ( isset($terms_by_id[$id]) )
153625 $terms_by_id[$id]->count = count($items);
153626 }
153627
153628 //
153629 // Default callbacks
153630 //
153631
153632 /**
153633 * Will update term count based on posts.
153634 *
153635 * Private function for the default callback for post_tag and category
153636 * taxonomies.
153637 *
153638 * @package WordPress
153639 * @subpackage Taxonomy
153640 * @access private
153641 * @since 2.3.0
153642 * @uses $wpdb
153643 *
153644 * @param array $terms List of Term taxonomy IDs
153645 */
153646 function _update_post_term_count( $terms ) {
153647 global $wpdb;
153648
153649 foreach ( (array) $terms as $term ) {
153650 $count = $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 = 'post' AND term_taxonomy_id = %d", $term ) );
153651 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
153652 }
153653 }
153654
153655
153656 /**
153657 * Generates a permalink for a taxonomy term archive.
153658 *
153659 * @since 2.5.0
153660 *
153661 * @param object|int|string $term
153662 * @param string $taxonomy
153663 * @return string HTML link to taxonomy term archive
153664 */
153665 function get_term_link( $term, $taxonomy ) {
153666 global $wp_rewrite;
153667
153668 // use legacy functions for core taxonomies until they are fully plugged in
153669 if ( $taxonomy == 'category' )
153670 return get_category_link($term);
153671 if ( $taxonomy == 'post_tag' )
153672 return get_tag_link($term);
153673
153674 $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
153675
153676 if ( !is_object($term) ) {
153677 if ( is_int($term) ) {
153678 $term = &get_term($term, $taxonomy);
153679 } else {
153680 $term = &get_term_by('slug', $term, $taxonomy);
153681 }
153682 }
153683 if ( is_wp_error( $term ) )
153684 return $term;
153685
153686 $slug = $term->slug;
153687
153688 if ( empty($termlink) ) {
153689 $file = get_option('home') . '/';
153690 $t = get_taxonomy($taxonomy);
153691 if ( $t->query_var )
153692 $termlink = "$file?$t->query_var=$slug";
153693 else
153694 $termlink = "$file?taxonomy=$taxonomy&term=$slug";
153695 } else {
153696 $termlink = str_replace("%$taxonomy%", $slug, $termlink);
153697 $termlink = get_option('home') . user_trailingslashit($termlink, 'category');
153698 }
153699 return apply_filters('term_link', $termlink, $term, $taxonomy);
153700 }
153701
153702 /**
153703 * Display the taxonomies of a post with available options.
153704 *
153705 * This function can be used within the loop to display the taxonomies for a
153706 * post without specifying the Post ID. You can also use it outside the Loop to
153707 * display the taxonomies for a specific post.
153708 *
153709 * The available defaults are:
153710 * 'post' : default is 0. The post ID to get taxonomies of.
153711 * 'before' : default is empty string. Display before taxonomies list.
153712 * 'sep' : default is empty string. Separate every taxonomy with value in this.
153713 * 'after' : default is empty string. Display this after the taxonomies list.
153714 *
153715 * @since 2.5.0
153716 * @uses get_the_taxonomies()
153717 *
153718 * @param array $args Override the defaults.
153719 */
153720 function the_taxonomies($args = array()) {
153721 $defaults = array(
153722 'post' => 0,
153723 'before' => '',
153724 'sep' => ' ',
153725 'after' => '',
153726 );
153727
153728 $r = wp_parse_args( $args, $defaults );
153729 extract( $r, EXTR_SKIP );
153730
153731 echo $before . join($sep, get_the_taxonomies($post)) . $after;
153732 }
153733
153734 /**
153735 * Retrieve all taxonomies associated with a post.
153736 *
153737 * This function can be used within the loop. It will also return an array of
153738 * the taxonomies with links to the taxonomy and name.
153739 *
153740 * @since 2.5.0
153741 *
153742 * @param int $post Optional. Post ID or will use Global Post ID (in loop).
153743 * @return array
153744 */
153745 function get_the_taxonomies($post = 0) {
153746 if ( is_int($post) )
153747 $post =& get_post($post);
153748 elseif ( !is_object($post) )
153749 $post =& $GLOBALS['post'];
153750
153751 $taxonomies = array();
153752
153753 if ( !$post )
153754 return $taxonomies;
153755
153756 $template = apply_filters('taxonomy_template', '%s: %l.');
153757
153758 foreach ( get_object_taxonomies($post) as $taxonomy ) {
153759 $t = (array) get_taxonomy($taxonomy);
153760 if ( empty($t['label']) )
153761 $t['label'] = $taxonomy;
153762 if ( empty($t['args']) )
153763 $t['args'] = array();
153764 if ( empty($t['template']) )
153765 $t['template'] = $template;
153766
153767 $terms = get_object_term_cache($post->ID, $taxonomy);
153768 if ( empty($terms) )
153769 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
153770
153771 $links = array();
153772
153773 foreach ( $terms as $term )
153774 $links[] = "<a href='" . attribute_escape(get_term_link($term, $taxonomy)) . "'>$term->name</a>";
153775
153776 if ( $links )
153777 $taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);
153778 }
153779 return $taxonomies;
153780 }
153781
153782 /**
153783 * Retrieve all taxonomies of a post with just the names.
153784 *
153785 * @since 2.5.0
153786 * @uses get_object_taxonomies()
153787 *
153788 * @param int $post Optional. Post ID
153789 * @return array
153790 */
153791 function get_post_taxonomies($post = 0) {
153792 $post =& get_post($post);
153793
153794 return get_object_taxonomies($post);
153795 }
153796
153797 /**
153798 * Determine if the given object is associated with any of the given terms.
153799 *
153800 * The given terms are checked against the object's terms' term_ids, names and slugs.
153801 * Terms given as integers will only be checked against the object's terms' term_ids.
153802 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
153803 *
153804 * @since 2.7.0
153805 * @uses get_object_term_cache()
153806 * @uses wp_get_object_terms()
153807 *
153808 * @param int $object_id. ID of the object (post ID, link ID, ...)
153809 * @param string $taxonomy. Single taxonomy name
153810 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said
153811 * @return bool|WP_Error. WP_Error on input error.
153812 */
153813 function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
153814 if ( !$object_id = (int) $object_id )
153815 return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
153816
153817 $object_terms = get_object_term_cache( $object_id, $taxonomy );
153818 if ( empty( $object_terms ) )
153819 $object_terms = wp_get_object_terms( $object_id, $taxonomy );
153820
153821 if ( is_wp_error( $object_terms ) )
153822 return $object_terms;
153823 if ( empty( $object_terms ) )
153824 return false;
153825 if ( empty( $terms ) )
153826 return ( !empty( $object_terms ) );
153827
153828 $terms = (array) $terms;
153829
153830 if ( $ints = array_filter( $terms, 'is_int' ) )
153831 $strs = array_diff( $terms, $ints );
153832 else
153833 $strs =& $terms;
153834
153835 foreach ( $object_terms as $object_term ) {
153836 if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id
153837 if ( $strs ) {
153838 if ( in_array( $object_term->term_id, $strs ) ) return true;
153839 if ( in_array( $object_term->name, $strs ) ) return true;
153840 if ( in_array( $object_term->slug, $strs ) ) return true;
153841 }
153842 }
153843
153844 return false;
153845 }
153846
153847 ?>