|
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['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false ); |
|
40
|
|
|
|
|
41
|
|
|
$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' ); |
|
42
|
|
|
$options['days'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) ); |
|
43
|
|
|
|
|
44
|
|
|
return $options; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function filter_js_array( $js_array ) { |
|
48
|
|
|
$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' ); |
|
49
|
|
|
$js_array['msg']['selectPostOption'] = __( 'Please select posts from at least one option', 'bulk-delete' ); |
|
50
|
|
|
|
|
51
|
|
|
$js_array['validators']['delete_posts_by_category'] = 'validateSelect2'; |
|
52
|
|
|
$js_array['error_msg']['delete_posts_by_category'] = 'selectCategory'; |
|
53
|
|
|
$js_array['msg']['selectCategory'] = __( 'Please select at least one category', 'bulk-delete' ); |
|
54
|
|
|
|
|
55
|
|
|
$js_array['validators']['delete_posts_by_tag'] = 'validateSelect2'; |
|
56
|
|
|
$js_array['error_msg']['delete_posts_by_category'] = 'selectTag'; |
|
57
|
|
|
$js_array['msg']['selectTag'] = __( 'Please select at least one tag', 'bulk-delete' ); |
|
58
|
|
|
|
|
59
|
|
|
$js_array['validators']['delete_posts_by_url'] = 'validateUrl'; |
|
60
|
|
|
$js_array['error_msg']['delete_posts_by_url'] = 'enterUrl'; |
|
61
|
|
|
$js_array['msg']['enterUrl'] = __( 'Please enter at least one post url', 'bulk-delete' ); |
|
62
|
|
|
|
|
63
|
|
|
$js_array['dt_iterators'][] = '_cats'; |
|
64
|
|
|
$js_array['dt_iterators'][] = '_tags'; |
|
65
|
|
|
$js_array['dt_iterators'][] = '_taxs'; |
|
66
|
|
|
$js_array['dt_iterators'][] = '_post_status'; |
|
67
|
|
|
|
|
68
|
|
|
return $js_array; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
47 |
|
protected function do_delete( $options ) { |
|
72
|
47 |
|
$query = $this->build_query( $options ); |
|
73
|
|
|
|
|
74
|
47 |
|
if ( empty( $query ) ) { |
|
75
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
|
76
|
|
|
return 0; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
47 |
|
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
|
56 |
|
protected function delete_posts_from_query( $query, $options ) { |
|
91
|
56 |
|
$query = bd_build_query_options( $options, $query ); |
|
92
|
56 |
|
$post_ids = bd_query( $query ); |
|
93
|
|
|
|
|
94
|
56 |
|
return $this->delete_posts_by_id( $post_ids, $options['force_delete'] ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Render the "private post" setting fields. |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function render_private_post_settings() { |
|
101
|
|
|
if( $this->are_private_posts_present() ){ |
|
102
|
|
|
bd_render_private_post_settings( $this->field_slug ); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Delete sticky posts. |
|
108
|
|
|
* |
|
109
|
|
|
* @param bool $force_delete Whether to bypass trash and force deletion. |
|
110
|
|
|
* |
|
111
|
|
|
* @return int Number of posts deleted. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function delete_sticky_posts( $force_delete ) { |
|
114
|
|
|
$sticky_post_ids = get_option( 'sticky_posts' ); |
|
115
|
|
|
|
|
116
|
|
|
if ( ! is_array( $sticky_post_ids ) ) { |
|
117
|
|
|
return 0; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->delete_posts_by_id( $sticky_post_ids, $force_delete ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Delete posts by ids. |
|
125
|
|
|
* |
|
126
|
|
|
* @param int[] $post_ids List of post ids to delete. |
|
127
|
|
|
* @param bool $force_delete True to force delete posts, False otherwise. |
|
128
|
|
|
* |
|
129
|
|
|
* @return int Number of posts deleted. |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function delete_posts_by_id( $post_ids, $force_delete ) { |
|
132
|
|
|
foreach ( $post_ids as $post_id ) { |
|
133
|
|
|
// `$force_delete` parameter to `wp_delete_post` won't work for custom post types. |
|
134
|
|
|
// See https://core.trac.wordpress.org/ticket/43672 |
|
135
|
|
|
if ( $force_delete ) { |
|
136
|
|
|
wp_delete_post( $post_id, true ); |
|
137
|
|
|
} else { |
|
138
|
|
|
wp_trash_post( $post_id ); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return count( $post_ids ); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|