'array', 'sanitize_callback' => array( Settings::class, 'sanitize' ), 'default' => Settings::defaults(), ) ); } public function action_links( $links ) { $url = admin_url( 'options-general.php?page=' . self::PAGE ); array_unshift( $links, '' . esc_html__( 'Settings', 'cache-shark' ) . '' ); return $links; } /* ---------- Actions (admin-post.php) ---------- */ public function handle_purge(): void { $this->guard( 'cache_shark_purge' ); CacheStore::purge_all(); do_action( 'cache_shark_purged_all' ); // Lets the preloader re-warm the cache. $this->back( 'purged' ); } public function handle_preload(): void { $this->guard( 'cache_shark_preload' ); ( new Preloader() )->start(); $this->back( 'preloading' ); } /** Every state-changing request needs a capability check AND a nonce. */ private function guard( string $action ): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You do not have permission to do that.', 'cache-shark' ), '', array( 'response' => 403 ) ); } check_admin_referer( $action ); } private function back( string $notice ): void { $fallback = admin_url( 'options-general.php?page=' . self::PAGE ); $target = wp_get_referer() ? wp_get_referer() : $fallback; wp_safe_redirect( add_query_arg( 'cs_notice', $notice, $target ) ); exit; } /* ---------- Admin bar ---------- */ public function admin_bar( $bar ): void { if ( ! current_user_can( self::CAPABILITY ) ) { return; } $bar->add_node( array( 'id' => 'cache-shark', 'title' => esc_html__( 'Clear cache', 'cache-shark' ), 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=cache_shark_purge' ), 'cache_shark_purge' ), ) ); } /* ---------- Notices ---------- */ public function notices(): void { if ( ! current_user_can( self::CAPABILITY ) ) { return; } $messages = array( 'purged' => __( 'Cache cleared.', 'cache-shark' ), 'preloading' => __( 'Preloading started. Pages are warmed in the background a few at a time.', 'cache-shark' ), ); // phpcs:ignore WordPress.Security.NonceVerification -- read-only, whitelisted key, output escaped. $key = isset( $_GET['cs_notice'] ) ? sanitize_key( wp_unslash( $_GET['cs_notice'] ) ) : ''; if ( isset( $messages[ $key ] ) ) { echo '
' . esc_html( $messages[ $key ] ) . '
' . esc_html__( 'Cache Shark:', 'cache-shark' ) . ' ' . wp_kses( $problem, array( 'code' => array() ) ) . '
). */
private function problems(): array {
$s = Installer::status();
$out = array();
if ( 'foreign' === $s['dropin'] ) {
$out[] = esc_html__( 'Another plugin already uses wp-content/advanced-cache.php, so page caching is paused. Deactivate the other caching plugin, then deactivate and reactivate Cache Shark.', 'cache-shark' );
} elseif ( 'missing' === $s['dropin'] ) {
$out[] = esc_html__( 'The cache drop-in could not be installed. Make sure wp-content is writable, then deactivate and reactivate the plugin.', 'cache-shark' );
}
if ( ! $s['wp_cache'] ) {
$out[] = esc_html__( 'WP_CACHE is not enabled. Add this line to wp-config.php, above the line that says "That\'s all, stop editing":', 'cache-shark' ) . " define( 'WP_CACHE', true );";
}
if ( ! $s['writable'] ) {
$out[] = esc_html__( 'The cache folder wp-content/cache/cache-shark is not writable.', 'cache-shark' );
}
return $out;
}
/* ---------- Settings page ---------- */
public function render_page(): void {
if ( ! current_user_can( self::CAPABILITY ) ) {
return;
}
$o = Settings::get();
$name = Settings::OPTION;
$stats = CacheStore::stats();
$s = Installer::status();
?>