Completed
Push — 516-fix/enhance-data-type-and-... ( dbb4dd...931ffd )
by
unknown
08:02 queued 05:10
created

DeleteCommentMetaModule   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 323
Duplicated Lines 0 %

Importance

Changes 8
Bugs 2 Features 1
Metric Value
eloc 159
dl 0
loc 323
rs 10
c 8
b 2
f 1
wmc 22

11 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 19 1
A register_cron_hooks() 0 2 1
A register() 0 5 1
A append_to_js_array() 0 4 1
A convert_user_input_to_options() 0 14 1
A add_filtering_options() 0 54 1
B change_meta_query() 0 34 6
A do_delete_comment_meta() 0 4 1
A render() 0 59 1
A process_filtering_options() 0 12 2
A do_delete() 0 34 6
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
166
		foreach ( $comments as $comment ) {
167
			// Todo: Don't delete all meta rows if there are duplicate meta keys.
168
			// See https://github.com/sudar/bulk-delete/issues/515 for details.
169
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
170
				$meta_deleted ++;
171
			}
172
		}
173
174
		return $meta_deleted;
175
	}
176
177
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
178
	protected function append_to_js_array( $js_array ) {
179
		$js_array['validators'][ $this->action ] = 'validateTextbox';
180
181
		return $js_array;
182
	}
183
184
	/**
185
	 * Append filtering options to the delete comment meta form.
186
	 *
187
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
188
	 *
189
	 * @since 0.1 of Bulk Delete Comment Meta add-on
190
	 */
191
	public function add_filtering_options() {
192
		?>
193
		<table class="optiontable" style="display:none;">
194
			<tr>
195
				<td>
196
					<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?>
197
					<?php $this->render_data_types_dropdown(); ?>
198
					<?php $this->render_numeric_operators_dropdown(); ?>
199
					<?php $this->render_string_operators_dropdown(); ?>
200
					<?php
201
						$operators = array( '=', '!=', '>', '<=', '>', '>=', 'EXISTS', 'NOT EXISTS' );
202
						$class     = 'date';
203
					?>
204
					<?php $this->render_numeric_operators_dropdown( $class, $operators ); ?>
205
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
206
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" class="date-picker">
207
					<span class="date-fields">
208
						<?php _e( 'Or', 'bulk-delete' ); ?>
209
						<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">
210
							<option value=""><?php _e( 'Select Relative date', 'bulk-delete' ); ?></option>
211
							<option value="yesterday"><?php _e( 'Yesterday', 'bulk-delete' ); ?></option>
212
							<option value="today"><?php _e( 'Today', 'bulk-delete' ); ?></option>
213
							<option value="tomorrow"><?php _e( 'Tomorrow', 'bulk-delete' ); ?></option>
214
							<option value="custom"><?php _e( 'Custom', 'bulk-delete' ); ?></option>
215
						</select>
216
						<?php echo apply_filters( 'bd_help_tooltip', '', __( 'You can select a date or enter a date which is relative to today.', 'bulk-delete' ) ); ?>
217
					</span>
218
					<span class="custom-date-fields">
219
						<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%;">
220
						<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_type">
221
							<option value="day"><?php _e( 'Day', 'bulk-delete' ); ?></option>
222
							<option value="week"><?php _e( 'Week', 'bulk-delete' ); ?></option>
223
							<option value="month"><?php _e( 'Month', 'bulk-delete' ); ?></option>
224
							<option value="year"><?php _e( 'Year', 'bulk-delete' ); ?></option>
225
						</select>
226
					</span>
227
				</td>
228
			</tr>
229
			<tr class="date-format-fields">
230
				<td colspan="2">
231
					<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_format">
232
						<?php _e( 'Meta value date format', 'bulk-delete' ); ?>
233
					</label>
234
					<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_date_format" placeholder="%Y-%m-%d">
235
					<?php echo apply_filters( 'bd_help_tooltip', '', __( "If you leave date format blank, then '%Y-%m-%d', will be assumed.", 'bulk-delete' ) ); ?>
236
					<p>
237
						<?php
238
						printf(
239
							/* translators: 1 Mysql Format specifier url.  */
240
							__( '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' ),
241
							'https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format'
242
						);
243
						?>
244
					</p>
245
				</td>
246
			</tr>
247
		</table>
248
		<?php
249
	}
250
251
	/**
252
	 * Process additional delete options.
253
	 *
254
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
255
	 *
256
	 * @since 0.1 of Bulk Delete Comment Meta add-on
257
	 *
258
	 * @param array $delete_options Delete options array.
259
	 * @param array $post           The POST array.
260
	 *
261
	 * @return array Processed delete options array.
262
	 */
263
	public function process_filtering_options( $delete_options, $post ) {
264
		if ( 'true' === bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) {
265
			$delete_options['meta_op']                = bd_array_get( $post, 'smbd_' . $this->field_slug . '_operator', '=' );
266
			$delete_options['meta_type']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' );
267
			$delete_options['meta_value']             = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' );
268
			$delete_options['relative_date']          = bd_array_get( $post, 'smbd_' . $this->field_slug . '_relative_date', '' );
269
			$delete_options['date_unit']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_unit', '' );
270
			$delete_options['date_type']              = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_type', '' );
271
			$delete_options['meta_value_date_format'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_date_format' );
272
		}
273
274
		return $delete_options;
275
	}
276
277
	/**
278
	 * Change the meta query.
279
	 *
280
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
281
	 *
282
	 * @since 0.1 of Bulk Delete Comment Meta add-on
283
	 *
284
	 * @param array $meta_query     Meta query.
285
	 * @param array $delete_options List of options chosen by the user.
286
	 *
287
	 * @return array Modified meta query.
288
	 */
289
	public function change_meta_query( $meta_query, $delete_options ) {
290
		$query_vars = array(
291
			'key'     => $delete_options['meta_key'],
292
			'compare' => $delete_options['meta_op'],
293
		);
294
		if ( array_key_exists( 'meta_type', $delete_options ) ) {
295
			$query_vars['type'] = $delete_options['meta_type'];
296
		}
297
		if ( in_array( $delete_options['meta_op'], array( 'EXISTS', 'NOT EXISTS' ), true ) ) {
298
			$meta_query = array( $query_vars );
299
300
			return $meta_query;
301
		}
302
		if ( 'DATE' === $delete_options['meta_type'] ) {
303
			$bd_date_handler = new DateQueryOverrider();
304
			$meta_query      = $bd_date_handler->get_query( $delete_options );
305
306
			return $meta_query;
307
		}
308
		switch ( $delete_options['meta_op'] ) {
309
			case 'IN':
310
				$meta_value = explode( ',', $delete_options['meta_value'] );
311
				break;
312
			case 'BETWEEN':
313
				$meta_value = explode( ',', $delete_options['meta_value'] );
314
				break;
315
			default:
316
				$meta_value = $delete_options['meta_value'];
317
		}
318
319
		$query_vars['value'] = $meta_value;
320
		$meta_query          = array( $query_vars );
321
322
		return $meta_query;
323
	}
324
325
	/**
326
	 * Hook handler.
327
	 *
328
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
329
	 *
330
	 * @since 0.1 of Bulk Delete Comment Meta add-on
331
	 *
332
	 * @param array $delete_options Delete options array.
333
	 */
334
	public function do_delete_comment_meta( $delete_options ) {
335
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
336
		$count = $this->delete( $delete_options );
337
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
338
	}
339
}
340