| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * The admin-specific functionality of the plugin. |
|---|
| 4 | * |
|---|
| 5 | * @link: https://www.acato.nl |
|---|
| 6 | * @since 2018.1 |
|---|
| 7 | * |
|---|
| 8 | * @package WP_Rest_Cache_Plugin |
|---|
| 9 | * @subpackage WP_Rest_Cache_Plugin/Admin |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | namespace WP_Rest_Cache_Plugin\Admin; |
|---|
| 13 | |
|---|
| 14 | use WP_Rest_Cache_Plugin\Includes\Caching\Caching; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * The admin-specific functionality of the plugin. |
|---|
| 18 | * |
|---|
| 19 | * Defines the plugin name, version, and two examples hooks for how to |
|---|
| 20 | * enqueue the admin-specific stylesheet and JavaScript. |
|---|
| 21 | * |
|---|
| 22 | * @package WP_Rest_Cache_Plugin |
|---|
| 23 | * @subpackage WP_Rest_Cache_Plugin/Admin |
|---|
| 24 | * @author: Richard Korthuis - Acato <richardkorthuis@acato.nl> |
|---|
| 25 | */ |
|---|
| 26 | class Admin { |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * The ID of this plugin. |
|---|
| 30 | * |
|---|
| 31 | * @access private |
|---|
| 32 | * @var string $plugin_name The ID of this plugin. |
|---|
| 33 | */ |
|---|
| 34 | private $plugin_name; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * The version of this plugin. |
|---|
| 38 | * |
|---|
| 39 | * @access private |
|---|
| 40 | * @var string $version The current version of this plugin. |
|---|
| 41 | */ |
|---|
| 42 | private $version; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * The settings panels for the WP REST Cache settings page. |
|---|
| 46 | * |
|---|
| 47 | * @access private |
|---|
| 48 | * @var array<string,array<string,mixed>> $settings_panels The settings panels for the WP REST Cache settings page. |
|---|
| 49 | */ |
|---|
| 50 | private $settings_panels; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Initialize the class and set its properties. |
|---|
| 54 | * |
|---|
| 55 | * @param string $plugin_name The name of this plugin. |
|---|
| 56 | * @param string $version The version of this plugin. |
|---|
| 57 | */ |
|---|
| 58 | public function __construct( $plugin_name, $version ) { |
|---|
| 59 | $this->plugin_name = $plugin_name; |
|---|
| 60 | $this->version = $version; |
|---|
| 61 | $this->settings_panels = []; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Register the stylesheets for the admin area. |
|---|
| 66 | * |
|---|
| 67 | * @return void |
|---|
| 68 | */ |
|---|
| 69 | public function enqueue_styles() { |
|---|
| 70 | wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wp-rest-cache-admin.css', [], $this->version, 'all' ); |
|---|
| 71 | if ( 'wp-rest-cache' === filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) |
|---|
| 72 | && 'clear-cache' === filter_input( INPUT_GET, 'sub', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) { |
|---|
| 73 | wp_enqueue_style( 'jquery-ui-progressbar', plugin_dir_url( __FILE__ ) . 'css/jquery-ui.css', [], $this->version, 'all' ); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Register the scripts for the admin area. |
|---|
| 79 | * |
|---|
| 80 | * @return void |
|---|
| 81 | */ |
|---|
| 82 | public function enqueue_scripts() { |
|---|
| 83 | if ( 'wp-rest-cache' === filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) |
|---|
| 84 | && 'clear-cache' === filter_input( INPUT_GET, 'sub', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) { |
|---|
| 85 | wp_enqueue_script( 'jquery-ui-progressbar' ); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Add a new menu item under Settings. |
|---|
| 91 | * |
|---|
| 92 | * @return void |
|---|
| 93 | */ |
|---|
| 94 | public function create_menu() { |
|---|
| 95 | /** |
|---|
| 96 | * Change who can see (and alter) the settings for the WP REST Cache. |
|---|
| 97 | * |
|---|
| 98 | * Allows to change the capability for the WP REST Cache settings page. |
|---|
| 99 | * |
|---|
| 100 | * @since 2019.4.3 |
|---|
| 101 | * |
|---|
| 102 | * @param string $capability The capability for the settings page. |
|---|
| 103 | */ |
|---|
| 104 | $capability = apply_filters( 'wp_rest_cache/settings_capability', 'administrator' ); |
|---|
| 105 | |
|---|
| 106 | $hook = add_submenu_page( |
|---|
| 107 | 'options-general.php', |
|---|
| 108 | 'WP REST Cache', |
|---|
| 109 | 'WP REST Cache', |
|---|
| 110 | $capability, |
|---|
| 111 | 'wp-rest-cache', |
|---|
| 112 | [ |
|---|
| 113 | $this, |
|---|
| 114 | 'settings_page', |
|---|
| 115 | ] |
|---|
| 116 | ); |
|---|
| 117 | |
|---|
| 118 | add_action( "load-$hook", [ $this, 'add_screen_options' ] ); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Add screen options to the WP Admin |
|---|
| 123 | * |
|---|
| 124 | * @return void |
|---|
| 125 | */ |
|---|
| 126 | public function add_screen_options() { |
|---|
| 127 | $args = [ |
|---|
| 128 | 'label' => __( 'Caches', 'wp-rest-cache' ), |
|---|
| 129 | 'default' => Includes\API_Caches_Table::ITEMS_PER_PAGE, |
|---|
| 130 | 'option' => 'caches_per_page', |
|---|
| 131 | ]; |
|---|
| 132 | add_screen_option( 'per_page', $args ); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Set the caches_per_pages screen option. |
|---|
| 137 | * |
|---|
| 138 | * @param int $option_value Screen option value. Default false to skip. |
|---|
| 139 | * @param string $option The option name. |
|---|
| 140 | * @param int $value The number of rows to use. |
|---|
| 141 | * |
|---|
| 142 | * @return int |
|---|
| 143 | */ |
|---|
| 144 | public function set_screen_option( $option_value, $option, $value ): int { |
|---|
| 145 | if ( 'caches_per_page' === $option ) { |
|---|
| 146 | return $value; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return $option_value; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Add a settings link to the plugin on the Plugin admin screen. |
|---|
| 154 | * |
|---|
| 155 | * @param array<int,string> $links An array of plugin action links. |
|---|
| 156 | * |
|---|
| 157 | * @return array<int,string> An array of plugin action links. |
|---|
| 158 | */ |
|---|
| 159 | public function add_plugin_settings_link( $links ): array { |
|---|
| 160 | $links[] = '<a href="' . |
|---|
| 161 | admin_url( 'options-general.php?page=wp-rest-cache' ) . '">' . |
|---|
| 162 | __( 'Settings', 'wp-rest-cache' ) . '</a>'; |
|---|
| 163 | |
|---|
| 164 | return $links; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Register plugin specific settings. |
|---|
| 169 | * |
|---|
| 170 | * @return void |
|---|
| 171 | */ |
|---|
| 172 | public function register_settings() { |
|---|
| 173 | register_setting( |
|---|
| 174 | 'wp-rest-cache-settings', |
|---|
| 175 | 'wp_rest_cache_timeout', |
|---|
| 176 | [ |
|---|
| 177 | 'type' => 'integer', |
|---|
| 178 | 'sanitize_callback' => [ $this, 'sanitize_timeout' ], |
|---|
| 179 | ] |
|---|
| 180 | ); |
|---|
| 181 | register_setting( |
|---|
| 182 | 'wp-rest-cache-settings', |
|---|
| 183 | 'wp_rest_cache_timeout_interval', |
|---|
| 184 | [ |
|---|
| 185 | 'type' => 'integer', |
|---|
| 186 | 'sanitize_callback' => [ $this, 'sanitize_timeout_interval' ], |
|---|
| 187 | ] |
|---|
| 188 | ); |
|---|
| 189 | register_setting( |
|---|
| 190 | 'wp-rest-cache-settings', |
|---|
| 191 | 'wp_rest_cache_regenerate', |
|---|
| 192 | [ |
|---|
| 193 | 'type' => 'string', |
|---|
| 194 | 'sanitize_callback' => [ $this, 'sanitize_checkbox' ], |
|---|
| 195 | ] |
|---|
| 196 | ); |
|---|
| 197 | register_setting( |
|---|
| 198 | 'wp-rest-cache-settings', |
|---|
| 199 | 'wp_rest_cache_regenerate_interval', |
|---|
| 200 | [ |
|---|
| 201 | 'type' => 'string', |
|---|
| 202 | 'sanitize_callback' => [ $this, 'sanitize_regenerate_interval' ], |
|---|
| 203 | ] |
|---|
| 204 | ); |
|---|
| 205 | register_setting( |
|---|
| 206 | 'wp-rest-cache-settings', |
|---|
| 207 | 'wp_rest_cache_regenerate_number', |
|---|
| 208 | [ |
|---|
| 209 | 'type' => 'integer', |
|---|
| 210 | 'sanitize_callback' => [ $this, 'sanitize_regenerate_number' ], |
|---|
| 211 | ] |
|---|
| 212 | ); |
|---|
| 213 | register_setting( |
|---|
| 214 | 'wp-rest-cache-settings', |
|---|
| 215 | 'wp_rest_cache_memcache_used', |
|---|
| 216 | [ |
|---|
| 217 | 'type' => 'string', |
|---|
| 218 | 'sanitize_callback' => [ $this, 'sanitize_checkbox' ], |
|---|
| 219 | ] |
|---|
| 220 | ); |
|---|
| 221 | register_setting( |
|---|
| 222 | 'wp-rest-cache-settings', |
|---|
| 223 | 'wp_rest_cache_global_cacheable_request_headers', |
|---|
| 224 | [ |
|---|
| 225 | 'type' => 'string', |
|---|
| 226 | 'sanitize_callback' => [ $this, 'sanitize_cacheable_request_headers' ], |
|---|
| 227 | ] |
|---|
| 228 | ); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * Sanitize the timeout setting. |
|---|
| 233 | * |
|---|
| 234 | * @param mixed $value The value to sanitize. |
|---|
| 235 | * |
|---|
| 236 | * @return int The sanitized value. |
|---|
| 237 | */ |
|---|
| 238 | public function sanitize_timeout( $value ): int { |
|---|
| 239 | $value = absint( $value ); |
|---|
| 240 | return max( 1, $value ); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | /** |
|---|
| 244 | * Sanitize the timeout interval setting. |
|---|
| 245 | * |
|---|
| 246 | * @param mixed $value The value to sanitize. |
|---|
| 247 | * |
|---|
| 248 | * @return int The sanitized value. |
|---|
| 249 | */ |
|---|
| 250 | public function sanitize_timeout_interval( $value ): int { |
|---|
| 251 | $allowed = [ |
|---|
| 252 | MINUTE_IN_SECONDS, |
|---|
| 253 | HOUR_IN_SECONDS, |
|---|
| 254 | DAY_IN_SECONDS, |
|---|
| 255 | WEEK_IN_SECONDS, |
|---|
| 256 | MONTH_IN_SECONDS, |
|---|
| 257 | YEAR_IN_SECONDS, |
|---|
| 258 | ]; |
|---|
| 259 | $value = absint( $value ); |
|---|
| 260 | return in_array( $value, $allowed, true ) ? $value : YEAR_IN_SECONDS; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | /** |
|---|
| 264 | * Sanitize a checkbox setting. |
|---|
| 265 | * |
|---|
| 266 | * @param mixed $value The value to sanitize. |
|---|
| 267 | * |
|---|
| 268 | * @return string The sanitized value ('1' or ''). |
|---|
| 269 | */ |
|---|
| 270 | public function sanitize_checkbox( $value ): string { |
|---|
| 271 | return '1' === $value ? '1' : ''; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | /** |
|---|
| 275 | * Sanitize the regenerate interval setting. |
|---|
| 276 | * |
|---|
| 277 | * @param mixed $value The value to sanitize. |
|---|
| 278 | * |
|---|
| 279 | * @return string The sanitized value. |
|---|
| 280 | */ |
|---|
| 281 | public function sanitize_regenerate_interval( $value ): string { |
|---|
| 282 | $schedules = wp_get_schedules(); |
|---|
| 283 | $value = sanitize_text_field( $value ); |
|---|
| 284 | return isset( $schedules[ $value ] ) ? $value : 'twicedaily'; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * Sanitize the regenerate number setting. |
|---|
| 289 | * |
|---|
| 290 | * @param mixed $value The value to sanitize. |
|---|
| 291 | * |
|---|
| 292 | * @return int The sanitized value. |
|---|
| 293 | */ |
|---|
| 294 | public function sanitize_regenerate_number( $value ): int { |
|---|
| 295 | $value = absint( $value ); |
|---|
| 296 | return max( 1, $value ); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * Sanitize the global cacheable request headers setting. |
|---|
| 301 | * |
|---|
| 302 | * @param mixed $value The value to sanitize. |
|---|
| 303 | * |
|---|
| 304 | * @return string The sanitized value. |
|---|
| 305 | */ |
|---|
| 306 | public function sanitize_cacheable_request_headers( $value ): string { |
|---|
| 307 | $value = sanitize_text_field( $value ); |
|---|
| 308 | $headers = array_map( 'trim', explode( ',', $value ) ); |
|---|
| 309 | $headers = array_filter( $headers ); |
|---|
| 310 | return implode( ',', $headers ); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | * Display the plugin settings page. |
|---|
| 315 | * |
|---|
| 316 | * @return void |
|---|
| 317 | */ |
|---|
| 318 | public function settings_page() { |
|---|
| 319 | /** This filter is documented in the function create_menu(). */ |
|---|
| 320 | if ( ! current_user_can( apply_filters( 'wp_rest_cache/settings_capability', 'administrator' ) ) ) { |
|---|
| 321 | wp_die( esc_html__( 'You do not have permission to access this page.', 'wp-rest-cache' ) ); |
|---|
| 322 | } |
|---|
| 323 | $this->settings_panels = apply_filters( 'wp_rest_cache/settings_panels', $this->settings_panels ); |
|---|
| 324 | |
|---|
| 325 | $sub = filter_input( INPUT_GET, 'sub', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
|---|
| 326 | if ( empty( $sub ) ) { |
|---|
| 327 | $sub = 'settings'; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | // prevent path traversal attacks in the sub parameter. |
|---|
| 331 | $potential_sub_file = __DIR__ . '/partials/sub-' . $sub . '.php'; |
|---|
| 332 | if ( dirname( $potential_sub_file ) !== __DIR__ . '/partials' ) { |
|---|
| 333 | $sub = 'settings'; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | include_once __DIR__ . '/partials/header.php'; |
|---|
| 337 | if ( isset( $this->settings_panels[ $sub ]['template'] ) ) { |
|---|
| 338 | include_once $this->settings_panels[ $sub ]['template']; |
|---|
| 339 | } elseif ( file_exists( __DIR__ . '/partials/sub-' . $sub . '.php' ) ) { |
|---|
| 340 | include_once __DIR__ . '/partials/sub-' . $sub . '.php'; |
|---|
| 341 | } else { |
|---|
| 342 | include_once __DIR__ . '/partials/sub-settings.php'; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /** |
|---|
| 347 | * Add a 'Clear REST cache' button to the wp-admin top bar. |
|---|
| 348 | * |
|---|
| 349 | * @return void |
|---|
| 350 | */ |
|---|
| 351 | public function admin_bar_item() { |
|---|
| 352 | /** This filter is documented in the function create_menu(). */ |
|---|
| 353 | if ( ! current_user_can( apply_filters( 'wp_rest_cache/settings_capability', 'administrator' ) ) ) { |
|---|
| 354 | return; |
|---|
| 355 | } |
|---|
| 356 | /** |
|---|
| 357 | * Show or hide the 'Clear REST cache' button in the wp-admin bar. |
|---|
| 358 | * |
|---|
| 359 | * Allows to hide (or show under conditions) the 'Clear REST cache button in the wp-admin bar. |
|---|
| 360 | * |
|---|
| 361 | * @since 2018.3.1 |
|---|
| 362 | * |
|---|
| 363 | * @param boolean $show Boolean whether the 'Clear REST cache' button should be shown. |
|---|
| 364 | */ |
|---|
| 365 | $show = apply_filters( 'wp_rest_cache/display_clear_cache_button', true ); |
|---|
| 366 | if ( true === $show ) { |
|---|
| 367 | global $wp_admin_bar; |
|---|
| 368 | |
|---|
| 369 | $args = [ |
|---|
| 370 | 'id' => 'wp-rest-cache-clear', |
|---|
| 371 | 'title' => '<span class="ab-icon"></span>' . __( 'Clear REST cache', 'wp-rest-cache' ), |
|---|
| 372 | 'href' => self::empty_cache_url(), |
|---|
| 373 | ]; |
|---|
| 374 | |
|---|
| 375 | $wp_admin_bar->add_menu( $args ); |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | /** |
|---|
| 380 | * Create the url to empty the cache. |
|---|
| 381 | * |
|---|
| 382 | * @return string The url to empty the cache. |
|---|
| 383 | */ |
|---|
| 384 | public static function empty_cache_url(): string { |
|---|
| 385 | return wp_nonce_url( admin_url( 'options-general.php?page=wp-rest-cache&sub=clear-cache' ), 'wp_rest_cache_options', 'wp_rest_cache_nonce' ); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | /** |
|---|
| 389 | * Handle the correct actions. I.e. dismiss a notice. |
|---|
| 390 | * |
|---|
| 391 | * @return void |
|---|
| 392 | */ |
|---|
| 393 | public function handle_actions() { |
|---|
| 394 | if ( isset( $_GET['wp_rest_cache_dismiss'] ) |
|---|
| 395 | && check_admin_referer( 'wp-rest-cache-dismiss-notice-' . filter_input( INPUT_GET, 'wp_rest_cache_dismiss' ) ) |
|---|
| 396 | ) { |
|---|
| 397 | $user_id = get_current_user_id(); |
|---|
| 398 | $dismissed_notices = get_user_meta( $user_id, 'wp_rest_cache_dismissed_notices', true ); |
|---|
| 399 | if ( ! is_array( $dismissed_notices ) ) { |
|---|
| 400 | $dismissed_notices = []; |
|---|
| 401 | } |
|---|
| 402 | if ( ! in_array( filter_input( INPUT_GET, 'wp_rest_cache_dismiss' ), $dismissed_notices, true ) ) { |
|---|
| 403 | $dismissed_notices[] = filter_input( INPUT_GET, 'wp_rest_cache_dismiss' ); |
|---|
| 404 | update_user_meta( $user_id, 'wp_rest_cache_dismissed_notices', $dismissed_notices ); |
|---|
| 405 | } |
|---|
| 406 | } |
|---|
| 407 | if ( isset( $_REQUEST['action'] ) && -1 !== $_REQUEST['action'] ) { |
|---|
| 408 | $wp_rest_cache_list = new \WP_Rest_Cache_Plugin\Admin\Includes\API_Caches_Table( 'endpoint' ); |
|---|
| 409 | $wp_rest_cache_list->process_action(); |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | /** |
|---|
| 414 | * Add a notice to the wp-admin. |
|---|
| 415 | * |
|---|
| 416 | * @param string $type The type of message (error|warning|success|info). |
|---|
| 417 | * @param string $message The message to display. |
|---|
| 418 | * @param string|bool $dismissible Boolean, should the message be dismissible or 'permanent' for permanently dismissible notice. |
|---|
| 419 | * @param array<string,string> $button An array with label + url to display a button with the notice. |
|---|
| 420 | * |
|---|
| 421 | * @return void |
|---|
| 422 | */ |
|---|
| 423 | protected function add_notice( $type, $message, $dismissible = true, $button = [] ) { |
|---|
| 424 | $notices = get_option( 'wp_rest_cache_admin_notices', [] ); |
|---|
| 425 | |
|---|
| 426 | if ( ! array_key_exists( $type, $notices ) ) { |
|---|
| 427 | $notices[ $type ] = []; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | if ( ! in_array( $message, array_column( $notices[ $type ], 'message' ), true ) ) { |
|---|
| 431 | $notices[ $type ][] = [ |
|---|
| 432 | 'message' => $message, |
|---|
| 433 | 'dismissible' => $dismissible, |
|---|
| 434 | 'button' => $button, |
|---|
| 435 | ]; |
|---|
| 436 | update_option( 'wp_rest_cache_admin_notices', $notices, false ); |
|---|
| 437 | } |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | /** |
|---|
| 441 | * Check if the MU plugin was created, if not display a warning. |
|---|
| 442 | * |
|---|
| 443 | * @return void |
|---|
| 444 | */ |
|---|
| 445 | public function check_muplugin_existence() { |
|---|
| 446 | if ( ! file_exists( WPMU_PLUGIN_DIR . '/wp-rest-cache.php' ) ) { |
|---|
| 447 | \WP_Rest_Cache_Plugin\Includes\Activator::create_mu_plugin(); |
|---|
| 448 | if ( ! file_exists( WPMU_PLUGIN_DIR . '/wp-rest-cache.php' ) ) { |
|---|
| 449 | |
|---|
| 450 | $from = '<code>' . substr( |
|---|
| 451 | plugin_dir_path( __DIR__ ) . 'sources/wp-rest-cache.php', |
|---|
| 452 | strpos( plugin_dir_path( __DIR__ ), '/wp-content/' ) |
|---|
| 453 | ) . '</code>'; |
|---|
| 454 | $to = '<code>' . substr( |
|---|
| 455 | WPMU_PLUGIN_DIR . '/wp-rest-cache.php', |
|---|
| 456 | strpos( WPMU_PLUGIN_DIR, '/wp-content/' ) |
|---|
| 457 | ) . '</code>'; |
|---|
| 458 | |
|---|
| 459 | $this->add_notice( |
|---|
| 460 | 'warning', |
|---|
| 461 | sprintf( |
|---|
| 462 | /* translators: %1$s: source-directory, %2$s: target-directory */ |
|---|
| 463 | __( 'You are not getting the best caching result! <br/>Please copy %1$s to %2$s', 'wp-rest-cache' ), |
|---|
| 464 | $from, |
|---|
| 465 | $to |
|---|
| 466 | ), |
|---|
| 467 | false |
|---|
| 468 | ); |
|---|
| 469 | } |
|---|
| 470 | } |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | /** |
|---|
| 474 | * Check if external object caching is being used, if so display a warning for needed Memcache(d) settings. |
|---|
| 475 | * |
|---|
| 476 | * @return void |
|---|
| 477 | */ |
|---|
| 478 | public function check_memcache_ext_object_caching() { |
|---|
| 479 | if ( wp_using_ext_object_cache() |
|---|
| 480 | && ( class_exists( 'Memcache' ) || class_exists( 'Memcached' ) ) |
|---|
| 481 | && ! Caching::get_instance()->get_memcache_used() ) { |
|---|
| 482 | $this->add_notice( |
|---|
| 483 | 'warning', |
|---|
| 484 | __( 'We have detected you are using external object caching. If you are using Memcache(d) as external object cache, please make sure you visit this plugin\'s settings page and check the `Using Memcache(d)` checkbox.', 'wp-rest-cache' ), |
|---|
| 485 | 'permanent' |
|---|
| 486 | ); |
|---|
| 487 | } |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | /** |
|---|
| 491 | * Display notices (if any) on the Admin dashboard |
|---|
| 492 | * |
|---|
| 493 | * @return void |
|---|
| 494 | */ |
|---|
| 495 | public function display_notices() { |
|---|
| 496 | if ( ! in_array( get_current_screen()->base, [ 'plugins', 'dashboard', 'settings_page_wp-rest-cache' ], true ) ) { |
|---|
| 497 | return; |
|---|
| 498 | } |
|---|
| 499 | $notices = get_option( 'wp_rest_cache_admin_notices', [] ); |
|---|
| 500 | if ( is_array( $notices ) && count( $notices ) ) { |
|---|
| 501 | $user_id = get_current_user_id(); |
|---|
| 502 | $dismissed_notices = get_user_meta( $user_id, 'wp_rest_cache_dismissed_notices', true ); |
|---|
| 503 | foreach ( $notices as $type => $messages ) { |
|---|
| 504 | foreach ( $messages as $message ) { |
|---|
| 505 | if ( ! is_array( $dismissed_notices ) || ! in_array( esc_attr( md5( $message['message'] ) ), $dismissed_notices, true ) ) { |
|---|
| 506 | ?> |
|---|
| 507 | <div |
|---|
| 508 | class="notice notice-<?php echo esc_attr( $type ); ?> <?php echo ( true === $message['dismissible'] ) ? 'is-dismissible' : ''; ?>"> |
|---|
| 509 | <p><strong>WP REST Cache:</strong> <?php echo wp_kses_post( $message['message'] ); ?></p> |
|---|
| 510 | <?php if ( isset( $message['button']['url'], $message['button']['label'] ) ) : ?> |
|---|
| 511 | <p><a class="<?php echo esc_attr( $message['button']['class'] ?? 'button' ); ?>" href="<?php echo esc_attr( $message['button']['url'] ); ?>"><?php echo esc_html( $message['button']['label'] ); ?></a></p> |
|---|
| 512 | <?php endif; ?> |
|---|
| 513 | <?php if ( 'permanent' === $message['dismissible'] ) : ?> |
|---|
| 514 | <?php |
|---|
| 515 | $url = get_admin_url() . wp_nonce_url( |
|---|
| 516 | '?wp_rest_cache_dismiss=' . esc_attr( md5( $message['message'] ) ), |
|---|
| 517 | 'wp-rest-cache-dismiss-notice-' . esc_attr( md5( $message['message'] ) ) |
|---|
| 518 | ); |
|---|
| 519 | ?> |
|---|
| 520 | <p><a class="button" |
|---|
| 521 | href="<?php echo esc_attr( $url ); ?>"><?php echo esc_html_e( 'Hide this message', 'wp-rest-cache' ); ?></a></p> |
|---|
| 522 | <?php endif; ?> |
|---|
| 523 | </div> |
|---|
| 524 | <?php |
|---|
| 525 | } |
|---|
| 526 | } |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | delete_option( 'wp_rest_cache_admin_notices' ); |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | /** |
|---|
| 534 | * Unschedule or schedule the cron based on the regenerate setting. |
|---|
| 535 | * |
|---|
| 536 | * @param mixed $old_value The old option value. |
|---|
| 537 | * @param mixed $value The new option value. |
|---|
| 538 | * |
|---|
| 539 | * @return void |
|---|
| 540 | */ |
|---|
| 541 | public function regenerate_updated( $old_value, $value ) { |
|---|
| 542 | if ( '1' === $value ) { |
|---|
| 543 | $wp_rest_cache_regenerate_interval = Caching::get_instance()->get_regenerate_interval(); |
|---|
| 544 | wp_schedule_event( time(), $wp_rest_cache_regenerate_interval, 'wp_rest_cache_regenerate_cron' ); |
|---|
| 545 | } else { |
|---|
| 546 | wp_clear_scheduled_hook( 'wp_rest_cache_regenerate_cron' ); |
|---|
| 547 | } |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | /** |
|---|
| 551 | * Update regenerate interval based on new setting. |
|---|
| 552 | * |
|---|
| 553 | * @param mixed $old_value The old option value. |
|---|
| 554 | * @param mixed $value The new option value. |
|---|
| 555 | * |
|---|
| 556 | * @return void |
|---|
| 557 | */ |
|---|
| 558 | public function regenerate_interval_updated( $old_value, $value ) { |
|---|
| 559 | if ( Caching::get_instance()->should_regenerate() ) { |
|---|
| 560 | wp_clear_scheduled_hook( 'wp_rest_cache_regenerate_cron' ); |
|---|
| 561 | wp_schedule_event( time(), $value, 'wp_rest_cache_regenerate_cron' ); |
|---|
| 562 | } |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | /** |
|---|
| 566 | * Flush caches in batches. Used through an ajax call from the 'Clear REST Cache' button. |
|---|
| 567 | * |
|---|
| 568 | * @return void |
|---|
| 569 | */ |
|---|
| 570 | public function flush_caches() { |
|---|
| 571 | check_ajax_referer( 'wp_rest_cache_clear_cache_ajax', 'wp_rest_cache_nonce' ); |
|---|
| 572 | /** This filter is documented in the function create_menu(). */ |
|---|
| 573 | if ( ! current_user_can( apply_filters( 'wp_rest_cache/settings_capability', 'administrator' ) ) ) { |
|---|
| 574 | wp_die( esc_html__( 'You do not have permission to perform this action.', 'wp-rest-cache' ) ); |
|---|
| 575 | } |
|---|
| 576 | $delete_caches = filter_input( INPUT_POST, 'delete_caches', FILTER_VALIDATE_BOOLEAN ); |
|---|
| 577 | |
|---|
| 578 | $caching = Caching::get_instance(); |
|---|
| 579 | $caching->delete_all_caches( $delete_caches ); |
|---|
| 580 | |
|---|
| 581 | $result = [ 'percentage' => 100 ]; // deprecated, since we delete all caches at once, we should remove the progress bar. |
|---|
| 582 | |
|---|
| 583 | echo wp_json_encode( $result ); |
|---|
| 584 | exit; |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | /** |
|---|
| 588 | * Upon plugin activation show a message about flushing the REST cache (or flush specific caches when certain |
|---|
| 589 | * plugins are activated). |
|---|
| 590 | * |
|---|
| 591 | * @param string $plugin The plugin that has just been activated. |
|---|
| 592 | * @param boolean $network_wide Whether the plugin has been activated network wide. |
|---|
| 593 | * |
|---|
| 594 | * @return void |
|---|
| 595 | */ |
|---|
| 596 | public function activated_plugin( $plugin, $network_wide ) { |
|---|
| 597 | // Wordfence alters the output of the users endpoint, so flush all users endpoint caches. |
|---|
| 598 | if ( 'wordfence/wordfence.php' === $plugin ) { |
|---|
| 599 | $rest_prefix = sprintf( '/%s/', get_option( 'wp_rest_cache_rest_prefix', 'wp-json' ) ); |
|---|
| 600 | $caching = Caching::get_instance(); |
|---|
| 601 | if ( $network_wide ) { |
|---|
| 602 | $site_ids = get_sites( [ 'fields' => 'ids' ] ); |
|---|
| 603 | foreach ( $site_ids as $site_id ) { |
|---|
| 604 | switch_to_blog( $site_id ); |
|---|
| 605 | $caching->delete_cache_by_endpoint( $rest_prefix . 'wp/v2/users', Caching::FLUSH_LOOSE ); |
|---|
| 606 | restore_current_blog(); |
|---|
| 607 | } |
|---|
| 608 | } else { |
|---|
| 609 | $caching->delete_cache_by_endpoint( $rest_prefix . 'wp/v2/users', Caching::FLUSH_LOOSE ); |
|---|
| 610 | } |
|---|
| 611 | } else { |
|---|
| 612 | $this->add_notice( |
|---|
| 613 | 'warning', |
|---|
| 614 | __( 'A new plugin has been activated. This might effect the WP REST API output, so please consider if clearing the REST Cache is necessary.', 'wp-rest-cache' ), |
|---|
| 615 | false, |
|---|
| 616 | [ |
|---|
| 617 | 'label' => __( 'Clear REST cache', 'wp-rest-cache' ), |
|---|
| 618 | 'url' => self::empty_cache_url(), |
|---|
| 619 | ] |
|---|
| 620 | ); |
|---|
| 621 | } |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | /** |
|---|
| 625 | * Upon plugin deactivation show a message about flushing the REST cache. |
|---|
| 626 | * |
|---|
| 627 | * @return void |
|---|
| 628 | */ |
|---|
| 629 | public function deactivated_plugin() { |
|---|
| 630 | $this->add_notice( |
|---|
| 631 | 'warning', |
|---|
| 632 | __( 'A plugin has been deactivated. This might effect the WP REST API output, so please consider if clearing the REST Cache is necessary.', 'wp-rest-cache' ), |
|---|
| 633 | false, |
|---|
| 634 | [ |
|---|
| 635 | 'label' => __( 'Clear REST cache', 'wp-rest-cache' ), |
|---|
| 636 | 'url' => self::empty_cache_url(), |
|---|
| 637 | ] |
|---|
| 638 | ); |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | /** |
|---|
| 642 | * Add custom WP CLI commands. |
|---|
| 643 | * |
|---|
| 644 | * @throws \Exception An exception is thrown if the command contains an error. |
|---|
| 645 | * |
|---|
| 646 | * @return void |
|---|
| 647 | */ |
|---|
| 648 | public function add_cli_commands() { |
|---|
| 649 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|---|
| 650 | \WP_CLI::add_command( 'wp-rest-cache', new \WP_Rest_Cache_Plugin\Includes\CLI\Flush_Command() ); |
|---|
| 651 | } |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | /** |
|---|
| 655 | * Filter the settings panels for the WP REST Cache settings page. |
|---|
| 656 | * |
|---|
| 657 | * @param array<string,array<string,mixed>> $panels The settings panels for the WP REST Cache settings page. |
|---|
| 658 | * |
|---|
| 659 | * @return array<string,array<string,mixed>> |
|---|
| 660 | */ |
|---|
| 661 | public function filter_settings_panels( $panels ) { |
|---|
| 662 | $settings_panels = [ |
|---|
| 663 | 'settings' => [ 'label' => __( 'Settings', 'wp-rest-cache' ) ], |
|---|
| 664 | 'endpoint-api' => [ 'label' => __( 'Endpoint API Caches', 'wp-rest-cache' ) ], |
|---|
| 665 | 'clear-cache' => [ 'label' => __( 'Clear Caches', 'wp-rest-cache' ) ], |
|---|
| 666 | ]; |
|---|
| 667 | |
|---|
| 668 | return array_merge( $panels, $settings_panels ); |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | /** |
|---|
| 672 | * Display a message about the Pro version of the plugin. |
|---|
| 673 | * |
|---|
| 674 | * @return void |
|---|
| 675 | */ |
|---|
| 676 | public function display_pro_message() { |
|---|
| 677 | $this->add_notice( |
|---|
| 678 | 'success', |
|---|
| 679 | __( 'Did you know that with the new Pro version of the WP REST Cache plugin you can easily configure custom endpoints to be cached?', 'wp-rest-cache' ), |
|---|
| 680 | 'permanent', |
|---|
| 681 | [ |
|---|
| 682 | 'label' => __( 'Upgrade to Pro', 'wp-rest-cache' ), |
|---|
| 683 | 'url' => 'https://plugins.acato.nl/', |
|---|
| 684 | 'class' => 'button button-primary', |
|---|
| 685 | ] |
|---|
| 686 | ); |
|---|
| 687 | } |
|---|
| 688 | } |
|---|