Passed
Push — analysis-8AOO2l ( 192129 )
by Sudar
30:45 queued 15:47
created

DeleteCommentMetaModule::append_to_js_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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