1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Posts\Modules; |
4
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Posts\PostsModule; |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Delete Posts by Sticky Post. |
11
|
|
|
* |
12
|
|
|
* @since 6.0.0 |
13
|
|
|
*/ |
14
|
|
|
class DeletePostsByStickyPostModule extends PostsModule { |
15
|
|
|
/** |
16
|
|
|
* Did the user requested for unsticking posts instead of deleting them? |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $did_unsticky_post_instead_of_delete = false; |
21
|
|
|
|
22
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
23
|
|
|
protected function initialize() { |
24
|
|
|
$this->item_type = 'posts'; |
25
|
|
|
$this->field_slug = 'sticky_post'; |
26
|
|
|
$this->meta_box_slug = 'delete_posts_by_sticky_post'; |
27
|
|
|
$this->action = 'delete_posts_by_sticky_post'; |
28
|
|
|
$this->messages = array( |
29
|
|
|
'box_label' => __( 'By Sticky Post', 'bulk-delete' ), |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
34
|
|
|
public function render() { |
35
|
|
|
if ( ! $this->are_sticky_posts_present() ) : ?> |
36
|
|
|
<h4> |
37
|
|
|
<?php _e( 'There are no sticky post present in this WordPress installation.', 'bulk-delete' ); ?> |
38
|
|
|
</h4> |
39
|
|
|
<?php return; ?> |
40
|
|
|
<?php endif; // phpcs:ignore?> |
|
|
|
|
41
|
|
|
|
42
|
|
|
<h4><?php _e( 'Select the sticky post that you want to delete', 'bulk-delete' ); ?></h4> |
43
|
|
|
|
44
|
|
|
<fieldset class="options"> |
45
|
|
|
<table class="optiontable"> |
46
|
|
|
<tr> |
47
|
|
|
<td scope="row" colspan="2"> |
48
|
|
|
<?php $this->render_sticky_posts_dropdown(); ?> |
49
|
|
|
</td> |
50
|
|
|
</tr> |
51
|
|
|
</table> |
52
|
|
|
|
53
|
|
|
<table class="optiontable"> |
54
|
|
|
<?php |
55
|
|
|
$this->render_filtering_table_header(); |
56
|
|
|
$this->render_sticky_action_settings(); |
57
|
|
|
$this->render_delete_settings(); |
58
|
|
|
?> |
59
|
|
|
</table> |
60
|
|
|
</fieldset> |
61
|
|
|
|
62
|
|
|
<?php |
63
|
|
|
$this->render_submit_button(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function filter_js_array( $js_array ) { |
67
|
|
|
$js_array['msg']['unstickyPostsWarning'] = __( 'Are you sure you want to remove the selected posts from being sticky?', 'bulk-delete' ); |
68
|
|
|
$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the selected posts?', 'bulk-delete' ); |
69
|
|
|
$js_array['msg']['selectStickyPost'] = __( 'Select at least one sticky post', 'bulk-delete' ); |
70
|
|
|
|
71
|
|
|
$js_array['validators'][ $this->action ] = 'validateStickyPost'; |
72
|
|
|
$js_array['error_msg'][ $this->action ] = 'selectStickyPost'; |
73
|
|
|
|
74
|
|
|
$js_array['pre_action_msg'][ $this->action ] = 'DeletePostsByStickyPostPreAction'; |
75
|
|
|
|
76
|
|
|
return $js_array; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
80
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
81
|
|
|
$options['selected_posts'] = bd_array_get( $request, 'smbd_' . $this->field_slug ); |
82
|
|
|
$options['sticky_action'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_sticky_action' ); |
83
|
|
|
|
84
|
|
|
return $options; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Override the `do_delete` function to handle unsticking posts. |
89
|
|
|
* |
90
|
|
|
* @inheritdoc |
91
|
|
|
* |
92
|
|
|
* @param array $options Array of Delete options. |
93
|
|
|
* |
94
|
|
|
* @return int Number of posts deleted or unsticked. |
95
|
|
|
*/ |
96
|
|
|
protected function do_delete( $options ) { |
97
|
|
|
if ( 'unsticky' === $options['sticky_action'] ) { |
98
|
|
|
$posts_unsticked = 0; |
99
|
|
|
|
100
|
|
|
if ( in_array( 'all', $options['selected_posts'], true ) ) { |
101
|
|
|
$options['selected_posts'] = get_option( 'sticky_posts' ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ( $options['selected_posts'] as $post_id ) { |
105
|
|
|
unstick_post( $post_id ); |
106
|
|
|
$posts_unsticked ++; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->did_unsticky_post_instead_of_delete = true; |
110
|
|
|
|
111
|
|
|
return $posts_unsticked; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( 'delete' === $options['sticky_action'] ) { |
115
|
|
|
$query = $this->build_query( $options ); |
116
|
|
|
|
117
|
|
|
if ( empty( $query ) ) { |
118
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
119
|
|
|
return 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->delete_posts_from_query( $query, $options ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
129
|
|
|
protected function build_query( $options ) { |
130
|
|
|
$query = array(); |
131
|
|
|
|
132
|
|
|
if ( in_array( 'all', $options['selected_posts'], true ) ) { |
133
|
|
|
$query['post__in'] = get_option( 'sticky_posts' ); |
134
|
|
|
} else { |
135
|
|
|
$query['post__in'] = $options['selected_posts']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $query; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing |
142
|
|
|
protected function get_success_message( $items_deleted ) { |
143
|
|
|
if ( $this->did_unsticky_post_instead_of_delete ) { |
144
|
|
|
/* translators: 1 Number of posts unsticked */ |
145
|
|
|
return _n( '%d sticky post was made into normal post', '%d sticky posts were made into normal posts', $items_deleted, 'bulk-delete' ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* translators: 1 Number of posts deleted */ |
149
|
|
|
return _n( 'Deleted %d sticky post', 'Deleted %d sticky posts', $items_deleted, 'bulk-delete' ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.