1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract Installable |
4
|
|
|
* |
5
|
|
|
* @package Stencil |
6
|
|
|
* @subpackage Upgrader |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Stencil_Installable |
11
|
|
|
*/ |
12
|
|
|
abstract class Stencil_Abstract_Installable implements Stencil_Installable_Interface { |
13
|
|
|
/** |
14
|
|
|
* Slug of this module. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $slug; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Readable name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $name; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Version of this module. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $version = '0.0.0'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Stencil_Installable constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $slug Slug of the module. |
38
|
|
|
* @param string $name Name of this module. |
39
|
|
|
*/ |
40
|
|
|
public function __construct( $slug, $name ) { |
41
|
|
|
$this->slug = $slug; |
42
|
|
|
$this->name = $name; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get slug name. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function get_slug() { |
51
|
|
|
return $this->slug; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Is this installable installed. |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function is_installed() { |
60
|
|
|
return is_dir( $this->get_directory() ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if there is an upgrade available |
65
|
|
|
* |
66
|
|
|
* @return bool|mixed |
67
|
|
|
*/ |
68
|
|
|
public function has_upgrade() { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Do all requirements pass so it is usable. |
74
|
|
|
* |
75
|
|
|
* @return bool|array TRUE if passed, array of errors if failed. |
76
|
|
|
*/ |
77
|
|
|
public function passed_requirements() { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the name. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function __toString() { |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Remove/uninstall |
92
|
|
|
* |
93
|
|
|
* @return bool|WP_Error |
94
|
|
|
*/ |
95
|
|
|
public function remove() { |
96
|
|
|
global $wp_filesystem; |
97
|
|
|
|
98
|
|
|
$target_path = $this->get_directory(); |
99
|
|
|
|
100
|
|
|
$upgrader = $this->get_upgrader(); |
101
|
|
|
$upgrader->init(); |
102
|
|
|
|
103
|
|
|
$skin = $upgrader->skin; |
104
|
|
|
|
105
|
|
|
$skin->header(); |
106
|
|
|
|
107
|
|
|
// Connect to the Filesystem first. |
108
|
|
|
$res = $upgrader->fs_connect( array( WP_CONTENT_DIR, $target_path ) ); |
109
|
|
|
|
110
|
|
|
// Mainly for non-connected filesystem. |
111
|
|
|
if ( ! $res ) { |
112
|
|
|
$skin->footer(); |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$skin->before(); |
118
|
|
|
|
119
|
|
|
if ( is_wp_error( $res ) ) { |
120
|
|
|
$this->cancel_installer( $skin, $res ); |
121
|
|
|
|
122
|
|
|
return $res; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$deleted = $wp_filesystem->delete( $target_path, true ); |
126
|
|
|
|
127
|
|
|
$skin->after(); |
128
|
|
|
$skin->footer(); |
129
|
|
|
|
130
|
|
|
return $deleted; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Install |
135
|
|
|
* |
136
|
|
|
* @param bool $upgrading Installing or upgrading. |
137
|
|
|
* |
138
|
|
|
* @return bool|WP_Error True on succes, WP_Error on failure |
139
|
|
|
*/ |
140
|
|
|
public function install( $upgrading = false ) { |
141
|
|
|
global $wp_filesystem; |
142
|
|
|
|
143
|
|
|
$download_link = $this->get_download_link(); |
144
|
|
|
$target_path = $this->get_directory(); |
145
|
|
|
|
146
|
|
|
require ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
147
|
|
|
|
148
|
|
|
$upgrader = $this->get_upgrader(); |
149
|
|
|
$upgrader->init(); |
150
|
|
|
|
151
|
|
|
if ( ! $upgrading ) { |
152
|
|
|
$upgrader->install_strings(); |
153
|
|
|
} else { |
154
|
|
|
$upgrader->upgrade_strings(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$skin = $upgrader->skin; |
158
|
|
|
|
159
|
|
|
$skin->header(); |
160
|
|
|
|
161
|
|
|
// Connect to the Filesystem first. |
162
|
|
|
$res = $upgrader->fs_connect( array( WP_CONTENT_DIR, $target_path ) ); |
163
|
|
|
|
164
|
|
|
// Mainly for non-connected filesystem. |
165
|
|
|
if ( ! $res ) { |
166
|
|
|
$skin->footer(); |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$skin->before(); |
172
|
|
|
|
173
|
|
|
if ( is_wp_error( $res ) ) { |
174
|
|
|
$this->cancel_installer( $skin, $res ); |
175
|
|
|
|
176
|
|
|
return $res; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Download the package (Note, This just returns the filename |
181
|
|
|
* of the file if the package is a local file) |
182
|
|
|
*/ |
183
|
|
|
$download = $upgrader->download_package( $download_link ); |
184
|
|
|
if ( is_wp_error( $download ) ) { |
185
|
|
|
$this->cancel_installer( $skin, $download ); |
186
|
|
|
|
187
|
|
|
return $download; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// Unzips the file into a temporary directory. |
191
|
|
|
$working_dir = $upgrader->unpack_package( $download, true ); |
192
|
|
|
if ( is_wp_error( $working_dir ) ) { |
193
|
|
|
$this->cancel_installer( $skin, $working_dir ); |
194
|
|
|
|
195
|
|
|
return $working_dir; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$temporary_path = $target_path . '_upgrading'; |
199
|
|
|
|
200
|
|
|
if ( $upgrading ) { |
201
|
|
|
$upgrader->maintenance_mode( true ); |
202
|
|
|
|
203
|
|
|
$skin->feedback( 'remove_old' ); |
204
|
|
|
|
205
|
|
|
if ( is_dir( $temporary_path ) ) { |
206
|
|
|
$wp_filesystem->delete( $temporary_path, true ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// Move current install. |
210
|
|
|
$wp_filesystem->move( $target_path, $temporary_path ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$skin->feedback( 'installing_package' ); |
214
|
|
|
|
215
|
|
|
$installed = $wp_filesystem->move( $working_dir . DIRECTORY_SEPARATOR . $this->get_slug() . '-master', $target_path ); |
216
|
|
|
$wp_filesystem->delete( $working_dir, true ); |
217
|
|
|
|
218
|
|
|
if ( $upgrading ) { |
219
|
|
|
if ( false === $installed || is_wp_error( $installed ) ) { |
220
|
|
|
// Restore old install. |
221
|
|
|
$wp_filesystem->move( $temporary_path, $target_path ); |
222
|
|
|
|
223
|
|
|
return false; |
224
|
|
|
} else { |
225
|
|
|
// Remove old install. |
226
|
|
|
$wp_filesystem->delete( $temporary_path, true ); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$upgrader->maintenance_mode( false ); |
231
|
|
|
|
232
|
|
|
$skin->feedback( $installed ? 'process_success' : 'process_failed' ); |
233
|
|
|
|
234
|
|
|
$skin->after(); |
235
|
|
|
$skin->footer(); |
236
|
|
|
|
237
|
|
|
// Done. |
238
|
|
|
return true; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Upgrade |
243
|
|
|
* |
244
|
|
|
* @return bool |
245
|
|
|
* @throws Exception When an upgrade is already in progress for this package. |
246
|
|
|
*/ |
247
|
|
|
public function upgrade() { |
248
|
|
|
return $this->install( true ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Cancel installer. |
253
|
|
|
* |
254
|
|
|
* @param WP_Upgrader_Skin $skin Skin to set message on. |
255
|
|
|
* @param WP_Error|string $error Error to display. |
256
|
|
|
*/ |
257
|
|
|
protected function cancel_installer( $skin, $error ) { |
258
|
|
|
$skin->error( $error ); |
259
|
|
|
$skin->after(); |
260
|
|
|
$skin->footer(); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|