WordPressを読む 60-8 /blog/wp-includs/taxonomy.php 8
2015/02/27
目次
- 1 /blog/wp-includs/taxonomy.php 8
- 2 関数 clean_term_cache()
- 3 関数 get_object_term_cache()
- 4 関数 update_object_term_cache()
- 5 関数 update_term_cache()
- 6 関数 _get_term_hierarchy()
- 7 関数 _get_term_children()
- 8 関数 _pad_term_counts()
- 9 関数 _update_post_term_count()
- 10 関数 _update_generic_term_count()
- 11 関数 get_term_link()
- 12 関数 the_taxonomies()
- 13 関数 get_the_taxonomies()
- 14 関数 get_post_taxonomies()
- 15 関数 is_object_in_taxonomy()
- 16 関数 wp_get_term_taxonomy_parent_id()
- 17 関数 wp_check_term_hierarchy_for_loops()
/blog/wp-includs/taxonomy.php 8
関数 clean_term_cache()
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 | /** * Will remove all of the term ids from the cache. * * @since 2.3.0 * @uses $wpdb * * @param int|array $ids Single or list of Term IDs * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context. * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true. */ function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { global $wpdb; if ( !is_array($ids) ) $ids = array($ids); $taxonomies = array(); // If no taxonomy, assume tt_ids. if ( empty($taxonomy) ) { $tt_ids = array_map('intval', $ids); $tt_ids = implode(', ', $tt_ids); $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)"); $ids = array(); foreach ( (array) $terms as $term ) { $taxonomies[] = $term->taxonomy; $ids[] = $term->term_id; wp_cache_delete($term->term_id, $term->taxonomy); } $taxonomies = array_unique($taxonomies); } else { $taxonomies = array($taxonomy); foreach ( $taxonomies as $taxonomy ) { foreach ( $ids as $id ) { wp_cache_delete($id, $taxonomy); } } } foreach ( $taxonomies as $taxonomy ) { if ( $clean_taxonomy ) { wp_cache_delete('all_ids', $taxonomy); wp_cache_delete('get', $taxonomy); delete_option("{$taxonomy}_children"); // Regenerate {$taxonomy}_children _get_term_hierarchy($taxonomy); } /** * Fires once after each taxonomy's term cache has been cleaned. * * @since 2.5.0 * * @param array $ids An array of term IDs. * @param string $taxonomy Taxonomy slug. */ do_action( 'clean_term_cache', $ids, $taxonomy ); } wp_cache_set( 'last_changed', microtime(), 'terms' ); } |
関数 get_object_term_cache()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Retrieves the taxonomy relationship to the term object id. * * @since 2.3.0 * * @uses wp_cache_get() Retrieves taxonomy relationship from cache * * @param int|array $id Term object ID * @param string $taxonomy Taxonomy Name * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. */ function get_object_term_cache($id, $taxonomy) { $cache = wp_cache_get($id, "{$taxonomy}_relationships"); return $cache; } |
関数 update_object_term_cache()
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 | = $id; break; } } } if ( empty( $ids ) ) return false; $terms = wp_get_object_terms($ids, $taxonomies, array('fields' =] 'all_with_object_id')); $object_terms = array(); foreach ( (array) $terms as $term ) $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term; foreach ( $ids as $id ) { foreach ( $taxonomies as $taxonomy ) { if ( ! isset($object_terms[$id][$taxonomy]) ) { if ( !isset($object_terms[$id]) ) $object_terms[$id] = array(); $object_terms[$id][$taxonomy] = array(); } } } foreach ( $object_terms as $id => $value ) { foreach ( $value as $taxonomy => $terms ) { wp_cache_add( $id, $terms, "{$taxonomy}_relationships" ); } } } |
関数 update_term_cache()
1 2 3 4 5 | taxonomy; wp_cache_add($term->term_id, $term, $term_taxonomy); } } |
関数 _get_term_hierarchy()
1 2 3 4 5 6 7 8 9 | 'all', 'orderby' => 'id', 'fields' => 'id=>parent')); foreach ( $terms as $term_id => $parent ) { if ( $parent > 0 ) $children[$parent][] = $term_id; } update_option("{$taxonomy}_children", $children); return $children; } |
関数 _get_term_children()
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 | ) ) return $empty_array; foreach ( (array) $terms as $term ) { $use_id = false; if ( !is_object($term) ) { $term = get_term($term, $taxonomy); if ( is_wp_error( $term ) ) return $term; $use_id = true; } if ( $term-]term_id == $term_id ) { continue; } if ( $term->parent == $term_id ) { if ( $use_id ) $term_list[] = $term->term_id; else $term_list[] = $term; if ( !isset($has_children[$term->term_id]) ) continue; if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) ) $term_list = array_merge($term_list, $children); } } return $term_list; } |
関数 _pad_term_counts()
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 | $term ) { $terms_by_id[$term->term_id] = & $terms[$key]; $term_ids[$term->term_taxonomy_id] = $term->term_id; } // Get the object and term ids and stick them in a lookup table $tax_obj = get_taxonomy($taxonomy); $object_types = esc_sql($tax_obj->object_type); $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 (" . implode(',', array_keys($term_ids)) . ") AND post_type IN ('" . implode("', '", $object_types) . "') AND post_status = 'publish'"); foreach ( $results as $row ) { $id = $term_ids[$row->term_taxonomy_id]; $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1; } // Touch every ancestor's lookup row for each post in each term foreach ( $term_ids as $term_id ) { $child = $term_id; while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) { if ( !empty( $term_items[$term_id] ) ) foreach ( $term_items[$term_id] as $item_id => $touches ) { $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1; } $child = $parent; } } // Transfer the touched cells foreach ( (array) $term_items as $id => $items ) if ( isset($terms_by_id[$id]) ) $terms_by_id[$id]->count = count($items); } |
関数 _update_post_term_count()
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 | object_type; foreach ( $object_types as &$object_type ) list( $object_type ) = explode( ':', $object_type ); $object_types = array_unique( $object_types ); if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) { unset( $object_types[ $check_attachments ] ); $check_attachments = true; } if ( $object_types ) $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); foreach ( (array) $terms as $term ) { $count = 0; // Attachments can be 'inherit' status, we need to base count off the parent's status if so if ( $check_attachments ) $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); if ( $object_types ) $count += (int) $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 IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_term_taxonomy', $term, $taxonomy ); $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_term_taxonomy', $term, $taxonomy ); } } |
関数 _update_generic_term_count()
1 2 3 4 5 6 7 8 9 10 | get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_term_taxonomy', $term, $taxonomy ); $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_term_taxonomy', $term, $taxonomy ); } } |
関数 get_term_link()
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 | taxonomy; $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); $slug = $term->slug; $t = get_taxonomy($taxonomy); if ( empty($termlink) ) { if ( 'category' == $taxonomy ) $termlink = '?cat=' . $term->term_id; elseif ( $t->query_var ) $termlink = "?$t->query_var=$slug"; else $termlink = "?taxonomy=$taxonomy&term=$slug"; $termlink = home_url($termlink); } else { if ( $t->rewrite['hierarchical'] ) { $hierarchical_slugs = array(); $ancestors = get_ancestors($term->term_id, $taxonomy); foreach ( (array)$ancestors as $ancestor ) { $ancestor_term = get_term($ancestor, $taxonomy); $hierarchical_slugs[] = $ancestor_term->slug; } $hierarchical_slugs = array_reverse($hierarchical_slugs); $hierarchical_slugs[] = $slug; $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink); } else { $termlink = str_replace("%$taxonomy%", $slug, $termlink); } $termlink = home_url( user_trailingslashit($termlink, 'category') ); } // Back Compat filters. if ( 'post_tag' == $taxonomy ) { /** * Filter the tag link. * * @since 2.3.0 * @deprecated 2.5.0 Use 'term_link' instead. * * @param string $termlink Tag link URL. * @param int $term_id Term ID. */ $termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); } elseif ( 'category' == $taxonomy ) { /** * Filter the category link. * * @since 1.5.0 * @deprecated 2.5.0 Use 'term_link' instead. * * @param string $termlink Category link URL. * @param int $term_id Term ID. */ $termlink = apply_filters( 'category_link', $termlink, $term->term_id ); } /** * Filter the term link. * * @since 2.5.0 * * @param string $termlink Term link URL. * @param object $term Term object. * @param string $taxonomy Taxonomy slug. */ return apply_filters( 'term_link', $termlink, $term, $taxonomy ); } |
関数 the_taxonomies()
1 2 3 4 5 6 7 8 9 10 11 12 | 0, 'before' => '', 'sep' => ' ', 'after' => '', /* translators: %s: taxonomy label, %l: list of term links */ 'template' => __( '%s: %l.' ) ); $r = wp_parse_args( $args, $defaults ); echo $r['before'] . join( $r['sep'], get_the_taxonomies( $r['post'], $r ) ) . $r['after']; } |
関数 get_the_taxonomies()
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 | __( '%s: %l.' ), ) ); $taxonomies = array(); if ( ! $post ) { return $taxonomies; } foreach ( get_object_taxonomies( $post ) as $taxonomy ) { $t = (array) get_taxonomy( $taxonomy ); if ( empty( $t['label'] ) ) { $t['label'] = $taxonomy; } if ( empty( $t['args'] ) ) { $t['args'] = array(); } if ( empty( $t['template'] ) ) { $t['template'] = $args['template']; } $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] ); } $links = array(); foreach ( $terms as $term ) { $links[] = "<a href='" . esc_attr( get_term_link( $term ) ) . "'>$term->name</a>"; } if ( $links ) { $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); } } return $taxonomies; } |
関数 get_post_taxonomies()
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 | <h4>[link id=8534 ta=1 name=関数 is_object_in_term() ]</h4> <pre /** * Determine if the given object is associated with any of the given terms. * * The given terms are checked against the object's terms' term_ids, names and slugs. * Terms given as integers will only be checked against the object's terms' term_ids. * If no terms are given, determines if object is associated with any terms in the given taxonomy. * * @since 2.7.0 * @uses get_object_term_cache() * @uses wp_get_object_terms() * * @param int $object_id ID of the object (post ID, link ID, ...) * @param string $taxonomy Single taxonomy name * @param int|string|array $terms Optional. Term term_id, name, slug or array of said * @return bool|WP_Error. WP_Error on input error. */ function is_object_in_term( $object_id, $taxonomy, $terms = null ) { if ( !$object_id = (int) $object_id ) return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) ); $object_terms = get_object_term_cache( $object_id, $taxonomy ); if ( false === $object_terms ) $object_terms = wp_get_object_terms( $object_id, $taxonomy ); if ( is_wp_error( $object_terms ) ) return $object_terms; if ( empty( $object_terms ) ) return false; if ( empty( $terms ) ) return ( !empty( $object_terms ) ); $terms = (array) $terms; if ( $ints = array_filter( $terms, 'is_int' ) ) $strs = array_diff( $terms, $ints ); else $strs =& $terms; foreach ( $object_terms as $object_term ) { if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id if ( $strs ) { if ( in_array( $object_term->term_id, $strs ) ) return true; if ( in_array( $object_term->name, $strs ) ) return true; if ( in_array( $object_term->slug, $strs ) ) return true; } } return false; } |
関数 is_object_in_taxonomy()
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 | <h4>[link id=8538 ta=1 name=関数 get_ancestors() ]</h4> <pre /** * Get an array of ancestor IDs for a given object. * * @param int $object_id The ID of the object * @param string $object_type The type of object for which we'll be retrieving ancestors. * @return array of ancestors from lowest to highest in the hierarchy. */ function get_ancestors($object_id = 0, $object_type = '') { $object_id = (int) $object_id; $ancestors = array(); if ( empty( $object_id ) ) { /** This filter is documented in wp-includes/taxonomy.php */ return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type ); } if ( is_taxonomy_hierarchical( $object_type ) ) { $term = get_term($object_id, $object_type); while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { $ancestors[] = (int) $term->parent; $term = get_term($term->parent, $object_type); } } elseif ( post_type_exists( $object_type ) ) { $ancestors = get_post_ancestors($object_id); } /** * Filter a given object's ancestors. * * @since 3.1.0 * * @param array $ancestors An array of object ancestors. * @param int $object_id Object ID. * @param string $object_type Type of object. */ return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type ); } |
関数 wp_get_term_taxonomy_parent_id()
1 2 | parent; } |
関数 wp_check_term_hierarchy_for_loops()
1 2 3 4 5 6 7 8 9 | ) ) return 0; // There's a loop, but it doesn't contain $term_id. Break the loop. foreach ( array_keys( $loop ) as $loop_member ) wp_update_term( $loop_member, $taxonomy, array( 'parent' =] 0 ) ); return $parent; } |