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
|
|
|
* Implementations provided by us. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $known_implementations = array( |
19
|
|
|
'stencil-dwoo' => 'Dwoo', |
20
|
|
|
'stencil-dwoo2' => 'Dwoo 2', |
21
|
|
|
'stencil-mustache' => 'Mustache', |
22
|
|
|
'stencil-savant3' => 'Savant 3', |
23
|
|
|
'stencil-smarty2' => 'Smarty 2.x', |
24
|
|
|
'stencil-smarty3' => 'Smarty 3.x', |
25
|
|
|
'stencil-twig' => 'Twig', |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Implementations that require a specific minimal PHP version |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $implementation_php_requirements = array( |
34
|
|
|
'stencil-dwoo' => '5.3.0', |
35
|
|
|
'stencil-dwoo2' => '5.3.0', |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Option page |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $option_page = 'stencil-options'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Option group |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $option_group = 'stencil-implementations'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The name of the option |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $option_name = 'install'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Stencil_Config constructor. |
61
|
|
|
*/ |
62
|
|
|
public function __construct() { |
63
|
|
|
// Register hooks for config page. |
64
|
|
|
add_action( 'admin_menu', array( $this, 'create_admin_menu' ) ); |
65
|
|
|
add_action( 'admin_init', array( $this, 'register_options' ) ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create the menu item |
70
|
|
|
*/ |
71
|
|
|
public function create_admin_menu() { |
72
|
|
|
// Create new top-level menu. |
73
|
|
|
add_menu_page( |
74
|
|
|
__( 'Stencil implementations', 'stencil' ), |
75
|
|
|
'Stencil', |
76
|
|
|
'install_plugins', |
77
|
|
|
'stencil-implementations', |
78
|
|
|
array( $this, 'settings_page' ) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Register settings |
84
|
|
|
*/ |
85
|
|
|
public function register_options() { |
86
|
|
|
|
87
|
|
|
register_setting( $this->option_group, $this->option_name ); |
88
|
|
|
|
89
|
|
|
add_settings_section( |
90
|
|
|
'stencil-implementations', |
91
|
|
|
'', |
92
|
|
|
'', |
93
|
|
|
$this->option_page |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
add_settings_field( |
97
|
|
|
'implementations', |
98
|
|
|
__( 'Implementations', 'stencil' ), |
99
|
|
|
array( $this, 'option_implementations' ), |
100
|
|
|
$this->option_page, |
101
|
|
|
'stencil-implementations' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Show plugins that are not installed yet (but tracked) |
107
|
|
|
* Check to install; installed plugins are grayed out and checked |
108
|
|
|
* but are ignored on save. |
109
|
|
|
*/ |
110
|
|
|
public function option_implementations() { |
111
|
|
|
foreach ( $this->known_implementations as $slug => $name ) { |
112
|
|
|
|
113
|
|
|
$attributes = array(); |
114
|
|
|
$available = true; |
115
|
|
|
$base = $this->option_name; |
116
|
|
|
$error = ''; |
117
|
|
|
|
118
|
|
|
if ( isset( $this->implementation_php_requirements[ $slug ] ) ) { |
119
|
|
|
if ( version_compare( PHP_VERSION, $this->implementation_php_requirements[ $slug ], '<' ) ) { |
120
|
|
|
$available = false; |
121
|
|
|
$error = sprintf( __( 'PHP version %s required, %s available.' ), $this->implementation_php_requirements[ $slug ], PHP_VERSION ); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$exists = is_dir( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $slug ); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Disable input if plugin is installed. |
129
|
|
|
*/ |
130
|
|
|
if ( $exists ) { |
131
|
|
|
$attributes[] = 'checked="checked"'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ( $exists || ! $available ) { |
135
|
|
|
$attributes[] = 'disabled="disabled"'; |
136
|
|
|
$base = 'dummy'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
printf( |
140
|
|
|
'<label><input type="checkbox" name="%s"%s>%s%s</label><br>', |
141
|
|
|
esc_attr( sprintf( '%s[plugin][%s]', $base, $slug ) ), |
142
|
|
|
implode( ' ', $attributes ), |
143
|
|
|
esc_html( $name ), |
144
|
|
|
! empty( $error ) ? sprintf( ' <small>(%s)</small>', $error ) : '' |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Show the settings |
151
|
|
|
*/ |
152
|
|
|
public function settings_page() { |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Show a list of known implementations |
156
|
|
|
* Mark installed ones |
157
|
|
|
* Provide checkbox to (bulk) install optional ones |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
print( '<div class="wrap">' ); |
161
|
|
|
printf( '<h2>%s</h2>', __( 'Stencil settings', 'stencil' ) ); |
162
|
|
|
|
163
|
|
|
$this->maybe_install_plugins(); |
164
|
|
|
|
165
|
|
|
print( '<form method="post" action="options.php">' ); |
166
|
|
|
|
167
|
|
|
settings_fields( $this->option_group ); |
168
|
|
|
do_settings_sections( $this->option_page ); |
169
|
|
|
submit_button( __( 'Install selected implementation(s)', 'stencil' ) ); |
170
|
|
|
|
171
|
|
|
print( '</form>' ); |
172
|
|
|
|
173
|
|
|
printf( '<p><em>%s</em></p>', __( 'Note that plugins do not update because they are provided via github, not the WordPress plugin directory.', 'stencil' ) ); |
174
|
|
|
|
175
|
|
|
print( '</div>' ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Install selected plugins |
180
|
|
|
*/ |
181
|
|
|
public function maybe_install_plugins() { |
182
|
|
|
/** |
183
|
|
|
* When a plugin can be updated; the field will be check on the settings |
184
|
|
|
* When all plugins have been installed, they disappear from the list. |
185
|
|
|
*/ |
186
|
|
|
$install_plugins = get_option( $this->option_name ); |
187
|
|
|
|
188
|
|
|
if ( |
189
|
|
|
empty( $install_plugins ) || |
190
|
|
|
! isset( $install_plugins['plugin'] ) || |
191
|
|
|
! is_array( $install_plugins['plugin'] ) |
192
|
|
|
) { |
193
|
|
|
return; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
printf( '<h2>%s</h2>', __( 'Installing plugins...', 'stencil' ) ); |
197
|
|
|
|
198
|
|
|
foreach ( $install_plugins['plugin'] as $slug => $on ) { |
199
|
|
|
|
200
|
|
|
$installed = $this->install_plugin( $slug ); |
201
|
|
|
|
202
|
|
|
if ( ! $installed ) { |
203
|
|
|
printf( |
204
|
|
|
'<em>%s</em><br>', |
205
|
|
|
sprintf( |
206
|
|
|
__( 'Plugin %s could not be installed!', 'stencil' ), |
207
|
|
|
$this->known_implementations[ $slug ] |
208
|
|
|
) |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
unset( $install_plugins['plugin'][ $slug ] ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
printf( '<b>%s</b>', __( 'Done.', 'stencil' ) ); |
216
|
|
|
|
217
|
|
|
if ( empty( $install_plugins['plugin'] ) ) { |
218
|
|
|
unset( $install_plugins['plugin'] ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
update_option( $this->option_name, $install_plugins ); |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Install plugin by slug |
227
|
|
|
* |
228
|
|
|
* @param string $slug Plugin slug. |
229
|
|
|
* |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function install_plugin( $slug ) { |
233
|
|
|
$download_link = $this->get_download_link( $slug ); |
234
|
|
|
|
235
|
|
|
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
236
|
|
|
|
237
|
|
|
iframe_header(); |
238
|
|
|
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( array() ) ); |
239
|
|
|
$upgrader->install( $download_link ); |
240
|
|
|
iframe_footer(); |
241
|
|
|
|
242
|
|
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get the download link for the plugin |
247
|
|
|
* |
248
|
|
|
* @param string $slug Plugin slug. |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
private function get_download_link( $slug ) { |
253
|
|
|
return sprintf( 'https://github.com/moorscode/%s/archive/master.zip', $slug ); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|