1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base class for video imports and queries |
4
|
|
|
* Date: 17/04/2018 |
5
|
|
|
*/ |
6
|
|
|
if ( ! class_exists( 'FooGallery_Pro_Video_Base' ) ) { |
7
|
|
|
|
8
|
|
|
class FooGallery_Pro_Video_Base { |
|
|
|
|
9
|
|
|
|
10
|
|
|
protected function error_response($message = "Unknown Error", $title = "An Error Occurred") { |
11
|
|
|
return array( |
12
|
|
|
"mode" => "error", |
13
|
|
|
"title" => $title, |
14
|
|
|
"message" => $message |
15
|
|
|
); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected function json_get($url, $args = array("method"=>"GET")) { |
19
|
|
|
if (!is_array($args)){ |
20
|
|
|
$args = array("method"=>"GET"); |
21
|
|
|
} |
22
|
|
|
if (!array_key_exists("method", $args) || (array_key_exists("method", $args) && (!is_string($args["method"]) || strtoupper($args["method"]) != "GET"))){ |
23
|
|
|
$args["method"] = "GET"; |
24
|
|
|
} |
25
|
|
|
return $this->json_response($url, $args); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function json_post($url, $args = array("method"=>"POST")) { |
29
|
|
|
if (!is_array($args)){ |
30
|
|
|
$args = array("method"=>"POST"); |
31
|
|
|
} |
32
|
|
|
if (!array_key_exists("method", $args) || (array_key_exists("method", $args) && (!is_string($args["method"]) || strtoupper($args["method"]) != "POST"))){ |
33
|
|
|
$args["method"] = "POST"; |
34
|
|
|
} |
35
|
|
|
return $this->json_response($url, $args); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function json_response($url, $args = array("method"=>"GET")) { |
39
|
|
|
$remote = wp_remote_request($url, $args); |
40
|
|
|
if (is_wp_error($remote)) { |
41
|
|
|
return $this->error_response("Error fetching JSON: " . $remote->get_error_message()); |
42
|
|
|
} |
43
|
|
|
// get the json string from the body of the response |
44
|
|
|
$body = wp_remote_retrieve_body($remote); |
45
|
|
|
// decode it into an object |
46
|
|
|
$json = json_decode($body); |
47
|
|
|
if (!is_object($json) && !is_array($json)){ |
48
|
|
|
return $this->error_response("Error fetching JSON: Invalid response body, unable to decode."); |
49
|
|
|
} |
50
|
|
|
return $json; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function is_error($response) { |
54
|
|
|
return is_array($response) && $response["mode"] === "error"; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function url_exists($url){ |
58
|
|
|
$remote = wp_safe_remote_head($url); |
59
|
|
|
return !is_wp_error($remote) && wp_remote_retrieve_response_code($remote) === 200; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Takes the supplied URL and retrieves its' MIME type. |
64
|
|
|
* |
65
|
|
|
* @param string $url The URL to fetch the MIME type for. |
66
|
|
|
* |
67
|
|
|
* @return bool|string Returns false if the MIME type could not be retrieved. |
68
|
|
|
*/ |
69
|
|
|
protected function get_mime_type($url){ |
70
|
|
|
$remote = wp_safe_remote_head($url); |
71
|
|
|
if (is_wp_error($remote)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
if ( !empty($remote) && is_array($remote["response"]) && $remote["response"]["code"] === 200 ){ |
75
|
|
|
return wp_remote_retrieve_header($remote, "content-type"); |
76
|
|
|
} |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.