-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTaxonomy_Switcher.php
287 lines (233 loc) · 5.79 KB
/
Taxonomy_Switcher.php
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* Class Taxonomy_Switcher.
*/
class Taxonomy_Switcher {
/**
* Taxonomy to switch from.
*
* @var string
*/
public $from = '';
/**
* Taxonomy to switch to.
*
* @var string
*/
public $to = '';
/**
* Parent term_id to limit by.
*
* @var int
*/
public $parent = 0;
/**
* Array of term IDs to convert.
*
* @var array
*/
public $terms = [];
/**
* Array of term IDs to convert.
*
* @var array
*/
public $term_ids = [];
/**
* Array of notices from conversion.
*
* @var array
*/
public $notices = [];
/**
* Array of error/success messages.
*
* @var array
*/
public $messages = [];
/**
* Whether or not is our option page.
*
* @var bool
*/
public $is_ui;
/**
* Setup the object.
*
* @since 1.0.0
*
* @param array $args Arguments containing from taxonomy, to taxonomy,
* and additional optional params.
*/
public function __construct( $args = [] ) {
$args = wp_parse_args( $args, [
'from_tax' => '',
'to_tax' => '',
'parent' => '',
'terms' => '',
] );
if ( ! $args['from_tax'] || ! $args['to_tax'] ) {
return;
}
if ( ! empty( $args['parent'] ) ) {
$this->parent = absint( $args['parent'] );
}
if ( ! empty( $args['terms'] ) ) {
$this->terms = wp_parse_id_list( $args['terms'] );
}
$this->is_ui = ( isset( $_GET['page'] ) && 'taxonomy-switcher' == $_GET['page'] );
$this->from = sanitize_text_field( $args['from_tax'] );
$this->to = sanitize_text_field( $args['to_tax'] );
}
/**
* Convert taxonomy of terms from the Admin.
*
* @since 1.0.0
*/
public function admin_convert() {
$count = $this->count();
if ( ! $count && $this->is_ui ) {
return $this->notice( $this->notices( 'no_terms' ) );
}
$this->notice( $this->notices( 'switching' ) );
if ( 0 < $this->parent ) {
$this->notice( $this->notices( 'limit_by_parent' ) );
} elseif ( ! empty( $this->terms ) ) {
$this->notice( $this->notices( 'limit_by_terms' ) );
}
set_time_limit( 0 );
$this->convert();
$this->notice( $this->notices( 'switched' ) );
if ( $this->is_ui ) {
return $this->notices;
}
die();
}
/**
* Stores and (maybe) displays notices.
*
* @since 1.0.0
*
* @param string $notice Notice to store and/or display.
*
* @return array
*/
public function notice( $notice ) {
// Add to our notices array.
$this->notices[] = $notice;
if ( ! $this->is_ui ) {
echo $notice;
}
return $this->notices;
}
/**
* Compile our notices.
*
* @since 1.0.0
*
* @param string $key Array key to retrieve.
*
* @return mixed
*/
public function notices( $key ) {
if ( ! empty( $this->messages ) ) {
return $this->messages[ $key ];
}
$count = $this->count();
$count_name = sprintf( _n( '1 term', '%d terms', $count, 'wds' ), $count );
$this->messages = [
'no_terms' => __( 'No terms to be switched. Check if the term exists in your "from" taxonomy.', 'wds' ),
'switching' => sprintf( __( 'Switching %s with the taxonomy \'%s\' to the taxonomy \'%s\'', 'wds' ), $count_name, $this->from, $this->to ),
'limit_by_parent' => sprintf( __( 'Limiting the switch by the parent term_id of %d', 'wds' ), $this->parent ),
'limit_by_terms' => sprintf( __( 'Limiting the switch to these terms: %s', 'wds' ), implode( ', ', $this->terms ) ),
'switched' => sprintf( __( 'Taxonomies switched for %s!', 'wds' ), $count_name ),
];
return $this->messages[ $key ];
}
/**
* Get term ids based on $from and $parent.
*
* @since 1.0.0
*
* @return array An array of term ids.
*/
public function get_term_ids() {
$args = [
'hide_empty' => false,
'fields' => 'ids',
'child_of' => $this->parent,
'include' => $this->terms,
];
$args = apply_filters( 'taxonomy_switcher_get_terms_args', $args, $this->from, $this->to, [
'parent' => $this->parent,
'terms' => $this->terms
] );
$terms = get_terms( $this->from, $args );
$this->term_ids = [];
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$this->term_ids = $terms;
}
return $this->term_ids;
}
/**
* Return the total count of terms found.
*
* @since 1.0.0
*
* @return int Total count of terms found.
*/
public function count() {
if ( empty( $this->term_ids ) ) {
$this->get_term_ids();
}
return count( $this->term_ids );
}
/**
* Convert taxonomy of terms.
*
* @since 1.0.0
*
* @return bool Whether the conversion was successful.
*/
public function convert() {
if ( empty( $this->term_ids ) ) {
$this->get_term_ids();
}
if ( empty( $this->term_ids ) ) {
return false;
}
global $wpdb;
$term_ids = array_map( 'absint', $this->term_ids );
$term_ids = implode( ', ', $term_ids );
$wpdb->query( $wpdb->prepare( "
UPDATE `{$wpdb->term_taxonomy}`
SET `taxonomy` = %s
WHERE `taxonomy` = %s AND `term_id` IN ( {$term_ids} )
", $this->to, $this->from ) );
if ( 0 < $this->parent ) {
$wpdb->query( $wpdb->prepare( "
UPDATE `{$wpdb->term_taxonomy}`
SET `parent` = 0
WHERE `parent` = %d AND `term_id` IN ( {$term_ids} )
", $this->parent ) );
}
$post_ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_menu_item_object_id' AND meta_value IN ( {$term_ids} );" );
update_postmeta_cache( $post_ids );
foreach ( $post_ids as $post_id ) {
$type = get_post_meta( $post_id, '_menu_item_type', true );
$object = get_post_meta( $post_id, '_menu_item_object', true );
if ( 'taxonomy' !== $type ) {
continue;
}
if ( $this->from !== $object ) {
continue;
}
update_post_meta( $post_id, '_menu_item_object', $this->to );
clean_post_cache( $post_id );
}
// Clean term caches
clean_term_cache( $term_ids, $this->from );
clean_term_cache( $term_ids, $this->to );
return true;
}
}