if ( ! $this->initialize_filesystem( '', true ) ) { return new WP_Error( 'fs', __( 'Could not initialize filesystem.', 'redis-cache' ) ); } $dropin_check = ! defined( 'WP_REDIS_DISABLE_DROPIN_CHECK' ) || ! WP_REDIS_DISABLE_DROPIN_CHECK; if ( ! $dropin_check ) { if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { return true; } if ( ! $wp_filesystem->is_writable( WP_CONTENT_DIR . '/object-cache.php' ) ) { return new WP_Error( 'writable', __( 'Object cache drop-in is not writable.', 'redis-cache' ) ); } return true; } $cachefile = WP_REDIS_PLUGIN_PATH . '/includes/object-cache.php'; $testfile = WP_CONTENT_DIR . '/object-cache.tmp'; if ( ! $wp_filesystem->exists( $cachefile ) ) { return new WP_Error( 'exists', __( 'Object cache file doesn’t exist.', 'redis-cache' ) ); } if ( $wp_filesystem->exists( $testfile ) ) { if ( ! $wp_filesystem->delete( $testfile ) ) { return new WP_Error( 'delete', __( 'Test file exists, but couldn’t be deleted.', 'redis-cache' ) ); } } if ( ! $wp_filesystem->is_writable( WP_CONTENT_DIR ) ) { return new WP_Error( 'writable', __( 'Content directory is not writable.', 'redis-cache' ) ); } if ( ! $wp_filesystem->copy( $cachefile, $testfile, true, FS_CHMOD_FILE ) ) { return new WP_Error( 'copy', __( 'Failed to copy test file.', 'redis-cache' ) ); } if ( ! $wp_filesystem->exists( $testfile ) ) { return new WP_Error( 'exists', __( 'Copied test file doesn’t exist.', 'redis-cache' ) ); } $meta = get_file_data( $testfile, [ 'Version' => 'Version' ] ); if ( $meta['Version'] !== WP_REDIS_VERSION ) { return new WP_Error( 'version', __( 'Couldn’t verify test file contents.', 'redis-cache' ) ); } if ( ! $wp_filesystem->delete( $testfile ) ) { return new WP_Error( 'delete', __( 'Copied test file couldn’t be deleted.', 'redis-cache' ) ); } return true; } /** * Calls the drop-in update method if necessary * * @return void */ public function maybe_update_dropin() { if ( defined( 'WP_REDIS_DISABLE_DROPIN_AUTOUPDATE' ) && WP_REDIS_DISABLE_DROPIN_AUTOUPDATE ) { return; } if ( $this->object_cache_dropin_outdated() ) { add_action( 'shutdown', [ $this, 'update_dropin' ] ); } } /** * Redirects to the plugin settings if the plugin was just activated * * @return void */ public function maybe_redirect() { if ( ! get_transient( '_rediscache_activation_redirect' ) ) { return; } if ( ! $this->current_user_can_manage_redis() ) { return; } delete_transient( '_rediscache_activation_redirect' ); wp_safe_redirect( network_admin_url( $this->page ) ); } /** * Updates the `object-cache.php` drop-in * * @return void */ public function update_dropin() { global $wp_filesystem; if ( ! $this->validate_object_cache_dropin() ) { return; } if ( $this->initialize_filesystem( '', true ) ) { $result = $wp_filesystem->copy( WP_REDIS_PLUGIN_PATH . '/includes/object-cache.php', WP_CONTENT_DIR . '/object-cache.php', true, FS_CHMOD_FILE ); /** * Fires on cache update-dropin event * * @since 1.3.5 * @param bool $result Whether the filesystem event (copy of the `object-cache.php` file) was successful. */ do_action( 'redis_object_cache_update_dropin', $result ); } } /** * Plugin activation hook * * @return void */ public static function on_activation() { set_transient( '_rediscache_activation_redirect', 1, 30 ); } /** * Plugin deactivation hook * * @param string $plugin Plugin basename. * @return void */ public function on_deactivation( $plugin ) { global $wp_filesystem; ob_start(); if ( $plugin === WP_REDIS_BASENAME ) { $timestamp = wp_next_scheduled( 'rediscache_discard_metrics' ); if ( $timestamp ) { wp_unschedule_event( $timestamp, 'rediscache_discard_metrics' ); } (new Predis)->flush(); if ( $this->validate_object_cache_dropin() && $this->initialize_filesystem( '', true ) ) { $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' ); } } ob_end_clean(); } /** * Helper method to retrieve a nonced plugin action link * * @param string $action The action to be executed once the link is followed. * @return string */ public function action_link( $action ) { if ( ! in_array( $action, $this->actions, true ) ) { return ''; } return wp_nonce_url( network_admin_url( add_query_arg( 'action', $action, $this->page ) ), $action ); } /** * Obscure `password` URL parameter. * * @param string $url * @return string */ public function obscure_url_secrets( $url ) { return preg_replace( '/password=[^&]+/', 'password=*****', (string) $url ); } /** * The capability required to manage Redis. * * @return string */ public function manage_redis_capability() { if ( defined( 'WP_REDIS_MANAGER_CAPABILITY' ) && WP_REDIS_MANAGER_CAPABILITY ) { return WP_REDIS_MANAGER_CAPABILITY; } $capability = is_multisite() ? 'manage_network_options' : 'manage_options'; /** * Filters the capability used to determine if a user can manage Redis. * * @since 2.6.0 * @param string $capability The default capability to determine if the user can manage cache. */ return apply_filters( 'redis_cache_manager_capability', $capability ); } /** * Does the current user have the capability to manage Redis? * * @return bool */ public function current_user_can_manage_redis() { return current_user_can( $this->manage_redis_capability() ); } /** * Prevent LiteSpeed Cache from overwriting the `object-cache.php` drop-in. * * @return void */ public function litespeed_disable_objectcache() { // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( isset( $_POST['LSCWP_CTRL'], $_POST['LSCWP_NONCE'], $_POST['object'] ) ) { $_POST['object'] = '0'; } } /** * Returns `true` if the plugin was installed by AccelerateWP from CloudLinux. * * @param bool $ignore_banner_constant * @return bool */ public static function acceleratewp_install( $ignore_banner_constant = false ) { $path = defined( 'WP_REDIS_PATH' ) ? WP_REDIS_PATH : null; $scheme = defined( 'WP_REDIS_SCHEME' ) ? WP_REDIS_SCHEME : null; if ( $scheme === 'unix' && strpos( (string) $path, '.clwpos/redis.sock' ) !== false ) { if ( $ignore_banner_constant ) { return true; } else { return defined( 'WP_REDIS_DISABLE_BANNERS' ) && WP_REDIS_DISABLE_BANNERS; } } return false; } }