Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Request_Handler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EE_Request_Handler, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');} |
||
9 | final class EE_Request_Handler { |
||
10 | |||
11 | /** |
||
12 | * @var array $_params $_REQUEST parameters |
||
13 | * @access private |
||
14 | */ |
||
15 | private $_params = array(); |
||
16 | |||
17 | /** |
||
18 | * @var array $_notice |
||
19 | * @access private |
||
20 | */ |
||
21 | private $_notice = array(); |
||
22 | |||
23 | /** |
||
24 | * rendered output to be returned to WP |
||
25 | * @var string |
||
26 | * @access private |
||
27 | */ |
||
28 | private $_output = ''; |
||
29 | |||
30 | /** |
||
31 | * whether current request is via AJAX |
||
32 | * @var boolean |
||
33 | * @access public |
||
34 | */ |
||
35 | public $ajax = false; |
||
36 | |||
37 | /** |
||
38 | * whether current request is via AJAX from the frontend of the site |
||
39 | * @var boolean |
||
40 | * @access public |
||
41 | */ |
||
42 | public $front_ajax = false; |
||
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * class constructor |
||
48 | * |
||
49 | * @access public |
||
50 | * @param WP $wp |
||
51 | * @return \EE_Request_Handler |
||
|
|||
52 | */ |
||
53 | public function __construct( $wp = null ) { |
||
54 | // grab request vars |
||
55 | // NOTE: WHEN MERGING TO 4.9 PLZ FAVOUR THE CHANGES IN 4.9 OVER THE FOLLOWING LINE |
||
56 | $this->_params = array_merge( $_GET, $_POST ); |
||
57 | // AJAX ??? |
||
58 | $this->ajax = defined( 'DOING_AJAX' ) && DOING_AJAX ? true : false; |
||
59 | $this->front_ajax = defined( 'EE_FRONT_AJAX' ) && EE_FRONT_AJAX ? true : false; |
||
60 | do_action( 'AHEE__EE_Request_Handler__construct__complete' ); |
||
61 | } |
||
62 | |||
63 | |||
64 | |||
65 | /** |
||
66 | * set_request_vars |
||
67 | * |
||
68 | * @access public |
||
69 | * @param WP $wp |
||
70 | * @return void |
||
71 | */ |
||
72 | public function parse_request( $wp = null ) { |
||
73 | //if somebody forgot to provide us with WP, that's ok because its global |
||
74 | if ( ! $wp instanceof WP ) { |
||
75 | global $wp; |
||
76 | } |
||
77 | $this->set_request_vars( $wp ); |
||
78 | } |
||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * set_request_vars |
||
84 | * |
||
85 | * @access public |
||
86 | * @param WP $wp |
||
87 | * @return void |
||
88 | */ |
||
89 | public function set_request_vars( $wp = null ) { |
||
90 | if ( ! is_admin() ) { |
||
91 | // set request post_id |
||
92 | $this->set( 'post_id', $this->get_post_id_from_request( $wp )); |
||
93 | // set request post name |
||
94 | $this->set( 'post_name', $this->get_post_name_from_request( $wp )); |
||
95 | // set request post_type |
||
96 | $this->set( 'post_type', $this->get_post_type_from_request( $wp )); |
||
97 | // true or false ? is this page being used by EE ? |
||
98 | $this->set_espresso_page(); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * get_post_id_from_request |
||
106 | * |
||
107 | * @access public |
||
108 | * @param WP $wp |
||
109 | * @return int |
||
110 | */ |
||
111 | public function get_post_id_from_request( $wp = null ) { |
||
112 | if ( ! $wp instanceof WP ){ |
||
113 | global $wp; |
||
114 | } |
||
115 | $post_id = null; |
||
116 | if ( isset( $wp->query_vars['p'] )) { |
||
117 | $post_id = $wp->query_vars['p']; |
||
118 | } |
||
119 | if ( ! $post_id && isset( $wp->query_vars['page_id'] )) { |
||
120 | $post_id = $wp->query_vars['page_id']; |
||
121 | } |
||
122 | if ( ! $post_id && isset( $wp->request ) && is_numeric( basename( $wp->request ))) { |
||
123 | $post_id = basename( $wp->request ); |
||
124 | } |
||
125 | return $post_id; |
||
126 | } |
||
127 | |||
128 | |||
129 | |||
130 | /** |
||
131 | * get_post_name_from_request |
||
132 | * |
||
133 | * @access public |
||
134 | * @param WP $wp |
||
135 | * @return string |
||
136 | */ |
||
137 | public function get_post_name_from_request( $wp = null ) { |
||
138 | if ( ! $wp instanceof WP ){ |
||
139 | global $wp; |
||
140 | } |
||
141 | $post_name = null; |
||
142 | View Code Duplication | if ( isset( $wp->query_vars['name'] ) && ! empty( $wp->query_vars['name'] )) { |
|
143 | $post_name = $wp->query_vars['name']; |
||
144 | } |
||
145 | View Code Duplication | if ( ! $post_name && isset( $wp->query_vars['pagename'] ) && ! empty( $wp->query_vars['pagename'] )) { |
|
146 | $post_name = $wp->query_vars['pagename']; |
||
147 | } |
||
148 | if ( ! $post_name && isset( $wp->request ) && ! empty( $wp->request )) { |
||
149 | $possible_post_name = basename( $wp->request ); |
||
150 | if ( ! is_numeric( $possible_post_name )) { |
||
151 | /** @type WPDB $wpdb */ |
||
152 | global $wpdb; |
||
153 | $SQL = "SELECT ID from $wpdb->posts WHERE post_status='publish' AND post_name=%s"; |
||
154 | $possible_post_name = $wpdb->get_var( $wpdb->prepare( $SQL, $possible_post_name )); |
||
155 | if ( $possible_post_name ) { |
||
156 | $post_name = $possible_post_name; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | if ( ! $post_name && $this->get( 'post_id' )) { |
||
161 | /** @type WPDB $wpdb */ |
||
162 | global $wpdb; |
||
163 | $SQL = "SELECT post_name from $wpdb->posts WHERE post_status='publish' AND ID=%d"; |
||
164 | $possible_post_name = $wpdb->get_var( $wpdb->prepare( $SQL, $this->get( 'post_id' ))); |
||
165 | if( $possible_post_name ) { |
||
166 | $post_name = $possible_post_name; |
||
167 | } |
||
168 | } |
||
169 | return $post_name; |
||
170 | } |
||
171 | |||
172 | |||
173 | |||
174 | /** |
||
175 | * get_post_type_from_request |
||
176 | * |
||
177 | * @access public |
||
178 | * @param WP $wp |
||
179 | * @return mixed |
||
180 | */ |
||
181 | public function get_post_type_from_request( $wp = null ) { |
||
182 | if ( ! $wp instanceof WP ){ |
||
183 | global $wp; |
||
184 | } |
||
185 | return isset( $wp->query_vars['post_type'] ) ? $wp->query_vars['post_type'] : null; |
||
186 | } |
||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * Just a helper method for getting the url for the displayed page. |
||
192 | * @param WP $wp |
||
193 | * @return bool|string|void |
||
194 | */ |
||
195 | public function get_current_page_permalink( $wp = null ) { |
||
196 | $post_id = $this->get_post_id_from_request( $wp ); |
||
197 | if ( $post_id ) { |
||
198 | $current_page_permalink = get_permalink( $post_id ); |
||
199 | } else { |
||
200 | if ( ! $wp instanceof WP ) { |
||
201 | global $wp; |
||
202 | } |
||
203 | if ( $wp->request ) { |
||
204 | $current_page_permalink = site_url( $wp->request ); |
||
205 | } else { |
||
206 | $current_page_permalink = esc_url( site_url( $_SERVER[ 'REQUEST_URI' ] ) ); |
||
207 | } |
||
208 | } |
||
209 | return $current_page_permalink; |
||
210 | } |
||
211 | |||
212 | |||
213 | |||
214 | /** |
||
215 | * test_for_espresso_page |
||
216 | * |
||
217 | * @access public |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function test_for_espresso_page() { |
||
221 | global $wp; |
||
222 | /** @type EE_CPT_Strategy $EE_CPT_Strategy */ |
||
223 | $EE_CPT_Strategy = EE_Registry::instance()->load_core( 'CPT_Strategy' ); |
||
224 | $espresso_CPT_taxonomies = $EE_CPT_Strategy->get_CPT_taxonomies(); |
||
225 | if ( is_array( $espresso_CPT_taxonomies ) ) { |
||
226 | foreach ( $espresso_CPT_taxonomies as $espresso_CPT_taxonomy =>$details ) { |
||
227 | if ( isset( $wp->query_vars, $wp->query_vars[ $espresso_CPT_taxonomy ] ) ) { |
||
228 | return true; |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | // load espresso CPT endpoints |
||
233 | $espresso_CPT_endpoints = $EE_CPT_Strategy->get_CPT_endpoints(); |
||
234 | $post_type_CPT_endpoints = array_flip( $espresso_CPT_endpoints ); |
||
235 | $post_types = (array)$this->get( 'post_type' ); |
||
236 | foreach ( $post_types as $post_type ) { |
||
237 | // was a post name passed ? |
||
238 | if ( isset( $post_type_CPT_endpoints[ $post_type ] ) ) { |
||
239 | // kk we know this is an espresso page, but is it a specific post ? |
||
240 | if ( ! $this->get( 'post_name' ) ) { |
||
241 | // there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events |
||
242 | $post_name = isset( $post_type_CPT_endpoints[ $this->get( 'post_type' ) ] ) ? $post_type_CPT_endpoints[ $this->get( 'post_type' ) ] : null; |
||
243 | // if the post type matches on of our then set the endpoint |
||
244 | if ( $post_name ) { |
||
245 | $this->set( 'post_name', $post_name ); |
||
246 | } |
||
247 | } |
||
248 | return true; |
||
249 | } |
||
250 | } |
||
251 | if ( $this->get( 'post_name' )) { |
||
252 | // load all pages using espresso shortcodes |
||
253 | $post_shortcodes = isset( EE_Registry::instance()->CFG->core->post_shortcodes ) ? EE_Registry::instance()->CFG->core->post_shortcodes : array(); |
||
254 | // make sure core pages are included |
||
255 | $espresso_pages = array_merge( $espresso_CPT_endpoints, $post_shortcodes ); |
||
256 | // was a post name passed ? |
||
257 | if ( isset( $espresso_pages[ $this->get( 'post_name' ) ] )) { |
||
258 | return true; |
||
259 | } |
||
260 | } |
||
261 | return false; |
||
262 | } |
||
263 | |||
264 | |||
265 | |||
266 | /** |
||
267 | * is_espresso_page |
||
268 | * |
||
269 | * @access public |
||
270 | * @param null|bool $value |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public function set_espresso_page( $value = null ) { |
||
274 | $value = $value ? $value : $this->test_for_espresso_page(); |
||
275 | $this->_params['is_espresso_page'] = $value; |
||
276 | } |
||
277 | |||
278 | |||
279 | |||
280 | /** |
||
281 | * is_espresso_page |
||
282 | * |
||
283 | * @access public |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function is_espresso_page() { |
||
287 | return isset( $this->_params['is_espresso_page'] ) ? $this->_params['is_espresso_page'] : false; |
||
288 | } |
||
289 | |||
290 | |||
291 | |||
292 | /** |
||
293 | * returns contents of $_REQUEST |
||
294 | * @return array |
||
295 | */ |
||
296 | public function params() { |
||
297 | return $this->_params; |
||
298 | } |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * setter |
||
304 | * |
||
305 | * @access public |
||
306 | * @param $key |
||
307 | * @param $value |
||
308 | * @param bool $override_ee |
||
309 | * @return void |
||
310 | */ |
||
311 | public function set( $key, $value, $override_ee = false ) { |
||
312 | // don't allow "ee" to be overwritten unless explicitly instructed to do so |
||
313 | if ( |
||
314 | $key !== 'ee' || |
||
315 | ( $key === 'ee' && empty( $this->_params['ee'] )) |
||
316 | || ( $key === 'ee' && ! empty( $this->_params['ee'] ) && $override_ee ) |
||
317 | ) { |
||
318 | $this->_params[ $key ] = $value; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | |||
323 | |||
324 | /** |
||
325 | * getter |
||
326 | * |
||
327 | * @access public |
||
328 | * @param $key |
||
329 | * @param null $default |
||
330 | * @return mixed |
||
331 | */ |
||
332 | public function get( $key, $default = null ) { |
||
335 | |||
336 | |||
337 | |||
338 | /** |
||
339 | * check if param exists |
||
340 | * |
||
341 | * @access public |
||
342 | * @param $key |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public function is_set( $key ) { |
||
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * remove param |
||
353 | * |
||
354 | * @access public |
||
355 | * @param $key |
||
356 | * @return void |
||
357 | */ |
||
358 | public function un_set( $key ) { |
||
361 | |||
362 | |||
363 | |||
364 | /** |
||
365 | * set_notice |
||
366 | * |
||
367 | * @access public |
||
368 | * @param $key |
||
369 | * @param $value |
||
370 | * @return void |
||
371 | */ |
||
372 | public function set_notice( $key, $value ) { |
||
375 | |||
376 | |||
377 | |||
378 | /** |
||
379 | * get_notice |
||
380 | * |
||
381 | * @access public |
||
382 | * @param $key |
||
383 | * @return mixed |
||
384 | */ |
||
385 | public function get_notice( $key ) { |
||
388 | |||
389 | |||
390 | |||
391 | /** |
||
392 | * add_output |
||
393 | * |
||
394 | * @access public |
||
395 | * @param $string |
||
396 | * @return void |
||
397 | */ |
||
398 | public function add_output( $string ) { |
||
401 | |||
402 | |||
403 | |||
404 | /** |
||
405 | * get_output |
||
406 | * |
||
407 | * @access public |
||
408 | * @return string |
||
409 | */ |
||
410 | public function get_output() { |
||
413 | |||
414 | |||
415 | |||
416 | /** |
||
417 | * @param $item |
||
418 | * @param $key |
||
419 | */ |
||
420 | public function sanitize_text_field_for_array_walk( &$item, &$key ) { |
||
421 | $item = strpos( $item, 'email' ) !== false ? sanitize_email( $item ) : sanitize_text_field( $item ); |
||
422 | } |
||
423 | |||
424 | |||
425 | |||
426 | /** |
||
427 | * @param $a |
||
428 | * @param $b |
||
429 | * @return bool |
||
430 | */ |
||
431 | public function __set($a,$b) { return false; } |
||
432 | |||
433 | |||
434 | |||
435 | /** |
||
436 | * @param $a |
||
437 | * @return bool |
||
438 | */ |
||
439 | public function __get($a) { return false; } |
||
440 | |||
441 | |||
442 | |||
443 | /** |
||
444 | * @param $a |
||
445 | * @return bool |
||
446 | */ |
||
447 | public function __isset($a) { return false; } |
||
448 | |||
449 | |||
450 | |||
451 | /** |
||
452 | * @param $a |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function __unset($a) { return false; } |
||
456 | |||
457 | |||
458 | |||
459 | /** |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function __clone() {} |
||
463 | |||
464 | |||
465 | |||
466 | /** |
||
467 | * @return bool |
||
468 | */ |
||
469 | public function __wakeup() {} |
||
470 | |||
471 | |||
472 | |||
473 | /** |
||
474 | * |
||
475 | */ |
||
476 | public function __destruct() {} |
||
477 | |||
478 | |||
479 | } |
||
480 | // End of file EE_Request_Handler.core.php |
||
481 | // Location: /core/EE_Request_Handler.core.php |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.