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
Pull Request — master (#909)
by Paul
02:48
created

Extensions::get_edd_data()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Class Extensions
7
 * @package HM\BackUpWordPress
8
 */
9
class Extensions {
10
11
	/**
12
	 * Contains the instantiated Extensions instance.
13
	 *
14
	 * @var Extensions $this->instance
15
	 */
16
	private static $instance;
17
18
	/**
19
	 * Holds the root URL of the API.
20
	 *
21
	 * @var string
22
	 */
23
	protected $root_url = '';
24
25
	/**
26
	 * Extensions constructor.
27
	 *
28
	 */
29
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
31
		$this->root_url  = 'https://bwp.hmn.md/wp-json/wp/v2/';
32
33
	}
34
35
	/**
36
	 * Returns the *Singleton* instance of this class.
37
	 *
38
	 * @staticvar Extensions $instance The *Singleton* instances of this class.
39
	 *
40
	 * @return Extensions The *Singleton* instance.
41
	 */
42
	public static function get_instance() {
43
44
		if ( ! ( self::$instance instanceof Extensions ) ) {
45
46
			self::$instance = new Extensions();
47
48
		}
49
50
		return self::$instance;
51
52
	}
53
54
	/**
55
	 * Parses the body of the API response and returns it.
56
	 *
57
	 * @return array|bool|mixed|object
58
	 */
59
	public function get_edd_data() {
60
61
		$response = $this->fetch( 'edd-downloads', 600 );
62
63
		if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
64
			return false;
65
		}
66
67
		return json_decode( $response['body'] );
68
69
	}
70
71
	/**
72
	 * Makes a request to the JSON API or retrieves the cached response. Caches the response for one day.
73
	 *
74
	 * @param $endpoint
75
	 * @param int $ttl
76
	 *
77
	 * @return array|mixed|\WP_Error
78
	 */
79
	function fetch( $endpoint, $ttl = DAY_IN_SECONDS ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
81
		$request_url = $this->root_url . $endpoint;
82
83
		$cache_key = md5( $request_url );
84
85
		$cached = get_transient( 'bwp_' . $cache_key );
86
87
		if ( $cached ) {
88
			return $cached;
89
		}
90
91
		$response = wp_remote_get( $request_url );
92
93
		set_transient( 'bwp_' . $cache_key, $response, $ttl );
94
95
		return $response;
96
97
	}
98
99
}
100