sudar /
bulk-delete
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Utility class for deleting Comment Meta. |
||
| 4 | * |
||
| 5 | * @since 5.4 |
||
| 6 | * |
||
| 7 | * @author Sudar |
||
| 8 | * |
||
| 9 | * @package BulkDelete\Meta |
||
| 10 | */ |
||
| 11 | class Bulk_Delete_Comment_Meta { |
||
| 12 | /** |
||
| 13 | * Box slug. |
||
| 14 | * |
||
| 15 | * @since 5.4 |
||
| 16 | */ |
||
| 17 | const BOX_COMMENT_META = 'bd-comment-meta'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Cron Hook. |
||
| 21 | * |
||
| 22 | * @since 5.4 |
||
| 23 | */ |
||
| 24 | const CRON_HOOK = 'do-bulk-delete-comment-meta'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Register comment-meta meta box for delete meta page. |
||
| 28 | * |
||
| 29 | * @static |
||
| 30 | * |
||
| 31 | * @since 5.4 |
||
| 32 | */ |
||
| 33 | public static function add_delete_comment_meta_box() { |
||
| 34 | $bd = BULK_DELETE(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 35 | |||
| 36 | add_meta_box( |
||
| 37 | self::BOX_COMMENT_META, |
||
| 38 | __( 'Bulk Delete Comment Meta', 'bulk-delete' ), |
||
| 39 | array( __CLASS__, 'render_delete_comment_meta_box' ), |
||
| 40 | $bd->meta_page, |
||
| 41 | 'advanced' |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Render delete comment-meta meta box for delete meta page. |
||
| 47 | * |
||
| 48 | * @static |
||
| 49 | * |
||
| 50 | * @since 5.4 |
||
| 51 | */ |
||
| 52 | public static function render_delete_comment_meta_box() { |
||
| 53 | if ( Bulk_Delete_Meta::is_meta_box_hidden( self::BOX_COMMENT_META ) ) { |
||
| 54 | printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . Bulk_Delete_meta::META_PAGE_SLUG ); |
||
| 55 | |||
| 56 | return; |
||
| 57 | } |
||
| 58 | ?> |
||
| 59 | <!-- Comment Meta box start--> |
||
| 60 | <fieldset class="options"> |
||
| 61 | <?php |
||
| 62 | $types = get_post_types( array( |
||
| 63 | 'public' => true, |
||
| 64 | '_builtin' => false, |
||
| 65 | ), 'names' |
||
| 66 | ); |
||
| 67 | |||
| 68 | array_unshift( $types, 'post' ); |
||
| 69 | ?> |
||
| 70 | <h4><?php _e( 'Select the post type whose comment meta fields you want to delete', 'bulk-delete' ); ?></h4> |
||
| 71 | <table class="optiontable"> |
||
| 72 | <?php |
||
| 73 | foreach ( $types as $type ) { |
||
| 74 | ?> |
||
| 75 | <tr> |
||
| 76 | <td> |
||
| 77 | <input name="smbd_cm_post_type" value = "<?php echo $type; ?>" type = "radio" class = "smbd_cm_post_type" <?php checked( $type, 'post' ); ?>> |
||
| 78 | <label for="smbd_cm_post_type"><?php echo $type; ?> </label> |
||
| 79 | </td> |
||
| 80 | </tr> |
||
| 81 | <?php |
||
| 82 | } |
||
| 83 | ?> |
||
| 84 | </table> |
||
| 85 | |||
| 86 | <h4><?php _e( 'Choose your comment meta field settings', 'bulk-delete' ); ?></h4> |
||
| 87 | <table class="optiontable"> |
||
| 88 | <tr> |
||
| 89 | <td> |
||
| 90 | <input name="smbd_cm_use_value" value="false" type="radio" checked> |
||
| 91 | <label for="smbd_cm_use_value"><?php echo __( 'Delete based on comment meta key name only', 'bulk-delete' ); ?></label> |
||
| 92 | </td> |
||
| 93 | </tr> |
||
| 94 | |||
| 95 | <tr> |
||
| 96 | <td> |
||
| 97 | <input name="smbd_cm_use_value" id="smbd_cm_use_value" value="true" type="radio" disabled> |
||
| 98 | <label for="smbd_cm_use_value"><?php echo __( 'Delete based on comment meta key name and value', 'bulk-delete' ); ?></label> |
||
| 99 | <span class="bd-cm-pro" style="color:red; vertical-align: middle;"> |
||
| 100 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-comment-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-c" target="_blank">Buy now</a> |
||
| 101 | </span> |
||
| 102 | </td> |
||
| 103 | </tr> |
||
| 104 | |||
| 105 | <tr> |
||
| 106 | <td> |
||
| 107 | <label for="smbd_cm_key"><?php _e( 'Comment Meta Key ', 'bulk-delete' ); ?></label> |
||
| 108 | <input name="smbd_cm_key" id="smbd_cm_key" placeholder="<?php _e( 'Meta Key', 'bulk-delete' ); ?>"> |
||
| 109 | </td> |
||
| 110 | </tr> |
||
| 111 | </table> |
||
| 112 | <?php |
||
| 113 | /** |
||
| 114 | * Add more fields to the delete comment meta field form. |
||
| 115 | * This hook can be used to add more fields to the delete comment meta field form. |
||
| 116 | * |
||
| 117 | * @since 5.4 |
||
| 118 | */ |
||
| 119 | do_action( 'bd_delete_comment_meta_form' ); |
||
| 120 | ?> |
||
| 121 | <table class="optiontable"> |
||
| 122 | <tr> |
||
| 123 | <td> |
||
| 124 | <h4><?php _e( 'Choose your deletion options', 'bulk-delete' ); ?></h4> |
||
| 125 | </td> |
||
| 126 | </tr> |
||
| 127 | |||
| 128 | <tr> |
||
| 129 | <td> |
||
| 130 | <input name="smbd_cm_restrict" id="smbd_cm_restrict" value = "true" type = "checkbox"> |
||
| 131 | <?php _e( 'Only restrict to comments which are ', 'bulk-delete' );?> |
||
| 132 | <select name="smbd_cm_op" id="smbd_cm_op" disabled="disabled"> |
||
| 133 | <option value ="before"><?php _e( 'older than', 'bulk-delete' );?></option> |
||
| 134 | <option value ="after"><?php _e( 'commented within last', 'bulk-delete' );?></option> |
||
| 135 | </select> |
||
| 136 | <input type="number" name="smbd_cm_days" id="smbd_cm_days" disabled value ="0" maxlength="4" size="4"><?php _e( 'days', 'bulk-delete' );?> |
||
| 137 | </td> |
||
| 138 | </tr> |
||
| 139 | |||
| 140 | <tr> |
||
| 141 | <td> |
||
| 142 | <input name="smbd_cm_limit" id="smbd_cm_limit" value = "true" type = "checkbox"> |
||
| 143 | <?php _e( 'Only delete comment meta field from first ', 'bulk-delete' );?> |
||
| 144 | <input type="number" name="smbd_cm_limit_to" id="smbd_cm_limit_to" disabled value ="0" maxlength="4" size="4"><?php _e( 'comments.', 'bulk-delete' );?> |
||
| 145 | <?php _e( 'Use this option if there are more than 1000 posts and the script times out.', 'bulk-delete' ) ?> |
||
| 146 | </td> |
||
| 147 | </tr> |
||
| 148 | |||
| 149 | <tr> |
||
| 150 | <td> |
||
| 151 | <input name="smbd_cm_cron" value="false" type = "radio" checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?> |
||
| 152 | <input name="smbd_cm_cron" value="true" type = "radio" id = "smbd_cm_cron" disabled > <?php _e( 'Schedule', 'bulk-delete' ); ?> |
||
| 153 | <input name="smbd_cm_cron_start" id="smbd_cm_cron_start" value = "now" type = "text" disabled><?php _e( 'repeat ', 'bulk-delete' );?> |
||
| 154 | <select name="smbd_cm_cron_freq" id="smbd_cm_cron_freq" disabled> |
||
| 155 | <option value = "-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
||
| 156 | <?php |
||
| 157 | $schedules = wp_get_schedules(); |
||
| 158 | foreach ( $schedules as $key => $value ) { |
||
| 159 | ?> |
||
| 160 | <option value = "<?php echo $key; ?>"><?php echo $value['display']; ?></option> |
||
| 161 | <?php |
||
| 162 | } |
||
| 163 | ?> |
||
| 164 | </select> |
||
| 165 | <span class="bd-cm-pro" style="color:red"> |
||
| 166 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a href = "http://bulkwp.com/addons/bulk-delete-comment-meta/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-m-c">Buy now</a> |
||
| 167 | </span> |
||
| 168 | </td> |
||
| 169 | </tr> |
||
| 170 | |||
| 171 | <tr> |
||
| 172 | <td> |
||
| 173 | <?php _e( 'Enter time in Y-m-d H:i:s format or enter now to use current time', 'bulk-delete' );?> |
||
| 174 | </td> |
||
| 175 | </tr> |
||
| 176 | |||
| 177 | </table> |
||
| 178 | </fieldset> |
||
| 179 | |||
| 180 | <p> |
||
| 181 | <button type="submit" name="bd_action" value="delete_meta_comment" class="button-primary"><?php _e( 'Bulk Delete ', 'bulk-delete' ) ?>»</button> |
||
| 182 | </p> |
||
| 183 | <!-- Comment Meta box end--> |
||
| 184 | <?php |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Filter JS Array and add validation hooks. |
||
| 189 | * |
||
| 190 | * @since 5.4 |
||
| 191 | * @static |
||
| 192 | * |
||
| 193 | * @param array $js_array JavaScript Array |
||
| 194 | * |
||
| 195 | * @return array Modified JavaScript Array |
||
| 196 | */ |
||
| 197 | public static function filter_js_array( $js_array ) { |
||
| 198 | $js_array['dt_iterators'][] = '_cm'; |
||
| 199 | $js_array['validators']['delete_meta_comment'] = 'noValidation'; |
||
| 200 | |||
| 201 | $js_array['pre_action_msg']['delete_meta_comment'] = 'deleteCMWarning'; |
||
| 202 | $js_array['msg']['deleteCMWarning'] = __( 'Are you sure you want to delete all the comment meta fields that match the selected filters?', 'bulk-delete' ); |
||
| 203 | |||
| 204 | return $js_array; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Controller for deleting comment meta fields. |
||
| 209 | * |
||
| 210 | * @static |
||
| 211 | * |
||
| 212 | * @since 5.4 |
||
| 213 | */ |
||
| 214 | public static function do_delete_comment_meta() { |
||
| 215 | $delete_options = array(); |
||
| 216 | $delete_options['post_type'] = esc_sql( bd_array_get( $_POST, 'smbd_cm_post_type', 'post' ) ); |
||
| 217 | |||
| 218 | $delete_options['use_value'] = bd_array_get_bool( $_POST, 'smbd_cm_use_value', false ); |
||
| 219 | $delete_options['meta_key'] = esc_sql( bd_array_get( $_POST, 'smbd_cm_key', '' ) ); |
||
| 220 | |||
| 221 | $delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_cm_limit_to', 0 ) ); |
||
| 222 | |||
| 223 | $delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_cm_restrict', false ); |
||
| 224 | $delete_options['op'] = esc_sql( bd_array_get( $_POST, 'smbd_cm_op', 'before' ) ); |
||
| 225 | $delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_cm_days', 0 ) ); |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Delete comment-meta delete options filter. |
||
| 229 | * This filter is for processing filtering options for deleting comment meta. |
||
| 230 | * |
||
| 231 | * @since 5.4 |
||
| 232 | */ |
||
| 233 | $delete_options = apply_filters( 'bd_delete_comment_meta_options', $delete_options, $_POST ); |
||
| 234 | |||
| 235 | if ( 'true' == bd_array_get( $_POST, 'smbd_cm_cron', 'false' ) ) { |
||
| 236 | $freq = $_POST['smbd_cm_cron_freq']; |
||
| 237 | $time = strtotime( $_POST['smbd_cm_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||
| 238 | |||
| 239 | if ( $freq == -1 ) { |
||
| 240 | wp_schedule_single_event( $time, self::CRON_HOOK, array( $delete_options ) ); |
||
| 241 | } else { |
||
| 242 | wp_schedule_event( $time, $freq, self::CRON_HOOK, array( $delete_options ) ); |
||
| 243 | } |
||
| 244 | $msg = __( 'Comment meta fields from the comments with the selected criteria are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
||
| 245 | sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG ); |
||
| 246 | } else { |
||
| 247 | $deleted_count = self::delete_comment_meta( $delete_options ); |
||
| 248 | $msg = sprintf( _n( 'Deleted comment meta field from %d comment', 'Deleted comment meta field from %d comments' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||
| 249 | } |
||
| 250 | |||
| 251 | add_settings_error( |
||
| 252 | Bulk_Delete_Meta::META_PAGE_SLUG, |
||
| 253 | 'deleted-comments', |
||
| 254 | $msg, |
||
| 255 | 'updated' |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Delete Comment Meta. |
||
| 261 | * |
||
| 262 | * @static |
||
| 263 | * |
||
| 264 | * @since 5.4 |
||
| 265 | * |
||
| 266 | * @param array $delete_options Options for deleting |
||
| 267 | * |
||
| 268 | * @return int Number of comments that were deleted |
||
| 269 | */ |
||
| 270 | public static function delete_comment_meta( $delete_options ) { |
||
| 271 | $count = 0; |
||
| 272 | $post_type = $delete_options['post_type']; |
||
| 273 | $limit_to = $delete_options['limit_to']; |
||
| 274 | $meta_key = $delete_options['meta_key']; |
||
| 275 | $use_value = $delete_options['use_value']; |
||
| 276 | $restrict = $delete_options['restrict']; |
||
| 277 | $days = $delete_options['days']; |
||
| 278 | $op = $delete_options['op']; |
||
| 279 | |||
| 280 | $options = array( |
||
| 281 | 'post_type' => $post_type, |
||
| 282 | ); |
||
| 283 | |||
| 284 | if ( $limit_to > 0 ) { |
||
| 285 | $options['number'] = $limit_to; |
||
| 286 | } |
||
| 287 | |||
| 288 | if ( $restrict ) { |
||
| 289 | $options['date_query'] = array( |
||
| 290 | array( |
||
| 291 | 'column' => 'comment_date', |
||
| 292 | $op => "{$days} day ago", |
||
| 293 | ), |
||
| 294 | ); |
||
| 295 | } |
||
| 296 | |||
| 297 | if ( $use_value ) { |
||
| 298 | $options['meta_query'] = apply_filters( 'bd_delete_comment_meta_query', array(), $delete_options ); |
||
| 299 | } else { |
||
| 300 | $options['meta_key'] = $meta_key; |
||
| 301 | } |
||
| 302 | |||
| 303 | $comments = get_comments( $options ); |
||
| 304 | |||
| 305 | foreach ( $comments as $comment ) { |
||
| 306 | if ( delete_comment_meta( $comment->comment_ID, $meta_key ) ) { |
||
| 307 | $count++; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | return $count; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | // hooks |
||
| 316 | 1 | add_action( 'bd_add_meta_box_for_meta', array( 'Bulk_Delete_Comment_Meta', 'add_delete_comment_meta_box' ) ); |
|
| 317 | 1 | add_action( 'bd_delete_meta_comment', array( 'Bulk_Delete_Comment_Meta', 'do_delete_comment_meta' ) ); |
|
| 318 | |||
| 319 | 1 | add_filter( 'bd_javascript_array', array( 'Bulk_Delete_Comment_Meta', 'filter_js_array' ) ); |
|
| 320 | ?> |
||
| 321 |