1
|
|
|
<?php |
2
|
|
|
namespace BulkWP\BulkDelete\Core\Posts; |
3
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
5
|
|
|
|
6
|
1 |
|
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
|
177 |
|
protected function build_query_options( $options, $query ) { |
57
|
177 |
|
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
|
177 |
|
protected function query( $query ) { |
68
|
177 |
|
return bd_query( $query ); |
69
|
|
|
} |
70
|
|
|
|
71
|
163 |
|
protected function do_delete( $options ) { |
72
|
163 |
|
$query = $this->build_query( $options ); |
73
|
|
|
|
74
|
163 |
|
if ( empty( $query ) ) { |
75
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
76
|
|
|
return 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
163 |
|
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
|
177 |
|
protected function delete_posts_from_query( $query, $options ) { |
91
|
177 |
|
$query = $this->build_query_options( $options, $query ); |
92
|
177 |
|
$posts = $this->query( $query ); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* List of posts to be deleted. |
96
|
|
|
* |
97
|
|
|
* @since 6.0.1 |
98
|
|
|
* |
99
|
|
|
* @param array $posts List of posts to be deleted. It could be just post_ds. |
100
|
|
|
* @param array $options Delete options. |
101
|
|
|
* @param BaseModule $this Module that is triggering deletion. |
102
|
|
|
*/ |
103
|
177 |
|
$post_ids = apply_filters( 'bd_posts_to_be_deleted', $posts, $options, $this ); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Triggered before the posts are deleted. |
107
|
|
|
* |
108
|
|
|
* @since 6.0.0 |
109
|
|
|
* |
110
|
|
|
* @param array $post_ids List of post ids that are going to be deleted. |
111
|
|
|
* @param array $options List of Delete Options. |
112
|
|
|
*/ |
113
|
177 |
|
do_action( 'bd_before_deleting_posts', $post_ids, $options ); |
114
|
|
|
|
115
|
177 |
|
$delete_post_count = $this->delete_posts_by_id( $post_ids, $options['force_delete'] ); |
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Triggered after the posts are deleted. |
119
|
|
|
* |
120
|
|
|
* @since 6.0.0 |
121
|
|
|
* |
122
|
|
|
* @param array $options Delete Options. |
123
|
|
|
*/ |
124
|
177 |
|
do_action( 'bd_after_deleting_posts', $options ); |
125
|
|
|
|
126
|
177 |
|
return $delete_post_count; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Render the "private post" setting fields. |
131
|
|
|
*/ |
132
|
|
|
protected function render_private_post_settings() { |
133
|
|
|
if ( $this->are_private_posts_present() ) { |
134
|
|
|
bd_render_private_post_settings( $this->field_slug ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Delete sticky posts. |
140
|
|
|
* |
141
|
|
|
* @param bool $force_delete Whether to bypass trash and force deletion. |
142
|
|
|
* |
143
|
|
|
* @return int Number of posts deleted. |
144
|
|
|
*/ |
145
|
|
|
protected function delete_sticky_posts( $force_delete ) { |
146
|
|
|
$sticky_post_ids = get_option( 'sticky_posts' ); |
147
|
|
|
|
148
|
|
|
if ( ! is_array( $sticky_post_ids ) ) { |
149
|
|
|
return 0; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->delete_posts_by_id( $sticky_post_ids, $force_delete ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Delete posts by ids. |
157
|
|
|
* |
158
|
|
|
* @param int[] $post_ids List of post ids to delete. |
159
|
|
|
* @param bool $force_delete True to force delete posts, False otherwise. |
160
|
|
|
* |
161
|
|
|
* @return int Number of posts deleted. |
162
|
|
|
*/ |
163
|
186 |
|
protected function delete_posts_by_id( $post_ids, $force_delete ) { |
164
|
|
|
/** |
165
|
|
|
* Filter the list of post ids that will be excluded from deletion. |
166
|
|
|
* |
167
|
|
|
* @since 6.0.0 |
168
|
|
|
* |
169
|
|
|
* @param array $excluded_ids Post IDs to be excluded. |
170
|
|
|
*/ |
171
|
186 |
|
$excluded_post_ids = apply_filters( 'bd_excluded_post_ids', array() ); |
172
|
|
|
|
173
|
186 |
|
if ( is_array( $excluded_post_ids ) && ! empty( $excluded_post_ids ) ) { |
174
|
|
|
$post_ids = array_diff( $post_ids, $excluded_post_ids ); |
175
|
|
|
} |
176
|
|
|
|
177
|
186 |
|
foreach ( $post_ids as $post_id ) { |
178
|
|
|
// `$force_delete` parameter to `wp_delete_post` won't work for custom post types. |
179
|
|
|
// See https://core.trac.wordpress.org/ticket/43672 |
180
|
132 |
|
if ( $force_delete ) { |
181
|
54 |
|
wp_delete_post( $post_id, true ); |
182
|
|
|
} else { |
183
|
78 |
|
wp_trash_post( $post_id ); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
186 |
|
return count( $post_ids ); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|