PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 0.2.1
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v0.2.1
1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 1.5.1 All 26 releases
flywp / includes / Admin / Fastcgi_Cache.php

Fastcgi_Cache.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 0.2.1, at includes/Admin/Fastcgi_Cache.php

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