| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\Admin; |
| 4 |
|
| 5 |
use FlyWP\Admin; |
| 6 |
use FlyWP\Helper; |
| 7 |
|
| 8 |
class Fastcgi_Cache { |
| 9 |
|
| 10 |
/** |
| 11 |
* Class constructor. |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'load-' . Admin::SCREEN_NAME, [ $this, 'save_settings' ] ); |
| 15 |
add_action( 'load-' . Admin::SCREEN_NAME, [ $this, 'handle_cleanup' ] ); |
| 16 |
add_action( 'load-' . Admin::SCREEN_NAME, [ $this, 'handle_enable_disable' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* FastCGI cache purge url. |
| 21 |
* |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
public function purge_cache_url() { |
| 25 |
return wp_nonce_url( |
| 26 |
add_query_arg( |
| 27 |
[ 'flywp-action' => 'purge-fastcgi-cache' ], |
| 28 |
flywp()->admin->page_url() |
| 29 |
), |
| 30 |
'fly-fastcgi-purge-cache' |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get enable/disable url. |
| 36 |
* |
| 37 |
* @param string $action |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function enable_disable_url( $action = 'enable' ) { |
| 42 |
return wp_nonce_url( |
| 43 |
add_query_arg( |
| 44 |
[ |
| 45 |
'flywp-action' => 'toggle-fastcgi-cache', |
| 46 |
'type' => $action, |
| 47 |
], |
| 48 |
flywp()->admin->page_url() |
| 49 |
), |
| 50 |
'fly-fastcgi-toggle-cache' |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Handle cache cleanup. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function handle_cleanup() { |
| 60 |
if ( ! isset( $_GET['flywp-action'] ) || 'purge-fastcgi-cache' !== $_GET['flywp-action'] ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if ( isset( $_GET['_wpnonce'] ) && ! wp_verify_nonce( $_GET['_wpnonce'], 'fly-fastcgi-purge-cache' ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
flywp()->fastcgi->clear_cache(); |
| 73 |
|
| 74 |
wp_safe_redirect( admin_url( 'tools.php?page=flywp&fly-notice=fastcgi-purged' ) ); |
| 75 |
exit; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Save cache settings. |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function save_settings() { |
| 84 |
if ( ! isset( $_POST['flywp-fastcgi-nonce'] ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! wp_verify_nonce( $_POST['flywp-fastcgi-nonce'], 'flywp-fastcgi-nonce' ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$settings = []; |
| 97 |
$keys = [ |
| 98 |
'home-purge-created' => 'home_created', |
| 99 |
'home-purge-deleted' => 'home_deleted', |
| 100 |
// 'single-post-created' => 'single_modified', |
| 101 |
// 'single-post-comment' => 'single_comment', |
| 102 |
]; |
| 103 |
|
| 104 |
foreach ( $keys as $form_key => $settings_key ) { |
| 105 |
$settings[ $settings_key ] = isset( $_POST[ $form_key ] ) ? true : false; |
| 106 |
} |
| 107 |
|
| 108 |
$old_settings = flywp()->fastcgi->settings(); |
| 109 |
$settings = array_merge( $old_settings, $settings ); |
| 110 |
|
| 111 |
update_option( flywp()->fastcgi::SETTINGS_KEY, $settings ); |
| 112 |
|
| 113 |
wp_safe_redirect( admin_url( 'tools.php?page=flywp&fly-notice=fastcgi-saved' ) ); |
| 114 |
exit; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Handle enable/disable. |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function handle_enable_disable() { |
| 123 |
if ( ! isset( $_GET['flywp-action'] ) || 'toggle-fastcgi-cache' !== $_GET['flywp-action'] ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
if ( isset( $_GET['_wpnonce'] ) && ! wp_verify_nonce( $_GET['_wpnonce'], 'fly-fastcgi-toggle-cache' ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$valid_types = [ 'enable', 'disable' ]; |
| 136 |
$type = isset( $_GET['type'] ) && in_array( $_GET['type'], $valid_types ) ? $_GET['type'] : 'enable'; |
| 137 |
|
| 138 |
$settings = flywp()->fastcgi->settings(); |
| 139 |
$settings['enabled'] = $type === 'enable' ? true : false; |
| 140 |
$notice = $type === 'enable' ? 'fastcgi-enabled' : 'fastcgi-disabled'; |
| 141 |
|
| 142 |
// Toggle cache in the API |
| 143 |
$reponse = flywp()->flyapi->cache_toggle( $type ); |
| 144 |
// Helper::dd( $reponse ); |
| 145 |
|
| 146 |
update_option( flywp()->fastcgi::SETTINGS_KEY, $settings ); |
| 147 |
|
| 148 |
wp_safe_redirect( admin_url( 'tools.php?page=flywp&fly-notice=' . $notice ) ); |
| 149 |
exit; |
| 150 |
} |
| 151 |
} |
| 152 |
|