@@ -18,181 +18,181 @@ |
||
18 | 18 | class RequestTypeContextDetector |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var RequestTypeContextFactoryInterface $factory |
|
23 | - */ |
|
24 | - private $factory; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var RequestInterface $request |
|
28 | - */ |
|
29 | - private $request; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var array $globalRouteConditions |
|
33 | - */ |
|
34 | - private $globalRouteConditions; |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * RequestTypeContextDetector constructor. |
|
39 | - * |
|
40 | - * @param RequestInterface $request |
|
41 | - * @param RequestTypeContextFactoryInterface $factory |
|
42 | - * @param array $globalRouteConditions an array for injecting values that would |
|
43 | - * otherwise be defined as global constants |
|
44 | - * or other global variables for the current |
|
45 | - * request route such as DOING_AJAX |
|
46 | - */ |
|
47 | - public function __construct( |
|
48 | - RequestInterface $request, |
|
49 | - RequestTypeContextFactoryInterface $factory, |
|
50 | - array $globalRouteConditions = array() |
|
51 | - ) { |
|
52 | - $this->request = $request; |
|
53 | - $this->factory = $factory; |
|
54 | - $this->globalRouteConditions = $globalRouteConditions; |
|
55 | - } |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * @return mixed |
|
60 | - */ |
|
61 | - private function getGlobalRouteCondition($globalRouteCondition, $default) |
|
62 | - { |
|
63 | - return isset($this->globalRouteConditions[ $globalRouteCondition ]) |
|
64 | - ? $this->globalRouteConditions[ $globalRouteCondition ] |
|
65 | - : $default; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @return RequestTypeContext |
|
71 | - * @throws InvalidArgumentException |
|
72 | - */ |
|
73 | - public function detectRequestTypeContext() |
|
74 | - { |
|
75 | - // Detect error scrapes |
|
76 | - if ($this->request->getRequestParam('wp_scrape_key') !== null |
|
77 | - && $this->request->getRequestParam('wp_scrape_nonce') !== null |
|
78 | - ) { |
|
79 | - return $this->factory->create(RequestTypeContext::WP_SCRAPE); |
|
80 | - } |
|
81 | - // Detect EE REST API |
|
82 | - if ($this->isEspressoRestApiRequest()) { |
|
83 | - return $this->factory->create(RequestTypeContext::API); |
|
84 | - } |
|
85 | - // Detect WP REST API |
|
86 | - if ($this->isWordPressRestApiRequest()) { |
|
87 | - return $this->factory->create(RequestTypeContext::WP_API); |
|
88 | - } |
|
89 | - // Detect AJAX |
|
90 | - if ($this->getGlobalRouteCondition('DOING_AJAX', false)) { |
|
91 | - if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
92 | - return $this->factory->create(RequestTypeContext::AJAX_FRONT); |
|
93 | - } |
|
94 | - if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
95 | - return $this->factory->create(RequestTypeContext::AJAX_ADMIN); |
|
96 | - } |
|
97 | - if ($this->request->getRequestParam('action') === 'heartbeat') { |
|
98 | - return $this->factory->create(RequestTypeContext::AJAX_HEARTBEAT); |
|
99 | - } |
|
100 | - return $this->factory->create(RequestTypeContext::AJAX_OTHER); |
|
101 | - } |
|
102 | - // Detect WP_Cron |
|
103 | - if ($this->isCronRequest()) { |
|
104 | - return $this->factory->create(RequestTypeContext::CRON); |
|
105 | - } |
|
106 | - // Detect command line requests |
|
107 | - if ($this->getGlobalRouteCondition('WP_CLI', false)) { |
|
108 | - return $this->factory->create(RequestTypeContext::CLI); |
|
109 | - } |
|
110 | - // detect WordPress admin (ie: "Dashboard") |
|
111 | - if ($this->getGlobalRouteCondition('is_admin', false)) { |
|
112 | - return $this->factory->create(RequestTypeContext::ADMIN); |
|
113 | - } |
|
114 | - // Detect iFrames |
|
115 | - if ($this->isIframeRoute()) { |
|
116 | - return $this->factory->create(RequestTypeContext::IFRAME); |
|
117 | - } |
|
118 | - // Detect Feeds |
|
119 | - if ($this->isFeedRequest()) { |
|
120 | - return $this->factory->create(RequestTypeContext::FEED); |
|
121 | - } |
|
122 | - // and by process of elimination... |
|
123 | - return $this->factory->create(RequestTypeContext::FRONTEND); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * @return bool |
|
129 | - */ |
|
130 | - private function isEspressoRestApiRequest() |
|
131 | - { |
|
132 | - // Check for URLs like http://mysite.com/?rest_route=/ee... and http://mysite.com/wp-json/ee/... |
|
133 | - return strpos( |
|
134 | - $this->request->getRequestParam('rest_route', false), |
|
135 | - '/' . Domain::API_NAMESPACE |
|
136 | - ) === 0 |
|
137 | - || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - private function isWordPressRestApiRequest() |
|
146 | - { |
|
147 | - // Check for URLs like http://mysite.com/?rest_route=/.. and http://mysite.com/wp-json/... |
|
148 | - return $this->request->getRequestParam('rest_route', false) |
|
149 | - || $this->uriPathMatches(trim(rest_get_url_prefix(), '/')); |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * @return bool |
|
155 | - */ |
|
156 | - private function isCronRequest() |
|
157 | - { |
|
158 | - return $this->uriPathMatches('wp-cron.php'); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * @return bool |
|
164 | - */ |
|
165 | - private function isFeedRequest() |
|
166 | - { |
|
167 | - return $this->uriPathMatches('feed'); |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @param string $component |
|
173 | - * @return bool |
|
174 | - */ |
|
175 | - private function uriPathMatches($component) |
|
176 | - { |
|
177 | - $request_uri = $this->request->requestUri(); |
|
178 | - $parts = explode('?', $request_uri); |
|
179 | - $path = trim(reset($parts), '/'); |
|
180 | - return strpos($path, $component) === 0; |
|
181 | - } |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * @return bool |
|
186 | - */ |
|
187 | - private function isIframeRoute() |
|
188 | - { |
|
189 | - $is_iframe_route = apply_filters( |
|
190 | - 'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute', |
|
191 | - $this->request->getRequestParam('event_list', '') === 'iframe' |
|
192 | - || $this->request->getRequestParam('ticket_selector', '') === 'iframe' |
|
193 | - || $this->request->getRequestParam('calendar', '') === 'iframe', |
|
194 | - $this |
|
195 | - ); |
|
196 | - return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN); |
|
197 | - } |
|
21 | + /** |
|
22 | + * @var RequestTypeContextFactoryInterface $factory |
|
23 | + */ |
|
24 | + private $factory; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var RequestInterface $request |
|
28 | + */ |
|
29 | + private $request; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var array $globalRouteConditions |
|
33 | + */ |
|
34 | + private $globalRouteConditions; |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * RequestTypeContextDetector constructor. |
|
39 | + * |
|
40 | + * @param RequestInterface $request |
|
41 | + * @param RequestTypeContextFactoryInterface $factory |
|
42 | + * @param array $globalRouteConditions an array for injecting values that would |
|
43 | + * otherwise be defined as global constants |
|
44 | + * or other global variables for the current |
|
45 | + * request route such as DOING_AJAX |
|
46 | + */ |
|
47 | + public function __construct( |
|
48 | + RequestInterface $request, |
|
49 | + RequestTypeContextFactoryInterface $factory, |
|
50 | + array $globalRouteConditions = array() |
|
51 | + ) { |
|
52 | + $this->request = $request; |
|
53 | + $this->factory = $factory; |
|
54 | + $this->globalRouteConditions = $globalRouteConditions; |
|
55 | + } |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * @return mixed |
|
60 | + */ |
|
61 | + private function getGlobalRouteCondition($globalRouteCondition, $default) |
|
62 | + { |
|
63 | + return isset($this->globalRouteConditions[ $globalRouteCondition ]) |
|
64 | + ? $this->globalRouteConditions[ $globalRouteCondition ] |
|
65 | + : $default; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @return RequestTypeContext |
|
71 | + * @throws InvalidArgumentException |
|
72 | + */ |
|
73 | + public function detectRequestTypeContext() |
|
74 | + { |
|
75 | + // Detect error scrapes |
|
76 | + if ($this->request->getRequestParam('wp_scrape_key') !== null |
|
77 | + && $this->request->getRequestParam('wp_scrape_nonce') !== null |
|
78 | + ) { |
|
79 | + return $this->factory->create(RequestTypeContext::WP_SCRAPE); |
|
80 | + } |
|
81 | + // Detect EE REST API |
|
82 | + if ($this->isEspressoRestApiRequest()) { |
|
83 | + return $this->factory->create(RequestTypeContext::API); |
|
84 | + } |
|
85 | + // Detect WP REST API |
|
86 | + if ($this->isWordPressRestApiRequest()) { |
|
87 | + return $this->factory->create(RequestTypeContext::WP_API); |
|
88 | + } |
|
89 | + // Detect AJAX |
|
90 | + if ($this->getGlobalRouteCondition('DOING_AJAX', false)) { |
|
91 | + if (filter_var($this->request->getRequestParam('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
92 | + return $this->factory->create(RequestTypeContext::AJAX_FRONT); |
|
93 | + } |
|
94 | + if (filter_var($this->request->getRequestParam('ee_admin_ajax'), FILTER_VALIDATE_BOOLEAN)) { |
|
95 | + return $this->factory->create(RequestTypeContext::AJAX_ADMIN); |
|
96 | + } |
|
97 | + if ($this->request->getRequestParam('action') === 'heartbeat') { |
|
98 | + return $this->factory->create(RequestTypeContext::AJAX_HEARTBEAT); |
|
99 | + } |
|
100 | + return $this->factory->create(RequestTypeContext::AJAX_OTHER); |
|
101 | + } |
|
102 | + // Detect WP_Cron |
|
103 | + if ($this->isCronRequest()) { |
|
104 | + return $this->factory->create(RequestTypeContext::CRON); |
|
105 | + } |
|
106 | + // Detect command line requests |
|
107 | + if ($this->getGlobalRouteCondition('WP_CLI', false)) { |
|
108 | + return $this->factory->create(RequestTypeContext::CLI); |
|
109 | + } |
|
110 | + // detect WordPress admin (ie: "Dashboard") |
|
111 | + if ($this->getGlobalRouteCondition('is_admin', false)) { |
|
112 | + return $this->factory->create(RequestTypeContext::ADMIN); |
|
113 | + } |
|
114 | + // Detect iFrames |
|
115 | + if ($this->isIframeRoute()) { |
|
116 | + return $this->factory->create(RequestTypeContext::IFRAME); |
|
117 | + } |
|
118 | + // Detect Feeds |
|
119 | + if ($this->isFeedRequest()) { |
|
120 | + return $this->factory->create(RequestTypeContext::FEED); |
|
121 | + } |
|
122 | + // and by process of elimination... |
|
123 | + return $this->factory->create(RequestTypeContext::FRONTEND); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * @return bool |
|
129 | + */ |
|
130 | + private function isEspressoRestApiRequest() |
|
131 | + { |
|
132 | + // Check for URLs like http://mysite.com/?rest_route=/ee... and http://mysite.com/wp-json/ee/... |
|
133 | + return strpos( |
|
134 | + $this->request->getRequestParam('rest_route', false), |
|
135 | + '/' . Domain::API_NAMESPACE |
|
136 | + ) === 0 |
|
137 | + || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + private function isWordPressRestApiRequest() |
|
146 | + { |
|
147 | + // Check for URLs like http://mysite.com/?rest_route=/.. and http://mysite.com/wp-json/... |
|
148 | + return $this->request->getRequestParam('rest_route', false) |
|
149 | + || $this->uriPathMatches(trim(rest_get_url_prefix(), '/')); |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * @return bool |
|
155 | + */ |
|
156 | + private function isCronRequest() |
|
157 | + { |
|
158 | + return $this->uriPathMatches('wp-cron.php'); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * @return bool |
|
164 | + */ |
|
165 | + private function isFeedRequest() |
|
166 | + { |
|
167 | + return $this->uriPathMatches('feed'); |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @param string $component |
|
173 | + * @return bool |
|
174 | + */ |
|
175 | + private function uriPathMatches($component) |
|
176 | + { |
|
177 | + $request_uri = $this->request->requestUri(); |
|
178 | + $parts = explode('?', $request_uri); |
|
179 | + $path = trim(reset($parts), '/'); |
|
180 | + return strpos($path, $component) === 0; |
|
181 | + } |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * @return bool |
|
186 | + */ |
|
187 | + private function isIframeRoute() |
|
188 | + { |
|
189 | + $is_iframe_route = apply_filters( |
|
190 | + 'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute', |
|
191 | + $this->request->getRequestParam('event_list', '') === 'iframe' |
|
192 | + || $this->request->getRequestParam('ticket_selector', '') === 'iframe' |
|
193 | + || $this->request->getRequestParam('calendar', '') === 'iframe', |
|
194 | + $this |
|
195 | + ); |
|
196 | + return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN); |
|
197 | + } |
|
198 | 198 | } |
@@ -60,8 +60,8 @@ discard block |
||
60 | 60 | */ |
61 | 61 | private function getGlobalRouteCondition($globalRouteCondition, $default) |
62 | 62 | { |
63 | - return isset($this->globalRouteConditions[ $globalRouteCondition ]) |
|
64 | - ? $this->globalRouteConditions[ $globalRouteCondition ] |
|
63 | + return isset($this->globalRouteConditions[$globalRouteCondition]) |
|
64 | + ? $this->globalRouteConditions[$globalRouteCondition] |
|
65 | 65 | : $default; |
66 | 66 | } |
67 | 67 | |
@@ -132,9 +132,9 @@ discard block |
||
132 | 132 | // Check for URLs like http://mysite.com/?rest_route=/ee... and http://mysite.com/wp-json/ee/... |
133 | 133 | return strpos( |
134 | 134 | $this->request->getRequestParam('rest_route', false), |
135 | - '/' . Domain::API_NAMESPACE |
|
135 | + '/'.Domain::API_NAMESPACE |
|
136 | 136 | ) === 0 |
137 | - || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE); |
|
137 | + || $this->uriPathMatches(trim(rest_get_url_prefix(), '/').'/'.Domain::API_NAMESPACE); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 |