Completed
Push — dev/6.1.0 ( dd2df4...d93012 )
by Sudar
132:54 queued 129:39
created

DeleteTermsByPostCountModule::append_to_js_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Terms\Modules;
4
5
use BulkWP\BulkDelete\Core\Terms\QueryOverriders\DeleteTermsQueryOverrider;
6
use BulkWP\BulkDelete\Core\Terms\TermsModule;
7
8 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
9
10
/**
11
 * Delete Terms by Post Count.
12
 *
13
 * @since 6.0.0
14
 */
15
class DeleteTermsByPostCountModule extends TermsModule {
16
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
17 12
	protected function initialize() {
18 12
		$this->item_type     = 'terms';
19 12
		$this->field_slug    = 'terms_by_post_count';
20 12
		$this->meta_box_slug = 'bd_delete_terms_by_post_count';
21 12
		$this->action        = 'delete_terms_by_post_count';
22 12
		$this->cron_hook     = 'do-bulk-delete-terms-by-post-count';
23 12
		$this->scheduler_url = 'https://bulkwp.com/addons/scheduler-for-deleting-terms/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-s-te';
24 12
		$this->messages      = array(
25 12
			'box_label'         => __( 'Delete Terms by Post Count', 'bulk-delete' ),
26 12
			'scheduled'         => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ),
27 12
			'cron_label'        => __( 'Delete Terms By post count', 'bulk-delete' ),
28 12
			'confirm_deletion'  => __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' ),
29 12
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the terms from the selected condition?', 'bulk-delete' ),
30 12
			'validation_error'  => __( 'Please enter the post count based on which terms should be deleted. A valid post count will be greater than or equal to zero', 'bulk-delete' ),
31
			/* translators: 1 Number of terms deleted */
32 12
			'deleted_one'       => __( 'Deleted %d term with the selected options', 'bulk-delete' ),
33
			/* translators: 1 Number of terms deleted */
34 12
			'deleted_multiple'  => __( 'Deleted %d terms with the selected options', 'bulk-delete' ),
35
		);
36
	}
37
38
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
39
	public function render() {
40
		?>
41
		<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4>
42
		<fieldset class="options">
43
			<table class="optiontable">
44
				<tr><?php $this->render_taxonomy_dropdown(); ?></tr>
45
46
				<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4>
47
				<tr>
48
					<td>
49
					<?php _e( 'Delete Terms if the post count is ', 'bulk-delete' ); ?>
50
					<?php $this->render_operators_dropdown( [ 'equals', 'numeric' ] ); ?>
51
					<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>" placeholder="Post count" min="0" class="validate">
52
						<?php
53
						$markup  = '';
54
						$content = __( 'Post count is the number of posts that are assigned to a term.', 'bulk-delete' );
55
						echo '&nbsp' . bd_generate_help_tooltip( $markup, $content );
56
						?>
57
					</td>
58
				</tr>
59
				<?php $this->render_cron_settings(); ?>
60
			</table>
61
		</fieldset>
62
63
		<?php
64
		$this->render_submit_button();
65
	}
66
67
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
68
	protected function append_to_js_array( $js_array ) {
69
		$js_array['validators'][ $this->action ] = 'validateTextbox';
70
71
		return $js_array;
72
	}
73
74
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
75
	protected function convert_user_input_to_options( $request, $options ) {
76
		$options['operator']   = bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' );
77
		$options['post_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug ) );
78
79
		return $options;
80
	}
81
82
	/**
83
	 * Get the list of terms ids that need to be deleted.
84
	 *
85
	 * Return an empty query array to short-circuit deletion.
86
	 *
87
	 * @param array $options Delete options.
88
	 *
89
	 * @return int[] List of term ids to delete.
90
	 */
91 12
	protected function get_term_ids_to_delete( $options ) {
92
		$query = array(
93 12
			'taxonomy'       => $options['taxonomy'],
94 12
			'bd_operator'    => $options['operator'],
95 12
			'bd_value'       => $options['post_count'],
96 12
			'bd_column_name' => 'count',
97
		);
98
99 12
		$query_overrider = new DeleteTermsQueryOverrider();
100 12
		$query_overrider->load();
101
102 12
		return $this->query_terms( $query );
103
	}
104
}
105