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,187 @@
<?php
namespace WP_Rocket\Engine\HealthCheck;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use UnexpectedValueException;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Add a weekly event to check for the cache directories sizes
* and send a notification if it's bigger thant the defined maximum size
*
* @since 3.3.5
*/
class CacheDirSizeCheck implements Subscriber_Interface {
/**
* Event name
*/
const CRON_NAME = 'rocket_cache_dir_size_check';
/**
* Maximum allowed size
*/
const MAX_SIZE = 10737418240;
/**
* ROUTE endpoint to request
*/
const ROUTE = 'api/wp-rocket/cache-dir-check.php';
/**
* Absolute path to the minify directory
*
* @var string
*/
private $minify_path;
/**
* Full URL to the API endpoint
*
* @var string
*/
private $api_endpoint;
/**
* Instantiate the class
*
* @param string $minify_path Absolute path to the minify directory.
* @param string $rocket_url WP Rocket website URL.
*/
public function __construct( $minify_path, $rocket_url ) {
$this->minify_path = $minify_path . get_current_blog_id();
$this->api_endpoint = $rocket_url . self::ROUTE;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.3.5
*
* @return array
*/
public static function get_subscribed_events() {
return [
'cron_schedules' => 'add_schedule',
'init' => 'schedule_cache_dir_size_check',
self::CRON_NAME => 'cache_dir_size_check',
'wp_rocket_upgrade' => [ 'delete_option_after_upgrade', 11, 2 ],
];
}
/**
* Adds the weekly interval if it doesn't already exist
*
* @since 3.3.5
*
* @param array $schedules Array of intervals.
* @return array
*/
public function add_schedule( $schedules ) {
if ( isset( $schedules['weekly'] ) ) {
return $schedules;
}
$schedules['weekly'] = [
'interval' => 604800,
'display' => __( 'weekly', 'rocket' ),
];
return $schedules;
}
/**
* Schedules the cron event if not yet scheduled.
*
* @since 3.3.5
*
* @return void
*/
public function schedule_cache_dir_size_check() {
if ( ! wp_next_scheduled( self::CRON_NAME ) ) {
wp_schedule_event( time(), 'weekly', self::CRON_NAME );
}
}
/**
* Checks the cache dir size when the event is triggered
* and send a notification if the directory size is above the defined maximum size
*
* @since 3.3.5
*
* @return void
*/
public function cache_dir_size_check() {
if ( 1 === (int) get_option( self::CRON_NAME ) ) {
return;
}
$checks = [
'min' => $this->minify_path,
];
foreach ( $checks as $type => $path ) {
$size = $this->get_dir_size( $path );
if ( $size > self::MAX_SIZE ) {
$this->send_notification( $type );
}
}
update_option( self::CRON_NAME, 1 );
}
/**
* Deletes the check size option when updating the plugin
*
* @since 3.3.6
*
* @param string $new_version Latest WP Rocket version.
* @param string $current_version Installed WP Rocket version.
*/
public function delete_option_after_upgrade( $new_version, $current_version ) {
if ( version_compare( $current_version, $new_version, '<' ) ) {
delete_option( self::CRON_NAME );
}
}
/**
* Gets the size of the provided directory
*
* @since 3.3.5
*
* @param string $dir Absolute path to the directory.
* @return int
*/
private function get_dir_size( $dir ) {
$size = 0;
try {
foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ) ) as $file ) {
$size += $file->getSize();
}
return $size;
} catch ( UnexpectedValueException $e ) {
return $size;
}
}
/**
* Sends a notification to our endpoint with the type of directory
*
* @since 3.3.5
*
* @param string $dir_type Type of directory.
* @return void
*/
private function send_notification( $dir_type ) {
wp_safe_remote_post(
$this->api_endpoint,
[
'body' => 'cache_dir_type=' . $dir_type,
]
);
}
}

View File

@@ -0,0 +1,149 @@
<?php
namespace WP_Rocket\Engine\HealthCheck;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
class HealthCheck implements Subscriber_Interface {
/**
* Instance of options.
*
* @var Options_Data
*/
private $options;
/**
* Array of events with their descriptions.
*
* @var array
*/
private $events;
/**
* Creates an instance of the health checker.
*
* @param Options_Data $options Options_Data instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
$this->events = $this->get_events();
}
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_notices' => 'missed_cron',
];
}
/**
* Display a warning notice if WP Rocket scheduled events are not running properly.
*
* @since 3.5.4
*/
public function missed_cron() {
if ( ! $this->should_check() ) {
return;
}
$delay = rocket_get_constant( 'DISABLE_WP_CRON' ) ? HOUR_IN_SECONDS : 5 * MINUTE_IN_SECONDS;
$list = '';
$events = $this->events;
foreach ( $this->events as $event => $description ) {
$timestamp = wp_next_scheduled( $event );
if (
false === $timestamp
||
( $timestamp + $delay - time() ) > 0
) {
unset( $events[ $event ] );
continue;
}
$list .= "<li>{$description}</li>";
}
if ( empty( $events ) ) {
return;
}
$message = sprintf(
'<p>%1$s</p>
<ul>%2$s</ul>
<p>%3$s</p>',
_n(
'The following scheduled event failed to run. This may indicate the CRON system is not running properly, which can prevent some WP Rocket features from working as intended:',
'The following scheduled events failed to run. This may indicate the CRON system is not running properly, which can prevent some WP Rocket features from working as intended:',
count( $events ),
'rocket'
),
$list,
__( 'Please contact your host to check if CRON is working.', 'rocket' )
);
rocket_notice_html(
[
'status' => 'warning',
'dismissible' => '',
'message' => $message,
'dismiss_button' => 'rocket_warning_cron',
]
);
}
/**
* Checks if health check should run.
*
* @since 3.5.4
*
* @return bool true when should do health check; else, false.
*/
protected function should_check() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return false;
}
if ( 'settings_page_wprocket' !== get_current_screen()->id ) {
return false;
}
$dismissed = (array) get_user_meta( get_current_user_id(), 'rocket_boxes', true );
if ( in_array( 'rocket_warning_cron', $dismissed, true ) ) {
return false;
}
return ! (
0 === (int) $this->options->get( 'purge_cron_interval', 0 )
&&
0 === (int) $this->options->get( 'async_css', 0 )
&&
0 === (int) $this->options->get( 'manual_preload', 0 )
&&
0 === (int) $this->options->get( 'schedule_automatic_cleanup', 0 )
);
}
/**
* Gets an array of events with their descriptions.
*
* @since 3.5.4
*
* @return array array of events => descriptions.
*/
protected function get_events() {
return [
'rocket_purge_time_event' => __( 'Scheduled Cache Purge', 'rocket' ),
'rocket_database_optimization_time_event' => __( 'Scheduled Database Optimization', 'rocket' ),
'rocket_database_optimization_cron_interval' => __( 'Database Optimization Process', 'rocket' ),
'rocket_preload_cron_interval' => _x( 'Preload', 'noun', 'rocket' ),
'rocket_critical_css_generation_cron_interval' => __( 'Critical Path CSS Generation Process', 'rocket' ),
];
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace WP_Rocket\Engine\HealthCheck;
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service Provider for health check subscribers
*
* @since 3.6
*/
class ServiceProvider 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 = [
'health_check',
'cache_dir_size_check',
];
/**
* Registers the option array in the container
*
* @since 3.6
* @author Remy Perona
*
* @return void
*/
public function register() {
$this->getContainer()->share( 'health_check', 'WP_Rocket\Engine\HealthCheck\HealthCheck' )
->withArgument( $this->getContainer()->get( 'options' ) );
$this->getContainer()->share( 'cache_dir_size_check', 'WP_Rocket\Engine\HealthCheck\CacheDirSizeCheck' )
->withArgument( rocket_get_constant( 'WP_ROCKET_MINIFY_CACHE_PATH' ) )
->withArgument( rocket_get_constant( 'WP_ROCKET_WEB_MAIN' ) );
}
}