sudar /
bulk-delete
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Utility class for deleting posts. |
||||
| 4 | * |
||||
| 5 | * @author Sudar |
||||
| 6 | * |
||||
| 7 | * @package BulkDelete |
||||
| 8 | */ |
||||
| 9 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
||||
| 10 | |||||
| 11 | class Bulk_Delete_Posts { |
||||
| 12 | /** |
||||
| 13 | * Render post status box. |
||||
| 14 | */ |
||||
| 15 | public static function render_delete_posts_by_status_box() { |
||||
| 16 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_STATUS ) ) { |
||||
| 17 | /* translators: 1 Number of posts that are deleted. */ |
||||
| 18 | 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::POSTS_PAGE_SLUG ); |
||||
| 19 | |||||
| 20 | return; |
||||
| 21 | } |
||||
| 22 | |||||
| 23 | $post_statuses = bd_get_post_statuses(); |
||||
| 24 | $post_count = wp_count_posts(); |
||||
| 25 | ?> |
||||
| 26 | <h4><?php _e( 'Select the post statuses from which you want to delete posts', 'bulk-delete' ); ?></h4> |
||||
| 27 | |||||
| 28 | <fieldset class="options"> |
||||
| 29 | <table class="optiontable"> |
||||
| 30 | |||||
| 31 | <?php foreach ( $post_statuses as $post_status ) : ?> |
||||
| 32 | <tr> |
||||
| 33 | <td> |
||||
| 34 | <input name="smbd_post_status[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>" |
||||
| 35 | value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox"> |
||||
| 36 | |||||
| 37 | <label for="smbd_<?php echo esc_attr( $post_status->name ); ?>"> |
||||
| 38 | <?php echo esc_html( $post_status->label ), ' '; ?> |
||||
| 39 | <?php if ( property_exists( $post_count, $post_status->name ) ) : ?> |
||||
| 40 | (<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>) |
||||
| 41 | <?php endif; ?> |
||||
| 42 | </label> |
||||
| 43 | </td> |
||||
| 44 | </tr> |
||||
| 45 | <?php endforeach; ?> |
||||
| 46 | |||||
| 47 | <?php $sticky_post_count = count( get_option( 'sticky_posts' ) ); ?> |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 48 | |||||
| 49 | <tr> |
||||
| 50 | <td> |
||||
| 51 | <input name="smbd_sticky" id="smbd_sticky" value="on" type="checkbox"> |
||||
| 52 | <label for="smbd_sticky"> |
||||
| 53 | <?php echo __( 'All Sticky Posts', 'bulk-delete' ), ' '; ?> |
||||
| 54 | (<?php echo absint( $sticky_post_count ), ' ', __( 'Posts', 'bulk-delete' ); ?>) |
||||
| 55 | <?php echo '<strong>', __( 'Note', 'bulk-delete' ), '</strong>: ', __( 'The date filter will not work for sticky posts', 'bulk-delete' ); ?> |
||||
| 56 | </label> |
||||
| 57 | </td> |
||||
| 58 | </tr> |
||||
| 59 | |||||
| 60 | </table> |
||||
| 61 | |||||
| 62 | <table class="optiontable"> |
||||
| 63 | <?php bd_render_filtering_table_header(); ?> |
||||
| 64 | <?php bd_render_restrict_settings( 'post_status' ); ?> |
||||
| 65 | <?php bd_render_delete_settings( 'post_status' ); ?> |
||||
| 66 | <?php bd_render_limit_settings( 'post_status' ); ?> |
||||
| 67 | <?php bd_render_cron_settings( 'post_status', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-status/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sps' ); ?> |
||||
| 68 | </table> |
||||
| 69 | |||||
| 70 | </fieldset> |
||||
| 71 | <?php |
||||
| 72 | bd_render_submit_button( 'delete_posts_by_status' ); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Delete posts by post status. |
||||
| 77 | * |
||||
| 78 | * @since 5.0 |
||||
| 79 | * @static |
||||
| 80 | * |
||||
| 81 | * Nonce verification is done in the hook that calls this function. |
||||
| 82 | * phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification |
||||
| 83 | */ |
||||
| 84 | public static function do_delete_posts_by_status() { |
||||
| 85 | $delete_options = array(); |
||||
| 86 | |||||
| 87 | $delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_post_status_restrict', false ); |
||||
| 88 | $delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_post_status_limit_to', 0 ) ); |
||||
| 89 | $delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_post_status_force_delete', false ); |
||||
| 90 | |||||
| 91 | $delete_options['date_op'] = bd_array_get( $_POST, 'smbd_post_status_op' ); |
||||
| 92 | $delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_post_status_days' ) ); |
||||
| 93 | |||||
| 94 | $delete_options['post_status'] = array_map( 'sanitize_text_field', bd_array_get( $_POST, 'smbd_post_status', array() ) ); |
||||
|
0 ignored issues
–
show
array() of type array is incompatible with the type string expected by parameter $default of bd_array_get().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 95 | |||||
| 96 | $delete_options['delete-sticky-posts'] = bd_array_get_bool( $_POST, 'smbd_sticky', false ); |
||||
| 97 | |||||
| 98 | if ( bd_array_get_bool( $_POST, 'smbd_post_status_cron', false ) ) { |
||||
| 99 | $freq = sanitize_text_field( $_POST['smbd_post_status_cron_freq'] ); |
||||
| 100 | $time = strtotime( $_POST['smbd_post_status_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||||
| 101 | |||||
| 102 | if ( -1 === $freq ) { |
||||
|
0 ignored issues
–
show
|
|||||
| 103 | wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_POST_STATUS, array( $delete_options ) ); |
||||
| 104 | } else { |
||||
| 105 | wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_POST_STATUS, array( $delete_options ) ); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | $msg = __( 'Posts with the selected status are scheduled for deletion.', 'bulk-delete' ) . ' '; |
||||
| 109 | |||||
| 110 | /* translators: 1 Url to view cron jobs */ |
||||
| 111 | $msg .= 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 ); |
||||
| 112 | } else { |
||||
| 113 | $deleted_count = self::delete_posts_by_status( $delete_options ); |
||||
| 114 | |||||
| 115 | /* translators: 1 Number of posts deleted */ |
||||
| 116 | $msg = sprintf( _n( 'Deleted %d post with the selected post status', 'Deleted %d posts with the selected post status', $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | add_settings_error( |
||||
| 120 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 121 | 'deleted-posts', |
||||
| 122 | $msg, |
||||
| 123 | 'updated' |
||||
| 124 | ); |
||||
| 125 | } // phpcs:enable |
||||
| 126 | |||||
| 127 | /** |
||||
| 128 | * Delete posts by post status - drafts, pending posts, scheduled posts etc. |
||||
| 129 | * |
||||
| 130 | * @since 5.0 |
||||
| 131 | * @static |
||||
| 132 | * |
||||
| 133 | * @param array $delete_options Options for deleting posts. |
||||
| 134 | * |
||||
| 135 | * @return int $posts_deleted Number of posts that were deleted |
||||
| 136 | */ |
||||
| 137 | public static function delete_posts_by_status( $delete_options ) { |
||||
| 138 | $delete_options = bd_convert_old_options_for_delete_post_by_status( $delete_options ); |
||||
| 139 | $delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
||||
| 140 | |||||
| 141 | $posts_deleted = 0; |
||||
| 142 | |||||
| 143 | if ( $delete_options['delete-sticky-posts'] ) { |
||||
| 144 | $posts_deleted += self::delete_sticky_posts( $delete_options['force_delete'] ); |
||||
| 145 | } |
||||
| 146 | |||||
| 147 | if ( empty( $delete_options['post_status'] ) ) { |
||||
| 148 | return $posts_deleted; |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | $options = array( |
||||
| 152 | 'post_status' => $delete_options['post_status'], |
||||
| 153 | 'post__not_in' => get_option( 'sticky_posts' ), |
||||
| 154 | ); |
||||
| 155 | |||||
| 156 | $options = bd_build_query_options( $delete_options, $options ); |
||||
| 157 | |||||
| 158 | $post_ids = bd_query( $options ); |
||||
| 159 | foreach ( $post_ids as $post_id ) { |
||||
| 160 | wp_delete_post( $post_id, $delete_options['force_delete'] ); |
||||
| 161 | } |
||||
| 162 | |||||
| 163 | $posts_deleted += count( $post_ids ); |
||||
| 164 | |||||
| 165 | return $posts_deleted; |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * Delete all sticky posts. |
||||
| 170 | * |
||||
| 171 | * @since 5.6.0 |
||||
| 172 | * |
||||
| 173 | * @param bool $force_delete Whether to force delete the posts. |
||||
| 174 | * |
||||
| 175 | * @return int Number of posts deleted. |
||||
| 176 | */ |
||||
| 177 | public static function delete_sticky_posts( $force_delete ) { |
||||
| 178 | $sticky_post_ids = get_option( 'sticky_posts' ); |
||||
| 179 | |||||
| 180 | foreach ( $sticky_post_ids as $sticky_post_id ) { |
||||
| 181 | wp_delete_post( $sticky_post_id, $force_delete ); |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | return count( $sticky_post_ids ); |
||||
| 185 | } |
||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * Render Delete posts by category box. |
||||
| 189 | */ |
||||
| 190 | public static function render_delete_posts_by_category_box() { |
||||
| 191 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_CATEGORY ) ) { |
||||
| 192 | 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::POSTS_PAGE_SLUG ); |
||||
| 193 | |||||
| 194 | return; |
||||
| 195 | } |
||||
| 196 | ?> |
||||
| 197 | <!-- Category Start--> |
||||
| 198 | <h4><?php _e( 'Select the post type from which you want to delete posts by category', 'bulk-delete' ); ?></h4> |
||||
| 199 | <fieldset class="options"> |
||||
| 200 | <table class="optiontable"> |
||||
| 201 | <?php bd_render_post_type_dropdown( 'cats' ); ?> |
||||
| 202 | </table> |
||||
| 203 | |||||
| 204 | <h4><?php _e( 'Select the categories from which you wan to delete posts', 'bulk-delete' ); ?></h4> |
||||
| 205 | <p><?php _e( 'Note: The post count below for each category is the total number of posts in that category, irrespective of post type', 'bulk-delete' ); ?>.</p> |
||||
| 206 | <?php |
||||
| 207 | $categories = get_categories( array( |
||||
| 208 | 'hide_empty' => false, |
||||
| 209 | ) |
||||
| 210 | ); |
||||
| 211 | ?> |
||||
| 212 | <table class="form-table"> |
||||
| 213 | <tr> |
||||
| 214 | <td scope="row"> |
||||
| 215 | <?php if( count($categories) > 50 ){?> |
||||
| 216 | <select class="select2Ajax" name="smbd_cats[]" data-term="category" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>" style="width:300px"> |
||||
| 217 | <option value="all"><?php _e( 'All Categories', 'bulk-delete' ); ?></option> |
||||
| 218 | </select> |
||||
| 219 | <?php }else{?> |
||||
| 220 | <select class="select2" name="smbd_cats[]" multiple data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"> |
||||
| 221 | <option value="all"><?php _e( 'All Categories', 'bulk-delete' ); ?></option> |
||||
| 222 | <?php foreach ( $categories as $category ) { ?> |
||||
| 223 | <option value="<?php echo $category->cat_ID; ?>"><?php echo $category->cat_name, ' (', $category->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option> |
||||
| 224 | <?php } ?> |
||||
| 225 | </select> |
||||
| 226 | <?php }?> |
||||
| 227 | </td> |
||||
| 228 | </tr> |
||||
| 229 | </table> |
||||
| 230 | |||||
| 231 | <table class="optiontable"> |
||||
| 232 | <?php bd_render_filtering_table_header(); ?> |
||||
| 233 | <?php bd_render_restrict_settings( 'cats' ); ?> |
||||
| 234 | <?php bd_render_delete_settings( 'cats' ); ?> |
||||
| 235 | <?php bd_render_private_post_settings( 'cats' ); ?> |
||||
| 236 | <?php bd_render_limit_settings( 'cats' ); ?> |
||||
| 237 | <?php bd_render_cron_settings( 'cats', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-category/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-sc' ); ?> |
||||
| 238 | </table> |
||||
| 239 | |||||
| 240 | </fieldset> |
||||
| 241 | <?php |
||||
| 242 | bd_render_submit_button( 'delete_posts_by_category' ); |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | /** |
||||
| 246 | * Process delete posts by category. |
||||
| 247 | * |
||||
| 248 | * @since 5.0 |
||||
| 249 | * @static |
||||
| 250 | */ |
||||
| 251 | public static function do_delete_posts_by_category() { |
||||
| 252 | $delete_options = array(); |
||||
| 253 | |||||
| 254 | $delete_options['post_type'] = bd_array_get( $_POST, 'smbd_cats_post_type', 'post' ); |
||||
| 255 | $delete_options['selected_cats'] = bd_array_get( $_POST, 'smbd_cats' ); |
||||
| 256 | $delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_cats_restrict', false ); |
||||
| 257 | $delete_options['private'] = bd_array_get_bool( $_POST, 'smbd_cats_private', false ); |
||||
| 258 | $delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_cats_limit_to', 0 ) ); |
||||
| 259 | $delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_cats_force_delete', false ); |
||||
| 260 | |||||
| 261 | $delete_options['date_op'] = bd_array_get( $_POST, 'smbd_cats_op' ); |
||||
| 262 | $delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_cats_days' ) ); |
||||
| 263 | |||||
| 264 | if ( bd_array_get_bool( $_POST, 'smbd_cats_cron', false ) ) { |
||||
| 265 | $freq = $_POST['smbd_cats_cron_freq']; |
||||
| 266 | $time = strtotime( $_POST['smbd_cats_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||||
| 267 | |||||
| 268 | if ( $freq == -1 ) { |
||||
| 269 | wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_CATEGORY, array( $delete_options ) ); |
||||
| 270 | } else { |
||||
| 271 | wp_schedule_event( $time, $freq , Bulk_Delete::CRON_HOOK_CATEGORY, array( $delete_options ) ); |
||||
| 272 | } |
||||
| 273 | |||||
| 274 | $msg = __( 'Posts from the selected categories are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
||||
| 275 | 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 ); |
||||
| 276 | } else { |
||||
| 277 | $deleted_count = self::delete_posts_by_category( $delete_options ); |
||||
| 278 | $msg = sprintf( _n( 'Deleted %d post from the selected categories', 'Deleted %d posts from the selected categories' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 279 | } |
||||
| 280 | |||||
| 281 | add_settings_error( |
||||
| 282 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 283 | 'deleted-posts', |
||||
| 284 | $msg, |
||||
| 285 | 'updated' |
||||
| 286 | ); |
||||
| 287 | } |
||||
| 288 | |||||
| 289 | /** |
||||
| 290 | * Delete posts by category. |
||||
| 291 | * |
||||
| 292 | * @since 5.0 |
||||
| 293 | * @static |
||||
| 294 | * |
||||
| 295 | * @param array $delete_options Options for deleting posts |
||||
| 296 | * |
||||
| 297 | * @return int $posts_deleted Number of posts that were deleted |
||||
| 298 | */ |
||||
| 299 | public static function delete_posts_by_category( $delete_options ) { |
||||
| 300 | // Backward compatibility code. Will be removed in Bulk Delete v6.0 |
||||
| 301 | $delete_options['post_type'] = bd_array_get( $delete_options, 'post_type', 'post' ); |
||||
| 302 | |||||
| 303 | if ( array_key_exists( 'cats_op', $delete_options ) ) { |
||||
| 304 | $delete_options['date_op'] = $delete_options['cats_op']; |
||||
| 305 | $delete_options['days'] = $delete_options['cats_days']; |
||||
| 306 | } |
||||
| 307 | |||||
| 308 | $delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
||||
| 309 | |||||
| 310 | $options = array(); |
||||
| 311 | $selected_cats = $delete_options['selected_cats']; |
||||
| 312 | if ( in_array( 'all', $selected_cats ) ) { |
||||
| 313 | $options['category__not__in'] = array(0); |
||||
| 314 | } else { |
||||
| 315 | $options['category__in'] = $selected_cats; |
||||
| 316 | } |
||||
| 317 | |||||
| 318 | $options = bd_build_query_options( $delete_options, $options ); |
||||
| 319 | $post_ids = bd_query( $options ); |
||||
| 320 | |||||
| 321 | foreach ( $post_ids as $post_id ) { |
||||
| 322 | // $force delete parameter to custom post types doesn't work |
||||
| 323 | if ( $delete_options['force_delete'] ) { |
||||
| 324 | wp_delete_post( $post_id, true ); |
||||
| 325 | } else { |
||||
| 326 | wp_trash_post( $post_id ); |
||||
| 327 | } |
||||
| 328 | } |
||||
| 329 | |||||
| 330 | return count( $post_ids ); |
||||
| 331 | } |
||||
| 332 | |||||
| 333 | /** |
||||
| 334 | * Render delete posts by tag box. |
||||
| 335 | */ |
||||
| 336 | public static function render_delete_posts_by_tag_box() { |
||||
| 337 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_TAG ) ) { |
||||
| 338 | 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::POSTS_PAGE_SLUG ); |
||||
| 339 | |||||
| 340 | return; |
||||
| 341 | } |
||||
| 342 | |||||
| 343 | $tags = get_tags(); |
||||
| 344 | if ( count( $tags ) > 0 ) { |
||||
| 345 | ?> |
||||
| 346 | <h4><?php _e( 'Select the tags from which you want to delete posts', 'bulk-delete' ) ?></h4> |
||||
| 347 | |||||
| 348 | <!-- Tags start--> |
||||
| 349 | <fieldset class="options"> |
||||
| 350 | <table class="form-table"> |
||||
| 351 | <tr> |
||||
| 352 | <td scope="row" colspan="2"> |
||||
| 353 | <?php if( count($tags) > 50 ){?> |
||||
| 354 | <select class="select2Ajax" name="smbd_tags[]" data-term="post_tag" multiple data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>" style="width:300px"> |
||||
| 355 | <option value="all"><?php _e( 'All Tags', 'bulk-delete' ); ?></option> |
||||
| 356 | </select> |
||||
| 357 | <?php } else{ ?> |
||||
| 358 | <select class="select2" name="smbd_tags[]" multiple data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"> |
||||
| 359 | <option value="all"><?php _e( 'All Tags', 'bulk-delete' ); ?></option> |
||||
| 360 | <?php foreach ( $tags as $tag ) { ?> |
||||
| 361 | <option value="<?php echo absint( $tag->term_id ); ?>"><?php echo $tag->name, ' (', $tag->count, ' ', __( 'Posts', 'bulk-delete' ), ')'; ?></option> |
||||
| 362 | <?php } ?> |
||||
| 363 | </select> |
||||
| 364 | <?php } ?> |
||||
| 365 | </td> |
||||
| 366 | </tr> |
||||
| 367 | </table> |
||||
| 368 | |||||
| 369 | <table class="optiontable"> |
||||
| 370 | <?php bd_render_filtering_table_header(); ?> |
||||
| 371 | <?php bd_render_restrict_settings( 'tags' ); ?> |
||||
| 372 | <?php bd_render_delete_settings( 'tags' ); ?> |
||||
| 373 | <?php bd_render_private_post_settings( 'tags' ); ?> |
||||
| 374 | <?php bd_render_limit_settings( 'tags' ); ?> |
||||
| 375 | <?php bd_render_cron_settings( 'tags', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-tag/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-st' ); ?> |
||||
| 376 | </table> |
||||
| 377 | </fieldset> |
||||
| 378 | <?php |
||||
| 379 | bd_render_submit_button( 'delete_posts_by_tag' ); |
||||
| 380 | } else { |
||||
| 381 | ?> |
||||
| 382 | <h4><?php _e( "You don't have any posts assigned to tags in this blog.", 'bulk-delete' ) ?></h4> |
||||
| 383 | <?php |
||||
| 384 | } |
||||
| 385 | } |
||||
| 386 | |||||
| 387 | /** |
||||
| 388 | * Process Delete Posts by tag request. |
||||
| 389 | * |
||||
| 390 | * @static |
||||
| 391 | * |
||||
| 392 | * @since 5.0 |
||||
| 393 | */ |
||||
| 394 | public static function do_delete_posts_by_tag() { |
||||
| 395 | $delete_options = array(); |
||||
| 396 | $delete_options['selected_tags'] = bd_array_get( $_POST, 'smbd_tags' ); |
||||
| 397 | $delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_tags_restrict', false ); |
||||
| 398 | $delete_options['private'] = bd_array_get( $_POST, 'smbd_tags_private' ); |
||||
| 399 | $delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_tags_limit_to', 0 ) ); |
||||
| 400 | $delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_tags_force_delete', false ); |
||||
| 401 | |||||
| 402 | $delete_options['date_op'] = bd_array_get( $_POST, 'smbd_tags_op' ); |
||||
| 403 | $delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_tags_days' ) ); |
||||
| 404 | |||||
| 405 | if ( bd_array_get( $_POST, 'smbd_tags_cron', 'false' ) == 'true' ) { |
||||
| 406 | $freq = $_POST['smbd_tags_cron_freq']; |
||||
| 407 | $time = strtotime( $_POST['smbd_tags_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||||
| 408 | |||||
| 409 | if ( $freq == -1 ) { |
||||
| 410 | wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_TAG, array( $delete_options ) ); |
||||
| 411 | } else { |
||||
| 412 | wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_TAG, array( $delete_options ) ); |
||||
| 413 | } |
||||
| 414 | $msg = __( 'Posts from the selected tags are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
||||
| 415 | 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 ); |
||||
| 416 | } else { |
||||
| 417 | $deleted_count = self::delete_posts_by_tag( $delete_options ); |
||||
| 418 | $msg = sprintf( _n( 'Deleted %d post from the selected tags', 'Deleted %d posts from the selected tags' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 419 | } |
||||
| 420 | |||||
| 421 | add_settings_error( |
||||
| 422 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 423 | 'deleted-posts', |
||||
| 424 | $msg, |
||||
| 425 | 'updated' |
||||
| 426 | ); |
||||
| 427 | } |
||||
| 428 | |||||
| 429 | /** |
||||
| 430 | * Delete posts by tag. |
||||
| 431 | * |
||||
| 432 | * @since 5.0 |
||||
| 433 | * @static |
||||
| 434 | * |
||||
| 435 | * @param array $delete_options Options for deleting posts |
||||
| 436 | * |
||||
| 437 | * @return int $posts_deleted Number of posts that were deleted |
||||
| 438 | */ |
||||
| 439 | public static function delete_posts_by_tag( $delete_options ) { |
||||
| 440 | // Backward compatibility code. Will be removed in Bulk Delete v6.0 |
||||
| 441 | if ( array_key_exists( 'tags_op', $delete_options ) ) { |
||||
| 442 | $delete_options['date_op'] = $delete_options['tags_op']; |
||||
| 443 | $delete_options['days'] = $delete_options['tags_days']; |
||||
| 444 | } |
||||
| 445 | |||||
| 446 | $delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
||||
| 447 | |||||
| 448 | $options = array(); |
||||
| 449 | $selected_tags = $delete_options['selected_tags']; |
||||
| 450 | if ( in_array( 'all', $selected_tags ) ) { |
||||
| 451 | $options['tag__not__in'] = array(0); |
||||
| 452 | } else { |
||||
| 453 | $options['tag__in'] = $selected_tags; |
||||
| 454 | } |
||||
| 455 | |||||
| 456 | $options = bd_build_query_options( $delete_options, $options ); |
||||
| 457 | $post_ids = bd_query( $options ); |
||||
| 458 | foreach ( $post_ids as $post_id ) { |
||||
| 459 | wp_delete_post( $post_id, $delete_options['force_delete'] ); |
||||
| 460 | } |
||||
| 461 | |||||
| 462 | return count( $post_ids ); |
||||
| 463 | } |
||||
| 464 | |||||
| 465 | /** |
||||
| 466 | * Render delete by custom taxonomy box. |
||||
| 467 | */ |
||||
| 468 | public static function render_delete_posts_by_taxonomy_box() { |
||||
| 469 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_TAX ) ) { |
||||
| 470 | 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::POSTS_PAGE_SLUG ); |
||||
| 471 | |||||
| 472 | return; |
||||
| 473 | } |
||||
| 474 | |||||
| 475 | $taxs = get_taxonomies( array( |
||||
| 476 | 'public' => true, |
||||
| 477 | '_builtin' => false, |
||||
| 478 | ), 'objects' |
||||
| 479 | ); |
||||
| 480 | |||||
| 481 | $terms_array = array(); |
||||
| 482 | if ( count( $taxs ) > 0 ) { |
||||
| 483 | foreach ( $taxs as $tax ) { |
||||
| 484 | $terms = get_terms( $tax->name ); |
||||
| 485 | if ( count( $terms ) > 0 ) { |
||||
|
0 ignored issues
–
show
It seems like
$terms can also be of type WP_Error; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 486 | $terms_array[$tax->name] = $terms; |
||||
| 487 | } |
||||
| 488 | } |
||||
| 489 | } |
||||
| 490 | |||||
| 491 | if ( count( $terms_array ) > 0 ) { |
||||
| 492 | ?> |
||||
| 493 | <!-- Custom tax Start--> |
||||
| 494 | <h4><?php _e( 'Select the post type from which you want to delete posts by custom taxonomy', 'bulk-delete' ); ?></h4> |
||||
| 495 | |||||
| 496 | <fieldset class="options"> |
||||
| 497 | <table class="optiontable"> |
||||
| 498 | <?php bd_render_post_type_dropdown( 'tax' ); ?> |
||||
| 499 | </table> |
||||
| 500 | |||||
| 501 | <h4><?php _e( 'Select the taxonomies from which you want to delete posts', 'bulk-delete' ) ?></h4> |
||||
| 502 | |||||
| 503 | <table class="optiontable"> |
||||
| 504 | <?php |
||||
| 505 | foreach ( $terms_array as $tax => $terms ) { |
||||
| 506 | ?> |
||||
| 507 | <tr> |
||||
| 508 | <td scope="row" > |
||||
| 509 | <input name="smbd_taxs" value="<?php echo $tax; ?>" type="radio" class="custom-tax"> |
||||
| 510 | </td> |
||||
| 511 | <td> |
||||
| 512 | <label for="smbd_taxs"><?php echo $taxs[$tax]->labels->name; ?> </label> |
||||
| 513 | </td> |
||||
| 514 | </tr> |
||||
| 515 | <?php |
||||
| 516 | } |
||||
| 517 | ?> |
||||
| 518 | </table> |
||||
| 519 | |||||
| 520 | <h4><?php _e( 'The selected taxonomy has the following terms. Select the terms from which you want to delete posts', 'bulk-delete' ) ?></h4> |
||||
| 521 | <p><?php _e( 'Note: The post count below for each term is the total number of posts in that term, irrespective of post type', 'bulk-delete' ); ?>.</p> |
||||
| 522 | <?php |
||||
| 523 | foreach ( $terms_array as $tax => $terms ) { |
||||
| 524 | ?> |
||||
| 525 | <table class="optiontable terms_<?php echo $tax;?> terms"> |
||||
| 526 | <?php |
||||
| 527 | foreach ( $terms as $term ) { |
||||
| 528 | ?> |
||||
| 529 | <tr> |
||||
| 530 | <td scope="row" > |
||||
| 531 | <input name="smbd_tax_terms[]" value="<?php echo $term->slug; ?>" type="checkbox" class="terms"> |
||||
| 532 | </td> |
||||
| 533 | <td> |
||||
| 534 | <label for="smbd_tax_terms"><?php echo $term->name; ?> (<?php echo $term->count . ' '; _e( 'Posts', 'bulk-delete' ); ?>)</label> |
||||
| 535 | </td> |
||||
| 536 | </tr> |
||||
| 537 | <?php |
||||
| 538 | } |
||||
| 539 | ?> |
||||
| 540 | </table> |
||||
| 541 | <?php |
||||
| 542 | } |
||||
| 543 | ?> |
||||
| 544 | <table class="optiontable"> |
||||
| 545 | <?php bd_render_filtering_table_header(); ?> |
||||
| 546 | <?php bd_render_restrict_settings( 'taxs' ); ?> |
||||
| 547 | <?php bd_render_delete_settings( 'taxs' ); ?> |
||||
| 548 | <?php bd_render_private_post_settings( 'taxs' ); ?> |
||||
| 549 | <?php bd_render_limit_settings( 'taxs' ); ?> |
||||
| 550 | <?php bd_render_cron_settings( 'taxs', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-taxonomy/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-stx' ); ?> |
||||
| 551 | </table> |
||||
| 552 | |||||
| 553 | </fieldset> |
||||
| 554 | <?php |
||||
| 555 | bd_render_submit_button( 'delete_posts_by_taxonomy' ); |
||||
| 556 | } else { |
||||
| 557 | ?> |
||||
| 558 | <h4><?php _e( "This WordPress installation doesn't have any non-empty custom taxonomies defined", 'bulk-delete' ) ?></h4> |
||||
| 559 | <?php |
||||
| 560 | } |
||||
| 561 | } |
||||
| 562 | |||||
| 563 | /** |
||||
| 564 | * Process Delete posts by Taxonomy Request. |
||||
| 565 | * |
||||
| 566 | * @static |
||||
| 567 | * |
||||
| 568 | * @since 5.0 |
||||
| 569 | */ |
||||
| 570 | public static function do_delete_posts_by_taxonomy() { |
||||
| 571 | $delete_options = array(); |
||||
| 572 | $delete_options['post_type'] = bd_array_get( $_POST, 'smbd_tax_post_type', 'post' ); |
||||
| 573 | $delete_options['selected_taxs'] = bd_array_get( $_POST, 'smbd_taxs' ); |
||||
| 574 | $delete_options['selected_tax_terms'] = bd_array_get( $_POST, 'smbd_tax_terms' ); |
||||
| 575 | $delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_taxs_restrict', false ); |
||||
| 576 | $delete_options['private'] = bd_array_get_bool( $_POST, 'smbd_taxs_private' ); |
||||
| 577 | $delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_taxs_limit_to', 0 ) ); |
||||
| 578 | $delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_taxs_force_delete', false ); |
||||
| 579 | |||||
| 580 | $delete_options['date_op'] = bd_array_get( $_POST, 'smbd_taxs_op' ); |
||||
| 581 | $delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_taxs_days' ) ); |
||||
| 582 | |||||
| 583 | if ( bd_array_get( $_POST, 'smbd_taxs_cron', 'false' ) == 'true' ) { |
||||
| 584 | $freq = $_POST['smbd_taxs_cron_freq']; |
||||
| 585 | $time = strtotime( $_POST['smbd_taxs_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||||
| 586 | |||||
| 587 | if ( $freq == -1 ) { |
||||
| 588 | wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_TAXONOMY, array( $delete_options ) ); |
||||
| 589 | } else { |
||||
| 590 | wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_TAXONOMY, array( $delete_options ) ); |
||||
| 591 | } |
||||
| 592 | $msg = __( 'Posts from the selected custom taxonomies are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
||||
| 593 | 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 ); |
||||
| 594 | } else { |
||||
| 595 | $deleted_count = self::delete_posts_by_taxonomy( $delete_options ); |
||||
| 596 | $msg = sprintf( _n( 'Deleted %d post from the selected custom taxonomies', 'Deleted %d posts from the selected custom taxonomies' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 597 | } |
||||
| 598 | |||||
| 599 | add_settings_error( |
||||
| 600 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 601 | 'deleted-posts', |
||||
| 602 | $msg, |
||||
| 603 | 'updated' |
||||
| 604 | ); |
||||
| 605 | } |
||||
| 606 | |||||
| 607 | /** |
||||
| 608 | * Delete posts by custom taxonomy. |
||||
| 609 | * |
||||
| 610 | * @since 5.0 |
||||
| 611 | * @static |
||||
| 612 | * |
||||
| 613 | * @param array $delete_options Options for deleting posts |
||||
| 614 | * |
||||
| 615 | * @return int $posts_deleted Number of posts that were deleted |
||||
| 616 | */ |
||||
| 617 | public static function delete_posts_by_taxonomy( $delete_options ) { |
||||
| 618 | // For compatibility reasons set default post type to 'post' |
||||
| 619 | $post_type = bd_array_get( $delete_options, 'post_type', 'post' ); |
||||
| 620 | |||||
| 621 | if ( array_key_exists( 'taxs_op', $delete_options ) ) { |
||||
| 622 | $delete_options['date_op'] = $delete_options['taxs_op']; |
||||
| 623 | $delete_options['days'] = $delete_options['taxs_days']; |
||||
| 624 | } |
||||
| 625 | |||||
| 626 | $delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
||||
| 627 | |||||
| 628 | $selected_taxs = $delete_options['selected_taxs']; |
||||
| 629 | $selected_tax_terms = $delete_options['selected_tax_terms']; |
||||
| 630 | |||||
| 631 | $options = array( |
||||
| 632 | 'post_status' => 'publish', |
||||
| 633 | 'post_type' => $post_type, |
||||
| 634 | 'tax_query' => array( |
||||
| 635 | array( |
||||
| 636 | 'taxonomy' => $selected_taxs, |
||||
| 637 | 'terms' => $selected_tax_terms, |
||||
| 638 | 'field' => 'slug', |
||||
| 639 | ), |
||||
| 640 | ), |
||||
| 641 | ); |
||||
| 642 | |||||
| 643 | $options = bd_build_query_options( $delete_options, $options ); |
||||
| 644 | $post_ids = bd_query( $options ); |
||||
| 645 | foreach ( $post_ids as $post_id ) { |
||||
| 646 | // $force delete parameter to custom post types doesn't work |
||||
| 647 | if ( $delete_options['force_delete'] ) { |
||||
| 648 | wp_delete_post( $post_id, true ); |
||||
| 649 | } else { |
||||
| 650 | wp_trash_post( $post_id ); |
||||
| 651 | } |
||||
| 652 | } |
||||
| 653 | |||||
| 654 | return count( $post_ids ); |
||||
| 655 | } |
||||
| 656 | |||||
| 657 | /** |
||||
| 658 | * Render delete by custom post type box. |
||||
| 659 | * |
||||
| 660 | * @static |
||||
| 661 | */ |
||||
| 662 | public static function render_delete_posts_by_post_type_box() { |
||||
| 663 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_TYPE ) ) { |
||||
| 664 | 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::POSTS_PAGE_SLUG ); |
||||
| 665 | |||||
| 666 | return; |
||||
| 667 | } |
||||
| 668 | |||||
| 669 | $types_array = array(); |
||||
| 670 | |||||
| 671 | $types = get_post_types( array( |
||||
| 672 | '_builtin' => false, |
||||
| 673 | ), 'names' |
||||
| 674 | ); |
||||
| 675 | |||||
| 676 | if ( count( $types ) > 0 ) { |
||||
| 677 | foreach ( $types as $type ) { |
||||
| 678 | $post_count = wp_count_posts( $type ); |
||||
| 679 | if ( $post_count->publish > 0 ) { |
||||
| 680 | $types_array["$type-publish"] = $post_count->publish; |
||||
| 681 | } |
||||
| 682 | if ( $post_count->future > 0 ) { |
||||
| 683 | $types_array["$type-future"] = $post_count->future; |
||||
| 684 | } |
||||
| 685 | if ( $post_count->pending > 0 ) { |
||||
| 686 | $types_array["$type-pending"] = $post_count->pending; |
||||
| 687 | } |
||||
| 688 | if ( $post_count->draft > 0 ) { |
||||
| 689 | $types_array["$type-draft"] = $post_count->draft; |
||||
| 690 | } |
||||
| 691 | if ( $post_count->private > 0 ) { |
||||
| 692 | $types_array["$type-private"] = $post_count->private; |
||||
| 693 | } |
||||
| 694 | } |
||||
| 695 | } |
||||
| 696 | |||||
| 697 | if ( count( $types_array ) > 0 ) { |
||||
| 698 | ?> |
||||
| 699 | <!-- Custom post type Start--> |
||||
| 700 | <h4><?php _e( 'Select the custom post types from which you want to delete posts', 'bulk-delete' ) ?></h4> |
||||
| 701 | |||||
| 702 | <fieldset class="options"> |
||||
| 703 | <table class="optiontable"> |
||||
| 704 | <?php |
||||
| 705 | foreach ( $types_array as $type => $count ) { |
||||
| 706 | ?> |
||||
| 707 | <tr> |
||||
| 708 | <td scope="row" > |
||||
| 709 | <input name="smbd_types[]" value="<?php echo $type; ?>" type="checkbox"> |
||||
| 710 | </td> |
||||
| 711 | <td> |
||||
| 712 | <label for="smbd_types"><?php echo BD_Util::display_post_type_status( $type ), ' (', $count, ')'; ?></label> |
||||
| 713 | </td> |
||||
| 714 | </tr> |
||||
| 715 | <?php |
||||
| 716 | } |
||||
| 717 | |||||
| 718 | bd_render_filtering_table_header(); |
||||
| 719 | bd_render_restrict_settings( 'types' ); |
||||
| 720 | bd_render_delete_settings( 'types' ); |
||||
| 721 | bd_render_limit_settings( 'types' ); |
||||
| 722 | bd_render_cron_settings( 'types', 'http://bulkwp.com/addons/scheduler-for-deleting-posts-by-post-type/?utm_source=wpadmin&utm_campaign=BulkDelete&utm_medium=buynow&utm_content=bd-spt' ); |
||||
| 723 | ?> |
||||
| 724 | </table> |
||||
| 725 | </fieldset> |
||||
| 726 | <?php |
||||
| 727 | bd_render_submit_button( 'delete_posts_by_post_type' ); |
||||
| 728 | } else { |
||||
| 729 | printf( '<h4>%s</h4>', __( "This WordPress installation doesn't have any non-empty custom post types", 'bulk-delete' ) ); |
||||
| 730 | } |
||||
| 731 | } |
||||
| 732 | |||||
| 733 | /** |
||||
| 734 | * Process request to delete posts by post type. |
||||
| 735 | * |
||||
| 736 | * @static |
||||
| 737 | * |
||||
| 738 | * @since 5.0 |
||||
| 739 | */ |
||||
| 740 | public static function do_delete_posts_by_post_type() { |
||||
| 741 | $delete_options = array(); |
||||
| 742 | |||||
| 743 | $delete_options['selected_types'] = bd_array_get( $_POST, 'smbd_types' ); |
||||
| 744 | $delete_options['restrict'] = bd_array_get_bool( $_POST, 'smbd_types_restrict', false ); |
||||
| 745 | $delete_options['limit_to'] = absint( bd_array_get( $_POST, 'smbd_types_limit_to', 0 ) ); |
||||
| 746 | $delete_options['force_delete'] = bd_array_get_bool( $_POST, 'smbd_types_force_delete', false ); |
||||
| 747 | |||||
| 748 | $delete_options['date_op'] = bd_array_get( $_POST, 'smbd_types_op' ); |
||||
| 749 | $delete_options['days'] = absint( bd_array_get( $_POST, 'smbd_types_days' ) ); |
||||
| 750 | |||||
| 751 | if ( bd_array_get( $_POST, 'smbd_types_cron', 'false' ) == 'true' ) { |
||||
| 752 | $freq = $_POST['smbd_types_cron_freq']; |
||||
| 753 | $time = strtotime( $_POST['smbd_types_cron_start'] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||||
| 754 | |||||
| 755 | if ( $freq == -1 ) { |
||||
| 756 | wp_schedule_single_event( $time, Bulk_Delete::CRON_HOOK_POST_TYPE, array( $delete_options ) ); |
||||
| 757 | } else { |
||||
| 758 | wp_schedule_event( $time, $freq, Bulk_Delete::CRON_HOOK_POST_TYPE, array( $delete_options ) ); |
||||
| 759 | } |
||||
| 760 | |||||
| 761 | $msg = __( 'Posts from the selected custom post type are scheduled for deletion.', 'bulk-delete' ) . ' ' . |
||||
| 762 | 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 ); |
||||
| 763 | } else { |
||||
| 764 | $deleted_count = self::delete_posts_by_post_type( $delete_options ); |
||||
| 765 | $msg = sprintf( _n( 'Deleted %d post from the selected custom post type', 'Deleted %d posts from the selected custom post type' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 766 | } |
||||
| 767 | |||||
| 768 | add_settings_error( |
||||
| 769 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 770 | 'deleted-posts', |
||||
| 771 | $msg, |
||||
| 772 | 'updated' |
||||
| 773 | ); |
||||
| 774 | } |
||||
| 775 | |||||
| 776 | /** |
||||
| 777 | * Delete posts by custom post type. |
||||
| 778 | * |
||||
| 779 | * @static |
||||
| 780 | * |
||||
| 781 | * @since 5.0 |
||||
| 782 | * |
||||
| 783 | * @param array $delete_options Options for deleting posts |
||||
| 784 | * |
||||
| 785 | * @return int $posts_deleted Number of posts that were deleted |
||||
| 786 | */ |
||||
| 787 | public static function delete_posts_by_post_type( $delete_options ) { |
||||
| 788 | // Backward compatibility code. Will be removed in Bulk Delete v6.0 |
||||
| 789 | if ( array_key_exists( 'types_op', $delete_options ) ) { |
||||
| 790 | $delete_options['date_op'] = $delete_options['types_op']; |
||||
| 791 | $delete_options['days'] = $delete_options['types_days']; |
||||
| 792 | } |
||||
| 793 | |||||
| 794 | $delete_options = apply_filters( 'bd_delete_options', $delete_options ); |
||||
| 795 | |||||
| 796 | $count = 0; |
||||
| 797 | $selected_types = $delete_options['selected_types']; |
||||
| 798 | |||||
| 799 | foreach ( $selected_types as $selected_type ) { |
||||
| 800 | $type_status = BD_Util::split_post_type_status( $selected_type ); |
||||
| 801 | |||||
| 802 | $type = $type_status['type']; |
||||
| 803 | $status = $type_status['status']; |
||||
| 804 | |||||
| 805 | $options = array( |
||||
| 806 | 'post_status' => $status, |
||||
| 807 | 'post_type' => $type, |
||||
| 808 | ); |
||||
| 809 | |||||
| 810 | $options = bd_build_query_options( $delete_options, $options ); |
||||
| 811 | $post_ids = bd_query( $options ); |
||||
| 812 | foreach ( $post_ids as $post_id ) { |
||||
| 813 | // $force delete parameter to custom post types doesn't work |
||||
| 814 | if ( $delete_options['force_delete'] ) { |
||||
| 815 | wp_delete_post( $post_id, true ); |
||||
| 816 | } else { |
||||
| 817 | wp_trash_post( $post_id ); |
||||
| 818 | } |
||||
| 819 | } |
||||
| 820 | |||||
| 821 | $count += count( $post_ids ); |
||||
| 822 | } |
||||
| 823 | |||||
| 824 | return $count; |
||||
| 825 | } |
||||
| 826 | |||||
| 827 | /** |
||||
| 828 | * Render delete by url box. |
||||
| 829 | * |
||||
| 830 | * @static |
||||
| 831 | */ |
||||
| 832 | public static function render_delete_posts_by_url_box() { |
||||
| 833 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_URL ) ) { |
||||
| 834 | 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::POSTS_PAGE_SLUG ); |
||||
| 835 | |||||
| 836 | return; |
||||
| 837 | } |
||||
| 838 | ?> |
||||
| 839 | <!-- URLs start--> |
||||
| 840 | <h4><?php _e( 'Delete posts and pages that have the following Permalink', 'bulk-delete' ); ?></h4> |
||||
| 841 | |||||
| 842 | <fieldset class="options"> |
||||
| 843 | <table class="optiontable"> |
||||
| 844 | <tr> |
||||
| 845 | <td scope="row" colspan="2"> |
||||
| 846 | <label for="smdb_specific_pages"><?php _e( 'Enter one post url (not post ids) per line', 'bulk-delete' ); ?></label> |
||||
| 847 | <br> |
||||
| 848 | <textarea style="width: 450px; height: 80px;" id="smdb_specific_pages_urls" name="smdb_specific_pages_urls" rows="5" columns="80"></textarea> |
||||
| 849 | </td> |
||||
| 850 | </tr> |
||||
| 851 | |||||
| 852 | <?php bd_render_filtering_table_header(); ?> |
||||
| 853 | <?php bd_render_delete_settings( 'specific' ); ?> |
||||
| 854 | |||||
| 855 | </table> |
||||
| 856 | </fieldset> |
||||
| 857 | <?php |
||||
| 858 | bd_render_submit_button( 'delete_posts_by_url' ); |
||||
| 859 | } |
||||
| 860 | |||||
| 861 | /** |
||||
| 862 | * Delete posts by url. |
||||
| 863 | * |
||||
| 864 | * @static |
||||
| 865 | * |
||||
| 866 | * @since 5.0 |
||||
| 867 | */ |
||||
| 868 | public static function do_delete_posts_by_url() { |
||||
| 869 | $force_delete = bd_array_get_bool( $_POST, 'smbd_specific_force_delete', false ); |
||||
| 870 | |||||
| 871 | $urls = preg_split( '/\r\n|\r|\n/', bd_array_get( $_POST, 'smdb_specific_pages_urls' ) ); |
||||
| 872 | foreach ( $urls as $url ) { |
||||
| 873 | $checkedurl = $url; |
||||
| 874 | if ( substr( $checkedurl , 0, 1 ) == '/' ) { |
||||
| 875 | $checkedurl = get_site_url() . $checkedurl ; |
||||
| 876 | } |
||||
| 877 | $postid = url_to_postid( $checkedurl ); |
||||
| 878 | wp_delete_post( $postid, $force_delete ); |
||||
| 879 | } |
||||
| 880 | |||||
| 881 | $deleted_count = count( $urls ); |
||||
| 882 | $msg = sprintf( _n( 'Deleted %d post with the specified urls', 'Deleted %d posts with the specified urls' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 883 | |||||
| 884 | add_settings_error( |
||||
| 885 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 886 | 'deleted-posts', |
||||
| 887 | $msg, |
||||
| 888 | 'updated' |
||||
| 889 | ); |
||||
| 890 | } |
||||
| 891 | |||||
| 892 | /** |
||||
| 893 | * Render delete by post revisions box. |
||||
| 894 | * |
||||
| 895 | * @static |
||||
| 896 | */ |
||||
| 897 | public static function render_posts_by_revision_box() { |
||||
| 898 | global $wpdb; |
||||
| 899 | |||||
| 900 | if ( BD_Util::is_posts_box_hidden( Bulk_Delete::BOX_POST_REVISION ) ) { |
||||
| 901 | 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::POSTS_PAGE_SLUG ); |
||||
| 902 | |||||
| 903 | return; |
||||
| 904 | } |
||||
| 905 | |||||
| 906 | $revisions = $wpdb->get_var( "select count(*) from $wpdb->posts where post_type = 'revision'" ); |
||||
| 907 | ?> |
||||
| 908 | <!-- Post Revisions start--> |
||||
| 909 | <h4><?php _e( 'Select the posts which you want to delete', 'bulk-delete' ); ?></h4> |
||||
| 910 | |||||
| 911 | <fieldset class="options"> |
||||
| 912 | <table class="optiontable"> |
||||
| 913 | <tr> |
||||
| 914 | <td> |
||||
| 915 | <input name="smbd_revisions" id ="smbd_revisions" value="revisions" type="checkbox"> |
||||
| 916 | <label for="smbd_revisions"><?php _e( 'All Revisions', 'bulk-delete' ); ?> (<?php echo $revisions . ' '; _e( 'Revisions', 'bulk-delete' ); ?>)</label> |
||||
| 917 | </td> |
||||
| 918 | </tr> |
||||
| 919 | |||||
| 920 | </table> |
||||
| 921 | </fieldset> |
||||
| 922 | <?php |
||||
| 923 | bd_render_submit_button( 'delete_posts_by_revision' ); |
||||
| 924 | } |
||||
| 925 | |||||
| 926 | /** |
||||
| 927 | * Process delete revisions request. |
||||
| 928 | * |
||||
| 929 | * @static |
||||
| 930 | * |
||||
| 931 | * @since 5.0 |
||||
| 932 | */ |
||||
| 933 | public static function do_delete_posts_by_revision() { |
||||
| 934 | $delete_options = array( 'revisions' => bd_array_get( $_POST, 'smbd_revisions' ) ); |
||||
| 935 | |||||
| 936 | $deleted_count = self::delete_posts_by_revision( $delete_options ); |
||||
| 937 | |||||
| 938 | $msg = sprintf( _n( 'Deleted %d post revision', 'Deleted %d post revisions' , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||||
| 939 | |||||
| 940 | add_settings_error( |
||||
| 941 | Bulk_Delete::POSTS_PAGE_SLUG, |
||||
| 942 | 'deleted-posts', |
||||
| 943 | $msg, |
||||
| 944 | 'updated' |
||||
| 945 | ); |
||||
| 946 | } |
||||
| 947 | |||||
| 948 | /** |
||||
| 949 | * Delete all post revisions. |
||||
| 950 | * |
||||
| 951 | * @since 5.0 |
||||
| 952 | * @static |
||||
| 953 | * |
||||
| 954 | * @param array $delete_options |
||||
| 955 | * |
||||
| 956 | * @return int The number of posts that were deleted |
||||
| 957 | */ |
||||
| 958 | public static function delete_posts_by_revision( $delete_options ) { |
||||
| 959 | global $wpdb; |
||||
| 960 | |||||
| 961 | // Revisions |
||||
| 962 | if ( 'revisions' == $delete_options['revisions'] ) { |
||||
| 963 | $revisions = $wpdb->get_results( "select ID from $wpdb->posts where post_type = 'revision'" ); |
||||
| 964 | |||||
| 965 | foreach ( $revisions as $revision ) { |
||||
| 966 | wp_delete_post( $revision->ID ); |
||||
| 967 | } |
||||
| 968 | |||||
| 969 | return count( $revisions ); |
||||
| 970 | } |
||||
| 971 | |||||
| 972 | return 0; |
||||
| 973 | } |
||||
| 974 | |||||
| 975 | /** |
||||
| 976 | * Filter JS Array and add validation hooks. |
||||
| 977 | * |
||||
| 978 | * @since 5.4 |
||||
| 979 | * @static |
||||
| 980 | * |
||||
| 981 | * @param array $js_array JavaScript Array |
||||
| 982 | * |
||||
| 983 | * @return array Modified JavaScript Array |
||||
| 984 | */ |
||||
| 985 | public static function filter_js_array( $js_array ) { |
||||
| 986 | $js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' ); |
||||
| 987 | $js_array['msg']['selectPostOption'] = __( 'Please select posts from at least one option', 'bulk-delete' ); |
||||
| 988 | |||||
| 989 | $js_array['validators']['delete_posts_by_category'] = 'validateSelect2'; |
||||
| 990 | $js_array['error_msg']['delete_posts_by_category'] = 'selectCategory'; |
||||
| 991 | $js_array['msg']['selectCategory'] = __( 'Please select at least one category', 'bulk-delete' ); |
||||
| 992 | |||||
| 993 | $js_array['validators']['delete_posts_by_tag'] = 'validateSelect2'; |
||||
| 994 | $js_array['error_msg']['delete_posts_by_category'] = 'selectTag'; |
||||
| 995 | $js_array['msg']['selectTag'] = __( 'Please select at least one tag', 'bulk-delete' ); |
||||
| 996 | |||||
| 997 | $js_array['validators']['delete_posts_by_url'] = 'validateUrl'; |
||||
| 998 | $js_array['error_msg']['delete_posts_by_url'] = 'enterUrl'; |
||||
| 999 | $js_array['msg']['enterUrl'] = __( 'Please enter at least one post url', 'bulk-delete' ); |
||||
| 1000 | |||||
| 1001 | $js_array['dt_iterators'][] = '_cats'; |
||||
| 1002 | $js_array['dt_iterators'][] = '_tags'; |
||||
| 1003 | $js_array['dt_iterators'][] = '_taxs'; |
||||
| 1004 | $js_array['dt_iterators'][] = '_types'; |
||||
| 1005 | $js_array['dt_iterators'][] = '_post_status'; |
||||
| 1006 | |||||
| 1007 | return $js_array; |
||||
| 1008 | } |
||||
| 1009 | |||||
| 1010 | /** |
||||
| 1011 | * Process delete cron job request. |
||||
| 1012 | * This should ideally go in a separate class. But I was |
||||
| 1013 | * lazy to create a separate class for a single function. |
||||
| 1014 | * |
||||
| 1015 | * @since 5.0 |
||||
| 1016 | * @static |
||||
| 1017 | */ |
||||
| 1018 | public static function do_delete_cron() { |
||||
| 1019 | $cron_id = absint( $_GET['cron_id'] ); |
||||
| 1020 | $cron_items = BD_Util::get_cron_schedules(); |
||||
| 1021 | wp_unschedule_event( $cron_items[$cron_id]['timestamp'], $cron_items[$cron_id]['type'], $cron_items[$cron_id]['args'] ); |
||||
| 1022 | |||||
| 1023 | $msg = __( 'The selected scheduled job was successfully deleted ', 'bulk-delete' ); |
||||
| 1024 | |||||
| 1025 | add_settings_error( |
||||
| 1026 | Bulk_Delete::CRON_PAGE_SLUG, |
||||
| 1027 | 'deleted-cron', |
||||
| 1028 | $msg, |
||||
| 1029 | 'updated' |
||||
| 1030 | ); |
||||
| 1031 | } |
||||
| 1032 | } |
||||
| 1033 | |||||
| 1034 | // hooks |
||||
| 1035 | 1 | add_action( 'bd_delete_posts_by_status' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_status' ) ); |
|||
| 1036 | 1 | add_action( 'bd_delete_posts_by_category' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_category' ) ); |
|||
| 1037 | 1 | add_action( 'bd_delete_posts_by_tag' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_tag' ) ); |
|||
| 1038 | 1 | add_action( 'bd_delete_posts_by_taxonomy' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_taxonomy' ) ); |
|||
| 1039 | 1 | add_action( 'bd_delete_posts_by_post_type' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_post_type' ) ); |
|||
| 1040 | 1 | add_action( 'bd_delete_posts_by_url' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_url' ) ); |
|||
| 1041 | 1 | add_action( 'bd_delete_posts_by_revision' , array( 'Bulk_Delete_Posts', 'do_delete_posts_by_revision' ) ); |
|||
| 1042 | |||||
| 1043 | 1 | add_action( 'bd_delete_cron' , array( 'Bulk_Delete_Posts', 'do_delete_cron' ) ); |
|||
| 1044 | 1 | add_filter( 'bd_javascript_array' , array( 'Bulk_Delete_Posts', 'filter_js_array' ) ); |
|||
| 1045 | ?> |
||||
| 1046 |