Completed
Push — 331-fix/delete-comment-meta-te... ( 73c359...67e11a )
by Rajan
18:56 queued 13:27
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
	
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 13
	public function register( $hook_suffix, $page_slug ) {
29 13
		parent::register( $hook_suffix, $page_slug );
30
31 13
		add_action( 'bd_delete_comment_meta_form', array( $this, 'add_filtering_options' ) );
32
33 13
		add_filter( 'bd_delete_comment_meta_options', array( $this, 'process_filtering_options' ), 10, 2 );
34 13
		add_filter( 'bd_delete_comment_meta_query', array( $this, 'change_meta_query' ), 10, 2 );
35 13
	}
36
37
	/**
38
	 * Render the Delete Comment Meta box.
39
	 */
40
	public function render() {
41
		?>
42
		<!-- Comment Meta box start-->
43
		<fieldset class="options">
44
			<h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4>
45
			<table class="optiontable">
46
				<?php $this->render_post_type_dropdown(); ?>
47
			</table>
48
49
			<h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4>
50
			<table class="optiontable">
51
				<tr>
52
					<td>
53
						<input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_use_value" value="false" type="radio" checked>
54
						<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>
55
					</td>
56
				</tr>
57
58
				<tr>
59
					<td>
60
						<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">
61
62
						<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>
63
					</td>
64
				</tr>
65
66
				<tr>
67
					<td>
68
						<label for="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label>
69
						<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' ); ?>">
70
					</td>
71
				</tr>
72
			</table>
73
74
			<?php
75
			/**
76
			 * Add more fields to the delete comment meta field form.
77
			 * This hook can be used to add more fields to the delete comment meta field form.
78
			 *
79
			 * @since 5.4
80
			 */
81
			do_action( 'bd_delete_comment_meta_form' );
82
			?>
83
			<table class="optiontable">
84
				<tr>
85
					<td colspan="2">
86
						<h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4>
87
					</td>
88
				</tr>
89
90
				<?php $this->render_restrict_settings(); ?>
91
				<?php $this->render_limit_settings(); ?>
92
				<?php $this->render_cron_settings(); ?>
93
94
			</table>
95
		</fieldset>
96
97
		<?php $this->render_submit_button(); ?>
98
99
		<!-- Comment Meta box end-->
100
		<?php
101
	}
102
103
	protected function convert_user_input_to_options( $request, $options ) {
104
		$options['post_type'] = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_type', 'post' ) );
105
106
		$options['use_value'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_use_value', false );
107
		$options['meta_key']  = esc_sql( bd_array_get( $request, 'smbd_' . $this->field_slug . '_meta_key', '' ) );
108
109
		/**
110
		 * Delete comment-meta delete options filter.
111
		 *
112
		 * This filter is for processing filtering options for deleting comment meta.
113
		 *
114
		 * @since 5.4
115
		 */
116
		return apply_filters( 'bd_delete_comment_meta_options', $options, $request );
117
	}
118
119 13
	protected function do_delete( $options ) {
120
		$args = array(
121 13
			'post_type' => $options['post_type'],
122
		);
123
124 13
		if ( $options['limit_to'] > 0 ) {
125 1
			$args['number'] = $options['limit_to'];
126
		}
127
128 13
		$op   = $options['date_op'];
129 13
		$days = $options['days'];
130
131 13
		if ( $options['restrict'] ) {
132 2
			$args['date_query'] = array(
133
				array(
134 2
					'column' => 'comment_date',
135 2
					$op      => "{$days} day ago",
136
				),
137
			);
138
		}
139
140 13
		if ( $options['use_value'] ) {
141 8
			$args['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $options );
142
		} else {
143 5
			$args['meta_key'] = $options['meta_key'];
144
		}
145
146 13
		$meta_deleted = 0;
147 13
		$comments     = get_comments( $args );
148
149 13
		foreach ( $comments as $comment ) {
150 13
			if ( delete_comment_meta( $comment->comment_ID, $options['meta_key'] ) ) {
151 13
				$meta_deleted ++;
152
			}
153
		}
154
155 13
		return $meta_deleted;
156
	}
157
158
	public function filter_js_array( $js_array ) {
159
		$js_array['dt_iterators'][]              = '_' . $this->field_slug;
160
		$js_array['validators'][ $this->action ] = 'noValidation';
161
162
		$js_array['pre_action_msg'][ $this->action ] = 'deleteCMWarning';
163
		$js_array['msg']['deleteCMWarning']          = __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' );
164
165
		return $js_array;
166
	}
167
168
	protected function get_success_message( $items_deleted ) {
169
		/* translators: 1 Number of comment deleted */
170
		return _n( 'Deleted comment meta field from %d comment', 'Deleted comment meta field from %d comments', $items_deleted, 'bulk-delete' );
171
	}
172
173
	/**
174
	 * Append filtering options to the delete comment meta form.
175
	 *
176
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
177
	 *
178
	 * @since 0.1 of Bulk Delete Comment Meta add-on
179
	 */
180
	public function add_filtering_options() {
181
		?>
182
		<table class="optiontable" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_filters" style="display:none;">
183
			<tr>
184
				<td>
185
					<?php _e( 'Comment Meta Value ', 'bulk-delete' ); ?>
186
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_meta_op">
187
						<option value="="><?php _e( 'equal to', 'bulk-delete' ); ?></option>
188
						<option value="!="><?php _e( 'not equal to', 'bulk-delete' ); ?></option>
189
						<option value="<"><?php _e( 'less than', 'bulk-delete' ); ?></option>
190
						<option value="<="><?php _e( 'less than or equal to', 'bulk-delete' ); ?></option>
191
						<option value=">"><?php _e( 'greater than', 'bulk-delete' ); ?></option>
192
						<option value=">="><?php _e( 'greater than or equal to', 'bulk-delete' ); ?></option>
193
						<option value="LIKE"><?php _e( 'like', 'bulk-delete' ); ?></option>
194
						<option value="NOT LIKE"><?php _e( 'not like', 'bulk-delete' ); ?></option>
195
					</select>
196
					<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type" id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_type">
197
						<option value="CHAR"><?php _e( 'CHAR', 'bulk-delete' ); ?></option>
198
						<option value="NUMERIC"><?php _e( 'NUMERIC', 'bulk-delete' ); ?></option>
199
						<option value="DECIMAL"><?php _e( 'DECIMAL', 'bulk-delete' ); ?></option>
200
						<option value="SIGNED"><?php _e( 'SIGNED', 'bulk-delete' ); ?></option>
201
						<option value="UNSIGNED"><?php _e( 'UNSIGNED', 'bulk-delete' ); ?></option>
202
						<option value="DATE"><?php _e( 'DATE', 'bulk-delete' ); ?></option>
203
						<option value="TIME"><?php _e( 'TIME', 'bulk-delete' ); ?></option>
204
						<option value="DATETIME"><?php _e( 'DATETIME', 'bulk-delete' ); ?></option>
205
						<option value="BINARY"><?php _e( 'BINARY', 'bulk-delete' ); ?></option>
206
					</select>
207
208
					<input type="text" placeholder="<?php _e( 'Meta Value', 'bulk-delete' ); ?>"
209
						name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value"
210
						id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value">
211
				</td>
212
			</tr>
213
		</table>
214
		<?php
215
	}
216
217
	/**
218
	 * Process additional delete options.
219
	 *
220
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
221
	 *
222
	 * @since 0.1 of Bulk Delete Comment Meta add-on
223
	 *
224
	 * @param array $delete_options Delete options array.
225
	 * @param array $post           The POST array.
226
	 *
227
	 * @return array Processed delete options array.
228
	 */
229
	public function process_filtering_options( $delete_options, $post ) {
230
		if ( 'true' == bd_array_get( $post, 'smbd_' . $this->field_slug . '_use_value', 'false' ) ) {
231
			$delete_options['meta_op']    = bd_array_get( $post, 'smbd_' . $this->field_slug . '_meta_op', '=' );
232
			$delete_options['meta_type']  = bd_array_get( $post, 'smbd_' . $this->field_slug . '_type', 'CHAR' );
233
			$delete_options['meta_value'] = bd_array_get( $post, 'smbd_' . $this->field_slug . '_value', '' );
234
		}
235
236
		return $delete_options;
237
	}
238
239
	/**
240
	 * Change the meta query.
241
	 *
242
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
243
	 *
244
	 * @since 0.1 of Bulk Delete Comment Meta add-on
245
	 *
246
	 * @param array $meta_query     Meta query.
247
	 * @param array $delete_options List of options chosen by the user.
248
	 *
249
	 * @return array Modified meta query.
250
	 */
251 8
	public function change_meta_query( $meta_query, $delete_options ) {
252
		$meta_query = array(
253
			array(
254 8
				'key'     => $delete_options['meta_key'],
255 8
				'value'   => $delete_options['meta_value'],
256 8
				'compare' => $delete_options['meta_op'],
257 8
				'type'    => $delete_options['meta_type'],
258
			),
259
		);
260
261 8
		return $meta_query;
262
	}
263
264
	/**
265
	 * Hook handler.
266
	 *
267
	 * This function was originally part of the Bulk Delete Comment Meta add-on.
268
	 *
269
	 * @since 0.1 of Bulk Delete Comment Meta add-on
270
	 *
271
	 * @param array $delete_options Delete options array.
272
	 */
273
	public function do_delete_comment_meta( $delete_options ) {
274
		do_action( 'bd_before_scheduler', $this->messages['cron_label'] );
275
		$count = $this->delete( $delete_options );
276
		do_action( 'bd_after_scheduler', $this->messages['cron_label'], $count );
277
	}
278
}
279