Completed
Push — 516-fix/enhance-data-type-and-... ( 4f396a...11e75d )
by
unknown
07:46 queued 03:16
created

DeleteCommentMetaModule   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 8
Bugs 2 Features 1
Metric Value
eloc 158
c 8
b 2
f 1
dl 0
loc 316
rs 10
wmc 21

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