Stencil_Installable_Versions::get()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.5907
cc 6
eloc 12
nc 8
nop 1
1
<?php
2
/**
3
 * Seperate place to keep track of latest versions
4
 *
5
 * @package Stencil
6
 * @subpackage Upgrader
7
 */
8
9
/**
10
 * Class Stencil_Installable_Versions
11
 */
12
class Stencil_Installable_Versions {
13
	/**
14
	 * Latest versions
15
	 *
16
	 * @var array
17
	 */
18
	private static $versions = array();
19
20
	/**
21
	 * Has the version file been loaded or not?
22
	 *
23
	 * @var bool
24
	 */
25
	private static $loaded = false;
26
27
	/**
28
	 * Get version for installable
29
	 *
30
	 * @param Stencil_Installable_Interface $installable Installable to get version of.
31
	 *
32
	 * @return string
33
	 */
34
	public static function get( Stencil_Installable_Interface $installable ) {
35
		self::load();
36
37
		if ( empty( self::$versions ) ) {
38
			return '0.0.0';
39
		}
40
41
		$slug = $installable->get_slug();
42
43
		if ( is_a( $installable, 'Stencil_Installable_Plugin' ) ) {
44
			if ( isset( self::$versions['plugins'][ $slug ] ) ) {
45
				return self::$versions['plugins'][ $slug ];
46
			}
47
		}
48
49
		if ( is_a( $installable, 'Stencil_Installable_Theme' ) ) {
50
			if ( isset( self::$versions['themes'][ $slug ] ) ) {
51
				return self::$versions['themes'][ $slug ];
52
			}
53
		}
54
55
		return '0.0.0';
56
	}
57
58
	/**
59
	 * Load versions from json file.
60
	 *
61
	 * @return array|bool
62
	 */
63
	private static function load() {
64
		if ( self::$loaded ) {
65
			return;
66
		}
67
68
		self::$loaded = true;
69
70
		$filename      = 'https://raw.githubusercontent.com/moorscode/stencil/master/config/versions.json';
71
		$versions_json = file_get_contents( $filename );
72
		if ( empty( $versions_json ) ) {
73
			return;
74
		}
75
76
		$decoded = json_decode( $versions_json, true );
77
		if ( is_array( $decoded ) ) {
78
			self::$versions = $decoded;
79
		}
80
	}
81
}
82