| 1 |
<?php |
| 2 |
namespace BulkWP\BulkDelete\Core\Posts; |
| 3 |
|
| 4 |
use BulkWP\BulkDelete\Core\Base\BaseModule; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 7 |
|
| 8 |
/** |
| 9 |
* Module for deleting posts. |
| 10 |
* |
| 11 |
* @since 6.0.0 |
| 12 |
*/ |
| 13 |
abstract class PostsModule extends BaseModule { |
| 14 |
/** |
| 15 |
* Build query params for WP_Query by using delete options. |
| 16 |
* |
| 17 |
* Return an empty query array to short-circuit deletion. |
| 18 |
* |
| 19 |
* @param array $options Delete options. |
| 20 |
* |
| 21 |
* @return array Query. |
| 22 |
*/ |
| 23 |
abstract protected function build_query( $options ); |
| 24 |
|
| 25 |
protected $item_type = 'posts'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Handle common filters. |
| 29 |
* |
| 30 |
* @param array $request Request array. |
| 31 |
* |
| 32 |
* @return array User options. |
| 33 |
*/ |
| 34 |
protected function parse_common_filters( $request ) { |
| 35 |
$options = array(); |
| 36 |
|
| 37 |
$options['restrict'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false ); |
| 38 |
$options['limit_to'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) ); |
| 39 |
$options['exclude_sticky'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_exclude_sticky', false ); |
| 40 |
$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false ); |
| 41 |
|
| 42 |
$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' ); |
| 43 |
$options['days'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) ); |
| 44 |
|
| 45 |
return $options; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Helper function to build the query params. |
| 50 |
* |
| 51 |
* @param array $options Delete Options. |
| 52 |
* @param array $query Params for WP Query. |
| 53 |
* |
| 54 |
* @return array Delete options array |
| 55 |
*/ |
| 56 |
protected function build_query_options( $options, $query ) { |
| 57 |
return bd_build_query_options( $options, $query ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Helper function for bd_query which runs query. |
| 62 |
* |
| 63 |
* @param array $query Params for WP Query. |
| 64 |
* |
| 65 |
* @return array Deleted Post IDs array |
| 66 |
*/ |
| 67 |
protected function query( $query ) { |
| 68 |
return bd_query( $query ); |
| 69 |
} |
| 70 |
|
| 71 |
protected function do_delete( $options ) { |
| 72 |
$query = $this->build_query( $options ); |
| 73 |
|
| 74 |
if ( empty( $query ) ) { |
| 75 |
// Short circuit deletion, if nothing needs to be deleted. |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
return $this->delete_posts_from_query( $query, $options ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Build the query using query params and then Delete posts. |
| 84 |
* |
| 85 |
* @param array $query Params for WP Query. |
| 86 |
* @param array $options Delete Options. |
| 87 |
* |
| 88 |
* @return int Number of posts deleted. |
| 89 |
*/ |
| 90 |
protected function delete_posts_from_query( $query, $options ) { |
| 91 |
$query = $this->build_query_options( $options, $query ); |
| 92 |
$posts = $this->query( $query ); |
| 93 |
|
| 94 |
$post_ids = $this->prepare_posts_for_deletion( $posts, $options ); |
| 95 |
|
| 96 |
/** |
| 97 |
* Triggered before the posts are deleted. |
| 98 |
* |
| 99 |
* @since 6.0.0 |
| 100 |
* |
| 101 |
* @param array $post_ids List of post ids that are going to be deleted. |
| 102 |
* @param array $options List of Delete Options. |
| 103 |
*/ |
| 104 |
do_action( 'bd_before_deleting_posts', $post_ids, $options ); //phpcs:ignore |
| 105 |
|
| 106 |
$delete_post_count = $this->delete_posts_by_id( $post_ids, $options['force_delete'] ); |
| 107 |
|
| 108 |
/** |
| 109 |
* Triggered after the posts are deleted. |
| 110 |
* |
| 111 |
* @since 6.0.0 |
| 112 |
* |
| 113 |
* @param array $options Delete Options. |
| 114 |
*/ |
| 115 |
do_action( 'bd_after_deleting_posts', $options ); //phpcs:ignore |
| 116 |
|
| 117 |
return $delete_post_count; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Render the "private post" setting fields. |
| 122 |
*/ |
| 123 |
protected function render_private_post_settings() { |
| 124 |
if ( $this->are_private_posts_present() ) { |
| 125 |
bd_render_private_post_settings( $this->field_slug ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Delete sticky posts. |
| 131 |
* |
| 132 |
* @param bool $force_delete Whether to bypass trash and force deletion. |
| 133 |
* |
| 134 |
* @return int Number of posts deleted. |
| 135 |
*/ |
| 136 |
protected function delete_sticky_posts( $force_delete ) { |
| 137 |
$sticky_post_ids = get_option( 'sticky_posts' ); |
| 138 |
|
| 139 |
if ( ! is_array( $sticky_post_ids ) ) { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
return $this->delete_posts_by_id( $sticky_post_ids, $force_delete ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Delete posts by ids. |
| 148 |
* |
| 149 |
* @param int[] $post_ids List of post ids to delete. |
| 150 |
* @param bool $force_delete True to force delete posts, False otherwise. |
| 151 |
* |
| 152 |
* @return int Number of posts deleted. |
| 153 |
*/ |
| 154 |
protected function delete_posts_by_id( $post_ids, $force_delete ) { |
| 155 |
/** |
| 156 |
* Filter the list of post ids that will be excluded from deletion. |
| 157 |
* |
| 158 |
* @since 6.0.0 |
| 159 |
* |
| 160 |
* @param array $excluded_ids Post IDs to be excluded. |
| 161 |
*/ |
| 162 |
$excluded_post_ids = apply_filters( 'bd_excluded_post_ids', array() ); //phpcs:ignore |
| 163 |
|
| 164 |
if ( is_array( $excluded_post_ids ) && ! empty( $excluded_post_ids ) ) { |
| 165 |
$post_ids = array_diff( $post_ids, $excluded_post_ids ); |
| 166 |
} |
| 167 |
|
| 168 |
foreach ( $post_ids as $post_id ) { |
| 169 |
// `$force_delete` parameter to `wp_delete_post` won't work for custom post types. |
| 170 |
// See https://core.trac.wordpress.org/ticket/43672 |
| 171 |
if ( $force_delete ) { |
| 172 |
wp_delete_post( $post_id, true ); |
| 173 |
} else { |
| 174 |
wp_trash_post( $post_id ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return count( $post_ids ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Prepare posts for deletion. |
| 183 |
* |
| 184 |
* Individual modules can override this method to exclude posts from getting deleted. |
| 185 |
* |
| 186 |
* @since 6.0.2 |
| 187 |
* |
| 188 |
* @param int[]|\WP_Post[] $posts List of posts to be deleted. It could be just post_ids. |
| 189 |
* @param array $options Delete options. |
| 190 |
* |
| 191 |
* @return int[] List of post ids that should be deleted. |
| 192 |
*/ |
| 193 |
protected function prepare_posts_for_deletion( array $posts, array $options ) { |
| 194 |
$post_ids = array(); |
| 195 |
|
| 196 |
foreach ( $posts as $post ) { |
| 197 |
if ( $post instanceof \WP_Post ) { |
| 198 |
$post_ids[] = $post->ID; |
| 199 |
} else { |
| 200 |
$post_ids[] = $post; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return $post_ids; |
| 205 |
} |
| 206 |
} |
| 207 |
|