WordPressを読む 60-5 /blog/wp-includes/taxonomy.php 5
2015/02/25
目次
/blog/wp-includes/taxonomy.php 5
関数 term_exists()
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 | /** * Check if Term exists. * * Formerly is_term(), introduced in 2.3.0. * * @since 3.0.0 * * @uses $wpdb * * @param int|string $term The term to check * @param string $taxonomy The taxonomy name to use * @param int $parent ID of parent term under which to confine the exists search. * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified * and the term ID exists. Returns an array of the term ID and the term taxonomy ID * if the taxonomy is specified and the pairing exists. */ function term_exists($term, $taxonomy = '', $parent = 0) { global $wpdb; $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; $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 "; if ( is_int($term) ) { if ( 0 == $term ) return 0; $where = 't.term_id = %d'; if ( !empty($taxonomy) ) return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); else return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); } $term = trim( wp_unslash( $term ) ); if ( '' === $slug = sanitize_title($term) ) return 0; $where = 't.slug = %s'; $else_where = 't.name = %s'; $where_fields = array($slug); $else_where_fields = array($term); if ( !empty($taxonomy) ) { $parent = (int) $parent; if ( $parent > 0 ) { $where_fields[] = $parent; $else_where_fields[] = $parent; $where .= ' AND tt.parent = %d'; $else_where .= ' AND tt.parent = %d'; } $where_fields[] = $taxonomy; $else_where_fields[] = $taxonomy; 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", $where_fields), ARRAY_A) ) return $result; 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", $else_where_fields), ARRAY_A); } if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) ) return $result; return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) ); } |
関数 term_is_ancestor_of()
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 | /** * Check if a term is an ancestor of another term. * * You can use either an id or the term object for both parameters. * * @since 3.4.0 * * @param int|object $term1 ID or object to check if this is the parent term. * @param int|object $term2 The child term. * @param string $taxonomy Taxonomy name that $term1 and $term2 belong to. * @return bool Whether $term2 is child of $term1 */ function term_is_ancestor_of( $term1, $term2, $taxonomy ) { if ( ! isset( $term1->term_id ) ) $term1 = get_term( $term1, $taxonomy ); if ( ! isset( $term2->parent ) ) $term2 = get_term( $term2, $taxonomy ); if ( empty( $term1->term_id ) || empty( $term2->parent ) ) return false; if ( $term2->parent == $term1->term_id ) return true; return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy ); } |
関数 sanitize_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 | /** * Sanitize Term all fields. * * Relies on sanitize_term_field() to sanitize the term. The difference is that * this function will sanitize <strong>all</strong> fields. The context is based * on sanitize_term_field(). * * The $term is expected to be either an array or an object. * * @since 2.3.0 * * @uses sanitize_term_field Used to sanitize all fields in a term * * @param array|object $term The term to check * @param string $taxonomy The taxonomy name to use * @param string $context Default is 'display'. * @return array|object Term with all fields sanitized */ function sanitize_term($term, $taxonomy, $context = 'display') { $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); $do_object = is_object( $term ); $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0); foreach ( (array) $fields as $field ) { if ( $do_object ) { if ( isset($term->$field) ) $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context); } else { if ( isset($term[$field]) ) $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context); } } if ( $do_object ) $term->filter = $context; else $term['filter'] = $context; return $term; } |
関数 sanitize_term_field()
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 | /** * Cleanse the field value in the term based on the context. * * Passing a term field value through the function should be assumed to have * cleansed the value for whatever context the term field is going to be used. * * If no context or an unsupported context is given, then default filters will * be applied. * * There are enough filters for each context to support a custom filtering * without creating your own filter function. Simply create a function that * hooks into the filter you need. * * @since 2.3.0 * * @uses $wpdb * * @param string $field Term field to sanitize * @param string $value Search for this term value * @param int $term_id Term ID * @param string $taxonomy Taxonomy Name * @param string $context Either edit, db, display, attribute, or js. * @return mixed sanitized field */ function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); if ( in_array( $field, $int_fields ) ) { $value = (int) $value; if ( $value < 0 ) $value = 0; } if ( 'raw' == $context ) return $value; if ( 'edit' == $context ) { /** * Filter a term field to edit before it is sanitized. * * The dynamic portion of the filter name, $field, refers to the term field. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. */ $value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy ); /** * Filter the taxonomy field to edit before it is sanitized. * * The dynamic portions of the filter name, $taxonomy, and $field, refer * to the taxonomy slug and taxonomy field, respectively. * * @since 2.3.0 * * @param mixed $value Value of the taxonomy field to edit. * @param int $term_id Term ID. */ $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id ); if ( 'description' == $field ) $value = esc_html($value); // textarea_escaped else $value = esc_attr($value); } else if ( 'db' == $context ) { /** * Filter a term field value before it is sanitized. * * The dynamic portion of the filter name, $field, refers to the term field. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param string $taxonomy Taxonomy slug. */ $value = apply_filters( "pre_term_{$field}", $value, $taxonomy ); /** * Filter a taxonomy field before it is sanitized. * * The dynamic portions of the filter name, $taxonomy, and $field, refer * to the taxonomy slug and field name, respectively. * * @since 2.3.0 * * @param mixed $value Value of the taxonomy field. */ $value = apply_filters( "pre_{$taxonomy}_{$field}", $value ); // Back compat filters if ( 'slug' == $field ) { /** * Filter the category nicename before it is sanitized. * * Use the pre_{$taxonomy}_{$field} hook instead. * * @since 2.0.3 * * @param string $value The category nicename. */ $value = apply_filters( 'pre_category_nicename', $value ); } } else if ( 'rss' == $context ) { /** * Filter the term field for use in RSS. * * The dynamic portion of the filter name, $field, refers to the term field. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param string $taxonomy Taxonomy slug. */ $value = apply_filters( "term_{$field}_rss", $value, $taxonomy ); /** * Filter the taxonomy field for use in RSS. * * The dynamic portions of the hook name, $taxonomy, and $field, refer * to the taxonomy slug and field name, respectively. * * @since 2.3.0 * * @param mixed $value Value of the taxonomy field. */ $value = apply_filters( "{$taxonomy}_{$field}_rss", $value ); } else { // Use display filters by default. /** * Filter the term field sanitized for display. * * The dynamic portion of the filter name, $field, refers to the term field name. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @param string $context Context to retrieve the term field value. */ $value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context ); /** * Filter the taxonomy field sanitized for display. * * The dynamic portions of the filter name, $taxonomy, and $field, refer * to the taxonomy slug and taxonomy field, respectively. * * @since 2.3.0 * * @param mixed $value Value of the taxonomy field. * @param int $term_id Term ID. * @param string $context Context to retrieve the taxonomy field value. */ $value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context ); } if ( 'attribute' == $context ) $value = esc_attr($value); else if ( 'js' == $context ) $value = esc_js($value); return $value; } |
関数 wp_count_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 | /** * Count how many terms are in Taxonomy. * * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true). * * @since 2.3.0 * * @uses get_terms() * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array. * * @param string $taxonomy Taxonomy name * @param array|string $args Overwrite defaults. See get_terms() * @return int|WP_Error How many terms are in $taxonomy. WP_Error if $taxonomy does not exist. */ function wp_count_terms( $taxonomy, $args = array() ) { $defaults = array('hide_empty' => false); $args = wp_parse_args($args, $defaults); // backwards compatibility if ( isset($args['ignore_empty']) ) { $args['hide_empty'] = $args['ignore_empty']; unset($args['ignore_empty']); } $args['fields'] = 'count'; return get_terms($taxonomy, $args); } |
関数 wp_delete_object_term_relationships()
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 | /** * Will unlink the object from the taxonomy or taxonomies. * * Will remove all relationships between the object and any terms in * a particular taxonomy or taxonomies. Does not remove the term or * taxonomy itself. * * @since 2.3.0 * @uses wp_remove_object_terms() * * @param int $object_id The term Object Id that refers to the term * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name. */ function wp_delete_object_term_relationships( $object_id, $taxonomies ) { $object_id = (int) $object_id; if ( !is_array($taxonomies) ) $taxonomies = array($taxonomies); foreach ( (array) $taxonomies as $taxonomy ) { $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); $term_ids = array_map( 'intval', $term_ids ); wp_remove_object_terms( $object_id, $term_ids, $taxonomy ); } } |
関数 wp_delete_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 | /** * Removes a term from the database. * * If the term is a parent of other terms, then the children will be updated to * that term's parent. * * The $args 'default' will only override the terms found, if there is only one * term found. Any other and the found terms are used. * * The $args 'force_default' will force the term supplied as default to be * assigned even if the object was not going to be termless * * @since 2.3.0 * * @uses $wpdb * * @param int $term Term ID * @param string $taxonomy Taxonomy Name * @param array|string $args Optional. Change 'default' term id and override found term ids. * @return bool|WP_Error Returns false if not term; true if completes delete action. */ function wp_delete_term( $term, $taxonomy, $args = array() ) { global $wpdb; $term = (int) $term; if ( ! $ids = term_exists($term, $taxonomy) ) return false; if ( is_wp_error( $ids ) ) return $ids; $tt_id = $ids['term_taxonomy_id']; $defaults = array(); if ( 'category' == $taxonomy ) { $defaults['default'] = get_option( 'default_category' ); if ( $defaults['default'] == $term ) return 0; // Don't delete the default category } $args = wp_parse_args($args, $defaults); if ( isset( $args['default'] ) ) { $default = (int) $args['default']; if ( ! term_exists( $default, $taxonomy ) ) { unset( $default ); } } if ( isset( $args['force_default'] ) ) { $force_default = $args['force_default']; } // Update children to point to new parent if ( is_taxonomy_hierarchical($taxonomy) ) { $term_obj = get_term($term, $taxonomy); if ( is_wp_error( $term_obj ) ) return $term_obj; $parent = $term_obj->parent; $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); /** * Fires immediately before a term to delete's children are reassigned a parent. * * @since 2.9.0 * * @param array $edit_tt_ids An array of term taxonomy IDs for the given term. */ do_action( 'edit_term_taxonomies', $edit_tt_ids ); $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) ); /** * Fires immediately after a term to delete's children are reassigned a parent. * * @since 2.9.0 * * @param array $edit_tt_ids An array of term taxonomy IDs for the given term. */ do_action( 'edited_term_taxonomies', $edit_tt_ids ); } $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); foreach ( (array) $objects as $object ) { $terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none')); if ( 1 == count($terms) && isset($default) ) { $terms = array($default); } else { $terms = array_diff($terms, array($term)); if (isset($default) && isset($force_default) && $force_default) $terms = array_merge($terms, array($default)); } $terms = array_map('intval', $terms); wp_set_object_terms($object, $terms, $taxonomy); } // Clean the relationship caches for all object types using this term $tax_object = get_taxonomy( $taxonomy ); foreach ( $tax_object->object_type as $object_type ) clean_object_term_cache( $objects, $object_type ); // Get the object before deletion so we can pass to actions below $deleted_term = get_term( $term, $taxonomy ); /** * Fires immediately before a term taxonomy ID is deleted. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. */ do_action( 'delete_term_taxonomy', $tt_id ); $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); /** * Fires immediately after a term taxonomy ID is deleted. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. */ do_action( 'deleted_term_taxonomy', $tt_id ); // Delete the term if no taxonomies use it. if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) ) $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); clean_term_cache($term, $taxonomy); /** * Fires after a term is deleted from the database and the cache is cleaned. * * @since 2.5.0 * * @param int $term Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. * @param mixed $deleted_term Copy of the already-deleted term, in the form specified * by the parent function. WP_Error otherwise. */ do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term ); /** * Fires after a term in a specific taxonomy is deleted. * * The dynamic portion of the hook name, $taxonomy, refers to the specific * taxonomy the term belonged to. * * @since 2.3.0 * * @param int $term Term ID. * @param int $tt_id Term taxonomy ID. * @param mixed $deleted_term Copy of the already-deleted term, in the form specified * by the parent function. WP_Error otherwise. */ do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term ); return true; } |