Completed
Branch FET-10857-model-field-factory (adac96)
by
unknown
140:18 queued 129:14
created

EE_Request_Handler::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
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
     * @var EE_Request $request
18
     */
19
    private $request;
20
21
    /**
22
     * @var array $_notice
23
     */
24
    private $_notice = array();
25
26
    /**
27
     * rendered output to be returned to WP
28
     *
29
     * @var string $_output
30
     */
31
    private $_output = '';
32
33
    /**
34
     * whether current request is via AJAX
35
     *
36
     * @var boolean $ajax
37
     */
38
    public $ajax = false;
39
40
    /**
41
     * whether current request is via AJAX from the frontend of the site
42
     *
43
     * @var boolean $front_ajax
44
     */
45
    public $front_ajax = false;
46
47
48
49
    /**
50
     * @param  EE_Request $request
51
     */
52
    public function __construct(EE_Request $request)
53
    {
54
        $this->request = $request;
55
        $this->ajax = $this->request->ajax;
56
        $this->front_ajax = $this->request->front_ajax;
57
        do_action('AHEE__EE_Request_Handler__construct__complete');
58
    }
59
60
61
62
    /**
63
     * @param WP $wp
64
     * @return void
65
     * @throws EE_Error
66
     * @throws ReflectionException
67
     */
68
    public function parse_request($wp = null)
69
    {
70
        //if somebody forgot to provide us with WP, that's ok because its global
71
        if (! $wp instanceof WP) {
72
            global $wp;
73
        }
74
        $this->set_request_vars($wp);
75
    }
76
77
78
79
    /**
80
     * @param WP $wp
81
     * @return void
82
     * @throws EE_Error
83
     * @throws ReflectionException
84
     */
85
    public function set_request_vars($wp = null)
86
    {
87
        if (! is_admin()) {
88
            // set request post_id
89
            $this->request->set('post_id', $this->get_post_id_from_request($wp));
90
            // set request post name
91
            $this->request->set('post_name', $this->get_post_name_from_request($wp));
92
            // set request post_type
93
            $this->request->set('post_type', $this->get_post_type_from_request($wp));
94
            // true or false ? is this page being used by EE ?
95
            $this->set_espresso_page();
96
        }
97
    }
98
99
100
101
    /**
102
     * @param WP $wp
103
     * @return int
104
     */
105
    public function get_post_id_from_request($wp = null)
106
    {
107
        if (! $wp instanceof WP) {
108
            global $wp;
109
        }
110
        $post_id = null;
111
        if (isset($wp->query_vars['p'])) {
112
            $post_id = $wp->query_vars['p'];
113
        }
114
        if (! $post_id && isset($wp->query_vars['page_id'])) {
115
            $post_id = $wp->query_vars['page_id'];
116
        }
117
        if (! $post_id && $wp->request !== null && is_numeric(basename($wp->request))) {
118
            $post_id = basename($wp->request);
119
        }
120
        return $post_id;
121
    }
122
123
124
125
    /**
126
     * @param WP $wp
127
     * @return string
128
     */
129
    public function get_post_name_from_request($wp = null)
130
    {
131
        if (! $wp instanceof WP) {
132
            global $wp;
133
        }
134
        $post_name = null;
135 View Code Duplication
        if (isset($wp->query_vars['name']) && ! empty($wp->query_vars['name'])) {
136
            $post_name = $wp->query_vars['name'];
137
        }
138 View Code Duplication
        if (! $post_name && isset($wp->query_vars['pagename']) && ! empty($wp->query_vars['pagename'])) {
139
            $post_name = $wp->query_vars['pagename'];
140
        }
141
        if (! $post_name && $wp->request !== null && ! empty($wp->request)) {
142
            $possible_post_name = basename($wp->request);
143
            if (! is_numeric($possible_post_name)) {
144
                /** @type WPDB $wpdb */
145
                global $wpdb;
146
                $SQL =
147
                    "SELECT ID from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s";
148
                $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $possible_post_name));
149
                if ($possible_post_name) {
150
                    $post_name = $possible_post_name;
151
                }
152
            }
153
        }
154
        if (! $post_name && $this->get('post_id')) {
155
            /** @type WPDB $wpdb */
156
            global $wpdb;
157
            $SQL =
158
                "SELECT post_name from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d";
159
            $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $this->get('post_id')));
160
            if ($possible_post_name) {
161
                $post_name = $possible_post_name;
162
            }
163
        }
164
        return $post_name;
165
    }
166
167
168
169
    /**
170
     * @param WP $wp
171
     * @return mixed
172
     */
173
    public function get_post_type_from_request($wp = null)
174
    {
175
        if (! $wp instanceof WP) {
176
            global $wp;
177
        }
178
        return isset($wp->query_vars['post_type'])
179
            ? $wp->query_vars['post_type']
180
            : null;
181
    }
182
183
184
185
    /**
186
     * Just a helper method for getting the url for the displayed page.
187
     *
188
     * @param  WP $wp
189
     * @return string
190
     */
191
    public function get_current_page_permalink($wp = null)
192
    {
193
        $post_id = $this->get_post_id_from_request($wp);
194
        if ($post_id) {
195
            $current_page_permalink = get_permalink($post_id);
196
        } else {
197
            if (! $wp instanceof WP) {
198
                global $wp;
199
            }
200
            if ($wp->request) {
201
                $current_page_permalink = site_url($wp->request);
202
            } else {
203
                $current_page_permalink = esc_url(site_url($_SERVER['REQUEST_URI']));
204
            }
205
        }
206
        return $current_page_permalink;
207
    }
208
209
210
211
    /**
212
     * @return bool
213
     * @throws EE_Error
214
     * @throws ReflectionException
215
     */
216
    public function test_for_espresso_page()
217
    {
218
        global $wp;
219
        /** @type EE_CPT_Strategy $EE_CPT_Strategy */
220
        $EE_CPT_Strategy = EE_Registry::instance()->load_core('CPT_Strategy');
221
        $espresso_CPT_taxonomies = $EE_CPT_Strategy->get_CPT_taxonomies();
222
        if (is_array($espresso_CPT_taxonomies)) {
223
            foreach ($espresso_CPT_taxonomies as $espresso_CPT_taxonomy => $details) {
224
                if (isset($wp->query_vars, $wp->query_vars[$espresso_CPT_taxonomy])) {
225
                    return true;
226
                }
227
            }
228
        }
229
        // load espresso CPT endpoints
230
        $espresso_CPT_endpoints = $EE_CPT_Strategy->get_CPT_endpoints();
231
        $post_type_CPT_endpoints = array_flip($espresso_CPT_endpoints);
232
        $post_types = (array)$this->get('post_type');
233
        foreach ($post_types as $post_type) {
234
            // was a post name passed ?
235
            if (isset($post_type_CPT_endpoints[$post_type])) {
236
                // kk we know this is an espresso page, but is it a specific post ?
237
                if (! $this->get('post_name')) {
238
                    // there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events
239
                    $post_name = isset($post_type_CPT_endpoints[$this->get('post_type')])
240
                        ? $post_type_CPT_endpoints[$this->get('post_type')]
241
                        : '';
242
                    // if the post type matches on of our then set the endpoint
243
                    if ($post_name) {
244
                        $this->set('post_name', $post_name);
245
                    }
246
                }
247
                return true;
248
            }
249
        }
250
        return false;
251
    }
252
    /**
253
     * @param $key
254
     * @param $value
255
     * @return    void
256
     */
257
    public function set_notice($key, $value)
258
    {
259
        $this->_notice[$key] = $value;
260
    }
261
262
263
264
    /**
265
     * @param $key
266
     * @return    mixed
267
     */
268
    public function get_notice($key)
269
    {
270
        return isset($this->_notice[$key])
271
            ? $this->_notice[$key]
272
            : null;
273
    }
274
275
276
277
    /**
278
     * @param $string
279
     * @return void
280
     */
281
    public function add_output($string)
282
    {
283
        $this->_output .= $string;
284
    }
285
286
287
288
    /**
289
     * @return string
290
     */
291
    public function get_output()
292
    {
293
        return $this->_output;
294
    }
295
296
297
298
    /**
299
     * @param $item
300
     * @param $key
301
     */
302
    public function sanitize_text_field_for_array_walk(&$item, &$key)
303
    {
304
        $item = strpos($item, 'email') !== false
305
            ? sanitize_email($item)
306
            : sanitize_text_field($item);
307
    }
308
309
310
311
    /**
312
     * @param null|bool $value
313
     * @return void
314
     * @throws EE_Error
315
     * @throws ReflectionException
316
     */
317
    public function set_espresso_page($value = null)
318
    {
319
        $this->request->set(
320
            'is_espresso_page',
321
            ! empty($value)
322
                ? $value
323
                : $this->test_for_espresso_page()
324
        );
325
    }
326
327
328
329
    /**
330
     * @return    mixed
331
     */
332
    public function is_espresso_page()
333
    {
334
        return $this->request->is_set('is_espresso_page');
335
    }
336
337
338
339
    /**
340
     * returns contents of $_REQUEST
341
     *
342
     * @return array
343
     */
344
    public function params()
345
    {
346
        return $this->request->params();
347
    }
348
349
350
351
    /**
352
     * @param      $key
353
     * @param      $value
354
     * @param bool $override_ee
355
     * @return    void
356
     */
357
    public function set($key, $value, $override_ee = false)
358
    {
359
        $this->request->set($key, $value, $override_ee);
360
    }
361
362
363
364
    /**
365
     * @param      $key
366
     * @param null $default
367
     * @return    mixed
368
     */
369
    public function get($key, $default = null)
370
    {
371
        return $this->request->get($key, $default);
372
    }
373
374
375
376
    /**
377
     * check if param exists
378
     *
379
     * @param $key
380
     * @return    boolean
381
     */
382
    public function is_set($key)
383
    {
384
        return $this->request->is_set($key);
385
    }
386
387
388
389
    /**
390
     * remove param
391
     *
392
     * @param $key
393
     * @return    void
394
     */
395
    public function un_set($key)
396
    {
397
        $this->request->un_set($key);
398
    }
399
400
401
402
}
403
// End of file EE_Request_Handler.core.php
404
// Location: /core/EE_Request_Handler.core.php
405