Passed
Push — 247-fix/delete-term-meta ( 0c7c80...e69629 )
by Rajan
07:33
created

Controller::load_taxonomy_term_meta()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 0
loc 18
ccs 0
cts 11
cp 0
crap 20
rs 9.6666
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
	/**
170
	 * Ajax call back function for getting taxonomies meta to load select2 options.
171
	 *
172
	 * @since 6.0.1
173
	 */
174
	public function load_taxonomy_term_meta() {
175
		$response = array();
176
177
		$term_id = sanitize_text_field( $_GET['term_id'] );
178
179
		$term_vals = get_term_meta($term_id);
0 ignored issues
show
Bug introduced by
$term_id of type string is incompatible with the type integer expected by parameter $term_id of get_term_meta(). ( Ignorable by Annotation )

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

179
		$term_vals = get_term_meta(/** @scrutinizer ignore-type */ $term_id);
Loading history...
180
181
		if ( ! empty( $term_vals ) && ! is_wp_error( $term_vals ) ) {
182
			foreach ( $term_vals as $key => $value ) {
183
				$response[] = array(
184
					$key,
185
					$value,
186
				);
187
			}
188
		}
189
190
		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

190
		echo /** @scrutinizer ignore-type */ wp_json_encode( $response );
Loading history...
191
		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...
192
	}
193
194
	/**
195
	 * Adds the settings link in the Plugin page.
196
	 *
197
	 * Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/.
198
	 *
199
	 * @since 6.0.0 Moved into Controller class.
200
	 *
201
	 * @staticvar string $this_plugin
202
	 *
203
	 * @param array  $action_links Action Links.
204
	 * @param string $file         Plugin file name.
205
	 *
206
	 * @return array Modified links.
207
	 */
208
	public function filter_plugin_action_links( $action_links, $file ) {
209
		static $this_plugin;
210
211
		if ( ! $this_plugin ) {
212
			$this_plugin = plugin_basename( $this->get_plugin_file() );
213
		}
214
215
		if ( $file === $this_plugin ) {
216
			/**
217
			 * Filter plugin action links added by Bulk Move.
218
			 *
219
			 * @since 6.0.0
220
			 *
221
			 * @param array Plugin Links.
222
			 */
223
			$bm_action_links = apply_filters( 'bd_plugin_action_links', array() );
224
225
			if ( ! empty( $bm_action_links ) ) {
226
				$action_links = array_merge( $bm_action_links, $action_links );
227
			}
228
		}
229
230
		return $action_links;
231
	}
232
233
	/**
234
	 * Log SQL query used by Bulk Delete.
235
	 *
236
	 * Query is logged only when `BD_DEBUG` is set.
237
	 *
238
	 * @since 5.6
239
	 * @since 6.0.0 Moved into Controller class.
240
	 *
241
	 * @param \WP_Query $wp_query WP Query object.
242
	 */
243
	public function log_sql_query( $wp_query ) {
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 1
	protected function load_old_hooks() {
281
		// license related.
282 1
		add_action( 'bd_license_form', array( 'BD_License', 'display_activate_license_form' ), 100 );
283 1
		add_action( 'bd_deactivate_license', array( 'BD_License', 'deactivate_license' ) );
284 1
		add_action( 'bd_delete_license', array( 'BD_License', 'delete_license' ) );
285 1
		add_action( 'bd_validate_license', array( 'BD_License', 'validate_license' ), 10, 2 );
286
287
		// Settings related.
288 1
		add_action( 'bd_before_secondary_menus', array( 'BD_Settings_Page', 'add_menu' ) );
289 1
		add_action( 'bd_admin_footer_settings_page', 'bd_modify_admin_footer' );
290 1
		add_action( 'admin_init', array( 'BD_Settings', 'create_settings' ), 100 );
291
292
		// Help tab related.
293 1
		add_action( 'bd_add_contextual_help', array( 'Bulk_Delete_Help_Screen', 'add_contextual_help' ) );
294
295
		// Misc page related.
296 1
		add_action( 'bd_admin_footer_misc_page', 'bd_modify_admin_footer' );
297 1
	}
298
}
299