1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Configure Implementations |
4
|
|
|
* |
5
|
|
|
* @package Stencil |
6
|
|
|
* @subpackage CMS |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Stencil_Config |
11
|
|
|
*/ |
12
|
|
|
class Stencil_Config { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* All installables available |
16
|
|
|
* |
17
|
|
|
* @var Stencil_Installables |
18
|
|
|
*/ |
19
|
|
|
private $installables; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Option page |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $option_page = 'stencil-options'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Option group |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $option_group = 'stencil-installables'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The name of the option |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $option_name = 'install'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Stencil_Config constructor. |
44
|
|
|
*/ |
45
|
|
|
public function __construct() { |
46
|
|
|
// Register hooks for config page. |
47
|
|
|
add_action( 'admin_menu', array( $this, 'create_admin_menu' ) ); |
48
|
|
|
add_action( 'admin_init', array( $this, 'register_options' ) ); |
49
|
|
|
add_action( 'admin_init', array( $this, 'register_installables' ) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create the menu item |
54
|
|
|
*/ |
55
|
|
|
public function create_admin_menu() { |
56
|
|
|
// Create new top-level menu. |
57
|
|
|
add_menu_page( |
58
|
|
|
__( 'Stencil implementations', 'stencil' ), |
59
|
|
|
'Stencil', |
60
|
|
|
'install_plugins', |
61
|
|
|
'stencil-installables', |
62
|
|
|
array( $this, 'settings_page' ) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Register settings |
68
|
|
|
*/ |
69
|
|
|
public function register_options() { |
70
|
|
|
|
71
|
|
|
register_setting( $this->option_group, $this->option_name ); |
72
|
|
|
|
73
|
|
|
add_settings_section( |
74
|
|
|
'stencil-installables', |
75
|
|
|
'', |
76
|
|
|
'', |
77
|
|
|
$this->option_page |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
add_settings_field( |
81
|
|
|
'plugins', |
82
|
|
|
__( 'Plugins', 'stencil' ), |
83
|
|
|
array( $this, 'option_plugins' ), |
84
|
|
|
$this->option_page, |
85
|
|
|
'stencil-installables' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
add_settings_field( |
89
|
|
|
'themes', |
90
|
|
|
__( 'Sample themes', 'stencil' ), |
91
|
|
|
array( $this, 'option_themes' ), |
92
|
|
|
$this->option_page, |
93
|
|
|
'stencil-installables' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Register installables. |
99
|
|
|
*/ |
100
|
|
|
public function register_installables() { |
101
|
|
|
$this->installables = new Stencil_Installables(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Show the settings |
106
|
|
|
*/ |
107
|
|
|
public function settings_page() { |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Show a list of known implementations |
111
|
|
|
* Mark installed ones |
112
|
|
|
* Provide checkbox to (bulk) install optional ones |
113
|
|
|
*/ |
114
|
|
|
|
115
|
|
|
print( '<div class="wrap">' ); |
116
|
|
|
printf( '<h2>%s</h2>', __( 'Stencil settings', 'stencil' ) ); |
117
|
|
|
|
118
|
|
|
$this->maybe_install_plugins(); |
119
|
|
|
$this->maybe_install_themes(); |
120
|
|
|
|
121
|
|
|
print( '<form method="post" action="options.php">' ); |
122
|
|
|
|
123
|
|
|
settings_fields( $this->option_group ); |
124
|
|
|
do_settings_sections( $this->option_page ); |
125
|
|
|
submit_button( __( 'Install selected item(s)', 'stencil' ) ); |
126
|
|
|
|
127
|
|
|
print( '</form>' ); |
128
|
|
|
|
129
|
|
|
printf( '<p><em>%s</em></p>', __( 'Note that plugins do not update because they are provided via github, not the WordPress plugin directory.', 'stencil' ) ); |
130
|
|
|
|
131
|
|
|
print( '</div>' ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Show plugins that are not installed yet (but tracked) |
136
|
|
|
* Check to install; installed plugins are grayed out and checked |
137
|
|
|
* but are ignored on save. |
138
|
|
|
*/ |
139
|
|
|
public function option_plugins() { |
140
|
|
|
$plugins = $this->installables->get_plugins(); |
141
|
|
|
foreach ( $plugins as $plugin ) { |
142
|
|
|
|
143
|
|
|
$attributes = array(); |
144
|
|
|
$available = true; |
145
|
|
|
$base = $this->option_name; |
146
|
|
|
$upgrade = $plugin->has_upgrade(); |
147
|
|
|
$exists = $plugin->is_installed(); |
148
|
|
|
$message = ''; |
149
|
|
|
|
150
|
|
|
$passed = $plugin->passed_requirements(); |
151
|
|
|
if ( is_array( $passed ) ) { |
152
|
|
|
$message = implode( '<br>', $passed ); |
153
|
|
|
$available = false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ( $available && $exists && $upgrade ) { |
157
|
|
|
$message = __( 'Upgrade available!', 'stencil' ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Check if installed. |
162
|
|
|
*/ |
163
|
|
|
if ( $exists ) { |
164
|
|
|
$attributes[] = 'checked="checked"'; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Disable input if plugin is installed. |
169
|
|
|
* Disable if not available for installation. |
170
|
|
|
*/ |
171
|
|
|
if ( ( $exists && ! $upgrade ) || ! $available ) { |
172
|
|
|
$attributes[] = 'disabled="disabled"'; |
173
|
|
|
$base = 'dummy'; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
printf( |
177
|
|
|
'<label><input type="checkbox" name="%s"%s>%s%s</label><br>', |
178
|
|
|
esc_attr( sprintf( '%s[plugin][%s]', $base, $plugin->get_slug() ) ), |
179
|
|
|
implode( ' ', $attributes ), |
180
|
|
|
esc_html( $plugin ), |
181
|
|
|
! empty( $message ) ? sprintf( ' <small><strong>(%s)</strong></small>', $message ) : '' |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Show plugins that are not installed yet (but tracked) |
188
|
|
|
* Check to install; installed plugins are grayed out and checked |
189
|
|
|
* but are ignored on save. |
190
|
|
|
*/ |
191
|
|
|
public function option_themes() { |
192
|
|
|
$themes = $this->installables->get_themes(); |
193
|
|
|
foreach ( $themes as $theme ) { |
194
|
|
|
|
195
|
|
|
$base = $this->option_name; |
196
|
|
|
$exists = $theme->is_installed(); |
197
|
|
|
$attributes = array(); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Check if installed. |
201
|
|
|
*/ |
202
|
|
|
if ( $exists ) { |
203
|
|
|
$attributes[] = 'checked="checked"'; |
204
|
|
|
$attributes[] = 'disabled="disabled"'; |
205
|
|
|
$base = 'dummy'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Disable input if plugin is installed. |
210
|
|
|
*/ |
211
|
|
|
printf( |
212
|
|
|
'<label><input type="checkbox" name="%s"%s>%s</label><br>', |
213
|
|
|
esc_attr( sprintf( '%s[theme][%s]', $base, $theme->get_slug() ) ), |
214
|
|
|
implode( ' ', $attributes ), |
215
|
|
|
esc_html( $theme ) |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Install selected plugins |
222
|
|
|
*/ |
223
|
|
|
private function maybe_install_plugins() { |
224
|
|
|
/** |
225
|
|
|
* When a plugin can be updated; the field will be check on the settings |
226
|
|
|
* When all plugins have been installed, they disappear from the list. |
227
|
|
|
*/ |
228
|
|
|
$install_plugins = get_option( $this->option_name ); |
229
|
|
|
|
230
|
|
|
if ( |
231
|
|
|
empty( $install_plugins ) || |
232
|
|
|
! isset( $install_plugins['plugin'] ) || |
233
|
|
|
! is_array( $install_plugins['plugin'] ) |
234
|
|
|
) { |
235
|
|
|
return; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
printf( '<h2>%s</h2>', __( 'Installing plugins...', 'stencil' ) ); |
239
|
|
|
|
240
|
|
|
foreach ( $install_plugins['plugin'] as $slug => $on ) { |
241
|
|
|
|
242
|
|
|
$plugin = $this->installables->get_by_slug( $slug ); |
243
|
|
|
|
244
|
|
|
if ( ! $plugin->is_installed() ) { |
245
|
|
|
$success = $plugin->install(); |
246
|
|
|
$message = __( 'Plugin %s could not be installed!', 'stencil' ); |
247
|
|
|
} else { |
248
|
|
|
$success = $plugin->upgrade(); |
249
|
|
|
$message = __( 'Plugin %s could not be upgraded!', 'stencil' ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if ( ! $success ) { |
253
|
|
|
printf( '<em>%s</em><br>', sprintf( $message, $plugin ) ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
unset( $install_plugins['plugin'][ $slug ] ); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
printf( '<b>%s</b>', __( 'Done.', 'stencil' ) ); |
260
|
|
|
|
261
|
|
|
if ( empty( $install_plugins['plugin'] ) ) { |
262
|
|
|
unset( $install_plugins['plugin'] ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
update_option( $this->option_name, $install_plugins ); |
266
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Install selected themes |
271
|
|
|
*/ |
272
|
|
|
private function maybe_install_themes() { |
273
|
|
|
/** |
274
|
|
|
* When a plugin can be updated; the field will be check on the settings |
275
|
|
|
* When all plugins have been installed, they disappear from the list. |
276
|
|
|
*/ |
277
|
|
|
$install_plugins = get_option( $this->option_name ); |
278
|
|
|
|
279
|
|
|
if ( |
280
|
|
|
empty( $install_plugins ) || |
281
|
|
|
! isset( $install_plugins['theme'] ) || |
282
|
|
|
! is_array( $install_plugins['theme'] ) |
283
|
|
|
) { |
284
|
|
|
return; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
printf( '<h2>%s</h2>', __( 'Installing themes...', 'stencil' ) ); |
288
|
|
|
|
289
|
|
|
foreach ( $install_plugins['theme'] as $slug => $on ) { |
290
|
|
|
|
291
|
|
|
$theme = $this->installables->get_by_slug( $slug ); |
292
|
|
|
|
293
|
|
|
if ( $theme->is_installed() ) { |
294
|
|
|
continue; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$installed = $theme->install(); |
298
|
|
|
|
299
|
|
|
if ( ! $installed ) { |
300
|
|
|
printf( |
301
|
|
|
'<em>%s</em><br>', |
302
|
|
|
sprintf( |
303
|
|
|
__( 'Theme %s could not be installed!', 'stencil' ), |
304
|
|
|
$theme |
305
|
|
|
) |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
unset( $install_plugins['theme'][ $slug ] ); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
printf( '<b>%s</b>', __( 'Done.', 'stencil' ) ); |
313
|
|
|
|
314
|
|
|
if ( empty( $install_plugins['theme'] ) ) { |
315
|
|
|
unset( $install_plugins['theme'] ); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
update_option( $this->option_name, $install_plugins ); |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|