Passed
Push — 701-fix/make-query-overrider-g... ( 81ac3b )
by
unknown
05:04
created

DeleteCommentMetaModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
ccs 15
cts 15
cp 1
crap 1
rs 9.7998
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Metas\Modules;
4
5
use BulkWP\BulkDelete\Core\Metas\MetasModule;
6
use BulkWP\BulkDelete\Core\Metas\QueryOverriders\DateQueryOverrider;
7
8 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
9
10
/**
11
 * Delete Comment Meta Module.
12
 *
13
 * @since 6.0.0
14
 */
15
class DeleteCommentMetaModule extends MetasModule {
16
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
17 24
	protected function initialize() {
18 24
		$this->field_slug    = 'comment_meta';
19 24
		$this->meta_box_slug = 'bd-comment-meta';
20 24
		$this->action        = 'delete_comment_meta';
21 24
		$this->cron_hook     = 'do-bulk-delete-comment-meta';
22 24
		$this->messages      = array(
23 24
			'box_label'         => __( 'Bulk Delete Comment Meta', 'bulk-delete' ),
24 24
			'scheduled'         => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
25 24
			'cron_label'        => __( 'Delete Comment Meta', 'bulk-delete' ),
26 24
			'confirm_deletion'  => __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' ),
27 24
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the comment meta fields that match the selected filters?', 'bulk-delete' ),
28 24
			'validation_error'  => __( 'Please enter meta key', 'bulk-delete' ),
29
			/* translators: 1 Number of comments deleted */
30 24
			'deleted_one'       => __( 'Deleted comment meta field from %d comment', 'bulk-delete' ),
31
			/* translators: 1 Number of comments deleted */
32 24
			'deleted_multiple'  => __( 'Deleted comment meta field from %d comments', 'bulk-delete' ),
33
		);
34
35 24
		$this->register_cron_hooks();
36
	}
37
38
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
39 24
	public function register( $hook_suffix, $page_slug ) {
40 24
		parent::register( $hook_suffix, $page_slug );
41
42 24
		add_action( 'bd_delete_comment_meta_form', array( $this, 'add_filtering_options' ) );
43 24
		add_filter( 'bd_delete_comment_meta_options', array( $this, 'process_filtering_options' ), 10, 2 );
44
	}
45
46
	/**
47
	 * Register additional module specific hooks that are needed in cron jobs.
48
	 *
49
	 * During a cron request, the register method is not called. So these hooks should be registered separately.
50
	 *
51
	 * @since 6.0.2
52
	 */
53 24
	protected function register_cron_hooks() {
54 24
		add_filter( 'bd_delete_comment_meta_query', array( $this, 'change_meta_query' ), 10, 2 );
55
	}
56
57
	/**
58
	 * Render the Delete Comment Meta box.
59
	 */
60
	public function render() {
61
		?>
62
		<!-- Comment Meta box start-->
63
		<fieldset class="options">
64
			<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4>
65
			<table class="optiontable">
66
				<?php $this->render_post_type_with_status( false ); ?>
67
			</table>
68
69
			<h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4>
70
			<table class="optiontable">
71
				<tr>
72
					<td>
73
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" class="use-value" value="false" type="radio" checked>
74
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on comment meta key name only', 'bulk-delete' ); ?></label>
75
					</td>
76
				</tr>
77
78
				<tr>
79
					<td>
80
						<input type="radio" class="use-value" value="true" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value">
81
82
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value"><?php echo __( 'Delete based on comment meta key name and value', 'bulk-delete' ); ?></label>
83
					</td>
84
				</tr>
85
86
				<tr>
87
					<td>
88
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label>
89
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>" class="validate">
90
					</td>
91
				</tr>
92
			</table>
93
94
			<?php
95
			/**
96
			 * Add more fields to the delete comment meta field form.
97
			 * This hook can be used to add more fields to the delete comment meta field form.
98
			 *
99
			 * @since 5.4
100
			 */
101
			do_action( 'bd_delete_comment_meta_form' );
102
			?>
103
			<table class="optiontable">
104
				<tr>
105
					<td colspan="2">
106
						<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
107
					</td>
108
				</tr>
109
110
				<?php $this->render_restrict_settings( 'comments' ); ?>
111
				<?php $this->render_limit_settings(); ?>
112
				<?php $this->render_cron_settings(); ?>
113
114
			</table>
115
		</fieldset>
116
117
		<?php $this->render_submit_button(); ?>
118
119
		<!-- Comment Meta box end-->
120
		<?php
121
	}
122
123
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
124
	protected function convert_user_input_to_options( $request, $options ) {
125
		$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug ) );
126
127
		$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false );
128
		$options['meta_key']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) );
129
130
		/**
131
		 * Delete comment-meta delete options filter.
132
		 *
133
		 * This filter is for processing filtering options for deleting comment meta.
134
		 *
135
		 * @since 5.4
136
		 */
137
		return apply_filters( 'bd_delete_comment_meta_options', $options, $request );
138
	}
139
140
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
141 24
	protected function do_delete( $options ) {
142 24
		$args = $this->get_post_type_and_status_args( $options['post_type'] );
143
144 24
		if ( $options['limit_to'] > 0 ) {
145 1
			$args['number'] = $options['limit_to'];
146
		}
147
148 24
		if ( $options['restrict'] ) {
149 2
			$args['date_query'] = array(
150
				array(
151 2
					'column'            => 'comment_date',
152 2
					$options['date_op'] => "{$options['days']} day ago",
153
				),
154
			);
155
		}
156
157 24
		if ( $options['use_value'] ) {
158 17
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
159
		} else {
160 7
			$args['meta_key'] = $options['meta_key'];
161
		}
162
163 24
		$meta_deleted = 0;
164 24
		$comments     = get_comments( $args );
165 24
		do_action( 'bd_after_meta_query' );
166
167 24
		foreach ( $comments as $comment ) {
168
			// Todo: Don't delete all meta rows if there are duplicate meta keys.
169
			// See https://github.com/sudar/bulk-delete/issues/515 for details.
170 24
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
171 24
				$meta_deleted ++;
172
			}
173
		}
174
175 24
		return $meta_deleted;
176
	}
177
178
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
179
	protected function append_to_js_array( $js_array ) {
180
		$js_array['validators'][ $this->action ] = 'validateTextbox';
181
182
		return $js_array;
183
	}
184
185
	/**
186
	 * Append filtering options to the delete comment meta form.
187
	 *
188
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
189
	 *
190
	 * @since 0.1 of Bulk Delete Comment Meta add-on
191
	 */
192
	public function add_filtering_options() {
193
		?>
194
		<table class="optiontable" style="display:none;">
195
			<tr>
196
				<td>
197
					<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?>
198
					<?php $this->render_data_types_dropdown(); ?>
199
					<?php
200
						$operators = array( '=', '!=', '<', '<=', '>', '>=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' );
201
					?>
202
					<?php $this->render_numeric_operators_dropdown( 'numeric', $operators ); ?>
203
					<?php
204
						$operators = array( '=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'STARTS_WITH', 'ENDS_WITH', 'EXISTS' );
205
					?>
206
					<?php $this->render_string_operators_dropdown( 'string', $operators ); ?>
207
					<?php
208
						$operators = array( '=', '!=', '<', '<=', '>', '>=', 'EXISTS' );
209
					?>
210
					<?php $this->render_numeric_operators_dropdown( 'date', $operators ); ?>
211
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
212
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" class="date-picker">
213
					<span class="date-fields">
214
						<?php _e( 'Or', 'bulk-delete' ); ?>
215
						<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_relative_date" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_relative_date" class="relative-date-fields">
216
							<option value=""><?php _e( 'Select Relative date', 'bulk-delete' ); ?></option>
217
							<option value="yesterday"><?php _e( 'Yesterday', 'bulk-delete' ); ?></option>
218
							<option value="today"><?php _e( 'Today', 'bulk-delete' ); ?></option>
219
							<option value="tomorrow"><?php _e( 'Tomorrow', 'bulk-delete' ); ?></option>
220
							<option value="custom"><?php _e( 'Custom', 'bulk-delete' ); ?></option>
221
						</select>
222
						<?php echo apply_filters( 'bd_help_tooltip', '', __( 'You can select a date or enter a date which is relative to today.', 'bulk-delete' ) ); ?>
223
					</span>
224
					<span class="custom-date-fields">
225
						<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_unit" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_unit" style="width: 5%;">
226
						<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_type">
227
							<option value="day"><?php _e( 'Day', 'bulk-delete' ); ?></option>
228
							<option value="week"><?php _e( 'Week', 'bulk-delete' ); ?></option>
229
							<option value="month"><?php _e( 'Month', 'bulk-delete' ); ?></option>
230
							<option value="year"><?php _e( 'Year', 'bulk-delete' ); ?></option>
231
						</select>
232
					</span>
233
				</td>
234
			</tr>
235
			<tr class="date-format-fields">
236
				<td colspan="2">
237
					<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_format">
238
						<?php _e( 'Meta value date format', 'bulk-delete' ); ?>
239
					</label>
240
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_format" placeholder="%Y-%m-%d">
241
					<?php echo apply_filters( 'bd_help_tooltip', '', __( "If you leave date format blank, then '%Y-%m-%d', will be assumed.", 'bulk-delete' ) ); ?>
242
					<p>
243
						<?php
244
						printf(
245
							/* translators: 1 Mysql Format specifier url.  */
246
							__( 'If you are storing the date in a format other than <em>YYYY-MM-DD</em> then enter the date format using <a href="%s" target="_blank" rel="noopener noreferrer">Mysql format specifiers</a>.', 'bulk-delete' ),
247
							'https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format'
248
						);
249
						?>
250
					</p>
251
				</td>
252
			</tr>
253
		</table>
254
		<?php
255
	}
256
257
	/**
258
	 * Process additional delete options.
259
	 *
260
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
261
	 *
262
	 * @since 0.1 of Bulk Delete Comment Meta add-on
263
	 *
264
	 * @param array $delete_options Delete options array.
265
	 * @param array $post           The POST array.
266
	 *
267
	 * @return array Processed delete options array.
268
	 */
269
	public function process_filtering_options( $delete_options, $post ) {
270
		if ( 'true' === bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) {
271
			$delete_options['meta_op']                = bd_array_get( $post, 'smbd_' . $this->field_slug . '_operator', '=' );
272
			$delete_options['meta_type']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' );
273
			$delete_options['meta_value']             = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' );
274
			$delete_options['relative_date']          = bd_array_get( $post, 'smbd_' . $this->field_slug . '_relative_date', '' );
275
			$delete_options['date_unit']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_unit', '' );
276
			$delete_options['date_type']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_type', '' );
277
			$delete_options['meta_value_date_format'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_format' );
278
			$delete_options['whose_meta']             = 'comment';
279
		}
280
281
		return $delete_options;
282
	}
283
284
	/**
285
	 * Change the meta query.
286
	 *
287
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
288
	 *
289
	 * @since 0.1 of Bulk Delete Comment Meta add-on
290
	 *
291
	 * @param array $meta_query     Meta query.
292
	 * @param array $delete_options List of options chosen by the user.
293
	 *
294
	 * @return array Modified meta query.
295
	 */
296 17
	public function change_meta_query( $meta_query, $delete_options ) {
297
		$query_vars = array(
298 17
			'key'     => $delete_options['meta_key'],
299 17
			'compare' => $delete_options['meta_op'],
300
		);
301 17
		if ( array_key_exists( 'meta_type', $delete_options ) ) {
302 17
			$query_vars['type'] = $delete_options['meta_type'];
303
		}
304 17
		if ( in_array( $delete_options['meta_op'], array( 'EXISTS', 'NOT EXISTS' ), true ) ) {
305 1
			$meta_query = array( $query_vars );
306
307 1
			return $meta_query;
308
		}
309 16
		if ( 'DATE' === $delete_options['meta_type'] ) {
310 7
			$bd_date_handler = new DateQueryOverrider();
311 7
			$meta_query      = $bd_date_handler->get_query( $delete_options );
312
313 7
			return $meta_query;
314
		}
315 9
		switch ( $delete_options['meta_op'] ) {
316
			case 'IN':
317 2
				$meta_value = explode( ',', $delete_options['meta_value'] );
318 2
				break;
319
			case 'BETWEEN':
320 1
				$meta_value = explode( ',', $delete_options['meta_value'] );
321 1
				break;
322
			default:
323 6
				$meta_value = $delete_options['meta_value'];
324
		}
325
326 9
		$query_vars['value'] = $meta_value;
327 9
		$meta_query          = array( $query_vars );
328
329 9
		return $meta_query;
330
	}
331
332
	/**
333
	 * Hook handler.
334
	 *
335
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
336
	 *
337
	 * @since 0.1 of Bulk Delete Comment Meta add-on
338
	 *
339
	 * @param array $delete_options Delete options array.
340
	 */
341
	public function do_delete_comment_meta( $delete_options ) {
342
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
343
		$count = $this->delete( $delete_options );
344
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
345
	}
346
}
347