Completed
Branch BUG/detect-wp-json-requests (f06a75)
by
unknown
380:35 queued 360:31
created
core/domain/services/contexts/RequestTypeContextDetector.php 3 patches
Doc Comments   +2 added lines patch added patch discarded remove patch
@@ -53,6 +53,8 @@
 block discarded – undo
53 53
 
54 54
 
55 55
     /**
56
+     * @param string $constant
57
+     * @param boolean $default
56 58
      * @return mixed
57 59
      */
58 60
     private function getConstant($constant, $default)
Please login to merge, or discard this patch.
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -19,172 +19,172 @@
 block discarded – undo
19 19
 class RequestTypeContextDetector
20 20
 {
21 21
 
22
-    /**
23
-     * @var RequestTypeContextFactoryInterface $factory
24
-     */
25
-    private $factory;
26
-
27
-    /**
28
-     * @var RequestInterface $request
29
-     */
30
-    private $request;
31
-
32
-    /**
33
-     * @var array $constants
34
-     */
35
-    private $constants;
36
-
37
-
38
-    /**
39
-     * RequestTypeContextDetector constructor.
40
-     *
41
-     * @param RequestInterface                   $request
42
-     * @param RequestTypeContextFactoryInterface $factory
43
-     */
44
-    public function __construct(
45
-        RequestInterface $request,
46
-        RequestTypeContextFactoryInterface $factory,
47
-        array $constants = array()
48
-    ) {
49
-        $this->request = $request;
50
-        $this->factory = $factory;
51
-        $this->constants = $constants;
52
-    }
53
-
54
-
55
-    /**
56
-     * @return mixed
57
-     */
58
-    private function getConstant($constant, $default)
59
-    {
60
-        return isset($this->constants[ $constant ]) ? $this->constants[ $constant ] : $default;
61
-    }
62
-
63
-
64
-    /**
65
-     * @return RequestTypeContext
66
-     * @throws InvalidArgumentException
67
-     */
68
-    public function detectRequestTypeContext()
69
-    {
70
-        // Detect error scrapes
71
-        if ($this->request->getRequestParam('wp_scrape_key') !== null
72
-            && $this->request->getRequestParam('wp_scrape_nonce') !== null
73
-        ) {
74
-            return $this->factory->create(RequestTypeContext::WP_SCRAPE);
75
-        }
76
-        // Detect EE REST API
77
-        if ($this->isEspressoRestApiRequest()) {
78
-            return $this->factory->create(RequestTypeContext::API);
79
-        }
80
-        // Detect WP REST API
81
-        if ($this->isWorPressRestApiRequest()) {
82
-            return $this->factory->create(RequestTypeContext::WP_API);
83
-        }
84
-        // Detect AJAX
85
-        if ($this->getConstant('DOING_AJAX', false)) {
86
-            if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) {
87
-                return $this->factory->create(RequestTypeContext::AJAX_FRONT);
88
-            }
89
-            if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) {
90
-                return $this->factory->create(RequestTypeContext::AJAX_ADMIN);
91
-            }
92
-            return $this->factory->create(RequestTypeContext::AJAX_OTHER);
93
-        }
94
-        // Detect WP_Cron
95
-        if ($this->isCronRequest()) {
96
-            return $this->factory->create(RequestTypeContext::CRON);
97
-        }
98
-        // Detect command line requests
99
-        if ($this->getConstant('WP_CLI', false)) {
100
-            return $this->factory->create(RequestTypeContext::CLI);
101
-        }
102
-        // detect WordPress admin (ie: "Dashboard")
103
-        if ($this->getConstant('is_admin', false)) {
104
-            return $this->factory->create(RequestTypeContext::ADMIN);
105
-        }
106
-        // Detect iFrames
107
-        if ($this->isIframeRoute()) {
108
-            return $this->factory->create(RequestTypeContext::IFRAME);
109
-        }
110
-        // Detect Feeds
111
-        if ($this->isFeedRequest()) {
112
-            return $this->factory->create(RequestTypeContext::FEED);
113
-        }
114
-        // and by process of elimination...
115
-        return $this->factory->create(RequestTypeContext::FRONTEND);
116
-    }
117
-
118
-
119
-    /**
120
-     * @return bool
121
-     */
122
-    private function isEspressoRestApiRequest()
123
-    {
124
-        $ee_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0')
125
-            ? trim(rest_get_url_prefix(), '/')
126
-            : 'wp-json';
127
-        $ee_rest_url_prefix .= '/' . Domain::API_NAMESPACE;
128
-        return $this->uriPathMatches($ee_rest_url_prefix);
129
-    }
130
-
131
-
132
-
133
-    /**
134
-     * @return bool
135
-     */
136
-    private function isWorPressRestApiRequest()
137
-    {
138
-        $wp_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0')
139
-            ? trim(rest_get_url_prefix(), '/')
140
-            : 'wp-json';
141
-        return $this->uriPathMatches($wp_rest_url_prefix);
142
-    }
143
-
144
-
145
-    /**
146
-     * @return bool
147
-     */
148
-    private function isCronRequest()
149
-    {
150
-        return $this->uriPathMatches('wp-cron.php');
151
-    }
152
-
153
-
154
-    /**
155
-     * @return bool
156
-     */
157
-    private function isFeedRequest()
158
-    {
159
-        return $this->uriPathMatches('feed');
160
-    }
161
-
162
-
163
-    /**
164
-     * @param string $component
165
-     * @return bool
166
-     */
167
-    private function uriPathMatches($component)
168
-    {
169
-        $request_uri = $this->request->requestUri();
170
-        $parts = explode('?', $request_uri);
171
-        $path = trim(reset($parts), '/');
172
-        return strpos($path, $component) === 0;
173
-    }
174
-
175
-
176
-    /**
177
-     * @return bool
178
-     */
179
-    private function isIframeRoute()
180
-    {
181
-        $is_iframe_route = apply_filters(
182
-            'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute',
183
-            $this->request->getRequestParam('event_list', '') === 'iframe'
184
-            || $this->request->getRequestParam('ticket_selector', '') === 'iframe'
185
-            || $this->request->getRequestParam('calendar', '') === 'iframe',
186
-            $this
187
-        );
188
-        return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN);
189
-    }
22
+	/**
23
+	 * @var RequestTypeContextFactoryInterface $factory
24
+	 */
25
+	private $factory;
26
+
27
+	/**
28
+	 * @var RequestInterface $request
29
+	 */
30
+	private $request;
31
+
32
+	/**
33
+	 * @var array $constants
34
+	 */
35
+	private $constants;
36
+
37
+
38
+	/**
39
+	 * RequestTypeContextDetector constructor.
40
+	 *
41
+	 * @param RequestInterface                   $request
42
+	 * @param RequestTypeContextFactoryInterface $factory
43
+	 */
44
+	public function __construct(
45
+		RequestInterface $request,
46
+		RequestTypeContextFactoryInterface $factory,
47
+		array $constants = array()
48
+	) {
49
+		$this->request = $request;
50
+		$this->factory = $factory;
51
+		$this->constants = $constants;
52
+	}
53
+
54
+
55
+	/**
56
+	 * @return mixed
57
+	 */
58
+	private function getConstant($constant, $default)
59
+	{
60
+		return isset($this->constants[ $constant ]) ? $this->constants[ $constant ] : $default;
61
+	}
62
+
63
+
64
+	/**
65
+	 * @return RequestTypeContext
66
+	 * @throws InvalidArgumentException
67
+	 */
68
+	public function detectRequestTypeContext()
69
+	{
70
+		// Detect error scrapes
71
+		if ($this->request->getRequestParam('wp_scrape_key') !== null
72
+			&& $this->request->getRequestParam('wp_scrape_nonce') !== null
73
+		) {
74
+			return $this->factory->create(RequestTypeContext::WP_SCRAPE);
75
+		}
76
+		// Detect EE REST API
77
+		if ($this->isEspressoRestApiRequest()) {
78
+			return $this->factory->create(RequestTypeContext::API);
79
+		}
80
+		// Detect WP REST API
81
+		if ($this->isWorPressRestApiRequest()) {
82
+			return $this->factory->create(RequestTypeContext::WP_API);
83
+		}
84
+		// Detect AJAX
85
+		if ($this->getConstant('DOING_AJAX', false)) {
86
+			if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) {
87
+				return $this->factory->create(RequestTypeContext::AJAX_FRONT);
88
+			}
89
+			if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) {
90
+				return $this->factory->create(RequestTypeContext::AJAX_ADMIN);
91
+			}
92
+			return $this->factory->create(RequestTypeContext::AJAX_OTHER);
93
+		}
94
+		// Detect WP_Cron
95
+		if ($this->isCronRequest()) {
96
+			return $this->factory->create(RequestTypeContext::CRON);
97
+		}
98
+		// Detect command line requests
99
+		if ($this->getConstant('WP_CLI', false)) {
100
+			return $this->factory->create(RequestTypeContext::CLI);
101
+		}
102
+		// detect WordPress admin (ie: "Dashboard")
103
+		if ($this->getConstant('is_admin', false)) {
104
+			return $this->factory->create(RequestTypeContext::ADMIN);
105
+		}
106
+		// Detect iFrames
107
+		if ($this->isIframeRoute()) {
108
+			return $this->factory->create(RequestTypeContext::IFRAME);
109
+		}
110
+		// Detect Feeds
111
+		if ($this->isFeedRequest()) {
112
+			return $this->factory->create(RequestTypeContext::FEED);
113
+		}
114
+		// and by process of elimination...
115
+		return $this->factory->create(RequestTypeContext::FRONTEND);
116
+	}
117
+
118
+
119
+	/**
120
+	 * @return bool
121
+	 */
122
+	private function isEspressoRestApiRequest()
123
+	{
124
+		$ee_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0')
125
+			? trim(rest_get_url_prefix(), '/')
126
+			: 'wp-json';
127
+		$ee_rest_url_prefix .= '/' . Domain::API_NAMESPACE;
128
+		return $this->uriPathMatches($ee_rest_url_prefix);
129
+	}
130
+
131
+
132
+
133
+	/**
134
+	 * @return bool
135
+	 */
136
+	private function isWorPressRestApiRequest()
137
+	{
138
+		$wp_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0')
139
+			? trim(rest_get_url_prefix(), '/')
140
+			: 'wp-json';
141
+		return $this->uriPathMatches($wp_rest_url_prefix);
142
+	}
143
+
144
+
145
+	/**
146
+	 * @return bool
147
+	 */
148
+	private function isCronRequest()
149
+	{
150
+		return $this->uriPathMatches('wp-cron.php');
151
+	}
152
+
153
+
154
+	/**
155
+	 * @return bool
156
+	 */
157
+	private function isFeedRequest()
158
+	{
159
+		return $this->uriPathMatches('feed');
160
+	}
161
+
162
+
163
+	/**
164
+	 * @param string $component
165
+	 * @return bool
166
+	 */
167
+	private function uriPathMatches($component)
168
+	{
169
+		$request_uri = $this->request->requestUri();
170
+		$parts = explode('?', $request_uri);
171
+		$path = trim(reset($parts), '/');
172
+		return strpos($path, $component) === 0;
173
+	}
174
+
175
+
176
+	/**
177
+	 * @return bool
178
+	 */
179
+	private function isIframeRoute()
180
+	{
181
+		$is_iframe_route = apply_filters(
182
+			'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute',
183
+			$this->request->getRequestParam('event_list', '') === 'iframe'
184
+			|| $this->request->getRequestParam('ticket_selector', '') === 'iframe'
185
+			|| $this->request->getRequestParam('calendar', '') === 'iframe',
186
+			$this
187
+		);
188
+		return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN);
189
+	}
190 190
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
      */
58 58
     private function getConstant($constant, $default)
59 59
     {
60
-        return isset($this->constants[ $constant ]) ? $this->constants[ $constant ] : $default;
60
+        return isset($this->constants[$constant]) ? $this->constants[$constant] : $default;
61 61
     }
62 62
 
63 63
 
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
         $ee_rest_url_prefix = RecommendedVersions::compareWordPressVersion('4.4.0')
125 125
             ? trim(rest_get_url_prefix(), '/')
126 126
             : 'wp-json';
127
-        $ee_rest_url_prefix .= '/' . Domain::API_NAMESPACE;
127
+        $ee_rest_url_prefix .= '/'.Domain::API_NAMESPACE;
128 128
         return $this->uriPathMatches($ee_rest_url_prefix);
129 129
     }
130 130
 
Please login to merge, or discard this patch.
core/services/request/Request.php 2 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
      *
175 175
      * @param       $key
176 176
      * @param null  $default
177
-     * @return mixed
177
+     * @return integer
178 178
      */
179 179
     public function getRequestParam($key, $default = null)
180 180
     {
@@ -287,7 +287,7 @@  discard block
 block discarded – undo
287 287
      * would return true if default parameters were set
288 288
      *
289 289
      * @param string $callback
290
-     * @param        $key
290
+     * @param        string $key
291 291
      * @param null   $default
292 292
      * @param array  $request_params
293 293
      * @return bool|mixed|null
Please login to merge, or discard this patch.
Indentation   +592 added lines, -592 removed lines patch added patch discarded remove patch
@@ -17,596 +17,596 @@
 block discarded – undo
17 17
 class Request implements InterminableInterface, RequestInterface, ReservedInstanceInterface
18 18
 {
19 19
 
20
-    /**
21
-     * $_GET parameters
22
-     *
23
-     * @var array $get
24
-     */
25
-    private $get;
26
-
27
-    /**
28
-     * $_POST parameters
29
-     *
30
-     * @var array $post
31
-     */
32
-    private $post;
33
-
34
-    /**
35
-     * $_COOKIE parameters
36
-     *
37
-     * @var array $cookie
38
-     */
39
-    private $cookie;
40
-
41
-    /**
42
-     * $_SERVER parameters
43
-     *
44
-     * @var array $server
45
-     */
46
-    private $server;
47
-
48
-    /**
49
-     * $_REQUEST parameters
50
-     *
51
-     * @var array $request
52
-     */
53
-    private $request;
54
-
55
-    /**
56
-     * @var RequestTypeContextCheckerInterface
57
-     */
58
-    private $request_type;
59
-
60
-    /**
61
-     * IP address for request
62
-     *
63
-     * @var string $ip_address
64
-     */
65
-    private $ip_address;
66
-
67
-    /**
68
-     * @var string $user_agent
69
-     */
70
-    private $user_agent;
71
-
72
-    /**
73
-     * true if current user appears to be some kind of bot
74
-     *
75
-     * @var bool $is_bot
76
-     */
77
-    private $is_bot;
78
-
79
-
80
-    /**
81
-     * @param array $get
82
-     * @param array $post
83
-     * @param array $cookie
84
-     * @param array $server
85
-     */
86
-    public function __construct(array $get, array $post, array $cookie, array $server)
87
-    {
88
-        // grab request vars
89
-        $this->get = $get;
90
-        $this->post = $post;
91
-        $this->cookie = $cookie;
92
-        $this->server = $server;
93
-        $this->request = array_merge($this->get, $this->post);
94
-        $this->ip_address = $this->visitorIp();
95
-    }
96
-
97
-
98
-    /**
99
-     * @param RequestTypeContextCheckerInterface $type
100
-     */
101
-    public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type)
102
-    {
103
-        $this->request_type = $type;
104
-    }
105
-
106
-
107
-    /**
108
-     * @return array
109
-     */
110
-    public function getParams()
111
-    {
112
-        return $this->get;
113
-    }
114
-
115
-
116
-    /**
117
-     * @return array
118
-     */
119
-    public function postParams()
120
-    {
121
-        return $this->post;
122
-    }
123
-
124
-
125
-    /**
126
-     * @return array
127
-     */
128
-    public function cookieParams()
129
-    {
130
-        return $this->cookie;
131
-    }
132
-
133
-
134
-    /**
135
-     * @return array
136
-     */
137
-    public function serverParams()
138
-    {
139
-        return $this->server;
140
-    }
141
-
142
-
143
-    /**
144
-     * returns contents of $_REQUEST
145
-     *
146
-     * @return array
147
-     */
148
-    public function requestParams()
149
-    {
150
-        return $this->request;
151
-    }
152
-
153
-
154
-    /**
155
-     * @param      $key
156
-     * @param      $value
157
-     * @param bool $override_ee
158
-     * @return    void
159
-     */
160
-    public function setRequestParam($key, $value, $override_ee = false)
161
-    {
162
-        // don't allow "ee" to be overwritten unless explicitly instructed to do so
163
-        if ($key !== 'ee'
164
-            || ($key === 'ee' && empty($this->request['ee']))
165
-            || ($key === 'ee' && ! empty($this->request['ee']) && $override_ee)
166
-        ) {
167
-            $this->request[ $key ] = $value;
168
-        }
169
-    }
170
-
171
-
172
-    /**
173
-     * returns   the value for a request param if the given key exists
174
-     *
175
-     * @param       $key
176
-     * @param null  $default
177
-     * @return mixed
178
-     */
179
-    public function getRequestParam($key, $default = null)
180
-    {
181
-        return $this->requestParameterDrillDown($key, $default, 'get');
182
-    }
183
-
184
-
185
-    /**
186
-     * check if param exists
187
-     *
188
-     * @param       $key
189
-     * @return bool
190
-     */
191
-    public function requestParamIsSet($key)
192
-    {
193
-        return $this->requestParameterDrillDown($key);
194
-    }
195
-
196
-
197
-    /**
198
-     * check if a request parameter exists whose key that matches the supplied wildcard pattern
199
-     * and return the value for the first match found
200
-     * wildcards can be either of the following:
201
-     *      ? to represent a single character of any type
202
-     *      * to represent one or more characters of any type
203
-     *
204
-     * @param string     $pattern
205
-     * @param null|mixed $default
206
-     * @return mixed
207
-     */
208
-    public function getMatch($pattern, $default = null)
209
-    {
210
-        return $this->requestParameterDrillDown($pattern, $default, 'match');
211
-    }
212
-
213
-
214
-    /**
215
-     * check if a request parameter exists whose key matches the supplied wildcard pattern
216
-     * wildcards can be either of the following:
217
-     *      ? to represent a single character of any type
218
-     *      * to represent one or more characters of any type
219
-     * returns true if a match is found or false if not
220
-     *
221
-     * @param string $pattern
222
-     * @return bool
223
-     */
224
-    public function matches($pattern)
225
-    {
226
-        return $this->requestParameterDrillDown($pattern, null, 'match') !== null;
227
-    }
228
-
229
-
230
-    /**
231
-     * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard
232
-     * @param string $pattern               A string including wildcards to be converted to a regex pattern
233
-     *                                      and used to search through the current request's parameter keys
234
-     * @param array  $request_params        The array of request parameters to search through
235
-     * @param mixed  $default               [optional] The value to be returned if no match is found.
236
-     *                                      Default is null
237
-     * @param string $return                [optional] Controls what kind of value is returned.
238
-     *                                      Options are:
239
-     *                                      'bool' will return true or false if match is found or not
240
-     *                                      'key' will return the first key found that matches the supplied pattern
241
-     *                                      'value' will return the value for the first request parameter
242
-     *                                      whose key matches the supplied pattern
243
-     *                                      Default is 'value'
244
-     * @return boolean|string
245
-     */
246
-    private function match($pattern, array $request_params, $default = null, $return = 'value')
247
-    {
248
-        $return = in_array($return, array('bool', 'key', 'value'), true)
249
-            ? $return
250
-            : 'is_set';
251
-        // replace wildcard chars with regex chars
252
-        $pattern = str_replace(
253
-            array("\*", "\?"),
254
-            array('.*', '.'),
255
-            preg_quote($pattern, '/')
256
-        );
257
-        foreach ($request_params as $key => $request_param) {
258
-            if (preg_match('/^' . $pattern . '$/is', $key)) {
259
-                // return value for request param
260
-                if ($return === 'value') {
261
-                    return $request_params[ $key ];
262
-                }
263
-                // or actual key or true just to indicate it was found
264
-                return $return === 'key' ? $key : true;
265
-            }
266
-        }
267
-        // match not found so return default value or false
268
-        return $return === 'value' ? $default : false;
269
-    }
270
-
271
-
272
-    /**
273
-     * the supplied key can be a simple string to represent a "top-level" request parameter
274
-     * or represent a key for a request parameter that is nested deeper within the request parameter array,
275
-     * by using square brackets to surround keys for deeper array elements.
276
-     * For example :
277
-     * if the supplied $key was: "first[second][third]"
278
-     * then this will attempt to drill down into the request parameter array to find a value.
279
-     * Given the following request parameters:
280
-     *  array(
281
-     *      'first' => array(
282
-     *          'second' => array(
283
-     *              'third' => 'has a value'
284
-     *          )
285
-     *      )
286
-     *  )
287
-     * would return true if default parameters were set
288
-     *
289
-     * @param string $callback
290
-     * @param        $key
291
-     * @param null   $default
292
-     * @param array  $request_params
293
-     * @return bool|mixed|null
294
-     */
295
-    private function requestParameterDrillDown(
296
-        $key,
297
-        $default = null,
298
-        $callback = 'is_set',
299
-        array $request_params = array()
300
-    ) {
301
-        $callback = in_array($callback, array('is_set', 'get', 'match'), true)
302
-            ? $callback
303
-            : 'is_set';
304
-        $request_params = ! empty($request_params)
305
-            ? $request_params
306
-            : $this->request;
307
-        // does incoming key represent an array like 'first[second][third]'  ?
308
-        if (strpos($key, '[') !== false) {
309
-            // turn it into an actual array
310
-            $key = str_replace(']', '', $key);
311
-            $keys = explode('[', $key);
312
-            $key = array_shift($keys);
313
-            if ($callback === 'match') {
314
-                $real_key = $this->match($key, $request_params, $default, 'key');
315
-                $key = $real_key ? $real_key : $key;
316
-            }
317
-            // check if top level key exists
318
-            if (isset($request_params[ $key ])) {
319
-                // build a new key to pass along like: 'second[third]'
320
-                // or just 'second' depending on depth of keys
321
-                $key_string = array_shift($keys);
322
-                if (! empty($keys)) {
323
-                    $key_string .= '[' . implode('][', $keys) . ']';
324
-                }
325
-                return $this->requestParameterDrillDown(
326
-                    $key_string,
327
-                    $default,
328
-                    $callback,
329
-                    $request_params[ $key ]
330
-                );
331
-            }
332
-        }
333
-        if ($callback === 'is_set') {
334
-            return isset($request_params[ $key ]);
335
-        }
336
-        if ($callback === 'match') {
337
-            return $this->match($key, $request_params, $default);
338
-        }
339
-        return isset($request_params[ $key ])
340
-            ? $request_params[ $key ]
341
-            : $default;
342
-    }
343
-
344
-
345
-    /**
346
-     * remove param
347
-     *
348
-     * @param      $key
349
-     * @param bool $unset_from_global_too
350
-     */
351
-    public function unSetRequestParam($key, $unset_from_global_too = false)
352
-    {
353
-        unset($this->request[ $key ]);
354
-        if ($unset_from_global_too) {
355
-            unset($_REQUEST[ $key ]);
356
-        }
357
-    }
358
-
359
-
360
-    /**
361
-     * @return string
362
-     */
363
-    public function ipAddress()
364
-    {
365
-        return $this->ip_address;
366
-    }
367
-
368
-
369
-    /**
370
-     * attempt to get IP address of current visitor from server
371
-     * plz see: http://stackoverflow.com/a/2031935/1475279
372
-     *
373
-     * @access public
374
-     * @return string
375
-     */
376
-    private function visitorIp()
377
-    {
378
-        $visitor_ip = '0.0.0.0';
379
-        $server_keys = array(
380
-            'HTTP_CLIENT_IP',
381
-            'HTTP_X_FORWARDED_FOR',
382
-            'HTTP_X_FORWARDED',
383
-            'HTTP_X_CLUSTER_CLIENT_IP',
384
-            'HTTP_FORWARDED_FOR',
385
-            'HTTP_FORWARDED',
386
-            'REMOTE_ADDR',
387
-        );
388
-        foreach ($server_keys as $key) {
389
-            if (isset($this->server[ $key ])) {
390
-                foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) {
391
-                    if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) {
392
-                        $visitor_ip = $ip;
393
-                    }
394
-                }
395
-            }
396
-        }
397
-        return $visitor_ip;
398
-    }
399
-
400
-
401
-    /**
402
-     * @return string
403
-     */
404
-    public function requestUri()
405
-    {
406
-        $request_uri = filter_input(
407
-            INPUT_SERVER,
408
-            'REQUEST_URI',
409
-            FILTER_SANITIZE_URL,
410
-            FILTER_NULL_ON_FAILURE
411
-        );
412
-        if (empty($request_uri)) {
413
-            // fallback sanitization if the above fails
414
-            $request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']);
415
-        }
416
-        return $request_uri;
417
-    }
418
-
419
-
420
-    /**
421
-     * @return string
422
-     */
423
-    public function userAgent()
424
-    {
425
-        return $this->user_agent;
426
-    }
427
-
428
-
429
-    /**
430
-     * @param string $user_agent
431
-     */
432
-    public function setUserAgent($user_agent = '')
433
-    {
434
-        if ($user_agent === '' || ! is_string($user_agent)) {
435
-            $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
436
-        }
437
-        $this->user_agent = $user_agent;
438
-    }
439
-
440
-
441
-    /**
442
-     * @return bool
443
-     */
444
-    public function isBot()
445
-    {
446
-        return $this->is_bot;
447
-    }
448
-
449
-
450
-    /**
451
-     * @param bool $is_bot
452
-     */
453
-    public function setIsBot($is_bot)
454
-    {
455
-        $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN);
456
-    }
457
-
458
-
459
-    /**
460
-     * @return bool
461
-     */
462
-    public function isActivation()
463
-    {
464
-        return $this->request_type->isActivation();
465
-    }
466
-
467
-
468
-    /**
469
-     * @param $is_activation
470
-     * @return bool
471
-     */
472
-    public function setIsActivation($is_activation)
473
-    {
474
-        return $this->request_type->setIsActivation($is_activation);
475
-    }
476
-
477
-
478
-    /**
479
-     * @return bool
480
-     */
481
-    public function isAdmin()
482
-    {
483
-        return $this->request_type->isAdmin();
484
-    }
485
-
486
-
487
-    /**
488
-     * @return bool
489
-     */
490
-    public function isAdminAjax()
491
-    {
492
-        return $this->request_type->isAdminAjax();
493
-    }
494
-
495
-
496
-    /**
497
-     * @return bool
498
-     */
499
-    public function isAjax()
500
-    {
501
-        return $this->request_type->isAjax();
502
-    }
503
-
504
-
505
-    /**
506
-     * @return bool
507
-     */
508
-    public function isEeAjax()
509
-    {
510
-        return $this->request_type->isEeAjax();
511
-    }
512
-
513
-
514
-    /**
515
-     * @return bool
516
-     */
517
-    public function isOtherAjax()
518
-    {
519
-        return $this->request_type->isOtherAjax();
520
-    }
521
-
522
-
523
-    /**
524
-     * @return bool
525
-     */
526
-    public function isApi()
527
-    {
528
-        return $this->request_type->isApi();
529
-    }
530
-
531
-
532
-    /**
533
-     * @return bool
534
-     */
535
-    public function isCli()
536
-    {
537
-        return $this->request_type->isCli();
538
-    }
539
-
540
-
541
-    /**
542
-     * @return bool
543
-     */
544
-    public function isCron()
545
-    {
546
-        return $this->request_type->isCron();
547
-    }
548
-
549
-
550
-    /**
551
-     * @return bool
552
-     */
553
-    public function isFeed()
554
-    {
555
-        return $this->request_type->isFeed();
556
-    }
557
-
558
-
559
-    /**
560
-     * @return bool
561
-     */
562
-    public function isFrontend()
563
-    {
564
-        return $this->request_type->isFrontend();
565
-    }
566
-
567
-
568
-    /**
569
-     * @return bool
570
-     */
571
-    public function isFrontAjax()
572
-    {
573
-        return $this->request_type->isFrontAjax();
574
-    }
575
-
576
-
577
-    /**
578
-     * @return bool
579
-     */
580
-    public function isIframe()
581
-    {
582
-        return $this->request_type->isIframe();
583
-    }
584
-
585
-
586
-    /**
587
-     * @return bool
588
-     */
589
-    public function isWordPressApi()
590
-    {
591
-        return $this->request_type->isWordPressApi();
592
-    }
593
-
594
-
595
-
596
-    /**
597
-     * @return bool
598
-     */
599
-    public function isWordPressScrape()
600
-    {
601
-        return $this->request_type->isWordPressScrape();
602
-    }
603
-
604
-
605
-    /**
606
-     * @return string
607
-     */
608
-    public function slug()
609
-    {
610
-        return $this->request_type->slug();
611
-    }
20
+	/**
21
+	 * $_GET parameters
22
+	 *
23
+	 * @var array $get
24
+	 */
25
+	private $get;
26
+
27
+	/**
28
+	 * $_POST parameters
29
+	 *
30
+	 * @var array $post
31
+	 */
32
+	private $post;
33
+
34
+	/**
35
+	 * $_COOKIE parameters
36
+	 *
37
+	 * @var array $cookie
38
+	 */
39
+	private $cookie;
40
+
41
+	/**
42
+	 * $_SERVER parameters
43
+	 *
44
+	 * @var array $server
45
+	 */
46
+	private $server;
47
+
48
+	/**
49
+	 * $_REQUEST parameters
50
+	 *
51
+	 * @var array $request
52
+	 */
53
+	private $request;
54
+
55
+	/**
56
+	 * @var RequestTypeContextCheckerInterface
57
+	 */
58
+	private $request_type;
59
+
60
+	/**
61
+	 * IP address for request
62
+	 *
63
+	 * @var string $ip_address
64
+	 */
65
+	private $ip_address;
66
+
67
+	/**
68
+	 * @var string $user_agent
69
+	 */
70
+	private $user_agent;
71
+
72
+	/**
73
+	 * true if current user appears to be some kind of bot
74
+	 *
75
+	 * @var bool $is_bot
76
+	 */
77
+	private $is_bot;
78
+
79
+
80
+	/**
81
+	 * @param array $get
82
+	 * @param array $post
83
+	 * @param array $cookie
84
+	 * @param array $server
85
+	 */
86
+	public function __construct(array $get, array $post, array $cookie, array $server)
87
+	{
88
+		// grab request vars
89
+		$this->get = $get;
90
+		$this->post = $post;
91
+		$this->cookie = $cookie;
92
+		$this->server = $server;
93
+		$this->request = array_merge($this->get, $this->post);
94
+		$this->ip_address = $this->visitorIp();
95
+	}
96
+
97
+
98
+	/**
99
+	 * @param RequestTypeContextCheckerInterface $type
100
+	 */
101
+	public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type)
102
+	{
103
+		$this->request_type = $type;
104
+	}
105
+
106
+
107
+	/**
108
+	 * @return array
109
+	 */
110
+	public function getParams()
111
+	{
112
+		return $this->get;
113
+	}
114
+
115
+
116
+	/**
117
+	 * @return array
118
+	 */
119
+	public function postParams()
120
+	{
121
+		return $this->post;
122
+	}
123
+
124
+
125
+	/**
126
+	 * @return array
127
+	 */
128
+	public function cookieParams()
129
+	{
130
+		return $this->cookie;
131
+	}
132
+
133
+
134
+	/**
135
+	 * @return array
136
+	 */
137
+	public function serverParams()
138
+	{
139
+		return $this->server;
140
+	}
141
+
142
+
143
+	/**
144
+	 * returns contents of $_REQUEST
145
+	 *
146
+	 * @return array
147
+	 */
148
+	public function requestParams()
149
+	{
150
+		return $this->request;
151
+	}
152
+
153
+
154
+	/**
155
+	 * @param      $key
156
+	 * @param      $value
157
+	 * @param bool $override_ee
158
+	 * @return    void
159
+	 */
160
+	public function setRequestParam($key, $value, $override_ee = false)
161
+	{
162
+		// don't allow "ee" to be overwritten unless explicitly instructed to do so
163
+		if ($key !== 'ee'
164
+			|| ($key === 'ee' && empty($this->request['ee']))
165
+			|| ($key === 'ee' && ! empty($this->request['ee']) && $override_ee)
166
+		) {
167
+			$this->request[ $key ] = $value;
168
+		}
169
+	}
170
+
171
+
172
+	/**
173
+	 * returns   the value for a request param if the given key exists
174
+	 *
175
+	 * @param       $key
176
+	 * @param null  $default
177
+	 * @return mixed
178
+	 */
179
+	public function getRequestParam($key, $default = null)
180
+	{
181
+		return $this->requestParameterDrillDown($key, $default, 'get');
182
+	}
183
+
184
+
185
+	/**
186
+	 * check if param exists
187
+	 *
188
+	 * @param       $key
189
+	 * @return bool
190
+	 */
191
+	public function requestParamIsSet($key)
192
+	{
193
+		return $this->requestParameterDrillDown($key);
194
+	}
195
+
196
+
197
+	/**
198
+	 * check if a request parameter exists whose key that matches the supplied wildcard pattern
199
+	 * and return the value for the first match found
200
+	 * wildcards can be either of the following:
201
+	 *      ? to represent a single character of any type
202
+	 *      * to represent one or more characters of any type
203
+	 *
204
+	 * @param string     $pattern
205
+	 * @param null|mixed $default
206
+	 * @return mixed
207
+	 */
208
+	public function getMatch($pattern, $default = null)
209
+	{
210
+		return $this->requestParameterDrillDown($pattern, $default, 'match');
211
+	}
212
+
213
+
214
+	/**
215
+	 * check if a request parameter exists whose key matches the supplied wildcard pattern
216
+	 * wildcards can be either of the following:
217
+	 *      ? to represent a single character of any type
218
+	 *      * to represent one or more characters of any type
219
+	 * returns true if a match is found or false if not
220
+	 *
221
+	 * @param string $pattern
222
+	 * @return bool
223
+	 */
224
+	public function matches($pattern)
225
+	{
226
+		return $this->requestParameterDrillDown($pattern, null, 'match') !== null;
227
+	}
228
+
229
+
230
+	/**
231
+	 * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard
232
+	 * @param string $pattern               A string including wildcards to be converted to a regex pattern
233
+	 *                                      and used to search through the current request's parameter keys
234
+	 * @param array  $request_params        The array of request parameters to search through
235
+	 * @param mixed  $default               [optional] The value to be returned if no match is found.
236
+	 *                                      Default is null
237
+	 * @param string $return                [optional] Controls what kind of value is returned.
238
+	 *                                      Options are:
239
+	 *                                      'bool' will return true or false if match is found or not
240
+	 *                                      'key' will return the first key found that matches the supplied pattern
241
+	 *                                      'value' will return the value for the first request parameter
242
+	 *                                      whose key matches the supplied pattern
243
+	 *                                      Default is 'value'
244
+	 * @return boolean|string
245
+	 */
246
+	private function match($pattern, array $request_params, $default = null, $return = 'value')
247
+	{
248
+		$return = in_array($return, array('bool', 'key', 'value'), true)
249
+			? $return
250
+			: 'is_set';
251
+		// replace wildcard chars with regex chars
252
+		$pattern = str_replace(
253
+			array("\*", "\?"),
254
+			array('.*', '.'),
255
+			preg_quote($pattern, '/')
256
+		);
257
+		foreach ($request_params as $key => $request_param) {
258
+			if (preg_match('/^' . $pattern . '$/is', $key)) {
259
+				// return value for request param
260
+				if ($return === 'value') {
261
+					return $request_params[ $key ];
262
+				}
263
+				// or actual key or true just to indicate it was found
264
+				return $return === 'key' ? $key : true;
265
+			}
266
+		}
267
+		// match not found so return default value or false
268
+		return $return === 'value' ? $default : false;
269
+	}
270
+
271
+
272
+	/**
273
+	 * the supplied key can be a simple string to represent a "top-level" request parameter
274
+	 * or represent a key for a request parameter that is nested deeper within the request parameter array,
275
+	 * by using square brackets to surround keys for deeper array elements.
276
+	 * For example :
277
+	 * if the supplied $key was: "first[second][third]"
278
+	 * then this will attempt to drill down into the request parameter array to find a value.
279
+	 * Given the following request parameters:
280
+	 *  array(
281
+	 *      'first' => array(
282
+	 *          'second' => array(
283
+	 *              'third' => 'has a value'
284
+	 *          )
285
+	 *      )
286
+	 *  )
287
+	 * would return true if default parameters were set
288
+	 *
289
+	 * @param string $callback
290
+	 * @param        $key
291
+	 * @param null   $default
292
+	 * @param array  $request_params
293
+	 * @return bool|mixed|null
294
+	 */
295
+	private function requestParameterDrillDown(
296
+		$key,
297
+		$default = null,
298
+		$callback = 'is_set',
299
+		array $request_params = array()
300
+	) {
301
+		$callback = in_array($callback, array('is_set', 'get', 'match'), true)
302
+			? $callback
303
+			: 'is_set';
304
+		$request_params = ! empty($request_params)
305
+			? $request_params
306
+			: $this->request;
307
+		// does incoming key represent an array like 'first[second][third]'  ?
308
+		if (strpos($key, '[') !== false) {
309
+			// turn it into an actual array
310
+			$key = str_replace(']', '', $key);
311
+			$keys = explode('[', $key);
312
+			$key = array_shift($keys);
313
+			if ($callback === 'match') {
314
+				$real_key = $this->match($key, $request_params, $default, 'key');
315
+				$key = $real_key ? $real_key : $key;
316
+			}
317
+			// check if top level key exists
318
+			if (isset($request_params[ $key ])) {
319
+				// build a new key to pass along like: 'second[third]'
320
+				// or just 'second' depending on depth of keys
321
+				$key_string = array_shift($keys);
322
+				if (! empty($keys)) {
323
+					$key_string .= '[' . implode('][', $keys) . ']';
324
+				}
325
+				return $this->requestParameterDrillDown(
326
+					$key_string,
327
+					$default,
328
+					$callback,
329
+					$request_params[ $key ]
330
+				);
331
+			}
332
+		}
333
+		if ($callback === 'is_set') {
334
+			return isset($request_params[ $key ]);
335
+		}
336
+		if ($callback === 'match') {
337
+			return $this->match($key, $request_params, $default);
338
+		}
339
+		return isset($request_params[ $key ])
340
+			? $request_params[ $key ]
341
+			: $default;
342
+	}
343
+
344
+
345
+	/**
346
+	 * remove param
347
+	 *
348
+	 * @param      $key
349
+	 * @param bool $unset_from_global_too
350
+	 */
351
+	public function unSetRequestParam($key, $unset_from_global_too = false)
352
+	{
353
+		unset($this->request[ $key ]);
354
+		if ($unset_from_global_too) {
355
+			unset($_REQUEST[ $key ]);
356
+		}
357
+	}
358
+
359
+
360
+	/**
361
+	 * @return string
362
+	 */
363
+	public function ipAddress()
364
+	{
365
+		return $this->ip_address;
366
+	}
367
+
368
+
369
+	/**
370
+	 * attempt to get IP address of current visitor from server
371
+	 * plz see: http://stackoverflow.com/a/2031935/1475279
372
+	 *
373
+	 * @access public
374
+	 * @return string
375
+	 */
376
+	private function visitorIp()
377
+	{
378
+		$visitor_ip = '0.0.0.0';
379
+		$server_keys = array(
380
+			'HTTP_CLIENT_IP',
381
+			'HTTP_X_FORWARDED_FOR',
382
+			'HTTP_X_FORWARDED',
383
+			'HTTP_X_CLUSTER_CLIENT_IP',
384
+			'HTTP_FORWARDED_FOR',
385
+			'HTTP_FORWARDED',
386
+			'REMOTE_ADDR',
387
+		);
388
+		foreach ($server_keys as $key) {
389
+			if (isset($this->server[ $key ])) {
390
+				foreach (array_map('trim', explode(',', $this->server[ $key ])) as $ip) {
391
+					if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) {
392
+						$visitor_ip = $ip;
393
+					}
394
+				}
395
+			}
396
+		}
397
+		return $visitor_ip;
398
+	}
399
+
400
+
401
+	/**
402
+	 * @return string
403
+	 */
404
+	public function requestUri()
405
+	{
406
+		$request_uri = filter_input(
407
+			INPUT_SERVER,
408
+			'REQUEST_URI',
409
+			FILTER_SANITIZE_URL,
410
+			FILTER_NULL_ON_FAILURE
411
+		);
412
+		if (empty($request_uri)) {
413
+			// fallback sanitization if the above fails
414
+			$request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']);
415
+		}
416
+		return $request_uri;
417
+	}
418
+
419
+
420
+	/**
421
+	 * @return string
422
+	 */
423
+	public function userAgent()
424
+	{
425
+		return $this->user_agent;
426
+	}
427
+
428
+
429
+	/**
430
+	 * @param string $user_agent
431
+	 */
432
+	public function setUserAgent($user_agent = '')
433
+	{
434
+		if ($user_agent === '' || ! is_string($user_agent)) {
435
+			$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
436
+		}
437
+		$this->user_agent = $user_agent;
438
+	}
439
+
440
+
441
+	/**
442
+	 * @return bool
443
+	 */
444
+	public function isBot()
445
+	{
446
+		return $this->is_bot;
447
+	}
448
+
449
+
450
+	/**
451
+	 * @param bool $is_bot
452
+	 */
453
+	public function setIsBot($is_bot)
454
+	{
455
+		$this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN);
456
+	}
457
+
458
+
459
+	/**
460
+	 * @return bool
461
+	 */
462
+	public function isActivation()
463
+	{
464
+		return $this->request_type->isActivation();
465
+	}
466
+
467
+
468
+	/**
469
+	 * @param $is_activation
470
+	 * @return bool
471
+	 */
472
+	public function setIsActivation($is_activation)
473
+	{
474
+		return $this->request_type->setIsActivation($is_activation);
475
+	}
476
+
477
+
478
+	/**
479
+	 * @return bool
480
+	 */
481
+	public function isAdmin()
482
+	{
483
+		return $this->request_type->isAdmin();
484
+	}
485
+
486
+
487
+	/**
488
+	 * @return bool
489
+	 */
490
+	public function isAdminAjax()
491
+	{
492
+		return $this->request_type->isAdminAjax();
493
+	}
494
+
495
+
496
+	/**
497
+	 * @return bool
498
+	 */
499
+	public function isAjax()
500
+	{
501
+		return $this->request_type->isAjax();
502
+	}
503
+
504
+
505
+	/**
506
+	 * @return bool
507
+	 */
508
+	public function isEeAjax()
509
+	{
510
+		return $this->request_type->isEeAjax();
511
+	}
512
+
513
+
514
+	/**
515
+	 * @return bool
516
+	 */
517
+	public function isOtherAjax()
518
+	{
519
+		return $this->request_type->isOtherAjax();
520
+	}
521
+
522
+
523
+	/**
524
+	 * @return bool
525
+	 */
526
+	public function isApi()
527
+	{
528
+		return $this->request_type->isApi();
529
+	}
530
+
531
+
532
+	/**
533
+	 * @return bool
534
+	 */
535
+	public function isCli()
536
+	{
537
+		return $this->request_type->isCli();
538
+	}
539
+
540
+
541
+	/**
542
+	 * @return bool
543
+	 */
544
+	public function isCron()
545
+	{
546
+		return $this->request_type->isCron();
547
+	}
548
+
549
+
550
+	/**
551
+	 * @return bool
552
+	 */
553
+	public function isFeed()
554
+	{
555
+		return $this->request_type->isFeed();
556
+	}
557
+
558
+
559
+	/**
560
+	 * @return bool
561
+	 */
562
+	public function isFrontend()
563
+	{
564
+		return $this->request_type->isFrontend();
565
+	}
566
+
567
+
568
+	/**
569
+	 * @return bool
570
+	 */
571
+	public function isFrontAjax()
572
+	{
573
+		return $this->request_type->isFrontAjax();
574
+	}
575
+
576
+
577
+	/**
578
+	 * @return bool
579
+	 */
580
+	public function isIframe()
581
+	{
582
+		return $this->request_type->isIframe();
583
+	}
584
+
585
+
586
+	/**
587
+	 * @return bool
588
+	 */
589
+	public function isWordPressApi()
590
+	{
591
+		return $this->request_type->isWordPressApi();
592
+	}
593
+
594
+
595
+
596
+	/**
597
+	 * @return bool
598
+	 */
599
+	public function isWordPressScrape()
600
+	{
601
+		return $this->request_type->isWordPressScrape();
602
+	}
603
+
604
+
605
+	/**
606
+	 * @return string
607
+	 */
608
+	public function slug()
609
+	{
610
+		return $this->request_type->slug();
611
+	}
612 612
 }
Please login to merge, or discard this patch.
core/domain/services/contexts/RequestTypeContextFactory.php 1 patch
Indentation   +108 added lines, -108 removed lines patch added patch discarded remove patch
@@ -17,116 +17,116 @@
 block discarded – undo
17 17
 class RequestTypeContextFactory implements RequestTypeContextFactoryInterface
18 18
 {
19 19
 
20
-    /**
21
-     * @var LoaderInterface $loader
22
-     */
23
-    private $loader;
20
+	/**
21
+	 * @var LoaderInterface $loader
22
+	 */
23
+	private $loader;
24 24
 
25 25
 
26
-    /**
27
-     * RequestTypeContextFactory constructor.
28
-     *
29
-     * @param LoaderInterface $loader
30
-     */
31
-    public function __construct(LoaderInterface $loader)
32
-    {
33
-        $this->loader = $loader;
34
-    }
26
+	/**
27
+	 * RequestTypeContextFactory constructor.
28
+	 *
29
+	 * @param LoaderInterface $loader
30
+	 */
31
+	public function __construct(LoaderInterface $loader)
32
+	{
33
+		$this->loader = $loader;
34
+	}
35 35
 
36 36
 
37
-    /**
38
-     * @param string $slug
39
-     * @return RequestTypeContext
40
-     */
41
-    public function create($slug)
42
-    {
43
-        switch ($slug) {
44
-            case RequestTypeContext::ACTIVATION:
45
-                $description = esc_html__(
46
-                    'The current request is for some form of activation',
47
-                    'event_espresso'
48
-                );
49
-                break;
50
-            case RequestTypeContext::API:
51
-                $description = esc_html__(
52
-                    'The current request is for the EE REST API',
53
-                    'event_espresso'
54
-                );
55
-                break;
56
-            case RequestTypeContext::AJAX_FRONT:
57
-                $description = esc_html__(
58
-                    'The current request is for the frontend via AJAX',
59
-                    'event_espresso'
60
-                );
61
-                break;
62
-            case RequestTypeContext::AJAX_ADMIN:
63
-                $description = esc_html__(
64
-                    'The current request is for the admin via AJAX',
65
-                    'event_espresso'
66
-                );
67
-                break;
68
-            case RequestTypeContext::AJAX_OTHER:
69
-                $description = esc_html__(
70
-                    'The current request is for non-EE related code via AJAX',
71
-                    'event_espresso'
72
-                );
73
-                break;
74
-            case RequestTypeContext::CRON:
75
-                $description = esc_html__(
76
-                    'The current request is for a WP_Cron',
77
-                    'event_espresso'
78
-                );
79
-                break;
80
-            case RequestTypeContext::CLI:
81
-                $description = esc_html__(
82
-                    'The current request is from the command line',
83
-                    'event_espresso'
84
-                );
85
-                break;
86
-            case RequestTypeContext::ADMIN:
87
-                $description = esc_html__(
88
-                    'The current request is for the admin',
89
-                    'event_espresso'
90
-                );
91
-                break;
92
-            case RequestTypeContext::IFRAME:
93
-                $description = esc_html__(
94
-                    'The current request is for an iframe',
95
-                    'event_espresso'
96
-                );
97
-                break;
98
-            case RequestTypeContext::FEED:
99
-                $description = esc_html__(
100
-                    'The current request is for a feed (ie: RSS)',
101
-                    'event_espresso'
102
-                );
103
-                break;
104
-            case RequestTypeContext::WP_API:
105
-                $description = esc_html__(
106
-                    'The current request is for the WordPress REST API',
107
-                    'event_espresso'
108
-                );
109
-                break;
110
-            case RequestTypeContext::WP_SCRAPE:
111
-                $description = esc_html__(
112
-                    'The current request is for a WordPress loopback scrape',
113
-                    'event_espresso'
114
-                );
115
-                break;
116
-            case RequestTypeContext::FRONTEND:
117
-            default:
118
-                $description = esc_html__(
119
-                    'The current request is for the frontend',
120
-                    'event_espresso'
121
-                );
122
-                break;
123
-        }
124
-        // we're using the Loader with sharing turned on,
125
-        // so that the generated RequestTypeContext object is accessible anywhere
126
-        // by simply requesting it again from the loader
127
-        return $this->loader->getShared(
128
-            'EventEspresso\core\domain\entities\contexts\RequestTypeContext',
129
-            array($slug, $description)
130
-        );
131
-    }
37
+	/**
38
+	 * @param string $slug
39
+	 * @return RequestTypeContext
40
+	 */
41
+	public function create($slug)
42
+	{
43
+		switch ($slug) {
44
+			case RequestTypeContext::ACTIVATION:
45
+				$description = esc_html__(
46
+					'The current request is for some form of activation',
47
+					'event_espresso'
48
+				);
49
+				break;
50
+			case RequestTypeContext::API:
51
+				$description = esc_html__(
52
+					'The current request is for the EE REST API',
53
+					'event_espresso'
54
+				);
55
+				break;
56
+			case RequestTypeContext::AJAX_FRONT:
57
+				$description = esc_html__(
58
+					'The current request is for the frontend via AJAX',
59
+					'event_espresso'
60
+				);
61
+				break;
62
+			case RequestTypeContext::AJAX_ADMIN:
63
+				$description = esc_html__(
64
+					'The current request is for the admin via AJAX',
65
+					'event_espresso'
66
+				);
67
+				break;
68
+			case RequestTypeContext::AJAX_OTHER:
69
+				$description = esc_html__(
70
+					'The current request is for non-EE related code via AJAX',
71
+					'event_espresso'
72
+				);
73
+				break;
74
+			case RequestTypeContext::CRON:
75
+				$description = esc_html__(
76
+					'The current request is for a WP_Cron',
77
+					'event_espresso'
78
+				);
79
+				break;
80
+			case RequestTypeContext::CLI:
81
+				$description = esc_html__(
82
+					'The current request is from the command line',
83
+					'event_espresso'
84
+				);
85
+				break;
86
+			case RequestTypeContext::ADMIN:
87
+				$description = esc_html__(
88
+					'The current request is for the admin',
89
+					'event_espresso'
90
+				);
91
+				break;
92
+			case RequestTypeContext::IFRAME:
93
+				$description = esc_html__(
94
+					'The current request is for an iframe',
95
+					'event_espresso'
96
+				);
97
+				break;
98
+			case RequestTypeContext::FEED:
99
+				$description = esc_html__(
100
+					'The current request is for a feed (ie: RSS)',
101
+					'event_espresso'
102
+				);
103
+				break;
104
+			case RequestTypeContext::WP_API:
105
+				$description = esc_html__(
106
+					'The current request is for the WordPress REST API',
107
+					'event_espresso'
108
+				);
109
+				break;
110
+			case RequestTypeContext::WP_SCRAPE:
111
+				$description = esc_html__(
112
+					'The current request is for a WordPress loopback scrape',
113
+					'event_espresso'
114
+				);
115
+				break;
116
+			case RequestTypeContext::FRONTEND:
117
+			default:
118
+				$description = esc_html__(
119
+					'The current request is for the frontend',
120
+					'event_espresso'
121
+				);
122
+				break;
123
+		}
124
+		// we're using the Loader with sharing turned on,
125
+		// so that the generated RequestTypeContext object is accessible anywhere
126
+		// by simply requesting it again from the loader
127
+		return $this->loader->getShared(
128
+			'EventEspresso\core\domain\entities\contexts\RequestTypeContext',
129
+			array($slug, $description)
130
+		);
131
+	}
132 132
 }
Please login to merge, or discard this patch.
core/domain/services/contexts/RequestTypeContextFactoryInterface.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -15,9 +15,9 @@
 block discarded – undo
15 15
 interface RequestTypeContextFactoryInterface
16 16
 {
17 17
 
18
-    /**
19
-     * @param string $slug
20
-     * @return RequestTypeContext
21
-     */
22
-    public function create($slug);
18
+	/**
19
+	 * @param string $slug
20
+	 * @return RequestTypeContext
21
+	 */
22
+	public function create($slug);
23 23
 }
Please login to merge, or discard this patch.
core/services/request/middleware/SetRequestTypeContextChecker.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -18,41 +18,41 @@
 block discarded – undo
18 18
 class SetRequestTypeContextChecker extends Middleware
19 19
 {
20 20
 
21
-    /**
22
-     * converts a Request to a Response
23
-     *
24
-     * @param RequestInterface  $request
25
-     * @param ResponseInterface $response
26
-     * @return ResponseInterface
27
-     * @throws InvalidArgumentException
28
-     */
29
-    public function handleRequest(RequestInterface $request, ResponseInterface $response)
30
-    {
31
-        $this->request  = $request;
32
-        $this->response = $response;
33
-        /** @var RequestTypeContextDetector $request_type_context_detector */
34
-        $request_type_context_detector = $this->loader->getShared(
35
-            'EventEspresso\core\domain\services\contexts\RequestTypeContextDetector',
36
-            array(
37
-                $this->request,
38
-                $this->loader->getShared(
39
-                    'EventEspresso\core\domain\services\contexts\RequestTypeContextFactory',
40
-                    array($this->loader)
41
-                ),
42
-                array(
43
-                    'DOING_AJAX' => defined('DOING_AJAX') && DOING_AJAX,
44
-                    'WP_CLI'     => defined('WP_CLI') && WP_CLI,
45
-                    'is_admin'   => is_admin(),
46
-                )
47
-            )
48
-        );
49
-        $request_type_context          = $request_type_context_detector->detectRequestTypeContext();
50
-        $request_type_context_checker  = $this->loader->getShared(
51
-            'EventEspresso\core\domain\services\contexts\RequestTypeContextChecker',
52
-            array($request_type_context)
53
-        );
54
-        $this->request->setRequestTypeContextChecker($request_type_context_checker);
55
-        $this->response = $this->processRequestStack($this->request, $this->response);
56
-        return $this->response;
57
-    }
21
+	/**
22
+	 * converts a Request to a Response
23
+	 *
24
+	 * @param RequestInterface  $request
25
+	 * @param ResponseInterface $response
26
+	 * @return ResponseInterface
27
+	 * @throws InvalidArgumentException
28
+	 */
29
+	public function handleRequest(RequestInterface $request, ResponseInterface $response)
30
+	{
31
+		$this->request  = $request;
32
+		$this->response = $response;
33
+		/** @var RequestTypeContextDetector $request_type_context_detector */
34
+		$request_type_context_detector = $this->loader->getShared(
35
+			'EventEspresso\core\domain\services\contexts\RequestTypeContextDetector',
36
+			array(
37
+				$this->request,
38
+				$this->loader->getShared(
39
+					'EventEspresso\core\domain\services\contexts\RequestTypeContextFactory',
40
+					array($this->loader)
41
+				),
42
+				array(
43
+					'DOING_AJAX' => defined('DOING_AJAX') && DOING_AJAX,
44
+					'WP_CLI'     => defined('WP_CLI') && WP_CLI,
45
+					'is_admin'   => is_admin(),
46
+				)
47
+			)
48
+		);
49
+		$request_type_context          = $request_type_context_detector->detectRequestTypeContext();
50
+		$request_type_context_checker  = $this->loader->getShared(
51
+			'EventEspresso\core\domain\services\contexts\RequestTypeContextChecker',
52
+			array($request_type_context)
53
+		);
54
+		$this->request->setRequestTypeContextChecker($request_type_context_checker);
55
+		$this->response = $this->processRequestStack($this->request, $this->response);
56
+		return $this->response;
57
+	}
58 58
 }
Please login to merge, or discard this patch.