Completed
Push — 247-fix/delete-term-meta ( d5f818...2a1fe3 )
by Sudar
67:09 queued 61:16
created

Controller   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 79
dl 0
loc 282
ccs 0
cts 90
cp 0
rs 10
c 0
b 0
f 0
wmc 29

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get_plugin_file() 0 4 1
B request_handler() 0 55 7
A log_sql_query() 0 17 2
A load_old_hooks() 0 17 1
A load() 0 18 3
A load_taxonomy_term() 0 24 4
A load_taxonomy_term_meta() 0 18 4
A verify_get_request_nonce() 0 6 2
A filter_plugin_action_links() 0 23 4
A increase_timeout() 0 3 1
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
		add_action( 'wp_ajax_bd_load_taxonomy_term_meta', array( $this, 'load_taxonomy_term_meta' ) );
31
32
		add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 );
33
		add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 );
34
35
		$this->load_old_hooks();
36
37
		if ( defined( 'BD_DEBUG' ) && BD_DEBUG ) {
0 ignored issues
show
Bug introduced by
The constant BulkWP\BulkDelete\Core\BD_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
			add_action( 'bd_after_query', array( $this, 'log_sql_query' ) );
39
		}
40
	}
41
42
	/**
43
	 * Handle both POST and GET requests.
44
	 * This method automatically triggers all the actions after checking the nonce.
45
	 */
46
	public function request_handler() {
47
		if ( isset( $_POST['bd_action'] ) ) {
48
			$bd_action   = sanitize_text_field( $_POST['bd_action'] );
49
			$nonce_valid = false;
50
51
			if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) {
52
				$nonce_valid = true;
53
			}
54
55
			/**
56
			 * Perform nonce check.
57
			 *
58
			 * @since 5.5
59
			 */
60
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
61
				return;
62
			}
63
64
			/**
65
			 * Before performing a bulk action.
66
			 * This hook is for doing actions just before performing any bulk operation.
67
			 *
68
			 * @since 5.4
69
			 */
70
			do_action( 'bd_pre_bulk_action', $bd_action );
71
72
			/**
73
			 * Perform the bulk operation.
74
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
75
			 *
76
			 * @since 5.4
77
			 */
78
			do_action( 'bd_' . $bd_action, $_POST );
79
		}
80
81
		if ( isset( $_GET['bd_action'] ) ) {
82
			$bd_action   = sanitize_text_field( $_GET['bd_action'] );
83
			$nonce_valid = false;
84
85
			/**
86
			 * Perform nonce check.
87
			 *
88
			 * @since 5.5.4
89
			 */
90
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
91
				return;
92
			}
93
94
			/**
95
			 * Perform the bulk operation.
96
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
97
			 *
98
			 * @since 5.5.4
99
			 */
100
			do_action( 'bd_' . $bd_action, $_GET );
101
		}
102
	}
103
104
	/**
105
	 * Increase PHP timeout.
106
	 *
107
	 * This is to prevent bulk operations from timing out
108
	 *
109
	 * @since 5.5.4
110
	 */
111
	public function increase_timeout() {
112
		// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
113
		@set_time_limit( 0 );
114
	}
115
116
	/**
117
	 * Verify if GET request has a valid nonce.
118
	 *
119
	 * @since  5.5.4
120
	 *
121
	 * @param bool   $result Whether nonce is valid.
122
	 * @param string $action Action name.
123
	 *
124
	 * @return bool True if nonce is valid, otherwise return $result.
125
	 */
126
	public function verify_get_request_nonce( $result, $action ) {
127
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
128
			return true;
129
		}
130
131
		return $result;
132
	}
133
134
	/**
135
	 * Ajax call back function for getting taxonomies to load select2 options.
136
	 *
137
	 * @since 6.0.0
138
	 */
139
	public function load_taxonomy_term() {
140
		$response = array();
141
142
		$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
143
144
		$terms = get_terms(
145
			array(
146
				'taxonomy'   => $taxonomy,
147
				'hide_empty' => false,
148
				'search'     => sanitize_text_field( $_GET['q'] ),
149
			)
150
		);
151
152
		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
153
			foreach ( $terms as $term ) {
154
				$response[] = array(
155
					absint( $term->term_id ),
156
					$term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')',
157
				);
158
			}
159
		}
160
161
		echo wp_json_encode( $response );
0 ignored issues
show
Bug introduced by
Are you sure wp_json_encode($response) of type false|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
		echo /** @scrutinizer ignore-type */ wp_json_encode( $response );
Loading history...
162
		die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
163
	}
164
165
	/**
166
	 * Ajax call back function for getting taxonomies meta to load select2 options.
167
	 *
168
	 * @since 6.0.0
169
	 */
170
	public function load_taxonomy_term_meta() {
171
		$response = array();
172
173
		$term_id = absint( $_GET['term_id'] );
174
175
		$term_vals = get_term_meta( $term_id );
176
177
		if ( ! empty( $term_vals ) && ! is_wp_error( $term_vals ) ) {
178
			foreach ( $term_vals as $key => $value ) {
179
				$response[] = array(
180
					$key,
181
					$value,
182
				);
183
			}
184
		}
185
186
		echo wp_json_encode( $response );
0 ignored issues
show
Bug introduced by
Are you sure wp_json_encode($response) of type false|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
		echo /** @scrutinizer ignore-type */ wp_json_encode( $response );
Loading history...
187
		die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
188
	}
189
190
	/**
191
	 * Adds the settings link in the Plugin page.
192
	 *
193
	 * Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/.
194
	 *
195
	 * @since 6.0.0 Moved into Controller class.
196
	 *
197
	 * @staticvar string $this_plugin
198
	 *
199
	 * @param array  $action_links Action Links.
200
	 * @param string $file         Plugin file name.
201
	 *
202
	 * @return array Modified links.
203
	 */
204
	public function filter_plugin_action_links( $action_links, $file ) {
205
		static $this_plugin;
206
207
		if ( ! $this_plugin ) {
208
			$this_plugin = plugin_basename( $this->get_plugin_file() );
209
		}
210
211
		if ( $file === $this_plugin ) {
212
			/**
213
			 * Filter plugin action links added by Bulk Move.
214
			 *
215
			 * @since 6.0.0
216
			 *
217
			 * @param array Plugin Links.
218
			 */
219
			$bm_action_links = apply_filters( 'bd_plugin_action_links', array() );
220
221
			if ( ! empty( $bm_action_links ) ) {
222
				$action_links = array_merge( $bm_action_links, $action_links );
223
			}
224
		}
225
226
		return $action_links;
227
	}
228
229
	/**
230
	 * Log SQL query used by Bulk Delete.
231
	 *
232
	 * Query is logged only when `BD_DEBUG` is set.
233
	 *
234
	 * @since 5.6
235
	 * @since 6.0.0 Moved into Controller class.
236
	 *
237
	 * @param \WP_Query|\WP_Term_Query|\WP_User_Query $wp_query Query object.
238
	 */
239
	public function log_sql_query( $wp_query ) {
240
		if ( ! property_exists( $wp_query, 'request' ) ) {
241
			return;
242
		}
243
244
		$query = $wp_query->request;
245
246
		/**
247
		 * Bulk Delete query is getting logged.
248
		 *
249
		 * @since 5.6
250
		 *
251
		 * @param string $query Bulk Delete SQL Query.
252
		 */
253
		do_action( 'bd_log_sql_query', $query );
254
255
		error_log( 'Bulk Delete Query: ' . $query );
256
	}
257
258
	/**
259
	 * Temporary fix to get plugin file.
260
	 *
261
	 * TODO: Remove this method from this class.
262
	 *
263
	 * @since 6.0.0
264
	 *
265
	 * @return string
266
	 */
267
	private function get_plugin_file() {
268
		$bd = BULK_DELETE();
269
270
		return $bd->get_plugin_file();
271
	}
272
273
	/**
274
	 * Load Old hooks.
275
	 *
276
	 * TODO: Refactor these hooks into seperate classes.
277
	 *
278
	 * @since 6.0.0
279
	 */
280
	protected function load_old_hooks() {
281
		// license related.
282
		add_action( 'bd_license_form', array( 'BD_License', 'display_activate_license_form' ), 100 );
283
		add_action( 'bd_deactivate_license', array( 'BD_License', 'deactivate_license' ) );
284
		add_action( 'bd_delete_license', array( 'BD_License', 'delete_license' ) );
285
		add_action( 'bd_validate_license', array( 'BD_License', 'validate_license' ), 10, 2 );
286
287
		// Settings related.
288
		add_action( 'bd_before_secondary_menus', array( 'BD_Settings_Page', 'add_menu' ) );
289
		add_action( 'bd_admin_footer_settings_page', 'bd_modify_admin_footer' );
290
		add_action( 'admin_init', array( 'BD_Settings', 'create_settings' ), 100 );
291
292
		// Help tab related.
293
		add_action( 'bd_add_contextual_help', array( 'Bulk_Delete_Help_Screen', 'add_contextual_help' ) );
294
295
		// Misc page related.
296
		add_action( 'bd_admin_footer_misc_page', 'bd_modify_admin_footer' );
297
	}
298
}
299