WordPress.org

Make WordPress Core

Changeset 23398


Ignore:
Timestamp:
02/08/2013 06:45:34 PM (5 years ago)
Author:
markjaquith
Message:

Introduce wp_remove_object_terms() and wp_add_object_terms(). No more fetch-alter-update nonsense. Use wp_remove_object_terms() in a few places internally.

props simonwheatley, scribu, ericmann, maxcutler. fixes #15475

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r23384 r23398  
    17131713 * @subpackage Taxonomy
    17141714 * @since 2.3.0
    1715  * @uses $wpdb
     1715 * @uses wp_remove_object_terms()
    17161716 *
    17171717 * @param int $object_id The term Object Id that refers to the term
     
    17191719 */
    17201720function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
    1721     global $wpdb;
    1722 
    17231721    $object_id = (int) $object_id;
    17241722
     
    17271725
    17281726    foreach ( (array) $taxonomies as $taxonomy ) {
    1729         $tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
    1730         $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
    1731         do_action( 'delete_term_relationships', $object_id, $tt_ids );
    1732         $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
    1733         do_action( 'deleted_term_relationships', $object_id, $tt_ids );
    1734         wp_update_term_count($tt_ids, $taxonomy);
     1727        $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
     1728        $term_ids = array_map( 'intval', $term_ids );
     1729        wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
    17351730    }
    17361731}
     
    21592154 * @subpackage Taxonomy
    21602155 * @since 2.3.0
    2161  * @uses $wpdb
     2156 * @uses wp_remove_object_terms()
    21622157 *
    21632158 * @param int $object_id The object to relate to.
     
    22162211
    22172212    if ( ! $append ) {
    2218         $delete_terms = array_diff($old_tt_ids, $tt_ids);
    2219         if ( $delete_terms ) {
    2220             $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
    2221             do_action( 'delete_term_relationships', $object_id, $delete_terms );
    2222             $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
    2223             do_action( 'deleted_term_relationships', $object_id, $delete_terms );
    2224             wp_update_term_count($delete_terms, $taxonomy);
     2213        $delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
     2214       
     2215        if ( $delete_tt_ids ) {
     2216            $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
     2217            $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 ) );
     2218            $delete_term_ids = array_map( 'intval', $delete_term_ids );
     2219           
     2220            $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
     2221            if ( is_wp_error( $remove ) ) {
     2222                return $remove;
     2223            }
    22252224        }
    22262225    }
     
    22432242    do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);
    22442243    return $tt_ids;
     2244}
     2245
     2246/**
     2247 * Add term(s) associated with a given object.
     2248 *
     2249 * @package WordPress
     2250 * @subpackage Taxonomy
     2251 * @since 3.6
     2252 * @uses wp_set_object_terms()
     2253 *
     2254 * @param int $object_id The ID of the object to which the terms will be added.
     2255 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to add.
     2256 * @param array|string $taxonomy Taxonomy name.
     2257 * @return array|WP_Error Affected Term IDs
     2258 */
     2259function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
     2260    return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
     2261}
     2262
     2263/**
     2264 * Remove term(s) associated with a given object.
     2265 *
     2266 * @package WordPress
     2267 * @subpackage Taxonomy
     2268 * @since 3.6
     2269 * @uses $wpdb
     2270 *
     2271 * @uses apply_filters() Calls 'delete_term_relationships' hook with object_id and tt_ids as parameters.
     2272 * @uses apply_filters() Calls 'deleted_term_relationships' hook with object_id and tt_ids as parameters.
     2273 *
     2274 * @param int $object_id The ID of the object from which the terms will be removed.
     2275 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove.
     2276 * @param array|string $taxonomy Taxonomy name.
     2277 * @return bool|WP_Error True on success, false or WP_Error on failure.
     2278 */
     2279function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
     2280    global $wpdb;
     2281
     2282    $object_id = (int) $object_id;
     2283
     2284    if ( ! taxonomy_exists( $taxonomy ) ) {
     2285        return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
     2286    }
     2287
     2288    if ( ! is_array( $terms ) ) {
     2289        $terms = array( $terms );
     2290    }
     2291
     2292    $tt_ids = array();
     2293   
     2294    foreach ( (array) $terms as $term ) {
     2295        if ( ! strlen( trim( $term ) ) ) {
     2296            continue;
     2297        }
     2298   
     2299        if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
     2300            // Skip if a non-existent term ID is passed.
     2301            if ( is_int( $term ) ) {
     2302                continue;
     2303            }
     2304        }
     2305   
     2306        if ( is_wp_error( $term_info ) ) {
     2307            return $term_info;
     2308        }
     2309   
     2310        $tt_ids[] = $term_info['term_taxonomy_id'];
     2311    }
     2312   
     2313    if ( $tt_ids ) {
     2314        $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
     2315        do_action( 'delete_term_relationships', $object_id, $tt_ids );
     2316        $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
     2317        do_action( 'deleted_term_relationships', $object_id, $tt_ids );
     2318        wp_update_term_count( $tt_ids, $taxonomy );
     2319       
     2320        return true;
     2321    }
     2322
     2323    return false;
    22452324}
    22462325
Note: See TracChangeset for help on using the changeset viewer.