-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwp-cli.php
55 lines (46 loc) · 2.13 KB
/
wp-cli.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
<?php
if (!defined('ABSPATH')) {
die();
}
if (!defined('WP_CLI')) return;
class WP_CLI_WP_Hestia_Nginx_Cache extends WP_CLI_Command {
public function __construct(){
$this -> plugin = Hestia_Nginx_Cache::get_instance();
}
public function purge(){
$result = $this -> plugin -> purge(true);
if ($result) {
$exit_code = wp_remote_retrieve_header($result, 'Hestia-Exit-Code');
}
if($exit_code == 0){
WP_CLI::success('Cache purged');
} else {
WP_CLI::error('Cache purge failed');
}
}
public function setup($args, $assoc_args){
if(empty($assoc_args)){
WP_CLI::error('Usage wp hestia-cache setup --access_key=YOUR_ACCESS_KEY --secret=YOUR_SECRET_KEY --hestia_user=HESTIA_USER --domain=DOMAIN --host=HOST --port=PORT --disable_automatic_purge=true');
}
$access_key = !empty($assoc_args['access_key']) ? $assoc_args['access_key'] : '';
$secret_key = !empty($assoc_args['secret_key']) ? $assoc_args['secret_key'] : '';
//hestia user / owner of domain
//wordpress doesn't like the word 'user' for input arguments so we use 'hestia_user' instead
$user = !empty($assoc_args['hestia_user']) ? $assoc_args['hestia_user'] : '';
//domain to purge
$domain = !empty($assoc_args['domain']) ? $assoc_args['domain'] : str_replace(array('https://','http://'), '', get_bloginfo('wpurl'));
//server hostname
$host = !empty($assoc_args['host']) ? $assoc_args['host'] : 'localhost';
$port = !empty($assoc_args['port']) ? $assoc_args['port'] : 8083;
$disable_automatic_purge = !empty($assoc_args['disable_automatic_purge']) ? $assoc_args['disable_automatic_purge'] : '';
if(empty($access_key) || empty($secret_key) || empty($user)){
WP_CLI::error('Please provide access_key, secret_key and hestia_user');
}
//validate
$port = intval($port);
$domain = parse_url($domain, PHP_URL_HOST) ?: $domain;
$options = array('access_key' => $access_key, 'secret_key' => $secret_key, 'user' => $user, 'domain' => $domain, 'host' => $host, 'port' => $port, 'disable_automatic_purge' => $disable_automatic_purge);
update_option(Hestia_Nginx_Cache::NAME, $options);
}
}
WP_CLI::add_command('hestia-cache', 'WP_CLI_WP_Hestia_Nginx_Cache');