Completed
Branch FET-10486-add-timestamp-checki... (611b15)
by
unknown
105:07 queued 90:18
created

EE_Request_Handler::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');}
2
/**
3
 * class EE_Request_Handler
4
 *
5
 * @package     Event Espresso
6
 * @subpackage  /core/
7
 * @author      Brent Christensen
8
 */
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  EE_Request $request
51
	 */
52
	public function __construct( EE_Request $request ) {
53
		// grab request vars
54
		$this->_params = $request->params();
55
		// AJAX ???
56
		$this->ajax = defined( 'DOING_AJAX' ) && DOING_AJAX ? true : false;
57
		$this->front_ajax = defined( 'EE_FRONT_AJAX' ) && EE_FRONT_AJAX ? true : false;
58
		do_action( 'AHEE__EE_Request_Handler__construct__complete' );
59
	}
60
61
62
63
	/**
64
	 *    set_request_vars
65
	 *
66
	 * @access public
67
	 * @param WP $wp
68
	 * @return void
69
	 */
70
	public function parse_request( $wp = null ) {
71
		//if somebody forgot to provide us with WP, that's ok because its global
72
		if ( ! $wp instanceof WP ) {
73
			global $wp;
74
		}
75
		$this->set_request_vars( $wp );
76
	}
77
78
79
80
	/**
81
	 *    set_request_vars
82
	 *
83
	 * @access public
84
	 * @param WP $wp
85
	 * @return void
86
	 */
87
	public function set_request_vars( $wp = null ) {
88
		if ( ! is_admin() ) {
89
			// set request post_id
90
			$this->set( 'post_id', $this->get_post_id_from_request( $wp ));
91
			// set request post name
92
			$this->set( 'post_name', $this->get_post_name_from_request( $wp ));
93
			// set request post_type
94
			$this->set( 'post_type', $this->get_post_type_from_request( $wp ));
95
			// true or false ? is this page being used by EE ?
96
			$this->set_espresso_page();
97
		}
98
	}
99
100
101
102
	/**
103
	 *    get_post_id_from_request
104
	 *
105
	 * @access public
106
	 * @param WP $wp
107
	 * @return int
108
	 */
109
	public function get_post_id_from_request( $wp = null ) {
110
		if ( ! $wp instanceof WP ){
111
			global $wp;
112
		}
113
		$post_id = null;
114
		if ( isset( $wp->query_vars['p'] )) {
115
			$post_id = $wp->query_vars['p'];
116
		}
117
		if ( ! $post_id && isset( $wp->query_vars['page_id'] )) {
118
			$post_id = $wp->query_vars['page_id'];
119
		}
120
		if ( ! $post_id && isset( $wp->request ) && is_numeric( basename( $wp->request ))) {
121
			$post_id = basename( $wp->request );
122
		}
123
		return $post_id;
124
	}
125
126
127
128
	/**
129
	 *    get_post_name_from_request
130
	 *
131
	 * @access public
132
	 * @param WP $wp
133
	 * @return string
134
	 */
135
	public function get_post_name_from_request( $wp = null ) {
136
		if ( ! $wp instanceof WP ){
137
			global $wp;
138
		}
139
		$post_name = null;
140 View Code Duplication
		if ( isset( $wp->query_vars['name'] ) && ! empty( $wp->query_vars['name'] )) {
141
			$post_name = $wp->query_vars['name'];
142
		}
143 View Code Duplication
		if ( ! $post_name && isset( $wp->query_vars['pagename'] ) && ! empty( $wp->query_vars['pagename'] )) {
144
			$post_name = $wp->query_vars['pagename'];
145
		}
146
		if ( ! $post_name && isset( $wp->request ) && ! empty( $wp->request )) {
147
			$possible_post_name = basename( $wp->request );
148
			if ( ! is_numeric( $possible_post_name )) {
149
				/** @type WPDB $wpdb */
150
				global $wpdb;
151
				$SQL = "SELECT ID from {$wpdb->posts} WHERE post_status='publish' AND post_name=%s";
152
				$possible_post_name = $wpdb->get_var( $wpdb->prepare( $SQL, $possible_post_name ));
153
				if ( $possible_post_name ) {
154
					$post_name = $possible_post_name;
155
				}
156
			}
157
		}
158
		if ( ! $post_name && $this->get( 'post_id' )) {
159
			/** @type WPDB $wpdb */
160
			global $wpdb;
161
			$SQL = "SELECT post_name from {$wpdb->posts} WHERE post_status='publish' AND ID=%d";
162
			$possible_post_name = $wpdb->get_var( $wpdb->prepare( $SQL, $this->get( 'post_id' )));
163
			if( $possible_post_name ) {
164
				$post_name = $possible_post_name;
165
			}
166
		}
167
		return $post_name;
168
	}
169
170
171
172
	/**
173
	 *    get_post_type_from_request
174
	 *
175
	 * @access public
176
	 * @param WP $wp
177
	 * @return mixed
178
	 */
179
	public function get_post_type_from_request( $wp = null ) {
180
		if ( ! $wp instanceof WP ){
181
			global $wp;
182
		}
183
		return isset( $wp->query_vars['post_type'] ) ? $wp->query_vars['post_type'] : null;
184
	}
185
186
187
188
	/**
189
	 * Just a helper method for getting the url for the displayed page.
190
	 * @param  WP $wp
191
	 * @return string
192
	 */
193
	public function get_current_page_permalink( $wp = null ) {
194
		$post_id = $this->get_post_id_from_request( $wp );
195
		if ( $post_id ) {
196
			$current_page_permalink = get_permalink( $post_id );
197
		} else {
198
			if ( ! $wp instanceof WP ) {
199
				global $wp;
200
			}
201
			if ( $wp->request ) {
202
				$current_page_permalink = site_url( $wp->request );
203
			} else {
204
				$current_page_permalink = esc_url( site_url( $_SERVER[ 'REQUEST_URI' ] ) );
205
			}
206
		}
207
		return $current_page_permalink;
208
	}
209
210
211
212
	/**
213
	 * 		test_for_espresso_page
214
	 *
215
	 * 		@access public
216
	 * 		@return bool
217
	 */
218
	public function test_for_espresso_page() {
219
		global $wp;
220
		/** @type EE_CPT_Strategy $EE_CPT_Strategy */
221
		$EE_CPT_Strategy = EE_Registry::instance()->load_core( 'CPT_Strategy' );
222
		$espresso_CPT_taxonomies = $EE_CPT_Strategy->get_CPT_taxonomies();
223
		if ( is_array( $espresso_CPT_taxonomies ) ) {
224
			foreach ( $espresso_CPT_taxonomies as $espresso_CPT_taxonomy =>$details ) {
225
				if ( isset( $wp->query_vars, $wp->query_vars[ $espresso_CPT_taxonomy ] ) ) {
226
					return true;
227
				}
228
			}
229
		}
230
		// load espresso CPT endpoints
231
		$espresso_CPT_endpoints = $EE_CPT_Strategy->get_CPT_endpoints();
232
		$post_type_CPT_endpoints = array_flip( $espresso_CPT_endpoints );
233
		$post_types = (array)$this->get( 'post_type' );
234
		foreach ( $post_types as $post_type ) {
235
			// was a post name passed ?
236
			if ( isset( $post_type_CPT_endpoints[ $post_type ] ) ) {
237
				// kk we know this is an espresso page, but is it a specific post ?
238
				if ( ! $this->get( 'post_name' ) ) {
239
					// there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events
240
					$post_name = isset( $post_type_CPT_endpoints[ $this->get( 'post_type' ) ] )
241
                        ? $post_type_CPT_endpoints[ $this->get( 'post_type' ) ]
242
                        : '';
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
		return false;
252
	}
253
254
255
256
	/**
257
	 *  is_espresso_page
258
	 *
259
	 * @access    public
260
	 * @param null|bool $value
261
	 * @return    void
262
	 */
263
	public function set_espresso_page( $value = null ) {
264
        $this->_params['is_espresso_page'] = ! empty($value) ? $value : $this->test_for_espresso_page();
265
	}
266
267
268
269
	/**
270
	 * 	is_espresso_page
271
	 *
272
	 *  @access 	public
273
	 *  @return 	mixed
274
	 */
275
	public function is_espresso_page() {
276
		return isset( $this->_params['is_espresso_page'] ) ? $this->_params['is_espresso_page'] : false;
277
	}
278
279
280
281
	/**
282
	 * returns contents of $_REQUEST
283
	 * @return array
284
	 */
285
	public function params() {
286
		return $this->_params;
287
	}
288
289
290
291
	/**
292
	 *    setter
293
	 *
294
	 * @access    public
295
	 * @param      $key
296
	 * @param      $value
297
	 * @param bool $override_ee
298
	 * @return    void
299
	 */
300 View Code Duplication
	public function set( $key, $value, $override_ee = false ) {
301
		// don't allow "ee" to be overwritten unless explicitly instructed to do so
302
		if (
303
			$key !== 'ee' ||
304
			( $key === 'ee' && empty( $this->_params['ee'] ))
305
			|| ( $key === 'ee' && ! empty( $this->_params['ee'] ) && $override_ee )
306
		) {
307
			$this->_params[ $key ] = $value;
308
		}
309
	}
310
311
312
313
	/**
314
	 *    getter
315
	 *
316
	 * @access    public
317
	 * @param      $key
318
	 * @param null $default
319
	 * @return    mixed
320
	 */
321
	public function get( $key, $default = null ) {
322
		return isset( $this->_params[ $key ] ) ? $this->_params[ $key ] : $default;
323
	}
324
325
326
327
	/**
328
	 *    check if param exists
329
	 *
330
	 * @access    public
331
	 * @param $key
332
	 * @return    boolean
333
	 */
334
	public function is_set( $key ) {
335
		return isset( $this->_params[ $key ] ) ? true : false;
336
	}
337
338
339
340
	/**
341
	 *    remove param
342
	 *
343
	 * @access    public
344
	 * @param $key
345
	 * @return    void
346
	 */
347
	public function un_set( $key ) {
348
		unset( $this->_params[ $key ] );
349
	}
350
351
352
353
	/**
354
	 *    set_notice
355
	 *
356
	 * @access    public
357
	 * @param $key
358
	 * @param $value
359
	 * @return    void
360
	 */
361
	public function set_notice( $key, $value ) {
362
		$this->_notice[ $key ] = $value;
363
	}
364
365
366
367
	/**
368
	 *    get_notice
369
	 *
370
	 * @access    public
371
	 * @param $key
372
	 * @return    mixed
373
	 */
374
	public function get_notice( $key ) {
375
		return isset( $this->_notice[ $key ] ) ? $this->_notice[ $key ] : null;
376
	}
377
378
379
380
	/**
381
	 *    add_output
382
	 *
383
	 * @access    public
384
	 * @param $string
385
	 * @return    void
386
	 */
387
	public function add_output( $string ) {
388
		$this->_output .= $string;
389
	}
390
391
392
393
	/**
394
	 * 	get_output
395
	 *
396
	 *  @access 	public
397
	 *  @return 	string
398
	 */
399
	public function get_output() {
400
		return $this->_output;
401
	}
402
403
404
405
	/**
406
	 * @param $item
407
	 * @param $key
408
	 */
409
	public function sanitize_text_field_for_array_walk( &$item, &$key ) {
410
		$item = strpos( $item, 'email' ) !== false ? sanitize_email( $item ) : sanitize_text_field( $item );
411
	}
412
413
414
415
	/**
416
	 * @param $a
417
	 * @param $b
418
	 * @return bool
419
	 */
420
	public function __set($a,$b) { return false; }
421
422
423
424
	/**
425
	 * @param $a
426
	 * @return bool
427
	 */
428
	public function __get($a) { return false; }
429
430
431
432
	/**
433
	 * @param $a
434
	 * @return bool
435
	 */
436
	public function __isset($a) { return false; }
437
438
439
440
	/**
441
	 * @param $a
442
	 * @return bool
443
	 */
444
	public function __unset($a) { return false; }
445
446
447
448
	/**
449
	 * @return void
450
	 */
451
	public function __clone() {}
452
453
454
455
	/**
456
	 * @return void
457
	 */
458
	public function __wakeup() {}
459
460
461
462
	/**
463
	 *
464
	 */
465
	public function __destruct() {}
466
467
468
}
469
// End of file EE_Request_Handler.core.php
470
// Location: /core/EE_Request_Handler.core.php
471