diff --git a/cachify.php b/cachify.php index e98a6af..aa7a397 100644 --- a/cachify.php +++ b/cachify.php @@ -91,7 +91,7 @@ * @param string $class_name the class name. */ function cachify_autoload( $class_name ) { - if ( in_array( $class_name, array( 'Cachify', 'Cachify_Backend', 'Cachify_CLI', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_REDIS' ), true ) ) { + if ( in_array( $class_name, array( 'Cachify', 'Cachify_Backend', 'Cachify_CLI', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_NOOP', 'Cachify_REDIS' ), true ) ) { require_once sprintf( '%s/inc/class-%s.php', CACHIFY_DIR, diff --git a/images/symbols.svg b/images/symbols.svg index e437576..1f34522 100644 --- a/images/symbols.svg +++ b/images/symbols.svg @@ -4,14 +4,14 @@ - - - - + + + + diff --git a/inc/class-cachify-noop.php b/inc/class-cachify-noop.php new file mode 100644 index 0000000..62634f9 --- /dev/null +++ b/inc/class-cachify-noop.php @@ -0,0 +1,112 @@ +unavailable_method = $unavailable_method; + } + + /** + * Availability check + * + * @return bool TRUE when installed + */ + public static function is_available() { + return true; + } + + /** + * Caching method as string + * + * @return string Caching method + */ + public static function stringify_method() { + return 'NOOP'; + } + + /** + * Store item in cache + * + * @param string $hash Hash of the entry. + * @param string $data Content of the entry. + * @param int $lifetime Lifetime of the entry. + * @param bool $sig_detail Show details in signature. + */ + public static function store_item( $hash, $data, $lifetime, $sig_detail ) { + // NOOP. + } + + /** + * Read item from cache + * + * @param string $hash Hash of the entry. + * + * @return false No content + */ + public static function get_item( $hash ) { + return false; + } + + /** + * Delete item from cache + * + * @param string $hash Hash of the entry. + * @param string $url URL of the entry [optional]. + */ + public static function delete_item( $hash, $url = '' ) { + // NOOP. + } + + /** + * Clear the cache + */ + public static function clear_cache() { + // NOOP. + } + + /** + * Print the cache + * + * @param bool $sig_detail Show details in signature. + * @param array $cache Array of cache values. + */ + public static function print_cache( $sig_detail, $cache ) { + // NOOP. + } + + /** + * Get the cache size + * + * @return int Column size + */ + public static function get_stats() { + return 0; + } +} diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 85eb658..debe51c 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -389,24 +389,74 @@ private static function _set_default_vars() { /* Options */ self::$options = self::_get_options(); - /* HDD */ - if ( self::METHOD_HDD === self::$options['use_apc'] && Cachify_HDD::is_available() ) { - self::$method = new Cachify_HDD(); - - /* MEMCACHED */ - } elseif ( self::METHOD_MMC === self::$options['use_apc'] && Cachify_MEMCACHED::is_available() ) { - self::$method = new Cachify_MEMCACHED(); - - /* REDIS */ - } elseif ( self::METHOD_REDIS === self::$options['use_apc'] && Cachify_REDIS::is_available() ) { - self::$method = new Cachify_REDIS(); - - /* DB */ + if ( self::METHOD_APC === self::$options['use_apc'] ) { + /* APC */ + add_action( 'admin_notices', array( __CLASS__, 'admin_notice_unavailable' ) ); + self::$method = new Cachify_NOOP( 'APC' ); + } elseif ( self::METHOD_HDD === self::$options['use_apc'] ) { + /* HDD */ + if ( Cachify_HDD::is_available() ) { + self::$method = new Cachify_HDD(); + } else { + add_action( 'admin_notices', array( __CLASS__, 'admin_notice_unavailable' ) ); + self::$method = new Cachify_NOOP( Cachify_HDD::stringify_method() ); + } + } elseif ( self::METHOD_MMC === self::$options['use_apc'] ) { + /* Memcached */ + if ( Cachify_MEMCACHED::is_available() ) { + self::$method = new Cachify_MEMCACHED(); + } else { + add_action( 'admin_notices', array( __CLASS__, 'admin_notice_unavailable' ) ); + self::$method = new Cachify_NOOP( Cachify_MEMCACHED::stringify_method() ); + } + } elseif ( self::METHOD_REDIS === self::$options['use_apc'] ) { + /* Redis */ + if ( Cachify_REDIS::is_available() ) { + self::$method = new Cachify_REDIS(); + } else { + add_action( 'admin_notices', array( __CLASS__, 'admin_notice_unavailable' ) ); + self::$method = new Cachify_NOOP( Cachify_REDIS::stringify_method() ); + } } else { + /* Database */ self::$method = new Cachify_DB(); } } + /** + * Show admin notice if caching backend is unavailable. + * + * @since 2.4.0 + */ + public static function admin_notice_unavailable() { + if ( current_user_can( 'manage_options' ) ) { + $unavailable_method = '-'; + if ( self::$method instanceof Cachify_NOOP ) { + $unavailable_method = self::$method->unavailable_method; + } + + printf( + '

%1$s

%2$s

%3$s

', + esc_html__( 'Cachify backend not available', 'cachify' ), + esc_html( + sprintf( + /* translators: Name of the caching backend inserted for placeholder */ + __( 'The configured caching backend is not available: %s', 'cachify' ), + $unavailable_method + ) + ), + wp_kses( + sprintf( + /* translators: Link to Cachify settings page inserted at placeholder */ + __( 'Please check your server configuration and visit the settings page to chose a different method.', 'cachify' ), + add_query_arg( array( 'page' => 'cachify' ), admin_url( 'options-general.php' ) ) + ), + array( 'a' => array( 'href' => array() ) ) + ) + ); + } + } + /** * Get options * diff --git a/tests/test-cachify-noop.php b/tests/test-cachify-noop.php new file mode 100644 index 0000000..92d0353 --- /dev/null +++ b/tests/test-cachify-noop.php @@ -0,0 +1,58 @@ +unavailable_method, 'unexpected name of unavailable method' ); + $noop = new Cachify_NOOP(); + self::assertSame( '', $noop->unavailable_method, 'unexpected default name of unavailable method' ); + } + + /** + * Test the actual caching. + */ + public function test_caching() { + self::go_to( '/testme/' ); + Cachify_NOOP::store_item( + '965b4abf2414e45036ab90c9d3f8dbc7', + 'Test Me

Test Content.

', + 3600, + false + ); + self::assertFalse( + Cachify_NOOP::get_item('965b4abf2414e45036ab90c9d3f8dbc7'), + "item should not have been stored" + ); + + Cachify_NOOP::delete_item( '965b4abf2414e45036ab90c9d3f8dbc7' ); + self::assertFalse( + Cachify_NOOP::get_item('965b4abf2414e45036ab90c9d3f8dbc7'), + "item present after deletion" + ); + } +}