defined('WPINC') || exit();
class Preset extends Import {
const TYPE_APPLY = 'apply';
const TYPE_RESTORE = 'restore';
const STANDARD_DIR = LSCWP_DIR . 'data/preset';
const BACKUP_DIR = LITESPEED_STATIC_DIR . '/auto-backup';
* Returns sorted backup names
public static function get_backups() {
return self::basename($path['name']);
$wp_filesystem->dirlist(self::BACKUP_DIR) ?: array()
* Removes extra backup files
public static function prune_backups() {
$backups = self::get_backups();
foreach (array_slice($backups, self::MAX_BACKUPS) as $backup) {
$path = self::get_backup($backup);
$wp_filesystem->delete($path);
Debug2::debug('[Preset] Deleted old backup from ' . $backup);
* Returns a settings file's extensionless basename given its filesystem path
public static function basename( $path ) {
return basename($path, '.data');
* Returns a standard preset's path given its extensionless basename
public static function get_standard( $name ) {
return path_join(self::STANDARD_DIR, $name . '.data');
* Returns a backup's path given its extensionless basename
public static function get_backup( $name ) {
return path_join(self::BACKUP_DIR, $name . '.data');
* Initializes the global $wp_filesystem object and clears stat cache
static function init_filesystem() {
require_once ABSPATH . '/wp-admin/includes/file.php';
public function __construct() {
Debug2::debug('[Preset] Init');
$this->_summary = self::get_summary();
* Applies a standard preset's settings given its extensionless basename
public function apply( $preset ) {
$this->make_backup($preset);
$path = self::get_standard($preset);
$result = $this->import_file($path) ? $preset : 'error';
* Restores settings from the backup file with the given timestamp, then deletes the file
public function restore( $timestamp ) {
foreach (self::get_backups() as $backup) {
if (preg_match('/^backup-' . $timestamp . '(-|$)/', $backup) === 1) {
$path = self::get_backup($backup);
if (!$this->import_file($path)) {
$wp_filesystem->delete($path);
Debug2::debug('[Preset] Deleted most recent backup from ' . $backup);
* Saves current settings as a backup file, then prunes extra backup files
public function make_backup( $preset ) {
$backup = 'backup-' . time() . '-before-' . $preset;
$data = $this->export(true);
$path = self::get_backup($backup);
File::save($path, $data, true);
Debug2::debug('[Preset] Backup saved to ' . $backup);
* Tries to import from a given settings file
function import_file( $path ) {
$debug = function ( $result, $name ) {
$action = $result ? 'Applied' : 'Failed to apply';
Debug2::debug('[Preset] ' . $action . ' settings from ' . $name);
$name = self::basename($path);
$contents = file_get_contents($path);
if (false === $contents) {
Debug2::debug('[Preset] ❌ Failed to get file contents');
return $debug(false, $name);
// Check if the data is v4+
if (strpos($contents, '["_version",') === 0) {
$contents = explode("\n", $contents);
foreach ($contents as $line) {
list($key, $value) = \json_decode($line, true);
$parsed = \json_decode(base64_decode($contents), true);
} catch (\Exception $ex) {
Debug2::debug('[Preset] ❌ Failed to parse serialized data');
return $debug(false, $name);
Debug2::debug('[Preset] ❌ Nothing to apply');
return $debug(false, $name);
$this->cls('Conf')->update_confs($parsed);
return $debug(true, $name);
function log( $preset ) {
$this->_summary['preset'] = $preset;
$this->_summary['preset_timestamp'] = time();
* Handles all request actions from main cls
public function handler() {
$type = Router::verify_type();
$this->apply(!empty($_GET['preset']) ? $_GET['preset'] : false);
$this->restore(!empty($_GET['timestamp']) ? $_GET['timestamp'] : false);