Completed
Push — 330-fix/delete-user-meta-add-a... ( d70968...8c3efe )
by Sudar
45:44 queued 42:41
created

Controller   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Test Coverage

Coverage 26.58%

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 259
ccs 21
cts 79
cp 0.2658
rs 10
c 0
b 0
f 0
wmc 26

9 Methods

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

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