| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\Admin; |
| 4 |
|
| 5 |
use FlyWP\Admin; |
| 6 |
use FlyWP\LiteSpeed as FlyWPLiteSpeed; |
| 7 |
|
| 8 |
/** |
| 9 |
* LiteSpeed class. |
| 10 |
*/ |
| 11 |
class Litespeed { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class constructor. |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
add_action( 'load-' . Admin::SCREEN_NAME, [ $this, 'handle_enable_disable' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get enable/disable url. |
| 22 |
* |
| 23 |
* @param string $action |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public function enable_disable_url( $action = 'enable' ) { |
| 28 |
return wp_nonce_url( |
| 29 |
add_query_arg( |
| 30 |
[ |
| 31 |
'flywp-action' => 'toggle-lscache', |
| 32 |
'type' => $action, |
| 33 |
], |
| 34 |
flywp()->admin->page_url() |
| 35 |
), |
| 36 |
'flywp-litespeed-nonce' |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Handle enable/disable action. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function handle_enable_disable() { |
| 46 |
if ( ! isset( $_GET['flywp-action'] ) || 'toggle-lscache' !== $_GET['flywp-action'] ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 51 |
if ( isset( $_GET['_wpnonce'] ) && ! wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'flywp-litespeed-nonce' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$valid_types = [ 'enable', 'disable' ]; |
| 60 |
|
| 61 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 62 |
$type = isset( $_GET['type'] ) && in_array( wp_unslash( $_GET['type'] ), $valid_types, true ) ? wp_unslash( $_GET['type'] ) : 'enable'; |
| 63 |
$status = $type === 'enable' ? '1' : '0'; |
| 64 |
$notice = $type === 'enable' ? 'lscache-enabled' : 'lscache-disabled'; |
| 65 |
|
| 66 |
update_option( FlyWPLiteSpeed::OPTION_KEY, $status ); |
| 67 |
|
| 68 |
flywp()->flyapi->cache_toggle( $type, 'lscache' ); |
| 69 |
|
| 70 |
wp_safe_redirect( admin_url( 'index.php?page=flywp&fly-notice=' . $notice ) ); |
| 71 |
exit; |
| 72 |
} |
| 73 |
} |
| 74 |
|