From 125be547e6d8dca643ed480faf3ef74983939a0c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 10 Nov 2023 18:17:46 +0100 Subject: [PATCH 01/10] use `__DIR__` instead of `dirname( __FILE__ )` We require PHP >= 5.6, so we can use the "modern" syntax here. --- cachify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachify.php b/cachify.php index 3cba0d43..6ee36ada 100644 --- a/cachify.php +++ b/cachify.php @@ -38,7 +38,7 @@ /* Constants */ define( 'CACHIFY_FILE', __FILE__ ); -define( 'CACHIFY_DIR', dirname( __FILE__ ) ); +define( 'CACHIFY_DIR', __DIR__ ); define( 'CACHIFY_BASE', plugin_basename( __FILE__ ) ); define( 'CACHIFY_CACHE_DIR', WP_CONTENT_DIR . '/cache/cachify' ); From b99d44618987fdf7e095fa3ac892b183f5abbcb4 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 10 Nov 2023 18:21:21 +0100 Subject: [PATCH 02/10] remove parenthesis from require_once call --- inc/class-cachify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-cachify.php b/inc/class-cachify.php index b0d1f74b..a83ea754 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -780,7 +780,7 @@ public static function process_flush_request( $data ) { /* Load on demand */ if ( ! function_exists( 'is_plugin_active_for_network' ) ) { - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + require_once ABSPATH . 'wp-admin/includes/plugin.php'; } /* Flush cache */ From b2fe4499c70e04337b39812b434c539d81c02e5c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 10 Nov 2023 18:22:48 +0100 Subject: [PATCH 03/10] rename $class parameter of autoload function to $class_name --- cachify.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cachify.php b/cachify.php index 6ee36ada..fed79856 100644 --- a/cachify.php +++ b/cachify.php @@ -88,14 +88,14 @@ /** * Autoload the class. * - * @param string $class the class name. + * @param string $class_name the class name. */ -function cachify_autoload( $class ) { - if ( in_array( $class, array( 'Cachify', 'Cachify_APC', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_CLI' ), true ) ) { +function cachify_autoload( $class_name ) { + if ( in_array( $class_name, array( 'Cachify', 'Cachify_APC', 'Cachify_DB', 'Cachify_HDD', 'Cachify_MEMCACHED', 'Cachify_CLI' ), true ) ) { require_once sprintf( '%s/inc/class-%s.php', CACHIFY_DIR, - strtolower( str_replace( '_', '-', $class ) ) + strtolower( str_replace( '_', '-', $class_name ) ) ); } } From bc263e8a18fc4cd20e659a79eb7758d1e2bf15ca Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 11 Nov 2023 11:13:42 +0100 Subject: [PATCH 04/10] fix precision alignment, equals signs and double arrows --- inc/class-cachify-cli.php | 17 ++++---- inc/class-cachify-hdd.php | 21 ++++------ inc/class-cachify-memcached.php | 8 ++-- inc/class-cachify.php | 73 ++++++++++++++++----------------- 4 files changed, 57 insertions(+), 62 deletions(-) diff --git a/inc/class-cachify-cli.php b/inc/class-cachify-cli.php index 4dc81d4c..f050dac3 100644 --- a/inc/class-cachify-cli.php +++ b/inc/class-cachify-cli.php @@ -74,10 +74,10 @@ public static function add_commands() { 'shortdesc' => 'Flush site cache', 'synopsis' => array( array( - 'type' => 'flag', - 'name' => 'all-methods', - 'description' => 'Flush all caching methods', - 'optional' => true, + 'type' => 'flag', + 'name' => 'all-methods', + 'description' => 'Flush all caching methods', + 'optional' => true, ), ), ) @@ -94,14 +94,13 @@ public static function add_commands() { 'shortdesc' => 'Get the size of the cache in bytes', 'synopsis' => array( array( - 'type' => 'flag', - 'name' => 'raw', - 'description' => 'Raw size output in bytes', - 'optional' => true, + 'type' => 'flag', + 'name' => 'raw', + 'description' => 'Raw size output in bytes', + 'optional' => true, ), ), ) ); - } } diff --git a/inc/class-cachify-hdd.php b/inc/class-cachify-hdd.php index 8d521a1e..4c89e958 100644 --- a/inc/class-cachify-hdd.php +++ b/inc/class-cachify-hdd.php @@ -122,7 +122,8 @@ public static function clear_cache() { */ public static function print_cache() { $filename = self::_file_html(); - $size = is_readable( $filename ) ? readfile( $filename ) : false; + $size = is_readable( $filename ) ? readfile( $filename ) : false; + if ( ! empty( $size ) ) { /* Ok, cache file has been sent to output. */ exit; @@ -212,7 +213,7 @@ private static function _create_file( $file, $data ) { clearstatcache(); /* Permissions */ - $stat = @stat( dirname( $file ) ); + $stat = @stat( dirname( $file ) ); $perms = $stat['mode'] & 0007777; $perms = $perms & 0000666; @chmod( $file, $perms ); @@ -255,18 +256,14 @@ private static function _clear_dir( $dir, $recursive = false ) { /* Directory or file */ if ( is_dir( $object ) && $recursive ) { self::_clear_dir( $object, $recursive ); - } else { - if ( self::_user_can_delete( $object ) ) { - unlink( $object ); - } + } elseif ( self::_user_can_delete( $object ) ) { + unlink( $object ); } } /* Remove directory */ - if ( $recursive ) { - if ( self::_user_can_delete( $dir ) && 0 === count( glob( trailingslashit( $dir ) . '*' ) ) ) { - @rmdir( $dir ); - } + if ( $recursive && self::_user_can_delete( $dir ) && 0 === count( glob( trailingslashit( $dir ) . '*' ) ) ) { + @rmdir( $dir ); } /* Clean up */ @@ -402,9 +399,9 @@ private static function _user_can_delete( $file ) { $file = trailingslashit( $file ); } - $ssl_prefix = is_ssl() ? 'https-' : ''; + $ssl_prefix = is_ssl() ? 'https-' : ''; $current_blog = get_blog_details( get_current_blog_id() ); - $blog_path = CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . $ssl_prefix . $current_blog->domain . $current_blog->path; + $blog_path = CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . $ssl_prefix . $current_blog->domain . $current_blog->path; if ( 0 !== strpos( $file, $blog_path ) ) { return false; diff --git a/inc/class-cachify-memcached.php b/inc/class-cachify-memcached.php index 688fbe5f..cc5be81f 100644 --- a/inc/class-cachify-memcached.php +++ b/inc/class-cachify-memcached.php @@ -31,8 +31,8 @@ final class Cachify_MEMCACHED { */ public static function is_available() { return class_exists( 'Memcached' ) - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - && isset( $_SERVER['SERVER_SOFTWARE'] ) && strpos( strtolower( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ), 'nginx' ) !== false; + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + && isset( $_SERVER['SERVER_SOFTWARE'] ) && strpos( strtolower( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ), 'nginx' ) !== false; } /** @@ -254,8 +254,8 @@ private static function _connect_server() { } else { self::$_memcached->setOptions( array( - Memcached::OPT_COMPRESSION => false, - Memcached::OPT_BUFFER_WRITES => true, + Memcached::OPT_COMPRESSION => false, + Memcached::OPT_BUFFER_WRITES => true, Memcached::OPT_BINARY_PROTOCOL => true, ) ); diff --git a/inc/class-cachify.php b/inc/class-cachify.php index a83ea754..a1bdf052 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -47,7 +47,7 @@ final class Cachify { * * @since 2.0.9 */ - const METHOD_DB = 0; + const METHOD_DB = 0; const METHOD_APC = 1; const METHOD_HDD = 2; const METHOD_MMC = 3; @@ -59,16 +59,16 @@ final class Cachify { * * @since 2.0.9 */ - const MINIFY_DISABLED = 0; + const MINIFY_DISABLED = 0; const MINIFY_HTML_ONLY = 1; - const MINIFY_HTML_JS = 2; + const MINIFY_HTML_JS = 2; /** * REST endpoints * * @var string */ - const REST_NAMESPACE = 'cachify/v1'; + const REST_NAMESPACE = 'cachify/v1'; const REST_ROUTE_FLUSH = 'flush'; /** @@ -421,15 +421,15 @@ private static function _get_options() { return wp_parse_args( get_option( 'cachify' ), array( - 'only_guests' => 1, - 'compress_html' => self::MINIFY_DISABLED, - 'cache_expires' => 12, - 'without_ids' => '', - 'without_agents' => '', - 'use_apc' => self::METHOD_DB, - 'reset_on_post' => 1, - 'reset_on_comment' => 0, - 'sig_detail' => 0, + 'only_guests' => 1, + 'compress_html' => self::MINIFY_DISABLED, + 'cache_expires' => 12, + 'without_ids' => '', + 'without_agents' => '', + 'use_apc' => self::METHOD_DB, + 'reset_on_post' => 1, + 'reset_on_comment' => 0, + 'sig_detail' => 0, ) ); } @@ -707,10 +707,10 @@ public static function add_flush_icon_script( $wp_admin_bar ) { 'cachify-admin-bar-flush', 'cachify_admin_bar_flush_ajax_object', array( - 'url' => esc_url_raw( rest_url( self::REST_NAMESPACE . '/' . self::REST_ROUTE_FLUSH ) ), - 'nonce' => wp_create_nonce( 'wp_rest' ), - 'flushing' => __( 'Flushing cache', 'cachify' ), - 'flushed' => __( 'Cache flushed successfully', 'cachify' ), + 'url' => esc_url_raw( rest_url( self::REST_NAMESPACE . '/' . self::REST_ROUTE_FLUSH ) ), + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'flushing' => __( 'Flushing cache', 'cachify' ), + 'flushed' => __( 'Cache flushed successfully', 'cachify' ), 'dashicon_success' => self::get_dashicon_success_class(), ) ); @@ -727,8 +727,8 @@ public static function add_flush_rest_endpoint() { self::REST_NAMESPACE, self::REST_ROUTE_FLUSH, array( - 'methods' => WP_REST_Server::DELETABLE, - 'callback' => array( + 'methods' => WP_REST_Server::DELETABLE, + 'callback' => array( __CLASS__, 'flush_cache', ), @@ -1048,7 +1048,7 @@ public static function post_update( $id, $data ) { public static function flush_cache_for_posts( $post ) { if ( is_int( $post ) ) { $post_id = $post; - $data = get_post( $post_id ); + $data = get_post( $post_id ); if ( ! is_object( $data ) ) { return; @@ -1164,7 +1164,8 @@ private static function _cache_hash( $url = '' ) { } $url_parts = wp_parse_url( $url ); - $hash_key = $prefix . $url_parts['host'] . $url_parts['path']; + $hash_key = $prefix . $url_parts['host'] . $url_parts['path']; + return md5( $hash_key ) . '.cachify'; } @@ -1242,17 +1243,17 @@ private static function _is_logged_in() { public static function register_flush_cache_hooks() { /* Define all default flush cache hooks */ $flush_cache_hooks = array( - 'cachify_flush_cache' => 10, - '_core_updated_successfully' => 10, - 'switch_theme' => 10, - 'before_delete_post' => 10, - 'wp_trash_post' => 10, - 'create_term' => 10, - 'delete_term' => 10, - 'edit_terms' => 10, - 'user_register' => 10, - 'edit_user_profile_update' => 10, - 'delete_user' => 10, + 'cachify_flush_cache' => 10, + '_core_updated_successfully' => 10, + 'switch_theme' => 10, + 'before_delete_post' => 10, + 'wp_trash_post' => 10, + 'create_term' => 10, + 'delete_term' => 10, + 'edit_terms' => 10, + 'user_register' => 10, + 'edit_user_profile_update' => 10, + 'delete_user' => 10, /* third party */ 'autoptimize_action_cachepurged' => 10, ); @@ -1263,7 +1264,6 @@ public static function register_flush_cache_hooks() { foreach ( $flush_cache_hooks as $hook => $priority ) { add_action( $hook, array( 'Cachify', 'flush_total_cache' ), $priority, 0 ); } - } /** @@ -1580,7 +1580,6 @@ public static function add_admin_resources( $hook ) { default: break; } - } /** @@ -1739,9 +1738,9 @@ public static function validate_options( $data ) { * @since 1.0 */ public static function options_page() { - $options = self::_get_options(); + $options = self::_get_options(); $cachify_tabs = self::_get_tabs( $options ); - $current_tab = isset( $_GET['cachify_tab'] ) && isset( $cachify_tabs[ $_GET['cachify_tab'] ] ) + $current_tab = isset( $_GET['cachify_tab'] ) && isset( $cachify_tabs[ $_GET['cachify_tab'] ] ) ? sanitize_text_field( wp_unslash( $_GET['cachify_tab'] ) ) : 'settings'; ?> @@ -1760,7 +1759,7 @@ public static function options_page() { esc_url( add_query_arg( array( - 'page' => 'cachify', + 'page' => 'cachify', 'cachify_tab' => $tab_key, ), admin_url( 'options-general.php' ) From aa6d8c8b9e13f097fbea6b745354ed31c590bc11 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 11 Nov 2023 11:18:19 +0100 Subject: [PATCH 05/10] update WPCS definitions to v3.0 --- composer.json | 8 ++++---- phpcs.xml | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index ee42659f..fd912d89 100644 --- a/composer.json +++ b/composer.json @@ -22,17 +22,17 @@ "forum": "https://wordpress.org/support/plugin/cachify" }, "require": { - "php": ">=5.2.0", + "php": ">=5.6", "composer/installers": "~1.0" }, "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^v0.7", + "dealerdirect/phpcodesniffer-composer-installer": "^v1.0", "matthiasmullie/minify": "^1.3", "phpunit/phpunit": "^5|^7|^9", "squizlabs/php_codesniffer": "^3.7", "phpcompatibility/phpcompatibility-wp": "^2.1", - "wp-coding-standards/wpcs": "^2.3", - "yoast/phpunit-polyfills": "^1.0" + "wp-coding-standards/wpcs": "^3.0", + "yoast/phpunit-polyfills": "^2.0" }, "scripts": { "post-install-cmd": [ diff --git a/phpcs.xml b/phpcs.xml index 750725cb..356fca15 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -21,7 +21,11 @@ - + + + + + From 0fa080b4b397b9ac438087c0b080de9fde218b1f Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 24 Nov 2023 15:52:50 +0100 Subject: [PATCH 06/10] fix parameter use of `trigger_error(esc_html(...))` (#295) The second parameter was supposed to be passed to `trigger_error`, not `esc_html`. Move the closing parenthesis to resolve this issue. --- inc/class-cachify-hdd.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/class-cachify-hdd.php b/inc/class-cachify-hdd.php index 4c89e958..04e6a887 100644 --- a/inc/class-cachify-hdd.php +++ b/inc/class-cachify-hdd.php @@ -175,7 +175,7 @@ private static function _create_files( $data ) { /* Create directory */ if ( ! wp_mkdir_p( $file_path ) ) { - trigger_error( esc_html( __METHOD__ . ": Unable to create directory {$file_path}.", E_USER_WARNING ) ); + trigger_error( esc_html( __METHOD__ . ": Unable to create directory {$file_path}." ), E_USER_WARNING ); return; } /* Write to file */ @@ -203,7 +203,7 @@ private static function _create_file( $file, $data ) { /* Writable? */ $handle = @fopen( $file, 'wb' ); if ( ! $handle ) { - trigger_error( esc_html( __METHOD__ . ": Could not write file {$file}.", E_USER_WARNING ) ); + trigger_error( esc_html( __METHOD__ . ": Could not write file {$file}." ), E_USER_WARNING ); return; } From 417bc5b375da53763826b6b7b9d2192461e19ffd Mon Sep 17 00:00:00 2001 From: New To Vaux <640187+newtovaux@users.noreply.github.com> Date: Fri, 8 Dec 2023 17:28:00 +0000 Subject: [PATCH 07/10] introduce Redis support (#252) Introduce a new caching backend that utilizes Redis object store. It is available, if PHPRedis is installed and requires a Redis server. The plugin connects to "redis://localhost:6379" by default, which can be overwritten using the "cachify_redis_servers" filter. Caching behavior is implemented similar to the memcached module. --- README.md | 7 +- cachify.php | 4 +- inc/class-cachify-redis.php | 235 ++++++++++++++++++++++++++++++++++++ inc/class-cachify.php | 16 ++- readme.txt | 7 +- 5 files changed, 260 insertions(+), 9 deletions(-) create mode 100644 inc/class-cachify-redis.php diff --git a/README.md b/README.md index d8e9d2e2..48248154 100755 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Cachify # -Smart, efficient cache solution for WordPress. Use DB, HDD, APC or Memcached for storing your blog pages. Make WordPress faster! +Smart, efficient cache solution for WordPress. Use DB, HDD, APC, Redis or Memcached for storing your blog pages. Make WordPress faster! ## Description ## -*Cachify* optimizes your page loads by caching posts, pages and custom post types as static content. You can choose between caching via database, on the web server’s hard drive (HDD), Memcached (only on Nginx) or — thanks to APC (Alternative PHP Cache) — directly in the web server’s system cache. Whenever a page or post is loaded, it can be pulled directly from the cache. The amount of database queries and PHP requests will dramatically decrease towards zero, depending on the caching method you chose. +*Cachify* optimizes your page loads by caching posts, pages and custom post types as static content. You can choose between caching via database, on the web server’s hard drive (HDD), Memcached (only on Nginx), Redis or — thanks to APC (Alternative PHP Cache) — directly in the web server’s system cache. Whenever a page or post is loaded, it can be pulled directly from the cache. The amount of database queries and PHP requests will dramatically decrease towards zero, depending on the caching method you chose. ### Features ### * Works with custom post types. -* Caching methods: DB, HDD, APC and Memcached. +* Caching methods: DB, HDD, APC, Redis and Memcached. * “Flush Cache” button in the WordPress toolbar. * Ready for WordPress Multisite. * Optional compression of HTML markup. @@ -43,6 +43,7 @@ Smart, efficient cache solution for WordPress. Use DB, HDD, APC or Memcached for * WordPress 4.4 or greater * APC 3.1.4 or greater (optional) * Memcached in Nginx (optional) +* Redis (optional, via the phpredis module) ## Frequently Asked Questions ## diff --git a/cachify.php b/cachify.php index fed79856..35d8f896 100644 --- a/cachify.php +++ b/cachify.php @@ -1,7 +1,7 @@ set( + self::_file_path(), + $data . self::_cache_signature( $sig_detail ), + $lifetime + ); + } + + /** + * Read item from cache + * + * @param string $hash Hash of the entry. + * @return mixed Content of the entry + */ + public static function get_item( $hash ) { + /* Server connect */ + if ( ! self::_connect_server() ) { + return; + } + + /* Get item */ + return self::$_redis->get( + self::_file_path() + ); + } + + /** + * Delete item from cache + * + * @param string $hash Hash of the entry [ignored]. + * @param string $url URL of the entry. + */ + public static function delete_item( $hash, $url ) { + /* Server connect */ + if ( ! self::_connect_server() ) { + return; + } + + /* Delete */ + self::$_redis->del( + self::_file_path( $url ) + ); + } + + /** + * Clear the cache + * + * @return void + */ + public static function clear_cache() { + /* Server connect */ + if ( ! self::_connect_server() ) { + return; + } + + /* Flush */ + @self::$_redis->flushAll(); + } + + /** + * Print the cache + * + * @param bool $sig_detail Show details in signature. + * @param string $cache Cached content. + */ + public static function print_cache( $sig_detail, $cache ) { + echo $cache; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + exit; + } + + /** + * Get the cache size + * + * @return integer Directory size + */ + public static function get_stats() { + /* Server connect */ + if ( ! self::_connect_server() ) { + return null; + } + + /* Info */ + $data = self::$_redis->info(); + + /* No stats? */ + if ( empty( $data ) ) { + return null; + } + + /* Empty */ + if ( empty( $data['used_memory'] ) ) { + return null; + } + + return $data['used_memory']; + } + + /** + * Generate signature + * + * @param bool $detail Show details in signature. + * @return string Signature string + */ + private static function _cache_signature( $detail ) { + return sprintf( + "\n\n", + 'Cachify | https://cachify.pluginkollektiv.org', + ( $detail ? 'Redis Cache' : __( 'Generated', 'cachify' ) ), + date_i18n( + 'd.m.Y H:i:s', + current_time( 'timestamp' ) + ) + ); + } + + /** + * Path of cache file + * + * @param string $path Request URI or permalink [optional]. + * @return string Path to cache file + */ + private static function _file_path( $path = null ) { + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated + $path_parts = wp_parse_url( $path ? $path : wp_unslash( $_SERVER['REQUEST_URI'] ) ); + + return trailingslashit( + sprintf( + '%s%s', + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated + wp_unslash( $_SERVER['HTTP_HOST'] ), + $path_parts['path'] + ) + ); + } + + /** + * Connect to Redis server + * + * @return boolean true/false TRUE on success + */ + private static function _connect_server() { + /* Not enabled? */ + if ( ! self::is_available() ) { + return false; + } + + /* Have object and it thinks it's connected to a server */ + if ( is_object( self::$_redis ) && self::$_redis->isConnected() ) { + return true; + } + + /* Init */ + self::$_redis = new Redis(); + + /* Set options & connect */ + try { + self::$_redis->connect( + (string) apply_filters( + 'cachify_redis_servers', + 'redis://localhost:6379' + ) + ); + } catch ( Exception $e ) { + return false; + } + + if ( ! self::$_redis->isConnected() ) { + return false; + } + + return true; + } +} diff --git a/inc/class-cachify.php b/inc/class-cachify.php index a1bdf052..3fb9e3ed 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -51,6 +51,7 @@ final class Cachify { const METHOD_APC = 1; const METHOD_HDD = 2; const METHOD_MMC = 3; + const METHOD_REDIS = 4; /** * Minify settings @@ -404,6 +405,10 @@ private static function _set_default_vars() { } 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 */ } else { self::$method = new Cachify_DB(); @@ -1441,6 +1446,9 @@ public static function flush_total_cache( $clear_all_methods = false ) { /* HDD */ Cachify_HDD::clear_cache(); + /* REDIS */ + Cachify_REDIS::clear_cache(); + /* MEMCACHED */ Cachify_MEMCACHED::clear_cache(); } else { @@ -1638,6 +1646,7 @@ private static function _method_select() { self::METHOD_APC => esc_html__( 'APC', 'cachify' ), self::METHOD_HDD => esc_html__( 'Hard disk', 'cachify' ), self::METHOD_MMC => esc_html__( 'Memcached', 'cachify' ), + self::METHOD_REDIS => esc_html__( 'Redis', 'cachify' ), ); /* APC */ @@ -1655,6 +1664,11 @@ private static function _method_select() { unset( $methods[2] ); } + /* Redis */ + if ( ! Cachify_REDIS::is_available() ) { + unset( $methods[4] ); + } + return $methods; } @@ -1709,7 +1723,7 @@ public static function validate_options( $data ) { self::flush_total_cache( true ); /* Notification */ - if ( self::$options['use_apc'] !== $data['use_apc'] && $data['use_apc'] >= self::METHOD_APC ) { + if ( self::$options['use_apc'] !== $data['use_apc'] && $data['use_apc'] >= self::METHOD_APC && self::METHOD_REDIS != $data['use_apc'] ) { add_settings_error( 'cachify_method_tip', 'cachify_method_tip', diff --git a/readme.txt b/readme.txt index c027032b..e38e239a 100644 --- a/readme.txt +++ b/readme.txt @@ -9,14 +9,14 @@ * License: GPLv2 or later * License URI: http://www.gnu.org/licenses/gpl-2.0.html -Smart, efficient cache solution for WordPress. Use DB, HDD, APC or Memcached for storing your blog pages. Make WordPress faster! +Smart, efficient cache solution for WordPress. Use DB, HDD, APC, Redis or Memcached for storing your blog pages. Make WordPress faster! ## Description ## -*Cachify* optimizes your page loads by caching posts, pages and custom post types as static content. You can choose between caching via database, on the web server’s hard drive (HDD), Memcached (only on Nginx) or — thanks to APC (Alternative PHP Cache) — directly in the web server’s system cache. Whenever a page or post is loaded, it can be pulled directly from the cache. The amount of database queries and PHP requests will dramatically decrease towards zero, depending on the caching method you chose. +*Cachify* optimizes your page loads by caching posts, pages and custom post types as static content. You can choose between caching via database, on the web server’s hard drive (HDD), Memcached (only on Nginx), Redis or — thanks to APC (Alternative PHP Cache) — directly in the web server’s system cache. Whenever a page or post is loaded, it can be pulled directly from the cache. The amount of database queries and PHP requests will dramatically decrease towards zero, depending on the caching method you chose. ### Features ### * Works with custom post types. -* Caching methods: DB, HDD, APC and Memcached. +* Caching methods: DB, HDD, APC, Redis and Memcached. * “Flush Cache” button in the WordPress toolbar. * Ready for WordPress Multisite. * Optional compression of HTML markup. @@ -50,6 +50,7 @@ Smart, efficient cache solution for WordPress. Use DB, HDD, APC or Memcached for * WordPress 3.8 or greater * APC 3.1.4 or greater (optional) * Memcached in Nginx (optional) +* Redis (optional, via the phpredis module) ## Frequently Asked Questions ## From d8ac6f96839960cb953d0a2a7da4ab1abaa46939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Mendon=C3=A7a?= Date: Mon, 11 Dec 2023 07:18:59 +0000 Subject: [PATCH 08/10] i18n: fix brand names and "Caching method" label (#297) * use uniform capitalizaton for "Cachify" and "FastCGI" * improve translatability of the "Caching method: ..." label --- CHANGELOG.md | 4 ++-- inc/cachify.settings.php | 2 +- inc/class-cachify.php | 12 ++++++++---- inc/setup/cachify.hdd.htaccess.php | 2 +- readme.txt | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca39bf2e..84a7fb87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,8 +64,8 @@ Requires PHP 5.6 and WordPress 4.7 or above * Fix for the PHP notice "Call to undefined function is_plugin_active_for_network" on WordPress Multisite ## 2.2.0 -* Toolbar: Display of the "Flush the cachify cache" button on the frontend -* Toolbar: Controlling the display of the "Flush the cachify cache" button via hook +* Toolbar: Display of the "Flush the Cachify cache" button on the frontend +* Toolbar: Controlling the display of the "Flush the Cachify cache" button via hook ## 2.1.9 * Vervollständigung des Cachify-Pfades in `robots.txt`: `Disallow: /wp-content/cache/cachify/` diff --git a/inc/cachify.settings.php b/inc/cachify.settings.php index 9f807e32..923284f9 100644 --- a/inc/cachify.settings.php +++ b/inc/cachify.settings.php @@ -41,7 +41,7 @@ printf( /* translators: Placeholder is the trash icon itself as dashicon */ esc_html__( 'Flush the cache by clicking the button below or the %1$s icon in the admin bar.', 'cachify' ), - '"' . esc_html__( 'Flush the cachify cache', 'cachify' ) . '"' + '"' . esc_html__( 'Flush the Cachify cache', 'cachify' ) . '"' ); ?>

diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 3fb9e3ed..4a709357 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -573,7 +573,7 @@ public static function add_dashboard_count( $items = array() ) { /* Right now item */ $items[] = sprintf( - ' + ' %s', @@ -583,8 +583,12 @@ public static function add_dashboard_count( $items = array() ) { ), admin_url( 'options-general.php' ) ), - esc_attr( strtolower( $method ) ), - esc_html__( 'Caching method', 'cachify' ), + sprintf( + /* translators: 1: "Caching method label"; 2: Actual method. */ + esc_html__( '%1$s: %2$s', 'cachify' ), + esc_html__( 'Caching method', 'cachify' ), + esc_attr( strtolower( $method ) ) + ), esc_attr( $method ), plugins_url( 'images/symbols.svg', CACHIFY_FILE ), esc_attr( strtolower( $method ) ), @@ -667,7 +671,7 @@ public static function add_flush_icon( $wp_admin_bar ) { ) . '', 'meta' => array( - 'title' => esc_html__( 'Flush the cachify cache', 'cachify' ), + 'title' => esc_html__( 'Flush the Cachify cache', 'cachify' ), ), ) ); diff --git a/inc/setup/cachify.hdd.htaccess.php b/inc/setup/cachify.hdd.htaccess.php index 4ab3335c..12cebb6b 100644 --- a/inc/setup/cachify.hdd.htaccess.php +++ b/inc/setup/cachify.hdd.htaccess.php @@ -78,7 +78,7 @@
  • - +

  • diff --git a/readme.txt b/readme.txt index e38e239a..847f7d1e 100644 --- a/readme.txt +++ b/readme.txt @@ -155,8 +155,8 @@ A complete documentation is available in the [online handbook](https://cachify.p * Fix for the PHP notice "Call to undefined function is_plugin_active_for_network" on WordPress Multisite ### 2.2.0 ### -* Toolbar: Display of the "Flush the cachify cache" button on the frontend -* Toolbar: Controlling the display of the "Flush the cachify cache" button via hook +* Toolbar: Display of the "Flush the Cachify cache" button on the frontend +* Toolbar: Controlling the display of the "Flush the Cachify cache" button via hook For the complete changelog, check out our [GitHub repository](https://github.com/pluginkollektiv/cachify). From 87b5b0388fe9ddaef7dea9904d32b390b28f3ef3 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 29 Mar 2024 13:20:25 +0100 Subject: [PATCH 09/10] update dev-dependencies and GH actions --- .github/workflows/test.yml | 16 +++++++++------- .../workflows/wordpress-plugin-asset-update.yml | 4 ++-- .github/workflows/wordpress-plugin-deploy.yml | 4 ++-- composer.json | 6 +++--- package.json | 8 ++++---- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 30b9bc6e..f1fbc86c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,13 +12,15 @@ jobs: wordpress: '5.9' - php: '8.0' wordpress: '6.0' - - php: '8.1' + - php: '8.2' + wordpress: '6.4' + - php: '8.3' wordpress: 'latest' - - php: '8.1' + - php: '8.3' wordpress: 'nightly' steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 - name: Setup PHP @@ -43,16 +45,16 @@ jobs: quality: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.0' + php-version: '8.2' tools: composer - name: Setup Node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: - node-version: '16' + node-version: '20' - name: Code style checks for PHP and CSS run: | composer install diff --git a/.github/workflows/wordpress-plugin-asset-update.yml b/.github/workflows/wordpress-plugin-asset-update.yml index b47e1ae9..a32fc4a8 100644 --- a/.github/workflows/wordpress-plugin-asset-update.yml +++ b/.github/workflows/wordpress-plugin-asset-update.yml @@ -8,11 +8,11 @@ jobs: name: Push to stable runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.0' + php-version: '8.2' tools: composer - name: Build run: composer install diff --git a/.github/workflows/wordpress-plugin-deploy.yml b/.github/workflows/wordpress-plugin-deploy.yml index e2406894..f74cfc47 100755 --- a/.github/workflows/wordpress-plugin-deploy.yml +++ b/.github/workflows/wordpress-plugin-deploy.yml @@ -9,11 +9,11 @@ jobs: name: New tag runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.0' + php-version: '8.2' tools: composer - name: Build run: composer install diff --git a/composer.json b/composer.json index fd912d89..9ae9daca 100644 --- a/composer.json +++ b/composer.json @@ -23,15 +23,15 @@ }, "require": { "php": ">=5.6", - "composer/installers": "~1.0" + "composer/installers": "^v1|^v2" }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^v1.0", "matthiasmullie/minify": "^1.3", "phpunit/phpunit": "^5|^7|^9", - "squizlabs/php_codesniffer": "^3.7", + "squizlabs/php_codesniffer": "^3.9", "phpcompatibility/phpcompatibility-wp": "^2.1", - "wp-coding-standards/wpcs": "^3.0", + "wp-coding-standards/wpcs": "^3.1", "yoast/phpunit-polyfills": "^2.0" }, "scripts": { diff --git a/package.json b/package.json index 2aa9ff0f..75f868ce 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "private": true, "devDependencies": { - "stylelint": "^14.10.0", - "eslint": "^8.22.0", - "@wordpress/eslint-plugin": "^12.9.0", - "@wordpress/stylelint-config": "^20.0.2" + "stylelint": "^14.16.1", + "eslint": "^8.57.0", + "@wordpress/eslint-plugin": "^17.11.0", + "@wordpress/stylelint-config": "^21.37.0" } } From 053b59a9336ac5211b5772ccb58407caf62232cd Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 29 Mar 2024 13:37:24 +0100 Subject: [PATCH 10/10] fix some inconsistent return values --- inc/class-cachify-apc.php | 2 +- inc/class-cachify-db.php | 11 ++++++----- inc/class-cachify-hdd.php | 6 +++--- inc/class-cachify-memcached.php | 4 ++-- inc/class-cachify-redis.php | 2 +- inc/class-cachify.php | 2 +- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/inc/class-cachify-apc.php b/inc/class-cachify-apc.php index 0e8c17c9..7992e54c 100644 --- a/inc/class-cachify-apc.php +++ b/inc/class-cachify-apc.php @@ -105,7 +105,7 @@ public static function clear_cache() { * @since 2.0 */ public static function print_cache() { - return; + // Not supported. } /** diff --git a/inc/class-cachify-db.php b/inc/class-cachify-db.php index 003c0394..ddbb9b6f 100644 --- a/inc/class-cachify-db.php +++ b/inc/class-cachify-db.php @@ -146,9 +146,10 @@ public static function get_stats() { global $wpdb; /* Read */ - - return $wpdb->get_var( - 'SELECT SUM( CHAR_LENGTH(option_value) ) FROM `' . $wpdb->options . "` WHERE `option_name` LIKE ('\_transient%.cachify')" + return intval( + $wpdb->get_var( + 'SELECT SUM( CHAR_LENGTH(option_value) ) FROM `' . $wpdb->options . "` WHERE `option_name` LIKE ('\_transient%.cachify')" + ) ); } @@ -166,7 +167,7 @@ public static function get_stats() { private static function _cache_signature( $detail, $meta ) { /* No array? */ if ( ! is_array( $meta ) ) { - return; + return ''; } if ( $detail ) { @@ -218,7 +219,7 @@ private static function _page_queries() { /** * Return execution time * - * @return int Execution time in seconds + * @return string Execution time in seconds * * @since 0.1 */ diff --git a/inc/class-cachify-hdd.php b/inc/class-cachify-hdd.php index 04e6a887..361e8f0b 100644 --- a/inc/class-cachify-hdd.php +++ b/inc/class-cachify-hdd.php @@ -275,14 +275,14 @@ private static function _clear_dir( $dir, $recursive = false ) { * * @param string $dir Directory path. * - * @return mixed Directory size + * @return int|false Directory size * * @since 2.0 */ public static function _dir_size( $dir = '.' ) { /* Is directory? */ if ( ! is_dir( $dir ) ) { - return; + return false; } /* Read */ @@ -293,7 +293,7 @@ public static function _dir_size( $dir = '.' ) { /* Empty? */ if ( empty( $objects ) ) { - return; + return false; } /* Init */ diff --git a/inc/class-cachify-memcached.php b/inc/class-cachify-memcached.php index cc5be81f..88d536a2 100644 --- a/inc/class-cachify-memcached.php +++ b/inc/class-cachify-memcached.php @@ -89,7 +89,7 @@ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { public static function get_item( $hash ) { /* Server connect */ if ( ! self::_connect_server() ) { - return; + return null; } /* Get item */ @@ -143,7 +143,7 @@ public static function clear_cache() { * @since 2.0.7 */ public static function print_cache() { - return; + // Not supported. } /** diff --git a/inc/class-cachify-redis.php b/inc/class-cachify-redis.php index df29673d..d32c1a5b 100644 --- a/inc/class-cachify-redis.php +++ b/inc/class-cachify-redis.php @@ -77,7 +77,7 @@ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { public static function get_item( $hash ) { /* Server connect */ if ( ! self::_connect_server() ) { - return; + return null; } /* Get item */ diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 4a709357..52aee24c 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -874,7 +874,7 @@ public static function flush_cache() { public static function flush_notice() { /* No admin */ if ( ! is_admin_bar_showing() || ! apply_filters( 'cachify_user_can_flush_cache', current_user_can( 'manage_options' ) ) ) { - return false; + return; } printf(