Passed
Pull Request — dev/6.1.0 (#564)
by
unknown
80:33 queued 76:35
created

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