Passed
Push — dev/6.0.0 ( f03c64...a274a9 )
by Sudar
03:13
created

Controller::verify_get_request_nonce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
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
	/**
18
	 * Load the controller and setup hooks and actions.
19 1
	 *
20 1
	 * @since 6.0.0
21 1
	 */
22 1
	public function load() {
23
		add_action( 'admin_init', array( $this, 'request_handler' ) );
24 1
25 1
		add_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 );
26
		add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 );
27
28
		add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 );
29
30
		add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) );
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
		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...
36
			add_action( 'bd_after_query', array( $this, 'log_sql_query' ) );
37
		}
38
	}
39
40
	/**
41
	 * Handle both POST and GET requests.
42
	 * This method automatically triggers all the actions after checking the nonce.
43
	 */
44
	public function request_handler() {
45
		if ( isset( $_POST['bd_action'] ) ) {
46
			$bd_action   = sanitize_text_field( $_POST['bd_action'] );
47
			$nonce_valid = false;
48
49
			if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) {
50
				$nonce_valid = true;
51
			}
52
53
			/**
54
			 * Perform nonce check.
55
			 *
56
			 * @since 5.5
57
			 */
58
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
59
				return;
60
			}
61
62
			/**
63
			 * Before performing a bulk action.
64
			 * This hook is for doing actions just before performing any bulk operation.
65
			 *
66
			 * @since 5.4
67
			 */
68
			do_action( 'bd_pre_bulk_action', $bd_action );
69
70
			/**
71
			 * Perform the bulk operation.
72
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
73
			 *
74
			 * @since 5.4
75
			 */
76
			do_action( 'bd_' . $bd_action, $_POST );
77
		}
78
79
		if ( isset( $_GET['bd_action'] ) ) {
80
			$bd_action   = sanitize_text_field( $_GET['bd_action'] );
81
			$nonce_valid = false;
82
83
			/**
84
			 * Perform nonce check.
85
			 *
86
			 * @since 5.5.4
87
			 */
88
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
89
				return;
90
			}
91
92
			/**
93
			 * Perform the bulk operation.
94
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
95
			 *
96
			 * @since 5.5.4
97
			 */
98
			do_action( 'bd_' . $bd_action, $_GET );
99
		}
100
	}
101
102
	/**
103
	 * Increase PHP timeout.
104
	 *
105
	 * This is to prevent bulk operations from timing out
106
	 *
107
	 * @since 5.5.4
108
	 */
109
	public function increase_timeout() {
110
		// phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved
111
		if ( ! ini_get( 'safe_mode' ) ) {
112
			// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
113
			@set_time_limit( 0 );
114
		}
115
	}
116
117
	/**
118
	 * Verify if GET request has a valid nonce.
119
	 *
120
	 * @since  5.5.4
121
	 *
122
	 * @param bool   $result Whether nonce is valid.
123
	 * @param string $action Action name.
124
	 *
125
	 * @return bool True if nonce is valid, otherwise return $result.
126
	 */
127
	public function verify_get_request_nonce( $result, $action ) {
128
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
129
			return true;
130
		}
131
132
		return $result;
133
	}
134
135
	/**
136
	 * Ajax call back function for getting taxonomies to load select2 options.
137
	 *
138
	 * @since 6.0.0
139
	 */
140
	public function load_taxonomy_term() {
141
		$response = array();
142
143
		$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
144
145
		$terms = get_terms(
146
			array(
147
				'taxonomy'   => $taxonomy,
148
				'hide_empty' => false,
149
				'search'     => sanitize_text_field( $_GET['q'] ),
150
			)
151
		);
152
153
		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
154
			foreach ( $terms as $term ) {
155
				$response[] = array(
156
					absint( $term->term_id ),
157
					$term->name . ' (' . $term->count . __( ' Posts', 'bulk-delete' ) . ')',
158
				);
159
			}
160
		}
161
162
		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

162
		echo /** @scrutinizer ignore-type */ wp_json_encode( $response );
Loading history...
163
		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...
164
	}
165
166
	/**
167
	 * Adds the settings link in the Plugin page.
168
	 *
169
	 * Based on http://striderweb.com/nerdaphernalia/2008/06/wp-use-action-links/.
170
	 *
171
	 * @since 6.0.0 Moved into Controller class.
172
	 *
173
	 * @staticvar string $this_plugin
174
	 *
175
	 * @param array  $action_links Action Links.
176
	 * @param string $file         Plugin file name.
177
	 *
178
	 * @return array Modified links.
179
	 */
180
	public function filter_plugin_action_links( $action_links, $file ) {
181
		static $this_plugin;
182
183
		if ( ! $this_plugin ) {
184
			$this_plugin = plugin_basename( $this->get_plugin_file() );
0 ignored issues
show
Bug introduced by
The method get_plugin_file() does not exist on BulkWP\BulkDelete\Core\Controller. ( Ignorable by Annotation )

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

184
			$this_plugin = plugin_basename( $this->/** @scrutinizer ignore-call */ get_plugin_file() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
		}
186
187
		if ( $file === $this_plugin ) {
188
			/**
189
			 * Filter plugin action links added by Bulk Move.
190
			 *
191
			 * @since 6.0.0
192
			 *
193
			 * @param array Plugin Links.
194
			 */
195
			$bm_action_links = apply_filters( 'bd_plugin_action_links', array() );
196
197
			if ( ! empty( $bm_action_links ) ) {
198
				$action_links = array_merge( $bm_action_links, $action_links );
199
			}
200
		}
201
202
		return $action_links;
203
	}
204
205
	/**
206
	 * Log SQL query used by Bulk Delete.
207
	 *
208
	 * Query is logged only when `BD_DEBUG` is set.
209
	 *
210
	 * @since 5.6
211
	 * @since 6.0.0 Moved into Controller class.
212
	 *
213
	 * @param \WP_Query $wp_query WP Query object.
214
	 */
215
	public function log_sql_query( $wp_query ) {
216
		$query = $wp_query->request;
217
218
		/**
219
		 * Bulk Delete query is getting logged.
220
		 *
221
		 * @since 5.6
222
		 *
223
		 * @param string $query Bulk Delete SQL Query.
224
		 */
225
		do_action( 'bd_log_sql_query', $query );
226
227
		error_log( 'Bulk Delete Query: ' . $query );
228
	}
229
}
230