WordPress.org

Make WordPress Core

Ticket #15475: 15475-3.diff

File 15475-3.diff, 5.1 KB (added by maxcutler, 3 years ago)

DRY patch for wp_remove_object_terms

  • wp-includes/taxonomy.php

     
    16781678 * @package WordPress 
    16791679 * @subpackage Taxonomy 
    16801680 * @since 2.3.0 
    1681  * @uses $wpdb 
     1681 * @uses wp_remove_object_terms 
    16821682 * 
    16831683 * @param int $object_id The term Object Id that refers to the term 
    16841684 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name. 
    16851685 */ 
    16861686function wp_delete_object_term_relationships( $object_id, $taxonomies ) { 
    1687         global $wpdb; 
    1688  
    16891687        $object_id = (int) $object_id; 
    16901688 
    16911689        if ( !is_array($taxonomies) ) 
    16921690                $taxonomies = array($taxonomies); 
    16931691 
    16941692        foreach ( (array) $taxonomies as $taxonomy ) { 
    1695                 $tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); 
    1696                 $in_tt_ids = "'" . implode("', '", $tt_ids) . "'"; 
    1697                 do_action( 'delete_term_relationships', $object_id, $tt_ids ); 
    1698                 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) ); 
    1699                 do_action( 'deleted_term_relationships', $object_id, $tt_ids ); 
    1700                 wp_update_term_count($tt_ids, $taxonomy); 
     1693                $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); 
     1694                $term_ids = array_map( 'intval', $term_ids ); 
     1695                wp_remove_object_terms( $object_id, $term_ids, $taxonomy ); 
    17011696        } 
    17021697} 
    17031698 
     
    21152110 * @subpackage Taxonomy 
    21162111 * @since 2.3.0 
    21172112 * @uses $wpdb 
     2113 * @uses wp_remove_object_terms 
    21182114 * 
    21192115 * @param int $object_id The object to relate to. 
    21202116 * @param array|int|string $terms The slug or id of the term, will replace all existing 
     
    21712167                wp_update_term_count( $new_tt_ids, $taxonomy ); 
    21722168 
    21732169        if ( ! $append ) { 
    2174                 $delete_terms = array_diff($old_tt_ids, $tt_ids); 
    2175                 if ( $delete_terms ) { 
    2176                         $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; 
    2177                         do_action( 'delete_term_relationships', $object_id, $delete_terms ); 
    2178                         $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) ); 
    2179                         do_action( 'deleted_term_relationships', $object_id, $delete_terms ); 
    2180                         wp_update_term_count($delete_terms, $taxonomy); 
     2170                $delete_tt_ids = array_diff($old_tt_ids, $tt_ids); 
     2171 
     2172                if ( $delete_tt_ids ) { 
     2173                        $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; 
     2174                        $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 ) ); 
     2175                        $delete_term_ids = array_map( 'intval', $delete_term_ids ); 
     2176 
     2177                        $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); 
     2178                        if ( is_wp_error( $remove ) ) 
     2179                                return $remove; 
    21812180                } 
    21822181        } 
    21832182 
     
    21982197} 
    21992198 
    22002199/** 
     2200 * Add term association(s) to a given object. 
     2201 * 
     2202 * @package WordPress 
     2203 * @subpackage Taxonomy 
     2204 * @since 3.4 
     2205 * @uses wp_set_object_terms 
     2206 * 
     2207 * @param int $object_id The object to relate to. 
     2208 * @param array|int|string $terms The slug or id of the term, will append to all existing 
     2209 * related terms in this taxonomy. 
     2210 * @param array|string $taxonomy The context in which to relate the term to the object. 
     2211 * @return array|WP_Error Affected Term IDs 
     2212 */ 
     2213function wp_add_object_terms( $object_id, $terms, $taxonomy ) { 
     2214        return wp_set_object_terms( $object_id, $terms, $taxonomy, true); 
     2215} 
     2216 
     2217/** 
     2218 * Remove term(s) associated with a given object. 
     2219 * 
     2220 * @package WordPress 
     2221 * @subpackage Taxonomy 
     2222 * @since 3.4 
     2223 * @uses $wpdb 
     2224 * 
     2225 * @param int|array $object_ids The ID(s) of the object(s) to retrieve. 
     2226 * @param array|int|string $terms The slug or id of the term to remove. 
     2227 * @param array|string $taxonomy The context in which to relate the term to the object. 
     2228 * @return bool|WP_Error True on success, false or WP_Error on failure. 
     2229 */ 
     2230function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { 
     2231        global $wpdb; 
     2232 
     2233        $object_id = (int) $object_id; 
     2234 
     2235        if ( ! taxonomy_exists( $taxonomy ) ) 
     2236                return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 
     2237 
     2238        if ( ! is_array( $terms ) ) 
     2239                $terms = array( $terms ); 
     2240 
     2241        $tt_ids = array(); 
     2242 
     2243        foreach ( (array) $terms as $term) { 
     2244                if ( ! strlen( trim( $term ) ) ) 
     2245                        continue; 
     2246 
     2247                if ( ! $term_info = term_exists( $term, $taxonomy ) ) { 
     2248                        // Skip if a non-existent term ID is passed. 
     2249                        if ( is_int( $term ) ) 
     2250                                continue; 
     2251                } 
     2252 
     2253                if ( is_wp_error( $term_info ) ) 
     2254                        return $term_info; 
     2255 
     2256                $tt_ids[] = $term_info['term_taxonomy_id']; 
     2257        } 
     2258 
     2259        if ( $tt_ids ) { 
     2260                $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; 
     2261                do_action( 'delete_term_relationships', $object_id, $tt_ids ); 
     2262                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); 
     2263                do_action( 'deleted_term_relationships', $object_id, $tt_ids ); 
     2264                wp_update_term_count( $tt_ids, $taxonomy ); 
     2265                return true; 
     2266        } 
     2267 
     2268        return false; 
     2269} 
     2270 
     2271/** 
    22012272 * Will make slug unique, if it isn't already. 
    22022273 * 
    22032274 * The $slug has to be unique global to every taxonomy, meaning that one