Make WordPress Core

Ticket #15475: 15475-4.diff

File 15475-4.diff, 4.7 KB (added by ericmann, 12 years ago)

Refresh patch and update docs

  • wp-includes/taxonomy.php

     
    17121712 * @package WordPress
    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
    17181718 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
    17191719 */
    17201720function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
    1721         global $wpdb;
    1722 
    17231721        $object_id = (int) $object_id;
    17241722
    17251723        if ( !is_array($taxonomies) )
    17261724                $taxonomies = array($taxonomies);
    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}
    17371732
     
    21582153 * @package WordPress
    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.
    21642159 * @param array|int|string $terms The slug or id of the term, will replace all existing
     
    22152210                wp_update_term_count( $new_tt_ids, $taxonomy );
    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        }
    22272226
     
    22452244}
    22462245
    22472246/**
     2247 * Remove term(s) associated with a given object.
     2248 *
     2249 * @package WordPress
     2250 * @subpackage Taxonomy
     2251 * @since 3.6
     2252 * @uses $wpdb
     2253 *
     2254 * @uses apply_filters() Calls 'delete_term_relationships' hook with object_id and tt_ids as parameters.
     2255 * @uses apply_filters() Calls 'deleted_term_relationships' hook with object_id and tt_ids as parameters.
     2256 *
     2257 * @param int $object_id The ID of the object from which to remove the terms.
     2258 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove.
     2259 * @param array|string $taxonomy Taxonomy name.
     2260 * @return bool|WP_Error True on success, false or WP_Error on failure.
     2261 */
     2262function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
     2263        global $wpdb;
     2264
     2265        $object_id = (int) $object_id;
     2266
     2267        if ( ! taxonomy_exists( $taxonomy ) ) {
     2268                return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
     2269        }
     2270
     2271        if ( ! is_array( $terms ) ) {
     2272                $terms = array( $terms );
     2273        }
     2274
     2275        $tt_ids = array();
     2276       
     2277        foreach ( (array) $terms as $term ) {
     2278                if ( ! strlen( trim( $term ) ) ) {
     2279                        continue;
     2280                }
     2281       
     2282                if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
     2283                        // Skip if a non-existent term ID is passed.
     2284                        if ( is_int( $term ) ) {
     2285                                continue;
     2286                        }
     2287                }
     2288       
     2289                if ( is_wp_error( $term_info ) ) {
     2290                        return $term_info;
     2291                }
     2292       
     2293                $tt_ids[] = $term_info['term_taxonomy_id'];
     2294        }
     2295       
     2296        if ( $tt_ids ) {
     2297                $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
     2298                do_action( 'delete_term_relationships', $object_id, $tt_ids );
     2299                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
     2300                do_action( 'deleted_term_relationships', $object_id, $tt_ids );
     2301                wp_update_term_count( $tt_ids, $taxonomy );
     2302               
     2303                return true;
     2304        }
     2305
     2306        return false;
     2307}
     2308
     2309/**
    22482310 * Will make slug unique, if it isn't already.
    22492311 *
    22502312 * The $slug has to be unique global to every taxonomy, meaning that one