diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 0dffdfd..f8d873d 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -273,6 +273,7 @@ public function nginx_helper_default_settings() { 'purge_page_on_new_comment' => 1, 'purge_page_on_deleted_comment' => 1, 'purge_feeds' => 1, + 'relay_client' => false, 'redis_hostname' => '127.0.0.1', 'redis_port' => '6379', 'redis_prefix' => 'nginx-cache:', @@ -280,18 +281,20 @@ public function nginx_helper_default_settings() { 'redis_username' => '', 'redis_password' => '', 'redis_database' => 0, + 'redis_timeout' => 5, + 'redis_read_timeout' => 1.5, 'purge_url' => '', - 'auth_enabled_by_constant' => 0, - 'cache_method_set_by_constant' => 0, - 'purge_method_set_by_constant' => 0, - 'redis_hostname_set_by_constant' => 0, - 'redis_port_set_by_constant' => 0, - 'redis_unix_socket_set_by_constant' => 0, - 'redis_prefix_set_by_constant' => 0, - 'redis_database_set_by_constant' => 0, - 'redis_username_set_by_constant' => 0, - 'redis_password_socket_set_by_constant' => 0, + 'cache_method_set_by_constant' => false, + 'purge_method_set_by_constant' => false, + 'redis_hostname_set_by_constant' => false, + 'redis_port_set_by_constant' => false, + 'redis_unix_socket_set_by_constant' => false, + 'redis_prefix_set_by_constant' => false, + 'redis_database_set_by_constant' => false, + 'redis_username_set_by_constant' => false, + 'redis_password_socket_set_by_constant' => false, 'homepage_purge_post_type_exceptions' => array(), + 'post_type_purge_exceptions' => array(), ); } @@ -317,19 +320,32 @@ public function nginx_helper_settings() { $this->nginx_helper_default_settings() ); - if ( defined( 'RT_WP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS' ) ) { - $data['homepage_purge_post_type_exceptions'] = RT_WP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS; - } + if ( defined( 'GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS' ) ) { + $data['homepage_purge_post_type_exceptions'] = GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS; + } + + if ( defined( 'GP_NGINX_HELPER_POST_TYPE_PURGE_EXCEPTIONS' ) ) { + $data['post_type_purge_exceptions'] = GP_NGINX_HELPER_POST_TYPE_PURGE_EXCEPTIONS; + } - if ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { + if ( defined( 'GP_NGINX_HELPER_PURGE_METHOD' ) ) { + $data['purge_method'] = GP_NGINX_HELPER_PURGE_METHOD; + $data['purge_method_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { $data['purge_method'] = RT_WP_NGINX_HELPER_PURGE_METHOD; - $data['purge_method_set_by_constant'] = 1; + $data['purge_method_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } - if ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { + if ( defined( 'GP_NGINX_HELPER_CACHE_METHOD' ) ) { + $data['cache_method'] = GP_NGINX_HELPER_CACHE_METHOD; + $data['cache_method_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + if ( 'enable_fastcgi' === GP_NGINX_HELPER_CACHE_METHOD ) { + $data['enable_purge'] = 1; + return $data; + } + } elseif ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { $data['cache_method'] = RT_WP_NGINX_HELPER_CACHE_METHOD; - $data['cache_method_set_by_constant'] = 1; - + $data['cache_method_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; if ( 'enable_fastcgi' === RT_WP_NGINX_HELPER_CACHE_METHOD ) { $data['enable_purge'] = 1; return $data; @@ -343,43 +359,86 @@ public function nginx_helper_settings() { $redis_port = false; $redis_unix_socket = false; - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { + if ( defined( 'GP_NGINX_HELPER_USE_RELAY' ) ) { + if (in_array( 'relay', get_loaded_extensions())) { + $data['relay_client'] = GP_NGINX_HELPER_USE_RELAY; + } + } + + if ( defined( 'GP_NGINX_HELPER_REDIS_PREFIX' ) ) { + $redis_prefix = GP_NGINX_HELPER_REDIS_PREFIX; + $data['redis_prefix_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { $redis_prefix = RT_WP_NGINX_HELPER_REDIS_PREFIX; - $data['redis_prefix'] = $redis_prefix; - $data['redis_prefix_set_by_constant'] = 1; + $data['redis_prefix_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; + } + if ( $redis_prefix ) { + $data['redis_prefix'] = $redis_prefix; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_PORT' ) ) { + $redis_port = GP_NGINX_HELPER_REDIS_PORT; + $data['redis_port_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { $redis_port = RT_WP_NGINX_HELPER_REDIS_PORT; - $data['redis_port'] = $redis_port; - $data['redis_port_set_by_constant'] = 1; + $data['redis_port_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; + } + if ( $redis_port ) { + $data['redis_port'] = $redis_port; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { + $redis_hostname = GP_NGINX_HELPER_REDIS_HOSTNAME; + $data['redis_hostname_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_HOSTNAME'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { $redis_hostname = RT_WP_NGINX_HELPER_REDIS_HOSTNAME; - $data['redis_hostname'] = $redis_hostname; - $data['redis_hostname_set_by_constant'] = 1; + $data['redis_hostname_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME'; + } + if ( $redis_hostname ) { + $data['redis_hostname'] = $redis_hostname; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { + $redis_unix_socket = GP_NGINX_HELPER_REDIS_UNIX_SOCKET; + $data['redis_unix_socket_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_HOSTNAME'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { $redis_unix_socket = RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET; - $data['redis_unix_socket'] = $redis_unix_socket; - $data['redis_unix_socket_set_by_constant'] = 1; + $data['redis_unix_socket_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME'; + } + if ( $redis_unix_socket ) { + $data['redis_unix_socket'] = $redis_unix_socket; + } + + if ( defined( 'GP_NGINX_HELPER_REDIS_DATABASE' ) ) { + $data['redis_database'] = GP_NGINX_HELPER_REDIS_DATABASE; + $data['redis_database_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_DATABASE'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_DATABASE' ) ) { + $data['redis_database'] = RT_WP_NGINX_HELPER_REDIS_DATABASE; + $data['redis_database_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_DATABASE'; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_DATABASE' ) ) { - $data['redis_database'] = RT_WP_NGINX_HELPER_REDIS_DATABASE; - $data['redis_database_set_by_constant'] = 1; + if ( defined( 'GP_NGINX_HELPER_REDIS_USERNAME' ) ) { + $data['redis_username'] = GP_NGINX_HELPER_REDIS_USERNAME; + $data['redis_username_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_USERNAME'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { + $data['redis_username'] = RT_WP_NGINX_HELPER_REDIS_USERNAME; + $data['redis_username_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_USERNAME'; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { - $data['redis_username'] = RT_WP_NGINX_HELPER_REDIS_USERNAME; - $data['redis_username_set_by_constant'] = 1; + if ( defined( 'GP_NGINX_HELPER_REDIS_PASSWORD' ) ) { + $data['redis_password'] = GP_NGINX_HELPER_REDIS_PASSWORD; + $data['redis_password_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_PASSWORD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { + $data['redis_password'] = RT_WP_NGINX_HELPER_REDIS_PASSWORD; + $data['redis_password_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_PASSWORD'; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { - $data['redis_password'] = RT_WP_NGINX_HELPER_REDIS_PASSWORD; - $data['redis_password_set_by_constant'] = 1; + if ( defined( 'GP_NGINX_HELPER_REDIS_TIMEOUT' ) ) { + $data['redis_timeout'] = GP_NGINX_HELPER_REDIS_TIMEOUT; + } + + if ( defined( 'GP_NGINX_HELPER_REDIS_READ_TIMEOUT' ) ) { + $data['redis_read_timeout'] = GP_NGINX_HELPER_REDIS_READ_TIMEOUT; } if ( $redis_prefix && $redis_hostname && $redis_port ) { @@ -672,19 +731,18 @@ public function update_map() { */ public function set_future_post_option_on_future_status( $new_status, $old_status, $post ) { - global $blog_id, $nginx_purger; - - $exclude_post_types = apply_filters( 'rt_nginx_helper_exclude_post_types', array( 'nav_menu_item' ) ); + global $blog_id, $nginx_purger, $nginx_helper_admin; - $post_type = $post->post_type; + $purge_exceptions = $nginx_helper_admin->options['post_type_purge_exceptions']; + $purge_exceptions[] = 'nav_menu_item'; + $exclude_post_types = apply_filters( 'rt_nginx_helper_exclude_post_types', $purge_exceptions ); + $post_type = $post->post_type; if ( in_array( $post_type, $exclude_post_types, true ) ) { - if ( 'nav_menu_item' !== $post_type ) { - $nginx_purger->log('* * * * *'); - $nginx_purger->log('* Post Type update - ' . $post_type . ' - purge trigger excluded...'); - $nginx_purger->log('* Filter: -> rt_nginx_helper_exclude_post_types'); - $nginx_purger->log('* * * * *'); - } + $nginx_purger->log('* * * * *'); + $nginx_purger->log('* Post Type update - ' . $post_type . ' - purge trigger excluded...'); + $nginx_purger->log('* Filter: -> rt_nginx_helper_exclude_post_types'); + $nginx_purger->log('* * * * *'); return; } diff --git a/admin/class-predis-purger.php b/admin/class-predis-purger.php deleted file mode 100644 index 1f4771f..0000000 --- a/admin/class-predis-purger.php +++ /dev/null @@ -1,283 +0,0 @@ -options['redis_unix_socket']; - $username = $nginx_helper_admin->options['redis_username']; - $password = $nginx_helper_admin->options['redis_password']; - $predis_connection_array['database'] = $nginx_helper_admin->options['redis_database']; - - if ( $path ) { - $predis_connection_array['path'] = $path; - } else { - $predis_connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; - $predis_connection_array['port'] = $nginx_helper_admin->options['redis_port']; - } - - if ( $username && $password ) { - $predis_connection_array['username'] = $username; - $predis_connection_array['password'] = $password; - } - - // redis server parameter. - $this->redis_object = new Predis\Client( $predis_connection_array ); - - try { - $this->redis_object->connect(); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - - /** - * Purge all. - */ - public function purge_all() { - - global $nginx_helper_admin; - - $prefix = trim( $nginx_helper_admin->options['redis_prefix'] ); - - $this->log( '* * * * *' ); - - // If Purge Cache link click from network admin then purge all. - if ( is_network_admin() ) { - - $this->delete_keys_by_wildcard( $prefix . '*' ); - $this->log( '* Purged Everything! * ' ); - - } else { // Else purge only site specific cache. - - $parse = wp_parse_url( get_home_url() ); - $parse['path'] = empty( $parse['path'] ) ? '/' : $parse['path']; - $this->delete_keys_by_wildcard( $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*' ); - $this->log( '* ' . get_home_url() . ' Purged! * ' ); - - } - - $this->log( '* * * * *' ); - - /** - * Fire an action after the Redis cache has been purged. - * - * @since 2.1.0 - */ - do_action( 'rt_nginx_helper_after_redis_purge_all' ); - } - - /** - * Purge url. - * - * @param string $url URL. - * @param bool $feed Feed or not. - */ - public function purge_url( $url, $feed = true ) { - - global $nginx_helper_admin; - - /** - * Filters the URL to be purged. - * - * @since 2.1.0 - * - * @param string $url URL to be purged. - */ - $url = apply_filters( 'rt_nginx_helper_purge_url', $url ); - - $this->log( '- Purging URL | ' . $url ); - - $parse = wp_parse_url( $url ); - - if ( ! isset( $parse['path'] ) ) { - $parse['path'] = ''; - } - - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; - - /** - * To delete device type caches such as `--mobile`, `--desktop`, `--lowend`, etc. - * This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache. - * - * For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL. - * Add this filter in separate plugin or simply in theme's function.php file: - * ``` - * add_filter( 'rt_nginx_helper_purge_url', function( $url ) { - * $url = $url . '--*'; - * return $url; - * }); - * ``` - * - * Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted. - * - * @since 2.1.0 - */ - if ( strpos( $_url_purge_base, '*' ) === false ) { - - $status = $this->delete_single_key( $_url_purge_base ); - - if ( $status ) { - $this->log( '- Purge URL | ' . $_url_purge_base ); - } else { - $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); - } - } else { - - $status = $this->delete_keys_by_wildcard( $_url_purge_base ); - - if ( $status ) { - $this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' ); - } else { - $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); - } - } - - } - - /** - * Custom purge urls. - */ - public function custom_purge_urls() { - - global $nginx_helper_admin; - - $parse = wp_parse_url( home_url() ); - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; - - $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? - explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); - - /** - * Allow plugins/themes to modify/extend urls. - * - * @param array $purge_urls URLs which needs to be purged. - * @param bool $wildcard If wildcard in url is allowed or not. default true. - */ - $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); - - if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - - foreach ( $purge_urls as $purge_url ) { - - $purge_url = trim( $purge_url ); - - if ( strpos( $purge_url, '*' ) === false ) { - - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_single_key( $purge_url ); - if ( $status ) { - $this->log( '- Purge URL | ' . $purge_url ); - } else { - $this->log( '- Not Found | ' . $purge_url, 'ERROR' ); - } - } else { - - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_keys_by_wildcard( $purge_url ); - - if ( $status ) { - $this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' ); - } else { - $this->log( '- Not Found | ' . $purge_url, 'ERROR' ); - } - } - } - } - - } - - /** - * Single Key Delete Example - * e.g. $key can be nginx-cache:httpGETexample.com/ - * - * @param string $key Key to delete cache. - * - * @return mixed - */ - public function delete_single_key( $key ) { - - try { - return $this->redis_object->executeRaw( array( 'DEL', $key ) ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - - /** - * Delete Keys by wildcard. - * e.g. $key can be nginx-cache:httpGETexample.com* - * - * Lua Script block to delete multiple keys using wildcard - * Script will return count i.e. number of keys deleted - * if return value is 0, that means no matches were found - * - * Call redis eval and return value from lua script - * - * @param string $pattern Pattern. - * - * @return mixed - */ - public function delete_keys_by_wildcard( $pattern ) { - - // Lua Script. - $lua = <<redis_object->eval( $lua, 1, $pattern ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - -} diff --git a/admin/class-purger.php b/admin/class-purger.php index 683afd5..419a5d3 100644 --- a/admin/class-purger.php +++ b/admin/class-purger.php @@ -88,19 +88,18 @@ public function purge_post_on_comment_change( $newstatus, $oldstatus, $comment ) $_comment_id = $comment->comment_ID; $_post_type = get_post_type( $_post_id ); - $exclude_post_types = apply_filters( 'rt_nginx_helper_comment_change_exclude_post_types', array() ); + $purge_exceptions = $nginx_helper_admin->options['post_type_purge_exceptions']; + $exclude_post_types = apply_filters( 'rt_nginx_helper_comment_change_exclude_post_types', $purge_exceptions ); + + $this->log( '* * * * *' ); if ( in_array( $_post_type, $exclude_post_types, true ) ) { - if ( 'nav_menu_item' !== $_post_type ) { - $this->log( '* * * * *' ); - $this->log('* Post Type comment update - ' . $_post_type . ' - purge trigger excluded...'); - $this->log('* Filter: rt_nginx_helper_comment_change_exclude_post_types'); - $this->log( '* * * * *' ); - } + $this->log('* Post Type comment update - ' . $_post_type . ' - purge trigger excluded...'); + $this->log('* Filter: rt_nginx_helper_comment_change_exclude_post_types'); + $this->log( '* * * * *' ); return; } - $this->log( '* * * * *' ); $this->log( '* Blog :: ' . addslashes( get_bloginfo( 'name' ) ) . ' ( ' . $blog_id . ' ). ' ); $this->log( '* Post :: ' . get_the_title( $_post_id ) . ' ( ' . $_post_id . ' ) ' ); $this->log( "* Comment :: $_comment_id." ); @@ -190,13 +189,16 @@ public function purge_post( $post_id ) { # [ 'post_type1', 'post_type1' ] # we can also do exceptions by post/page slug too + $purge_exceptions = $nginx_helper_admin->options['homepage_purge_post_type_exceptions']; + $exclude_post_types = apply_filters( 'gp_nginx_helper_post_type_comment_change_homepage_purge_exceptions', $purge_exceptions ); + if ( 1 === (int) $nginx_helper_admin->options['purge_homepage_on_edit'] ) { - if ( ! in_array( $_post_type, $nginx_helper_admin->options['homepage_purge_post_type_exceptions'], true ) ) { + if ( ! in_array( $_post_type, $exclude_post_types, true ) ) { $this->_purge_homepage(); } else { $this->log('* * * * *'); $this->log('* Post Type update - ' . $_post_type . ' - homepage purge trigger excluded...'); - $this->log('* ' . $_post_type . ' in constant array: RT_WP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS'); + $this->log('* ' . $_post_type . ' in constant array: GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS'); $this->log('* * * * *'); } } diff --git a/admin/class-phpredis-purger.php b/admin/class-redis-purger.php similarity index 67% rename from admin/class-phpredis-purger.php rename to admin/class-redis-purger.php index b297d6a..8764649 100644 --- a/admin/class-phpredis-purger.php +++ b/admin/class-redis-purger.php @@ -10,13 +10,13 @@ */ /** - * Description of PhpRedis_Purger + * Description of Redis_Purger * * @package nginx-helper * @subpackage nginx-helper/admin * @author rtCamp */ -class PhpRedis_Purger extends Purger { +class Redis_Purger extends Purger { /** * PHP Redis api object. @@ -27,57 +27,125 @@ class PhpRedis_Purger extends Purger { */ public $redis_object; + public $redis_client; + /** * Initialize the class and set its properties. * * @since 2.0.0 */ - public function __construct() { + public function __construct( $redis_client = 'phpredis' ) { global $nginx_helper_admin; + $this->redis_client = $redis_client; + + $connection_array = []; + $path = $nginx_helper_admin->options['redis_unix_socket']; + $username = $nginx_helper_admin->options['redis_username']; + $password = $nginx_helper_admin->options['redis_password']; + $redis_database = $nginx_helper_admin->options['redis_database']; + try { - $this->redis_object = new Redis(); + switch ( $redis_client ) { - /*Composer sets default to version that doesn't allow modern php*/ - $redis_connection_others_array = array(); + case 'predis': - $path = $nginx_helper_admin->options['redis_unix_socket']; + if ( ! class_exists('Predis\Autoloader')) { + require_once NGINX_HELPER_BASEPATH . 'admin/predis.php'; + } - if ( $path ) { - $host = $path; - $port = 0; - } else { - $host = $nginx_helper_admin->options['redis_hostname']; - $port = $nginx_helper_admin->options['redis_port']; - } + Predis\Autoloader::register(); - $username = $nginx_helper_admin->options['redis_username']; - $password = $nginx_helper_admin->options['redis_password']; + if ($path) { + $connection_array['path'] = $path; + } else { + $connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; + $connection_array['port'] = $nginx_helper_admin->options['redis_port']; + } - if ( $username && $password ) { - $redis_connection_others_array['auth'] = [$username, $password]; - } + if ($username && $password) { + $connection_array['username'] = $username; + $connection_array['password'] = $password; + } + + $connection_array['timeout'] = $nginx_helper_admin->options['redis_timeout']; + $connection_array['read_write_timeout'] = $nginx_helper_admin->options['redis_read_timeout']; + + $this->redis_object = new Predis\Client($connection_array); + + $this->redis_object->connect(); + + break; + + case 'relay': + + $this->redis_object = new Relay\Relay; + + if ($path) { + $host = $path; + $port = -1; + } else { + $host = $nginx_helper_admin->options['redis_hostname']; + $port = $nginx_helper_admin->options['redis_port']; + } + + $this->redis_object->connect( + $host, + $port, + $nginx_helper_admin->options['redis_timeout'], + '', + 300, + $nginx_helper_admin->options['redis_read_timeout'] + ); + + if ($username && $password) { + $this->redis_object->auth([$username, $password]); + } + + $this->redis_object->select($redis_database); + + $this->redis_object->setOption(\Relay\Relay::OPT_USE_CACHE, false); + + break; + + case 'phpredis': + + default: + + $this->redis_object = new Redis(); + + if ($path) { + $host = $path; + $port = -1; + } else { + $host = $nginx_helper_admin->options['redis_hostname']; + $port = $nginx_helper_admin->options['redis_port']; + } - $this->redis_object->connect( - $host, - $port, - 5, - '', - 100, - 1.5, - $redis_connection_others_array - ); + if ($username && $password) { + $connection_array['auth'] = [$username, $password]; + } + + $this->redis_object->connect( + $host, + $port, + $nginx_helper_admin->options['redis_timeout'], + '', + 300, + $nginx_helper_admin->options['redis_read_timeout'], + $connection_array + ); - $redis_database = $nginx_helper_admin->options['redis_database']; + $this->redis_object->select($redis_database); - $this->redis_object->select($redis_database); + break; + } } catch ( Exception $e ) { $this->log( $e->getMessage(), 'ERROR' ); } - } /** @@ -242,7 +310,6 @@ public function custom_purge_urls() { } } } - } /** @@ -255,12 +322,18 @@ public function custom_purge_urls() { */ public function delete_single_key( $key ) { + $this->log( '- Redis Client: ' . $this->redis_client ); + try { - return $this->redis_object->del( $key ); + if ( 'predis' === $this->redis_client ) { + return $this->redis_object->executeRaw( array( 'DEL', $key ) ); + } else { + return $this->redis_object->del($key); + } } catch ( Exception $e ) { $this->log( $e->getMessage(), 'ERROR' ); + return 187; } - } /** @@ -279,6 +352,8 @@ public function delete_single_key( $key ) { */ public function delete_keys_by_wildcard( $pattern ) { + $this->log( '- Redis Client: ' . $this->redis_client ); + // Lua Script. $lua = <<redis_object->eval( $lua, array( $pattern ), 1 ); } catch ( Exception $e ) { $this->log( $e->getMessage(), 'ERROR' ); + return 187; } } diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index 6d0ff5d..cfa2530 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -196,7 +196,8 @@ echo '

'; esc_html_e( sprintf( - __("Set by wp-config.php constant: define( 'RT_WP_NGINX_HELPER_CACHE_METHOD', '%s' );", 'nginx-helper'), + __("Set by wp-config.php constant: define( '%s', '%s' );", 'nginx-helper'), + $cache_method_set_by_constant, $cache_method ) ); @@ -244,7 +245,8 @@ echo '

'; esc_html_e( sprintf( - __("Set by wp-config.php constant: define( 'RT_WP_NGINX_HELPER_PURGE_METHOD', '%s' );", 'nginx-helper'), + __("Set by wp-config.php constant: define( '%s', '%s' );", 'nginx-helper'), + $purge_method_set_by_constant, $purge_method ) ); @@ -380,14 +382,23 @@ if ( $redis_hostname_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_HOSTNAME', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_hostname_set_by_constant + ) + ); echo '

'; - } if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( 'Ignored! - UNIX socket is set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } else { @@ -412,14 +423,24 @@ if ( $redis_port_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_PORT', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_port_set_by_constant + ) + ); echo '

'; } if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( 'Ignored! - UNIX socket is set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } else { @@ -444,7 +465,12 @@ if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } @@ -459,7 +485,12 @@ if ( $redis_prefix_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_PREFIX', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_prefix_set_by_constant + ) + ); echo '

'; } @@ -474,7 +505,12 @@ if ( $redis_database_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_DATABASE', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_database_set_by_constant + ) + ); echo '

'; } @@ -492,7 +528,12 @@ if ( $redis_username_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_USERNAME', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_username_set_by_constant + ) + ); echo '

'; } @@ -510,7 +551,12 @@ if ( $redis_password_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_PASSWORD', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_password_set_by_constant + ) + ); echo '

'; } diff --git a/composer.json b/composer.json index f215446..f0742c1 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "source": "https://github.com/rtCamp/nginx-helper/" }, "require": { - "php": ">=5.3.2", + "php": ">=5.4", "composer/installers": "^1.0" }, "require-dev": { diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index f96e6d0..c1d7020 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -173,17 +173,17 @@ private function define_admin_hooks() { // Defines global variables. if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) { - if ( class_exists( 'Redis' ) ) { // Use PHP5-Redis extension if installed. - - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-phpredis-purger.php'; - $nginx_purger = new PhpRedis_Purger(); - + if ( $nginx_helper_admin->options['relay_client'] ) { + $redis_client = 'relay'; + } elseif ( class_exists( 'Redis' ) ) { + $redis_client = 'phpredis'; } else { + $redis_client = 'predis'; + } - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-predis-purger.php'; - $nginx_purger = new Predis_Purger(); + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; + $nginx_purger = new Redis_Purger( $redis_client ); - } } else { require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-fastcgi-purger.php';