Completed
Push — master ( 234952...c4c208 )
by Sudar
01:21
created

Bulk_Move_Posts::move_custom_taxonomy()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 5
nop 0
dl 0
loc 54
rs 7.255
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Utility class for moving posts.
4
 *
5
 * @since   1.0
6
 *
7
 * @author  Sudar
8
 */
9
class Bulk_Move_Posts {
10
11
	/**
12
	 * Render move categories box.
13
	 *
14
	 * @since 1.0
15
	 */
16
	public static function render_move_category_box() {
17
18
		if ( Bulk_Move_Util::is_posts_box_hidden( Bulk_Move::BOX_CATEGORY ) ) {
19
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-move' ), 'tools.php?page=' . Bulk_Move::POSTS_PAGE_SLUG );
20
21
			return;
22
		}
23
		?>
24
		<!-- Category Start-->
25
		<h4><?php _e( 'On the left side, select the category whose post you want to move. In the right side select the category to which you want the posts to be moved.', 'bulk-move' ); ?></h4>
26
27
		<fieldset class="options">
28
			<table class="optiontable">
29
				<tr>
30
					<td scope="row" >
31
						<?php
32
						wp_dropdown_categories( array(
33
							'name'         => 'smbm_mc_selected_cat',
34
							'show_count'   => true,
35
							'hierarchical' => true,
36
							'orderby'      => 'NAME',
37
							'hide_empty'   => false,
38
						) );
39
						?>
40
						==>
41
					</td>
42
					<td scope="row" >
43
						<?php
44
						wp_dropdown_categories( array(
45
							'name'             => 'smbm_mc_mapped_cat',
46
							'show_count'       => true,
47
							'hierarchical'     => true,
48
							'orderby'          => 'NAME',
49
							'hide_empty'       => false,
50
							'show_option_none' => __( 'Remove Category', 'bulk-move' ),
51
						) );
52
						?>
53
					</td>
54
				</tr>
55
56
			</table>
57
			<p>
58
				<?php _e( 'If the post contains other categories, then', 'bulk-move' ); ?>
59
				<input type="radio" name="smbm_mc_overwrite" value="overwrite" checked><?php _e( 'Remove them', 'bulk-move' ); ?>
60
				<input type="radio" name="smbm_mc_overwrite" value="no-overwrite"><?php _e( "Don't remove them", 'bulk-move' ); ?>
61
			</p>
62
63
		</fieldset>
64
		<p class="submit">
65
			<button type="submit" name="bm_action" value="move_cats" class="button-primary"><?php _e( 'Bulk Move ', 'bulk-move' ); ?>&raquo;</button>
66
		</p>
67
		<!-- Category end-->
68
		<?php
69
	}
70
71
	/**
72
	 * Move posts from one category to another.
73
	 *
74
	 * @static
75
	 * @access public
76
	 *
77
	 * @since  1.2.0
78
	 */
79
	public static function move_cats() {
80
		if ( check_admin_referer( 'sm-bulk-move-posts', 'sm-bulk-move-posts-nonce' ) ) {
81
82
			do_action( 'bm_pre_request_handler' );
83
84
			$wp_query = new WP_Query();
85
			$bm       = BULK_MOVE();
86
87
			// move by cats.
88
			$old_cat = absint( $_POST['smbm_mc_selected_cat'] );
89
			$new_cat = ( -1 === $_POST['smbm_mc_mapped_cat'] ) ? -1 : absint( $_POST['smbm_mc_mapped_cat'] );
90
91
			$posts = $wp_query->query(array(
92
				'category__in' => array( $old_cat ),
93
				'post_type'    => 'post',
94
				'nopaging'     => 'true',
95
			) );
96
97
			foreach ( $posts as $post ) {
98
				$current_cats = array_diff( wp_get_post_categories( $post->ID ), array( $old_cat ) );
99
100
				if ( -1 !== $new_cat ) {
101
					if ( isset( $_POST['smbm_mc_overwrite'] ) && 'overwrite' == $_POST['smbm_mc_overwrite'] ) {
102
						// Remove old categories.
103
						$current_cats = array( $new_cat );
104
					} else {
105
						// Add to existing categories.
106
						$current_cats[] = $new_cat;
107
					}
108
				}
109
110
				if ( count( $current_cats ) == 0 ) {
111
					$current_cats = array( get_option( 'default_category' ) );
112
				}
113
				$current_cats = array_values( $current_cats );
114
				wp_update_post(array(
115
					'ID'            => $post->ID,
116
					'post_category' => $current_cats,
117
				) );
118
			}
119
120
			$bm->msg = sprintf( _n( 'Moved %d post from the selected category', 'Moved %d posts from the selected category' , count( $posts ), 'bulk-move' ), count( $posts ) );
121
		}
122
	}
123
124
	/**
125
	 * Render move by tag box.
126
	 *
127
	 * @since 1.1
128
	 * @static
129
	 * @access public
130
	 */
131
	public static function render_move_tag_box() {
132
133
		if ( Bulk_Move_Util::is_posts_box_hidden( Bulk_Move::BOX_TAG ) ) {
134
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-move' ), 'tools.php?page=' . Bulk_Move::POSTS_PAGE_SLUG );
135
136
			return;
137
		}
138
139
		$tags = bm_get_tags_or_fail();
140
141
		if ( empty( $tags ) ) {
142
			return;
143
		}
144
		?>
145
146
		<!-- Tag Start-->
147
		<h4><?php _e( 'On the left side, select the tag whose post you want to move. In the right side select the tag to which you want the posts to be moved.', 'bulk-move' ); ?></h4>
148
149
		<fieldset class="options">
150
			<table class="optiontable">
151
				<tr>
152
					<td scope="row" >
153
						<?php bm_render_tags_dropdown( 'smbm_mt_old_tag', $tags ); ?>
154
						==>
155
					</td>
156
					<td scope="row" >
157
						<?php bm_render_tags_dropdown( 'smbm_mt_new_tag', $tags, true ); ?>
158
					</td>
159
				</tr>
160
161
			</table>
162
			<p>
163
				<?php _e( 'If the post contains other tags, then', 'bulk-move' ); ?>
164
				<input type="radio" name="smbm_mt_overwrite" value="overwrite" checked><?php _e( 'Remove them', 'bulk-move' ); ?>
165
				<input type="radio" name="smbm_mt_overwrite" value="no-overwrite"><?php _e( "Don't remove them", 'bulk-move' ); ?>
166
			</p>
167
		</fieldset>
168
		<p class="submit">
169
			<button type="submit" name="bm_action" value="move_tags" class="button-primary"><?php _e( 'Bulk Move ', 'bulk-move' ); ?>&raquo;</button>
170
		</p>
171
		<!-- Tag end-->
172
		<?php
173
	}
174
175
	/**
176
	 * Move posts from one tag to another.
177
	 *
178
	 * @static
179
	 * @access public
180
	 *
181
	 * @since  1.2.0
182
	 */
183
	public static function move_tags() {
184
185
		if ( check_admin_referer( 'sm-bulk-move-posts', 'sm-bulk-move-posts-nonce' ) ) {
186
187
			do_action( 'bm_pre_request_handler' );
188
189
			$wp_query = new WP_Query();
190
			$bm       = BULK_MOVE();
191
192
			$old_tag = absint( $_POST['smbm_mt_old_tag'] );
193
			$new_tag = ( -1 === $_POST['smbm_mt_new_tag'] ) ? -1 : absint( $_POST['smbm_mt_new_tag'] );
194
195
			$posts = $wp_query->query( array(
196
				'tag__in'   => $old_tag,
197
				'post_type' => 'post',
198
				'nopaging'  => 'true',
199
			));
200
201
			foreach ( $posts as $post ) {
202
				$current_tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
203
				$current_tags = array_diff( $current_tags, array( $old_tag ) );
204
205
				if ( -1 !== $new_tag ) {
206
					if ( isset( $_POST['smbm_mt_overwrite'] ) && 'overwrite' == $_POST['smbm_mt_overwrite'] ) {
207
						// Remove old tags.
208
						$current_tags = array( $new_tag );
209
					} else {
210
						// add to existing tags.
211
						$current_tags[] = $new_tag;
212
					}
213
				}
214
215
				$current_tags = array_values( $current_tags );
216
				wp_set_post_tags( $post->ID, $current_tags );
217
			}
218
219
			$bm->msg = sprintf( _n( 'Moved %d post from the selected tag', 'Moved %d posts from the selected tag', count( $posts ), 'bulk-move' ), count( $posts ) );
220
		}
221
	}
222
223
	/**
224
	 * Render move category by tag box.
225
	 *
226
	 * @since 1.2
227
	 * @static
228
	 * @access public
229
	 */
230
	public static function render_move_category_by_tag_box() {
231
232
		if ( Bulk_Move_Util::is_posts_box_hidden( Bulk_Move::BOX_CATEGORY_BY_TAG ) ) {
233
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-move' ), 'tools.php?page=' . Bulk_Move::POSTS_PAGE_SLUG );
234
235
			return;
236
		}
237
238
		$tags = bm_get_tags_or_fail();
239
240
		if ( empty( $tags ) ) {
241
			return;
242
		}
243
		?>
244
245
		<!-- Category by Tag Start-->
246
		<h4>
247
			<?php _e( 'On the left side, select the tag whose post you want to move. In the right side select the category to which you want the posts to be moved.', 'bulk-move' ); ?>
248
		</h4>
249
250
		<fieldset class="options">
251
			<table class="optiontable">
252
				<tr>
253
					<td scope="row">
254
						<?php bm_render_tags_dropdown( 'smbm_mct_old_tag', $tags ); ?>
255
						==>
256
					</td>
257
					<td scope="row" >
258
						<?php
259
						wp_dropdown_categories( array(
260
							'name'             => 'smbm_mct_mapped_cat',
261
							'show_count'       => true,
262
							'hierarchical'     => true,
263
							'orderby'          => 'NAME',
264
							'hide_empty'       => false,
265
							'show_option_none' => __( 'Choose Category', 'bulk-move' ),
266
						) );
267
						?>
268
					</td>
269
				</tr>
270
271
			</table>
272
			<p>
273
				<?php _e( 'If the post contains other categories, then', 'bulk-move' ); ?>
274
				<input type="radio" name="smbm_mct_overwrite" value="overwrite" checked><?php _e( 'Remove them', 'bulk-move' ); ?>
275
				<input type="radio" name="smbm_mct_overwrite" value="no-overwrite"><?php _e( "Don't remove them", 'bulk-move' ); ?>
276
			</p>
277
		</fieldset>
278
		<p class="submit">
279
			<button type="submit" name="bm_action" value="move_category_by_tag" class="button-primary"><?php _e( 'Bulk Move ', 'bulk-move' ); ?>&raquo;</button>
280
		</p>
281
		<!-- Tag end-->
282
		<?php
283
	}
284
285
	/**
286
	 * Move posts from a tag to another category.
287
	 *
288
	 * @static
289
	 * @access public
290
	 *
291
	 * @since  1.2.0
292
	 */
293
	public static function move_category_by_tag() {
294
295
		if ( check_admin_referer( 'sm-bulk-move-posts', 'sm-bulk-move-posts-nonce' ) ) {
296
297
			do_action( 'bm_pre_request_handler' );
298
299
			$wp_query = new WP_Query();
300
			$bm       = BULK_MOVE();
301
302
			$old_tag = absint( $_POST['smbm_mct_old_tag'] );
303
			$new_cat = ( -1 === $_POST['smbm_mct_mapped_cat'] ) ? -1 : absint( $_POST['smbm_mct_mapped_cat'] );
304
305
			$posts = $wp_query->query( array(
306
				'tag__in'   => $old_tag,
307
				'post_type' => 'post',
308
				'nopaging'  => 'true',
309
			));
310
311
			foreach ( $posts as $post ) {
312
				$current_cats = wp_get_post_categories( $post->ID );
313
314
				if ( -1 !== $new_cat ) {
315
					if ( isset( $_POST['smbm_mct_overwrite'] ) && 'overwrite' == $_POST['smbm_mct_overwrite'] ) {
316
						// Remove old categories.
317
						$current_cats = array( $new_cat );
318
					} else {
319
						// Add to existing categories.
320
						$current_cats[] = $new_cat;
321
					}
322
				}
323
324
				if ( count( $current_cats ) == 0 ) {
325
					$current_cats = array( get_option( 'default_category' ) );
326
				}
327
				$current_cats = array_values( $current_cats );
328
				wp_update_post( array(
329
					'ID'            => $post->ID,
330
					'post_category' => $current_cats,
331
				) );
332
			}
333
334
			$bm->msg = sprintf( _n( 'Moved %d post from the selected tag to the new category.', 'Moved %d posts from the selected tag to the new category.' , count( $posts ), 'bulk-move' ), count( $posts ) );
335
		}
336
	}
337
338
	/**
339
	 * Render debug box.
340
	 *
341
	 * @static
342
	 * @access public
343
	 *
344
	 * @since  1.0
345
	 */
346
	public static function render_debug_box() {
347
348
		// Get max script execution time from option.
349
		$max_execution_time = get_option( Bulk_Move::SCRIPT_TIMEOUT_OPTION );
350
		if ( ! $max_execution_time ) {
351
			$max_execution_time = '';
352
		}
353
		?>
354
		<!-- Debug box start-->
355
		<p>
356
			<?php _e( 'If you are seeing a blank page after clicking the Bulk Move button, then ', 'bulk-move' ); ?>
357
			<a href = "http://sudarmuthu.com/wordpress/bulk-move#faq"><?php _e( 'check out this FAQ', 'bulk-move' ); ?></a>.
358
			<?php _e( 'You also need need the following debug information.', 'bulk-move' ); ?>
359
		</p>
360
		<table cellspacing="10">
361
			<tr>
362
				<th align="right"><?php _e( 'PHP Version ', 'bulk-move' ); ?></th>
363
				<td><?php echo phpversion(); ?></td>
364
			</tr>
365
			<tr>
366
				<th align="right"><?php _e( 'WordPress Version ', 'bulk-move' ); ?></th>
367
				<td><?php echo get_bloginfo( 'version' ); ?></td>
368
			</tr>
369
			<tr>
370
				<th align="right"><?php _e( 'Plugin Version ', 'bulk-move' ); ?></th>
371
				<td><?php echo Bulk_Move::VERSION; ?></td>
372
			</tr>
373
			<tr>
374
				<th align="right"><?php _e( 'Available memory size ', 'bulk-move' ); ?></th>
375
				<td><?php echo ini_get( 'memory_limit' ); ?></td>
376
			</tr>
377
			<tr>
378
				<th align="right"><?php _e( 'Script time out ', 'bulk-move' ); ?></th>
379
				<td><strong><?php echo ini_get( 'max_execution_time' ); ?></strong> (<?php _e( 'In php.ini', 'bulk-move' ); ?>). <?php _e( 'Custom value: ', 'bulk-move' ); ?><input type="text" id="smbm_max_execution_time" name="smbm_max_execution_time" value="<?php echo $max_execution_time; ?>" > <button type="submit" name="bm_action" value="save_timeout" class="button-primary"><?php _e( 'Save', 'bulk-move' ) ?> &raquo;</button></td>
380
			</tr>
381
			<tr>
382
				<th align="right"><?php _e( 'Script input time ', 'bulk-move' ); ?></th>
383
				<td><?php echo ini_get( 'max_input_time' ); ?></td>
384
			</tr>
385
		</table>
386
387
		<p><em><?php _e( 'If you are looking to delete posts in bulk, try out my ', 'bulk-move' ); ?> <a href = "http://sudarmuthu.com/wordpress/bulk-delete"><?php _e( 'Bulk Delete Plugin', 'bulk-move' ); ?></a>.</em></p>
388
		<!-- Debug box end-->
389
		<?php
390
	}
391
392
	/**
393
	 * Save php timeout value.
394
	 *
395
	 * @static
396
	 * @access public
397
	 *
398
	 * @since  1.2.0
399
	 */
400
	public static function save_timeout() {
401
402
		if ( check_admin_referer( 'sm-bulk-move-posts', 'sm-bulk-move-posts-nonce' ) ) {
403
			$bm                     = BULK_MOVE();
404
			$new_max_execution_time = $_POST['smbm_max_execution_time'];
405
406
			if ( is_numeric( $new_max_execution_time ) ) {
407
				$option_updated = update_option( Bulk_Move::SCRIPT_TIMEOUT_OPTION, $new_max_execution_time );
408
409
				if ( $option_updated ) {
410
					$bm->msg = sprintf( __( 'Max execution time was successfully saved as %s seconds.', 'bulk-move' ), $new_max_execution_time );
411
				} else {
412
					// Error saving option.
413
					$bm->msg = __( 'An unknown error occurred while saving your options.', 'bulk-move' );
414
				}
415
			} else {
416
				// Error, value was not numeric.
417
				$bm->msg = sprintf( __( 'Could not update the max execution time to %s, it was not numeric.  Enter the max number of seconds this script should run.', 'bulk-move' ), $new_max_execution_time );
418
			}
419
		}
420
	}
421
422
	/**
423
	 * Change php `script_timeout`.
424
	 *
425
	 * @static
426
	 * @access public
427
	 *
428
	 * @since  1.2.0
429
	 */
430
	public static function change_timeout() {
431
		// get max script execution time from option.
432
		$max_execution_time = get_option( Bulk_Move::SCRIPT_TIMEOUT_OPTION );
433
		if ( ! $max_execution_time ) {
434
			// Increase script timeout in order to handle many posts.
435
			ini_set( 'max_execution_time', $max_execution_time );
436
		}
437
	}
438
439
	/**
440
	 * Loads the custom Taxonomy by Post Type.
441
	 *
442
	 * @since 1.3.0
443
	 */
444
	public static function load_custom_taxonomy_by_post_type() {
445
		check_ajax_referer( Bulk_Move::BOX_CUSTOM_TERMS_NONCE, 'nonce' );
446
447
		$post_type  = isset( $_POST['post_type'] ) ? sanitize_text_field( $_POST['post_type'] ) : 'post';
448
		$taxonomies = get_object_taxonomies( $post_type );
449
450
		$no_taxonomy_message = sprintf( __( 'There are no taxonomies associated with "%s" post type.', 'bulk-move' ), $post_type );
451
452
		wp_send_json_success(
453
			array(
454
				'taxonomies'            => $taxonomies,
455
				'no_taxonomy_msg'       => $no_taxonomy_message,
456
				'select_taxonomy_label' => __( 'Select Taxonomy', 'bulk-move' ),
457
			)
458
		);
459
	}
460
461
	/**
462
	 * Loads the custom Terms by Taxonomy.
463
	 *
464
	 * @since 1.3.0
465
	 */
466
	public static function load_custom_terms_by_taxonomy() {
467
		check_ajax_referer( Bulk_Move::BOX_CUSTOM_TERMS_NONCE, 'nonce' );
468
469
		$terms    = array();
470
		$taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : '';
471
472
		$wp_terms = get_terms(
473
			array(
474
				'taxonomy'   => $taxonomy,
475
				'hide_empty' => false,
476
				'orderby'    => 'name',
477
			)
478
		);
479
480
		if ( ! is_wp_error( $wp_terms ) ) {
481
			foreach ( $wp_terms as $wp_term ) {
482
				$terms[ $wp_term->term_id ] = array( 'term_name' => esc_html( $wp_term->name ), 'term_count' => absint( $wp_term->count ) );
483
			}
484
		}
485
486
		$no_terms_message = sprintf( __( 'There are no terms associated with "%s" taxonomy.', 'bulk-move' ), $taxonomy );
487
488
		wp_send_json_success(
489
			array(
490
				'terms'             => $terms,
491
				'no_term_msg'       => $no_terms_message,
492
				'select_term_label' => __( 'Select Term', 'bulk-move' ),
493
				'remove_term_label' => __( 'Remove Term', 'bulk-move' ),
494
			)
495
		);
496
	}
497
498
	/**
499
	 * Render move terms box.
500
	 *
501
	 * @since 1.3.0
502
	 */
503
	public static function render_move_by_custom_taxonomy_box() {
504
505
		if ( Bulk_Move_Util::is_posts_box_hidden( Bulk_Move::BOX_CATEGORY ) ) {
506
			printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-move' ), 'tools.php?page=' . Bulk_Move::POSTS_PAGE_SLUG );
507
508
			return;
509
		}
510
		?>
511
512
		<!-- Custom Taxonomy Start-->
513
514
		<fieldset class="options">
515
			<table class="optiontable">
516
				<tr>
517
					<td scope="row" colspan="2">
518
						<?php _e( 'Select the post type to show its custom taxonomy.', 'bulk-move' ); ?>
519
					</td>
520
					<td scope="row">
521
				</tr>
522
523
				<tr>
524
					<td scope="row" colspan="2">
525
						<select name="smbm_mbct_post_type" id="smbm_mbct_post_type">
526
							<option value="-1"><?php _e( 'Select Post type', 'bulk-move' ); ?></option>
527
528
							<?php
529
							$custom_post_types = get_post_types( array( 'public' => true ) );
530
							?>
531
532
							<?php foreach ( $custom_post_types as $post_type ) : ?>
533
								<option value="<?php echo esc_attr( $post_type ); ?>"><?php echo esc_html( $post_type ); ?></option>
534
							<?php endforeach; ?>
535
						</select>
536
					</td>
537
				</tr>
538
539
				<tr class="taxonomy-select-row">
540
					<td scope="row" colspan="2">
541
						<?php _e( 'Select taxonomy to show its terms.', 'bulk-move' ); ?>
542
					</td>
543
					<td scope="row">
544
				</tr>
545
546
				<tr class="taxonomy-select-row">
547
					<td scope="row" colspan="2">
548
						<select name="smbm_mbct_taxonomy" id="smbm_mbct_taxonomy">
549
							<option value="select"><?php _e( 'Select Taxonomy', 'bulk-move' ); ?></option>
550
						</select>
551
					</td>
552
				</tr>
553
554
				<tr class="term-select-row">
555
					<td scope="row" colspan="2">
556
						<?php _e( 'Select terms to move its posts.', 'bulk-move' ); ?>
557
					</td>
558
					<td scope="row">
559
				</tr>
560
561
				<tr class="term-select-row">
562
					<td scope="row" >
563
						<select name="smbm_mbct_selected_term" id="smbm_mbct_selected_term" class="postform">
564
							<option class="level-0" value="-1"><?php _e( ' Select Term&nbsp;&nbsp;', 'bulk-move' ); ?></option>
565
						</select>
566
						==>
567
					</td>
568
					<td scope="row" >
569
						<select name="smbm_mbct_mapped_term" id="smbm_mbct_mapped_term" class="postform">
570
							<option class="level-0" value="-1"><?php _e( 'Remove Term&nbsp;&nbsp;', 'bulk-move' ); ?></option>
571
						</select>
572
					</td>
573
				</tr>
574
			</table>
575
576
			<p class="bm_ct_filters">
577
				<?php _e( 'If the post contains other terms, then', 'bulk-move' ); ?>
578
				<input type="radio" name="smbm_mbct_overwrite" value="overwrite" checked><?php _e( 'Remove them', 'bulk-move' ); ?>
579
				<input type="radio" name="smbm_mbct_overwrite" value="no-overwrite"><?php _e( "Don't remove them", 'bulk-move' ); ?>
580
			</p>
581
582
		</fieldset>
583
584
		<p class="submit bm_ct_submit">
585
			<button type="submit" name="bm_action" value="move_custom_taxonomy" class="button-primary"><?php _e( 'Bulk Move ', 'bulk-move' ); ?>&raquo;</button>
586
		</p>
587
588
		<!-- Custom Taxonomy end-->
589
		<?php
590
	}
591
592
	/**
593
	 * Move posts from one custom taxonomy to another.
594
	 *
595
	 * @since 1.3.0
596
	 */
597
	public static function move_custom_taxonomy() {
598
		if ( ! check_admin_referer( 'sm-bulk-move-posts', 'sm-bulk-move-posts-nonce' ) ) {
599
			return;
600
		}
601
602
		do_action( 'bm_pre_request_handler' );
603
604
		$wp_query = new WP_Query();
605
		$bm       = BULK_MOVE();
606
607
		$old_term   = absint( $_POST['smbm_mbct_selected_term'] );
608
		$taxonomy   = $_POST['smbm_mbct_taxonomy'];
609
		$post_types = array( $_POST['smbm_mbct_post_type'] );
610
611
		$new_term = ( -1 === $_POST['smbm_mbct_mapped_term'] ) ? -1 : absint( $_POST['smbm_mbct_mapped_term'] );
612
613
		$posts_count = 0 ;
614
615
		if ( -1 !== $old_term ) {
616
			foreach ( $post_types as $post_type ) {
617
				$posts_args = array(
618
					'tax_query' => array(
619
						array(
620
							'taxonomy' => $taxonomy,
621
							'field'    => 'term_id',
622
							'terms'    => $old_term,
623
						),
624
					),
625
					'post_type' => $post_type,
626
					'nopaging'  => 'true',
627
				);
628
629
				$posts = $wp_query->query( $posts_args );
630
				$posts_count += count( $posts );
631
632
				foreach ( $posts as $post ) {
633
634
					if ( -1 !== $new_term ) {
635
						if ( isset( $_POST['smbm_mbct_overwrite'] ) && 'overwrite' == $_POST['smbm_mbct_overwrite'] ) {
636
							$is_append_terms = false;
637
						} else {
638
							$is_append_terms = true;
639
						}
640
						wp_set_object_terms( $post->ID, $new_term, $taxonomy, $is_append_terms );
641
					} else {
642
						wp_remove_object_terms( $post->ID, $old_term, $taxonomy );
643
					}
644
				}
645
			}
646
		}
647
648
		/* translators: 1 number of posts deleted, 2 the taxonomy from which the posts were deleted */
649
		$bm->msg = sprintf( _n( 'Moved %1$d post from the selected %2$s taxonomy', 'Moved %1$d posts from the selected %2$s taxonomy', $posts_count, 'bulk-move' ), $posts_count, $taxonomy );
650
	}
651
}
652
653
add_action( 'bm_pre_request_handler'  , array( 'Bulk_Move_Posts', 'change_timeout' ) );
654
add_action( 'bm_move_cats'            , array( 'Bulk_Move_Posts', 'move_cats' ) );
655
add_action( 'bm_move_tags'            , array( 'Bulk_Move_Posts', 'move_tags' ) );
656
add_action( 'bm_move_category_by_tag' , array( 'Bulk_Move_Posts', 'move_category_by_tag' ) );
657
add_action( 'bm_save_timeout'         , array( 'Bulk_Move_Posts', 'save_timeout' ) );
658
add_action( 'bm_move_custom_taxonomy' , array( 'Bulk_Move_Posts', 'move_custom_taxonomy' ) );
659
add_action( 'wp_ajax_load_custom_taxonomy_by_post_type', array( 'Bulk_Move_Posts', 'load_custom_taxonomy_by_post_type' ) );
660
add_action( 'wp_ajax_load_custom_terms_by_taxonomy', array( 'Bulk_Move_Posts', 'load_custom_terms_by_taxonomy' ) );
661