| 1 |
<?php |
| 2 |
|
| 3 |
namespace Upress\EzCache\Rest; |
| 4 |
|
| 5 |
use Upress\EzCache\Settings; |
| 6 |
use Upress\EzCache\PremiumFeatures; |
| 7 |
|
| 8 |
class SettingsController { |
| 9 |
|
| 10 |
function show() { |
| 11 |
$settings = Settings::get_settings(); |
| 12 |
|
| 13 |
return [ |
| 14 |
'success' => true, |
| 15 |
'data' => $settings, |
| 16 |
]; |
| 17 |
} |
| 18 |
|
| 19 |
function update( $request ) { |
| 20 |
$json = $request->get_json_params(); |
| 21 |
$updated_settings = []; |
| 22 |
foreach ( $json as $key => $value ) { |
| 23 |
$updated_settings[ $key ] = $value; |
| 24 |
} |
| 25 |
|
| 26 |
// Strip premium features for free users |
| 27 |
if ( ! PremiumFeatures::is_premium() ) { |
| 28 |
$premium = PremiumFeatures::get_premium_features(); |
| 29 |
foreach ( $premium as $key ) { |
| 30 |
unset( $updated_settings[ $key ] ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
Settings::set_settings( $updated_settings ); |
| 35 |
|
| 36 |
return [ |
| 37 |
'success' => true, |
| 38 |
'data' => Settings::get_settings(), |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
function destroy() { |
| 43 |
$default_settings = (array) Settings::get_default_settings(); |
| 44 |
Settings::set_settings( $default_settings ); |
| 45 |
|
| 46 |
return [ |
| 47 |
'success' => true, |
| 48 |
'data' => Settings::get_settings(), |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
// ── Dev Mode ────────────────────────────────────────── |
| 53 |
|
| 54 |
function devModeStatus() { |
| 55 |
$status = \Upress\EzCache\Cache::get_dev_mode_status(); |
| 56 |
return [ 'success' => true, 'data' => $status ]; |
| 57 |
} |
| 58 |
|
| 59 |
function enableDevMode( $request ) { |
| 60 |
$duration = $request->get_param( 'duration' ); |
| 61 |
if ( $duration === 'permanent' || $duration === '0' ) { |
| 62 |
$seconds = 0; |
| 63 |
} else { |
| 64 |
$seconds = max( (int) $duration, 3600 ); |
| 65 |
} |
| 66 |
\Upress\EzCache\Cache::enable_dev_mode( $seconds ); |
| 67 |
return [ |
| 68 |
'success' => true, |
| 69 |
'data' => [ 'active' => true, 'message' => 'Development mode enabled' ], |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
function disableDevMode() { |
| 74 |
\Upress\EzCache\Cache::disable_dev_mode(); |
| 75 |
return [ |
| 76 |
'success' => true, |
| 77 |
'data' => [ 'active' => false, 'message' => 'Development mode disabled' ], |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
// ── Diagnostics ────────────────────────────────────── |
| 82 |
|
| 83 |
function diagnose( $request ) { |
| 84 |
$site_url = home_url(); |
| 85 |
|
| 86 |
// Call Go diagnostic API |
| 87 |
$response = wp_remote_post( 'https://api.ezcache-wp.com/analyze', [ |
| 88 |
'headers' => [ 'Content-Type' => 'application/json' ], |
| 89 |
'body' => json_encode( [ 'url' => $site_url ] ), |
| 90 |
'timeout' => 20, |
| 91 |
] ); |
| 92 |
|
| 93 |
if ( is_wp_error( $response ) ) { |
| 94 |
return new \WP_Error( 'diag_error', $response->get_error_message(), [ 'status' => 500 ] ); |
| 95 |
} |
| 96 |
|
| 97 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 98 |
return [ 'success' => true, 'data' => $data ]; |
| 99 |
} |
| 100 |
|
| 101 |
// ── Settings Backup/Restore ────────────────────────── |
| 102 |
|
| 103 |
private function get_backup_dir() { |
| 104 |
$dir = WP_CONTENT_DIR . '/ezcache-backups'; |
| 105 |
if ( ! is_dir( $dir ) ) { |
| 106 |
wp_mkdir_p( $dir ); |
| 107 |
file_put_contents( $dir . '/.htaccess', 'Deny from all' ); |
| 108 |
file_put_contents( $dir . '/index.php', '<?php // Silence is golden' ); |
| 109 |
} |
| 110 |
return $dir; |
| 111 |
} |
| 112 |
|
| 113 |
function listBackups() { |
| 114 |
$dir = $this->get_backup_dir(); |
| 115 |
$files = glob( $dir . '/*.json' ); |
| 116 |
$backups = []; |
| 117 |
|
| 118 |
foreach ( $files as $file ) { |
| 119 |
$content = json_decode( file_get_contents( $file ), true ); |
| 120 |
$backups[] = [ |
| 121 |
'filename' => basename( $file ), |
| 122 |
'name' => isset( $content['name'] ) ? $content['name'] : basename( $file, '.json' ), |
| 123 |
'created_at' => isset( $content['created_at'] ) ? $content['created_at'] : date( 'Y-m-d H:i:s', filemtime( $file ) ), |
| 124 |
'size' => filesize( $file ), |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
usort( $backups, function( $a, $b ) { return strcmp( $b['created_at'], $a['created_at'] ); } ); |
| 129 |
|
| 130 |
return [ 'success' => true, 'data' => $backups ]; |
| 131 |
} |
| 132 |
|
| 133 |
function createBackup( $request ) { |
| 134 |
$name = sanitize_text_field( $request->get_param( 'name' ) ); |
| 135 |
if ( empty( $name ) ) { |
| 136 |
$name = 'Backup ' . date( 'Y-m-d H:i' ); |
| 137 |
} |
| 138 |
|
| 139 |
$settings = \Upress\EzCache\Settings::get_settings(); |
| 140 |
$backup = [ |
| 141 |
'name' => $name, |
| 142 |
'created_at' => date( 'Y-m-d H:i:s' ), |
| 143 |
'version' => defined( 'EZCACHE_VERSION' ) ? EZCACHE_VERSION : '2.1.0', |
| 144 |
'site_url' => home_url(), |
| 145 |
'settings' => $settings, |
| 146 |
]; |
| 147 |
|
| 148 |
$dir = $this->get_backup_dir(); |
| 149 |
$filename = sanitize_file_name( strtolower( str_replace( ' ', '-', $name ) ) ) . '-' . date( 'Ymd-His' ) . '.json'; |
| 150 |
file_put_contents( $dir . '/' . $filename, json_encode( $backup, JSON_PRETTY_PRINT ) ); |
| 151 |
|
| 152 |
return [ |
| 153 |
'success' => true, |
| 154 |
'data' => [ |
| 155 |
'filename' => $filename, |
| 156 |
'message' => 'Backup created: ' . $name, |
| 157 |
], |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
function restoreBackup( $request ) { |
| 162 |
$filename = sanitize_file_name( $request->get_param( 'filename' ) ); |
| 163 |
|
| 164 |
// Handle file upload |
| 165 |
$upload = $request->get_param( 'settings_json' ); |
| 166 |
if ( ! empty( $upload ) ) { |
| 167 |
$backup = json_decode( $upload, true ); |
| 168 |
} else { |
| 169 |
$dir = $this->get_backup_dir(); |
| 170 |
$filepath = $dir . '/' . $filename; |
| 171 |
|
| 172 |
if ( ! file_exists( $filepath ) ) { |
| 173 |
return new \WP_Error( 'not_found', 'Backup file not found', [ 'status' => 404 ] ); |
| 174 |
} |
| 175 |
|
| 176 |
$backup = json_decode( file_get_contents( $filepath ), true ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( empty( $backup['settings'] ) ) { |
| 180 |
return new \WP_Error( 'invalid_backup', 'Invalid backup file', [ 'status' => 400 ] ); |
| 181 |
} |
| 182 |
|
| 183 |
\Upress\EzCache\Settings::set_settings( (array) $backup['settings'] ); |
| 184 |
|
| 185 |
return [ |
| 186 |
'success' => true, |
| 187 |
'data' => [ |
| 188 |
'message' => 'Settings restored from: ' . ( $backup['name'] ?? $filename ), |
| 189 |
'settings' => \Upress\EzCache\Settings::get_settings(), |
| 190 |
], |
| 191 |
]; |
| 192 |
} |
| 193 |
} |
| 194 |
|