Completed
Branch FET-10857-model-field-factory (f704a4)
by
unknown
69:41 queued 57:57
created

EE_Request_Handler::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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