Settings on Multisite, Tools on a single site (read-only per site on Multisite). */ final class ELOOC_Admin { /** * Page slug. */ private const SLUG = 'elooc-cache-status'; /** * Tabs: OPcache first, then one per object cache backend. */ private const TABS = array( 'opcache', 'redis', 'memcached', 'apcu', 'file' ); /** * Register hooks. * * @return void */ public static function init(): void { add_action( 'admin_menu', array( __CLASS__, 'register_menu' ) ); add_action( 'network_admin_menu', array( __CLASS__, 'register_network_menu' ) ); add_action( 'admin_post_elooc_action', array( __CLASS__, 'handle' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) ); // After the menus exist, so core can report the real screen IDs, whatever the language. add_action( 'admin_menu', array( __CLASS__, 'register_donate_notice' ), 99 ); add_action( 'network_admin_menu', array( __CLASS__, 'register_donate_notice' ), 99 ); } /** * Show the donate notice on the plugin page, under Tools or in Network Admin. * * @return void */ public static function register_donate_notice(): void { static $done = false; if ( $done ) { return; } $done = true; new ELOOC_Donate_Notice( ELOOC_FILE, array( get_plugin_page_hookname( self::SLUG, 'tools.php' ), get_plugin_page_hookname( self::SLUG, 'settings.php' ), ) ); } /** * State of the drop-in file, read once per request. * * @var string|null */ private static ?string $dropin_state = null; /** * State of wp-content/object-cache.php: none, ours or foreign. * * @return string */ private static function dropin_state(): string { if ( null === self::$dropin_state ) { self::$dropin_state = ELOOC_Dropin::state(); } return self::$dropin_state; } /** * Add the page under Tools (read-only on Multisite). * * @return void */ public static function register_menu(): void { add_management_page( __( 'EffortLess Opcode and Object Cache', 'effortless-opcode-and-object-cache' ), __( 'Opcode and Object Cache', 'effortless-opcode-and-object-cache' ), 'manage_options', self::SLUG, array( __CLASS__, 'render' ) ); } /** * Add the page under Network Admin > Settings. * * @return void */ public static function register_network_menu(): void { add_submenu_page( 'settings.php', __( 'EffortLess Opcode and Object Cache', 'effortless-opcode-and-object-cache' ), __( 'Opcode and Object Cache', 'effortless-opcode-and-object-cache' ), 'manage_network_options', self::SLUG, array( __CLASS__, 'render' ) ); } /** * Load the stylesheet on our page. * * @param string $hook Hook suffix. * @return void */ public static function enqueue_assets( string $hook ): void { if ( false === strpos( $hook, self::SLUG ) ) { return; } wp_enqueue_style( 'elooc-admin', plugins_url( 'assets/css/admin.css', ELOOC_FILE ), array(), ELOOC_VERSION ); } /** * Whether the current user may change things (not just look). * * @return bool */ private static function can_manage(): bool { return current_user_can( ELOOC_Dropin::capability() ); } /** * URL of the page where the settings are managed. * * @param string $tab Tab name. * @return string */ private static function manage_url( string $tab = 'opcache' ): string { $args = array( 'page' => self::SLUG, 'tab' => $tab, ); return add_query_arg( $args, is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' ) ); } /** * Names of the tabs. * * @return array */ private static function tab_labels(): array { return array( 'opcache' => __( 'OPcache', 'effortless-opcode-and-object-cache' ), 'redis' => 'Redis', 'memcached' => 'Memcached', 'apcu' => 'APCu', 'file' => __( 'Files', 'effortless-opcode-and-object-cache' ), ); } /** * Keep a message for the next page load of this user. * * @param string $type success or error. * @param string $message Text. * @return void */ private static function flash( string $type, string $message ): void { // User meta, not a transient: transients live in the object cache once one is active, and these // actions install, remove or re-key it. update_user_meta( get_current_user_id(), 'elooc_flash', array( $type, $message ) ); } /** * Take the stored message, if any. * * @return array{0:string,1:string}|null */ private static function take_flash(): ?array { $value = get_user_meta( get_current_user_id(), 'elooc_flash', true ); if ( ! is_array( $value ) || 2 !== count( $value ) ) { return null; } delete_user_meta( get_current_user_id(), 'elooc_flash' ); return array( (string) $value[0], (string) $value[1] ); } /** * Status badge. * * @param string $css_class CSS class: elooc-ok, elooc-bad, elooc-warn or elooc-off. * @param string $text Label. * @return void */ private static function badge( string $css_class, string $text ): void { printf( '%s', esc_attr( $css_class ), esc_html( $text ) ); } /** * Render the page. * * @return void */ public static function render(): void { if ( ! current_user_can( 'manage_options' ) && ! self::can_manage() ) { wp_die( esc_html__( 'You do not have permission to view this page.', 'effortless-opcode-and-object-cache' ) ); } // The tab only selects what is shown. // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tab = isset( $_GET['tab'] ) && is_string( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'opcache'; $tab = in_array( $tab, self::TABS, true ) ? $tab : 'opcache'; $manage = self::can_manage() && ( ! is_multisite() || is_network_admin() ); $flash = self::take_flash(); $labels = self::tab_labels(); ?>

'; } } /** * Whether the installed drop-in is ours and uses this backend. * * @param string $backend Backend name. * @return bool */ private static function backend_active( string $backend ): bool { return 'ours' === self::dropin_state() && ELOOC_Config::get()['installed'] === $backend; } /** * A form that carries the nonce, action and tab. * * @param string $tab Tab to return to. * @return void */ private static function form_open( string $tab ): void { ?>
%3$s ', esc_attr( $css_class ), esc_attr( $action ), esc_html( $label ) ); } /** * OPcache tab. * * @param array $opcache Status. * @param bool $manage Whether the user may change things. * @return void */ private static function render_opcache( array $opcache, bool $manage ): void { $ini_value = ELOOC_User_Ini::block_value(); ?>

$value ) : ?>

zend_extension=opcache
opcache.enable=1

elooc_info() : array(); ?>

403 ) ); } check_admin_referer( 'elooc_action', 'elooc_nonce' ); self::$dropin_state = null; $do = isset( $_POST['elooc_do'] ) && is_string( $_POST['elooc_do'] ) ? sanitize_key( wp_unslash( $_POST['elooc_do'] ) ) : ''; $tab = isset( $_POST['elooc_tab'] ) && is_string( $_POST['elooc_tab'] ) ? sanitize_key( wp_unslash( $_POST['elooc_tab'] ) ) : 'opcache'; $tab = in_array( $tab, self::TABS, true ) ? $tab : 'opcache'; $backend = isset( $_POST['elooc_backend'] ) && is_string( $_POST['elooc_backend'] ) ? sanitize_key( wp_unslash( $_POST['elooc_backend'] ) ) : ''; // ELOOC_Config::sanitize() checks the values again and keeps the stored ones for fields that are not sent. $has_form = isset( $_POST['elooc_settings'] ) && is_array( $_POST['elooc_settings'] ); $form = array(); foreach ( array( 'redis_host', 'memcached_host' ) as $field ) { if ( $has_form && isset( $_POST['elooc_settings'][ $field ] ) && is_string( $_POST['elooc_settings'][ $field ] ) ) { $form[ $field ] = sanitize_text_field( wp_unslash( $_POST['elooc_settings'][ $field ] ) ); } } foreach ( array( 'redis_port', 'redis_db', 'memcached_port', 'ttl_days' ) as $field ) { if ( $has_form && isset( $_POST['elooc_settings'][ $field ] ) && is_string( $_POST['elooc_settings'][ $field ] ) ) { $form[ $field ] = (string) absint( wp_unslash( $_POST['elooc_settings'][ $field ] ) ); } } if ( $has_form && ! empty( $_POST['elooc_settings']['redis_password_clear'] ) ) { $form['redis_password_clear'] = '1'; } elseif ( $has_form && isset( $_POST['elooc_settings']['redis_password'] ) && is_string( $_POST['elooc_settings']['redis_password'] ) ) { $form['redis_password'] = wp_unslash( $_POST['elooc_settings']['redis_password'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- A password must be kept byte for byte; any cleaning would change it. } $backend_actions = array( 'activate', 'deactivate', 'test', 'flush' ); if ( in_array( $do, $backend_actions, true ) && ! in_array( $backend, array( 'redis', 'memcached', 'apcu', 'file' ), true ) ) { self::flash( 'error', __( 'Unknown cache.', 'effortless-opcode-and-object-cache' ) ); self::finish( $tab ); } // The submitted settings are only tried here; they are stored once the server has answered. $settings = in_array( $do, array( 'activate', 'test' ), true ) ? ELOOC_Config::sanitize( $form ) : ELOOC_Config::get(); $names = self::tab_labels(); switch ( $do ) { case 'activate': if ( ! ELOOC_Tester::extension_loaded( $backend ) ) { /* translators: %s: backend name. */ self::flash( 'error', sprintf( __( 'The PHP extension for %s is not loaded, so it cannot be activated.', 'effortless-opcode-and-object-cache' ), $names[ $backend ] ) ); break; } $settings['backend'] = $backend; $result = ELOOC_Dropin::install( $settings ); if ( is_wp_error( $result ) ) { self::flash( 'error', $result->get_error_message() ); } else { /* translators: %s: backend name. */ self::flash( 'success', sprintf( __( '%s is active. It takes effect on the next page load.', 'effortless-opcode-and-object-cache' ), $names[ $result ] ?? $result ) ); } break; case 'deactivate': if ( ! self::backend_active( $backend ) ) { self::flash( 'error', __( 'This cache is not the active one.', 'effortless-opcode-and-object-cache' ) ); break; } $result = ELOOC_Dropin::remove(); self::flash( is_wp_error( $result ) ? 'error' : 'success', is_wp_error( $result ) ? $result->get_error_message() : __( 'The object cache was deactivated and the drop-in removed.', 'effortless-opcode-and-object-cache' ) ); break; case 'test': $result = ELOOC_Tester::test( $backend, $settings ); if ( true === $result ) { ELOOC_Config::update( $settings ); /* translators: %s: backend name. */ self::flash( 'success', sprintf( __( '%s works: a test value was stored and read back. The settings were saved.', 'effortless-opcode-and-object-cache' ), $names[ $backend ] ) ); } else { self::flash( 'error', $result . ' ' . __( 'The settings were not saved.', 'effortless-opcode-and-object-cache' ) ); } break; case 'flush': if ( self::backend_active( $backend ) ) { if ( wp_cache_flush() ) { self::flash( 'success', __( 'The object cache was flushed.', 'effortless-opcode-and-object-cache' ) ); } else { self::flash( 'error', __( 'The cache server did not accept the flush. It is flushed on the next request that reaches the server.', 'effortless-opcode-and-object-cache' ) ); } } else { self::flash( 'error', __( 'This cache is not the active one.', 'effortless-opcode-and-object-cache' ) ); } break; case 'opcache_off': if ( ! ELOOC_Status::opcache()['loaded'] ) { self::flash( 'error', __( 'The OPcache extension is not loaded, so it cannot be switched.', 'effortless-opcode-and-object-cache' ) ); break; } $result = ELOOC_User_Ini::add( false ); self::flash( is_wp_error( $result ) ? 'error' : 'success', is_wp_error( $result ) ? $result->get_error_message() : __( 'The line was written to .user.ini. PHP rereads the file about every five minutes; reload this page then.', 'effortless-opcode-and-object-cache' ) ); break; case 'opcache_remove': $result = ELOOC_User_Ini::remove(); self::flash( is_wp_error( $result ) ? 'error' : 'success', is_wp_error( $result ) ? $result->get_error_message() : __( 'The line was removed from .user.ini.', 'effortless-opcode-and-object-cache' ) ); break; default: self::flash( 'error', __( 'Unknown action.', 'effortless-opcode-and-object-cache' ) ); } self::finish( $tab ); } /** * Redirect back to the tab. * * @param string $tab Tab name. * @return void */ private static function finish( string $tab ): void { wp_safe_redirect( is_multisite() ? self::manage_url( $tab ) : add_query_arg( array( 'page' => self::SLUG, 'tab' => $tab, ), admin_url( 'tools.php' ) ) ); exit; } }