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