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 1; |
78
|
|
|
|
79
|
|
|
return self::DAY_TIMESTAMP; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get available upgrades. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function get_upgrades() { |
88
|
|
|
return $this->upgrades; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check for all upgrades |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function check_for_upgrades() { |
97
|
|
|
// Don't check twice. |
98
|
|
|
static $checked = false; |
99
|
|
|
|
100
|
|
|
if ( false !== $checked ) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$checked = true; |
105
|
|
|
|
106
|
|
|
$this->upgrades = $this->installables->get_upgradable(); |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Upgrade packages |
113
|
|
|
*/ |
114
|
|
|
public function upgrade_all() { |
115
|
|
|
if ( empty( $this->upgrades ) ) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
foreach ( $this->upgrades as $installable ) { |
120
|
|
|
$installable->upgrade(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Save upgrade information to the database. |
126
|
|
|
*/ |
127
|
|
|
private function save_upgrade_information() { |
128
|
|
|
$option_name = $this->get_option_name(); |
129
|
|
|
|
130
|
|
|
$info = array( |
131
|
|
|
'last_check_timestamp' => time(), |
132
|
|
|
'upgrades' => $this->upgrades, |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
update_option( $option_name, $info ); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.