WordPressを読む 60-6 /blog/wp-includes/taxonomy.php 6
2015/02/26
目次
/blog/wp-includes/taxonomy.php 6
関数 wp_delete_category()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Deletes one existing category. * * @since 2.0.0 * @uses wp_delete_term() * * @param int $cat_ID * @return mixed Returns true if completes delete action; false if term doesn't exist; * Zero on attempted deletion of default Category; WP_Error object is also a possibility. */ function wp_delete_category( $cat_ID ) { return wp_delete_term( $cat_ID, 'category' ); } |
関数 wp_get_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 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 | /** * Retrieves the terms associated with the given object(s), in the supplied taxonomies. * * The following information has to do the $args parameter and for what can be * contained in the string or array of that parameter, if it exists. * * The first argument is called, 'orderby' and has the default value of 'name'. * The other value that is supported is 'count'. * * The second argument is called, 'order' and has the default value of 'ASC'. * The only other value that will be acceptable is 'DESC'. * * The final argument supported is called, 'fields' and has the default value of * 'all'. There are multiple other options that can be used instead. Supported * values are as follows: 'all', 'ids', 'names', and finally * 'all_with_object_id'. * * The fields argument also decides what will be returned. If 'all' or * 'all_with_object_id' is chosen or the default kept intact, then all matching * terms objects will be returned. If either 'ids' or 'names' is used, then an * array of all matching term ids or term names will be returned respectively. * * @since 2.3.0 * @uses $wpdb * * @param int|array $object_ids The ID(s) of the object(s) to retrieve. * @param string|array $taxonomies The taxonomies to retrieve terms from. * @param array|string $args Change what is returned * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist. */ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { global $wpdb; if ( empty( $object_ids ) || empty( $taxonomies ) ) return array(); if ( !is_array($taxonomies) ) $taxonomies = array($taxonomies); foreach ( $taxonomies as $taxonomy ) { if ( ! taxonomy_exists($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } if ( !is_array($object_ids) ) $object_ids = array($object_ids); $object_ids = array_map('intval', $object_ids); $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); $args = wp_parse_args( $args, $defaults ); $terms = array(); if ( count($taxonomies) > 1 ) { foreach ( $taxonomies as $index => $taxonomy ) { $t = get_taxonomy($taxonomy); if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) { unset($taxonomies[$index]); $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); } } } else { $t = get_taxonomy($taxonomies[0]); if ( isset($t->args) && is_array($t->args) ) $args = array_merge($args, $t->args); } $orderby = $args['orderby']; $order = $args['order']; $fields = $args['fields']; if ( 'count' == $orderby ) $orderby = 'tt.count'; else if ( 'name' == $orderby ) $orderby = 't.name'; else if ( 'slug' == $orderby ) $orderby = 't.slug'; else if ( 'term_group' == $orderby ) $orderby = 't.term_group'; else if ( 'term_order' == $orderby ) $orderby = 'tr.term_order'; else if ( 'none' == $orderby ) { $orderby = ''; $order = ''; } else { $orderby = 't.term_id'; } // tt_ids queries can only be none or tr.term_taxonomy_id if ( ('tt_ids' == $fields) && !empty($orderby) ) $orderby = 'tr.term_taxonomy_id'; if ( !empty($orderby) ) $orderby = "ORDER BY $orderby"; $order = strtoupper( $order ); if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) $order = 'ASC'; $taxonomies = "'" . implode("', '", $taxonomies) . "'"; $object_ids = implode(', ', $object_ids); $select_this = ''; if ( 'all' == $fields ) $select_this = 't.*, tt.*'; else if ( 'ids' == $fields ) $select_this = 't.term_id'; else if ( 'names' == $fields ) $select_this = 't.name'; else if ( 'slugs' == $fields ) $select_this = 't.slug'; else if ( 'all_with_object_id' == $fields ) $select_this = 't.*, tt.*, tr.object_id'; $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) $orderby $order"; $objects = false; if ( 'all' == $fields || 'all_with_object_id' == $fields ) { $_terms = $wpdb->get_results( $query ); foreach ( $_terms as $key => $term ) { $_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' ); } $terms = array_merge( $terms, $_terms ); update_term_cache( $terms ); $objects = true; } else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { $_terms = $wpdb->get_col( $query ); $_field = ( 'ids' == $fields ) ? 'term_id' : 'name'; foreach ( $_terms as $key => $term ) { $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' ); } $terms = array_merge( $terms, $_terms ); } else if ( 'tt_ids' == $fields ) { $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) $orderby $order"); foreach ( $terms as $key => $tt_id ) { $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context. } } if ( ! $terms ) { $terms = array(); } elseif ( $objects && 'all_with_object_id' !== $fields ) { $_tt_ids = array(); $_terms = array(); foreach ( $terms as $term ) { if ( in_array( $term->term_taxonomy_id, $_tt_ids ) ) { continue; } $_tt_ids[] = $term->term_taxonomy_id; $_terms[] = $term; } $terms = $_terms; } elseif ( ! $objects ) { $terms = array_values( array_unique( $terms ) ); } /** * Filter the terms for a given object or objects. * * @since 2.8.0 * * @param array $terms An array of terms for the given object or objects. * @param array|int $object_ids Object ID or array of IDs. * @param array|string $taxonomies A taxonomy or array of taxonomies. * @param array $args An array of arguments for retrieving terms for * the given object(s). */ return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); } |
関数 wp_insert_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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | /** * Add a new term to the database. * * A non-existent term is inserted in the following sequence: * 1. The term is added to the term table, then related to the taxonomy. * 2. If everything is correct, several actions are fired. * 3. The 'term_id_filter' is evaluated. * 4. The term cache is cleaned. * 5. Several more actions are fired. * 6. An array is returned containing the term_id and term_taxonomy_id. * * If the 'slug' argument is not empty, then it is checked to see if the term * is invalid. If it is not a valid, existing term, it is added and the term_id * is given. * * If the taxonomy is hierarchical, and the 'parent' argument is not empty, * the term is inserted and the term_id will be given. * Error handling: * If $taxonomy does not exist or $term is empty, * a WP_Error object will be returned. * * If the term already exists on the same hierarchical level, * or the term slug and name are not unique, a WP_Error object will be returned. * * @global wpdb $wpdb The WordPress database object. * @since 2.3.0 * * @param string $term The term to add or update. * @param string $taxonomy The taxonomy to which to add the term * @param array|string $args { * Arguments to change values of the inserted term. * * @type string 'alias_of' Slug of the term to make this term an alias of. * Default empty string. Accepts a term slug. * @type string 'description' The term description. * Default empty string. * @type int 'parent' The id of the parent term. * Default 0. * @type string 'slug' The term slug to use. * Default empty string. * } * @return array|WP_Error An array containing the term_id and term_taxonomy_id, WP_Error otherwise. */ function wp_insert_term( $term, $taxonomy, $args = array() ) { global $wpdb; if ( ! taxonomy_exists($taxonomy) ) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } /** * Filter a term before it is sanitized and inserted into the database. * * @since 3.0.0 * * @param string $term The term to add or update. * @param string $taxonomy Taxonomy slug. */ $term = apply_filters( 'pre_insert_term', $term, $taxonomy ); if ( is_wp_error( $term ) ) { return $term; } if ( is_int($term) && 0 == $term ) { return new WP_Error('invalid_term_id', __('Invalid term ID')); } if ( '' == trim($term) ) { return new WP_Error('empty_term_name', __('A name is required for this term')); } $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args( $args, $defaults ); if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); } $args['name'] = $term; $args['taxonomy'] = $taxonomy; $args = sanitize_term($args, $taxonomy, 'db'); // expected_slashed ($name) $name = wp_unslash( $args['name'] ); $description = wp_unslash( $args['description'] ); $parent = (int) $args['parent']; $slug_provided = ! empty( $args['slug'] ); if ( ! $slug_provided ) { $_name = trim( $name ); $existing_term = get_term_by( 'name', $_name, $taxonomy ); if ( $existing_term ) { $slug = $existing_term->slug; } else { $slug = sanitize_title( $name ); } } else { $slug = $args['slug']; } $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; /** * Fires immediately before the given terms are edited. * * @since 2.9.0 * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. */ do_action( 'edit_terms', $alias->term_id, $taxonomy ); $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) ); /** * Fires immediately after the given terms are edited. * * @since 2.9.0 * * @param int $term_id Term ID * @param string $taxonomy Taxonomy slug. */ do_action( 'edited_terms', $alias->term_id, $taxonomy ); } } if ( $term_id = term_exists($slug) ) { $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A ); // We've got an existing term in the same taxonomy, which matches the name of the new term: if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) { // Hierarchical, and it matches an existing term, Do not allow same "name" in the same level. $siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => $parent ) ); if ( in_array($name, $siblings) ) { if ( $slug_provided ) { return new WP_Error( 'term_exists', __( 'A term with the name and slug provided already exists with this parent.' ), $exists['term_id'] ); } else { return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $exists['term_id'] ); } } else { $slug = wp_unique_term_slug($slug, (object) $args); if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) { return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); } $term_id = (int) $wpdb->insert_id; } } elseif ( $existing_term['name'] != $name ) { // We've got an existing term, with a different name, Create the new term. $slug = wp_unique_term_slug($slug, (object) $args); if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) { return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); } $term_id = (int) $wpdb->insert_id; } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) ) { // Same name, same slug. return new WP_Error( 'term_exists', __( 'A term with the name and slug provided already exists.' ), $exists['term_id'] ); } } else { // This term does not exist at all in the database, Create it. $slug = wp_unique_term_slug($slug, (object) $args); if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) { return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); } $term_id = (int) $wpdb->insert_id; } // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. if ( empty($slug) ) { $slug = sanitize_title($slug, $term_id); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_terms', $term_id, $taxonomy ); $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 ) ); if ( !empty($tt_id) ) { return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); } $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) ); $tt_id = (int) $wpdb->insert_id; /** * Fires immediately after a new term is created, before the term cache is 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( "create_term", $term_id, $tt_id, $taxonomy ); /** * Fires after a new term is created for a specific taxonomy. * * The dynamic portion of the hook name, $taxonomy, refers * to the slug of the taxonomy the term was created for. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action( "create_$taxonomy", $term_id, $tt_id ); /** * Filter the term ID after a new term is created. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Taxonomy term ID. */ $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); clean_term_cache($term_id, $taxonomy); /** * Fires after a new term is created, and after the term cache has been cleaned. * * @since 2.3.0 */ do_action( "created_term", $term_id, $tt_id, $taxonomy ); /** * Fires after a new term in a specific taxonomy is created, and after the term * cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action( "created_$taxonomy", $term_id, $tt_id ); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); } |
関数 wp_set_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 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 | /** * Create Term and Taxonomy Relationships. * * Relates an object (post, link etc) to a term and taxonomy type. Creates the * term and taxonomy relationship if it doesn't already exist. Creates a term if * it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. * A term has no meaning until it is given context by defining which taxonomy it * exists under. * * @since 2.3.0 * @uses wp_remove_object_terms() * * @param int $object_id The object to relate to. * @param array|int|string $terms A single term slug, single term id, or array of either term slugs or ids. * Will replace all existing related terms in this taxonomy. * @param array|string $taxonomy The context in which to relate the term to the object. * @param bool $append Optional. If false will delete difference of terms. Default false. * @return array|WP_Error Affected Term IDs. */ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 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); if ( ! $append ) $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none')); else $old_tt_ids = array(); $tt_ids = array(); $term_ids = array(); $new_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; $term_info = wp_insert_term($term, $taxonomy); } if ( is_wp_error($term_info) ) return $term_info; $term_ids[] = $term_info['term_id']; $tt_id = $term_info['term_taxonomy_id']; $tt_ids[] = $tt_id; 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 ) ) ) continue; /** * Fires immediately before an object-term relationship is added. * * @since 2.9.0 * * @param int $object_id Object ID. * @param int $tt_id Term taxonomy ID. */ do_action( 'add_term_relationship', $object_id, $tt_id ); $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) ); /** * Fires immediately after an object-term relationship is added. * * @since 2.9.0 * * @param int $object_id Object ID. * @param int $tt_id Term taxonomy ID. */ do_action( 'added_term_relationship', $object_id, $tt_id ); $new_tt_ids[] = $tt_id; } if ( $new_tt_ids ) wp_update_term_count( $new_tt_ids, $taxonomy ); if ( ! $append ) { $delete_tt_ids = array_diff( $old_tt_ids, $tt_ids ); if ( $delete_tt_ids ) { $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) ); $delete_term_ids = array_map( 'intval', $delete_term_ids ); $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); if ( is_wp_error( $remove ) ) { return $remove; } } } $t = get_taxonomy($taxonomy); if ( ! $append && isset($t->sort) && $t->sort ) { $values = array(); $term_order = 0; $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); foreach ( $tt_ids as $tt_id ) if ( in_array($tt_id, $final_tt_ids) ) $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order); if ( $values ) if ( false === $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)" ) ) return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database' ), $wpdb->last_error ); } wp_cache_delete( $object_id, $taxonomy . '_relationships' ); /** * Fires after an object's terms have been set. * * @since 2.8.0 * * @param int $object_id Object ID. * @param array $terms An array of object terms. * @param array $tt_ids An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether to append new terms to the old terms. * @param array $old_tt_ids Old array of term taxonomy IDs. */ do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ); return $tt_ids; } |