Completed
Pull Request — dev/6.0.0 (#445)
by Rajan
24:05 queued 20:54
created

Controller::load_taxonomy_term_meta()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 0
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
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 1
	public function load() {
22 1
		add_action( 'admin_init', array( $this, 'request_handler' ) );
23
24 1
		add_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 );
25 1
		add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 );
26
27 1
		add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 );
28
29 1
		add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) );
30 1
		add_action( 'wp_ajax_bd_load_taxonomy_term_meta', array( $this, 'load_taxonomy_term_meta' ) );
31
32 1
		add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 );
33 1
		add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 );
34
35 1
		$this->load_old_hooks();
36
37 1
		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 1
	}
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 PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved
113
		if ( ! ini_get( 'safe_mode' ) ) {
114
			// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
115
			@set_time_limit( 0 );
116
		}
117
	}
118
119
	/**
120
	 * Verify if GET request has a valid nonce.
121
	 *
122
	 * @since  5.5.4
123
	 *
124
	 * @param bool   $result Whether nonce is valid.
125
	 * @param string $action Action name.
126
	 *
127
	 * @return bool True if nonce is valid, otherwise return $result.
128
	 */
129
	public function verify_get_request_nonce( $result, $action ) {
130
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
131
			return true;
132
		}
133
134
		return $result;
135
	}
136
137
	/**
138
	 * Ajax call back function for getting taxonomies to load select2 options.
139
	 *
140
	 * @since 6.0.0
141
	 */
142
	public function load_taxonomy_term() {
143
		$response = array();
144
145
		$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
146
147
		$terms = get_terms(
148
			array(
149
				'taxonomy'   => $taxonomy,
150
				'hide_empty' => false,
151
				'search'     => sanitize_text_field( $_GET['q'] ),
152
			)
153
		);
154
155
		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
156
			foreach ( $terms as $term ) {
157
				$response[] = array(
158
					absint( $term->term_id ),
159
					$term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')',
160
				);
161
			}
162
		}
163
164
		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

164
		echo /** @scrutinizer ignore-type */ wp_json_encode( $response );
Loading history...
165
		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...
166
	}
167
168
	/**
169
	 * Ajax call back function for getting taxonomies meta to load select2 options.
170
	 *
171
	 * @since 6.0.0
172
	 */
173
	public function load_taxonomy_term_meta() {
174
		$response = array();
175
176
		$term_id = absint( $_GET['term_id'] );
177
178
		$term_vals = get_term_meta( $term_id );
179
180
		if ( ! empty( $term_vals ) && ! is_wp_error( $term_vals ) ) {
181
			foreach ( $term_vals as $key => $value ) {
182
				$response[] = array(
183
					$key,
184
					$value,
185
				);
186
			}
187
		}
188
189
		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

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