id, 'gorilla-cache' ) === false ) { return; } wp_enqueue_style( 'gorilla-cache-admin', GORILLA_CACHE_URL . 'css/admin.css', array(), self::asset_version( 'css/admin.css' ), 'all' ); } /** * The version a stylesheet or script is loaded with: the plugin's version and a fingerprint of the file, so a * browser or CDN that keeps files for years fetches it again whenever it changes, even within one version. * * @param string $file Path inside the plugin. * @return string */ private static function asset_version( $file ) { $path = GORILLA_CACHE_PATH . $file; return GORILLA_CACHE_VERSION . ( is_readable( $path ) ? '-' . substr( (string) md5_file( $path ), 0, 8 ) : '' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function enqueue_scripts() { // Only enqueue on our admin page. $screen = get_current_screen(); if ( ! $screen || strpos( $screen->id, 'gorilla-cache' ) === false ) { return; } wp_enqueue_script( 'gorilla-cache-admin', GORILLA_CACHE_URL . 'js/admin.js', array( 'jquery' ), self::asset_version( 'js/admin.js' ), false ); // The script's own words, translated. wp_localize_script( 'gorilla-cache-admin', 'gorillaCache', array( 'copied' => __( 'Copied!', 'gorilla-cache' ), 'copyFailed' => __( 'Copying did not work. Select the text and copy it by hand.', 'gorilla-cache' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'statusNonce' => wp_create_nonce( 'gorilla_cache_status' ), ) ); } /** * The Status tab's two cards, freshly rendered, so the open tab keeps its numbers and warming progress current * without a reload. Each request also lets WordPress run its scheduled tasks, so warming moves on while the tab * is open. */ public function ajax_status() { check_ajax_referer( 'gorilla_cache_status' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } $gorilla_cache_cards_only = true; ob_start(); try { include plugin_dir_path( __FILE__ ) . 'views/status.php'; } finally { $html = (string) ob_get_clean(); } wp_send_json_success( array( 'html' => $html, 'warming' => in_array( GorillaCache_Preload::summary(), array( 'starting', 'warming' ), true ), ) ); } /** * Add menu item for this plugin. * * Menu location is determined by GORILLA_CACHE_MENU_LOCATION constant. * Supports: 'top-level', 'settings', 'tools', 'woocommerce'. * * @since 1.0.0 */ public function add_plugin_admin_menu() { $menu_location = defined( 'GORILLA_CACHE_MENU_LOCATION' ) ? GORILLA_CACHE_MENU_LOCATION : 'top-level'; switch ( $menu_location ) { case 'settings': add_options_page( __( 'Gorilla Cache Settings', 'gorilla-cache' ), __( 'Gorilla Cache', 'gorilla-cache' ), 'manage_options', 'gorilla-cache', array( $this, 'display_plugin_admin_page' ) ); break; case 'tools': add_management_page( __( 'Gorilla Cache', 'gorilla-cache' ), __( 'Gorilla Cache', 'gorilla-cache' ), 'manage_options', 'gorilla-cache', array( $this, 'display_plugin_admin_page' ) ); break; case 'woocommerce': // WooCommerce's menu exists only when WooCommerce is loaded, and every active plugin has loaded by now. if ( class_exists( 'WooCommerce' ) ) { add_submenu_page( 'woocommerce', __( 'Gorilla Cache', 'gorilla-cache' ), __( 'Gorilla Cache', 'gorilla-cache' ), 'manage_options', 'gorilla-cache', array( $this, 'display_plugin_admin_page' ) ); } else { // Fallback to top-level if WooCommerce not active. $this->add_top_level_menu(); } break; case 'top-level': default: $this->add_top_level_menu(); break; } } /** * Add top-level menu page. * * @since 1.0.0 * @access private */ private function add_top_level_menu() { add_menu_page( __( 'Gorilla Cache Settings', 'gorilla-cache' ), __( 'Gorilla Cache', 'gorilla-cache' ), 'manage_options', 'gorilla-cache', array( $this, 'display_plugin_admin_page' ), 'dashicons-admin-generic', 100 ); } /** * Get the admin page URL based on menu location. * * @since 1.0.0 * @param string $hash Optional. URL hash fragment (e.g., '#help'). * @return string The admin page URL. */ public function get_admin_page_url( $hash = '' ) { return gorilla_cache_get_admin_url( $hash ); } /** * Add settings link to the plugin page. * * @since 1.0.0 * @param array $links Array of plugin action links. * @return array Modified array of plugin action links. */ public function add_settings_link( $links ) { $settings_link = '' . esc_html__( 'Settings', 'gorilla-cache' ) . ''; array_unshift( $links, $settings_link ); return $links; } /** * Whether caching is on, beside Gorilla Cache in the plugin list: the owner sees it without opening the settings. * * @param string[] $meta Row links and text. * @param string $file Plugin file. * @return string[] */ public function plugin_row_status( $meta, $file ) { if ( plugin_basename( GORILLA_CACHE_FILE ) !== $file || ! current_user_can( 'manage_options' ) ) { return $meta; } $on = GorillaCache_Installer::is_caching(); $text = $on ? __( 'Caching is on', 'gorilla-cache' ) : __( 'Caching is off: see why', 'gorilla-cache' ); $text = ! $on && GorillaCache_Site::paused() ? __( 'Caching is off: turned off by you', 'gorilla-cache' ) : $text; $text = ! $on && defined( 'GORILLA_CACHE_PAUSED' ) && GORILLA_CACHE_PAUSED ? __( 'Caching is off: turned off in wp-config.php', 'gorilla-cache' ) : $text; $meta[] = '' . esc_html( $text ) . ''; return $meta; } /** * On the Plugins screen only, a warning when caching is held back by something the owner must fix (a file that * cannot be written, a WP_CACHE line). It says how to fix it and disappears once it is fixed. A stand-down for * another cache is not a problem and shows no notice (the plugin list says "Caching is off"). */ public function problem_notice() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'plugins' !== $screen->id || ! current_user_can( 'activate_plugins' ) ) { return; } $state = GorillaCache_Installer::state(); $problems = array( 'wp_config_unwritable', 'wp_config_missing', 'wp_config_foreign_false', 'wp_cache_off_at_runtime', 'cache_dir_unwritable', 'dropin_unwritable' ); if ( 'on' !== $state['verdict'] || ! in_array( $state['reason'], $problems, true ) ) { return; } printf( '
%1$s %2$s %4$s
'; if ( '' !== $move['from'] && $move['from'] !== $move['to'] ) { /* translators: 1: the site's address now, 2: its address before. */ echo esc_html( sprintf( __( 'Gorilla Cache noticed that this site now runs at %1$s (before: %2$s) and cleared its cache, so no page from the other copy is shown.', 'gorilla-cache' ), $move['to'], $move['from'] ) ); } else { echo esc_html__( 'Gorilla Cache noticed that this site now runs on another server, database or environment and cleared its cache, so no page from the other copy is shown.', 'gorilla-cache' ); } if ( GorillaCache_Installer::is_caching() && GorillaCache_Preload::enabled() ) { echo ' ' . esc_html__( 'It is warming this site\'s own pages again.', 'gorilla-cache' ); } if ( $move['copy'] && ! GorillaCache_Site::paused() ) { /* translators: %s: the site's address. */ echo ' ' . esc_html( sprintf( __( 'This looks like a staging or development copy of your site (%s). Logged-in users always see fresh pages, but a logged-out preview can show a cached page for a while, so you may want to turn caching off here.', 'gorilla-cache' ), $move['to'] ) ); } echo '
'; if ( $move['copy'] && ! GorillaCache_Site::paused() && current_user_can( 'activate_plugins' ) ) { echo '' . esc_html__( 'Turn caching off here', 'gorilla-cache' ) . ' '; } echo '' . esc_html__( 'Dismiss', 'gorilla-cache' ) . ''; echo '