Completed
Pull Request — master (#1069)
by
unknown
09:21
created
core/domain/services/contexts/RequestTypeContextDetector.php 2 patches
Indentation   +175 added lines, -175 removed lines patch added patch discarded remove patch
@@ -18,179 +18,179 @@
 block discarded – undo
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
-        return strpos(
133
-            $this->request->getRequestParam('rest_route', false),
134
-            '/' . Domain::API_NAMESPACE
135
-            ) === 0
136
-            || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE);
137
-    }
138
-
139
-
140
-
141
-    /**
142
-     * @return bool
143
-     */
144
-    private function isWordPressRestApiRequest()
145
-    {
146
-        return $this->request->getRequestParam('rest_route', false)
147
-            || $this->uriPathMatches(trim(rest_get_url_prefix(), '/'));
148
-    }
149
-
150
-
151
-    /**
152
-     * @return bool
153
-     */
154
-    private function isCronRequest()
155
-    {
156
-        return $this->uriPathMatches('wp-cron.php');
157
-    }
158
-
159
-
160
-    /**
161
-     * @return bool
162
-     */
163
-    private function isFeedRequest()
164
-    {
165
-        return $this->uriPathMatches('feed');
166
-    }
167
-
168
-
169
-    /**
170
-     * @param string $component
171
-     * @return bool
172
-     */
173
-    private function uriPathMatches($component)
174
-    {
175
-        $request_uri = $this->request->requestUri();
176
-        $parts = explode('?', $request_uri);
177
-        $path = trim(reset($parts), '/');
178
-        return strpos($path, $component) === 0;
179
-    }
180
-
181
-
182
-    /**
183
-     * @return bool
184
-     */
185
-    private function isIframeRoute()
186
-    {
187
-        $is_iframe_route = apply_filters(
188
-            'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute',
189
-            $this->request->getRequestParam('event_list', '') === 'iframe'
190
-            || $this->request->getRequestParam('ticket_selector', '') === 'iframe'
191
-            || $this->request->getRequestParam('calendar', '') === 'iframe',
192
-            $this
193
-        );
194
-        return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN);
195
-    }
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
+		return strpos(
133
+			$this->request->getRequestParam('rest_route', false),
134
+			'/' . Domain::API_NAMESPACE
135
+			) === 0
136
+			|| $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE);
137
+	}
138
+
139
+
140
+
141
+	/**
142
+	 * @return bool
143
+	 */
144
+	private function isWordPressRestApiRequest()
145
+	{
146
+		return $this->request->getRequestParam('rest_route', false)
147
+			|| $this->uriPathMatches(trim(rest_get_url_prefix(), '/'));
148
+	}
149
+
150
+
151
+	/**
152
+	 * @return bool
153
+	 */
154
+	private function isCronRequest()
155
+	{
156
+		return $this->uriPathMatches('wp-cron.php');
157
+	}
158
+
159
+
160
+	/**
161
+	 * @return bool
162
+	 */
163
+	private function isFeedRequest()
164
+	{
165
+		return $this->uriPathMatches('feed');
166
+	}
167
+
168
+
169
+	/**
170
+	 * @param string $component
171
+	 * @return bool
172
+	 */
173
+	private function uriPathMatches($component)
174
+	{
175
+		$request_uri = $this->request->requestUri();
176
+		$parts = explode('?', $request_uri);
177
+		$path = trim(reset($parts), '/');
178
+		return strpos($path, $component) === 0;
179
+	}
180
+
181
+
182
+	/**
183
+	 * @return bool
184
+	 */
185
+	private function isIframeRoute()
186
+	{
187
+		$is_iframe_route = apply_filters(
188
+			'FHEE__EventEspresso_core_domain_services_contexts_RequestTypeContextDetector__isIframeRoute',
189
+			$this->request->getRequestParam('event_list', '') === 'iframe'
190
+			|| $this->request->getRequestParam('ticket_selector', '') === 'iframe'
191
+			|| $this->request->getRequestParam('calendar', '') === 'iframe',
192
+			$this
193
+		);
194
+		return filter_var($is_iframe_route, FILTER_VALIDATE_BOOLEAN);
195
+	}
196 196
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
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
 
@@ -131,9 +131,9 @@  discard block
 block discarded – undo
131 131
     {
132 132
         return strpos(
133 133
             $this->request->getRequestParam('rest_route', false),
134
-            '/' . Domain::API_NAMESPACE
134
+            '/'.Domain::API_NAMESPACE
135 135
             ) === 0
136
-            || $this->uriPathMatches(trim(rest_get_url_prefix(), '/') . '/' . Domain::API_NAMESPACE);
136
+            || $this->uriPathMatches(trim(rest_get_url_prefix(), '/').'/'.Domain::API_NAMESPACE);
137 137
     }
138 138
 
139 139
 
Please login to merge, or discard this patch.