Completed
Push — 50-feature/delete-blogs-in-mul... ( ad0625...05ec87 )
by
unknown
42:17 queued 38:54
created

Controller::load_term_metas()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 32
rs 9.7333
1
<?php
2
3
namespace BulkWP\BulkDelete\Core;
4
5
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_terms', array( $this, 'load_taxonomy_terms' ) );
30
		add_action( 'wp_ajax_bd_load_term_metas', array( $this, 'load_term_metas' ) );
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 to load taxonomy terms by taxonomy.
136
	 *
137
	 * @since 6.0.0
138
	 */
139
	public function load_taxonomy_terms() {
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 to load term meta by term.
167
	 *
168
	 * @since 6.1.0
169
	 */
170
	public function load_term_metas() {
171
		$response = [
172
			[
173
				'id'   => 0,
174
				'text' => __( 'No term meta found', 'bulk-delete' ),
175
			],
176
		];
177
178
		$term_id = absint( $_GET['term_id'] );
179
180
		$term_metas = get_term_meta( $term_id );
181
182
		if ( is_array( $term_metas ) && ! empty( $term_metas ) ) {
183
			$keys = array_keys( $term_metas );
184
185
			$response = [
186
				[
187
					'id'   => 0,
188
					'text' => __( 'Select Term Meta Key', 'bulk-delete' ),
189
				],
190
			];
191
192
			foreach ( $keys as $key ) {
193
				$response[] = [
194
					'id'   => $key,
195
					'text' => $key,
196
				];
197
			}
198
		}
199
200
		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

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