Completed
Push — master ( d4e339...240d62 )
by Sudar
01:15
created

load_custom_taxonomy_by_post_type()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
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
		$bulk_move = BULK_MOVE();
446
		check_ajax_referer( $bulk_move::BOX_CUSTOM_TERMS_NONCE, 'security' );
447
		$post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( $_POST['post_type'] ) : 'post';
448
449
		$taxonomies = get_object_taxonomies( $post_type );
450
451
		ob_start();
452
		?>
453
		<option class="level-0" value="select"><?php _e( 'Select Taxonomy&nbsp;&nbsp;', 'bulk-move' ); ?></option>
454
455
		<?php foreach ( $taxonomies as $taxonomy ) : ?>
456
			<option class="level-0" value="<?php echo esc_attr( $taxonomy ); ?>">
457
				<?php echo esc_html( $taxonomy ); ?>
458
			</option>
459
		<?php endforeach; ?>
460
461
		<?php
462
		$data = ob_get_clean();
463
		wp_send_json_success( $data );
464
	}
465
466
	/**
467
	 * Loads the custom Terms by Taxonomy.
468
	 *
469
	 * @since 1.3.0
470
	 */
471
	public static function load_custom_terms_by_taxonomy() {
472
		$bulk_move = BULK_MOVE();
473
		check_ajax_referer( $bulk_move::BOX_CUSTOM_TERMS_NONCE, 'security' );
474
		$taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : 'category';
475
476
		$args = array(
477
			'taxonomy'   => $taxonomy,
478
			'hide_empty' => false,
479
			'orderby'    => 'name',
480
		);
481
		$terms = get_terms( $args );
482
483
		$select_term = '<option class="level-0" value="-1">' . __( 'Select Term&nbsp;&nbsp;', '"bulk-move' ) . '</option>';
484
		$map_term    = '<option class="level-0" value="-1">' . __( 'Remove Term&nbsp;&nbsp;', 'bullk-move' ) . '</option>';
485
486
		ob_start();
487
		?>
488
489
		<?php foreach ( $terms as $term ) : ?>
490
			<option class="level-0" value="<?php echo absint( $term->term_id ); ?>">
491
				<?php echo esc_html( $term->name ); ?>&nbsp;&nbsp;(<?php echo absint( $term->count ); ?>)
492
			</option>
493
		<?php endforeach; ?>
494
495
		<?php
496
		$content = ob_get_clean();
497
		$select_term .= $content;
498
		$map_term .= $content;
499
500
		$data = array( 'select_term' => $select_term, 'map_term' => $map_term );
501
		wp_send_json_success( $data );
502
	}
503
504
	/**
505
	 * Render move terms box.
506
	 *
507
	 * @since 1.3.0
508
	 */
509
	public static function render_move_by_custom_taxonomy_box() {
510
511
		if ( Bulk_Move_Util::is_posts_box_hidden( Bulk_Move::BOX_CATEGORY ) ) {
512
			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 );
513
514
			return;
515
		}
516
		?>
517
		<!-- Custom Taxonomy Start-->
518
		<h4><?php _e( 'Select the post type to show its taxonomy. On the left side, select the term whose posts you want to move. On the right side select the term to which you want the posts to be moved.', 'bulk-move' ); ?></h4>
519
520
		<fieldset class="options">
521
			<table class="optiontable">
522
				<tr>
523
					<td scope="row" colspan="2">
524
						<?php _e( 'Select the post type to show its custom taxonomy.', 'bulk-move' ); ?>
525
					</td>
526
					<td scope="row">
527
				</tr>
528
				<tr>
529
					<td scope="row" colspan="2">
530
						<?php
531
						$custom_post_types_args = array( '_builtin' => false );
532
						$custom_post_types      = get_post_types( $custom_post_types_args );
533
						?>
534
						<?php if ( count( $custom_post_types ) === 0 ) : ?>
535
							<p>
536
								<span class="error-notice"><?php _e( 'You have no custom post type registered.', 'bulk-move' ); ?></span>
537
							</p>
538
						<?php endif; ?>
539
						<p>
540
							<select name="smbm_mbct_post_type" id="smbm_mbct_post_type">
541
								<option value="select"><?php _e( 'Select Post type', 'bulk-move' ); ?></option>
542
								<?php foreach ( $custom_post_types as $post_type ) : ?>
543
									<option value="<?php echo $post_type; ?>"><?php echo $post_type; ?></option>
544
								<?php endforeach; ?>
545
							</select>
546
						</p>
547
					</td>
548
				</tr>
549
				<tr class="taxonomy-select-row">
550
					<td scope="row" colspan="2">
551
						<?php _e( 'Select taxonomy to show its terms.', 'bulk-move' ); ?>
552
					</td>
553
					<td scope="row">
554
				</tr>
555
				<tr class="taxonomy-select-row">
556
					<td scope="row" colspan="2">
557
						<p>
558
							<select name="smbm_mbct_taxonomy" id="smbm_mbct_taxonomy">
559
								<option value="select"><?php _e( 'Select Taxonomy', 'bulk-move' ); ?></option>
560
							</select>
561
						</p>
562
					</td>
563
				</tr>
564
				<tr class="term-select-row">
565
					<td scope="row" colspan="2">
566
						<?php _e( 'Select terms to move its posts.', 'bulk-move' ); ?>
567
					</td>
568
					<td scope="row">
569
				</tr>
570
				<tr class="term-select-row">
571
					<td scope="row" >
572
						<select name="smbm_mbct_selected_term" id="smbm_mbct_selected_term" class="postform">
573
							<option class="level-0" value="-1"><?php _e( ' Select Term&nbsp;&nbsp;', 'bulk-move' ); ?></option>
574
						</select>
575
						==>
576
					</td>
577
					<td scope="row" >
578
						<select name="smbm_mbct_mapped_term" id="smbm_mbct_mapped_term" class="postform">
579
							<option class="level-0" value="-1"><?php _e( 'Remove Term&nbsp;&nbsp;', 'bulk-move' ); ?></option>
580
						</select>
581
					</td>
582
				</tr>
583
584
			</table>
585
			<p>
586
				<?php _e( 'If the post contains other terms, then', 'bulk-move' ); ?>
587
				<input type="radio" name="smbm_mbct_overwrite" value="overwrite" checked><?php _e( 'Remove them', 'bulk-move' ); ?>
588
				<input type="radio" name="smbm_mbct_overwrite" value="no-overwrite"><?php _e( "Don't remove them", 'bulk-move' ); ?>
589
			</p>
590
591
		</fieldset>
592
		<p class="submit">
593
			<button type="submit" name="bm_action" value="move_custom_taxonomy" class="button-primary"><?php _e( 'Bulk Move ', 'bulk-move' ); ?>&raquo;</button>
594
		</p>
595
		<!-- Custom Taxonomy end-->
596
		<?php
597
	}
598
599
	/**
600
	 * Move posts from one custom taxonomy to another.
601
	 *
602
	 * @since 1.3.0
603
	 */
604
	public static function move_custom_taxonomy() {
605
		if ( check_admin_referer( 'sm-bulk-move-posts', 'sm-bulk-move-posts-nonce' ) ) {
606
607
			do_action( 'bm_pre_request_handler' );
608
609
			$wp_query = new WP_Query();
610
			$bm       = BULK_MOVE();
611
612
			// Move by terms.
613
			$old_cat    = absint( $_POST['smbm_mbct_selected_term'] );
614
			$taxonomy   = $_POST['smbm_mbct_taxonomy'];
615
			$post_types = array( $_POST['smbm_mbct_post_type'] );
616
617
			$new_cat = ( -1 === $_POST['smbm_mbct_mapped_term'] ) ? -1 : absint( $_POST['smbm_mbct_mapped_term'] );
618
619
			$posts_count = 0 ;
620
621
			if ( -1 !== $old_cat ) {
622
				foreach ( $post_types as $post_type ) {
623
					$posts_args = array(
624
						'tax_query' => array(
625
							array(
626
								'taxonomy' => $taxonomy,
627
								'field'    => 'term_id',
628
								'terms'    => $old_cat,
629
							),
630
						),
631
						'post_type' => $post_type,
632
						'nopaging'  => 'true',
633
					);
634
635
					$posts = $wp_query->query( $posts_args );
636
					$posts_count += count( $posts );
637
638
					foreach ( $posts as $post ) {
639
640
						if ( -1 !== $new_cat ) {
641
							if ( isset( $_POST['smbm_mbct_overwrite'] ) && 'overwrite' == $_POST['smbm_mbct_overwrite'] ) {
642
								$is_append_terms = false;
643
							} else {
644
								$is_append_terms = true;
645
							}
646
							wp_set_object_terms( $post->ID, $new_cat, $taxonomy, $is_append_terms );
647
						} else {
648
							wp_remove_object_terms( $post->ID, $old_cat, $taxonomy );
649
						}
650
					}
651
				}
652
			}
653
654
			$bm->msg = sprintf( _n( 'Moved %d post from the selected category', 'Moved %d posts from the selected category' , $posts_count, 'bulk-move' ), $posts_count );
655
		}
656
	}
657
}
658
659
// Hooks
660
add_action( 'bm_pre_request_handler'  , array( 'Bulk_Move_Posts', 'change_timeout' ) );
661
add_action( 'bm_move_cats'            , array( 'Bulk_Move_Posts', 'move_cats' ) );
662
add_action( 'bm_move_tags'            , array( 'Bulk_Move_Posts', 'move_tags' ) );
663
add_action( 'bm_move_category_by_tag' , array( 'Bulk_Move_Posts', 'move_category_by_tag' ) );
664
add_action( 'bm_save_timeout'         , array( 'Bulk_Move_Posts', 'save_timeout' ) );
665
add_action( 'bm_move_custom_taxonomy' , array( 'Bulk_Move_Posts', 'move_custom_taxonomy' ) );
666
add_action( 'wp_ajax_load_custom_taxonomy_by_post_type', array( 'Bulk_Move_Posts', 'load_custom_taxonomy_by_post_type' ) );
667
add_action( 'wp_ajax_load_custom_terms_by_taxonomy', array( 'Bulk_Move_Posts', 'load_custom_terms_by_taxonomy' ) );
668