Passed
Push — analysis-D2GvQJ ( 9b001d )
by Sudar
71:50 queued 38s
created

DeleteCommentMetaModule::change_meta_query()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 10
nop 2
dl 0
loc 34
rs 8.9137
c 0
b 0
f 0
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
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
	protected function initialize() {
18
		$this->field_slug    = 'comment_meta';
19
		$this->meta_box_slug = 'bd-comment-meta';
20
		$this->action        = 'delete_comment_meta';
21
		$this->cron_hook     = 'do-bulk-delete-comment-meta';
22
		$this->messages      = array(
23
			'box_label'         => __( 'Bulk Delete Comment Meta', 'bulk-delete' ),
24
			'scheduled'         => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
25
			'cron_label'        => __( 'Delete Comment Meta', 'bulk-delete' ),
26
			'confirm_deletion'  => __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' ),
27
			'confirm_scheduled' => __( 'Are you sure you want to schedule deletion for all the comment meta fields that match the selected filters?', 'bulk-delete' ),
28
			'validation_error'  => __( 'Please enter meta key', 'bulk-delete' ),
29
			/* translators: 1 Number of comments deleted */
30
			'deleted_one'       => __( 'Deleted comment meta field from %d comment', 'bulk-delete' ),
31
			/* translators: 1 Number of comments deleted */
32
			'deleted_multiple'  => __( 'Deleted comment meta field from %d comments', 'bulk-delete' ),
33
		);
34
35
		$this->register_cron_hooks();
36
	}
37
38
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
39
	public function register( $hook_suffix, $page_slug ) {
40
		parent::register( $hook_suffix, $page_slug );
41
42
		add_action( 'bd_delete_comment_meta_form', array( $this, 'add_filtering_options' ) );
43
		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
	protected function register_cron_hooks() {
54
		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
	protected function do_delete( $options ) {
142
		$args = $this->get_post_type_and_status_args( $options['post_type'] );
143
144
		if ( $options['limit_to'] > 0 ) {
145
			$args['number'] = $options['limit_to'];
146
		}
147
148
		if ( $options['restrict'] ) {
149
			$args['date_query'] = array(
150
				array(
151
					'column'            => 'comment_date',
152
					$options['date_op'] => "{$options['days']} day ago",
153
				),
154
			);
155
		}
156
157
		if ( $options['use_value'] ) {
158
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
159
		} else {
160
			$args['meta_key'] = $options['meta_key'];
161
		}
162
163
		$meta_deleted = 0;
164
		$comments     = get_comments( $args );
165
		do_action( 'bd_after_meta_query' );
166
167
		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
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
171
				$meta_deleted ++;
172
			}
173
		}
174
175
		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 $this->render_numeric_operators_dropdown(); ?>
200
					<?php $this->render_string_operators_dropdown(); ?>
201
					<?php
202
						$operators = array( '=', '!=', '>', '<=', '>', '>=', 'EXISTS', 'NOT EXISTS' );
203
						$class     = 'date';
204
					?>
205
					<?php $this->render_numeric_operators_dropdown( $class, $operators ); ?>
206
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
207
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" class="date-picker">
208
					<span class="date-fields">
209
						<?php _e( 'Or', 'bulk-delete' ); ?>
210
						<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">
211
							<option value=""><?php _e( 'Select Relative date', 'bulk-delete' ); ?></option>
212
							<option value="yesterday"><?php _e( 'Yesterday', 'bulk-delete' ); ?></option>
213
							<option value="today"><?php _e( 'Today', 'bulk-delete' ); ?></option>
214
							<option value="tomorrow"><?php _e( 'Tomorrow', 'bulk-delete' ); ?></option>
215
							<option value="custom"><?php _e( 'Custom', 'bulk-delete' ); ?></option>
216
						</select>
217
						<?php echo apply_filters( 'bd_help_tooltip', '', __( 'You can select a date or enter a date which is relative to today.', 'bulk-delete' ) ); ?>
218
					</span>
219
					<span class="custom-date-fields">
220
						<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%;">
221
						<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_type">
222
							<option value="day"><?php _e( 'Day', 'bulk-delete' ); ?></option>
223
							<option value="week"><?php _e( 'Week', 'bulk-delete' ); ?></option>
224
							<option value="month"><?php _e( 'Month', 'bulk-delete' ); ?></option>
225
							<option value="year"><?php _e( 'Year', 'bulk-delete' ); ?></option>
226
						</select>
227
					</span>
228
				</td>
229
			</tr>
230
			<tr class="date-format-fields">
231
				<td colspan="2">
232
					<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_format">
233
						<?php _e( 'Meta value date format', 'bulk-delete' ); ?>
234
					</label>
235
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_format" placeholder="%Y-%m-%d">
236
					<?php echo apply_filters( 'bd_help_tooltip', '', __( "If you leave date format blank, then '%Y-%m-%d', will be assumed.", 'bulk-delete' ) ); ?>
237
					<p>
238
						<?php
239
						printf(
240
							/* translators: 1 Mysql Format specifier url.  */
241
							__( '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' ),
242
							'https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format'
243
						);
244
						?>
245
					</p>
246
				</td>
247
			</tr>
248
		</table>
249
		<?php
250
	}
251
252
	/**
253
	 * Process additional delete options.
254
	 *
255
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
256
	 *
257
	 * @since 0.1 of Bulk Delete Comment Meta add-on
258
	 *
259
	 * @param array $delete_options Delete options array.
260
	 * @param array $post           The POST array.
261
	 *
262
	 * @return array Processed delete options array.
263
	 */
264
	public function process_filtering_options( $delete_options, $post ) {
265
		if ( 'true' === bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) {
266
			$delete_options['meta_op']                = bd_array_get( $post, 'smbd_' . $this->field_slug . '_operator', '=' );
267
			$delete_options['meta_type']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' );
268
			$delete_options['meta_value']             = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' );
269
			$delete_options['relative_date']          = bd_array_get( $post, 'smbd_' . $this->field_slug . '_relative_date', '' );
270
			$delete_options['date_unit']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_unit', '' );
271
			$delete_options['date_type']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_type', '' );
272
			$delete_options['meta_value_date_format'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_format' );
273
		}
274
275
		return $delete_options;
276
	}
277
278
	/**
279
	 * Change the meta query.
280
	 *
281
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
282
	 *
283
	 * @since 0.1 of Bulk Delete Comment Meta add-on
284
	 *
285
	 * @param array $meta_query     Meta query.
286
	 * @param array $delete_options List of options chosen by the user.
287
	 *
288
	 * @return array Modified meta query.
289
	 */
290
	public function change_meta_query( $meta_query, $delete_options ) {
291
		$query_vars = array(
292
			'key'     => $delete_options['meta_key'],
293
			'compare' => $delete_options['meta_op'],
294
		);
295
		if ( array_key_exists( 'meta_type', $delete_options ) ) {
296
			$query_vars['type'] = $delete_options['meta_type'];
297
		}
298
		if ( in_array( $delete_options['meta_op'], array( 'EXISTS', 'NOT EXISTS' ), true ) ) {
299
			$meta_query = array( $query_vars );
300
301
			return $meta_query;
302
		}
303
		if ( 'DATE' === $delete_options['meta_type'] ) {
304
			$bd_date_handler = new DateQueryOverrider();
305
			$meta_query      = $bd_date_handler->get_query( $delete_options );
306
307
			return $meta_query;
308
		}
309
		switch ( $delete_options['meta_op'] ) {
310
			case 'IN':
311
				$meta_value = explode( ',', $delete_options['meta_value'] );
312
				break;
313
			case 'BETWEEN':
314
				$meta_value = explode( ',', $delete_options['meta_value'] );
315
				break;
316
			default:
317
				$meta_value = $delete_options['meta_value'];
318
		}
319
320
		$query_vars['value'] = $meta_value;
321
		$meta_query          = array( $query_vars );
322
323
		return $meta_query;
324
	}
325
326
	/**
327
	 * Hook handler.
328
	 *
329
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
330
	 *
331
	 * @since 0.1 of Bulk Delete Comment Meta add-on
332
	 *
333
	 * @param array $delete_options Delete options array.
334
	 */
335
	public function do_delete_comment_meta( $delete_options ) {
336
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
337
		$count = $this->delete( $delete_options );
338
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
339
	}
340
}
341