Completed
Branch BUG-10626-dst-unit-test (cc62a6)
by
unknown
37:15 queued 23:58
created

EE_Request_Handler::set()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 6
nc 2
nop 3
dl 10
loc 10
rs 8.2222
c 0
b 0
f 0
1
<?php use EventEspresso\core\interfaces\InterminableInterface;
2
3
if ( ! defined( 'EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');}
4
/**
5
 * class EE_Request_Handler
6
 *
7
 * @package     Event Espresso
8
 * @subpackage  /core/
9
 * @author      Brent Christensen
10
 */
11
final class EE_Request_Handler implements InterminableInterface {
12
13
	/**
14
	 * 	@var 	array	$_params 	$_REQUEST parameters
15
	 *  @access 	private
16
	 */
17
	private $_params = array();
18
19
	/**
20
	 * 	@var 	array 	$_notice
21
	 *  @access 	private
22
	 */
23
	private $_notice = array();
24
25
	/**
26
	 * 	rendered output to be returned to WP
27
	 *	@var 	string
28
	 * 	@access 	private
29
	 */
30
	private $_output = '';
31
32
	/**
33
	 * 	whether current request is via AJAX
34
	 *	@var 	boolean
35
	 * 	@access public
36
	 */
37
	public $ajax = false;
38
39
	/**
40
	 * 	whether current request is via AJAX from the frontend of the site
41
	 *	@var 	boolean
42
	 * 	@access public
43
	 */
44
	public $front_ajax = false;
45
46
47
48
	/**
49
	 *    class constructor
50
	 *
51
	 * @access public
52
	 * @param  EE_Request $request
53
	 */
54
	public function __construct( EE_Request $request ) {
55
		// grab request vars
56
		$this->_params = $request->params();
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 string
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' ) ] )
243
                        ? $post_type_CPT_endpoints[ $this->get( 'post_type' ) ]
244
                        : '';
245
					// if the post type matches on of our then set the endpoint
246
					if ( $post_name ) {
247
						$this->set( 'post_name', $post_name );
248
					}
249
				}
250
				return true;
251
			}
252
		}
253
		return false;
254
	}
255
256
257
258
	/**
259
	 *  is_espresso_page
260
	 *
261
	 * @access    public
262
	 * @param null|bool $value
263
	 * @return    void
264
	 */
265
	public function set_espresso_page( $value = null ) {
266
        $this->_params['is_espresso_page'] = ! empty($value) ? $value : $this->test_for_espresso_page();
267
	}
268
269
270
271
	/**
272
	 * 	is_espresso_page
273
	 *
274
	 *  @access 	public
275
	 *  @return 	mixed
276
	 */
277
	public function is_espresso_page() {
278
		return isset( $this->_params['is_espresso_page'] ) ? $this->_params['is_espresso_page'] : false;
279
	}
280
281
282
283
	/**
284
	 * returns contents of $_REQUEST
285
	 * @return array
286
	 */
287
	public function params() {
288
		return $this->_params;
289
	}
290
291
292
293
	/**
294
	 *    setter
295
	 *
296
	 * @access    public
297
	 * @param      $key
298
	 * @param      $value
299
	 * @param bool $override_ee
300
	 * @return    void
301
	 */
302 View Code Duplication
	public function set( $key, $value, $override_ee = false ) {
303
		// don't allow "ee" to be overwritten unless explicitly instructed to do so
304
		if (
305
			$key !== 'ee' ||
306
			( $key === 'ee' && empty( $this->_params['ee'] ))
307
			|| ( $key === 'ee' && ! empty( $this->_params['ee'] ) && $override_ee )
308
		) {
309
			$this->_params[ $key ] = $value;
310
		}
311
	}
312
313
314
315
	/**
316
	 *    getter
317
	 *
318
	 * @access    public
319
	 * @param      $key
320
	 * @param null $default
321
	 * @return    mixed
322
	 */
323
	public function get( $key, $default = null ) {
324
		return isset( $this->_params[ $key ] ) ? $this->_params[ $key ] : $default;
325
	}
326
327
328
329
	/**
330
	 *    check if param exists
331
	 *
332
	 * @access    public
333
	 * @param $key
334
	 * @return    boolean
335
	 */
336
	public function is_set( $key ) {
337
		return isset( $this->_params[ $key ] ) ? true : false;
338
	}
339
340
341
342
	/**
343
	 *    remove param
344
	 *
345
	 * @access    public
346
	 * @param $key
347
	 * @return    void
348
	 */
349
	public function un_set( $key ) {
350
		unset( $this->_params[ $key ] );
351
	}
352
353
354
355
	/**
356
	 *    set_notice
357
	 *
358
	 * @access    public
359
	 * @param $key
360
	 * @param $value
361
	 * @return    void
362
	 */
363
	public function set_notice( $key, $value ) {
364
		$this->_notice[ $key ] = $value;
365
	}
366
367
368
369
	/**
370
	 *    get_notice
371
	 *
372
	 * @access    public
373
	 * @param $key
374
	 * @return    mixed
375
	 */
376
	public function get_notice( $key ) {
377
		return isset( $this->_notice[ $key ] ) ? $this->_notice[ $key ] : null;
378
	}
379
380
381
382
	/**
383
	 *    add_output
384
	 *
385
	 * @access    public
386
	 * @param $string
387
	 * @return    void
388
	 */
389
	public function add_output( $string ) {
390
		$this->_output .= $string;
391
	}
392
393
394
395
	/**
396
	 * 	get_output
397
	 *
398
	 *  @access 	public
399
	 *  @return 	string
400
	 */
401
	public function get_output() {
402
		return $this->_output;
403
	}
404
405
406
407
	/**
408
	 * @param $item
409
	 * @param $key
410
	 */
411
	public function sanitize_text_field_for_array_walk( &$item, &$key ) {
412
		$item = strpos( $item, 'email' ) !== false ? sanitize_email( $item ) : sanitize_text_field( $item );
413
	}
414
415
416
417
	/**
418
	 * @param $a
419
	 * @param $b
420
	 * @return bool
421
	 */
422
	public function __set($a,$b) { return false; }
423
424
425
426
	/**
427
	 * @param $a
428
	 * @return bool
429
	 */
430
	public function __get($a) { return false; }
431
432
433
434
	/**
435
	 * @param $a
436
	 * @return bool
437
	 */
438
	public function __isset($a) { return false; }
439
440
441
442
	/**
443
	 * @param $a
444
	 * @return bool
445
	 */
446
	public function __unset($a) { return false; }
447
448
449
450
	/**
451
	 * @return void
452
	 */
453
	public function __clone() {}
454
455
456
457
	/**
458
	 * @return void
459
	 */
460
	public function __wakeup() {}
461
462
463
464
	/**
465
	 *
466
	 */
467
	public function __destruct() {}
468
469
470
}
471
// End of file EE_Request_Handler.core.php
472
// Location: /core/EE_Request_Handler.core.php
473