-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensure-wordpress-and-theme.php
308 lines (265 loc) · 9.62 KB
/
ensure-wordpress-and-theme.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* In order to create the databases, its tables and put some content in it, the
* operator need to call this script. This script is basically a minimalistic
* "wp cli" that instanciate the WordPress framework and call the same functions
* that the installer does, but does not require to have a valid WordPress
* installation (a `wp-config.php` and `wp-includes/version.php` as the "wp cli"
* does).
*
* As it's calling WordPress code, the `ABSPATH` constant has to be correctly
* defined. It's meant to stand in the root of the wp-dev directory, but feel
* free to have it pointing to any directory containing an extract of an
* WordPress source. See the __WORDPRESS_SOURCE_DIR constant to change it.
*
* It also have to be able to connect to the database, so name equivalent to
* `DB.wordpress-test.svc` have to be accessible. It means that if you want it
* to work from outside the cluster, you have to use something like KubeVPN.
**/
require_once("./plugins/Plugin.php");
error_log(" ... Hello from wp-ops/ensure-wordpress-and-theme.php ... ");
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$shortops = "h";
$longopts = array(
"name:",
"wp-dir:",
"path:",
"title::",
"tagline::",
"theme::",
"discourage::",
"wp-host:",
"db-host:",
"db-name:",
"db-user:",
"db-password:",
"plugins:",
"unit-id:",
"languages:",
"secret-dir:",
"restored-site:",
);
$options = getopt($shortops, $longopts);
if ( key_exists("h", $options) ) {
$help = <<<EOD
Usage:
ensure-wordpress-and-theme.php --name="site-a" --path="/site-A"
Options:
--name Mandatory Identifier (as in k8s CR's name). Example: "site-a"
--wp-host Mandatory Hostname (“site_name”) for WordPress
--db-host Mandatory Hostname of the database to connect to
--db-user Mandatory Username of the database to connect to
--db-password Mandatory Password for the database to connect to
--wp-dir Mandatory The path to the WordPress installation to load.
--path Mandatory URL's path of the site. Example: "/site-A"
--title Optional Site's title (blogname). Example: "This is the site A"
Default set to --name.
--tagline Optional Site's description (tagline). Example: "A site about A and a."
--theme Optional Set the site's theme.
Default to __WORDPRESS_DEFAULT_THEME (wp-theme-2018)
--discourage Optional Set search engine visibility. 1 means discourage search
engines from indexing this site, but it is up to search
engines to honor this request.
--plugins Mandatory List of non-default plugins.
--unit-id Mandatory Plugin unit ID
--languages Mandatory List of languages
--secret-dir Mandatory Secret file's folder
--restored-site Optional `1` if it's a restored site, the default value is `0` (for new sites)
EOD;
echo $help . "\n";
exit();
}
function bad_option ($message) {
echo $message;
echo "\nUse -h to get additional help.\n";
exit(1);
}
foreach(["name", "path", "wp-dir", "wp-host",
"db-host", "db-name", "db-user", "db-password"] as $opt) {
if ( empty($options[$opt]) ) {
bad_option("\"--$opt\" is required.");
}
}
if ( empty($options["title"]) ) {
$options["title"] = $options["name"];
}
define( 'ABSPATH', $options["wp-dir"]);
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
define( 'WP_DEBUG', 1);
define( 'WP_DEBUG_DISPLAY', 1);
define( '__WORDPRESS_DOMAIN', $options["wp-host"] );
define( '__WORDPRESS_DEFAULT_THEME', 'wp-theme-2018' );
function ensure_clean_site_url($path) {
$site_url = __WORDPRESS_DOMAIN . "/{$path}/";
return preg_replace('#/+#','/', $site_url);
}
define("WP_SITEURL", "https://" . ensure_clean_site_url($options["path"]));
$_SERVER['HTTP_HOST'] = __WORDPRESS_DOMAIN;
define("DB_HOST", $options["db-host"]);
define("DB_NAME", $options["db-name"]);
define("DB_USER", $options["db-user"]);
define("DB_PASSWORD", $options["db-password"]);
define("PLUGINS", $options["plugins"] ?? "");
define("UNIT_ID", $options["unit-id"]);
define("LANGUAGES", $options["languages"]);
define("SECRETS_DIR", $options["secret-dir"]);
$options["restored-site"] = $options["restored-site"] ?? '0';
define("RESTORED_SITE", $options["restored-site"]);
global $table_prefix; $table_prefix = "wp_";
define("WP_ADMIN", true);
define("WP_INSTALLING", true);
require_once( ABSPATH . 'wp-settings.php' );
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
function ensure_db_schema () {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
if (! is_blog_installed()) {
make_db_current_silent();
}
populate_options();
populate_roles();
wp_upgrade();
}
function ensure_admin_user ($user_name, $user_email, $user_password) {
if (! username_exists($user_name)) {
wp_create_user( $user_name, $user_password, $user_email );
}
$user = new WP_User( username_exists($user_name) );
$user->set_role( 'administrator' );
update_option( 'admin_email', $user_email );
}
function get_admin_user_id () {
return 1; // wp-cli does same
}
/**
* Whatever wp_install does, that was not already done above.
*/
function ensure_other_basic_wordpress_things ( $options ) {
# Set search engine visibility. 1 means discourage search engines from
# indexing this site, but it is up to search engines to honor this request.
update_option( 'blog_public', ( empty($options["discourage"]) ? '1' : '0' ) );
update_option( 'fresh_site', 1 );
update_option( 'siteurl', wp_guess_url() );
update_option( 'permalink_structure', '/%postname%/' );
# Use a page as home page, instead of posts.
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', 2 ); // This is the sample page
wp_install_defaults( get_admin_user_id() );
wp_install_maybe_enable_pretty_permalinks();
flush_rewrite_rules();
wp_cache_flush();
}
function ensure_site_title ( $options ) {
update_option( 'blogname', stripslashes($options["title"]) );
}
function ensure_tagline ( $options ) {
if ( !empty($options["tagline"]) ) {
update_option( 'blogdescription', stripslashes($options["tagline"]) );
}
}
function ensure_theme ( $options ) {
if ( empty( $options[ 'theme' ] ) ) {
$options[ 'theme' ] = __WORDPRESS_DEFAULT_THEME;
}
global $wp_theme_directories; $wp_theme_directories = [];
require_once( ABSPATH . 'wp-includes/theme.php' );
$theme = wp_get_theme( $options[ 'theme' ] );
print( switch_theme( $theme->get_stylesheet() ) );
}
function get_plugin_list () {
$defaultPlugins = array(
"EPFL-404",
"Enlighter",
"EPFL-Accred",
"epfl-cache-control",
"epfl-coming-soon",
"EPFL-Content-Filter",
"epfl-remote-content-shortcode",
"EPFL-settings",
"EPFL-Tequila",
"ewww-image-optimizer",
"find-my-blocks",
"flowpaper",
"Polylang",
"tinymce-advanced",
"vsmd",
"wp-gutenberg-epfl",
"wp-media-folder",
);
$specificPlugin = [];
if (PLUGINS !== null and PLUGINS != '') {
$specificPlugin = explode(',', PLUGINS);
}
return array_merge($defaultPlugins, $specificPlugin);
}
function ensure_plugins () {
# This is the default plugin list that should be activated at installation
$pluginList = get_plugin_list();
$languagesList = explode(',', LANGUAGES);
foreach ($pluginList as $pluginName) {
$plugin = Plugin::create($pluginName, UNIT_ID, SECRETS_DIR, $languagesList, ABSPATH);
$activatedPlugin = activate_plugin($plugin->getPluginPath());
if ($activatedPlugin instanceof WP_Error) {
throw new ErrorException(var_dump($activatedPlugin->errors) . " - " . $plugin->getPluginPath());
}
$plugin->addSpecialConfiguration();
$plugin->updateOptions();
}
}
function delete_default_pages_and_posts () {
$pages = get_posts([
'post_type' => ['page', 'post'],
'posts_per_page' => -1, // get all
'post_status' => array_keys(get_post_statuses()), // all post statuses (publish, draft, private etc...)
]);
foreach ($pages as $page) {
wp_delete_post($page->ID, true);
}
}
// https://stackoverflow.com/a/31284266
function generate_random_password(
$keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
) {
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
if ($max < 1) {
throw new Exception('$keyspace must be at least two characters long');
}
for ($i = 0; $i < 32; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}
function ensure_plugins_for_restore () {
$pluginPath = [];
$pluginList = get_plugin_list();
$languagesList = explode(',', LANGUAGES);
foreach ($pluginList as $pluginName) {
$plugin = Plugin::create($pluginName, UNIT_ID, SECRETS_DIR, $languagesList, ABSPATH);
$pluginPath[] = $plugin->getPluginPath();
}
update_option("active_plugins", $pluginPath);
}
if (RESTORED_SITE == 1) {
ensure_plugins_for_restore();
echo "Plugins successfully configured\n";
} else {
echo "DB schema\n";
ensure_db_schema();
echo "Options and common WordPress settings\n";
ensure_other_basic_wordpress_things($options);
echo "Admin user\n";
ensure_admin_user("admin", "[email protected]", generate_random_password());
echo "Site title\n";
ensure_site_title($options);
echo "Tagline\n";
ensure_tagline($options);
echo "Theme\n";
ensure_theme($options);
echo "Delete default pages and posts\n";
delete_default_pages_and_posts();
echo "Plugins\n";
ensure_plugins();
echo "WordPress and plugins successfully installed\n";
}