WordPressを読む 60-7 /blog/wp-includs/taxonomy.php 7
2015/02/27
目次
/blog/wp-includs/taxonomy.php 7
関数 wp_add_object_terms()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Add term(s) associated with a given object. * * @since 3.6.0 * @uses wp_set_object_terms() * * @param int $object_id The ID of the object to which the terms will be added. * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to add. * @param array|string $taxonomy Taxonomy name. * @return array|WP_Error Affected Term IDs */ function wp_add_object_terms( $object_id, $terms, $taxonomy ) { return wp_set_object_terms( $object_id, $terms, $taxonomy, true ); } |
関数 wp_remove_object_terms()
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 | /** * Remove term(s) associated with a given object. * * @since 3.6.0 * @uses $wpdb * * @param int $object_id The ID of the object from which the terms will be removed. * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove. * @param array|string $taxonomy Taxonomy name. * @return bool|WP_Error True on success, false or WP_Error on failure. */ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { global $wpdb; $object_id = (int) $object_id; if ( ! taxonomy_exists( $taxonomy ) ) { return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); } if ( ! is_array( $terms ) ) { $terms = array( $terms ); } $tt_ids = array(); foreach ( (array) $terms as $term ) { if ( ! strlen( trim( $term ) ) ) { continue; } if ( ! $term_info = term_exists( $term, $taxonomy ) ) { // Skip if a non-existent term ID is passed. if ( is_int( $term ) ) { continue; } } if ( is_wp_error( $term_info ) ) { return $term_info; } $tt_ids[] = $term_info['term_taxonomy_id']; } if ( $tt_ids ) { $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; /** * Fires immediately before an object-term relationship is deleted. * * @since 2.9.0 * * @param int $object_id Object ID. * @param array $tt_ids An array of term taxonomy IDs. */ do_action( 'delete_term_relationships', $object_id, $tt_ids ); $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); /** * Fires immediately after an object-term relationship is deleted. * * @since 2.9.0 * * @param int $object_id Object ID. * @param array $tt_ids An array of term taxonomy IDs. */ do_action( 'deleted_term_relationships', $object_id, $tt_ids ); wp_update_term_count( $tt_ids, $taxonomy ); return (bool) $deleted; } return false; } |
関数 wp_unique_term_slug()
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 | /** * Will make slug unique, if it isn't already. * * The $slug has to be unique global to every taxonomy, meaning that one * taxonomy term can't have a matching slug with another taxonomy term. Each * slug has to be globally unique for every taxonomy. * * The way this works is that if the taxonomy that the term belongs to is * hierarchical and has a parent, it will append that parent to the $slug. * * If that still doesn't return an unique slug, then it try to append a number * until it finds a number that is truly unique. * * The only purpose for $term is for appending a parent, if one exists. * * @since 2.3.0 * @uses $wpdb * * @param string $slug The string that will be tried for a unique slug * @param object $term The term object that the $slug will belong too * @return string Will return a true unique slug. */ function wp_unique_term_slug($slug, $term) { global $wpdb; if ( ! term_exists( $slug ) ) return $slug; // If the taxonomy supports hierarchy and the term has a parent, make the slug unique // by incorporating parent slugs. if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) { $the_parent = $term->parent; while ( ! empty($the_parent) ) { $parent_term = get_term($the_parent, $term->taxonomy); if ( is_wp_error($parent_term) || empty($parent_term) ) break; $slug .= '-' . $parent_term->slug; if ( ! term_exists( $slug ) ) return $slug; if ( empty($parent_term->parent) ) break; $the_parent = $parent_term->parent; } } // If we didn't get a unique slug, try appending a number to make it unique. if ( ! empty( $term->term_id ) ) $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); else $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); if ( $wpdb->get_var( $query ) ) { $num = 2; do { $alt_slug = $slug . "-$num"; $num++; $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); } while ( $slug_check ); $slug = $alt_slug; } return $slug; } |
関数 wp_update_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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | /** * Update term based on arguments provided. * * The $args will indiscriminately override all values with the same field name. * Care must be taken to not override important information need to update or * update will fail (or perhaps create a new term, neither would be acceptable). * * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not * defined in $args already. * * 'alias_of' will create a term group, if it doesn't already exist, and update * it for the $term. * * If the 'slug' argument in $args is missing, then the 'name' in $args will be * used. It should also be noted that if you set 'slug' and it isn't unique then * a WP_Error will be passed back. If you don't pass any slug, then a unique one * will be created for you. * * For what can be overrode in $args, check the term scheme can contain and stay * away from the term keys. * * @since 2.3.0 * * @uses $wpdb * * @param int $term_id The ID of the term * @param string $taxonomy The context in which to relate the term to the object. * @param array|string $args Overwrite term field values * @return array|WP_Error Returns Term ID and Taxonomy Term ID */ function wp_update_term( $term_id, $taxonomy, $args = array() ) { global $wpdb; if ( ! taxonomy_exists($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); $term_id = (int) $term_id; // First, get all of the original args $term = get_term ($term_id, $taxonomy, ARRAY_A); if ( is_wp_error( $term ) ) return $term; // Escape data pulled from DB. $term = wp_slash($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); $parsed_args = $args; // expected_slashed ($name) $name = wp_unslash( $args['name'] ); $description = wp_unslash( $args['description'] ); $parsed_args['name'] = $name; $parsed_args['description'] = $description; if ( '' == trim($name) ) return new WP_Error('empty_term_name', __('A name is required for this term')); $empty_slug = false; if ( empty( $args['slug'] ) ) { $empty_slug = true; $slug = sanitize_title($name); } else { $slug = $args['slug']; } $parsed_args['slug'] = $slug; $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0; if ( $args['alias_of'] ) { $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) ); if ( $alias->term_group ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_terms', $alias->term_id, $taxonomy ); $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_terms', $alias->term_id, $taxonomy ); } $parsed_args['term_group'] = $term_group; } /** * Filter the term parent. * * Hook to this filter to see if it will cause a hierarchy loop. * * @since 3.1.0 * * @param int $parent ID of the parent term. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @param array $parsed_args An array of potentially altered update arguments for the given term. * @param array $args An array of update arguments for the given term. */ $parent = apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args ); // Check for duplicate slug $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); if ( $id && ($id != $term_id) ) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ( $empty_slug || ( $parent != $term['parent']) ) $slug = wp_unique_term_slug($slug, (object) $args); else return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); } /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_terms', $term_id, $taxonomy ); $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); if ( empty($slug) ) { $slug = sanitize_title($name, $term_id); $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); } /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_terms', $term_id, $taxonomy ); $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) ); /** * Fires immediate before a term-taxonomy relationship is updated. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); /** * Fires immediately after a term-taxonomy relationship is updated. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); // Clean the relationship caches for all object types using this term $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); $tax_object = get_taxonomy( $taxonomy ); foreach ( $tax_object->object_type as $object_type ) { clean_object_term_cache( $objects, $object_type ); } /** * Fires after a term has been updated, but before the term cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( "edit_term", $term_id, $tt_id, $taxonomy ); /** * Fires after a term in a specific taxonomy has been updated, but before the term * cache has been cleaned. * * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action( "edit_$taxonomy", $term_id, $tt_id ); /** This filter is documented in wp-includes/taxonomy.php */ $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); clean_term_cache($term_id, $taxonomy); /** * Fires after a term has been updated, and the term cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( "edited_term", $term_id, $tt_id, $taxonomy ); /** * Fires after a term for a specific taxonomy has been updated, and the term * cache has been cleaned. * * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action( "edited_$taxonomy", $term_id, $tt_id ); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); } |
関数 wp_defer_term_counting()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Enable or disable term counting. * * @since 2.5.0 * * @param bool $defer Optional. Enable if true, disable if false. * @return bool Whether term counting is enabled or disabled. */ function wp_defer_term_counting($defer=null) { static $_defer = false; if ( is_bool($defer) ) { $_defer = $defer; // flush any deferred counts if ( !$defer ) wp_update_term_count( null, null, true ); } return $_defer; } |
関数 wp_update_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 34 35 36 37 38 39 40 41 | /** * Updates the amount of terms in taxonomy. * * If there is a taxonomy callback applied, then it will be called for updating * the count. * * The default action is to count what the amount of terms have the relationship * of term ID. Once that is done, then update the database. * * @since 2.3.0 * @uses $wpdb * * @param int|array $terms The term_taxonomy_id of the terms * @param string $taxonomy The context of the term. * @return bool If no terms will return false, and if successful will return true. */ function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) { static $_deferred = array(); if ( $do_deferred ) { foreach ( (array) array_keys($_deferred) as $tax ) { wp_update_term_count_now( $_deferred[$tax], $tax ); unset( $_deferred[$tax] ); } } if ( empty($terms) ) return false; if ( !is_array($terms) ) $terms = array($terms); if ( wp_defer_term_counting() ) { if ( !isset($_deferred[$taxonomy]) ) $_deferred[$taxonomy] = array(); $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) ); return true; } return wp_update_term_count_now( $terms, $taxonomy ); } |
関数 wp_update_term_count_now()
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 | /** * Perform term count update immediately. * * @since 2.5.0 * * @param array $terms The term_taxonomy_id of terms to update. * @param string $taxonomy The context of the term. * @return bool Always true when complete. */ function wp_update_term_count_now( $terms, $taxonomy ) { $terms = array_map('intval', $terms); $taxonomy = get_taxonomy($taxonomy); if ( !empty($taxonomy->update_count_callback) ) { call_user_func($taxonomy->update_count_callback, $terms, $taxonomy); } else { $object_types = (array) $taxonomy->object_type; foreach ( $object_types as &$object_type ) { if ( 0 === strpos( $object_type, 'attachment:' ) ) list( $object_type ) = explode( ':', $object_type ); } if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) { // Only post types are attached to this taxonomy _update_post_term_count( $terms, $taxonomy ); } else { // Default count updater _update_generic_term_count( $terms, $taxonomy ); } } clean_term_cache($terms, '', false); return true; } |
関数 clean_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 32 33 34 35 36 37 38 39 40 | // // Cache // /** * Removes the taxonomy relationship to terms from the cache. * * Will remove the entire taxonomy relationship containing term $object_id. The * term IDs have to exist within the taxonomy $object_type for the deletion to * take place. * * @since 2.3.0 * * @see get_object_taxonomies() for more on $object_type * * @param int|array $object_ids Single or list of term object ID(s) * @param array|string $object_type The taxonomy object type */ function clean_object_term_cache($object_ids, $object_type) { if ( !is_array($object_ids) ) $object_ids = array($object_ids); $taxonomies = get_object_taxonomies( $object_type ); foreach ( $object_ids as $id ) { foreach ( $taxonomies as $taxonomy ) { wp_cache_delete($id, "{$taxonomy}_relationships"); } } /** * Fires after the object term cache has been cleaned. * * @since 2.5.0 * * @param array $object_ids An array of object IDs. * @param string $objet_type Object type. */ do_action( 'clean_object_term_cache', $object_ids, $object_type ); } |