WordPressを読む 60-2 /blog/wp-includes/taxonomy.php 2
2015/02/25
目次
/blog/wp-includes/taxonomy.php 2
関数 unregister_taxonomy_for_object_type()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * Remove an already registered taxonomy from an object type. * * @since 3.7.0 * * @param string $taxonomy Name of taxonomy object. * @param string $object_type Name of the object type. * @return bool True if successful, false if not. */ function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) { global $wp_taxonomies; if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) return false; if ( ! get_post_type_object( $object_type ) ) return false; $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ); if ( false === $key ) return false; unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] ); return true; } |
関数 get_objects_in_term()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // // Term API // /** * Retrieve object_ids of valid taxonomy and term. * * The strings of $taxonomies must exist before this function will continue. On * failure of finding a valid taxonomy, it will return an WP_Error class, kind * of like Exceptions in PHP 5, except you can't catch them. Even so, you can * still test for the WP_Error class and get the error message. * * The $terms aren't checked the same as $taxonomies, but still need to exist * for $object_ids to be returned. * * It is possible to change the order that object_ids is returned by either * using PHP sort family functions or using the database by using $args with * either ASC or DESC array. The value should be in the key named 'order'. * * @since 2.3.0 * * @uses $wpdb * @uses wp_parse_args() Creates an array from string $args. * * @param int|array $term_ids Term id or array of term ids of terms that will be used * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names * @param array|string $args Change the order of the object_ids, either ASC or DESC * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found. */ function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { global $wpdb; if ( ! is_array( $term_ids ) ) { $term_ids = array( $term_ids ); } if ( ! is_array( $taxonomies ) ) { $taxonomies = array( $taxonomies ); } foreach ( (array) $taxonomies as $taxonomy ) { if ( ! taxonomy_exists( $taxonomy ) ) { return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); } } $defaults = array( 'order' => 'ASC' ); $args = wp_parse_args( $args, $defaults ); $order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC'; $term_ids = array_map('intval', $term_ids ); $taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; $term_ids = "'" . implode( "', '", $term_ids ) . "'"; $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 ($term_ids) ORDER BY tr.object_id $order"); if ( ! $object_ids ){ return array(); } return $object_ids; } |
関数 get_tax_sql()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Given a taxonomy query, generates SQL to be appended to a main query. * * @since 3.1.0 * * @see WP_Tax_Query * * @param array $tax_query A compact tax query * @param string $primary_table * @param string $primary_id_column * @return array */ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { $tax_query_obj = new WP_Tax_Query( $tax_query ); return $tax_query_obj->get_sql( $primary_table, $primary_id_column ); } |
クラス WP_Tax_Query::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /** * Container class for a multiple taxonomy query. * * @since 3.1.0 */ class WP_Tax_Query { /** * List of taxonomy queries. A single taxonomy query is an associative array: * - 'taxonomy' string The taxonomy being queried. Optional when using the term_taxonomy_id field. * - 'terms' string|array The list of terms * - 'field' string (optional) Which term field is being used. * Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id' * Default: 'term_id' * - 'operator' string (optional) * Possible values: 'AND', 'IN' or 'NOT IN'. * Default: 'IN' * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified. * Default: true * * @since 3.1.0 * @access public * @var array */ public $queries = array(); /** * The relation between the queries. Can be one of 'AND' or 'OR'. * * @since 3.1.0 * @access public * @var string */ public $relation; /** * Standard response when the query should not return any rows. * * @since 3.2.0 * @access private * @var string */ private static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' ); |
クラス WP_Tax_Query::__construct()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /** * Constructor. * * Parses a compact tax query and sets defaults. * * @since 3.1.0 * @access public * * @param array $tax_query A compact tax query: * array( * 'relation' => 'OR', * array( * 'taxonomy' => 'tax1', * 'terms' => array( 'term1', 'term2' ), * 'field' => 'slug', * ), * array( * 'taxonomy' => 'tax2', * 'terms' => array( 'term-a', 'term-b' ), * 'field' => 'slug', * ), * ) */ public function __construct( $tax_query ) { if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) { $this->relation = 'OR'; } else { $this->relation = 'AND'; } $defaults = array( 'taxonomy' => '', 'terms' => array(), 'include_children' => true, 'field' => 'term_id', 'operator' => 'IN', ); foreach ( $tax_query as $query ) { if ( ! is_array( $query ) ) continue; $query = array_merge( $defaults, $query ); $query['terms'] = (array) $query['terms']; $this->queries[] = $query; } } |
クラス WP_Tax_Query::get_sql()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /** * Generates SQL clauses to be appended to a main query. * * @since 3.1.0 * @access public * * @param string $primary_table * @param string $primary_id_column * @return array */ public function get_sql( $primary_table, $primary_id_column ) { global $wpdb; $join = ''; $where = array(); $i = 0; $count = count( $this->queries ); foreach ( $this->queries as $index => $query ) { $this->clean_query( $query ); if ( is_wp_error( $query ) ) { return self::$no_results; } $terms = $query['terms']; $operator = strtoupper( $query['operator'] ); if ( 'IN' == $operator ) { if ( empty( $terms ) ) { if ( 'OR' == $this->relation ) { if ( ( $index + 1 === $count ) && empty( $where ) ) { return self::$no_results; } continue; } else { return self::$no_results; } } $terms = implode( ',', $terms ); $alias = $i ? 'tt' . $i : $wpdb->term_relationships; $join .= " INNER JOIN $wpdb->term_relationships"; $join .= $i ? " AS $alias" : ''; $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)"; $where[] = "$alias.term_taxonomy_id $operator ($terms)"; } elseif ( 'NOT IN' == $operator ) { if ( empty( $terms ) ) { continue; } $terms = implode( ',', $terms ); $where[] = "$primary_table.$primary_id_column NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($terms) )"; } elseif ( 'AND' == $operator ) { if ( empty( $terms ) ) { continue; } $num_terms = count( $terms ); $terms = implode( ',', $terms ); $where[] = "( SELECT COUNT(1) FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($terms) AND object_id = $primary_table.$primary_id_column ) = $num_terms"; } $i++; } if ( ! empty( $where ) ) { $where = ' AND ( ' . implode( " $this->relation ", $where ) . ' )'; } else { $where = ''; } return compact( 'join', 'where' ); } |
クラス WP_Tax_Query::clean_query()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * Validates a single query. * * @since 3.2.0 * @access private * * @param array &$query The single query */ private function clean_query( &$query ) { if ( empty( $query['taxonomy'] ) ) { if ( 'term_taxonomy_id' !== $query['field'] ) { $query = new WP_Error( 'Invalid taxonomy' ); return; } // so long as there are shared terms, include_children requires that a taxonomy is set $query['include_children'] = false; } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) { $query = new WP_Error( 'Invalid taxonomy' ); return; } $query['terms'] = array_unique( (array) $query['terms'] ); if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) { $this->transform_query( $query, 'term_id' ); if ( is_wp_error( $query ) ) return; $children = array(); foreach ( $query['terms'] as $term ) { $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) ); $children[] = $term; } $query['terms'] = $children; } $this->transform_query( $query, 'term_taxonomy_id' ); } |
クラス WP_Tax_Query::transform_query()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /** * Transforms a single query, from one field to another. * * @since 3.2.0 * * @param array &$query The single query * @param string $resulting_field The resulting field */ public function transform_query( &$query, $resulting_field ) { global $wpdb; if ( empty( $query['terms'] ) ) return; if ( $query['field'] == $resulting_field ) return; $resulting_field = sanitize_key( $resulting_field ); switch ( $query['field'] ) { case 'slug': case 'name': $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'"; $terms = $wpdb->get_col( " SELECT $wpdb->term_taxonomy.$resulting_field FROM $wpdb->term_taxonomy INNER JOIN $wpdb->terms USING (term_id) WHERE taxonomy = '{$query['taxonomy']}' AND $wpdb->terms.{$query['field']} IN ($terms) " ); break; case 'term_taxonomy_id': $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); $terms = $wpdb->get_col( " SELECT $resulting_field FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($terms) " ); break; default: $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); $terms = $wpdb->get_col( " SELECT $resulting_field FROM $wpdb->term_taxonomy WHERE taxonomy = '{$query['taxonomy']}' AND term_id IN ($terms) " ); } if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { $query = new WP_Error( 'Inexistent terms' ); return; } $query['terms'] = $terms; $query['field'] = $resulting_field; } } |
クラス WP_Tax_Query::クラス定義終了
関数 get_term()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | /** * Get all Term data from database by Term ID. * * The usage of the get_term function is to apply filters to a term object. It * is possible to get a term object from the database before applying the * filters. * * $term ID must be part of $taxonomy, to get from the database. Failure, might * be able to be captured by the hooks. Failure would be the same value as $wpdb * returns for the get_row method. * * There are two hooks, one is specifically for each term, named 'get_term', and * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the * term object, and the taxonomy name as parameters. Both hooks are expected to * return a Term object. * * 'get_term' hook - Takes two parameters the term Object and the taxonomy name. * Must return term object. Used in get_term() as a catch-all filter for every * $term. * * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy * name. Must return term object. $taxonomy will be the taxonomy name, so for * example, if 'category', it would be 'get_category' as the filter name. Useful * for custom taxonomies or plugging into default taxonomies. * * @since 2.3.0 * * @uses $wpdb * @uses sanitize_term() Cleanses the term based on $filter context before returning. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * * @param int|object $term If integer, will get from database. If object will apply filters and return $term. * @param string $taxonomy Taxonomy name that $term is part of. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N * @param string $filter Optional, default is raw or no WordPress defined filter will applied. * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not * exist then WP_Error will be returned. */ function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { global $wpdb; if ( empty($term) ) { $error = new WP_Error('invalid_term', __('Empty Term')); return $error; } if ( ! taxonomy_exists($taxonomy) ) { $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); return $error; } if ( is_object($term) && empty($term->filter) ) { wp_cache_add($term->term_id, $term, $taxonomy); $_term = $term; } else { if ( is_object($term) ) $term = $term->term_id; if ( !$term = (int) $term ) return null; if ( ! $_term = wp_cache_get($term, $taxonomy) ) { $_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) ); if ( ! $_term ) return null; wp_cache_add($term, $_term, $taxonomy); } } /** * Filter a term. * * @since 2.3.0 * * @param int|object $_term Term object or ID. * @param string $taxonomy The taxonomy slug. */ $_term = apply_filters( 'get_term', $_term, $taxonomy ); /** * Filter a taxonomy. * * The dynamic portion of the filter name, $taxonomy, refers * to the taxonomy slug. * * @since 2.3.0 * * @param int|object $_term Term object or ID. * @param string $taxonomy The taxonomy slug. */ $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); $_term = sanitize_term($_term, $taxonomy, $filter); if ( $output == OBJECT ) { return $_term; } elseif ( $output == ARRAY_A ) { $__term = get_object_vars($_term); return $__term; } elseif ( $output == ARRAY_N ) { $__term = array_values(get_object_vars($_term)); return $__term; } else { return $_term; } } |