Completed
Pull Request — dev/6.0.1 (#616)
by
unknown
97:02 queued 71:20
created

DeleteCommentMetaModule::do_delete_comment_meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
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
7 1
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 9
	protected function initialize() {
16 9
		$this->field_slug    = 'comment_meta';
17 9
		$this->meta_box_slug = 'bd-comment-meta';
18 9
		$this->action        = 'delete_comment_meta';
19 9
		$this->cron_hook     = 'do-bulk-delete-comment-meta';
20 9
		$this->messages      = array(
21 9
			'box_label'  => __( 'Bulk Delete Comment Meta', 'bulk-delete' ),
22 9
			'scheduled'  => __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
23 9
			'cron_label' => __( 'Delete Comment Meta', 'bulk-delete' ),
24
		);
25 9
	}
26
27 9
	public function register( $hook_suffix, $page_slug ) {
28 9
		parent::register( $hook_suffix, $page_slug );
29
30 9
		add_action( 'bd_delete_comment_meta_form', array( $this, 'add_filtering_options' ) );
31
32 9
		add_filter( 'bd_delete_comment_meta_options', array( $this, 'process_filtering_options' ), 10, 2 );
33 9
		add_filter( 'bd_delete_comment_meta_query', array( $this, 'change_meta_query' ), 10, 2 );
34 9
	}
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_with_status( false ); ?>
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" 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" 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" 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 ) );
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 9
	protected function do_delete( $options ) {
119 9
		$type_status = $this->split_post_type_and_status( $options['post_type'] );
120
		$args        = array(
121 9
			'post_type'   => $type_status['type'],
122 9
			'post_status' => $type_status['status'],
123
		);
124
125 9
		if ( $options['limit_to'] > 0 ) {
126 1
			$args['number'] = $options['limit_to'];
127
		}
128
129 9
		$op   = $options['date_op'];
130 9
		$days = $options['days'];
131
132 9
		if ( $options['restrict'] ) {
133 2
			$args['date_query'] = array(
134
				array(
135 2
					'column' => 'comment_date',
136 2
					$op      => "{$days} day ago",
137
				),
138
			);
139
		}
140
141 9
		if ( $options['use_value'] ) {
142 2
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
143
		} else {
144 7
			$args['meta_key'] = $options['meta_key'];
145
		}
146
147 9
		$meta_deleted = 0;
148 9
		$comments     = get_comments( $args );
149
150 9
		foreach ( $comments as $comment ) {
151
			// Todo: Don't delete all meta rows if there are duplicate meta keys.
152
			// See https://github.com/sudar/bulk-delete/issues/515 for details.
153 9
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
154 9
				$meta_deleted ++;
155
			}
156
		}
157
158 9
		return $meta_deleted;
159
	}
160
161
	protected function append_to_js_array( $js_array ) {
162
		$js_array['validators'][ $this->action ] = 'noValidation';
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" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_filters" style="display:none;">
185
			<tr>
186
				<td>
187
					<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?>
188
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type">
189
						<option value="CHAR"><?php _e( 'CHAR', 'bulk-delete' ); ?></option>
190
						<option value="NUMERIC"><?php _e( 'NUMERIC', 'bulk-delete' ); ?></option>
191
						<option value="DECIMAL"><?php _e( 'DECIMAL', 'bulk-delete' ); ?></option>
192
						<option value="SIGNED"><?php _e( 'SIGNED', 'bulk-delete' ); ?></option>
193
						<option value="UNSIGNED"><?php _e( 'UNSIGNED', 'bulk-delete' ); ?></option>
194
						<option value="DATE"><?php _e( 'DATE', 'bulk-delete' ); ?></option>
195
						<option value="TIME"><?php _e( 'TIME', 'bulk-delete' ); ?></option>
196
						<option value="DATETIME"><?php _e( 'DATETIME', 'bulk-delete' ); ?></option>
197
						<option value="BINARY"><?php _e( 'BINARY', 'bulk-delete' ); ?></option>
198
					</select>
199
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op">
200
						<option value="="><?php _e( 'equal to', 'bulk-delete' ); ?></option>
201
						<option value="!="><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
202
						<option value="<"><?php _e( 'less than', 'bulk-delete' ); ?></option>
203
						<option value="<="><?php _e( 'less than or equal to', 'bulk-delete' ); ?></option>
204
						<option value=">"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
205
						<option value=">="><?php _e( 'greater than or equal to', 'bulk-delete' ); ?></option>
206
						<option value="LIKE"><?php _e( 'like', 'bulk-delete' ); ?></option>
207
						<option value="NOT LIKE"><?php _e( 'not like', 'bulk-delete' ); ?></option>
208
					</select>
209
					<input type="text" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>"
210
						name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
211
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value">
212
				</td>
213
			</tr>
214
		</table>
215
		<?php
216
	}
217
218
	/**
219
	 * Process additional delete options.
220
	 *
221
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
222
	 *
223
	 * @since 0.1 of Bulk Delete Comment Meta add-on
224
	 *
225
	 * @param array $delete_options Delete options array.
226
	 * @param array $post           The POST array.
227
	 *
228
	 * @return array Processed delete options array.
229
	 */
230
	public function process_filtering_options( $delete_options, $post ) {
231
		if ( 'true' == bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) {
232
			$delete_options['meta_op']    = bd_array_get( $post, 'smbd_' . $this->field_slug . '_meta_op', '=' );
233
			$delete_options['meta_type']  = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' );
234
			$delete_options['meta_value'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' );
235
		}
236
237
		return $delete_options;
238
	}
239
240
	/**
241
	 * Change the meta query.
242
	 *
243
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
244
	 *
245
	 * @since 0.1 of Bulk Delete Comment Meta add-on
246
	 *
247
	 * @param array $meta_query     Meta query.
248
	 * @param array $delete_options List of options chosen by the user.
249
	 *
250
	 * @return array Modified meta query.
251
	 */
252 2
	public function change_meta_query( $meta_query, $delete_options ) {
253
		$meta_query = array(
254
			array(
255 2
				'key'     => $delete_options['meta_key'],
256 2
				'value'   => $delete_options['meta_value'],
257 2
				'compare' => $delete_options['meta_op'],
258 2
				'type'    => $delete_options['meta_type'],
259
			),
260
		);
261
262 2
		return $meta_query;
263
	}
264
265
	/**
266
	 * Hook handler.
267
	 *
268
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
269
	 *
270
	 * @since 0.1 of Bulk Delete Comment Meta add-on
271
	 *
272
	 * @param array $delete_options Delete options array.
273
	 */
274
	public function do_delete_comment_meta( $delete_options ) {
275
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
276
		$count = $this->delete( $delete_options );
277
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
278
	}
279
}
280