1 | <?php |
||
2 | |||
3 | namespace Clubdeuce\WPGoogleMaps; |
||
4 | |||
5 | /** |
||
6 | * Class HTTP |
||
7 | * @package Clubdeuce\WPGoogleMaps |
||
8 | */ |
||
9 | class HTTP { |
||
10 | |||
11 | /** |
||
12 | * @param string $url |
||
13 | * @return \WP_HTTP_Response|\WP_Error |
||
14 | */ |
||
15 | 2 | public function make_request( $url ) { |
|
16 | |||
17 | 2 | $return = new \WP_Error( 1, 'Invalid URL', $url ); |
|
18 | |||
19 | 2 | if ( wp_http_validate_url( $url ) ) { |
|
20 | 1 | $request = $this->_get_data( $url ); |
|
21 | |||
22 | 1 | $return = new \WP_Error( $request['response']['code'], $request['response']['message'] ); |
|
23 | |||
24 | 1 | if ( 200 == $request['response']['code'] ) { |
|
25 | 1 | $return = $request; |
|
26 | } |
||
27 | |||
28 | } |
||
29 | |||
30 | 2 | return $return; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
31 | |||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param $url |
||
36 | * @return array|\WP_Error |
||
37 | */ |
||
38 | 1 | private function _get_data( $url ) { |
|
39 | |||
40 | 1 | $cache_key = md5( serialize( $url ) ); |
|
41 | |||
42 | 1 | if ( ! $data = wp_cache_get( $cache_key ) ) { |
|
43 | $data = wp_remote_get( $url ); |
||
44 | wp_cache_add( $cache_key, $data, 300 ); |
||
45 | } |
||
46 | |||
47 | 1 | return $data; |
|
48 | |||
49 | } |
||
50 | |||
51 | } |