1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core; |
4
|
|
|
|
5
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Bulk Delete Controller. |
9
|
|
|
* |
10
|
|
|
* Handle all requests and automatically perform nonce checks. |
11
|
|
|
* |
12
|
|
|
* @since 5.5.4 |
13
|
|
|
* @since 6.0.0 Added namespace. |
14
|
|
|
*/ |
15
|
|
|
class Controller { |
16
|
|
|
/** |
17
|
|
|
* Load the controller and setup hooks and actions. |
18
|
|
|
* |
19
|
|
|
* @since 6.0.0 |
20
|
|
|
*/ |
21
|
|
|
public function load() { |
22
|
|
|
add_action( 'admin_init', array( $this, 'request_handler' ) ); |
23
|
|
|
|
24
|
|
|
add_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 ); |
25
|
|
|
add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 ); |
26
|
|
|
|
27
|
|
|
add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 ); |
28
|
|
|
|
29
|
|
|
add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) ); |
30
|
|
|
|
31
|
|
|
add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 ); |
32
|
|
|
add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 ); |
33
|
|
|
|
34
|
|
|
$this->load_old_hooks(); |
35
|
|
|
|
36
|
|
|
if ( defined( 'BD_DEBUG' ) && BD_DEBUG ) { |
|
|
|
|
37
|
|
|
add_action( 'bd_after_query', array( $this, 'log_sql_query' ) ); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handle both POST and GET requests. |
43
|
|
|
* This method automatically triggers all the actions after checking the nonce. |
44
|
|
|
*/ |
45
|
|
|
public function request_handler() { |
46
|
|
|
if ( isset( $_POST['bd_action'] ) ) { |
47
|
|
|
$bd_action = sanitize_text_field( $_POST['bd_action'] ); |
48
|
|
|
$nonce_valid = false; |
49
|
|
|
|
50
|
|
|
if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) { |
51
|
|
|
$nonce_valid = true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Perform nonce check. |
56
|
|
|
* |
57
|
|
|
* @since 5.5 |
58
|
|
|
*/ |
59
|
|
|
if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Before performing a bulk action. |
65
|
|
|
* This hook is for doing actions just before performing any bulk operation. |
66
|
|
|
* |
67
|
|
|
* @since 5.4 |
68
|
|
|
*/ |
69
|
|
|
do_action( 'bd_pre_bulk_action', $bd_action ); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Perform the bulk operation. |
73
|
|
|
* This hook is for doing the bulk operation. Nonce check has already happened by this point. |
74
|
|
|
* |
75
|
|
|
* @since 5.4 |
76
|
|
|
*/ |
77
|
|
|
do_action( 'bd_' . $bd_action, $_POST ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ( isset( $_GET['bd_action'] ) ) { |
81
|
|
|
$bd_action = sanitize_text_field( $_GET['bd_action'] ); |
82
|
|
|
$nonce_valid = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Perform nonce check. |
86
|
|
|
* |
87
|
|
|
* @since 5.5.4 |
88
|
|
|
*/ |
89
|
|
|
if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Perform the bulk operation. |
95
|
|
|
* This hook is for doing the bulk operation. Nonce check has already happened by this point. |
96
|
|
|
* |
97
|
|
|
* @since 5.5.4 |
98
|
|
|
*/ |
99
|
|
|
do_action( 'bd_' . $bd_action, $_GET ); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Increase PHP timeout. |
105
|
|
|
* |
106
|
|
|
* This is to prevent bulk operations from timing out |
107
|
|
|
* |
108
|
|
|
* @since 5.5.4 |
109
|
|
|
*/ |
110
|
|
|
public function increase_timeout() { |
111
|
|
|
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
112
|
|
|
@set_time_limit( 0 ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Verify if GET request has a valid nonce. |
117
|
|
|
* |
118
|
|
|
* @since 5.5.4 |
119
|
|
|
* |
120
|
|
|
* @param bool $result Whether nonce is valid. |
121
|
|
|
* @param string $action Action name. |
122
|
|
|
* |
123
|
|
|
* @return bool True if nonce is valid, otherwise return $result. |
124
|
|
|
*/ |
125
|
|
|
public function verify_get_request_nonce( $result, $action ) { |
126
|
|
|
if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Ajax call back function for getting taxonomies to load select2 options. |
135
|
|
|
* |
136
|
|
|
* @since 6.0.0 |
137
|
|
|
*/ |
138
|
|
|
public function load_taxonomy_term() { |
139
|
|
|
$response = array(); |
140
|
|
|
|
141
|
|
|
$taxonomy = sanitize_text_field( $_GET['taxonomy'] ); |
142
|
|
|
|
143
|
|
|
$terms = get_terms( |
144
|
|
|
array( |
145
|
|
|
'taxonomy' => $taxonomy, |
146
|
|
|
'hide_empty' => false, |
147
|
|
|
'search' => sanitize_text_field( $_GET['q'] ), |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { |
152
|
|
|
foreach ( $terms as $term ) { |
153
|
|
|
$response[] = array( |
154
|
|
|
absint( $term->term_id ), |
155
|
|
|
$term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')', |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
echo wp_json_encode( $response ); |
|
|
|
|
161
|
|
|
die; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Adds the settings link in the Plugin page. |
166
|
|
|
* |
167
|
|
|
* Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/. |
168
|
|
|
* |
169
|
|
|
* @since 6.0.0 Moved into Controller class. |
170
|
|
|
* |
171
|
|
|
* @staticvar string $this_plugin |
172
|
|
|
* |
173
|
|
|
* @param array $action_links Action Links. |
174
|
|
|
* @param string $file Plugin file name. |
175
|
|
|
* |
176
|
|
|
* @return array Modified links. |
177
|
|
|
*/ |
178
|
|
|
public function filter_plugin_action_links( $action_links, $file ) { |
179
|
|
|
static $this_plugin; |
180
|
|
|
|
181
|
|
|
if ( ! $this_plugin ) { |
182
|
|
|
$this_plugin = plugin_basename( $this->get_plugin_file() ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ( $file === $this_plugin ) { |
186
|
|
|
/** |
187
|
|
|
* Filter plugin action links added by Bulk Move. |
188
|
|
|
* |
189
|
|
|
* @since 6.0.0 |
190
|
|
|
* |
191
|
|
|
* @param array Plugin Links. |
192
|
|
|
*/ |
193
|
|
|
$bm_action_links = apply_filters( 'bd_plugin_action_links', array() ); |
194
|
|
|
|
195
|
|
|
if ( ! empty( $bm_action_links ) ) { |
196
|
|
|
$action_links = array_merge( $bm_action_links, $action_links ); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $action_links; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Log SQL query used by Bulk Delete. |
205
|
|
|
* |
206
|
|
|
* Query is logged only when `BD_DEBUG` is set. |
207
|
|
|
* |
208
|
|
|
* @since 5.6 |
209
|
|
|
* @since 6.0.0 Moved into Controller class. |
210
|
|
|
* |
211
|
|
|
* @param \WP_Query|\WP_Term_Query|\WP_User_Query $wp_query Query object. |
212
|
|
|
*/ |
213
|
|
|
public function log_sql_query( $wp_query ) { |
214
|
|
|
if ( ! property_exists( $wp_query, 'request' ) ) { |
215
|
|
|
return; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$query = $wp_query->request; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Bulk Delete query is getting logged. |
222
|
|
|
* |
223
|
|
|
* @since 5.6 |
224
|
|
|
* |
225
|
|
|
* @param string $query Bulk Delete SQL Query. |
226
|
|
|
*/ |
227
|
|
|
do_action( 'bd_log_sql_query', $query ); |
228
|
|
|
|
229
|
|
|
error_log( 'Bulk Delete Query: ' . $query ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Temporary fix to get plugin file. |
234
|
|
|
* |
235
|
|
|
* TODO: Remove this method from this class. |
236
|
|
|
* |
237
|
|
|
* @since 6.0.0 |
238
|
|
|
* |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
private function get_plugin_file() { |
242
|
|
|
$bd = BULK_DELETE(); |
243
|
|
|
|
244
|
|
|
return $bd->get_plugin_file(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Load Old hooks. |
249
|
|
|
* |
250
|
|
|
* TODO: Refactor these hooks into seperate classes. |
251
|
|
|
* |
252
|
|
|
* @since 6.0.0 |
253
|
|
|
*/ |
254
|
|
|
protected function load_old_hooks() { |
255
|
|
|
// license related. |
256
|
|
|
add_action( 'bd_license_form', array( 'BD_License', 'display_activate_license_form' ), 100 ); |
257
|
|
|
add_action( 'bd_deactivate_license', array( 'BD_License', 'deactivate_license' ) ); |
258
|
|
|
add_action( 'bd_delete_license', array( 'BD_License', 'delete_license' ) ); |
259
|
|
|
add_action( 'bd_validate_license', array( 'BD_License', 'validate_license' ), 10, 2 ); |
260
|
|
|
|
261
|
|
|
// Settings related. |
262
|
|
|
add_action( 'bd_before_secondary_menus', array( 'BD_Settings_Page', 'add_menu' ) ); |
263
|
|
|
add_action( 'bd_admin_footer_settings_page', 'bd_modify_admin_footer' ); |
264
|
|
|
add_action( 'admin_init', array( 'BD_Settings', 'create_settings' ), 100 ); |
265
|
|
|
|
266
|
|
|
// Help tab related. |
267
|
|
|
add_action( 'bd_add_contextual_help', array( 'Bulk_Delete_Help_Screen', 'add_contextual_help' ) ); |
268
|
|
|
|
269
|
|
|
// Misc page related. |
270
|
|
|
add_action( 'bd_admin_footer_misc_page', 'bd_modify_admin_footer' ); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|