Ticket #15475: 15475-3.diff

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

DRY patch for wp_remove_object_terms

Line 
1Index: wp-includes/taxonomy.php
2===================================================================
3--- wp-includes/taxonomy.php    (revision 19735)
4+++ wp-includes/taxonomy.php    (working copy)
5@@ -1678,26 +1678,21 @@
6  * @package WordPress
7  * @subpackage Taxonomy
8  * @since 2.3.0
9- * @uses $wpdb
10+ * @uses wp_remove_object_terms
11  *
12  * @param int $object_id The term Object Id that refers to the term
13  * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
14  */
15 function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
16-       global $wpdb;
17-
18        $object_id = (int) $object_id;
19 
20        if ( !is_array($taxonomies) )
21                $taxonomies = array($taxonomies);
22 
23        foreach ( (array) $taxonomies as $taxonomy ) {
24-               $tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
25-               $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
26-               do_action( 'delete_term_relationships', $object_id, $tt_ids );
27-               $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
28-               do_action( 'deleted_term_relationships', $object_id, $tt_ids );
29-               wp_update_term_count($tt_ids, $taxonomy);
30+               $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
31+               $term_ids = array_map( 'intval', $term_ids );
32+               wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
33        }
34 }
35 
36@@ -2115,6 +2110,7 @@
37  * @subpackage Taxonomy
38  * @since 2.3.0
39  * @uses $wpdb
40+ * @uses wp_remove_object_terms
41  *
42  * @param int $object_id The object to relate to.
43  * @param array|int|string $terms The slug or id of the term, will replace all existing
44@@ -2171,13 +2167,16 @@
45                wp_update_term_count( $new_tt_ids, $taxonomy );
46 
47        if ( ! $append ) {
48-               $delete_terms = array_diff($old_tt_ids, $tt_ids);
49-               if ( $delete_terms ) {
50-                       $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
51-                       do_action( 'delete_term_relationships', $object_id, $delete_terms );
52-                       $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
53-                       do_action( 'deleted_term_relationships', $object_id, $delete_terms );
54-                       wp_update_term_count($delete_terms, $taxonomy);
55+               $delete_tt_ids = array_diff($old_tt_ids, $tt_ids);
56+
57+               if ( $delete_tt_ids ) {
58+                       $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
59+                       $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 ) );
60+                       $delete_term_ids = array_map( 'intval', $delete_term_ids );
61+
62+                       $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
63+                       if ( is_wp_error( $remove ) )
64+                               return $remove;
65                }
66        }
67 
68@@ -2198,6 +2197,78 @@
69 }
70 
71 /**
72+ * Add term association(s) to a given object.
73+ *
74+ * @package WordPress
75+ * @subpackage Taxonomy
76+ * @since 3.4
77+ * @uses wp_set_object_terms
78+ *
79+ * @param int $object_id The object to relate to.
80+ * @param array|int|string $terms The slug or id of the term, will append to all existing
81+ * related terms in this taxonomy.
82+ * @param array|string $taxonomy The context in which to relate the term to the object.
83+ * @return array|WP_Error Affected Term IDs
84+ */
85+function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
86+       return wp_set_object_terms( $object_id, $terms, $taxonomy, true);
87+}
88+
89+/**
90+ * Remove term(s) associated with a given object.
91+ *
92+ * @package WordPress
93+ * @subpackage Taxonomy
94+ * @since 3.4
95+ * @uses $wpdb
96+ *
97+ * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
98+ * @param array|int|string $terms The slug or id of the term to remove.
99+ * @param array|string $taxonomy The context in which to relate the term to the object.
100+ * @return bool|WP_Error True on success, false or WP_Error on failure.
101+ */
102+function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
103+       global $wpdb;
104+
105+       $object_id = (int) $object_id;
106+
107+       if ( ! taxonomy_exists( $taxonomy ) )
108+               return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
109+
110+       if ( ! is_array( $terms ) )
111+               $terms = array( $terms );
112+
113+       $tt_ids = array();
114+
115+       foreach ( (array) $terms as $term) {
116+               if ( ! strlen( trim( $term ) ) )
117+                       continue;
118+
119+               if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
120+                       // Skip if a non-existent term ID is passed.
121+                       if ( is_int( $term ) )
122+                               continue;
123+               }
124+
125+               if ( is_wp_error( $term_info ) )
126+                       return $term_info;
127+
128+               $tt_ids[] = $term_info['term_taxonomy_id'];
129+       }
130+
131+       if ( $tt_ids ) {
132+               $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
133+               do_action( 'delete_term_relationships', $object_id, $tt_ids );
134+               $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
135+               do_action( 'deleted_term_relationships', $object_id, $tt_ids );
136+               wp_update_term_count( $tt_ids, $taxonomy );
137+               return true;
138+       }
139+
140+       return false;
141+}
142+
143+/**
144  * Will make slug unique, if it isn't already.
145  *
146  * The $slug has to be unique global to every taxonomy, meaning that one