Stencil_Upgrader::get_option_name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Update the upgrader
4
 *
5
 * @package Stencil
6
 * @subpackage Upgrader
7
 */
8
9
/**
10
 * Class Stencil_Upgrader
11
 */
12
class Stencil_Upgrader {
13
14
	/**
15
	 * Check for upgrades once a day.
16
	 */
17
	const DAY_TIMESTAMP = 86400; // 60*60*24 = one da
18
19
	/**
20
	 * Packages that can be upgraded
21
	 *
22
	 * @var array
23
	 */
24
	protected $upgrades = array();
25
26
	/**
27
	 * Installables instance.
28
	 *
29
	 * @var Stencil_Installables
30
	 */
31
	protected $installables;
32
33
	/**
34
	 * * Transient name to use for periodical upgrade checks.
35
	 */
36
	const TRANSIENT_NAME = 'stencil_upgrader_upgrader:last_check_timestamp';
37
38
	/**
39
	 * Stencil_Upgrader constructor.
40
	 */
41
	public function __construct() {
42
		// Periodically check for upgrades.
43
		$option_name = $this->get_option_name();
44
		$timeout     = $this->get_upgrade_timeout();
45
46
		$this->installables = new Stencil_Installables();
47
48
		// Get saved information.
49
		$info = get_option( $option_name );
50
51
		if ( false !== $info ) {
52
			$this->upgrades = $info['upgrades'];
53
		}
54
55
		// Check if we need to read version info.
56
		if ( false === $info || $info['last_check_timestamp'] + $timeout < time() ) {
57
			$this->check_for_upgrades();
58
			$this->save_upgrade_information();
59
		}
60
	}
61
62
	/**
63
	 * Get transient name to use.
64
	 *
65
	 * @return string
66
	 */
67
	protected function get_option_name() {
68
		return self::TRANSIENT_NAME;
69
	}
70
71
	/**
72
	 * Get the periodically check timeout.
73
	 *
74
	 * @return int
75
	 */
76
	protected function get_upgrade_timeout() {
77
		return self::DAY_TIMESTAMP;
78
	}
79
80
	/**
81
	 * Get available upgrades.
82
	 *
83
	 * @return array
84
	 */
85
	public function get_upgrades() {
86
		return $this->upgrades;
87
	}
88
89
	/**
90
	 * Check for all upgrades
91
	 *
92
	 * @return bool
93
	 */
94
	public function check_for_upgrades() {
95
		// Don't check twice.
96
		static $checked = false;
97
98
		if ( false !== $checked ) {
99
			return false;
100
		}
101
102
		$checked = true;
103
104
		$this->upgrades = $this->installables->get_upgradable();
105
106
		return true;
107
	}
108
109
	/**
110
	 * Upgrade packages
111
	 */
112
	public function upgrade_all() {
113
		if ( empty( $this->upgrades ) ) {
114
			return;
115
		}
116
117
		foreach ( $this->upgrades as $installable ) {
118
			$installable->upgrade();
119
		}
120
	}
121
122
	/**
123
	 * Save upgrade information to the database.
124
	 */
125
	private function save_upgrade_information() {
126
		$option_name = $this->get_option_name();
127
128
		$info = array(
129
			'last_check_timestamp' => time(),
130
			'upgrades'             => $this->upgrades,
131
		);
132
133
		update_option( $option_name, $info );
134
	}
135
}
136