author | Anthony Ly <anthonyly.com@gmail.com> |
Tue, 12 Mar 2013 18:21:39 +0100 | |
changeset 206 | 919b4ddb13fa |
parent 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* Simple and uniform HTTP request API. |
|
4 |
* |
|
5 |
* Will eventually replace and standardize the WordPress HTTP requests made. |
|
6 |
* |
|
7 |
* @link http://trac.wordpress.org/ticket/4779 HTTP API Proposal |
|
8 |
* |
|
9 |
* @package WordPress |
|
10 |
* @subpackage HTTP |
|
11 |
* @since 2.7.0 |
|
12 |
*/ |
|
13 |
||
14 |
/** |
|
15 |
* Returns the initialized WP_Http Object |
|
16 |
* |
|
17 |
* @since 2.7.0 |
|
18 |
* @access private |
|
19 |
* |
|
20 |
* @return WP_Http HTTP Transport object. |
|
21 |
*/ |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
22 |
function _wp_http_get_object() { |
136 | 23 |
static $http; |
24 |
||
25 |
if ( is_null($http) ) |
|
26 |
$http = new WP_Http(); |
|
27 |
||
28 |
return $http; |
|
29 |
} |
|
30 |
||
31 |
/** |
|
32 |
* Retrieve the raw response from the HTTP request. |
|
33 |
* |
|
34 |
* The array structure is a little complex. |
|
35 |
* |
|
36 |
* <code> |
|
37 |
* $res = array( 'headers' => array(), 'response' => array('code' => int, 'message' => string) ); |
|
38 |
* </code> |
|
39 |
* |
|
40 |
* All of the headers in $res['headers'] are with the name as the key and the |
|
41 |
* value as the value. So to get the User-Agent, you would do the following. |
|
42 |
* |
|
43 |
* <code> |
|
44 |
* $user_agent = $res['headers']['user-agent']; |
|
45 |
* </code> |
|
46 |
* |
|
47 |
* The body is the raw response content and can be retrieved from $res['body']. |
|
48 |
* |
|
49 |
* This function is called first to make the request and there are other API |
|
50 |
* functions to abstract out the above convoluted setup. |
|
51 |
* |
|
52 |
* @since 2.7.0 |
|
53 |
* |
|
54 |
* @param string $url Site URL to retrieve. |
|
55 |
* @param array $args Optional. Override the defaults. |
|
56 |
* @return WP_Error|array The response or WP_Error on failure. |
|
57 |
*/ |
|
58 |
function wp_remote_request($url, $args = array()) { |
|
59 |
$objFetchSite = _wp_http_get_object(); |
|
60 |
return $objFetchSite->request($url, $args); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Retrieve the raw response from the HTTP request using the GET method. |
|
65 |
* |
|
66 |
* @see wp_remote_request() For more information on the response array format. |
|
67 |
* |
|
68 |
* @since 2.7.0 |
|
69 |
* |
|
70 |
* @param string $url Site URL to retrieve. |
|
71 |
* @param array $args Optional. Override the defaults. |
|
72 |
* @return WP_Error|array The response or WP_Error on failure. |
|
73 |
*/ |
|
74 |
function wp_remote_get($url, $args = array()) { |
|
75 |
$objFetchSite = _wp_http_get_object(); |
|
76 |
return $objFetchSite->get($url, $args); |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Retrieve the raw response from the HTTP request using the POST method. |
|
81 |
* |
|
82 |
* @see wp_remote_request() For more information on the response array format. |
|
83 |
* |
|
84 |
* @since 2.7.0 |
|
85 |
* |
|
86 |
* @param string $url Site URL to retrieve. |
|
87 |
* @param array $args Optional. Override the defaults. |
|
88 |
* @return WP_Error|array The response or WP_Error on failure. |
|
89 |
*/ |
|
90 |
function wp_remote_post($url, $args = array()) { |
|
91 |
$objFetchSite = _wp_http_get_object(); |
|
92 |
return $objFetchSite->post($url, $args); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Retrieve the raw response from the HTTP request using the HEAD method. |
|
97 |
* |
|
98 |
* @see wp_remote_request() For more information on the response array format. |
|
99 |
* |
|
100 |
* @since 2.7.0 |
|
101 |
* |
|
102 |
* @param string $url Site URL to retrieve. |
|
103 |
* @param array $args Optional. Override the defaults. |
|
104 |
* @return WP_Error|array The response or WP_Error on failure. |
|
105 |
*/ |
|
106 |
function wp_remote_head($url, $args = array()) { |
|
107 |
$objFetchSite = _wp_http_get_object(); |
|
108 |
return $objFetchSite->head($url, $args); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Retrieve only the headers from the raw response. |
|
113 |
* |
|
114 |
* @since 2.7.0 |
|
115 |
* |
|
116 |
* @param array $response HTTP response. |
|
117 |
* @return array The headers of the response. Empty array if incorrect parameter given. |
|
118 |
*/ |
|
119 |
function wp_remote_retrieve_headers(&$response) { |
|
120 |
if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers'])) |
|
121 |
return array(); |
|
122 |
||
123 |
return $response['headers']; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Retrieve a single header by name from the raw response. |
|
128 |
* |
|
129 |
* @since 2.7.0 |
|
130 |
* |
|
131 |
* @param array $response |
|
132 |
* @param string $header Header name to retrieve value from. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
133 |
* @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist. |
136 | 134 |
*/ |
135 |
function wp_remote_retrieve_header(&$response, $header) { |
|
136 |
if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers'])) |
|
137 |
return ''; |
|
138 |
||
139 |
if ( array_key_exists($header, $response['headers']) ) |
|
140 |
return $response['headers'][$header]; |
|
141 |
||
142 |
return ''; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Retrieve only the response code from the raw response. |
|
147 |
* |
|
148 |
* Will return an empty array if incorrect parameter value is given. |
|
149 |
* |
|
150 |
* @since 2.7.0 |
|
151 |
* |
|
152 |
* @param array $response HTTP response. |
|
153 |
* @return string the response code. Empty string on incorrect parameter given. |
|
154 |
*/ |
|
155 |
function wp_remote_retrieve_response_code(&$response) { |
|
156 |
if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response'])) |
|
157 |
return ''; |
|
158 |
||
159 |
return $response['response']['code']; |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Retrieve only the response message from the raw response. |
|
164 |
* |
|
165 |
* Will return an empty array if incorrect parameter value is given. |
|
166 |
* |
|
167 |
* @since 2.7.0 |
|
168 |
* |
|
169 |
* @param array $response HTTP response. |
|
170 |
* @return string The response message. Empty string on incorrect parameter given. |
|
171 |
*/ |
|
172 |
function wp_remote_retrieve_response_message(&$response) { |
|
173 |
if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response'])) |
|
174 |
return ''; |
|
175 |
||
176 |
return $response['response']['message']; |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Retrieve only the body from the raw response. |
|
181 |
* |
|
182 |
* @since 2.7.0 |
|
183 |
* |
|
184 |
* @param array $response HTTP response. |
|
185 |
* @return string The body of the response. Empty string if no body or incorrect parameter given. |
|
186 |
*/ |
|
187 |
function wp_remote_retrieve_body(&$response) { |
|
188 |
if ( is_wp_error($response) || ! isset($response['body']) ) |
|
189 |
return ''; |
|
190 |
||
191 |
return $response['body']; |
|
192 |
} |
|
193 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
194 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
195 |
* Determines if there is an HTTP Transport that can process this request. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
196 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
197 |
* @since 3.2.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
198 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
199 |
* @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
200 |
* @param string $url Optional. If given, will check if the URL requires SSL and adds that requirement to the capabilities array. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
201 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
202 |
* @return bool |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
203 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
204 |
function wp_http_supports( $capabilities = array(), $url = null ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
205 |
$objFetchSite = _wp_http_get_object(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
206 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
207 |
$capabilities = wp_parse_args( $capabilities ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
208 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
209 |
$count = count( $capabilities ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
210 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
211 |
// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
212 |
if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
213 |
$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
214 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
215 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
216 |
if ( $url && !isset( $capabilities['ssl'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
217 |
$scheme = parse_url( $url, PHP_URL_SCHEME ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
218 |
if ( 'https' == $scheme || 'ssl' == $scheme ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
219 |
$capabilities['ssl'] = true; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
220 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
221 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
222 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
223 |
return (bool) $objFetchSite->_get_first_available_transport( $capabilities ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
224 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
225 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
226 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
227 |
* Get the HTTP Origin of the current request. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
228 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
229 |
* @since 3.4.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
230 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
231 |
* @return string URL of the origin. Empty string if no origin. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
232 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
233 |
function get_http_origin() { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
234 |
$origin = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
235 |
if ( ! empty ( $_SERVER[ 'HTTP_ORIGIN' ] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
236 |
$origin = $_SERVER[ 'HTTP_ORIGIN' ]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
237 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
238 |
return apply_filters( 'http_origin', $origin ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
239 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
240 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
241 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
242 |
* Retrieve list of allowed http origins. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
243 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
244 |
* @since 3.4.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
245 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
246 |
* @return array Array of origin URLs. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
247 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
248 |
function get_allowed_http_origins() { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
249 |
$admin_origin = parse_url( admin_url() ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
250 |
$home_origin = parse_url( home_url() ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
251 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
252 |
// @todo preserve port? |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
253 |
$allowed_origins = array_unique( array( |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
254 |
'http://' . $admin_origin[ 'host' ], |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
255 |
'https://' . $admin_origin[ 'host' ], |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
256 |
'http://' . $home_origin[ 'host' ], |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
257 |
'https://' . $home_origin[ 'host' ], |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
258 |
) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
259 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
260 |
return apply_filters( 'allowed_http_origins' , $allowed_origins ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
261 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
262 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
263 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
264 |
* Determines if the http origin is an authorized one. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
265 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
266 |
* @since 3.4.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
267 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
268 |
* @param string Origin URL. If not provided, the value of get_http_origin() is used. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
269 |
* @return bool True if the origin is allowed. False otherwise. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
270 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
271 |
function is_allowed_http_origin( $origin = null ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
$origin_arg = $origin; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
273 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
274 |
if ( null === $origin ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
275 |
$origin = get_http_origin(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
276 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
277 |
if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
278 |
$origin = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
279 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
280 |
return apply_filters( 'allowed_http_origin', $origin, $origin_arg ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
281 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
282 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
283 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
284 |
* Send Access-Control-Allow-Origin and related headers if the current request |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
285 |
* is from an allowed origin. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
286 |
* |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
287 |
* If the request is an OPTIONS request, the script exits with either access |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
288 |
* control headers sent, or a 403 response if the origin is not allowed. For |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
289 |
* other request methods, you will receive a return value. |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
290 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
291 |
* @since 3.4.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
292 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
293 |
* @return bool|string Returns the origin URL if headers are sent. Returns false |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
294 |
* if headers are not sent. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
295 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
296 |
function send_origin_headers() { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
297 |
$origin = get_http_origin(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
298 |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
299 |
if ( is_allowed_http_origin( $origin ) ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
300 |
@header( 'Access-Control-Allow-Origin: ' . $origin ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
301 |
@header( 'Access-Control-Allow-Credentials: true' ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
302 |
if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
303 |
exit; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
304 |
return $origin; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
305 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
306 |
|
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
307 |
if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
308 |
status_header( 403 ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
309 |
exit; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
310 |
} |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
311 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
312 |
return false; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
313 |
} |