Completed
Pull Request — dev/6.0.0 (#461)
by Rajan
26:22 queued 19:08
created

DeleteCommentMetaModule::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Metas\Modules;
4
5
use BulkWP\BulkDelete\Core\Metas\MetasModule;
6
7 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Delete Comment Meta Module.
11
 */
12
class DeleteCommentMetaModule extends MetasModule {
13
	/**
14
	 * Initialize and setup variables.
15
	 */
16 13
	protected function initialize() {
17 13
		$this->field_slug    = 'comment_meta';
18 13
		$this->meta_box_slug = 'bd-comment-meta';
19 13
		$this->action        = 'delete_comment_meta';
20 13
		$this->cron_hook     = 'do-bulk-delete-comment-meta';
21 13
		$this->messages      = array(
22 13
			'box_label'  => __( 'Bulk Delete Comment Meta', 'bulk-delete' ),
23 13
			'scheduled'  => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
24 13
			'cron_label' => __( 'Delete Comment Meta', 'bulk-delete' ),
25
		);
26 13
	}
27
28
	/**
29
	 * Register.
30
	 *
31
	 * @param string $hook_suffix Page Hook Suffix.
32
	 * @param string $page_slug   Page slug.
33
	 */
34 13
	public function register( $hook_suffix, $page_slug ) {
35 13
		parent::register( $hook_suffix, $page_slug );
36
37 13
		add_action( 'bd_delete_comment_meta_form', array( $this, 'add_filtering_options' ) );
38
39 13
		add_filter( 'bd_delete_comment_meta_options', array( $this, 'process_filtering_options' ), 10, 2 );
40 13
		add_filter( 'bd_delete_comment_meta_query', array( $this, 'change_meta_query' ), 10, 2 );
41 13
	}
42
43
	/**
44
	 * Render the Delete Comment Meta box.
45
	 */
46
	public function render() {
47
		?>
48
		<!-- Comment Meta box start-->
49
		<fieldset class="options">
50
			<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4>
51
			<table class="optiontable">
52
				<?php $this->render_post_type_dropdown(); ?>
53
			</table>
54
55
			<h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4>
56
			<table class="optiontable">
57
				<tr>
58
					<td>
59
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked>
60
						<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>
61
					</td>
62
				</tr>
63
64
				<tr>
65
					<td>
66
						<input type="radio" value="true" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value">
67
68
						<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>
69
					</td>
70
				</tr>
71
72
				<tr>
73
					<td>
74
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label>
75
						<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' ); ?>">
76
					</td>
77
				</tr>
78
			</table>
79
80
			<?php
81
			/**
82
			 * Add more fields to the delete comment meta field form.
83
			 * This hook can be used to add more fields to the delete comment meta field form.
84
			 *
85
			 * @since 5.4
86
			 */
87
			do_action( 'bd_delete_comment_meta_form' );
88
			?>
89
			<table class="optiontable">
90
				<tr>
91
					<td colspan="2">
92
						<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
93
					</td>
94
				</tr>
95
96
				<?php $this->render_restrict_settings(); ?>
97
				<?php $this->render_limit_settings(); ?>
98
				<?php $this->render_cron_settings(); ?>
99
100
			</table>
101
		</fieldset>
102
103
		<?php $this->render_submit_button(); ?>
104
105
		<!-- Comment Meta box end-->
106
		<?php
107
	}
108
109
	/**
110
	 * Process user input and create metabox options.
111
	 *
112
	 * @param array $request Request array.
113
	 * @param array $options User options.
114
	 *
115
	 * @return array User options.
116
	 */
117
	protected function convert_user_input_to_options( $request, $options ) {
118
		$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) );
119
120
		$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false );
121
		$options['meta_key']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) );
122
123
		/**
124
		 * Delete comment-meta delete options filter.
125
		 *
126
		 * This filter is for processing filtering options for deleting comment meta.
127
		 *
128
		 * @since 5.4
129
		 */
130
		return apply_filters( 'bd_delete_comment_meta_options', $options, $request );
131
	}
132
133
	/**
134
	 * Perform the deletion.
135
	 *
136
	 * @param array $options Array of Delete options.
137
	 *
138
	 * @return int Number of items that were deleted.
139
	 */
140 13
	protected function do_delete( $options ) {
141
		$args = array(
142 13
			'post_type' => $options['post_type'],
143
		);
144
145 13
		if ( $options['limit_to'] > 0 ) {
146 1
			$args['number'] = $options['limit_to'];
147
		}
148
149 13
		$op   = $options['date_op'];
150 13
		$days = $options['days'];
151
152 13
		if ( $options['restrict'] ) {
153 2
			$args['date_query'] = array(
154
				array(
155 2
					'column' => 'comment_date',
156 2
					$op      => "{$days} day ago",
157
				),
158
			);
159
		}
160
161 13
		if ( $options['use_value'] ) {
162 8
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
163
		} else {
164 5
			$args['meta_key'] = $options['meta_key'];
165
		}
166
167 13
		$meta_deleted = 0;
168 13
		$comments     = get_comments( $args );
169
170 13
		foreach ( $comments as $comment ) {
171 13
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
172 13
				$meta_deleted ++;
173
			}
174
		}
175
176 13
		return $meta_deleted;
177
	}
178
179
	/**
180
	 * Filter JS Array and add pro hooks.
181
	 *
182
	 * @param array $js_array JavaScript Array.
183
	 *
184
	 * @return array Modified JavaScript Array.
185
	 */
186
	public function filter_js_array( $js_array ) {
187
		$js_array['dt_iterators'][]              = '_' . $this->field_slug;
188
		$js_array['validators'][ $this->action ] = 'noValidation';
189
190
		$js_array['pre_action_msg'][ $this->action ] = 'deleteCMWarning';
191
		$js_array['msg']['deleteCMWarning']          = __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' );
192
193
		return $js_array;
194
	}
195
196
	/**
197
	 * Get Success Message.
198
	 *
199
	 * @param int $items_deleted Number of items that were deleted.
200
	 *
201
	 * @return string Success message.
202
	 */
203
	protected function get_success_message( $items_deleted ) {
204
		/* translators: 1 Number of posts deleted */
205
		return _n( 'Deleted comment meta field from %d comment', 'Deleted comment meta field from %d comments', $items_deleted, 'bulk-delete' );
206
	}
207
208
	/**
209
	 * Append filtering options to the delete comment meta form.
210
	 *
211
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
212
	 *
213
	 * @since 0.1 of Bulk Delete Comment Meta add-on
214
	 */
215
	public function add_filtering_options() {
216
		?>
217
		<table class="optiontable" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_filters" style="display:none;">
218
			<tr>
219
				<td>
220
					<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?>
221
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op">
222
						<option value="="><?php _e( 'equal to', 'bulk-delete' ); ?></option>
223
						<option value="!="><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
224
						<option value="<"><?php _e( 'less than', 'bulk-delete' ); ?></option>
225
						<option value="<="><?php _e( 'less than or equal to', 'bulk-delete' ); ?></option>
226
						<option value=">"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
227
						<option value=">="><?php _e( 'greater than or equal to', 'bulk-delete' ); ?></option>
228
						<option value="LIKE"><?php _e( 'like', 'bulk-delete' ); ?></option>
229
						<option value="NOT LIKE"><?php _e( 'not like', 'bulk-delete' ); ?></option>
230
					</select>
231
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type">
232
						<option value="CHAR"><?php _e( 'CHAR', 'bulk-delete' ); ?></option>
233
						<option value="NUMERIC"><?php _e( 'NUMERIC', 'bulk-delete' ); ?></option>
234
						<option value="DECIMAL"><?php _e( 'DECIMAL', 'bulk-delete' ); ?></option>
235
						<option value="SIGNED"><?php _e( 'SIGNED', 'bulk-delete' ); ?></option>
236
						<option value="UNSIGNED"><?php _e( 'UNSIGNED', 'bulk-delete' ); ?></option>
237
						<option value="DATE"><?php _e( 'DATE', 'bulk-delete' ); ?></option>
238
						<option value="TIME"><?php _e( 'TIME', 'bulk-delete' ); ?></option>
239
						<option value="DATETIME"><?php _e( 'DATETIME', 'bulk-delete' ); ?></option>
240
						<option value="BINARY"><?php _e( 'BINARY', 'bulk-delete' ); ?></option>
241
					</select>
242
243
					<input type="text" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>"
244
						name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
245
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value">
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 . '_meta_op', '=' );
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
		}
270
271
		return $delete_options;
272
	}
273
274
	/**
275
	 * Change the meta query.
276
	 *
277
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
278
	 *
279
	 * @since 0.1 of Bulk Delete Comment Meta add-on
280
	 *
281
	 * @param array $meta_query     Meta query.
282
	 * @param array $delete_options List of options chosen by the user.
283
	 *
284
	 * @return array Modified meta query.
285
	 */
286 8
	public function change_meta_query( $meta_query, $delete_options ) {
287
		$meta_query = array(
288
			array(
289 8
				'key'     => $delete_options['meta_key'],
290 8
				'value'   => $delete_options['meta_value'],
291 8
				'compare' => $delete_options['meta_op'],
292 8
				'type'    => $delete_options['meta_type'],
293
			),
294
		);
295
296 8
		return $meta_query;
297
	}
298
299
	/**
300
	 * Hook handler.
301
	 *
302
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
303
	 *
304
	 * @since 0.1 of Bulk Delete Comment Meta add-on
305
	 *
306
	 * @param array $delete_options Delete options array.
307
	 */
308
	public function do_delete_comment_meta( $delete_options ) {
309
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
310
		$count = $this->delete( $delete_options );
311
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
312
	}
313
}
314