add wp-rocket

This commit is contained in:
nguyen dung
2022-02-18 19:09:35 +07:00
parent 39b8cb3612
commit 3110d00ee7
927 changed files with 271703 additions and 2 deletions

View File

@@ -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' );
}
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace WP_Rocket\Traits;
/**
* Statically store values.
*
* @since 3.3
* @author Grégory Viguier
*/
trait Memoize {
/**
* Store the values.
*
* @var array
* @since 3.3
* @access private
* @author Grégory Viguier
*/
private static $memoized = [];
/**
* Tell if a value is memoized.
*
* @since 3.3
* @access public
* @author Grégory Viguier
*
* @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
* @access public
* @author Grégory Viguier
*
* @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
* @access public
* @author Grégory Viguier
*
* @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 ];
}
/**
* Create a hash based on an array of arguments.
*
* @since 3.3
* @access private
* @author Grégory Viguier
*
* @param array $args An array of arguments.
* @return string
*/
final private static function get_memoize_args_hash( $args ) {
if ( [] === $args ) {
return 'd751713988987e9331980363e24189ce'; // `md5( json_encode( [] ) )`
}
return md5( call_user_func( 'json_encode', $args ) );
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace WP_Rocket\Traits;
use WP_Rocket\Logger\Logger;
/**
* Trait for the plugin updater.
*
* @since 3.3.6
* @author Grégory Viguier
*/
trait Updater_Api_Tools {
/**
* An ID to use when a API request fails.
*
* @var string
* @since 3.3.6
* @access protected
* @author Grégory Viguier
*/
/*protected $request_error_id;*/
/** ----------------------------------------------------------------------------------------- */
/** TOOLS =================================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get a \WP_Error object to use when the request to WP Rockets server fails.
*
* @since 3.3.6
* @access protected
* @author Grégory Viguier
*
* @param mixed $data Error data to pass along the \WP_Error object.
* @return \WP_Error
*/
protected function get_request_error( $data = [] ) {
if ( ! is_array( $data ) ) {
$data = [
'response' => $data,
];
}
Logger::debug(
'Error when contacting the API.',
array_merge( [ 'Plugin Information' ], $data )
);
return new \WP_Error(
$this->request_error_id,
sprintf(
// translators: %s is an URL.
__( 'An unexpected error occurred. Something may be wrong with WP-Rocket.me or this server&#8217;s configuration. If you continue to have problems, <a href="%s">contact support</a>.', 'rocket' ),
$this->get_support_url()
),
$data
);
}
/**
* Get support URL.
*
* @since 3.3.6
* @access protected
* @author Grégory Viguier
*
* @return string
*/
protected function get_support_url() {
return rocket_get_external_url(
'support',
[
'utm_source' => 'wp_plugin',
'utm_medium' => 'wp_rocket',
]
);
}
/**
* Get a plugin slug, given its full path.
*
* @since 3.3.6
* @access protected
* @author Grégory Viguier
*
* @param string $plugin_file Full path to the plugin.
* @return string
*/
protected function get_plugin_slug( $plugin_file ) {
$plugin_file = trim( $plugin_file, '/' );
$plugin_slug = explode( '/', $plugin_file );
$plugin_slug = end( $plugin_slug );
$plugin_slug = str_replace( '.php', '', $plugin_slug );
return $plugin_slug;
}
}