initial commit
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
namespace LiteSpeed\CLI;
|
||||
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Debug2;
|
||||
use LiteSpeed\Base;
|
||||
use LiteSpeed\Task;
|
||||
use LiteSpeed\Crawler as Crawler2;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* Crawler
|
||||
*/
|
||||
class Crawler extends Base
|
||||
{
|
||||
private $__crawler;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Debug2::debug('CLI_Crawler init');
|
||||
|
||||
$this->__crawler = Crawler2::cls();
|
||||
}
|
||||
|
||||
/**
|
||||
* List all crawler
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # List all crawlers
|
||||
* $ wp litespeed-crawler l
|
||||
*
|
||||
*/
|
||||
public function l()
|
||||
{
|
||||
$this->list();
|
||||
}
|
||||
|
||||
/**
|
||||
* List all crawler
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # List all crawlers
|
||||
* $ wp litespeed-crawler list
|
||||
*
|
||||
*/
|
||||
public function list()
|
||||
{
|
||||
$crawler_list = $this->__crawler->list_crawlers();
|
||||
$summary = Crawler2::get_summary();
|
||||
if ($summary['curr_crawler'] >= count($crawler_list)) {
|
||||
$summary['curr_crawler'] = 0;
|
||||
}
|
||||
$is_running = time() - $summary['is_running'] <= $this->conf(Base::O_CRAWLER_RUN_DURATION);
|
||||
|
||||
$seconds = $this->conf(Base::O_CRAWLER_RUN_INTERVAL);
|
||||
if ($seconds > 0) {
|
||||
$recurrence = '';
|
||||
$hours = (int) floor($seconds / 3600);
|
||||
if ($hours) {
|
||||
if ($hours > 1) {
|
||||
$recurrence .= sprintf(__('%d hours', 'litespeed-cache'), $hours);
|
||||
} else {
|
||||
$recurrence .= sprintf(__('%d hour', 'litespeed-cache'), $hours);
|
||||
}
|
||||
}
|
||||
$minutes = (int) floor(($seconds % 3600) / 60);
|
||||
if ($minutes) {
|
||||
$recurrence .= ' ';
|
||||
if ($minutes > 1) {
|
||||
$recurrence .= sprintf(__('%d minutes', 'litespeed-cache'), $minutes);
|
||||
} else {
|
||||
$recurrence .= sprintf(__('%d minute', 'litespeed-cache'), $minutes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$list = array();
|
||||
foreach ($crawler_list as $i => $v) {
|
||||
$hit = !empty($summary['crawler_stats'][$i]['H']) ? $summary['crawler_stats'][$i]['H'] : 0;
|
||||
$miss = !empty($summary['crawler_stats'][$i]['M']) ? $summary['crawler_stats'][$i]['M'] : 0;
|
||||
|
||||
$blacklisted = !empty($summary['crawler_stats'][$i]['B']) ? $summary['crawler_stats'][$i]['B'] : 0;
|
||||
$blacklisted += !empty($summary['crawler_stats'][$i]['N']) ? $summary['crawler_stats'][$i]['N'] : 0;
|
||||
|
||||
if (isset($summary['crawler_stats'][$i]['W'])) {
|
||||
$waiting = $summary['crawler_stats'][$i]['W'] ?: 0;
|
||||
} else {
|
||||
$waiting = $summary['list_size'] - $hit - $miss - $blacklisted;
|
||||
}
|
||||
|
||||
$analytics = 'Waiting: ' . $waiting;
|
||||
$analytics .= ' Hit: ' . $hit;
|
||||
$analytics .= ' Miss: ' . $miss;
|
||||
$analytics .= ' Blocked: ' . $blacklisted;
|
||||
|
||||
$running = '';
|
||||
if ($i == $summary['curr_crawler']) {
|
||||
$running = 'Pos: ' . ($summary['last_pos'] + 1);
|
||||
if ($is_running) {
|
||||
$running .= '(Running)';
|
||||
}
|
||||
}
|
||||
|
||||
$status = $this->__crawler->is_active($i) ? '✅' : '❌';
|
||||
|
||||
$list[] = array(
|
||||
'ID' => $i + 1,
|
||||
'Name' => wp_strip_all_tags($v['title']),
|
||||
'Frequency' => $recurrence,
|
||||
'Status' => $status,
|
||||
'Analytics' => $analytics,
|
||||
'Running' => $running,
|
||||
);
|
||||
}
|
||||
|
||||
WP_CLI\Utils\format_items('table', $list, array('ID', 'Name', 'Frequency', 'Status', 'Analytics', 'Running'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable one crawler
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Turn on 2nd crawler
|
||||
* $ wp litespeed-crawler enable 2
|
||||
*
|
||||
*/
|
||||
public function enable($args)
|
||||
{
|
||||
$id = $args[0] - 1;
|
||||
if ($this->__crawler->is_active($id)) {
|
||||
WP_CLI::error('ID #' . $id . ' had been enabled');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->__crawler->toggle_activeness($id);
|
||||
WP_CLI::success('Enabled crawler #' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable one crawler
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Turn off 1st crawler
|
||||
* $ wp litespeed-crawler disable 1
|
||||
*
|
||||
*/
|
||||
public function disable($args)
|
||||
{
|
||||
$id = $args[0] - 1;
|
||||
if (!$this->__crawler->is_active($id)) {
|
||||
WP_CLI::error('ID #' . $id . ' has been disabled');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->__crawler->toggle_activeness($id);
|
||||
WP_CLI::success('Disabled crawler #' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run crawling
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Start crawling
|
||||
* $ wp litespeed-crawler r
|
||||
*
|
||||
*/
|
||||
public function r()
|
||||
{
|
||||
$this->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run crawling
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Start crawling
|
||||
* $ wp litespeed-crawler run
|
||||
*
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
self::debug('⚠️⚠️⚠️ Forced take over lane (CLI)');
|
||||
$this->__crawler->Release_lane();
|
||||
|
||||
Task::async_call('crawler');
|
||||
|
||||
$summary = Crawler2::get_summary();
|
||||
|
||||
WP_CLI::success('Start crawling. Current crawler #' . ($summary['curr_crawler'] + 1) . ' [position] ' . $summary['last_pos'] . ' [total] ' . $summary['list_size']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset position
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Reset crawler position
|
||||
* $ wp litespeed-crawler reset
|
||||
*
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->__crawler->reset_pos();
|
||||
|
||||
$summary = Crawler2::get_summary();
|
||||
|
||||
WP_CLI::success('Reset position. Current crawler #' . ($summary['curr_crawler'] + 1) . ' [position] ' . $summary['last_pos'] . ' [total] ' . $summary['list_size']);
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace LiteSpeed\CLI;
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Debug2;
|
||||
use LiteSpeed\Report;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* Debug API CLI
|
||||
*/
|
||||
class Debug
|
||||
{
|
||||
private $__report;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Debug2::debug('CLI_Debug init');
|
||||
|
||||
$this->__report = Report::cls();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send report
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Send env report to LiteSpeed
|
||||
* $ wp litespeed-debug send
|
||||
*
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$num = $this->__report->post_env();
|
||||
WP_CLI::success('Report Number = ' . $num);
|
||||
}
|
||||
}
|
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
namespace LiteSpeed\CLI;
|
||||
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Lang;
|
||||
use LiteSpeed\Debug2;
|
||||
use LiteSpeed\Img_Optm;
|
||||
use LiteSpeed\Utility;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* Image Optm API CLI
|
||||
*/
|
||||
class Image
|
||||
{
|
||||
private $__img_optm;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Debug2::debug('CLI_Cloud init');
|
||||
|
||||
$this->__img_optm = Img_Optm::cls();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send image optimization request to QUIC.cloud server
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Send image optimization request
|
||||
* $ wp litespeed-image push
|
||||
*
|
||||
*/
|
||||
public function push()
|
||||
{
|
||||
$this->__img_optm->new_req();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull optimized images from QUIC.cloud server
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Pull images back from cloud
|
||||
* $ wp litespeed-image pull
|
||||
*
|
||||
*/
|
||||
public function pull()
|
||||
{
|
||||
$this->__img_optm->pull(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show optimization status based on local data
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Show optimization status
|
||||
* $ wp litespeed-image s
|
||||
*
|
||||
*/
|
||||
public function s()
|
||||
{
|
||||
$this->status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show optimization status based on local data
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Show optimization status
|
||||
* $ wp litespeed-image status
|
||||
*
|
||||
*/
|
||||
public function status()
|
||||
{
|
||||
$summary = Img_Optm::get_summary();
|
||||
$img_count = $this->__img_optm->img_count();
|
||||
foreach (Lang::img_status() as $k => $v) {
|
||||
if (isset($img_count["img.$k"])) {
|
||||
$img_count["$v - images"] = $img_count["img.$k"];
|
||||
unset($img_count["img.$k"]);
|
||||
}
|
||||
if (isset($img_count["group.$k"])) {
|
||||
$img_count["$v - groups"] = $img_count["group.$k"];
|
||||
unset($img_count["group.$k"]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array('reduced', 'reduced_webp') as $v) {
|
||||
if (!empty($summary[$v])) {
|
||||
$summary[$v] = Utility::real_size($summary[$v]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($summary['last_requested'])) {
|
||||
$summary['last_requested'] = date('m/d/y H:i:s', $summary['last_requested']);
|
||||
}
|
||||
|
||||
$list = array();
|
||||
foreach ($summary as $k => $v) {
|
||||
$list[] = array('key' => $k, 'value' => $v);
|
||||
}
|
||||
|
||||
$list2 = array();
|
||||
foreach ($img_count as $k => $v) {
|
||||
if (!$v) {
|
||||
continue;
|
||||
}
|
||||
$list2[] = array('key' => $k, 'value' => $v);
|
||||
}
|
||||
|
||||
WP_CLI\Utils\format_items('table', $list, array('key', 'value'));
|
||||
|
||||
WP_CLI::line(WP_CLI::colorize('%CImages in database summary:%n'));
|
||||
WP_CLI\Utils\format_items('table', $list2, array('key', 'value'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up unfinished image data from QUIC.cloud server
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Clean up unfinished requests
|
||||
* $ wp litespeed-image clean
|
||||
*
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
$this->__img_optm->clean();
|
||||
|
||||
WP_CLI::line(WP_CLI::colorize('%CLatest status:%n'));
|
||||
|
||||
$this->status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove original image backups
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Remove original image backups
|
||||
* $ wp litespeed-image rm_bkup
|
||||
*
|
||||
*/
|
||||
public function rm_bkup()
|
||||
{
|
||||
$this->__img_optm->rm_bkup();
|
||||
}
|
||||
}
|
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
namespace LiteSpeed\CLI;
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Debug2;
|
||||
use LiteSpeed\Cloud;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* QUIC.cloud API CLI
|
||||
*/
|
||||
class Online
|
||||
{
|
||||
private $__cloud;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Debug2::debug('CLI_Cloud init');
|
||||
|
||||
$this->__cloud = Cloud::cls();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate domain key from QUIC.cloud server (See https://quic.cloud/terms/)
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Generate domain API key from QUIC.cloud
|
||||
* $ wp litespeed-online init
|
||||
*
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$key = $this->__cloud->gen_key();
|
||||
if ($key) {
|
||||
WP_CLI::success('key = ' . $key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync usage data from QUIC.cloud
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Sync QUIC.cloud service usage info
|
||||
* $ wp litespeed-online sync
|
||||
*
|
||||
*/
|
||||
public function sync($args, $assoc_args)
|
||||
{
|
||||
$json = $this->__cloud->sync_usage();
|
||||
|
||||
if (!empty($assoc_args['format'])) {
|
||||
WP_CLI::print_value($json, $assoc_args);
|
||||
return;
|
||||
}
|
||||
|
||||
WP_CLI::success('Sync successfully');
|
||||
|
||||
$list = array();
|
||||
foreach (Cloud::$SERVICES as $v) {
|
||||
$list[] = array(
|
||||
'key' => $v,
|
||||
'used' => !empty($json['usage.' . $v]['used']) ? $json['usage.' . $v]['used'] : 0,
|
||||
'quota' => !empty($json['usage.' . $v]['quota']) ? $json['usage.' . $v]['quota'] : 0,
|
||||
'PayAsYouGo_Used' => !empty($json['usage.' . $v]['pag_used']) ? $json['usage.' . $v]['pag_used'] : 0,
|
||||
'PayAsYouGo_Balance' => !empty($json['usage.' . $v]['pag_bal']) ? $json['usage.' . $v]['pag_bal'] : 0,
|
||||
);
|
||||
}
|
||||
|
||||
WP_CLI\Utils\format_items('table', $list, array('key', 'used', 'quota', 'PayAsYouGo_Used', 'PayAsYouGo_Balance'));
|
||||
}
|
||||
|
||||
/**
|
||||
* List all QUIC.cloud services
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # List all services tag
|
||||
* $ wp litespeed-online services
|
||||
*
|
||||
*/
|
||||
public function services($args, $assoc_args)
|
||||
{
|
||||
if (!empty($assoc_args['format'])) {
|
||||
WP_CLI::print_value(Cloud::$SERVICES, $assoc_args);
|
||||
return;
|
||||
}
|
||||
|
||||
$list = array();
|
||||
foreach (Cloud::$SERVICES as $v) {
|
||||
$list[] = array(
|
||||
'service' => $v,
|
||||
);
|
||||
}
|
||||
|
||||
WP_CLI\Utils\format_items('table', $list, array('service'));
|
||||
}
|
||||
|
||||
/**
|
||||
* List all QUIC.cloud servers in use
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # List all QUIC.cloud servers in use
|
||||
* $ wp litespeed-online nodes
|
||||
*
|
||||
*/
|
||||
public function nodes($args, $assoc_args)
|
||||
{
|
||||
$json = Cloud::get_summary();
|
||||
|
||||
$list = array();
|
||||
$json_output = array();
|
||||
foreach (Cloud::$SERVICES as $v) {
|
||||
$server = !empty($json['server.' . $v]) ? $json['server.' . $v] : '';
|
||||
$list[] = array(
|
||||
'service' => $v,
|
||||
'server' => $server,
|
||||
);
|
||||
$json_output[] = array($v => $server);
|
||||
}
|
||||
|
||||
if (!empty($assoc_args['format'])) {
|
||||
WP_CLI::print_value($json_output, $assoc_args);
|
||||
return;
|
||||
}
|
||||
|
||||
WP_CLI\Utils\format_items('table', $list, array('service', 'server'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect closest node server for current service
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Detect closest node for one service
|
||||
* $ wp litespeed-online ping img_optm
|
||||
*
|
||||
*/
|
||||
public function ping($param)
|
||||
{
|
||||
$svc = $param[0];
|
||||
$json = $this->__cloud->detect_cloud($svc);
|
||||
WP_CLI::success('Updated closest server.');
|
||||
WP_CLI::log('svc = ' . $svc);
|
||||
WP_CLI::log('node = ' . $json);
|
||||
}
|
||||
}
|
@@ -0,0 +1,372 @@
|
||||
<?php
|
||||
namespace LiteSpeed\CLI;
|
||||
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Base;
|
||||
use LiteSpeed\Admin_Settings;
|
||||
use LiteSpeed\Utility;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* LiteSpeed Cache option Interface
|
||||
*/
|
||||
class Option extends Base
|
||||
{
|
||||
/**
|
||||
* Set an individual LiteSpeed Cache option.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <key>
|
||||
* : The option key to update.
|
||||
*
|
||||
* <newvalue>
|
||||
* : The new value to set the option to.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Set to not cache the login page
|
||||
* $ wp litespeed-option set cache-priv false
|
||||
* $ wp litespeed-option set 'cdn-mapping[url][0]' https://cdn.EXAMPLE.com
|
||||
* $ wp litespeed-option set media-lqip_exc $'line1\nline2'
|
||||
*
|
||||
*/
|
||||
public function set($args, $assoc_args)
|
||||
{
|
||||
/**
|
||||
* Note: If the value is multiple dimensions like cdn-mapping, need to specially handle it both here and in `const.default.ini`
|
||||
*
|
||||
* For CDN/Crawler mutlti dimension settings, if all children are empty in one line, will delete that line. To delete one line, just set all to empty.
|
||||
* E.g. to delete cdn-mapping[0], need to run below:
|
||||
* `set cdn-mapping[url][0] ''`
|
||||
* `set cdn-mapping[inc_img][0] ''`
|
||||
* `set cdn-mapping[inc_css][0] ''`
|
||||
* `set cdn-mapping[inc_js][0] ''`
|
||||
* `set cdn-mapping[filetype][0] ''`
|
||||
*/
|
||||
$key = $args[0];
|
||||
$val = $args[1];
|
||||
|
||||
/**
|
||||
* For CDN mapping, allow:
|
||||
* `set 'cdn-mapping[url][0]' https://the1st_cdn_url`
|
||||
* `set 'cdn-mapping[inc_img][0]' true`
|
||||
* `set 'cdn-mapping[inc_img][0]' 1`
|
||||
* @since 2.7.1
|
||||
*
|
||||
* For Crawler cookies:
|
||||
* `set 'crawler-cookies[name][0]' my_currency`
|
||||
* `set 'crawler-cookies[vals][0]' "USD\nTWD"`
|
||||
*
|
||||
* For multi lines setting:
|
||||
* `set media-lqip_exc $'img1.jpg\nimg2.jpg'`
|
||||
*/
|
||||
|
||||
// Build raw data
|
||||
$raw_data = array(
|
||||
Admin_Settings::ENROLL => array($key),
|
||||
);
|
||||
|
||||
// Contains child set
|
||||
if (strpos($key, '[')) {
|
||||
parse_str($key . '=' . $val, $key2);
|
||||
$raw_data = array_merge($raw_data, $key2);
|
||||
} else {
|
||||
$raw_data[$key] = $val;
|
||||
}
|
||||
|
||||
$this->cls('Admin_Settings')->save($raw_data);
|
||||
WP_CLI::line("$key:");
|
||||
$this->get($args, $assoc_args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin options.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Get all options
|
||||
* $ wp litespeed-option all
|
||||
* $ wp litespeed-option all --json
|
||||
*
|
||||
*/
|
||||
public function all($args, $assoc_args)
|
||||
{
|
||||
$options = $this->get_options();
|
||||
|
||||
if (!empty($assoc_args['format'])) {
|
||||
WP_CLI::print_value($options, $assoc_args);
|
||||
return;
|
||||
}
|
||||
|
||||
$option_out = array();
|
||||
|
||||
$buf = WP_CLI::colorize('%CThe list of options:%n');
|
||||
WP_CLI::line($buf);
|
||||
|
||||
foreach ($options as $k => $v) {
|
||||
if ($k == self::O_CDN_MAPPING || $k == self::O_CRAWLER_COOKIES) {
|
||||
foreach ($v as $k2 => $v2) {
|
||||
// $k2 is numeric
|
||||
if (is_array($v2)) {
|
||||
foreach ($v2 as $k3 => $v3) {
|
||||
// $k3 = 'url/inc_img/name/vals'
|
||||
if (is_array($v3)) {
|
||||
$option_out[] = array('key' => '', 'value' => '');
|
||||
foreach ($v3 as $k4 => $v4) {
|
||||
$option_out[] = array('key' => $k4 == 0 ? "{$k}[$k3][$k2]" : '', 'value' => $v4);
|
||||
}
|
||||
$option_out[] = array('key' => '', 'value' => '');
|
||||
} else {
|
||||
$option_out[] = array('key' => "{$k}[$k3][$k2]", 'value' => $v3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} elseif (is_array($v) && $v) {
|
||||
// $v = implode( PHP_EOL, $v );
|
||||
$option_out[] = array('key' => '', 'value' => '');
|
||||
foreach ($v as $k2 => $v2) {
|
||||
$option_out[] = array('key' => $k2 == 0 ? $k : '', 'value' => $v2);
|
||||
}
|
||||
$option_out[] = array('key' => '', 'value' => '');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (array_key_exists($k, self::$_default_options) && is_bool(self::$_default_options[$k]) && !$v) {
|
||||
$v = 0;
|
||||
}
|
||||
|
||||
if ($v === '' || $v === array()) {
|
||||
$v = "''";
|
||||
}
|
||||
|
||||
$option_out[] = array('key' => $k, 'value' => $v);
|
||||
}
|
||||
|
||||
WP_CLI\Utils\format_items('table', $option_out, array('key', 'value'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin options.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Get one option
|
||||
* $ wp litespeed-option get cache-priv
|
||||
* $ wp litespeed-option get 'cdn-mapping[url][0]'
|
||||
*
|
||||
*/
|
||||
public function get($args, $assoc_args)
|
||||
{
|
||||
$id = $args[0];
|
||||
|
||||
$child = false;
|
||||
if (strpos($id, '[')) {
|
||||
parse_str($id, $id2);
|
||||
Utility::compatibility();
|
||||
$id = array_key_first($id2);
|
||||
|
||||
$child = array_key_first($id2[$id]); // `url`
|
||||
if (!$child) {
|
||||
WP_CLI::error('Wrong child key');
|
||||
return;
|
||||
}
|
||||
$numeric = array_key_first($id2[$id][$child]); // `0`
|
||||
if ($numeric === null) {
|
||||
WP_CLI::error('Wrong 2nd level numeric key');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset(self::$_default_options[$id])) {
|
||||
WP_CLI::error('ID not exist [id] ' . $id);
|
||||
return;
|
||||
}
|
||||
|
||||
$v = $this->conf($id);
|
||||
$default_v = self::$_default_options[$id];
|
||||
|
||||
/**
|
||||
* For CDN_mapping and crawler_cookies
|
||||
* Examples of option name:
|
||||
* cdn-mapping[url][0]
|
||||
* crawler-cookies[name][1]
|
||||
*/
|
||||
if ($id == self::O_CDN_MAPPING) {
|
||||
if (!in_array($child, array(self::CDN_MAPPING_URL, self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS, self::CDN_MAPPING_FILETYPE))) {
|
||||
WP_CLI::error('Wrong child key');
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ($id == self::O_CRAWLER_COOKIES) {
|
||||
if (!in_array($child, array(self::CRWL_COOKIE_NAME, self::CRWL_COOKIE_VALS))) {
|
||||
WP_CLI::error('Wrong child key');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ($id == self::O_CDN_MAPPING || $id == self::O_CRAWLER_COOKIES) {
|
||||
if (!empty($v[$numeric][$child])) {
|
||||
$v = $v[$numeric][$child];
|
||||
} else {
|
||||
if ($id == self::O_CDN_MAPPING) {
|
||||
if (in_array($child, array(self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS))) {
|
||||
$v = 0;
|
||||
} else {
|
||||
$v = "''";
|
||||
}
|
||||
} else {
|
||||
$v = "''";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($v)) {
|
||||
$v = implode(PHP_EOL, $v);
|
||||
}
|
||||
|
||||
if (!$v && $id != self::O_CDN_MAPPING && $id != self::O_CRAWLER_COOKIES) {
|
||||
// empty array for CDN/crawler has been handled
|
||||
if (is_bool($default_v)) {
|
||||
$v = 0;
|
||||
} elseif (!is_array($default_v)) {
|
||||
$v = "''";
|
||||
}
|
||||
}
|
||||
|
||||
WP_CLI::line($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export plugin options to a file.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* [--filename=<path>]
|
||||
* : The default path used is CURRENTDIR/lscache_wp_options_DATE-TIME.txt.
|
||||
* To select a different file, use this option.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Export options to a file.
|
||||
* $ wp litespeed-option export
|
||||
*
|
||||
*/
|
||||
public function export($args, $assoc_args)
|
||||
{
|
||||
if (isset($assoc_args['filename'])) {
|
||||
$file = $assoc_args['filename'];
|
||||
} else {
|
||||
$file = getcwd() . '/litespeed_options_' . date('d_m_Y-His') . '.data';
|
||||
}
|
||||
|
||||
if (!is_writable(dirname($file))) {
|
||||
WP_CLI::error('Directory not writable.');
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $this->cls('Import')->export(true);
|
||||
|
||||
if (file_put_contents($file, $data) === false) {
|
||||
WP_CLI::error('Failed to create file.');
|
||||
} else {
|
||||
WP_CLI::success('Created file ' . $file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Import plugin options from a file.
|
||||
*
|
||||
* The file must be formatted as such:
|
||||
* option_key=option_value
|
||||
* One per line.
|
||||
* A Semicolon at the beginning of the line indicates a comment and will be skipped.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <file>
|
||||
* : The file to import options from.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Import options from CURRENTDIR/options.txt
|
||||
* $ wp litespeed-option import options.txt
|
||||
*
|
||||
*/
|
||||
public function import($args, $assoc_args)
|
||||
{
|
||||
$file = $args[0];
|
||||
if (!file_exists($file) || !is_readable($file)) {
|
||||
WP_CLI::error('File does not exist or is not readable.');
|
||||
}
|
||||
|
||||
$res = $this->cls('Import')->import($file);
|
||||
|
||||
if (!$res) {
|
||||
WP_CLI::error('Failed to parse serialized data from file.');
|
||||
}
|
||||
|
||||
WP_CLI::success('Options imported. [File] ' . $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import plugin options from a remote file.
|
||||
*
|
||||
* The file must be formatted as such:
|
||||
* option_key=option_value
|
||||
* One per line.
|
||||
* A Semicolon at the beginning of the line indicates a comment and will be skipped.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <url>
|
||||
* : The URL to import options from.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Import options from https://domain.com/options.txt
|
||||
* $ wp litespeed-option import_remote https://domain.com/options.txt
|
||||
*
|
||||
*/
|
||||
|
||||
public function import_remote($args, $assoc_args)
|
||||
{
|
||||
$file = $args[0];
|
||||
|
||||
$tmp_file = download_url($file);
|
||||
|
||||
if (is_wp_error($tmp_file)) {
|
||||
WP_CLI::error('Failed to download file.');
|
||||
return;
|
||||
}
|
||||
|
||||
$res = $this->cls('Import')->import($tmp_file);
|
||||
|
||||
if (!$res) {
|
||||
WP_CLI::error('Failed to parse serialized data from file.');
|
||||
}
|
||||
|
||||
WP_CLI::success('Options imported. [File] ' . $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all options to default.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Reset all options
|
||||
* $ wp litespeed-option reset
|
||||
*
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->cls('Import')->reset();
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
namespace LiteSpeed\CLI;
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Debug2;
|
||||
use LiteSpeed\Preset;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* Presets CLI
|
||||
*/
|
||||
|
||||
class Presets
|
||||
{
|
||||
private $__preset;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Debug2::debug('CLI_Presets init');
|
||||
|
||||
$this->__preset = Preset::cls();
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a standard preset's settings.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Apply the preset called "basic"
|
||||
* $ wp litespeed-presets apply basic
|
||||
*
|
||||
*/
|
||||
|
||||
public function apply($args)
|
||||
{
|
||||
$preset = $args[0];
|
||||
|
||||
if (!isset($preset)) {
|
||||
WP_CLI::error('Please specify a preset to apply.');
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->__preset->apply($preset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sorted backup names.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Get all backups
|
||||
* $ wp litespeed-presets get_backups
|
||||
*
|
||||
*/
|
||||
|
||||
public function get_backups()
|
||||
{
|
||||
$backups = $this->__preset->get_backups();
|
||||
|
||||
foreach ($backups as $backup) {
|
||||
WP_CLI::line($backup);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores settings from the backup file with the given timestamp, then deletes the file.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Restore the backup with the timestamp 1667485245
|
||||
* $ wp litespeed-presets restore 1667485245
|
||||
*
|
||||
*/
|
||||
|
||||
public function restore($args)
|
||||
{
|
||||
$timestamp = $args[0];
|
||||
|
||||
if (!isset($timestamp)) {
|
||||
WP_CLI::error('Please specify a timestamp to restore.');
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->__preset->restore($timestamp);
|
||||
}
|
||||
}
|
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
namespace LiteSpeed\CLI;
|
||||
|
||||
defined('WPINC') || exit();
|
||||
|
||||
use LiteSpeed\Core;
|
||||
use LiteSpeed\Router;
|
||||
use LiteSpeed\Admin_Display;
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* LiteSpeed Cache Purge Interface
|
||||
*/
|
||||
class Purge
|
||||
{
|
||||
/**
|
||||
* List all site domains and ids on the network.
|
||||
*
|
||||
* For use with the blog subcommand.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # List all the site domains and ids in a table.
|
||||
* $ wp litespeed-purge network_list
|
||||
*/
|
||||
public function network_list($args)
|
||||
{
|
||||
if (!is_multisite()) {
|
||||
WP_CLI::error('This is not a multisite installation!');
|
||||
|
||||
return;
|
||||
}
|
||||
$buf = WP_CLI::colorize("%CThe list of installs:%n\n");
|
||||
|
||||
if (version_compare($GLOBALS['wp_version'], '4.6', '<')) {
|
||||
$sites = wp_get_sites();
|
||||
foreach ($sites as $site) {
|
||||
$buf .= WP_CLI::colorize('%Y' . $site['domain'] . $site['path'] . ':%n ID ' . $site['blog_id']) . "\n";
|
||||
}
|
||||
} else {
|
||||
$sites = get_sites();
|
||||
foreach ($sites as $site) {
|
||||
$buf .= WP_CLI::colorize('%Y' . $site->domain . $site->path . ':%n ID ' . $site->blog_id) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
WP_CLI::line($buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an ajax request to the site. Takes an action and the nonce string to perform.
|
||||
*
|
||||
* @since 1.0.14
|
||||
*/
|
||||
private function _send_request($action, $extra = array())
|
||||
{
|
||||
$data = array(
|
||||
Router::ACTION => $action,
|
||||
Router::NONCE => wp_create_nonce($action),
|
||||
);
|
||||
if (!empty($extra)) {
|
||||
$data = array_merge($data, $extra);
|
||||
}
|
||||
|
||||
$url = admin_url('admin-ajax.php');
|
||||
WP_CLI::debug('URL is ' . $url);
|
||||
|
||||
$out = WP_CLI\Utils\http_request('GET', $url, $data);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges all cache entries for the blog (the entire network if multisite).
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Purge Everything associated with the WordPress install.
|
||||
* $ wp litespeed-purge all
|
||||
*
|
||||
*/
|
||||
public function all($args)
|
||||
{
|
||||
if (is_multisite()) {
|
||||
$action = Core::ACTION_QS_PURGE_EMPTYCACHE;
|
||||
} else {
|
||||
$action = Core::ACTION_QS_PURGE_ALL;
|
||||
}
|
||||
|
||||
$purge_ret = $this->_send_request($action);
|
||||
|
||||
if ($purge_ret->success) {
|
||||
WP_CLI::success(__('Purged All!', 'litespeed-cache'));
|
||||
} else {
|
||||
WP_CLI::error('Something went wrong! Got ' . $purge_ret->status_code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges all cache entries for the blog.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <blogid>
|
||||
* : The blog id to purge
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # In a multisite install, purge only the shop.example.com cache (stored as blog id 2).
|
||||
* $ wp litespeed-purge blog 2
|
||||
*
|
||||
*/
|
||||
public function blog($args)
|
||||
{
|
||||
if (!is_multisite()) {
|
||||
WP_CLI::error('Not a multisite installation.');
|
||||
return;
|
||||
}
|
||||
$blogid = $args[0];
|
||||
if (!is_numeric($blogid)) {
|
||||
$error = WP_CLI::colorize('%RError: invalid blog id entered.%n');
|
||||
WP_CLI::line($error);
|
||||
$this->network_list($args);
|
||||
return;
|
||||
}
|
||||
$site = get_blog_details($blogid);
|
||||
if ($site === false) {
|
||||
$error = WP_CLI::colorize('%RError: invalid blog id entered.%n');
|
||||
WP_CLI::line($error);
|
||||
$this->network_list($args);
|
||||
return;
|
||||
}
|
||||
switch_to_blog($blogid);
|
||||
|
||||
$purge_ret = $this->_send_request(Core::ACTION_QS_PURGE_ALL);
|
||||
if ($purge_ret->success) {
|
||||
WP_CLI::success(__('Purged the blog!', 'litespeed-cache'));
|
||||
} else {
|
||||
WP_CLI::error('Something went wrong! Got ' . $purge_ret->status_code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges all cache tags related to a url.
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <url>
|
||||
* : The url to purge.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Purge the front page.
|
||||
* $ wp litespeed-purge url https://mysite.com/
|
||||
*
|
||||
*/
|
||||
public function url($args)
|
||||
{
|
||||
$data = array(
|
||||
Router::ACTION => Core::ACTION_QS_PURGE,
|
||||
);
|
||||
$url = $args[0];
|
||||
$deconstructed = wp_parse_url($url);
|
||||
if (empty($deconstructed)) {
|
||||
WP_CLI::error('url passed in is invalid.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_multisite()) {
|
||||
if (get_blog_id_from_url($deconstructed['host'], '/') === 0) {
|
||||
WP_CLI::error('Multisite url passed in is invalid.');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$deconstructed_site = wp_parse_url(get_home_url());
|
||||
if ($deconstructed['host'] !== $deconstructed_site['host']) {
|
||||
WP_CLI::error('Single site url passed in is invalid.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
WP_CLI::debug('url is ' . $url);
|
||||
|
||||
$purge_ret = WP_CLI\Utils\http_request('GET', $url, $data);
|
||||
if ($purge_ret->success) {
|
||||
WP_CLI::success(__('Purged the url!', 'litespeed-cache'));
|
||||
} else {
|
||||
WP_CLI::error('Something went wrong! Got ' . $purge_ret->status_code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for purging by ids.
|
||||
*
|
||||
* @access private
|
||||
* @since 1.0.15
|
||||
* @param array $args The id list to parse.
|
||||
* @param string $select The purge by kind
|
||||
* @param function(int $id) $callback The callback function to check the id.
|
||||
*/
|
||||
private function _purgeby($args, $select, $callback)
|
||||
{
|
||||
$filtered = array();
|
||||
foreach ($args as $val) {
|
||||
if (!ctype_digit($val)) {
|
||||
WP_CLI::debug('[LSCACHE] Skip val, not a number. ' . $val);
|
||||
continue;
|
||||
}
|
||||
$term = $callback($val);
|
||||
if (!empty($term)) {
|
||||
WP_CLI::line($term->name);
|
||||
$filtered[] = in_array($callback, array('get_tag', 'get_category')) ? $term->name : $val;
|
||||
} else {
|
||||
WP_CLI::debug('[LSCACHE] Skip val, not a valid term. ' . $val);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($filtered)) {
|
||||
WP_CLI::error('Arguments must be integer ids.');
|
||||
return;
|
||||
}
|
||||
|
||||
$str = implode(',', $filtered);
|
||||
|
||||
$purge_titles = array(
|
||||
0 => 'Category',
|
||||
1 => 'Post ID',
|
||||
2 => 'Tag',
|
||||
3 => 'URL',
|
||||
);
|
||||
|
||||
WP_CLI::line('Will purge the following: [' . $purge_titles[$select] . '] ' . $str);
|
||||
|
||||
$data = array(
|
||||
Admin_Display::PURGEBYOPT_SELECT => $select,
|
||||
Admin_Display::PURGEBYOPT_LIST => $str,
|
||||
);
|
||||
|
||||
$purge_ret = $this->_send_request(Core::ACTION_PURGE_BY, $data);
|
||||
if ($purge_ret->success) {
|
||||
WP_CLI::success(__('Purged!', 'litespeed-cache'));
|
||||
} else {
|
||||
WP_CLI::error('Something went wrong! Got ' . $purge_ret->status_code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges cache tags for a WordPress tag
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <ids>...
|
||||
* : the Term IDs to purge.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Purge the tag ids 1, 3, and 5
|
||||
* $ wp litespeed-purge tag 1 3 5
|
||||
*
|
||||
*/
|
||||
public function tag($args)
|
||||
{
|
||||
$this->_purgeby($args, Admin_Display::PURGEBY_TAG, 'get_tag');
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges cache tags for a WordPress category
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <ids>...
|
||||
* : the Term IDs to purge.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Purge the category ids 1, 3, and 5
|
||||
* $ wp litespeed-purge category 1 3 5
|
||||
*
|
||||
*/
|
||||
public function category($args)
|
||||
{
|
||||
$this->_purgeby($args, Admin_Display::PURGEBY_CAT, 'get_category');
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges cache tags for a WordPress Post/Product
|
||||
*
|
||||
* @alias product
|
||||
*
|
||||
* ## OPTIONS
|
||||
*
|
||||
* <ids>...
|
||||
* : the Post IDs to purge.
|
||||
*
|
||||
* ## EXAMPLES
|
||||
*
|
||||
* # Purge the post ids 1, 3, and 5
|
||||
* $ wp litespeed-purge post_id 1 3 5
|
||||
*
|
||||
*/
|
||||
public function post_id($args)
|
||||
{
|
||||
$this->_purgeby($args, Admin_Display::PURGEBY_PID, 'get_post');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user