Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Buffer;
|
||||
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Handle page cache and optimizations.
|
||||
*
|
||||
* @since 3.3
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract class Abstract_Buffer {
|
||||
|
||||
/**
|
||||
* Process identifier used by the logger.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $process_id;
|
||||
|
||||
/**
|
||||
* Instance of the Tests class.
|
||||
*
|
||||
* @var Tests
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $tests;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param Tests $tests Tests instance.
|
||||
*/
|
||||
public function __construct( Tests $tests ) {
|
||||
$this->tests = $tests;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PROCESS ================================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Launch the process if the tests succeed.
|
||||
* This should be the first thing to use after initializing the class.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @see $this->tests->can_init_process()
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract public function maybe_init_process();
|
||||
|
||||
/**
|
||||
* Process the page buffer if the 2nd set of tests succeed.
|
||||
* It should be used like this:
|
||||
* ob_start( [ $this, 'maybe_process_buffer' ] );
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @see $this->tests->can_process_buffer()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $buffer The buffer content.
|
||||
* @return string The buffered content
|
||||
*/
|
||||
abstract public function maybe_process_buffer( $buffer );
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** LOG ===================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Log the last test "error".
|
||||
*
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected function log_last_test_error() {
|
||||
$error = $this->tests->get_last_error();
|
||||
|
||||
$this->log( $error['message'], $error['data'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Log events.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $message A message to log.
|
||||
* @param array $data Related data.
|
||||
* @param string $type Event type to log. Possible values are 'info', 'error', and 'debug' (default).
|
||||
*/
|
||||
protected function log( $message, $data = [], $type = 'debug' ) {
|
||||
$data = array_merge(
|
||||
[
|
||||
$this->get_process_id(),
|
||||
'request_uri' => $this->tests->get_raw_request_uri(),
|
||||
],
|
||||
$data
|
||||
);
|
||||
|
||||
if ( isset( $data['cookies'] ) ) {
|
||||
$data['cookies'] = Logger::remove_auth_cookies( $data['cookies'] );
|
||||
}
|
||||
|
||||
switch ( $type ) {
|
||||
case 'info':
|
||||
Logger::info( $message, $data );
|
||||
break;
|
||||
case 'error':
|
||||
Logger::error( $message, $data );
|
||||
break;
|
||||
default:
|
||||
Logger::debug( $message, $data );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the process identifier.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_process_id() {
|
||||
return $this->process_id . ' - Thread #' . Logger::get_thread_id();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if the page content is HTML.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $buffer The buffer content.
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_html( $buffer ) {
|
||||
return preg_match( '/<\/html>/i', $buffer );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,802 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Buffer;
|
||||
|
||||
/**
|
||||
* Handle page cache.
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
class Cache extends Abstract_Buffer {
|
||||
|
||||
/**
|
||||
* Process identifier used by the logger.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.3
|
||||
*/
|
||||
protected $process_id = 'caching process';
|
||||
|
||||
/**
|
||||
* Tests instance
|
||||
*
|
||||
* @var Tests
|
||||
*/
|
||||
protected $tests;
|
||||
|
||||
/**
|
||||
* Config instance
|
||||
*
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* Path to the directory containing the cache files.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.3
|
||||
*/
|
||||
private $cache_dir_path;
|
||||
|
||||
/**
|
||||
* Exclude urls from wp canonical redirect.
|
||||
*
|
||||
* @var array Array of url patterns to exclude from wp canonical redirect.
|
||||
*/
|
||||
private $wp_redirect_exclusions = [
|
||||
'(.*)wp\-json(/.*|$)',
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param Tests $tests Tests instance.
|
||||
* @param Config $config Config instance.
|
||||
* @param array $args {
|
||||
* An array of arguments.
|
||||
*
|
||||
* @type string $cache_dir_path Path to the directory containing the cache files.
|
||||
* }
|
||||
*/
|
||||
public function __construct( Tests $tests, Config $config, array $args ) {
|
||||
$this->config = $config;
|
||||
$this->cache_dir_path = rtrim( $args['cache_dir_path'], '/' ) . '/';
|
||||
|
||||
parent::__construct( $tests );
|
||||
|
||||
$this->log( 'CACHING PROCESS STARTED.', [], 'info' );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** CACHE =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Serve the cache file if it exists. If not, init the buffer.
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
public function maybe_init_process() {
|
||||
if ( ! $this->tests->can_init_process() ) {
|
||||
$this->define_donotoptimize_true();
|
||||
$this->log_last_test_error();
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->maybe_allow_wp_redirect() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve the cache file if it exists.
|
||||
*/
|
||||
$cache_filepath = $this->get_cache_path();
|
||||
|
||||
$this->log(
|
||||
'Looking for cache file.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
]
|
||||
);
|
||||
|
||||
$cache_filepath_gzip = $cache_filepath . '_gzip';
|
||||
$accept_encoding = $this->config->get_server_input( 'HTTP_ACCEPT_ENCODING' );
|
||||
$accept_gzip = $accept_encoding && false !== strpos( $accept_encoding, 'gzip' );
|
||||
|
||||
// Check if cache file exist.
|
||||
if ( $accept_gzip && is_readable( $cache_filepath_gzip ) ) {
|
||||
$this->serve_gzip_cache_file( $cache_filepath_gzip );
|
||||
}
|
||||
|
||||
if ( is_readable( $cache_filepath ) ) {
|
||||
$this->serve_cache_file( $cache_filepath );
|
||||
}
|
||||
|
||||
// Maybe we're looking for a webp file.
|
||||
$cache_filename = basename( $cache_filepath );
|
||||
|
||||
if ( strpos( $cache_filename, '-webp' ) !== false ) {
|
||||
// We're looking for a webp file that doesn't exist: try to locate any `.no-webp` file.
|
||||
$cache_dir_path = rtrim( dirname( $cache_filepath ), '/\\' ) . DIRECTORY_SEPARATOR;
|
||||
|
||||
if ( file_exists( $cache_dir_path . '.no-webp' ) ) {
|
||||
// We have a `.no-webp` file: try to deliver a non-webp cache file.
|
||||
$cache_filepath = $cache_dir_path . str_replace( '-webp', '', $cache_filename );
|
||||
$cache_filepath_gzip = $cache_filepath . '_gzip';
|
||||
|
||||
$this->log(
|
||||
'Looking for non-webp cache file.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
]
|
||||
);
|
||||
|
||||
// Try to deliver the non-webp version instead.
|
||||
if ( $accept_gzip && is_readable( $cache_filepath_gzip ) ) {
|
||||
$this->serve_gzip_cache_file( $cache_filepath_gzip );
|
||||
}
|
||||
|
||||
if ( is_readable( $cache_filepath ) ) {
|
||||
$this->serve_cache_file( $cache_filepath );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* No cache file yet: launch caching process.
|
||||
*/
|
||||
$this->log(
|
||||
'Start buffer.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
]
|
||||
);
|
||||
|
||||
ob_start( [ $this, 'maybe_process_buffer' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve a cache file.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $cache_filepath Path to the cache file.
|
||||
*/
|
||||
private function serve_cache_file( $cache_filepath ) {
|
||||
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', filemtime( $cache_filepath ) ) . ' GMT' );
|
||||
|
||||
$if_modified_since = $this->get_if_modified_since();
|
||||
|
||||
// Checking if the client is validating his cache and if it is current.
|
||||
if ( $if_modified_since && ( strtotime( $if_modified_since ) === @filemtime( $cache_filepath ) ) ) {
|
||||
// Client's cache is current, so we just respond '304 Not Modified'.
|
||||
header( $this->config->get_server_input( 'SERVER_PROTOCOL', '' ) . ' 304 Not Modified', true, 304 );
|
||||
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
|
||||
header( 'Cache-Control: no-cache, must-revalidate' );
|
||||
|
||||
$this->log(
|
||||
'Serving `304` cache file.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
'modified' => $if_modified_since,
|
||||
],
|
||||
'info'
|
||||
);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Serve the cache if file isn't store in the client browser cache.
|
||||
readfile( $cache_filepath ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile
|
||||
|
||||
$this->log(
|
||||
'Serving cache file.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
'modified' => $if_modified_since,
|
||||
],
|
||||
'info'
|
||||
);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve a gzipped cache file.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $cache_filepath Path to the gzip cache file.
|
||||
*/
|
||||
private function serve_gzip_cache_file( $cache_filepath ) {
|
||||
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', filemtime( $cache_filepath ) ) . ' GMT' );
|
||||
|
||||
$if_modified_since = $this->get_if_modified_since();
|
||||
|
||||
// Checking if the client is validating his cache and if it is current.
|
||||
if ( $if_modified_since && ( strtotime( $if_modified_since ) === @filemtime( $cache_filepath ) ) ) {
|
||||
// Client's cache is current, so we just respond '304 Not Modified'.
|
||||
header( $this->config->get_server_input( 'SERVER_PROTOCOL', '' ) . ' 304 Not Modified', true, 304 );
|
||||
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
|
||||
header( 'Cache-Control: no-cache, must-revalidate' );
|
||||
|
||||
$this->log(
|
||||
'Serving `304` gzip cache file.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
'modified' => $if_modified_since,
|
||||
],
|
||||
'info'
|
||||
);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Serve the cache if file isn't store in the client browser cache.
|
||||
readgzfile( $cache_filepath );
|
||||
|
||||
$this->log(
|
||||
'Serving gzip cache file.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
'modified' => $if_modified_since,
|
||||
],
|
||||
'info'
|
||||
);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe cache the page content.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $buffer The buffer content.
|
||||
* @return string The buffered content.
|
||||
*/
|
||||
public function maybe_process_buffer( $buffer ) {
|
||||
if ( ! $this->tests->can_process_buffer( $buffer ) ) {
|
||||
$this->log_last_test_error();
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$footprint = '';
|
||||
$is_html = $this->is_html( $buffer );
|
||||
|
||||
if ( ! static::can_generate_caching_files() ) {
|
||||
// Not allowed to generate cache files.
|
||||
if ( $is_html ) {
|
||||
$footprint = $this->get_rocket_footprint();
|
||||
}
|
||||
|
||||
$this->log(
|
||||
'Page not cached by filter.',
|
||||
[
|
||||
'filter' => 'do_rocket_generate_caching_files',
|
||||
]
|
||||
);
|
||||
return $buffer . $footprint;
|
||||
}
|
||||
|
||||
$webp_enabled = preg_match( '@<!-- Rocket (has|no) webp -->@', $buffer, $webp_tag );
|
||||
$has_webp = ! empty( $webp_tag ) ? 'has' === $webp_tag[1] : false;
|
||||
$cache_filepath = $this->get_cache_path( [ 'webp' => $has_webp ] );
|
||||
$cache_dir_path = dirname( $cache_filepath );
|
||||
|
||||
// Create cache folders.
|
||||
rocket_mkdir_p( $cache_dir_path );
|
||||
|
||||
if ( $is_html ) {
|
||||
$footprint = $this->get_rocket_footprint( time() );
|
||||
}
|
||||
|
||||
// Webp request.
|
||||
if ( $webp_enabled ) {
|
||||
$buffer = str_replace( $webp_tag[0], '', $buffer );
|
||||
|
||||
if ( ! $has_webp ) {
|
||||
// The buffer doesn’t contain webp files.
|
||||
$cache_dir_path = rtrim( dirname( $cache_filepath ), '/\\' );
|
||||
|
||||
$this->maybe_create_nowebp_file( $cache_dir_path );
|
||||
}
|
||||
}
|
||||
|
||||
$this->write_cache_file( $cache_filepath, $buffer . $footprint );
|
||||
$this->maybe_create_nginx_mobile_file( $cache_dir_path );
|
||||
|
||||
// Send headers with the last modified time of the cache file.
|
||||
if ( file_exists( $cache_filepath ) ) {
|
||||
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', filemtime( $cache_filepath ) ) . ' GMT' );
|
||||
}
|
||||
|
||||
if ( $is_html ) {
|
||||
$footprint = $this->get_rocket_footprint();
|
||||
}
|
||||
|
||||
$this->log(
|
||||
'Page cached.',
|
||||
[
|
||||
'path' => $cache_filepath,
|
||||
],
|
||||
'info'
|
||||
);
|
||||
|
||||
return $buffer . $footprint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the cache file(s)
|
||||
*
|
||||
* @since 3.5
|
||||
*
|
||||
* @param string $cache_filepath Absolute path to the cache file.
|
||||
* @param string $content Content to write in the cache file.
|
||||
* @return void
|
||||
*/
|
||||
private function write_cache_file( $cache_filepath, $content ) {
|
||||
$gzip_filepath = $cache_filepath . '_gzip';
|
||||
$temp_filepath = $cache_filepath . '_temp';
|
||||
$temp_gzip_filepath = $gzip_filepath . '_temp';
|
||||
|
||||
if ( rocket_direct_filesystem()->exists( $temp_filepath ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the cache file.
|
||||
if ( ! rocket_put_content( $temp_filepath, $content ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_direct_filesystem()->move( $temp_filepath, $cache_filepath, true );
|
||||
|
||||
if ( function_exists( 'gzencode' ) ) {
|
||||
/**
|
||||
* Filters the Gzip compression level to use for the cache file
|
||||
*
|
||||
* @param int $compression_level Compression level between 0 and 9.
|
||||
*/
|
||||
$compression_level = apply_filters( 'rocket_gzencode_level_compression', 6 );
|
||||
|
||||
if ( ! rocket_put_content( $temp_gzip_filepath, gzencode( $content, $compression_level ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_direct_filesystem()->move( $temp_gzip_filepath, $gzip_filepath, true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the cache file.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param array $args {
|
||||
* A list of arguments.
|
||||
*
|
||||
* @type bool $webp Set to false to prevent adding the part related to webp.
|
||||
* }
|
||||
* @return string
|
||||
*/
|
||||
public function get_cache_path( $args = [] ) {
|
||||
$args = array_merge(
|
||||
[
|
||||
'webp' => true,
|
||||
],
|
||||
$args
|
||||
);
|
||||
$cookies = $this->tests->get_cookies();
|
||||
$request_uri_path = $this->get_request_cache_path( $cookies );
|
||||
$filename = 'index';
|
||||
|
||||
$filename = $this->maybe_mobile_filename( $filename );
|
||||
|
||||
// Rename the caching filename for SSL URLs.
|
||||
if ( is_ssl() && $this->config->get_config( 'cache_ssl' ) ) {
|
||||
$filename .= '-https';
|
||||
}
|
||||
|
||||
if ( $args['webp'] ) {
|
||||
$filename = $this->maybe_webp_filename( $filename );
|
||||
}
|
||||
|
||||
$filename = $this->maybe_dynamic_cookies_filename( $filename, $cookies );
|
||||
|
||||
// Ensure proper formatting of the path.
|
||||
$request_uri_path = preg_replace_callback( '/%[0-9A-F]{2}/', [ $this, 'reset_lowercase' ], $request_uri_path );
|
||||
// Directories in Windows can't contain question marks.
|
||||
$request_uri_path = str_replace( '?', '#', $request_uri_path );
|
||||
// Limit filename max length to 255 characters.
|
||||
$request_uri_path .= '/' . substr( $filename, 0, 250 ) . '.html';
|
||||
|
||||
return $request_uri_path;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Declares and sets value of constant preventing Optimizations.
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
private function define_donotoptimize_true() {
|
||||
if ( ! defined( 'DONOTROCKETOPTIMIZE' ) ) {
|
||||
define( 'DONOTROCKETOPTIMIZE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets If-modified-since header value
|
||||
*
|
||||
* @since 3.3
|
||||
* @return string
|
||||
*/
|
||||
private function get_if_modified_since() {
|
||||
if ( function_exists( 'apache_request_headers' ) ) {
|
||||
$headers = apache_request_headers();
|
||||
|
||||
return isset( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : '';
|
||||
}
|
||||
|
||||
return $this->config->get_server_input( 'HTTP_IF_MODIFIED_SINCE', '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get WP Rocket footprint
|
||||
*
|
||||
* @since 3.0.5 White label footprint if WP_ROCKET_WHITE_LABEL_FOOTPRINT is defined.
|
||||
* @since 2.0
|
||||
*
|
||||
* @param int $time UNIX timestamp when the cache file was saved.
|
||||
* @return string The footprint that will be printed
|
||||
*/
|
||||
private function get_rocket_footprint( $time = '' ) {
|
||||
$footprint = defined( 'WP_ROCKET_WHITE_LABEL_FOOTPRINT' ) ?
|
||||
"\n" . '<!-- Cached for great performance' :
|
||||
"\n" . '<!-- This website is like a Rocket, isn\'t it? Performance optimized by ' . WP_ROCKET_PLUGIN_NAME . '. Learn more: https://wp-rocket.me';
|
||||
if ( ! empty( $time ) ) {
|
||||
$footprint .= ' - Debug: cached@' . $time;
|
||||
}
|
||||
$footprint .= ' -->';
|
||||
return $footprint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a hidden empty file for mobile detection on NGINX with the Rocket NGINX configuration.
|
||||
*
|
||||
* @param string $cache_dir_path Path to the current cache directory.
|
||||
* @return void
|
||||
*/
|
||||
private function maybe_create_nginx_mobile_file( $cache_dir_path ) {
|
||||
global $is_nginx;
|
||||
|
||||
if ( ! $this->config->get_config( 'do_caching_mobile_files' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $is_nginx ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nginx_mobile_detect = $cache_dir_path . '/.mobile-active';
|
||||
|
||||
if ( rocket_direct_filesystem()->exists( $nginx_mobile_detect ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_direct_filesystem()->touch( $nginx_mobile_detect );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a hidden empty file when webp is enabled but the buffer doesn’t contain webp files.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @param string $cache_dir_path Path to the current cache directory (without trailing slah).
|
||||
*/
|
||||
private function maybe_create_nowebp_file( $cache_dir_path ) {
|
||||
$nowebp_filepath = $cache_dir_path . DIRECTORY_SEPARATOR . '.no-webp';
|
||||
|
||||
if ( rocket_direct_filesystem()->exists( $nowebp_filepath ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_direct_filesystem()->touch( $nowebp_filepath );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if generating cache files is allowed.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function can_generate_caching_files() {
|
||||
/**
|
||||
* Allow to the generate the caching file.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @param bool True will force the cache file generation.
|
||||
*/
|
||||
return (bool) apply_filters( 'do_rocket_generate_caching_files', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the base cache path for the current request
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param array $cookies Cookies for the current request.
|
||||
* @return string
|
||||
*/
|
||||
private function get_request_cache_path( $cookies ) {
|
||||
$host = $this->config->get_host();
|
||||
|
||||
if ( $this->config->get_config( 'url_no_dots' ) ) {
|
||||
$host = str_replace( '.', '_', $host );
|
||||
}
|
||||
|
||||
$request_uri = $this->tests->get_clean_request_uri();
|
||||
$cookie_hash = $this->config->get_config( 'cookie_hash' );
|
||||
$logged_in_cookie = $this->config->get_config( 'logged_in_cookie' );
|
||||
$logged_in_cookie_no_hash = str_replace( $cookie_hash, '', $logged_in_cookie );
|
||||
|
||||
// Get cache folder of host name.
|
||||
if ( $logged_in_cookie && isset( $cookies[ $logged_in_cookie ] ) && ! $this->tests->has_rejected_cookie( $logged_in_cookie_no_hash ) ) {
|
||||
if ( $this->config->get_config( 'common_cache_logged_users' ) ) {
|
||||
return $this->cache_dir_path . $host . '-loggedin-' . $this->config->get_config( 'secret_cache_key' ) . rtrim( $request_uri, '/' );
|
||||
}
|
||||
|
||||
$user_key = explode( '|', $cookies[ $logged_in_cookie ] );
|
||||
$user_key = reset( $user_key );
|
||||
$user_key = $this->sanitize_user( $user_key ) . '-' . $this->config->get_config( 'secret_cache_key' );
|
||||
|
||||
// Get cache folder of host name.
|
||||
return $this->cache_dir_path . $host . '-' . $user_key . rtrim( $request_uri, '/' );
|
||||
}
|
||||
|
||||
return $this->cache_dir_path . $host . rtrim( $request_uri, '/' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the filename if the request is from a mobile device.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $filename Cache filename.
|
||||
* @return string
|
||||
*/
|
||||
private function maybe_mobile_filename( $filename ) {
|
||||
$cache_mobile_files_tablet = $this->config->get_config( 'cache_mobile_files_tablet' );
|
||||
|
||||
if ( ! ( $this->config->get_config( 'cache_mobile' ) && $this->config->get_config( 'do_caching_mobile_files' ) ) ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
if ( ! $cache_mobile_files_tablet ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WP_Rocket_Mobile_Detect' ) ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
$detect = new \WP_Rocket_Mobile_Detect();
|
||||
|
||||
if ( $detect->isMobile() && ! $detect->isTablet() && 'desktop' === $cache_mobile_files_tablet || ( $detect->isMobile() || $detect->isTablet() ) && 'mobile' === $cache_mobile_files_tablet ) {
|
||||
return $filename .= '-mobile';
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the filename if the request is WebP compatible
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @param string $filename Cache filename.
|
||||
* @return string
|
||||
*/
|
||||
private function maybe_webp_filename( $filename ) {
|
||||
if ( ! $this->config->get_config( 'cache_webp' ) ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force WP Rocket to disable its webp cache.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @param bool $disable_webp_cache Set to true to disable the webp cache.
|
||||
*/
|
||||
$disable_webp_cache = apply_filters( 'rocket_disable_webp_cache', false );
|
||||
|
||||
if ( $disable_webp_cache ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
if ( ! $this->is_browser_webp_compatible() ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
return $filename . '-webp';
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the browser is WebP compatible
|
||||
*
|
||||
* @since 3.12.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_browser_webp_compatible(): bool {
|
||||
// Only to supporting browsers.
|
||||
$http_accept = $this->config->get_server_input( 'HTTP_ACCEPT', '' );
|
||||
|
||||
if (
|
||||
empty( $http_accept )
|
||||
&&
|
||||
function_exists( 'apache_request_headers' )
|
||||
) {
|
||||
$headers = apache_request_headers();
|
||||
$http_accept = isset( $headers['Accept'] ) ? $headers['Accept'] : '';
|
||||
}
|
||||
|
||||
if (
|
||||
! empty( $http_accept )
|
||||
&&
|
||||
false !== strpos( $http_accept, 'webp' )
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->is_user_agent_compatible();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the User Agent if the Accept headers is missing the WebP info
|
||||
*
|
||||
* @since 3.12.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_user_agent_compatible(): bool {
|
||||
$user_agent = $this->config->get_server_input( 'HTTP_USER_AGENT' );
|
||||
|
||||
if ( empty( $user_agent ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( preg_match( '#Firefox/(?<version>[0-9]{2,})#i', $user_agent, $matches ) ) {
|
||||
if ( 66 >= (int) $matches['version'] ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( preg_match( '#(?:iPad|iPhone)(.*)Version/(?<version>[0-9]{2,})#i', $user_agent, $matches ) ) {
|
||||
if ( 14 > (int) $matches['version'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( preg_match( '#Version/(?<version>[0-9]{2,})(?:.*)Safari#i', $user_agent, $matches ) ) {
|
||||
if ( 16 > (int) $matches['version'] ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the filename if dynamic cookies are set
|
||||
*
|
||||
* @param string $filename Cache filename.
|
||||
* @param array $cookies Cookies for the request.
|
||||
* @return string
|
||||
*/
|
||||
private function maybe_dynamic_cookies_filename( $filename, $cookies ) {
|
||||
$cache_dynamic_cookies = $this->config->get_config( 'cache_dynamic_cookies' );
|
||||
|
||||
if ( ! $cache_dynamic_cookies ) {
|
||||
return $filename;
|
||||
}
|
||||
|
||||
foreach ( $cache_dynamic_cookies as $key => $cookie_name ) {
|
||||
if ( is_array( $cookie_name ) ) {
|
||||
if ( isset( $_COOKIE[ $key ] ) ) {
|
||||
foreach ( $cookie_name as $cookie_key ) {
|
||||
if ( '' !== $cookies[ $key ][ $cookie_key ] ) {
|
||||
$cache_key = $cookies[ $key ][ $cookie_key ];
|
||||
$cache_key = preg_replace( '/[^a-z0-9_\-]/i', '-', $cache_key );
|
||||
$filename .= '-' . $cache_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $cookies[ $cookie_name ] ) && '' !== $cookies[ $cookie_name ] ) {
|
||||
$cache_key = $cookies[ $cookie_name ];
|
||||
$cache_key = preg_replace( '/[^a-z0-9_\-]/i', '-', $cache_key );
|
||||
$filename .= '-' . $cache_key;
|
||||
}
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force lowercase on encoded url strings from different alphabets to prevent issues on some hostings.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param array $matches Cache path.
|
||||
* @return string Cache path in lowercase.
|
||||
*/
|
||||
protected function reset_lowercase( $matches ) {
|
||||
return strtolower( $matches[0] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes a string username.
|
||||
*
|
||||
* @param string $user String username.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function sanitize_user( string $user = '' ): string {
|
||||
return strtolower( rawurlencode( $user ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if permalink structure and url match.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function maybe_allow_wp_redirect(): bool {
|
||||
|
||||
$exclusions = implode( '|', $this->wp_redirect_exclusions );
|
||||
|
||||
// Return early for excluded urls.
|
||||
if ( preg_match( '#' . $exclusions . '#', $this->tests->get_request_uri_base() )
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$permalink_structure = $this->config->get_config( 'permalink_structure' );
|
||||
|
||||
// Last character of permalink.
|
||||
$permalink_last_char = '/' !== substr( $permalink_structure, -1 ) ? '' : '/';
|
||||
|
||||
// Request uri without protocol & domain name.
|
||||
$request_uri = $this->tests->get_request_uri_base();
|
||||
|
||||
// Last character of request uri.
|
||||
$request_uri_last_char = '/' !== substr( $request_uri, -1 ) ? '' : '/';
|
||||
|
||||
// In cases where we have the home with a trailng slash (visible or invisible)
|
||||
// and permalink is without trailing slash.
|
||||
if ( '' === $permalink_last_char ) {
|
||||
// Check for root installation.
|
||||
$request_uri_last_char = '/' === $request_uri ? '' : $request_uri_last_char;
|
||||
|
||||
/**
|
||||
* Check for subdir installation.
|
||||
* Use config file name to get home request_uri.
|
||||
*/
|
||||
$home = str_replace( $this->config->get_host(), '', basename( $this->config->get_config_file_path()['path'] ) );
|
||||
$home = str_replace( '.', '/', str_replace( '.php', '', $home ) );
|
||||
|
||||
if ( '/' !== $request_uri && rtrim( $request_uri, '/' ) === $home ) {
|
||||
$request_uri_last_char = '';
|
||||
}
|
||||
}
|
||||
|
||||
return $permalink_last_char !== $request_uri_last_char;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Buffer;
|
||||
|
||||
/**
|
||||
* Configuration class for WP Rocket cache
|
||||
*
|
||||
* @since 3.3
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Config {
|
||||
use \WP_Rocket\Traits\Memoize;
|
||||
|
||||
/**
|
||||
* Path to the directory containing the config files.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.3
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private static $config_dir_path;
|
||||
|
||||
/**
|
||||
* Values of $_SERVER to use for some tests.
|
||||
*
|
||||
* @var array
|
||||
* @since 3.3
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private static $server;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $args {
|
||||
* An array of arguments.
|
||||
*
|
||||
* @type string $config_dir_path WP Rocket config directory path.
|
||||
* @type array $server Values of $_SERVER to use for the tests. Default is $_SERVER.
|
||||
* }
|
||||
*/
|
||||
public function __construct( $args ) {
|
||||
if ( isset( self::$config_dir_path ) ) {
|
||||
// Make sure to keep the same values all along.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! isset( $args['server'] ) && ! empty( $_SERVER ) && is_array( $_SERVER ) ) {
|
||||
$args['server'] = $_SERVER;
|
||||
}
|
||||
|
||||
self::$config_dir_path = rtrim( $args['config_dir_path'], '/' ) . '/';
|
||||
self::$server = ! empty( $args['server'] ) && is_array( $args['server'] ) ? $args['server'] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a $_SERVER entry.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $entry_name Name of the entry.
|
||||
* @param mixed $default Value to return if the entry is not set.
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_server_input( $entry_name, $default = null ) {
|
||||
if ( ! isset( self::$server[ $entry_name ] ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return self::$server[ $entry_name ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the `server` property.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_server() {
|
||||
return self::$server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific config/option value.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $config_name Name of a specific config/option.
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_config( $config_name ) {
|
||||
$config = $this->get_configs();
|
||||
return isset( $config[ $config_name ] ) ? $config[ $config_name ] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the whole current configuration.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array|bool An array containing the configuration. False on failure.
|
||||
*/
|
||||
public function get_configs() {
|
||||
if ( self::is_memoized( __FUNCTION__ ) ) {
|
||||
return self::get_memoized( __FUNCTION__ );
|
||||
}
|
||||
|
||||
$config_file_path = $this->get_config_file_path();
|
||||
|
||||
if ( ! $config_file_path['success'] ) {
|
||||
return self::memoize( __FUNCTION__, [], false );
|
||||
}
|
||||
|
||||
include $config_file_path['path'];
|
||||
|
||||
$config = [
|
||||
'cookie_hash' => '',
|
||||
'logged_in_cookie' => '',
|
||||
'common_cache_logged_users' => 0,
|
||||
'cache_mobile_files_tablet' => 'desktop',
|
||||
'cache_ssl' => 0,
|
||||
'cache_webp' => 0,
|
||||
'cache_mobile' => 0,
|
||||
'do_caching_mobile_files' => 0,
|
||||
'secret_cache_key' => '',
|
||||
'cache_reject_uri' => '',
|
||||
'cache_query_strings' => [],
|
||||
'cache_ignored_parameters' => [],
|
||||
'cache_reject_cookies' => '',
|
||||
'cache_reject_ua' => '',
|
||||
'cache_mandatory_cookies' => '',
|
||||
'cache_dynamic_cookies' => [],
|
||||
'url_no_dots' => 0,
|
||||
'permalink_structure' => '',
|
||||
];
|
||||
|
||||
foreach ( $config as $entry_name => $entry_value ) {
|
||||
$var_name = 'rocket_' . $entry_name;
|
||||
|
||||
if ( isset( $$var_name ) ) {
|
||||
$config[ $entry_name ] = $$var_name;
|
||||
}
|
||||
}
|
||||
|
||||
return self::memoize( __FUNCTION__, [], $config );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the host, to use for config and cache file path.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_host() {
|
||||
if ( self::is_memoized( __FUNCTION__ ) ) {
|
||||
return self::get_memoized( __FUNCTION__ );
|
||||
}
|
||||
|
||||
$host = $this->get_server_input( 'HTTP_HOST', (string) time() );
|
||||
$host = preg_replace( '/:\d+$/', '', $host );
|
||||
$host = trim( strtolower( $host ), '.' );
|
||||
|
||||
return self::memoize( __FUNCTION__, [], rawurlencode( $host ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to an existing config file.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool The path to the file. False if no file is found.
|
||||
*/
|
||||
public function get_config_file_path() {
|
||||
if ( self::is_memoized( __FUNCTION__ ) ) {
|
||||
return self::get_memoized( __FUNCTION__ );
|
||||
}
|
||||
|
||||
$config_dir_real_path = realpath( self::$config_dir_path ) . DIRECTORY_SEPARATOR;
|
||||
|
||||
$host = $this->get_host();
|
||||
|
||||
$path = str_replace( '\\', '/', strtok( $this->get_server_input( 'REQUEST_URI', '' ), '?' ) );
|
||||
$path = preg_replace( '|(?<=.)/+|', '/', $path );
|
||||
$path = explode( '%2F', preg_replace( '/^(?:%2F)*(.*?)(?:%2F)*$/', '$1', rawurlencode( $path ) ) );
|
||||
// Remove empty array values.
|
||||
$path = array_filter( $path );
|
||||
|
||||
/**
|
||||
* If path is not empty.
|
||||
* i.e url with something like this `multisite/green/sample-page` after the host.
|
||||
*/
|
||||
if ( ! empty( $path ) ) {
|
||||
$config_file_paths = [];
|
||||
|
||||
// Loop through paths and store valid config file paths matching the url current path in an array.
|
||||
foreach ( $path as $p ) {
|
||||
static $dir;
|
||||
|
||||
if ( realpath( self::$config_dir_path . $host . '.' . $p . '.php' ) && 0 === stripos( realpath( self::$config_dir_path . $host . '.' . $p . '.php' ), $config_dir_real_path ) ) {
|
||||
$config_file_paths[] = self::$config_dir_path . $host . '.' . $p . '.php';
|
||||
}
|
||||
|
||||
if ( realpath( self::$config_dir_path . $host . '.' . $dir . $p . '.php' ) && 0 === stripos( realpath( self::$config_dir_path . $host . '.' . $dir . $p . '.php' ), $config_dir_real_path ) ) {
|
||||
$config_file_paths[] = self::$config_dir_path . $host . '.' . $dir . $p . '.php';
|
||||
}
|
||||
|
||||
$dir .= $p . '.';
|
||||
}
|
||||
|
||||
// Reverse array order so that subsite config file paths can come first.
|
||||
$config_file_paths = array_reverse( $config_file_paths );
|
||||
|
||||
/**
|
||||
* Check if there was a matching config file for the url current path
|
||||
* and return the first
|
||||
*/
|
||||
if ( ! empty( $config_file_paths ) ) {
|
||||
return self::memoize(
|
||||
__FUNCTION__,
|
||||
[],
|
||||
[
|
||||
'success' => true,
|
||||
'path' => $config_file_paths[0],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( realpath( self::$config_dir_path . $host . '.php' ) && 0 === stripos( realpath( self::$config_dir_path . $host . '.php' ), $config_dir_real_path ) ) {
|
||||
$config_file_path = self::$config_dir_path . $host . '.php';
|
||||
return self::memoize(
|
||||
__FUNCTION__,
|
||||
[],
|
||||
[
|
||||
'success' => true,
|
||||
'path' => $config_file_path,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return self::memoize(
|
||||
__FUNCTION__,
|
||||
[],
|
||||
[
|
||||
'success' => false,
|
||||
'path' => self::$config_dir_path . $host . implode( '/', $path ) . '.php',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** SPECIFIC CONFIG GETTERS ================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get rejected cookies as a regex pattern.
|
||||
* `#` is used as pattern delimiter.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_rejected_cookies() {
|
||||
$rejected_cookies = $this->get_config( 'cache_reject_cookies' );
|
||||
|
||||
if ( '' === $rejected_cookies ) {
|
||||
return $rejected_cookies;
|
||||
}
|
||||
|
||||
return '#' . $rejected_cookies . '#';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mandatory cookies as a regex pattern.
|
||||
* `#` is used as pattern delimiter.
|
||||
*
|
||||
* @since 3.3
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_mandatory_cookies() {
|
||||
$mandatory_cookies = $this->get_config( 'cache_mandatory_cookies' );
|
||||
|
||||
if ( '' === $mandatory_cookies ) {
|
||||
return $mandatory_cookies;
|
||||
}
|
||||
|
||||
return '#' . $mandatory_cookies . '#';
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace WP_Rocket\ServiceProvider;
|
||||
|
||||
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
|
||||
|
||||
/**
|
||||
* Service provider for WP Rocket features common for admin and front
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
class Common_Subscribers extends AbstractServiceProvider {
|
||||
|
||||
/**
|
||||
* The provides array is a way to let the container
|
||||
* know that a service is provided by this service
|
||||
* provider. Every service that is registered via
|
||||
* this service provider must have an alias added
|
||||
* to this array or it will be ignored.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = [
|
||||
'detect_missing_tags_subscriber',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers items with the container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$this->getContainer()->share( 'detect_missing_tags_subscriber', 'WP_Rocket\Subscriber\Tools\Detect_Missing_Tags_Subscriber' )
|
||||
->addTag( 'common_subscriber' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace WP_Rocket\ServiceProvider;
|
||||
|
||||
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
|
||||
|
||||
/**
|
||||
* Service provider for the WP Rocket options
|
||||
*
|
||||
* @since 3.3
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Options extends AbstractServiceProvider {
|
||||
|
||||
/**
|
||||
* The provides array is a way to let the container
|
||||
* know that a service is provided by this service
|
||||
* provider. Every service that is registered via
|
||||
* this service provider must have an alias added
|
||||
* to this array or it will be ignored.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = [
|
||||
'options',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers the option array in the container
|
||||
*
|
||||
* @since 3.3
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$this->getContainer()->add( 'options', 'WP_Rocket\Admin\Options_Data' )
|
||||
->addArgument( $this->getContainer()->get( 'options_api' )->get( 'settings', [] ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Admin;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Manages options using the WordPress options API.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
abstract class Abstract_Options {
|
||||
/**
|
||||
* Gets the option for the given name. Returns the default value if the value does not exist.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $name Name of the option to get.
|
||||
* @param mixed $default Default value to return if the value does not exist.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get( $name, $default = null );
|
||||
|
||||
/**
|
||||
* Sets the value of an option. Update the value if the option for the given name already exists.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
* @param string $name Name of the option to set.
|
||||
* @param mixed $value Value to set for the option.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function set( $name, $value );
|
||||
|
||||
/**
|
||||
* Deletes the option with the given name.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $name Name of the option to delete.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function delete( $name );
|
||||
|
||||
/**
|
||||
* Checks if the option with the given name exists.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $name Name of the option to check.
|
||||
*
|
||||
* @return boolean True if the option exists, false otherwise
|
||||
*/
|
||||
public function has( $name ) {
|
||||
return null !== $this->get( $name );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Admin;
|
||||
|
||||
use WP_Rocket\Logger\Logger;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class that handles few things about the logs.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Logs implements Subscriber_Interface {
|
||||
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'pre_update_option_' . WP_ROCKET_SLUG => [ 'enable_debug', 10, 2 ],
|
||||
'admin_post_rocket_download_debug_file' => 'download_debug_file',
|
||||
'admin_post_rocket_delete_debug_file' => 'delete_debug_file',
|
||||
];
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** DEBUG ACTIVATION ======================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Enable or disable the debug mode when settings are saved.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $newvalue An array of submitted options values.
|
||||
* @param array $oldvalue An array of previous options values.
|
||||
* @return array Updated submitted options values.
|
||||
*/
|
||||
public function enable_debug( $newvalue, $oldvalue ) {
|
||||
if ( empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
return $newvalue;
|
||||
}
|
||||
|
||||
if ( ! empty( $newvalue['debug_enabled'] ) ) {
|
||||
Logger::enable_debug();
|
||||
} else {
|
||||
Logger::disable_debug();
|
||||
}
|
||||
|
||||
unset( $newvalue['debug_enabled'] );
|
||||
|
||||
return $newvalue;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** ADMIN POST CALLBACKS ==================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Download the log file.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function download_debug_file() {
|
||||
if ( ! $this->verify_nonce( 'download_debug_file' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! $this->current_user_can() ) {
|
||||
$this->redirect();
|
||||
}
|
||||
|
||||
$contents = Logger::get_log_file_contents();
|
||||
|
||||
if ( is_wp_error( $contents ) ) {
|
||||
add_settings_error( 'general', $contents->get_error_code(), $contents->get_error_message(), 'error' );
|
||||
set_transient( 'settings_errors', get_settings_errors(), 30 );
|
||||
|
||||
$this->redirect( add_query_arg( 'settings-updated', 1, wp_get_referer() ) );
|
||||
}
|
||||
|
||||
$file_name = Logger::get_log_file_path();
|
||||
$file_name = basename( $file_name, '.log' ) . Logger::get_log_file_extension();
|
||||
|
||||
nocache_headers();
|
||||
@header( 'Content-Type: text/x-log' );
|
||||
@header( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
|
||||
@header( 'Content-Transfer-Encoding: binary' );
|
||||
@header( 'Content-Length: ' . strlen( $contents ) );
|
||||
@header( 'Connection: close' );
|
||||
echo $contents; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the log file.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_debug_file() {
|
||||
if ( ! $this->verify_nonce( 'delete_debug_file' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! $this->current_user_can() ) {
|
||||
$this->redirect();
|
||||
}
|
||||
|
||||
if ( ! Logger::delete_log_file() ) {
|
||||
add_settings_error( 'general', 'debug_file_not_deleted', __( 'The debug file could not be deleted.', 'rocket' ), 'error' );
|
||||
set_transient( 'settings_errors', get_settings_errors(), 30 );
|
||||
|
||||
$this->redirect( add_query_arg( 'settings-updated', 1, wp_get_referer() ) );
|
||||
}
|
||||
|
||||
// Done.
|
||||
$this->redirect();
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** TOOLS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Verify the nonce sent in $_GET['_wpnonce'].
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $nonce_name The nonce name.
|
||||
* @return bool
|
||||
*/
|
||||
protected function verify_nonce( $nonce_name ) {
|
||||
return isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $nonce_name ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current user can operate.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function current_user_can() {
|
||||
return current_user_can( 'rocket_manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect the user.
|
||||
*
|
||||
* @since 3.1.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $redirect URL to redirect the user to.
|
||||
*/
|
||||
protected function redirect( $redirect = null ) {
|
||||
if ( empty( $redirect ) ) {
|
||||
$redirect = wp_get_referer();
|
||||
}
|
||||
|
||||
wp_safe_redirect( esc_url_raw( $redirect ) );
|
||||
die();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Admin;
|
||||
|
||||
/**
|
||||
* Manages the data inside an option.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Options_Data {
|
||||
/**
|
||||
* Option data
|
||||
*
|
||||
* @var Array Array of data inside the option
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Array $options Array of data coming from an option.
|
||||
*/
|
||||
public function __construct( $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the provided key exists in the option data array.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $key key name.
|
||||
* @return boolean true if it exists, false otherwise
|
||||
*/
|
||||
public function has( $key ) {
|
||||
return isset( $this->options[ $key ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value associated with a specific key.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $key key name.
|
||||
* @param mixed $default default value to return if key doesn't exist.
|
||||
* @return mixed
|
||||
*/
|
||||
public function get( $key, $default = '' ) {
|
||||
/**
|
||||
* Pre-filter any WP Rocket option before read
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @param mixed $default The default value.
|
||||
*/
|
||||
$value = apply_filters( 'pre_get_rocket_option_' . $key, null, $default ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
||||
|
||||
if ( null !== $value ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ( 'consumer_key' === $key && rocket_has_constant( 'WP_ROCKET_KEY' ) ) {
|
||||
return WP_ROCKET_KEY;
|
||||
} elseif ( 'consumer_email' === $key && rocket_has_constant( 'WP_ROCKET_EMAIL' ) ) {
|
||||
return WP_ROCKET_EMAIL;
|
||||
}
|
||||
|
||||
if ( ! $this->has( $key ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter any WP Rocket option after read
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @param mixed $default The default value.
|
||||
*/
|
||||
return apply_filters( 'get_rocket_option_' . $key, $this->options[ $key ], $default ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value associated with a specific key.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $key key name.
|
||||
* @param mixed $value to set.
|
||||
* @return void
|
||||
*/
|
||||
public function set( $key, $value ) {
|
||||
$this->options[ $key ] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets multiple values.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $options An array of key/value pairs to set.
|
||||
* @return void
|
||||
*/
|
||||
public function set_values( $options ) {
|
||||
foreach ( $options as $key => $value ) {
|
||||
$this->set( $key, $value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the option array.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_options() {
|
||||
return $this->options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Admin;
|
||||
|
||||
/**
|
||||
* Manages options using the WordPress options API.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Options extends Abstract_Options {
|
||||
/**
|
||||
* The prefix used by WP Rocket options.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $prefix WP Rocket options prefix.
|
||||
*/
|
||||
public function __construct( $prefix = '' ) {
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the option name used to store the option in the WordPress database.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $name Unprefixed name of the option.
|
||||
*
|
||||
* @return string Option name used to store it
|
||||
*/
|
||||
public function get_option_name( $name ) {
|
||||
return $this->prefix . $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the option for the given name. Returns the default value if the value does not exist.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $name Name of the option to get.
|
||||
* @param mixed $default Default value to return if the value does not exist.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get( $name, $default = null ) {
|
||||
$option = get_option( $this->get_option_name( $name ), $default );
|
||||
|
||||
if ( is_array( $default ) && ! is_array( $option ) ) {
|
||||
$option = (array) $option;
|
||||
}
|
||||
|
||||
return $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of an option. Update the value if the option for the given name already exists.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
* @param string $name Name of the option to set.
|
||||
* @param mixed $value Value to set for the option.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set( $name, $value ) {
|
||||
update_option( $this->get_option_name( $name ), $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the option with the given name.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $name Name of the option to delete.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete( $name ) {
|
||||
delete_option( $this->get_option_name( $name ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace WP_Rocket;
|
||||
|
||||
use WP_Rocket\Interfaces\Render_Interface;
|
||||
|
||||
/**
|
||||
* Handle rendering of HTML content created by WP Rocket.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
abstract class Abstract_Render implements Render_Interface {
|
||||
/**
|
||||
* Path to the templates
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $template_path;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $template_path Path to the templates.
|
||||
*/
|
||||
public function __construct( $template_path ) {
|
||||
$this->template_path = $template_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the given template if it's readable.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $template Template slug.
|
||||
* @param array $data Data to pass to the template.
|
||||
*/
|
||||
public function generate( $template, $data = [] ) {
|
||||
$template_path = $this->get_template_path( $template );
|
||||
|
||||
if ( ! rocket_direct_filesystem()->is_readable( $template_path ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
include $template_path;
|
||||
|
||||
return trim( ob_get_clean() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path a specific template.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $path Relative path to the template.
|
||||
* @return string
|
||||
*/
|
||||
private function get_template_path( $path ) {
|
||||
return $this->template_path . '/' . $path . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the button template.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $type Type of button (can be button or link).
|
||||
* @param string $action Action to be performed.
|
||||
* @param array $args Optional array of arguments to populate the button attributes.
|
||||
* @return void
|
||||
*/
|
||||
public function render_action_button( $type, $action, $args = [] ) {
|
||||
$default = [
|
||||
'label' => '',
|
||||
'action' => '',
|
||||
'url' => '',
|
||||
'parameter' => '',
|
||||
'attributes' => '',
|
||||
];
|
||||
|
||||
$args = wp_parse_args( $args, $default );
|
||||
|
||||
if ( ! empty( $args['attributes'] ) ) {
|
||||
$attributes = '';
|
||||
foreach ( $args['attributes'] as $key => $value ) {
|
||||
$attributes .= ' ' . sanitize_key( $key ) . '="' . esc_attr( $value ) . '"';
|
||||
}
|
||||
|
||||
$args['attributes'] = $attributes;
|
||||
}
|
||||
|
||||
if ( 'link' !== $type ) {
|
||||
$args['action'] = $action;
|
||||
echo $this->generate( 'buttons/button', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
|
||||
return;
|
||||
}
|
||||
|
||||
switch ( $action ) {
|
||||
case 'ask_support':
|
||||
case 'view_account':
|
||||
$args['url'] = rocket_get_external_url(
|
||||
'ask_support' === $action ? 'support' : 'account',
|
||||
[
|
||||
'utm_source' => 'wp_plugin',
|
||||
'utm_medium' => 'wp_rocket',
|
||||
]
|
||||
);
|
||||
break;
|
||||
case 'purge_cache':
|
||||
case 'preload':
|
||||
case 'rocket_purge_cloudflare':
|
||||
case 'rocket_purge_sucuri':
|
||||
case 'rocket_rollback':
|
||||
case 'rocket_export':
|
||||
case 'rocket_generate_critical_css':
|
||||
case 'rocket_purge_rocketcdn':
|
||||
case 'rocket_clear_usedcss':
|
||||
$referer = '';
|
||||
|
||||
if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
|
||||
$referer = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
|
||||
$referer = '&_wp_http_referer=' . rawurlencode( remove_query_arg( 'fl_builder', $referer ) );
|
||||
}
|
||||
|
||||
$url = admin_url( 'admin-post.php?action=' . $action );
|
||||
|
||||
$url .= $referer;
|
||||
|
||||
if ( ! empty( $args['parameters'] ) ) {
|
||||
$url = add_query_arg( $args['parameters'], $url );
|
||||
}
|
||||
|
||||
if ( 'purge_cache' === $action ) {
|
||||
$action .= '_all';
|
||||
}
|
||||
|
||||
$args['url'] = wp_nonce_url( $url, $action );
|
||||
break;
|
||||
case 'documentation':
|
||||
$args['url'] = get_rocket_documentation_url();
|
||||
break;
|
||||
}
|
||||
|
||||
echo $this->generate( 'buttons/link', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class to check if the current WordPress and PHP versions meet our requirements
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class WP_Rocket_Requirements_Check {
|
||||
/**
|
||||
* Plugin Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_name;
|
||||
|
||||
/**
|
||||
* Plugin filepath
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_file;
|
||||
|
||||
/**
|
||||
* Plugin version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_version;
|
||||
|
||||
/**
|
||||
* Plugin previous version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_last_version;
|
||||
|
||||
/**
|
||||
* Required WordPress version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $wp_version;
|
||||
|
||||
/**
|
||||
* Required PHP version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $php_version;
|
||||
|
||||
/**
|
||||
* WP Rocket options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $args {
|
||||
* Arguments to populate the class properties.
|
||||
*
|
||||
* @type string $plugin_name Plugin name.
|
||||
* @type string $wp_version Required WordPress version.
|
||||
* @type string $php_version Required PHP version.
|
||||
* @type string $plugin_file Plugin filepath.
|
||||
* }
|
||||
*/
|
||||
public function __construct( $args ) {
|
||||
foreach ( [ 'plugin_name', 'plugin_file', 'plugin_version', 'plugin_last_version', 'wp_version', 'php_version' ] as $setting ) {
|
||||
if ( isset( $args[ $setting ] ) ) {
|
||||
$this->$setting = $args[ $setting ];
|
||||
}
|
||||
}
|
||||
|
||||
$this->plugin_last_version = version_compare( PHP_VERSION, '5.3' ) >= 0 ? $this->plugin_last_version : '2.10.12';
|
||||
$this->options = get_option( 'wp_rocket_settings' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if all requirements are ok, if not, display a notice and the rollback
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check() {
|
||||
if ( ! $this->php_passes() || ! $this->wp_passes() ) {
|
||||
|
||||
add_action( 'admin_notices', [ $this, 'notice' ] );
|
||||
add_action( 'admin_post_rocket_rollback', [ $this, 'rollback' ] );
|
||||
add_filter( 'http_request_args', [ $this, 'add_own_ua' ], 10, 2 );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current PHP version is equal or superior to the required PHP version
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function php_passes() {
|
||||
return version_compare( PHP_VERSION, $this->php_version ) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current WordPress version is equal or superior to the required PHP version
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function wp_passes() {
|
||||
global $wp_version;
|
||||
|
||||
return version_compare( $wp_version, $this->wp_version ) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Warns if PHP or WP version are less than the defined values and offer rollback.
|
||||
*
|
||||
* @since 3.0 Updated minimum PHP version to 5.4 and minimum WordPress version to 4.2
|
||||
* @since 3.0 Moved to class
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
public function notice() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Translators: %1$s = Plugin name, %2$s = Plugin version.
|
||||
$message = '<p>' . sprintf( __( 'To function properly, %1$s %2$s requires at least:', 'rocket' ), $this->plugin_name, $this->plugin_version ) . '</p><ul>';
|
||||
|
||||
if ( ! $this->php_passes() ) {
|
||||
// Translators: %1$s = PHP version required.
|
||||
$message .= '<li>' . sprintf( __( 'PHP %1$s. To use this WP Rocket version, please ask your web host how to upgrade your server to PHP %1$s or higher.', 'rocket' ), $this->php_version ) . '</li>';
|
||||
}
|
||||
|
||||
if ( ! $this->wp_passes() ) {
|
||||
// Translators: %1$s = WordPress version required.
|
||||
$message .= '<li>' . sprintf( __( 'WordPress %1$s. To use this WP Rocket version, please upgrade WordPress to version %1$s or higher.', 'rocket' ), $this->wp_version ) . '</li>';
|
||||
}
|
||||
|
||||
$message .= '</ul><p>' . __( 'If you are not able to upgrade, you can rollback to the previous version by using the button below.', 'rocket' ) . '</p><p><a href="' . wp_nonce_url( admin_url( 'admin-post.php?action=rocket_rollback' ), 'rocket_rollback' ) . '" class="button">' .
|
||||
// Translators: %s = Previous plugin version.
|
||||
sprintf( __( 'Re-install version %s', 'rocket' ), $this->plugin_last_version )
|
||||
. '</a></p>';
|
||||
|
||||
echo '<div class="notice notice-error">' . wp_kses_post( $message ) . '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the rollback
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
public function rollback() {
|
||||
check_ajax_referer( 'rocket_rollback' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$consumer_key = isset( $this->options['consumer_key'] ) ? $this->options['consumer_key'] : false;
|
||||
|
||||
if ( ! $consumer_key && defined( 'WP_ROCKET_KEY' ) ) {
|
||||
$consumer_key = WP_ROCKET_KEY;
|
||||
}
|
||||
|
||||
$plugin_transient = get_site_transient( 'update_plugins' );
|
||||
$plugin_folder = plugin_basename( dirname( $this->plugin_file ) );
|
||||
$plugin_file = basename( $this->plugin_file );
|
||||
$url = sprintf( 'https://wp-rocket.me/%s/wp-rocket_%s.zip', $consumer_key, $this->plugin_last_version );
|
||||
$temp_array = [
|
||||
'slug' => $plugin_folder,
|
||||
'new_version' => $this->plugin_last_version,
|
||||
'url' => 'https://wp-rocket.me',
|
||||
'package' => $url,
|
||||
];
|
||||
|
||||
$temp_object = (object) $temp_array;
|
||||
$plugin_transient->response[ $plugin_folder . '/' . $plugin_file ] = $temp_object;
|
||||
set_site_transient( 'update_plugins', $plugin_transient );
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
|
||||
// translators: %s is the plugin name.
|
||||
$title = sprintf( __( '%s Update Rollback', 'rocket' ), $this->plugin_name );
|
||||
$plugin = 'wp-rocket/wp-rocket.php';
|
||||
$nonce = 'upgrade-plugin_' . $plugin;
|
||||
$url = 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $plugin );
|
||||
$upgrader_skin = new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) );
|
||||
$upgrader = new Plugin_Upgrader( $upgrader_skin );
|
||||
remove_filter( 'site_transient_update_plugins', 'rocket_check_update', 1 );
|
||||
$upgrader->upgrade( $plugin );
|
||||
wp_die(
|
||||
'',
|
||||
// translators: %s is the plugin name.
|
||||
sprintf( esc_html__( '%s Update Rollback', 'rocket' ), esc_html( $this->plugin_name ) ),
|
||||
[
|
||||
'response' => 200,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the User Agent when doing a request to WP Rocket server
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $request Array of arguments associated with the request.
|
||||
* @param string $url URL requested.
|
||||
*/
|
||||
public function add_own_ua( $request, $url ) {
|
||||
if ( strpos( $url, 'wp-rocket.me' ) === false ) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$consumer_key = isset( $this->options['consumer_key'] ) ? $this->options['consumer_key'] : false;
|
||||
|
||||
if ( ! $consumer_key && defined( 'WP_ROCKET_KEY' ) ) {
|
||||
$consumer_key = WP_ROCKET_KEY;
|
||||
}
|
||||
|
||||
$consumer_email = isset( $this->options['consumer_email'] ) ? $this->options['consumer_email'] : false;
|
||||
|
||||
if ( ! $consumer_email && defined( 'WP_ROCKET_EMAIL' ) ) {
|
||||
$consumer_email = WP_ROCKET_EMAIL;
|
||||
}
|
||||
|
||||
$request['user-agent'] = sprintf( '%s;WP-Rocket|%s%s|%s|%s|%s|;', $request['user-agent'], $this->plugin_version, '', $consumer_key, $consumer_email, esc_url( home_url() ) );
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
||||
+1692
File diff suppressed because one or more lines are too long
+46
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "wp-media/background-processing",
|
||||
"description": "Async & Background Tasks Processing",
|
||||
"homepage": "https://github.com/wp-media/background-processing",
|
||||
"license": "GPL-2.0+",
|
||||
"authors": [
|
||||
{
|
||||
"name": "WP Media",
|
||||
"email": "contact@wp-media.me",
|
||||
"homepage": "https://wp-media.me"
|
||||
}
|
||||
],
|
||||
"type": "library",
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"support": {
|
||||
"issues": "https://github.com/wp-media/background-processing/issues",
|
||||
"source": "https://github.com/wp-media/background-processing"
|
||||
},
|
||||
"require-dev": {
|
||||
"php": "^5.6 || ^7",
|
||||
"brain/monkey": "^2.0",
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
|
||||
"phpcompatibility/phpcompatibility-wp": "^2.0",
|
||||
"phpunit/phpunit": "^5.7 || ^7",
|
||||
"wp-coding-standards/wpcs": "^2",
|
||||
"wp-media/phpunit": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [ "" ]
|
||||
},
|
||||
"autoload-dev": {},
|
||||
"scripts": {
|
||||
"test-unit": "\"vendor/bin/wpmedia-phpunit\" unit path=Tests/Unit",
|
||||
"test-integration": "\"vendor/bin/wpmedia-phpunit\" integration path=Tests/Integration/",
|
||||
"run-tests": [
|
||||
"@test-unit",
|
||||
"@test-integration"
|
||||
],
|
||||
"install-codestandards": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
|
||||
"phpcs": "phpcs --basepath=.",
|
||||
"phpcs-changed": "./bin/phpcs-changed.sh",
|
||||
"phpcs:fix": "phpcbf"
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/**
|
||||
* WP Async Request
|
||||
*
|
||||
* @package WP-Background-Processing
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract WP_Rocket_WP_Async_Request class.
|
||||
*
|
||||
* @abstract
|
||||
*/
|
||||
abstract class WP_Rocket_WP_Async_Request {
|
||||
|
||||
/**
|
||||
* Prefix
|
||||
*
|
||||
* (default value: 'wp')
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
protected $prefix = 'wp';
|
||||
|
||||
/**
|
||||
* Action
|
||||
*
|
||||
* (default value: 'async_request')
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
protected $action = 'async_request';
|
||||
|
||||
/**
|
||||
* Identifier
|
||||
*
|
||||
* @var mixed
|
||||
* @access protected
|
||||
*/
|
||||
protected $identifier;
|
||||
|
||||
/**
|
||||
* Data
|
||||
*
|
||||
* (default value: array())
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* Initiate new async request
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->identifier = $this->prefix . '_' . $this->action;
|
||||
|
||||
add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
|
||||
add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data used during the request
|
||||
*
|
||||
* @param array $data Data.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function data( $data ) {
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the async request
|
||||
*
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function dispatch() {
|
||||
$url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
|
||||
$args = $this->get_post_args();
|
||||
|
||||
return wp_remote_post( esc_url_raw( $url ), $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get query args
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_query_args() {
|
||||
if ( property_exists( $this, 'query_args' ) ) {
|
||||
return $this->query_args;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'action' => $this->identifier,
|
||||
'nonce' => wp_create_nonce( $this->identifier ),
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the post arguments used during an async request.
|
||||
*
|
||||
* @param array $url
|
||||
*/
|
||||
return apply_filters( $this->identifier . '_query_args', $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get query URL
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_query_url() {
|
||||
if ( property_exists( $this, 'query_url' ) ) {
|
||||
return $this->query_url;
|
||||
}
|
||||
|
||||
$url = admin_url( 'admin-ajax.php' );
|
||||
|
||||
/**
|
||||
* Filters the post arguments used during an async request.
|
||||
*
|
||||
* @param string $url
|
||||
*/
|
||||
return apply_filters( $this->identifier . '_query_url', $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post args
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_post_args() {
|
||||
if ( property_exists( $this, 'post_args' ) ) {
|
||||
return $this->post_args;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'timeout' => 0.01,
|
||||
'blocking' => false,
|
||||
'body' => $this->data,
|
||||
'cookies' => $_COOKIE,
|
||||
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the post arguments used during an async request.
|
||||
*
|
||||
* @param array $args
|
||||
*/
|
||||
return apply_filters( $this->identifier . '_post_args', $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe handle
|
||||
*
|
||||
* Check WP_Rocket_for correct nonce and pass to handler.
|
||||
*/
|
||||
public function maybe_handle() {
|
||||
// Don't lock up other requests while processing
|
||||
session_write_close();
|
||||
|
||||
check_ajax_referer( $this->identifier, 'nonce' );
|
||||
|
||||
$this->handle();
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle
|
||||
*
|
||||
* Override this method to perform any actions required
|
||||
* during the async request.
|
||||
*/
|
||||
abstract protected function handle();
|
||||
|
||||
}
|
||||
+528
@@ -0,0 +1,528 @@
|
||||
<?php
|
||||
/**
|
||||
* WP Background Process
|
||||
*
|
||||
* @package WP-Background-Processing
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract WP_Rocket_WP_Background_Process class.
|
||||
*
|
||||
* @abstract
|
||||
* @extends WP_Rocket_WP_Async_Request
|
||||
*/
|
||||
abstract class WP_Rocket_WP_Background_Process extends WP_Rocket_WP_Async_Request {
|
||||
|
||||
/**
|
||||
* Action
|
||||
*
|
||||
* (default value: 'background_process')
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
protected $action = 'background_process';
|
||||
|
||||
/**
|
||||
* Start time of current process.
|
||||
*
|
||||
* (default value: 0)
|
||||
*
|
||||
* @var int
|
||||
* @access protected
|
||||
*/
|
||||
protected $start_time = 0;
|
||||
|
||||
/**
|
||||
* Cron_hook_identifier
|
||||
*
|
||||
* @var mixed
|
||||
* @access protected
|
||||
*/
|
||||
protected $cron_hook_identifier;
|
||||
|
||||
/**
|
||||
* Cron_interval_identifier
|
||||
*
|
||||
* @var mixed
|
||||
* @access protected
|
||||
*/
|
||||
protected $cron_interval_identifier;
|
||||
|
||||
/**
|
||||
* Initiate new background process
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->cron_hook_identifier = $this->identifier . '_cron';
|
||||
$this->cron_interval_identifier = $this->identifier . '_cron_interval';
|
||||
|
||||
add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) );
|
||||
add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function dispatch() {
|
||||
// Schedule the cron healthcheck.
|
||||
$this->schedule_event();
|
||||
|
||||
// Perform remote post.
|
||||
return parent::dispatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Push to queue
|
||||
*
|
||||
* @param mixed $data Data.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function push_to_queue( $data ) {
|
||||
$this->data[] = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save queue
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function save() {
|
||||
$key = $this->generate_key();
|
||||
|
||||
if ( ! empty( $this->data ) ) {
|
||||
update_site_option( $key, $this->data );
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update queue
|
||||
*
|
||||
* @param string $key Key.
|
||||
* @param array $data Data.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function update( $key, $data ) {
|
||||
if ( ! empty( $data ) ) {
|
||||
update_site_option( $key, $data );
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete queue
|
||||
*
|
||||
* @param string $key Key.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function delete( $key ) {
|
||||
delete_site_option( $key );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate key
|
||||
*
|
||||
* Generates a unique key based on microtime. Queue items are
|
||||
* given a unique key so that they can be merged upon save.
|
||||
*
|
||||
* @param int $length Length.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generate_key( $length = 64 ) {
|
||||
$unique = md5( microtime() . rand() );
|
||||
$prepend = $this->identifier . '_batch_';
|
||||
|
||||
return substr( $prepend . $unique, 0, $length );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe process queue
|
||||
*
|
||||
* Checks whether data exists within the queue and that
|
||||
* the process is not already running.
|
||||
*/
|
||||
public function maybe_handle() {
|
||||
// Don't lock up other requests while processing
|
||||
session_write_close();
|
||||
|
||||
if ( $this->is_process_running() ) {
|
||||
// Background process already running.
|
||||
wp_die();
|
||||
}
|
||||
|
||||
if ( $this->is_queue_empty() ) {
|
||||
// No data to process.
|
||||
wp_die();
|
||||
}
|
||||
|
||||
check_ajax_referer( $this->identifier, 'nonce' );
|
||||
|
||||
$this->handle();
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is queue empty
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_queue_empty() {
|
||||
global $wpdb;
|
||||
|
||||
$table = $wpdb->options;
|
||||
$column = 'option_name';
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$table = $wpdb->sitemeta;
|
||||
$column = 'meta_key';
|
||||
}
|
||||
|
||||
$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
|
||||
|
||||
$count = $wpdb->get_var( $wpdb->prepare( "
|
||||
SELECT COUNT(*)
|
||||
FROM {$table}
|
||||
WHERE {$column} LIKE %s
|
||||
", $key ) );
|
||||
|
||||
return ( $count > 0 ) ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is process running
|
||||
*
|
||||
* Check whether the current process is already running
|
||||
* in a background process.
|
||||
*/
|
||||
protected function is_process_running() {
|
||||
if ( get_site_transient( $this->identifier . '_process_lock' ) ) {
|
||||
// Process already running.
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is process cancelled
|
||||
*
|
||||
* Check whether the current process is cancelled
|
||||
* in a background process.
|
||||
*/
|
||||
protected function is_process_cancelled() {
|
||||
if ( ! \rocket_direct_filesystem()->exists( WP_ROCKET_CACHE_ROOT_PATH . '.' . $this->identifier . '_process_cancelled' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock process
|
||||
*
|
||||
* Lock the process so that multiple instances can't run simultaneously.
|
||||
* Override if applicable, but the duration should be greater than that
|
||||
* defined in the time_exceeded() method.
|
||||
*/
|
||||
protected function lock_process() {
|
||||
$this->start_time = time(); // Set start time of current process.
|
||||
|
||||
$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
|
||||
$lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );
|
||||
|
||||
set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock process
|
||||
*
|
||||
* Unlock the process so that other instances can spawn.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function unlock_process() {
|
||||
delete_site_transient( $this->identifier . '_process_lock' );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get batch
|
||||
*
|
||||
* @return stdClass Return the first batch from the queue
|
||||
*/
|
||||
protected function get_batch() {
|
||||
global $wpdb;
|
||||
|
||||
$table = $wpdb->options;
|
||||
$column = 'option_name';
|
||||
$key_column = 'option_id';
|
||||
$value_column = 'option_value';
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$table = $wpdb->sitemeta;
|
||||
$column = 'meta_key';
|
||||
$key_column = 'meta_id';
|
||||
$value_column = 'meta_value';
|
||||
}
|
||||
|
||||
$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
|
||||
|
||||
$query = $wpdb->get_row( $wpdb->prepare( "
|
||||
SELECT *
|
||||
FROM {$table}
|
||||
WHERE {$column} LIKE %s
|
||||
ORDER BY {$key_column} ASC
|
||||
LIMIT 1
|
||||
", $key ) );
|
||||
|
||||
$batch = new stdClass();
|
||||
$batch->key = $query->$column;
|
||||
$batch->data = maybe_unserialize( $query->$value_column );
|
||||
|
||||
return $batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle
|
||||
*
|
||||
* Pass each queue item to the task handler, while remaining
|
||||
* within server memory and time limit constraints.
|
||||
*/
|
||||
protected function handle() {
|
||||
$this->lock_process();
|
||||
|
||||
do {
|
||||
$batch = $this->get_batch();
|
||||
|
||||
foreach ( $batch->data as $key => $value ) {
|
||||
$task = $this->task( $value );
|
||||
|
||||
if ( false !== $task ) {
|
||||
$batch->data[ $key ] = $task;
|
||||
} else {
|
||||
unset( $batch->data[ $key ] );
|
||||
}
|
||||
|
||||
if ( $this->time_exceeded() || $this->memory_exceeded() || $this->is_process_cancelled() ) {
|
||||
// Batch limits reached.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Update or delete current batch.
|
||||
if ( ! empty( $batch->data ) && ! $this->is_process_cancelled() ) {
|
||||
$this->update( $batch->key, $batch->data );
|
||||
} else {
|
||||
$this->complete_batch();
|
||||
$this->delete( $batch->key );
|
||||
}
|
||||
} while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() && ! $this->is_process_cancelled() );
|
||||
|
||||
$this->unlock_process();
|
||||
|
||||
// Start next batch or complete process.
|
||||
if ( ! $this->is_queue_empty() ) {
|
||||
$this->dispatch();
|
||||
} else {
|
||||
$this->complete();
|
||||
}
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Memory exceeded
|
||||
*
|
||||
* Ensures the batch process never exceeds 90%
|
||||
* of the maximum WordPress memory.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function memory_exceeded() {
|
||||
$memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory
|
||||
$current_memory = memory_get_usage( true );
|
||||
$return = false;
|
||||
|
||||
if ( $current_memory >= $memory_limit ) {
|
||||
$return = true;
|
||||
}
|
||||
|
||||
return apply_filters( $this->identifier . '_memory_exceeded', $return );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get memory limit
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function get_memory_limit() {
|
||||
if ( function_exists( 'ini_get' ) ) {
|
||||
$memory_limit = ini_get( 'memory_limit' );
|
||||
} else {
|
||||
// Sensible default.
|
||||
$memory_limit = '128M';
|
||||
}
|
||||
|
||||
if ( ! $memory_limit || -1 === intval( $memory_limit ) ) {
|
||||
// Unlimited, set to 32GB.
|
||||
$memory_limit = '32000M';
|
||||
}
|
||||
|
||||
return wp_convert_hr_to_bytes( $memory_limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Time exceeded.
|
||||
*
|
||||
* Ensures the batch never exceeds a sensible time limit.
|
||||
* A timeout limit of 30s is common on shared hosting.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function time_exceeded() {
|
||||
$finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds
|
||||
$return = false;
|
||||
|
||||
if ( time() >= $finish ) {
|
||||
$return = true;
|
||||
}
|
||||
|
||||
return apply_filters( $this->identifier . '_time_exceeded', $return );
|
||||
}
|
||||
|
||||
/**
|
||||
* Current batch is completed.
|
||||
*/
|
||||
protected function complete_batch(){
|
||||
// Override this code on the instantiated process class WP_Rocket_just if needed.
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete.
|
||||
*
|
||||
* Override if applicable, but ensure that the below actions are
|
||||
* performed, or, call parent::complete().
|
||||
*/
|
||||
protected function complete() {
|
||||
// Unschedule the cron healthcheck.
|
||||
$this->clear_scheduled_event();
|
||||
|
||||
\rocket_direct_filesystem()->delete( WP_ROCKET_CACHE_ROOT_PATH . '.' . $this->identifier . '_process_cancelled' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule cron healthcheck
|
||||
*
|
||||
* @param mixed $schedules Schedules.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function schedule_cron_healthcheck( $schedules ) {
|
||||
$interval = apply_filters( $this->identifier . '_cron_interval', 5 );
|
||||
|
||||
if ( property_exists( $this, 'cron_interval' ) ) {
|
||||
$interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
|
||||
}
|
||||
|
||||
// Adds every 5 minutes to the existing schedules.
|
||||
$schedules[ $this->identifier . '_cron_interval' ] = array(
|
||||
'interval' => MINUTE_IN_SECONDS * $interval,
|
||||
'display' => sprintf( __( 'Every %d Minutes' ), $interval ),
|
||||
);
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle cron healthcheck
|
||||
*
|
||||
* Restart the background process if not already running
|
||||
* and data exists in the queue.
|
||||
*/
|
||||
public function handle_cron_healthcheck() {
|
||||
if ( $this->is_process_running() ) {
|
||||
// Background process already running.
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( $this->is_queue_empty() ) {
|
||||
// No data to process.
|
||||
$this->clear_scheduled_event();
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->handle();
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule event
|
||||
*/
|
||||
protected function schedule_event() {
|
||||
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
|
||||
wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear scheduled event
|
||||
*/
|
||||
protected function clear_scheduled_event() {
|
||||
$timestamp = wp_next_scheduled( $this->cron_hook_identifier );
|
||||
|
||||
if ( $timestamp ) {
|
||||
wp_unschedule_event( $timestamp, $this->cron_hook_identifier );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel Process
|
||||
*
|
||||
* Stop processing queue items, clear cronjob and delete batch.
|
||||
*
|
||||
*/
|
||||
public function cancel_process() {
|
||||
if ( ! $this->is_queue_empty() ) {
|
||||
$batch = $this->get_batch();
|
||||
$this->delete( $batch->key );
|
||||
$this->unlock_process();
|
||||
wp_clear_scheduled_hook( $this->cron_hook_identifier );
|
||||
|
||||
\rocket_direct_filesystem()->touch( WP_ROCKET_CACHE_ROOT_PATH . '.' . $this->identifier . '_process_cancelled' );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Task
|
||||
*
|
||||
* Override this method to perform any actions required on each
|
||||
* queue item. Return the modified item WP_Rocket_for further processing
|
||||
* in the next pass through. Or, return false to remove the
|
||||
* item from the queue.
|
||||
*
|
||||
* @param mixed $item Queue item to iterate over.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function task( $item );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Event_Management;
|
||||
|
||||
/**
|
||||
* The event manager manages events using the WordPress plugin API.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Carl Alexander <contact@carlalexander.ca>
|
||||
*/
|
||||
class Event_Manager {
|
||||
/**
|
||||
* Adds a callback to a specific hook of the WordPress plugin API.
|
||||
*
|
||||
* @uses add_filter()
|
||||
*
|
||||
* @param string $hook_name Name of the hook.
|
||||
* @param callable $callback Callback function.
|
||||
* @param int $priority Priority.
|
||||
* @param int $accepted_args Number of arguments.
|
||||
*/
|
||||
public function add_callback( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
|
||||
add_filter( $hook_name, $callback, $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an event subscriber.
|
||||
*
|
||||
* The event manager registers all the hooks that the given subscriber
|
||||
* wants to register with the WordPress Plugin API.
|
||||
*
|
||||
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
|
||||
*/
|
||||
public function add_subscriber( Subscriber_Interface $subscriber ) {
|
||||
if ( $subscriber instanceof Event_Manager_Aware_Subscriber_Interface ) {
|
||||
$subscriber->set_event_manager( $this );
|
||||
}
|
||||
|
||||
$events = $subscriber->get_subscribed_events();
|
||||
|
||||
if ( empty( $events ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
|
||||
$this->add_subscriber_callback( $subscriber, $hook_name, $parameters );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the WordPress plugin API to see if the given hook has
|
||||
* the given callback. The priority of the callback will be returned
|
||||
* or false. If no callback is given will return true or false if
|
||||
* there's any callbacks registered to the hook.
|
||||
*
|
||||
* @uses has_filter()
|
||||
*
|
||||
* @param string $hook_name Hook name.
|
||||
* @param mixed $callback Callback.
|
||||
*
|
||||
* @return bool|int
|
||||
*/
|
||||
public function has_callback( $hook_name, $callback = false ) {
|
||||
return has_filter( $hook_name, $callback );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given callback from the given hook. The WordPress plugin API only
|
||||
* removes the hook if the callback and priority match a registered hook.
|
||||
*
|
||||
* @uses remove_filter()
|
||||
*
|
||||
* @param string $hook_name Hook name.
|
||||
* @param callable $callback Callback.
|
||||
* @param int $priority Priority.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function remove_callback( $hook_name, $callback, $priority = 10 ) {
|
||||
return remove_filter( $hook_name, $callback, $priority );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an event subscriber.
|
||||
*
|
||||
* The event manager removes all the hooks that the given subscriber
|
||||
* wants to register with the WordPress Plugin API.
|
||||
*
|
||||
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
|
||||
*/
|
||||
public function remove_subscriber( Subscriber_Interface $subscriber ) {
|
||||
foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
|
||||
$this->remove_subscriber_callback( $subscriber, $hook_name, $parameters );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given subscriber's callback to a specific hook
|
||||
* of the WordPress plugin API.
|
||||
*
|
||||
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
|
||||
* @param string $hook_name Hook name.
|
||||
* @param mixed $parameters Parameters, can be a string, an array or a multidimensional array.
|
||||
*/
|
||||
private function add_subscriber_callback( Subscriber_Interface $subscriber, $hook_name, $parameters ) {
|
||||
if ( is_string( $parameters ) ) {
|
||||
$this->add_callback( $hook_name, [ $subscriber, $parameters ] );
|
||||
} elseif ( is_array( $parameters ) && count( $parameters ) !== count( $parameters, COUNT_RECURSIVE ) ) {
|
||||
foreach ( $parameters as $parameter ) {
|
||||
$this->add_subscriber_callback( $subscriber, $hook_name, $parameter );
|
||||
}
|
||||
} elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
|
||||
$this->add_callback( $hook_name, [ $subscriber, $parameters[0] ], isset( $parameters[1] ) ? $parameters[1] : 10, isset( $parameters[2] ) ? $parameters[2] : 1 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given subscriber's callback to a specific hook
|
||||
* of the WordPress plugin API.
|
||||
*
|
||||
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
|
||||
* @param string $hook_name Hook name.
|
||||
* @param mixed $parameters Parameters, can be a string, an array or a multidimensional array.
|
||||
*/
|
||||
private function remove_subscriber_callback( Subscriber_Interface $subscriber, $hook_name, $parameters ) {
|
||||
if ( is_string( $parameters ) ) {
|
||||
$this->remove_callback( $hook_name, [ $subscriber, $parameters ] );
|
||||
} elseif ( is_array( $parameters ) && count( $parameters ) !== count( $parameters, COUNT_RECURSIVE ) ) {
|
||||
foreach ( $parameters as $parameter ) {
|
||||
$this->remove_subscriber_callback( $subscriber, $hook_name, $parameter );
|
||||
}
|
||||
} elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
|
||||
$this->remove_callback( $hook_name, [ $subscriber, $parameters[0] ], isset( $parameters[1] ) ? $parameters[1] : 10 );
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Event_Management;
|
||||
|
||||
interface Event_Manager_Aware_Subscriber_Interface extends Subscriber_Interface {
|
||||
/**
|
||||
* Set the WordPress event manager for the subscriber.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Event_Manager $event_manager Event_Manager instance.
|
||||
*/
|
||||
public function set_event_manager( Event_Manager $event_manager );
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Event_Management;
|
||||
|
||||
/**
|
||||
* A Subscriber knows what specific WordPress events it wants to listen to.
|
||||
*
|
||||
* When an EventManager adds a Subscriber, it gets all the WordPress events that
|
||||
* it wants to listen to. It then adds the subscriber as a listener for each of them.
|
||||
*
|
||||
* @author Carl Alexander <contact@carlalexander.ca>
|
||||
*/
|
||||
interface Subscriber_Interface {
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* The array key is the event name. The value can be:
|
||||
*
|
||||
* * The method name
|
||||
* * An array with the method name and priority
|
||||
* * An array with the method name, priority and number of accepted arguments
|
||||
*
|
||||
* For instance:
|
||||
*
|
||||
* * array('hook_name' => 'method_name')
|
||||
* * array('hook_name' => array('method_name', $priority))
|
||||
* * array('hook_name' => array('method_name', $priority, $accepted_args))
|
||||
* * array('hook_name' => array(array('method_name_1', $priority_1, $accepted_args_1)), array('method_name_2', $priority_2, $accepted_args_2)))
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Interfaces;
|
||||
|
||||
/**
|
||||
* Render interface
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*/
|
||||
interface Render_Interface {
|
||||
/**
|
||||
* Renders the given template if it's readable.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $template Template slug.
|
||||
* @param array $data Data to pass to the template.
|
||||
*/
|
||||
public function generate( $template, $data );
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
if ( isset( $rocket_path ) ) {
|
||||
require_once $rocket_path . 'inc/Logger/HTMLFormatter.php';
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
if ( isset( $rocket_path ) ) {
|
||||
require_once $rocket_path . 'inc/Logger/Logger.php';
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
if ( isset( $rocket_path ) ) {
|
||||
require_once $rocket_path . 'inc/Logger/StreamHandler.php';
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Tools;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Detect and report when <html>, wp_footer() and <body> tags are missing.
|
||||
*
|
||||
* @since 3.4.2
|
||||
* @author Soponar Cristina
|
||||
*/
|
||||
class Detect_Missing_Tags_Subscriber implements Subscriber_Interface {
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.4.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'admin_notices' => 'rocket_notice_missing_tags',
|
||||
'rocket_before_maybe_process_buffer' => 'maybe_missing_tags',
|
||||
'wp_rocket_upgrade' => 'delete_transient_after_upgrade',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there is a missing </html> or </body> tag
|
||||
*
|
||||
* @since 3.4.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
*/
|
||||
public function maybe_missing_tags( $html ) {
|
||||
// If there is a redirect the content is empty and can display a false positive notice.
|
||||
if ( strlen( $html ) <= 255 ) {
|
||||
return;
|
||||
}
|
||||
// If the http response is not 200 do not report missing tags.
|
||||
if ( http_response_code() !== 200 ) {
|
||||
return;
|
||||
}
|
||||
// If content type is not HTML do not report missing tags.
|
||||
if ( empty( $_SERVER['content_type'] ) || false === strpos( wp_unslash( $_SERVER['content_type'] ), 'text/html' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
return;
|
||||
}
|
||||
// If the content does not contain HTML Doctype, do not report missing tags.
|
||||
if ( false === stripos( $html, '<!DOCTYPE html' ) ) {
|
||||
return;
|
||||
}
|
||||
Logger::info(
|
||||
'START Detect Missing closing tags ( <html>, </body> or wp_footer() )',
|
||||
[
|
||||
'maybe_missing_tags',
|
||||
'URI' => $this->get_raw_request_uri(),
|
||||
]
|
||||
);
|
||||
|
||||
// Remove all comments before testing tags. If </html> or </body> tags are commented this will identify it as a missing tag.
|
||||
$html = preg_replace( '/<!--([\\s\\S]*?)-->/', '', $html );
|
||||
$missing_tags = [];
|
||||
if ( false === strpos( $html, '</html>' ) ) {
|
||||
$missing_tags[] = '</html>';
|
||||
Logger::debug(
|
||||
'Not found closing </html> tag.',
|
||||
[
|
||||
'maybe_missing_tags',
|
||||
'URI' => $this->get_raw_request_uri(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ( false === strpos( $html, '</body>' ) ) {
|
||||
$missing_tags[] = '</body>';
|
||||
Logger::debug(
|
||||
'Not found closing </body> tag.',
|
||||
[
|
||||
'maybe_missing_tags',
|
||||
'URI' => $this->get_raw_request_uri(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ( did_action( 'wp_footer' ) === 0 ) {
|
||||
$missing_tags[] = 'wp_footer()';
|
||||
Logger::debug(
|
||||
'wp_footer() function did not run.',
|
||||
[
|
||||
'maybe_missing_tags',
|
||||
'URI' => $this->get_raw_request_uri(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $missing_tags ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$transient = get_transient( 'rocket_notice_missing_tags' );
|
||||
$transient = is_array( $transient ) ? $transient : [];
|
||||
$missing_tags = array_unique( array_merge( $transient, $missing_tags ) );
|
||||
|
||||
if ( count( $transient ) === count( $missing_tags ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prevent saving the transient if the notice is dismissed.
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
if ( in_array( 'rocket_notice_missing_tags', (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
set_transient( 'rocket_notice_missing_tags', $missing_tags );
|
||||
}
|
||||
|
||||
/**
|
||||
* This notice is displayed if there is a missing required tag or function: </html>, </body> or wp_footer()
|
||||
*
|
||||
* @since 3.4.2
|
||||
* @author Soponar Cristina
|
||||
*/
|
||||
public function rocket_notice_missing_tags() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) || 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notice = get_transient( 'rocket_notice_missing_tags' );
|
||||
if ( empty( $notice ) || ! is_array( $notice ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $notice as $i => $tag ) {
|
||||
$notice[ $i ] = '<code>' . esc_html( $tag ) . '</code>';
|
||||
}
|
||||
|
||||
$msg = '<b>' . __( 'WP Rocket: ', 'rocket' ) . '</b>';
|
||||
$msg .= sprintf(
|
||||
/* translators: %1$s = missing tags; */
|
||||
esc_html( _n( 'Failed to detect the following requirement in your theme: closing %1$s.', 'Failed to detect the following requirements in your theme: closing %1$s.', count( $notice ), 'rocket' ) ),
|
||||
// translators: Documentation exists in EN, FR.
|
||||
wp_sprintf_l( '%l', $notice )
|
||||
);
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/99-pages-not-cached-or-minify-cssjs-not-working/?utm_source=wp_plugin&utm_medium=wp_rocket#theme', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
\rocket_notice_html(
|
||||
[
|
||||
'status' => 'info',
|
||||
'dismissible' => '',
|
||||
'message' => $msg,
|
||||
'dismiss_button' => __FUNCTION__,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request URI.
|
||||
*
|
||||
* @since 3.4.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_raw_request_uri() {
|
||||
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( '' === $_SERVER['REQUEST_URI'] ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '/' . esc_html( ltrim( wp_unslash( $_SERVER['REQUEST_URI'] ), '/' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the transient storing the missing tags when updating the plugin
|
||||
*
|
||||
* @since 3.4.2.2
|
||||
* @author Soponar Cristina
|
||||
*/
|
||||
public function delete_transient_after_upgrade() {
|
||||
delete_transient( 'rocket_notice_missing_tags' );
|
||||
}
|
||||
}
|
||||
+390
@@ -0,0 +1,390 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Subscriber for the WebP support with EWWW.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class EWWW_Subscriber implements Webp_Interface, Subscriber_Interface {
|
||||
use Webp_Common;
|
||||
|
||||
/**
|
||||
* Options_Data instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
* @access private
|
||||
* @author Remy Perona
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* EWWW basename.
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $plugin_basename;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_webp_plugins' => 'register',
|
||||
'wp_rocket_loaded' => 'load_hooks',
|
||||
];
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Launch filters.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function load_hooks() {
|
||||
if ( ! $this->options->get( 'cache_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every time EWWW is (de)activated, we must "sync" our webp cache option.
|
||||
*/
|
||||
if ( did_action( 'activate_' . $this->get_basename() ) ) {
|
||||
$this->plugin_activation();
|
||||
}
|
||||
if ( did_action( 'deactivate_' . $this->get_basename() ) ) {
|
||||
$this->plugin_deactivation();
|
||||
}
|
||||
add_action( 'activate_' . $this->get_basename(), [ $this, 'plugin_activation' ], 20 );
|
||||
add_action( 'deactivate_' . $this->get_basename(), [ $this, 'plugin_deactivation' ], 20 );
|
||||
|
||||
if ( ! function_exists( 'ewww_image_optimizer_get_option' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since Rocket already updates the config file after updating its options, there is no need to do it again if the CDN or zone options change.
|
||||
* Sadly, we can’t monitor EWWW options accurately to update our config file.
|
||||
*/
|
||||
|
||||
add_filter( 'rocket_cdn_cnames', [ $this, 'maybe_remove_images_cnames' ], 1000, 2 );
|
||||
add_filter( 'rocket_allow_cdn_images', [ $this, 'maybe_remove_images_from_cdn_dropdown' ] );
|
||||
|
||||
$option_names = [
|
||||
'ewww_image_optimizer_exactdn',
|
||||
'ewww_image_optimizer_webp_for_cdn',
|
||||
];
|
||||
|
||||
foreach ( $option_names as $option_name ) {
|
||||
if ( $this->is_active_for_network() ) {
|
||||
add_filter( 'add_site_option_' . $option_name, [ $this, 'trigger_webp_change' ] );
|
||||
add_filter( 'update_site_option_' . $option_name, [ $this, 'trigger_webp_change' ] );
|
||||
add_filter( 'delete_site_option_' . $option_name, [ $this, 'trigger_webp_change' ] );
|
||||
} else {
|
||||
add_filter( 'add_option_' . $option_name, [ $this, 'trigger_webp_change' ] );
|
||||
add_filter( 'update_option_' . $option_name, [ $this, 'trigger_webp_change' ] );
|
||||
add_filter( 'delete_option_' . $option_name, [ $this, 'trigger_webp_change' ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove CDN hosts for images if EWWW uses ExactDN.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $hosts List of CDN URLs.
|
||||
* @param array $zones List of zones. Default is [ 'all' ].
|
||||
* @return array
|
||||
*/
|
||||
public function maybe_remove_images_cnames( $hosts, $zones ) {
|
||||
if ( ! $hosts ) {
|
||||
return $hosts;
|
||||
}
|
||||
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
|
||||
return $hosts;
|
||||
}
|
||||
// EWWW uses ExactDN: WPR CDN should be disabled for images.
|
||||
if ( ! in_array( 'images', $zones, true ) ) {
|
||||
// Not asking for images.
|
||||
return $hosts;
|
||||
}
|
||||
if ( ! array_diff( $zones, [ 'all', 'images' ] ) ) {
|
||||
// This is clearly for images: return an empty list of hosts.
|
||||
return [];
|
||||
}
|
||||
|
||||
// We also want other things, like js and css: let's only remove the hosts for 'images'.
|
||||
$cdn_urls = $this->options->get( 'cdn_cnames', [] );
|
||||
|
||||
if ( ! $cdn_urls ) {
|
||||
return $hosts;
|
||||
}
|
||||
|
||||
// Separate image hosts from the other ones.
|
||||
$image_hosts = [];
|
||||
$other_hosts = [];
|
||||
$cdn_zones = $this->options->get( 'cdn_zone', [] );
|
||||
|
||||
foreach ( $cdn_urls as $k => $urls ) {
|
||||
if ( ! in_array( $cdn_zones[ $k ], $zones, true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$urls = explode( ',', $urls );
|
||||
$urls = array_map( 'trim', $urls );
|
||||
|
||||
if ( 'images' === $cdn_zones[ $k ] ) {
|
||||
foreach ( $urls as $url ) {
|
||||
$image_hosts[] = $url;
|
||||
}
|
||||
} else {
|
||||
foreach ( $urls as $url ) {
|
||||
$other_hosts[] = $url;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the image hosts are not also used for other things (duplicate).
|
||||
$image_hosts = array_diff( $image_hosts, $other_hosts );
|
||||
|
||||
// Then remove the remaining from the final list.
|
||||
return array_diff( $hosts, $image_hosts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe remove the images option from the CDN dropdown.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $allow true to add the option, false otherwise.
|
||||
* @return bool
|
||||
*/
|
||||
public function maybe_remove_images_from_cdn_dropdown( $allow ) {
|
||||
if ( ! $allow ) {
|
||||
return $allow;
|
||||
}
|
||||
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
|
||||
return $allow;
|
||||
}
|
||||
|
||||
// EWWW uses ExactDN: WPR CDN should be disabled for images.
|
||||
return false;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC TOOLS ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the plugin name.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return 'EWWW';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin identifier.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return 'ewww';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin converts images to webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_converting_to_webp() {
|
||||
if ( ! function_exists( 'ewww_image_optimizer_get_option' ) ) {
|
||||
// No EWWW, no webp.
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin serves webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp() {
|
||||
if ( ! function_exists( 'ewww_image_optimizer_get_option' ) ) {
|
||||
// No EWWW, no webp.
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
|
||||
// EWWW uses ExactDN (WPR CDN should be disabled for images).
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp_for_cdn' ) ) {
|
||||
// EWWW uses JS to rewrite file extensions.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Decide if rewrite rules are used.
|
||||
if ( ! function_exists( 'ewww_image_optimizer_webp_rewrite_verify' ) ) {
|
||||
// Uh?
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'get_home_path' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'extract_from_markers' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/misc.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns null if rules are present and valid. Otherwise it returns rules to be inserted.
|
||||
* Note: this also returns null if WP Fastest Cache rules for webp are found in the file.
|
||||
*
|
||||
* @see ewww_image_optimizer_wpfc_webp_enabled()
|
||||
*/
|
||||
$use_rewrite_rules = ! ewww_image_optimizer_webp_rewrite_verify();
|
||||
|
||||
/**
|
||||
* Filter wether EWW is using rewrite rules for webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $use_rewrite_rules True when EWWW uses rewrite rules. False otherwise.
|
||||
*/
|
||||
return (bool) apply_filters( 'rocket_webp_ewww_use_rewrite_rules', $use_rewrite_rules );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin uses a CDN-compatible technique to serve webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp_compatible_with_cdn() {
|
||||
if ( ! function_exists( 'ewww_image_optimizer_get_option' ) ) {
|
||||
// No EWWW, no webp.
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_exactdn' ) ) {
|
||||
// EWWW uses ExactDN.
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_webp_for_cdn' ) ) {
|
||||
// EWWW uses JS to rewrite file extensions.
|
||||
return true;
|
||||
}
|
||||
|
||||
// At this point, the plugin is using rewrite rules or nothing.
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin basename.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_basename() {
|
||||
if ( empty( $this->plugin_basename ) ) {
|
||||
$this->plugin_basename = rocket_has_constant( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' )
|
||||
? plugin_basename( rocket_get_constant( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' ) )
|
||||
: 'ewww-image-optimizer/ewww-image-optimizer.php';
|
||||
}
|
||||
|
||||
return $this->plugin_basename;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PRIVATE TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if EWWW is active for network.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_active_for_network() {
|
||||
static $is;
|
||||
|
||||
if ( isset( $is ) ) {
|
||||
return $is;
|
||||
}
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
$is = false;
|
||||
return $is;
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
$is = is_plugin_active_for_network( $this->get_basename() ) && ! get_site_option( 'ewww_image_optimizer_allow_multisite_override' );
|
||||
|
||||
return $is;
|
||||
}
|
||||
}
|
||||
+435
@@ -0,0 +1,435 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Subscriber for the WebP support with Imagify.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Imagify_Subscriber implements Webp_Interface, Subscriber_Interface {
|
||||
use Webp_Common;
|
||||
|
||||
/**
|
||||
* Options_Data instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
* @access private
|
||||
* @author Remy Perona
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Imagify basename.
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $plugin_basename;
|
||||
|
||||
/**
|
||||
* Imagify’s "serve webp" option name.
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $plugin_option_name_to_serve_webp;
|
||||
|
||||
/**
|
||||
* Temporarily store the result of $this->is_serving_webp().
|
||||
*
|
||||
* @var bool
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $tmp_is_serving_webp;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_webp_plugins' => 'register',
|
||||
'wp_rocket_loaded' => 'load_hooks',
|
||||
];
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Launch filters.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function load_hooks() {
|
||||
if ( ! $this->options->get( 'cache_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every time Imagify is (de)activated, we must "sync" our webp cache option.
|
||||
*/
|
||||
add_action( 'imagify_activation', [ $this, 'plugin_activation' ], 20 );
|
||||
add_action( 'imagify_deactivation', [ $this, 'plugin_deactivation' ], 20 );
|
||||
|
||||
if ( ! rocket_has_constant( 'IMAGIFY_VERSION' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since Rocket already updates the config file after updating its options, there is no need to do it again if the CDN or zone options change.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Every time Imagify’s option changes, we must "sync" our webp cache option.
|
||||
*/
|
||||
$option_name = $this->get_option_name_to_serve_webp();
|
||||
|
||||
if ( $this->is_active_for_network() ) {
|
||||
add_filter( 'add_site_option_' . $option_name, [ $this, 'sync_on_network_option_add' ], 10, 3 );
|
||||
add_filter( 'update_site_option_' . $option_name, [ $this, 'sync_on_network_option_update' ], 10, 4 );
|
||||
add_filter( 'pre_delete_site_option_' . $option_name, [ $this, 'store_option_value_before_network_delete' ], 10, 2 );
|
||||
add_filter( 'delete_site_option_' . $option_name, [ $this, 'sync_on_network_option_delete' ], 10, 2 );
|
||||
return;
|
||||
}
|
||||
|
||||
add_filter( 'add_option_' . $option_name, [ $this, 'sync_on_option_add' ], 10, 2 );
|
||||
add_filter( 'update_option_' . $option_name, [ $this, 'sync_on_option_update' ], 10, 2 );
|
||||
add_filter( 'delete_option', [ $this, 'store_option_value_before_delete' ] );
|
||||
add_filter( 'delete_option_' . $option_name, [ $this, 'sync_on_option_delete' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe deactivate webp cache after Imagify network option has been successfully added.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the network option.
|
||||
* @param mixed $value Value of the network option.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function sync_on_network_option_add( $option, $value, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id && ! empty( $value['display_webp'] ) ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe activate or deactivate webp cache after Imagify network option has been modified.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the network option.
|
||||
* @param mixed $value Current value of the network option.
|
||||
* @param mixed $old_value Old value of the network option.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function sync_on_network_option_update( $option, $value, $old_value, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id ) {
|
||||
$this->sync_on_option_update( $old_value, $value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the Imagify network option value before it is deleted.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Option name.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function store_option_value_before_network_delete( $option, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id ) {
|
||||
$this->tmp_is_serving_webp = $this->is_serving_webp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe activate webp cache after Imagify network option has been deleted.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the network option.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function sync_on_network_option_delete( $option, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id && false !== $this->tmp_is_serving_webp ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe deactivate webp cache after Imagify option has been successfully added.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to add.
|
||||
* @param mixed $value Value of the option.
|
||||
*/
|
||||
public function sync_on_option_add( $option, $value ) {
|
||||
if ( ! empty( $value['display_webp'] ) ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe activate or deactivate webp cache after Imagify option has been modified.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $old_value The old option value.
|
||||
* @param mixed $value The new option value.
|
||||
*/
|
||||
public function sync_on_option_update( $old_value, $value ) {
|
||||
$old_display = ! empty( $old_value['display_webp'] );
|
||||
$display = ! empty( $value['display_webp'] );
|
||||
|
||||
if ( $old_display !== $display || $old_value['display_webp_method'] !== $value['display_webp_method'] ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the Imagify option value before it is deleted.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to delete.
|
||||
*/
|
||||
public function store_option_value_before_delete( $option ) {
|
||||
if ( $this->get_option_name_to_serve_webp() === $option ) {
|
||||
$this->tmp_is_serving_webp = $this->is_serving_webp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe activate webp cache after Imagify option has been deleted.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the deleted option.
|
||||
*/
|
||||
public function sync_on_option_delete( $option ) {
|
||||
if ( false !== $this->tmp_is_serving_webp ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC TOOLS ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the plugin name.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return 'Imagify';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin identifier.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return 'imagify';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin converts images to webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_converting_to_webp() {
|
||||
if ( ! function_exists( 'get_imagify_option' ) ) {
|
||||
// No Imagify, no webp.
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) get_imagify_option( 'convert_to_webp' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin serves webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp() {
|
||||
if ( ! function_exists( 'get_imagify_option' ) ) {
|
||||
// No Imagify, no webp.
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) get_imagify_option( 'display_webp' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin uses a CDN-compatible technique to serve webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp_compatible_with_cdn() {
|
||||
if ( ! $this->is_serving_webp() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 'rewrite' !== get_imagify_option( 'display_webp_method' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin basename.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_basename() {
|
||||
if ( empty( $this->plugin_basename ) ) {
|
||||
$this->plugin_basename = rocket_has_constant( 'IMAGIFY_FILE' )
|
||||
? plugin_basename( rocket_get_constant( 'IMAGIFY_FILE' ) )
|
||||
: 'imagify/imagify.php';
|
||||
}
|
||||
|
||||
return $this->plugin_basename;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PRIVATE TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the name of the Imagify’s "serve webp" option.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_option_name_to_serve_webp() {
|
||||
if ( ! empty( $this->plugin_option_name_to_serve_webp ) ) {
|
||||
return $this->plugin_option_name_to_serve_webp;
|
||||
}
|
||||
|
||||
$default = 'imagify_settings';
|
||||
|
||||
if ( ! class_exists( '\Imagify_Options' ) || ! method_exists( '\Imagify_Options', 'get_instance' ) ) {
|
||||
$this->plugin_option_name_to_serve_webp = $default;
|
||||
return $this->plugin_option_name_to_serve_webp;
|
||||
}
|
||||
|
||||
$instance = \Imagify_Options::get_instance();
|
||||
|
||||
if ( ! method_exists( $instance, 'get_option_name' ) ) {
|
||||
$this->plugin_option_name_to_serve_webp = $default;
|
||||
return $this->plugin_option_name_to_serve_webp;
|
||||
}
|
||||
|
||||
$this->plugin_option_name_to_serve_webp = $instance->get_option_name();
|
||||
|
||||
return $this->plugin_option_name_to_serve_webp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if Imagify is active for network.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_active_for_network() {
|
||||
static $is;
|
||||
|
||||
if ( isset( $is ) ) {
|
||||
return $is;
|
||||
}
|
||||
|
||||
if ( function_exists( 'imagify_is_active_for_network' ) ) {
|
||||
$is = imagify_is_active_for_network();
|
||||
return $is;
|
||||
}
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
$is = false;
|
||||
return $is;
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
$is = is_plugin_active_for_network( $this->get_basename() );
|
||||
|
||||
return $is;
|
||||
}
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Subscriber for the WebP support with Optimus.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Optimus_Subscriber implements Webp_Interface, Subscriber_Interface {
|
||||
|
||||
/**
|
||||
* Optimus basename.
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $plugin_basename;
|
||||
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
if ( ! defined( 'OPTIMUS_FILE' ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'rocket_webp_plugins' => 'register',
|
||||
];
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Register the plugin.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $webp_plugins An array of Webp_Interface objects.
|
||||
* @return array
|
||||
*/
|
||||
public function register( $webp_plugins ) {
|
||||
$webp_plugins[] = $this;
|
||||
return $webp_plugins;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC TOOLS ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the plugin name.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return 'Optimus';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin identifier.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return 'optimus';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin converts images to webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_converting_to_webp() {
|
||||
if ( class_exists( '\Optimus' ) && method_exists( '\Optimus', 'get_options' ) ) {
|
||||
$options = \Optimus::get_options();
|
||||
} else {
|
||||
$options = get_option( 'optimus' );
|
||||
}
|
||||
|
||||
return ! empty( $options['webp_convert'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin serves webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin uses a CDN-compatible technique to serve webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp_compatible_with_cdn() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin basename.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_basename() {
|
||||
if ( empty( $this->plugin_basename ) ) {
|
||||
$this->plugin_basename = defined( 'OPTIMUS_FILE' ) ? plugin_basename( OPTIMUS_FILE ) : 'optimus/optimus.php';
|
||||
}
|
||||
|
||||
return $this->plugin_basename;
|
||||
}
|
||||
}
|
||||
+294
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Subscriber for the WebP support with ShortPixel.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class ShortPixel_Subscriber implements Webp_Interface, Subscriber_Interface {
|
||||
use Webp_Common;
|
||||
|
||||
/**
|
||||
* Options_Data instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
* @access private
|
||||
* @author Remy Perona
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* ShortPixel basename.
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $plugin_basename;
|
||||
|
||||
/**
|
||||
* ShortPixel’s "serve webp" option name.
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $plugin_option_name_to_serve_webp = 'wp-short-pixel-create-webp-markup';
|
||||
|
||||
/**
|
||||
* Temporarily store the result of $this->is_serving_webp().
|
||||
*
|
||||
* @var bool
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
private $tmp_is_serving_webp;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param Options_Data $options Options instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_webp_plugins' => 'register',
|
||||
'wp_rocket_loaded' => 'load_hooks',
|
||||
];
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Launch filters.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function load_hooks() {
|
||||
if ( ! $this->options->get( 'cache_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every time ShortPixel is (de)activated, we must "sync" our webp cache option.
|
||||
*/
|
||||
if ( did_action( 'activate_' . $this->get_basename() ) ) {
|
||||
$this->plugin_activation();
|
||||
}
|
||||
if ( did_action( 'deactivate_' . $this->get_basename() ) ) {
|
||||
$this->plugin_deactivation();
|
||||
}
|
||||
add_action( 'activate_' . $this->get_basename(), [ $this, 'plugin_activation' ], 20 );
|
||||
add_action( 'deactivate_' . $this->get_basename(), [ $this, 'plugin_deactivation' ], 20 );
|
||||
|
||||
if ( ! defined( 'SHORTPIXEL_IMAGE_OPTIMISER_VERSION' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since Rocket already updates the config file after updating its options, there is no need to do it again if the CDN or zone options change.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Every time ShortPixel’s option changes, we must "sync" our webp cache option.
|
||||
*/
|
||||
$option_name = $this->plugin_option_name_to_serve_webp;
|
||||
|
||||
add_filter( 'add_option_' . $option_name, [ $this, 'sync_on_option_add' ], 10, 2 );
|
||||
add_filter( 'update_option_' . $option_name, [ $this, 'sync_on_option_update' ], 10, 2 );
|
||||
add_filter( 'delete_option', [ $this, 'store_option_value_before_delete' ] );
|
||||
add_filter( 'delete_option_' . $option_name, [ $this, 'sync_on_option_delete' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe deactivate webp cache after ShortPixel option has been successfully added.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to add.
|
||||
* @param mixed $value Value of the option.
|
||||
*/
|
||||
public function sync_on_option_add( $option, $value ) {
|
||||
if ( $value ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe activate or deactivate webp cache after ShortPixel option has been modified.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $old_value The old option value.
|
||||
* @param mixed $value The new option value.
|
||||
*/
|
||||
public function sync_on_option_update( $old_value, $value ) {
|
||||
/**
|
||||
* 0 = Don’t serve webp.
|
||||
* 1 = <picture> + buffer
|
||||
* 2 = <picture> + hooks
|
||||
* 3 = .htaccess
|
||||
*/
|
||||
$old_value = $old_value > 0;
|
||||
$value = $value > 0;
|
||||
|
||||
if ( $old_value !== $value ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the ShortPixel option value before it is deleted.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to delete.
|
||||
*/
|
||||
public function store_option_value_before_delete( $option ) {
|
||||
if ( $this->plugin_option_name_to_serve_webp === $option ) {
|
||||
$this->tmp_is_serving_webp = $this->is_serving_webp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe activate webp cache after ShortPixel option has been deleted.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function sync_on_option_delete() {
|
||||
if ( false !== $this->tmp_is_serving_webp ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC TOOLS ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the plugin name.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return 'ShortPixel';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin identifier.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return 'shortpixel';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin converts images to webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_converting_to_webp() {
|
||||
return (bool) get_option( 'wp-short-create-webp' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin serves webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp() {
|
||||
return (bool) get_option( $this->plugin_option_name_to_serve_webp );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the plugin uses a CDN-compatible technique to serve webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp_compatible_with_cdn() {
|
||||
$display = (int) get_option( $this->plugin_option_name_to_serve_webp );
|
||||
|
||||
if ( ! $display ) {
|
||||
// The option is not enabled, no webp.
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 3 === $display ) {
|
||||
// The option is set to "rewrite rules".
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin basename.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_basename() {
|
||||
if ( empty( $this->plugin_basename ) ) {
|
||||
$this->plugin_basename = defined( 'SHORTPIXEL_PLUGIN_FILE' ) ? plugin_basename( SHORTPIXEL_PLUGIN_FILE ) : 'shortpixel-image-optimiser/wp-shortpixel.php';
|
||||
}
|
||||
|
||||
return $this->plugin_basename;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp;
|
||||
|
||||
/**
|
||||
* Trait for webp subscribers, focussed on plugins that serve webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
trait Webp_Common {
|
||||
|
||||
/**
|
||||
* Register the plugin.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $webp_plugins An array of Webp_Interface objects.
|
||||
* @return array
|
||||
*/
|
||||
public function register( $webp_plugins ) {
|
||||
$webp_plugins[] = $this;
|
||||
return $webp_plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* On plugin activation, deactivate Rocket webp cache if the plugin is serving webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function plugin_activation() {
|
||||
if ( $this->is_serving_webp() ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* On plugin deactivation, activate Rocket webp cache if the plugin is serving webp.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function plugin_deactivation() {
|
||||
if ( $this->is_serving_webp() ) {
|
||||
$this->trigger_webp_change();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger an action when the webp feature is enabled/disabled in a third party plugin.
|
||||
*
|
||||
* @since 3.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function trigger_webp_change() {
|
||||
/**
|
||||
* Trigger an action when the webp feature is enabled/disabled in a third party plugin.
|
||||
*
|
||||
* @since 3.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
do_action( 'rocket_third_party_webp_change' );
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp;
|
||||
|
||||
/**
|
||||
* Interface to use for webp subscribers.
|
||||
*
|
||||
* @since 3.4
|
||||
*/
|
||||
interface Webp_Interface {
|
||||
|
||||
/**
|
||||
* Get the plugin name.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name();
|
||||
|
||||
/**
|
||||
* Get the plugin identifier.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id();
|
||||
|
||||
/**
|
||||
* Tell if the plugin converts images to webp.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_converting_to_webp();
|
||||
|
||||
/**
|
||||
* Tell if the plugin serves webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp();
|
||||
|
||||
/**
|
||||
* Tell if the plugin uses a CDN-compatible technique to serve webp images on frontend.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_serving_webp_compatible_with_cdn();
|
||||
|
||||
/**
|
||||
* Get the plugin basename.
|
||||
*
|
||||
* @since 3.4
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_basename();
|
||||
}
|
||||
Vendored
+398
@@ -0,0 +1,398 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class that handles events related to plugins that add mobile themes.
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Mobile_Subscriber implements Subscriber_Interface {
|
||||
|
||||
/**
|
||||
* Options to activate when a mobile plugin is active.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $options = [
|
||||
'cache_mobile' => 1,
|
||||
'do_caching_mobile_files' => 1,
|
||||
];
|
||||
|
||||
/**
|
||||
* Cache the value of self::is_mobile_plugin_active().
|
||||
*
|
||||
* @since 3.2
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @var array An array of arrays of booleans.
|
||||
* First level of keys corresponds to the network ID. Second level of keys corresponds to the blog ID.
|
||||
*/
|
||||
protected static $is_mobile_active = [];
|
||||
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
// In case a mobile plugin has already been activated.
|
||||
$do = [];
|
||||
$undo = [];
|
||||
$plugin_events = [];
|
||||
|
||||
if ( ! function_exists( '\is_plugin_active' ) ) {
|
||||
include_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
foreach ( static::get_mobile_plugins() as $plugin => $plugin_data ) {
|
||||
if ( \did_action( 'activate_' . $plugin ) &&
|
||||
! isset( $plugin_data['is_active_callback'] ) ) {
|
||||
$do[] = $plugin;
|
||||
}
|
||||
|
||||
if ( \did_action( 'activate_' . $plugin ) &&
|
||||
isset( $plugin_data['is_active_callback'] ) &&
|
||||
call_user_func( $plugin_data['is_active_callback'] ) ) {
|
||||
$do[] = $plugin;
|
||||
}
|
||||
|
||||
if ( \did_action( 'deactivate_' . $plugin ) ) {
|
||||
$undo[] = $plugin;
|
||||
}
|
||||
|
||||
if ( \is_plugin_active( $plugin ) ) {
|
||||
if ( isset( $plugin_data['activation_hook'] ) ) {
|
||||
$plugin_events[ $plugin_data['activation_hook'] ] = 'maybe_update_mobile_cache_activation_plugin_hook';
|
||||
}
|
||||
if ( isset( $plugin_data['deactivation_hook'] ) ) {
|
||||
$plugin_events[ $plugin_data['deactivation_hook'] ] = 'maybe_update_mobile_cache_activation_plugin_hook';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( array_diff( $do, $undo ) || array_diff( $undo, $do ) ) {
|
||||
static::update_mobile_cache_activation();
|
||||
}
|
||||
|
||||
// Register events.
|
||||
$events = [
|
||||
// Plugin activation/deactivation.
|
||||
'add_option_active_plugins' => [ 'add_option_callback', 10, 2 ],
|
||||
'update_option_active_plugins' => [ 'update_option_callback', 10, 2 ],
|
||||
'delete_option_active_plugins' => 'delete_option_callback',
|
||||
'add_site_option_active_sitewide_plugins' => [ 'add_site_option_callback', 10, 3 ],
|
||||
'update_site_option_active_sitewide_plugins' => [ 'update_site_option_callback', 10, 4 ],
|
||||
'delete_site_option_active_sitewide_plugins' => [ 'delete_site_option_callback', 10, 2 ],
|
||||
// WPR settings (`get_option()`).
|
||||
'option_' . WP_ROCKET_SLUG => 'mobile_options_filter',
|
||||
];
|
||||
|
||||
foreach ( static::$options as $option => $value ) {
|
||||
// WPR settings (`get_rocket_option()`).
|
||||
$events[ 'pre_get_rocket_option_' . $option ] = 'is_mobile_plugin_active_callback';
|
||||
}
|
||||
|
||||
$events = array_merge( $events, $plugin_events );
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOK CALLBACKS ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Callback triggered after the option `active_plugins` is created.
|
||||
* This should normally never be triggered.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to add.
|
||||
* @param mixed $value Value of the option.
|
||||
*/
|
||||
public function add_option_callback( $option, $value ) {
|
||||
$this->maybe_update_mobile_cache_activation( $value, [] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback triggered after the option `active_plugins` is updated.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $old_value The old option value.
|
||||
* @param mixed $value Value of the option.
|
||||
*/
|
||||
public function update_option_callback( $old_value, $value ) {
|
||||
$this->maybe_update_mobile_cache_activation( $value, $old_value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback triggered after the option `active_plugins` is deleted.
|
||||
* Very low probability to be triggered.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_option_callback() {
|
||||
static::update_mobile_cache_activation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback triggered after the option `active_sitewide_plugins` is created.
|
||||
* This should normally never be triggered.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to add.
|
||||
* @param mixed $value Value of the option.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function add_site_option_callback( $option, $value, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id ) {
|
||||
$this->maybe_update_mobile_cache_activation( $value, [] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback triggered after the option `active_sitewide_plugins` is updated.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to add.
|
||||
* @param mixed $value Value of the option.
|
||||
* @param mixed $old_value The old option value.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function update_site_option_callback( $option, $value, $old_value, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id ) {
|
||||
$this->maybe_update_mobile_cache_activation( $value, $old_value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback triggered after the option `active_sitewide_plugins` is deleted.
|
||||
* Very low probability to be triggered.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option Name of the option to add.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
public function delete_site_option_callback( $option, $network_id ) {
|
||||
if ( get_current_network_id() === $network_id ) {
|
||||
static::update_mobile_cache_activation();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable mobile caching when a mobile plugin is activated, or revert it back to its previous state when a mobile plugin is deactivated.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $value The new option value.
|
||||
* @param mixed $old_value The old option value.
|
||||
*/
|
||||
public function maybe_update_mobile_cache_activation( $value, $old_value ) {
|
||||
$plugins = static::get_mobile_plugins();
|
||||
$plugins = array_keys( $plugins );
|
||||
$value = array_intersect( $plugins, (array) $value );
|
||||
$old_value = array_intersect( $plugins, (array) $old_value );
|
||||
|
||||
if ( $value !== $old_value ) {
|
||||
static::update_mobile_cache_activation();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables mobile caching when a mobile plugin option is activated, or reverts it back to its previous state when a mobile plugin option is deactivated.
|
||||
*
|
||||
* @since 3.4.2
|
||||
* @access public
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_update_mobile_cache_activation_plugin_hook() {
|
||||
$is_mobile_plugin_active = static::is_mobile_plugin_active();
|
||||
static::reset_class_cache();
|
||||
$is_new_mobile_plugin_active = static::is_mobile_plugin_active();
|
||||
if ( $is_mobile_plugin_active !== $is_new_mobile_plugin_active ) {
|
||||
static::update_mobile_cache_activation();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces the values for the mobile options if a mobile plugin is active.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $values Option values.
|
||||
* @return array
|
||||
*/
|
||||
public function mobile_options_filter( $values ) {
|
||||
if ( static::is_mobile_plugin_active() ) {
|
||||
return array_merge( (array) $values, static::$options );
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces the value for a mobile option if a mobile plugin is active.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param int|null $value Option value.
|
||||
* @return int|null
|
||||
*/
|
||||
public function is_mobile_plugin_active_callback( $value ) {
|
||||
if ( static::is_mobile_plugin_active() ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** MAIN HELPERS ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Update the config file and the advanced cache file.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public static function update_mobile_cache_activation() {
|
||||
// Reset class cache.
|
||||
static::reset_class_cache();
|
||||
|
||||
// Update the config file.
|
||||
rocket_generate_config_file();
|
||||
// Update the advanced cache file.
|
||||
rocket_generate_advanced_cache_file();
|
||||
// Flush htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset `is_mobile_plugin_active()` cache.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public static function reset_class_cache() {
|
||||
// Reset class cache.
|
||||
unset( static::$is_mobile_active[ get_current_network_id() ][ get_current_blog_id() ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the concerned plugins.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_mobile_plugins() {
|
||||
return [
|
||||
'jetpack/jetpack.php' => [
|
||||
'is_active_callback' => function () {
|
||||
if ( ! class_exists( 'Jetpack' ) ) {
|
||||
return false;
|
||||
}
|
||||
return \Jetpack::is_active() && \Jetpack::is_module_active( 'minileven' );
|
||||
},
|
||||
'activation_hook' => 'jetpack_activate_module_minileven',
|
||||
'deactivation_hook' => 'jetpack_deactivate_module_minileven',
|
||||
],
|
||||
'wptouch/wptouch.php' => [],
|
||||
'wiziapp-create-your-own-native-iphone-app/wiziapp.php' => [],
|
||||
'wordpress-mobile-pack/wordpress-mobile-pack.php' => [],
|
||||
'wp-mobilizer/wp-mobilizer.php' => [],
|
||||
'wp-mobile-edition/wp-mobile-edition.php' => [],
|
||||
'device-theme-switcher/dts_controller.php' => [],
|
||||
'wp-mobile-detect/wp-mobile-detect.php' => [],
|
||||
'easy-social-share-buttons3/easy-social-share-buttons3.php' => [],
|
||||
'jet-menu/jet-menu.php' => [],
|
||||
'wpdiscuz/class.WpdiscuzCore.php' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if a mobile plugin is active.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if a mobile plugin in the list is active, false otherwise.
|
||||
*/
|
||||
public static function is_mobile_plugin_active() {
|
||||
$network_id = get_current_network_id();
|
||||
$blog_id = get_current_blog_id();
|
||||
|
||||
if ( isset( static::$is_mobile_active[ $network_id ][ $blog_id ] ) ) {
|
||||
return static::$is_mobile_active[ $network_id ][ $blog_id ];
|
||||
}
|
||||
|
||||
if ( ! function_exists( '\is_plugin_active' ) ) {
|
||||
include_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
if ( ! isset( static::$is_mobile_active[ $network_id ] ) ) {
|
||||
static::$is_mobile_active[ $network_id ] = [];
|
||||
}
|
||||
|
||||
foreach ( static::get_mobile_plugins() as $mobile_plugin => $mobile_plugin_data ) {
|
||||
if ( \is_plugin_active( $mobile_plugin ) &&
|
||||
isset( $mobile_plugin_data['is_active_callback'] ) &&
|
||||
call_user_func( $mobile_plugin_data['is_active_callback'] ) ) {
|
||||
static::$is_mobile_active[ $network_id ][ $blog_id ] = true;
|
||||
return true;
|
||||
}
|
||||
if ( \is_plugin_active( $mobile_plugin ) &&
|
||||
! isset( $mobile_plugin_data['is_active_callback'] ) ) {
|
||||
static::$is_mobile_active[ $network_id ][ $blog_id ] = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static::$is_mobile_active[ $network_id ][ $blog_id ] = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class that handles events related to Next Gen Gallery.
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class NGG_Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
if ( ! class_exists( 'C_NextGEN_Bootstrap' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
return [
|
||||
'run_ngg_resource_manager' => 'deactivate_resource_manager',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate NGG Resource Manager to prevent conflict with WP Rocket output buffering
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param bool $valid_request Indicates if the current request is valid for the NGG resource manager.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deactivate_resource_manager( $valid_request ) {
|
||||
if ( is_admin() ) {
|
||||
return $valid_request;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Third_Party\Plugins;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Compatibility class for SyntaxHighlighter plugin
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class SyntaxHighlighter_Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
if ( ! class_exists( 'SyntaxHighlighter' ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'rocket_exclude_defer_js' => 'exclude_defer_js_syntaxhighlighter_scripts',
|
||||
'rocket_exclude_js' => 'exclude_minify_js_syntaxhighlighter_scripts',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds SyntaxHighlighter scripts to defer JS exclusion
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $excluded_scripts Array of scripts to exclude.
|
||||
* @return array
|
||||
*/
|
||||
public function exclude_defer_js_syntaxhighlighter_scripts( $excluded_scripts ) {
|
||||
return array_merge(
|
||||
$excluded_scripts,
|
||||
[
|
||||
'syntaxhighlighter/syntaxhighlighter3/scripts/(.*).js',
|
||||
'syntaxhighlighter/syntaxhighlighter2/scripts/(.*).js',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds SyntaxHighlighter scripts to minify/combine JS exclusion
|
||||
*
|
||||
* @since 3.3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $excluded_scripts Array of scripts to exclude.
|
||||
* @return array
|
||||
*/
|
||||
public function exclude_minify_js_syntaxhighlighter_scripts( $excluded_scripts ) {
|
||||
return array_merge(
|
||||
$excluded_scripts,
|
||||
[
|
||||
rocket_clean_exclude_file( plugins_url( 'syntaxhighlighter/syntaxhighlighter3/scripts/(.*).js' ) ),
|
||||
rocket_clean_exclude_file( plugins_url( 'syntaxhighlighter/syntaxhighlighter2/scripts/(.*).js' ) ),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Traits;
|
||||
|
||||
trait Config_Updater {
|
||||
/**
|
||||
* Update htaccess and WP Rocket config file if the option was modified.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $old_value Option's previous value.
|
||||
* @param string $value Option's new value.
|
||||
* @return void
|
||||
*/
|
||||
public function after_update_single_option( $old_value, $value ) {
|
||||
if ( $old_value !== $value ) {
|
||||
$this->flush_htaccess();
|
||||
$this->generate_config_file();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the htaccess update request
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function flush_htaccess() {
|
||||
wp_cache_set( 'rocket_flush_htaccess', 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets WP Rocket config file update request
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function generate_config_file() {
|
||||
wp_cache_set( 'rocket_generate_config_file', 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the files update if requested
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_update_config() {
|
||||
if ( wp_cache_get( 'rocket_flush_htaccess' ) ) {
|
||||
flush_rocket_htaccess();
|
||||
wp_cache_delete( 'rocket_flush_htaccess' );
|
||||
}
|
||||
|
||||
if ( wp_cache_get( 'rocket_generate_config_file' ) ) {
|
||||
\rocket_generate_config_file();
|
||||
wp_cache_delete( 'rocket_generate_config_file' );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Traits;
|
||||
|
||||
/**
|
||||
* Statically store values.
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
trait Memoize {
|
||||
|
||||
/**
|
||||
* Store the values.
|
||||
*
|
||||
* @var array
|
||||
* @since 3.3
|
||||
*/
|
||||
private static $memoized = [];
|
||||
|
||||
/**
|
||||
* Tell if a value is memoized.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $method Name of the method.
|
||||
* @param array $args Arguments passed to the parent method. It is used to build a hash.
|
||||
* @return bool
|
||||
*/
|
||||
final public static function is_memoized( $method, $args = [] ) {
|
||||
$hash = self::get_memoize_args_hash( $args );
|
||||
return isset( self::$memoized[ $method ][ $hash ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a stored value.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $method Name of the method.
|
||||
* @param array $args Arguments passed to the parent method. It is used to build a hash.
|
||||
* @return mixed
|
||||
*/
|
||||
final public static function get_memoized( $method, $args = [] ) {
|
||||
$hash = self::get_memoize_args_hash( $args );
|
||||
return isset( self::$memoized[ $method ][ $hash ] ) ? self::$memoized[ $method ][ $hash ] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache a value.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param string $method Name of the method.
|
||||
* @param array $args Arguments passed to the parent method. It is used to build a hash.
|
||||
* @param mixed $value Value to store.
|
||||
* @return mixed The stored value.
|
||||
*/
|
||||
final public static function memoize( $method, $args = [], $value = null ) {
|
||||
$hash = self::get_memoize_args_hash( $args );
|
||||
|
||||
if ( ! isset( self::$memoized[ $method ] ) ) {
|
||||
self::$memoized[ $method ] = [];
|
||||
}
|
||||
|
||||
self::$memoized[ $method ][ $hash ] = $value;
|
||||
return self::$memoized[ $method ][ $hash ]; //phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a hash based on an array of arguments.
|
||||
*
|
||||
* @since 3.3
|
||||
*
|
||||
* @param array $args An array of arguments.
|
||||
* @return string
|
||||
*/
|
||||
private static function get_memoize_args_hash( $args ) {
|
||||
if ( [] === $args ) {
|
||||
return 'd751713988987e9331980363e24189ce'; // `md5( json_encode( [] ) )`
|
||||
}
|
||||
|
||||
return md5( call_user_func( 'json_encode', $args ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user