Initial commit: Atomaste website

This commit is contained in:
2025-12-10 12:17:30 -05:00
commit 0b9e5d1605
19260 changed files with 5206382 additions and 0 deletions

View File

@@ -0,0 +1,259 @@
<?php
/**
* LiteSpeed Cache CLI Crawler Commands
*
* Provides WP-CLI commands for managing LiteSpeed Cache crawlers.
*
* @package LiteSpeed
* @since 1.1.0
*/
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 {
/**
* Crawler instance
*
* @var Crawler2 $crawler
*/
private $crawler;
/**
* Constructor for Crawler CLI commands
*
* @since 1.1.0
*/
public function __construct() {
Debug2::debug('CLI_Crawler init');
$this->crawler = Crawler2::cls();
}
/**
* List all crawlers
*
* Displays a table of all crawlers with their details.
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # List all crawlers
* $ wp litespeed-crawler l
*
* @since 1.1.0
*/
public function l() {
$this->list();
}
/**
* List all crawlers
*
* Displays a table of all crawlers with their details.
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # List all crawlers
* $ wp litespeed-crawler list
*
* @since 1.1.0
*/
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'] <= 900;
$crawler_run_interval = defined('LITESPEED_CRAWLER_RUN_INTERVAL') ? LITESPEED_CRAWLER_RUN_INTERVAL : 600; // Specify time in seconds for the time between each run interval
if ($crawler_run_interval > 0) {
$recurrence = '';
$hours = (int) floor($crawler_run_interval / 3600);
if ($hours) {
if ($hours > 1) {
$recurrence .= sprintf(__('%d hours', 'litespeed-cache'), $hours);
} else {
$recurrence .= sprintf(__('%d hour', 'litespeed-cache'), $hours);
}
}
$minutes = (int) floor(($crawler_run_interval % 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][Crawler2::STATUS_HIT]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_HIT] : 0;
$miss = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_MISS]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_MISS] : 0;
$blacklisted = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_BLACKLIST]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_BLACKLIST] : 0;
$blacklisted += !empty($summary['crawler_stats'][$i][Crawler2::STATUS_NOCACHE]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_NOCACHE] : 0;
if (isset($summary['crawler_stats'][$i][Crawler2::STATUS_WAIT])) {
$waiting = $summary['crawler_stats'][$i][Crawler2::STATUS_WAIT] ?? 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
*
* <id>
* : The ID of the crawler to enable.
*
* ## EXAMPLES
*
* # Turn on 2nd crawler
* $ wp litespeed-crawler enable 2
*
* @since 1.1.0
* @param array $args Command arguments.
*/
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
*
* <id>
* : The ID of the crawler to disable.
*
* ## EXAMPLES
*
* # Turn off 1st crawler
* $ wp litespeed-crawler disable 1
*
* @since 1.1.0
* @param array $args Command arguments.
*/
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
*
* @since 1.1.0
*/
public function r() {
$this->run();
}
/**
* Run crawling
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # Start crawling
* $ wp litespeed-crawler run
*
* @since 1.1.0
*/
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 crawler position
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # Reset crawler position
* $ wp litespeed-crawler reset
*
* @since 1.1.0
*/
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']);
}
}

View File

@@ -0,0 +1,227 @@
<?php
/**
* LiteSpeed CLI - database cleanup
*
* Add CLI database cleanup commands.
*
* @package LiteSpeed
* @since 7.3
*/
namespace LiteSpeed\CLI;
defined('WPINC') || exit();
use LiteSpeed\Debug2;
use LiteSpeed\DB_Optm;
use WP_CLI;
/**
* LiteSpeed Cache Database CLI
*/
class Database {
/**
* Current blog id the optimization is working on.
*
* @var int|false $current_blog Current blog id.
*/
private $current_blog = false;
/**
* Database class.
*
* @var DB_Optim $db Database class.
*/
private $db;
/**
* Class constructor.
*/
public function __construct() {
Debug2::debug('CLI_Database init');
$this->db = DB_Optm::cls();
}
/**
* List all site domains and ids on the network.
*/
public function network_list() {
if ( !is_multisite() ) {
WP_CLI::error('This is not a multisite installation!');
return;
}
$buf = WP_CLI::colorize("%CThe list of installs:%n\n");
$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);
}
/**
* Change to blog sent as param.
*
* @param array $args Description.
*/
private function change_to_blog( $args ) {
if ( !isset( $args[0] ) || 'blog' !== $args[0] ) {
return;
}
$this->current_blog = get_current_blog_id();
$blogid = $args[1];
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 ( false === $site ) {
$error = WP_CLI::colorize( '%RError: invalid blog id entered.%n' );
WP_CLI::line( $error );
$this->network_list( $args );
return;
}
switch_to_blog( $blogid );
}
/**
* Change to previous blog.
*/
private function change_to_default() {
// Check if previous blog set.
if ( $this->current_blog ) {
switch_to_blog( $this->current_blog );
// Switched to previous blog.
$this->current_blog = false;
}
}
/**
* Show CLI response.
*
* @param boolean $result Flag if result is success or failure.
* @param string $action Action name.
*/
private function show_response( $result, $action ) {
if ($result) {
WP_CLI::success( $result );
} else {
WP_CLI::error( 'Error running optimization: ' . $action );
}
}
/**
* Clean actions function.
*
* @param int $args Action arguments.
* @param array $types What data to clean.
*/
private function clean_action( $args, $types ) {
$this->change_to_blog( $args );
foreach ( $types as $type ) {
$result = $this->db->handler_clean_db_cli( $type );
$this->show_response( $result, $type );
}
$this->change_to_default();
}
/**
* Clear posts data(revisions, orphaned, auto drafts, trashed posts).
* # Start clearing posts data.
* $ wp litespeed-database clear_posts
* $ wp litespeed-database clear_posts blog 2
*
* @param string $args Action arguments.
*/
public function clear_posts( $args ) {
$types = array(
'revision',
'orphaned_post_meta',
'auto_draft',
'trash_post',
);
$this->clean_action( $args, $types );
}
/**
* Clear comments(spam and trash comments).
* # Start clearing comments.
* $ wp litespeed-database clear_comments
* $ wp litespeed-database clear_comments blog 2
*
* @param string $args Action arguments.
*/
public function clear_comments( $args ) {
$types = array(
'spam_comment',
'trash_comment',
);
$this->clean_action( $args, $types );
}
/**
* Clear trackbacks/pingbacks.
* # Start clearing trackbacks/pingbacks.
* $ wp litespeed-database clear_trackbacks
* $ wp litespeed-database clear_trackbacks blog 2
*
* @param string $args Action arguments.
*/
public function clear_trackbacks( $args ) {
$types = array(
'trackback-pingback',
);
$this->clean_action( $args, $types );
}
/**
* Clear transients.
* # Start clearing transients.
* $ wp litespeed-database clear_transients
* $ wp litespeed-database clear_transients blog 2
*
* @param string $args Action arguments.
*/
public function clear_transients( $args ) {
$types = array(
'expired_transient',
'all_transients',
);
$this->clean_action( $args, $types );
}
/**
* Optimize tables.
* # Start optimizing tables.
* $ wp litespeed-database optimize_tables
* $ wp litespeed-database optimize_tables blog 2
*
* @param string $args Action arguments.
*/
public function optimize_tables( $args ) {
$types = array(
'optimize_tables',
);
$this->clean_action( $args, $types );
}
/**
* Optimize database by running all possible operations.
* # Start optimizing all.
* $ wp litespeed-database optimize_all
* $ wp litespeed-database optimize_all blog 2
*
* @param string $args Action arguments.
*/
public function optimize_all( $args ) {
$types = array(
'all',
);
$this->clean_action( $args, $types );
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Debug API CLI for LiteSpeed integration.
*
* @package LiteSpeed\CLI
*/
namespace LiteSpeed\CLI;
defined( 'WPINC' ) || exit();
use LiteSpeed\Debug2;
use LiteSpeed\Report;
use WP_CLI;
/**
* Debug API CLI
*/
class Debug {
/**
* Report instance.
*
* @var Report
*/
private $report;
/**
* Constructor for Debug CLI.
*/
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 );
}
}

View File

@@ -0,0 +1,194 @@
<?php
/**
* Image Optimization API CLI for LiteSpeed integration.
*
* @package LiteSpeed\CLI
*/
namespace LiteSpeed\CLI;
defined( 'WPINC' ) || exit();
use LiteSpeed\Lang;
use LiteSpeed\Debug2;
use LiteSpeed\Img_Optm;
use LiteSpeed\Utility;
use WP_CLI;
/**
* Image Optimization API CLI
*/
class Image {
/**
* Image optimization instance.
*
* @var Img_Optm
*/
private $img_optm;
/**
* Constructor for Image CLI.
*/
public function __construct() {
Debug2::debug( 'CLI_Cloud init' );
$this->img_optm = Img_Optm::cls();
}
/**
* Batch toggle optimized images with original images.
*
* ## OPTIONS
*
* [<type>]
* : Type to switch to (orig or optm).
*
* ## EXAMPLES
*
* # Switch to original images
* $ wp litespeed-image batch_switch orig
*
* # Switch to optimized images
* $ wp litespeed-image batch_switch optm
*
* @param array $param Positional arguments (type).
*/
public function batch_switch( $param ) {
$type = $param[0];
$this->img_optm->batch_switch( $type );
}
/**
* 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 (alias for status).
*
* ## 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', 'reduced_avif' ) as $v ) {
if ( ! empty( $summary[$v] ) ) {
$summary[$v] = Utility::real_size( $summary[$v] );
}
}
if ( ! empty( $summary['last_requested'] ) ) {
$summary['last_requested'] = gmdate( '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();
}
}

View File

@@ -0,0 +1,323 @@
<?php
/**
* QUIC.cloud API CLI for LiteSpeed integration.
*
* @package LiteSpeed\CLI
*/
namespace LiteSpeed\CLI;
defined( 'WPINC' ) || exit();
use LiteSpeed\Debug2;
use LiteSpeed\Cloud;
use WP_CLI;
/**
* QUIC.cloud API CLI
*/
class Online {
/**
* Cloud instance.
*
* @var Cloud
*/
private $cloud;
/**
* Constructor for Online CLI.
*/
public function __construct() {
Debug2::debug( 'CLI_Cloud init' );
$this->cloud = Cloud::cls();
}
/**
* Init domain on QUIC.cloud server (See https://quic.cloud/terms/)
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # Activate domain on QUIC.cloud (! Require SERVER IP setting to be set first)
* $ wp litespeed-online init
*/
public function init() {
$resp = $this->cloud->init_qc_cli();
if ( ! empty( $resp['qc_activated'] ) ) {
$main_domain = ! empty( $resp['main_domain'] ) ? $resp['main_domain'] : false;
$this->cloud->update_qc_activation( $resp['qc_activated'], $main_domain );
WP_CLI::success( 'Init successfully. Activated type: ' . $resp['qc_activated'] );
} else {
WP_CLI::error( 'Init failed!' );
}
}
/**
* Init domain CDN service on QUIC.cloud server (See https://quic.cloud/terms/)
*
* ## OPTIONS
*
* [--method=<method>]
* : The method to use (e.g., cname, ns, cfi).
*
* [--ssl-cert=<cert>]
* : Path to SSL certificate.
*
* [--ssl-key=<key>]
* : Path to SSL key.
*
* [--cf-token=<token>]
* : Cloudflare token for CFI method.
*
* [--format=<format>]
* : Output format (e.g., json).
*
* ## EXAMPLES
*
* # Activate domain CDN on QUIC.cloud (support --format=json)
* $ wp litespeed-online cdn_init --method=cname|ns
* $ wp litespeed-online cdn_init --method=cname|ns --ssl-cert=xxx.pem --ssl-key=xxx
* $ wp litespeed-online cdn_init --method=cfi --cf-token=xxxxxxxx
* $ wp litespeed-online cdn_init --method=cfi --cf-token=xxxxxxxx --ssl-cert=xxx.pem --ssl-key=xxx
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function cdn_init( $args, $assoc_args ) {
if ( empty( $assoc_args['method'] ) ) {
WP_CLI::error( 'Init CDN failed! Missing parameters `--method`.' );
return;
}
if ( ( ! empty( $assoc_args['ssl-cert'] ) && empty( $assoc_args['ssl-key'] ) ) || ( empty( $assoc_args['ssl-cert'] ) && ! empty( $assoc_args['ssl-key'] ) ) ) {
WP_CLI::error( 'Init CDN failed! SSL cert must be present together w/ SSL key.' );
return;
}
if ( 'cfi' === $assoc_args['method'] && empty( $assoc_args['cf-token'] ) ) {
WP_CLI::error( 'Init CDN failed! CFI must set `--cf-token`.' );
return;
}
$cert = ! empty( $assoc_args['ssl-cert'] ) ? $assoc_args['ssl-cert'] : '';
$key = ! empty( $assoc_args['ssl-key'] ) ? $assoc_args['ssl-key'] : '';
$cf_token = ! empty( $assoc_args['cf-token'] ) ? $assoc_args['cf-token'] : '';
$resp = $this->cloud->init_qc_cdn_cli( $assoc_args['method'], $cert, $key, $cf_token );
if ( ! empty( $resp['qc_activated'] ) ) {
$main_domain = ! empty( $resp['main_domain'] ) ? $resp['main_domain'] : false;
$this->cloud->update_qc_activation( $resp['qc_activated'], $main_domain, true );
}
if ( ! empty( $assoc_args['format'] ) && 'json' === $assoc_args['format'] ) {
WP_CLI::log( wp_json_encode( $resp ) );
return;
}
if ( ! empty( $resp['qc_activated'] ) ) {
WP_CLI::success( 'Init QC CDN successfully. Activated type: ' . $resp['qc_activated'] );
} else {
WP_CLI::error( 'Init QC CDN failed!' );
}
if ( ! empty( $resp['cname'] ) ) {
WP_CLI::success( 'cname: ' . $resp['cname'] );
}
if ( ! empty( $resp['msgs'] ) ) {
WP_CLI::success( 'msgs: ' . wp_json_encode( $resp['msgs'] ) );
}
}
/**
* Link user account by api key
*
* ## OPTIONS
*
* [--email=<email>]
* : User email for QUIC.cloud account.
*
* [--api-key=<key>]
* : API key for QUIC.cloud account.
*
* ## EXAMPLES
*
* # Link user account by api key
* $ wp litespeed-online link --email=xxx@example.com --api-key=xxxx
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function link( $args, $assoc_args ) {
if ( empty( $assoc_args['email'] ) || empty( $assoc_args['api-key'] ) ) {
WP_CLI::error( 'Link to QUIC.cloud failed! Missing parameters `--email` or `--api-key`.' );
return;
}
$resp = $this->cloud->link_qc_cli( $assoc_args['email'], $assoc_args['api-key'] );
if ( ! empty( $resp['qc_activated'] ) ) {
$main_domain = ! empty( $resp['main_domain'] ) ? $resp['main_domain'] : false;
$this->cloud->update_qc_activation( $resp['qc_activated'], $main_domain, true );
WP_CLI::success( 'Link successfully!' );
WP_CLI::log( wp_json_encode( $resp ) );
} else {
WP_CLI::error( 'Link failed!' );
}
}
/**
* Sync usage data from QUIC.cloud
*
* ## OPTIONS
*
* [--format=<format>]
* : Output format (e.g., json).
*
* ## EXAMPLES
*
* # Sync QUIC.cloud service usage info
* $ wp litespeed-online sync
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
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' ) );
}
/**
* Check QC account status
*
* ## OPTIONS
*
* ## EXAMPLES
*
* # Check QC account status
* $ wp litespeed-online cdn_status
*/
public function cdn_status() {
$resp = $this->cloud->cdn_status_cli();
WP_CLI::log( wp_json_encode( $resp ) );
}
/**
* List all QUIC.cloud services
*
* ## OPTIONS
*
* [--format=<format>]
* : Output format (e.g., json).
*
* ## EXAMPLES
*
* # List all services tag
* $ wp litespeed-online services
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
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
*
* [--format=<format>]
* : Output format (e.g., json).
*
* ## EXAMPLES
*
* # List all QUIC.cloud servers in use
* $ wp litespeed-online nodes
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
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
*
* [<service>]
* : Service to ping (e.g., img_optm).
*
* [--force]
* : Force detection of the closest server.
*
* ## EXAMPLES
*
* # Detect closest node for one service
* $ wp litespeed-online ping img_optm
* $ wp litespeed-online ping img_optm --force
*
* @param array $param Positional arguments (service).
* @param array $assoc_args Associative arguments.
*/
public function ping( $param, $assoc_args ) {
$svc = $param[0];
$force = ! empty( $assoc_args['force'] );
$json = $this->cloud->detect_cloud( $svc, $force );
if ( $json ) {
WP_CLI::success( 'Updated closest server.' );
}
WP_CLI::log( 'svc = ' . $svc );
WP_CLI::log( 'node = ' . ( $json ? $json : '-' ) );
}
}

View File

@@ -0,0 +1,418 @@
<?php
/**
* LiteSpeed Cache option Interface CLI.
*
* @package LiteSpeed\CLI
*/
namespace LiteSpeed\CLI;
defined( 'WPINC' ) || exit();
use LiteSpeed\Base;
use LiteSpeed\Admin_Settings;
use LiteSpeed\Utility;
use WP_CLI;
use WP_Filesystem;
/**
* 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'
*
* @param array $args Positional arguments (key, newvalue).
* @param array $assoc_args Associative arguments.
*/
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.json`
// For CDN/Crawler multi 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`
//
// 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 ( false !== 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 all plugin options.
*
* ## OPTIONS
*
* [--format=<format>]
* : Output format (e.g., json).
*
* ## EXAMPLES
*
* # Get all options
* $ wp litespeed-option all
* $ wp litespeed-option all --json
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
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 ( self::O_CDN_MAPPING === $k || self::O_CRAWLER_COOKIES === $k ) {
foreach ( $v as $k2 => $v2 ) {
// $k2 is numeric
if ( is_array( $v2 ) ) {
foreach ( $v2 as $k3 => $v3 ) {
// $k3 is 'url/inc_img/name/vals'
if ( is_array( $v3 ) ) {
$option_out[] = array(
'key' => '',
'value' => '',
);
foreach ( $v3 as $k4 => $v4 ) {
$option_out[] = array(
'key' => 0 === $k4 ? "{$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 ) {
$option_out[] = array(
'key' => '',
'value' => '',
);
foreach ( $v as $k2 => $v2 ) {
$option_out[] = array(
'key' => 0 === $k2 ? $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 || array() === $v ) {
$v = "''";
}
$option_out[] = array(
'key' => $k,
'value' => $v,
);
}
WP_CLI\Utils\format_items( 'table', $option_out, array( 'key', 'value' ) );
}
/**
* Get a specific plugin option.
*
* ## OPTIONS
*
* <id>
* : The option ID to retrieve (e.g., cache-priv, cdn-mapping[url][0]).
*
* ## EXAMPLES
*
* # Get one option
* $ wp litespeed-option get cache-priv
* $ wp litespeed-option get 'cdn-mapping[url][0]'
*
* @param array $args Positional arguments (id).
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$id = $args[0];
$child = false;
if ( false !== strpos( $id, '[' ) ) {
parse_str( $id, $id2 );
Utility::compatibility();
$id = array_key_first( $id2 );
$child = array_key_first( $id2[ $id ] ); // is `url`
if ( ! $child ) {
WP_CLI::error( 'Wrong child key' );
return;
}
$numeric = array_key_first( $id2[ $id ][ $child ] ); // `0`
if ( null === $numeric ) {
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 ( self::O_CDN_MAPPING === $id ) {
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 ), true ) ) {
WP_CLI::error( 'Wrong child key' );
return;
}
}
if ( self::O_CRAWLER_COOKIES === $id ) {
if ( ! in_array( $child, array( self::CRWL_COOKIE_NAME, self::CRWL_COOKIE_VALS ), true ) ) {
WP_CLI::error( 'Wrong child key' );
return;
}
}
if ( self::O_CDN_MAPPING === $id || self::O_CRAWLER_COOKIES === $id ) {
if ( ! empty( $v[ $numeric ][ $child ] ) ) {
$v = $v[ $numeric ][ $child ];
} elseif ( self::O_CDN_MAPPING === $id ) {
if ( in_array( $child, array( self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS ), true ) ) {
$v = 0;
} else {
$v = "''";
}
} else {
$v = "''";
}
}
if ( is_array( $v ) ) {
$v = implode( PHP_EOL, $v );
}
if ( ! $v && self::O_CDN_MAPPING !== $id && self::O_CRAWLER_COOKIES !== $id ) {
// 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
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function export( $args, $assoc_args ) {
if ( isset( $assoc_args['filename'] ) ) {
$file = $assoc_args['filename'];
} else {
$file = getcwd() . '/litespeed_options_' . gmdate( 'd_m_Y-His' ) . '.data';
}
global $wp_filesystem;
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
if ( ! $wp_filesystem->is_writable( dirname( $file ) ) ) {
WP_CLI::error( 'Directory not writable.' );
return;
}
$data = $this->cls( 'Import' )->export( true );
if ( false === $wp_filesystem->put_contents( $file, $data ) ) {
WP_CLI::error( 'Failed to create file.' );
return;
}
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
*
* @param array $args Positional arguments (file).
* @param array $assoc_args Associative arguments.
*/
public function import( $args, $assoc_args ) {
$file = $args[0];
global $wp_filesystem;
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
if ( ! $wp_filesystem->exists( $file ) || ! $wp_filesystem->is_readable( $file ) ) {
WP_CLI::error( 'File does not exist or is not readable.' );
return;
}
$res = $this->cls( 'Import' )->import( $file );
if ( ! $res ) {
WP_CLI::error( 'Failed to parse serialized data from file.' );
return;
}
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
*
* @param array $args Positional arguments (url).
*/
public function import_remote( $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.' );
return;
}
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();
}
}

View File

@@ -0,0 +1,106 @@
<?php
/**
* Presets CLI for LiteSpeed Cache.
*
* @package LiteSpeed\CLI
*/
namespace LiteSpeed\CLI;
defined( 'WPINC' ) || exit();
use LiteSpeed\Debug2;
use LiteSpeed\Preset;
use WP_CLI;
/**
* Presets CLI
*/
class Presets {
/**
* Preset instance.
*
* @var Preset
*/
private $preset;
/**
* Constructor for Presets CLI.
*/
public function __construct() {
Debug2::debug( 'CLI_Presets init' );
$this->preset = Preset::cls();
}
/**
* Applies a standard preset's settings.
*
* ## OPTIONS
*
* <preset>
* : The preset name to apply (e.g., basic).
*
* ## EXAMPLES
*
* # Apply the preset called "basic"
* $ wp litespeed-presets apply basic
*
* @param array $args Positional arguments (preset).
*/
public function apply( $args ) {
$preset = $args[0];
if ( empty( $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
*
* <timestamp>
* : The timestamp of the backup to restore.
*
* ## EXAMPLES
*
* # Restore the backup with the timestamp 1667485245
* $ wp litespeed-presets restore 1667485245
*
* @param array $args Positional arguments (timestamp).
*/
public function restore( $args ) {
$timestamp = $args[0];
if ( empty( $timestamp ) ) {
WP_CLI::error( 'Please specify a timestamp to restore.' );
return;
}
return $this->preset->restore( $timestamp );
}
}

View File

@@ -0,0 +1,298 @@
<?php
/**
* LiteSpeed Cache Purge Interface CLI.
*
* @package LiteSpeed\CLI
*/
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() {
if ( ! is_multisite() ) {
WP_CLI::error( 'This is not a multisite installation!' );
return;
}
$buf = WP_CLI::colorize( '%CThe list of installs:%n' ) . PHP_EOL;
$sites = get_sites();
foreach ( $sites as $site ) {
$buf .= WP_CLI::colorize( '%Y' . $site->domain . $site->path . ':%n ID ' . $site->blog_id ) . PHP_EOL;
}
WP_CLI::line( $buf );
}
/**
* Sends an AJAX request to the site.
*
* @param string $action The action to perform.
* @param array $extra Additional data to include in the request.
* @return object The HTTP response.
* @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() {
$action = is_multisite() ? Core::ACTION_QS_PURGE_EMPTYCACHE : 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
*
* @param array $args Positional arguments (blogid).
*/
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 ( false === $site ) {
$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/
*
* @param array $args Positional arguments (URL).
*/
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 ( 0 === get_blog_id_from_url( $deconstructed['host'], '/' ) ) {
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.
*
* @param array $args The ID list to parse.
* @param string $select The purge by kind.
* @param callable $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' ), true ) ? $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(
Admin_Display::PURGEBY_CAT => 'Category',
Admin_Display::PURGEBY_PID => 'Post ID',
Admin_Display::PURGEBY_TAG => 'Tag',
Admin_Display::PURGEBY_URL => '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
*
* @param array $args Positional arguments (IDs).
*/
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
*
* @param array $args Positional arguments (IDs).
*/
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
*
* @param array $args Positional arguments (IDs).
*/
public function post_id( $args ) {
$this->purgeby( $args, Admin_Display::PURGEBY_PID, 'get_post' );
}
}