Completed
Push — master ( 56bca7...987e5f )
by Sudar
55:17 queued 46:03
created

DeleteCommentMetaModule::register_cron_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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