Completed
Push — 641-fix/refactor-messages-in-t... ( 8585ef...b6c79e )
by Sudar
146:06 queued 76:01
created

DeleteCommentMetaModule::do_delete()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
c 0
b 0
f 0
nc 24
nop 1
dl 0
loc 34
rs 9.0444
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" 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" 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" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_filters" style="display:none;">
188
			<tr>
189
				<td>
190
					<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?>
191
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type">
192
						<option value="CHAR"><?php _e( 'CHAR', 'bulk-delete' ); ?></option>
193
						<option value="NUMERIC"><?php _e( 'NUMERIC', 'bulk-delete' ); ?></option>
194
						<option value="DECIMAL"><?php _e( 'DECIMAL', 'bulk-delete' ); ?></option>
195
						<option value="SIGNED"><?php _e( 'SIGNED', 'bulk-delete' ); ?></option>
196
						<option value="UNSIGNED"><?php _e( 'UNSIGNED', 'bulk-delete' ); ?></option>
197
						<option value="DATE"><?php _e( 'DATE', 'bulk-delete' ); ?></option>
198
						<option value="TIME"><?php _e( 'TIME', 'bulk-delete' ); ?></option>
199
						<option value="DATETIME"><?php _e( 'DATETIME', 'bulk-delete' ); ?></option>
200
						<option value="BINARY"><?php _e( 'BINARY', 'bulk-delete' ); ?></option>
201
					</select>
202
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op">
203
						<option value="="><?php _e( 'equal to', 'bulk-delete' ); ?></option>
204
						<option value="!="><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
205
						<option value="<"><?php _e( 'less than', 'bulk-delete' ); ?></option>
206
						<option value="<="><?php _e( 'less than or equal to', 'bulk-delete' ); ?></option>
207
						<option value=">"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
208
						<option value=">="><?php _e( 'greater than or equal to', 'bulk-delete' ); ?></option>
209
						<option value="LIKE"><?php _e( 'like', 'bulk-delete' ); ?></option>
210
						<option value="NOT LIKE"><?php _e( 'not like', 'bulk-delete' ); ?></option>
211
					</select>
212
					<input type="text" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>"
213
						name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
214
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value">
215
				</td>
216
			</tr>
217
		</table>
218
		<?php
219
	}
220
221
	/**
222
	 * Process additional delete options.
223
	 *
224
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
225
	 *
226
	 * @since 0.1 of Bulk Delete Comment Meta add-on
227
	 *
228
	 * @param array $delete_options Delete options array.
229
	 * @param array $post           The POST array.
230
	 *
231
	 * @return array Processed delete options array.
232
	 */
233
	public function process_filtering_options( $delete_options, $post ) {
234
		if ( 'true' == bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) {
235
			$delete_options['meta_op']    = bd_array_get( $post, 'smbd_' . $this->field_slug . '_meta_op', '=' );
236
			$delete_options['meta_type']  = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' );
237
			$delete_options['meta_value'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' );
238
		}
239
240
		return $delete_options;
241
	}
242
243
	/**
244
	 * Change the meta query.
245
	 *
246
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
247
	 *
248
	 * @since 0.1 of Bulk Delete Comment Meta add-on
249
	 *
250
	 * @param array $meta_query     Meta query.
251
	 * @param array $delete_options List of options chosen by the user.
252
	 *
253
	 * @return array Modified meta query.
254
	 */
255
	public function change_meta_query( $meta_query, $delete_options ) {
256
		$meta_query = array(
257
			array(
258
				'key'     => $delete_options['meta_key'],
259
				'value'   => $delete_options['meta_value'],
260
				'compare' => $delete_options['meta_op'],
261
				'type'    => $delete_options['meta_type'],
262
			),
263
		);
264
265
		return $meta_query;
266
	}
267
268
	/**
269
	 * Hook handler.
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 $delete_options Delete options array.
276
	 */
277
	public function do_delete_comment_meta( $delete_options ) {
278
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
279
		$count = $this->delete( $delete_options );
280
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
281
	}
282
}
283