GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9c0c8a...f2b063 )
by Brad
05:17 queued 02:45
created

FooGallery_Pro_Video_Base::json_post()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 2
dl 0
loc 9
rs 9.2222
c 0
b 0
f 0
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
}