add wp-rocket
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Cache;
|
||||
|
||||
use WP_Rocket\Event_Management\Event_Manager;
|
||||
use WP_Rocket\Event_Management\Event_Manager_Aware_Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Subscriber for the cache admin events
|
||||
*
|
||||
* @since 3.5.5
|
||||
*/
|
||||
class AdminSubscriber implements Event_Manager_Aware_Subscriber_Interface {
|
||||
/**
|
||||
* Event Manager instance
|
||||
*
|
||||
* @var Event_Manager;
|
||||
*/
|
||||
protected $event_manager;
|
||||
|
||||
/**
|
||||
* AdvancedCache instance
|
||||
*
|
||||
* @var AdvancedCache
|
||||
*/
|
||||
private $advanced_cache;
|
||||
|
||||
/**
|
||||
* WPCache instance
|
||||
*
|
||||
* @var WPCache
|
||||
*/
|
||||
private $wp_cache;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param AdvancedCache $advanced_cache AdvancedCache instance.
|
||||
* @param WPCache $wp_cache WPCache instance.
|
||||
*/
|
||||
public function __construct( AdvancedCache $advanced_cache, WPCache $wp_cache ) {
|
||||
$this->advanced_cache = $advanced_cache;
|
||||
$this->wp_cache = $wp_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
$slug = rocket_get_constant( 'WP_ROCKET_SLUG' );
|
||||
|
||||
return [
|
||||
'admin_init' => [
|
||||
[ 'register_terms_row_action' ],
|
||||
[ 'maybe_set_wp_cache' ],
|
||||
],
|
||||
'admin_notices' => [
|
||||
[ 'notice_advanced_cache_permissions' ],
|
||||
[ 'notice_wp_config_permissions' ],
|
||||
],
|
||||
"update_option_{$slug}" => [ 'maybe_set_wp_cache', 12 ],
|
||||
'site_status_tests' => 'add_wp_cache_status_test',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the event manager for the subscriber.
|
||||
*
|
||||
* @param Event_Manager $event_manager Event Manager instance.
|
||||
*/
|
||||
public function set_event_manager( Event_Manager $event_manager ) {
|
||||
$this->event_manager = $event_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the action for each public taxonomy
|
||||
*
|
||||
* @since 3.5.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_terms_row_action() {
|
||||
$taxonomies = get_taxonomies(
|
||||
[
|
||||
'public' => true,
|
||||
'publicly_queryable' => true,
|
||||
]
|
||||
);
|
||||
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
$this->event_manager->add_callback( "{$taxonomy}_row_actions", [ $this, 'add_purge_term_link' ], 10, 2 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a link "Purge this cache" in the terms list table
|
||||
*
|
||||
* @param array $actions An array of action links to be displayed.
|
||||
* @param WP_Term $term Term object.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_purge_term_link( $actions, $term ) {
|
||||
if ( ! current_user_can( 'rocket_purge_terms' ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
$url = wp_nonce_url(
|
||||
admin_url( "admin-post.php?action=purge_cache&type=term-{$term->term_id}&taxonomy={$term->taxonomy}" ),
|
||||
"purge_cache_term-{$term->term_id}"
|
||||
);
|
||||
|
||||
$actions['rocket_purge'] = sprintf(
|
||||
'<a href="%1$s">%2$s</a>',
|
||||
$url,
|
||||
__( 'Clear this cache', 'rocket' )
|
||||
);
|
||||
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the notice for advanced-cache.php permissions
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice_advanced_cache_permissions() {
|
||||
$this->advanced_cache->notice_permissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set WP_CACHE constant to true if needed
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_set_wp_cache() {
|
||||
$this->wp_cache->maybe_set_wp_cache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the notice for wp-config.php permissions
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice_wp_config_permissions() {
|
||||
$this->wp_cache->notice_wp_config_permissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Site Health check for the WP_CACHE constant value
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param array $tests An array of tests to perform.
|
||||
* @return array
|
||||
*/
|
||||
public function add_wp_cache_status_test( $tests ) {
|
||||
return $this->wp_cache->add_wp_cache_status_test( $tests );
|
||||
}
|
||||
}
|
206
wp-content/plugins/wp-rocket/inc/Engine/Cache/AdvancedCache.php
Normal file
206
wp-content/plugins/wp-rocket/inc/Engine/Cache/AdvancedCache.php
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Cache;
|
||||
|
||||
use WP_Filesystem_Direct;
|
||||
use WP_Rocket\Engine\Activation\ActivationInterface;
|
||||
use WP_Rocket\Engine\Deactivation\DeactivationInterface;
|
||||
|
||||
class AdvancedCache implements ActivationInterface, DeactivationInterface {
|
||||
|
||||
/**
|
||||
* Absolute path to template files
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $template_path;
|
||||
|
||||
/**
|
||||
* WP Content directory path
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $content_dir;
|
||||
|
||||
/**
|
||||
* Instance of the filesystem handler.
|
||||
*
|
||||
* @var WP_Filesystem_Direct
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* Instantiate of the class.
|
||||
*
|
||||
* @param string $template_path Absolute path to template files.
|
||||
* @param WP_Filesystem_Direct $filesystem Instance of the filesystem handler.
|
||||
*/
|
||||
public function __construct( $template_path, $filesystem ) {
|
||||
$this->template_path = $template_path;
|
||||
$this->content_dir = rocket_get_constant( 'WP_CONTENT_DIR' );
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actions to perform on plugin activation
|
||||
*
|
||||
* @since 3.6.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function activate() {
|
||||
add_action( 'rocket_activation', [ $this, 'update_advanced_cache' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Actions to perform on plugin deactivation
|
||||
*
|
||||
* @since 3.6.3
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deactivate() {
|
||||
add_action( 'rocket_deactivation', [ $this, 'update_advanced_cache' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the advanced-cache.php file with its content
|
||||
*
|
||||
* @since 3.6.3
|
||||
*
|
||||
* @param int $sites_number Number of WP Rocket config files found.
|
||||
* @return void
|
||||
*/
|
||||
public function update_advanced_cache( $sites_number = 0 ) {
|
||||
/**
|
||||
* Filters whether to generate the advanced-cache.php file.
|
||||
*
|
||||
* @since 3.6.3
|
||||
*
|
||||
* @param bool True (default) to go ahead with advanced cache file generation; false to stop generation.
|
||||
*/
|
||||
if ( ! (bool) apply_filters( 'rocket_generate_advanced_cache_file', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = $this->get_advanced_cache_content();
|
||||
|
||||
if ( 'rocket_deactivation' === current_filter() ) {
|
||||
if ( is_multisite() && 0 !== $sites_number ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = '';
|
||||
}
|
||||
|
||||
$this->filesystem->put_contents(
|
||||
"{$this->content_dir}/advanced-cache.php",
|
||||
$content,
|
||||
rocket_get_filesystem_perms( 'file' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the content for the advanced-cache.php file
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_advanced_cache_content() {
|
||||
$content = $this->filesystem->get_contents( $this->template_path . 'advanced-cache.php' );
|
||||
$mobile = is_rocket_generate_caching_mobile_files() ? '$1' : '';
|
||||
$content = preg_replace( "/'{{MOBILE_CACHE}}';(\X*)'{{\/MOBILE_CACHE}}';/", $mobile, $content );
|
||||
|
||||
$replacements = [
|
||||
'{{WP_ROCKET_PHP_VERSION}}' => rocket_get_constant( 'WP_ROCKET_PHP_VERSION' ),
|
||||
'{{WP_ROCKET_PATH}}' => rocket_get_constant( 'WP_ROCKET_PATH' ),
|
||||
'{{WP_ROCKET_CONFIG_PATH}}' => rocket_get_constant( 'WP_ROCKET_CONFIG_PATH' ),
|
||||
'{{WP_ROCKET_CACHE_PATH}}' => rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ),
|
||||
];
|
||||
|
||||
foreach ( $replacements as $key => $value ) {
|
||||
$content = str_replace( $key, $value, $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the content of advanced-cache.php file.
|
||||
*
|
||||
* @since 2.1
|
||||
*
|
||||
* @param string $content The content that will be printed in advanced-cache.php.
|
||||
*/
|
||||
return (string) apply_filters( 'rocket_advanced_cache_file', $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* This warning is displayed when the advanced-cache.php file isn't writeable
|
||||
*
|
||||
* @since 3.6 Moved to a method in AdvancedCache
|
||||
* @since 2.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice_permissions() {
|
||||
if ( ! $this->is_user_allowed() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This filter is documented in inc/functions/files.php.
|
||||
if ( ! (bool) apply_filters( 'rocket_generate_advanced_cache_file', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
$this->filesystem->is_writable( "{$this->content_dir}/advanced-cache.php" )
|
||||
||
|
||||
rocket_get_constant( 'WP_ROCKET_ADVANCED_CACHE' )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notice_name = 'rocket_warning_advanced_cache_permissions';
|
||||
|
||||
if (
|
||||
in_array(
|
||||
$notice_name,
|
||||
(array) get_user_meta( get_current_user_id(), 'rocket_boxes', true ),
|
||||
true
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $this->get_notice_message(),
|
||||
'dismiss_button' => $notice_name,
|
||||
'readonly_content' => $this->get_advanced_cache_content(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current user can see the notices
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_user_allowed() {
|
||||
return current_user_can( 'rocket_manage_options' ) && rocket_valid_key();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the message to display in the notice
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_notice_message() {
|
||||
return rocket_notice_writing_permissions( basename( $this->content_dir ) . '/advanced-cache.php' );
|
||||
}
|
||||
}
|
223
wp-content/plugins/wp-rocket/inc/Engine/Cache/Purge.php
Normal file
223
wp-content/plugins/wp-rocket/inc/Engine/Cache/Purge.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Cache;
|
||||
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
use WP_Term;
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* Cache Purge handling class
|
||||
*/
|
||||
class Purge {
|
||||
/**
|
||||
* Filesystem instance
|
||||
*
|
||||
* @var WP_Filesystem_Direct
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* Initialize the class
|
||||
*
|
||||
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
|
||||
*/
|
||||
public function __construct( $filesystem ) {
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges cache for the dates archives of a post
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @return void
|
||||
*/
|
||||
public function purge_dates_archives( $post ) {
|
||||
foreach ( $this->get_dates_archives( $post ) as $url ) {
|
||||
$this->purge_url( $url, true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge URL cache.
|
||||
*
|
||||
* @param string $url URL to be purged.
|
||||
* @param boolean $pagination Purge also pagination.
|
||||
* @return void
|
||||
*/
|
||||
private function purge_url( $url, $pagination = false ) {
|
||||
global $wp_rewrite;
|
||||
|
||||
$parsed_url = $this->parse_url( $url );
|
||||
|
||||
foreach ( _rocket_get_cache_dirs( $parsed_url['host'] ) as $dir ) {
|
||||
$path = $dir . $parsed_url['path'];
|
||||
|
||||
if ( ! $this->filesystem->exists( $path ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $this->get_iterator( $path ) as $item ) {
|
||||
if ( $item->isFile() ) {
|
||||
$this->filesystem->delete( $item->getPathname() );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $pagination ) {
|
||||
$this->maybe_remove_dir( $path . DIRECTORY_SEPARATOR . $wp_rewrite->pagination_base );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the dates archives URLs for the provided post
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @return array
|
||||
*/
|
||||
private function get_dates_archives( $post ) {
|
||||
$time = get_the_time( 'Y-m-d', $post );
|
||||
|
||||
if ( empty( $time ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$date = explode( '-', $time );
|
||||
$urls = [
|
||||
get_year_link( $date[0] ),
|
||||
get_month_link( $date[0], $date[1] ),
|
||||
get_day_link( $date[0], $date[1], $date[2] ),
|
||||
];
|
||||
|
||||
/**
|
||||
* Filter the list of dates URLs.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @param array $urls List of dates URLs.
|
||||
*/
|
||||
return (array) apply_filters( 'rocket_post_dates_urls', $urls );
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses URL and return the parts array
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param string $url URL to parse.
|
||||
* @return array
|
||||
*/
|
||||
private function parse_url( $url ) {
|
||||
$parsed_url = get_rocket_parse_url( $url );
|
||||
|
||||
/** This filter is documented in inc/front/htaccess.php */
|
||||
if ( apply_filters( 'rocket_url_no_dots', false ) ) {
|
||||
$parsed_url['host'] = str_replace( '.', '_', $parsed_url['host'] );
|
||||
}
|
||||
|
||||
return $parsed_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the iterator for the given path
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param string $path Absolute path.
|
||||
* @return DirectoryIterator|array
|
||||
*/
|
||||
private function get_iterator( $path ) {
|
||||
try {
|
||||
$iterator = new DirectoryIterator( $path );
|
||||
} catch ( Exception $e ) {
|
||||
// No action required, as logging not enabled.
|
||||
$iterator = [];
|
||||
}
|
||||
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively remove the provided directory and its content
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param string $dir Absolute path for the directory.
|
||||
* @return void
|
||||
*/
|
||||
private function maybe_remove_dir( $dir ) {
|
||||
if ( $this->filesystem->is_dir( $dir ) ) {
|
||||
rocket_rrmdir( $dir, [], $this->filesystem );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge all terms archives urls associated to a specific post.
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
*/
|
||||
public function purge_post_terms_urls( WP_Post $post ) {
|
||||
foreach ( $this->get_post_terms_urls( $post ) as $url ) {
|
||||
$this->purge_url( $url );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all terms archives urls associated to a specific post.
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
*
|
||||
* @return array $urls List of taxonomies URLs
|
||||
*/
|
||||
private function get_post_terms_urls( WP_Post $post ) {
|
||||
$urls = [];
|
||||
$taxonomies = get_object_taxonomies( get_post_type( $post->ID ), 'objects' );
|
||||
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
if ( ! $taxonomy->public || 'product_shipping_class' === $taxonomy->name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the terms related to post.
|
||||
$terms = get_the_terms( $post->ID, $taxonomy->name );
|
||||
|
||||
if ( empty( $terms ) || is_wp_error( $terms ) ) {
|
||||
continue;
|
||||
}
|
||||
foreach ( $terms as $term ) {
|
||||
$term_url = get_term_link( $term->slug, $taxonomy->name );
|
||||
if ( ! is_wp_error( $term_url ) ) {
|
||||
$urls[] = $term_url;
|
||||
}
|
||||
if ( ! is_taxonomy_hierarchical( $taxonomy->name ) ) {
|
||||
continue;
|
||||
}
|
||||
$ancestors = (array) get_ancestors( $term->term_id, $taxonomy->name );
|
||||
foreach ( $ancestors as $ancestor ) {
|
||||
$ancestor_object = get_term( $ancestor, $taxonomy->name );
|
||||
if ( ! $ancestor_object instanceof WP_Term ) {
|
||||
continue;
|
||||
}
|
||||
$ancestor_term_url = get_term_link( $ancestor_object->slug, $taxonomy->name );
|
||||
if ( ! is_wp_error( $ancestor_term_url ) ) {
|
||||
$urls[] = $ancestor_term_url;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Filter the list of taxonomies URLs
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @param array $urls List of taxonomies URLs
|
||||
*/
|
||||
return apply_filters( 'rocket_post_terms_urls', $urls );
|
||||
}
|
||||
}
|
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Cache;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
|
||||
/**
|
||||
* Subscriber for the cache purge actions
|
||||
*
|
||||
* @since 3.5
|
||||
*/
|
||||
class PurgeActionsSubscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* WP Rocket options instance
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Purge instance
|
||||
*
|
||||
* @var Purge
|
||||
*/
|
||||
private $purge;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Options_Data $options WP Rocket options instance.
|
||||
* @param Purge $purge Purge instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options, Purge $purge ) {
|
||||
$this->options = $options;
|
||||
$this->purge = $purge;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'profile_update' => 'purge_user_cache',
|
||||
'delete_user' => 'purge_user_cache',
|
||||
'create_term' => [ 'maybe_purge_cache_on_term_change', 10, 3 ],
|
||||
'edit_term' => [ 'maybe_purge_cache_on_term_change', 10, 3 ],
|
||||
'delete_term' => [ 'maybe_purge_cache_on_term_change', 10, 3 ],
|
||||
'after_rocket_clean_post' => [
|
||||
[ 'purge_dates_archives' ],
|
||||
[ 'purge_post_terms_urls' ],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges the cache of the corresponding user
|
||||
*
|
||||
* @since 3.5
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @return void
|
||||
*/
|
||||
public function purge_user_cache( $user_id ) {
|
||||
if ( ! $this->should_purge_user_cache() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_clean_user( $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges the cache when a public term is created|updated|deleted
|
||||
*
|
||||
* @since 3.5.5
|
||||
*
|
||||
* @param int $term_id Term ID.
|
||||
* @param int $tt_id Term taxonomy ID.
|
||||
* @param string $taxonomy Taxonomy slug.
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_purge_cache_on_term_change( $term_id, $tt_id, $taxonomy ) {
|
||||
if ( ! $this->is_taxonomy_public( $taxonomy ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_clean_domain();
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges cache for the dates archives of a post after cleaning the post
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @return void
|
||||
*/
|
||||
public function purge_dates_archives( $post ) {
|
||||
$this->purge->purge_dates_archives( $post );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge all terms archives urls associated to a specific post.
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @return void
|
||||
*/
|
||||
public function purge_post_terms_urls( $post ) {
|
||||
$this->purge->purge_post_terms_urls( $post );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given taxonomy is public
|
||||
*
|
||||
* @param string $name Taxonomy name.
|
||||
* @return bool
|
||||
*/
|
||||
private function is_taxonomy_public( $name ) {
|
||||
$taxonomy = get_taxonomy( $name );
|
||||
|
||||
if ( false === $taxonomy ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ( $taxonomy->public && $taxonomy->publicly_queryable );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user cache should be purged
|
||||
*
|
||||
* @since 3.5
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private function should_purge_user_cache() {
|
||||
if ( ! $this->options->get( 'cache_logged_user', 0 ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// This filter is documented in /inc/functions/files.php.
|
||||
return ! (bool) apply_filters( 'rocket_common_cache_logged_users', false );
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Cache;
|
||||
|
||||
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
|
||||
|
||||
/**
|
||||
* Service Provider for cache subscribers
|
||||
*
|
||||
* @since 3.5.5
|
||||
*/
|
||||
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 = [
|
||||
'advanced_cache',
|
||||
'wp_cache',
|
||||
'purge',
|
||||
'purge_actions_subscriber',
|
||||
'admin_cache_subscriber',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers the option array in the container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$filesystem = rocket_direct_filesystem();
|
||||
|
||||
$this->getContainer()->add( 'advanced_cache', 'WP_Rocket\Engine\Cache\AdvancedCache' )
|
||||
->withArgument( $this->getContainer()->get( 'template_path' ) . '/cache/' )
|
||||
->withArgument( $filesystem );
|
||||
$this->getContainer()->add( 'wp_cache', 'WP_Rocket\Engine\Cache\WPCache' )
|
||||
->withArgument( $filesystem );
|
||||
$this->getContainer()->add( 'purge', 'WP_Rocket\Engine\Cache\Purge' )
|
||||
->withArgument( $filesystem );
|
||||
$this->getContainer()->share( 'purge_actions_subscriber', 'WP_Rocket\Engine\Cache\PurgeActionsSubscriber' )
|
||||
->withArgument( $this->getContainer()->get( 'options' ) )
|
||||
->withArgument( $this->getContainer()->get( 'purge' ) );
|
||||
$this->getContainer()->share( 'admin_cache_subscriber', 'WP_Rocket\Engine\Cache\AdminSubscriber' )
|
||||
->withArgument( $this->getContainer()->get( 'advanced_cache' ) )
|
||||
->withArgument( $this->getContainer()->get( 'wp_cache' ) );
|
||||
}
|
||||
}
|
385
wp-content/plugins/wp-rocket/inc/Engine/Cache/WPCache.php
Normal file
385
wp-content/plugins/wp-rocket/inc/Engine/Cache/WPCache.php
Normal file
@@ -0,0 +1,385 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Cache;
|
||||
|
||||
use WP_Rocket\Engine\Activation\ActivationInterface;
|
||||
use WP_Rocket\Engine\Deactivation\DeactivationInterface;
|
||||
|
||||
class WPCache implements ActivationInterface, DeactivationInterface {
|
||||
/**
|
||||
* Filesystem instance.
|
||||
*
|
||||
* @var WP_Filesystem_Direct
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
|
||||
*/
|
||||
public function __construct( $filesystem ) {
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs these actions during the plugin activation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function activate() {
|
||||
add_action( 'rocket_activation', [ $this, 'update_wp_cache' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs these actions during the plugin deactivation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deactivate() {
|
||||
add_action( 'rocket_deactivation', [ $this, 'update_wp_cache' ] );
|
||||
add_filter( 'rocket_prevent_deactivation', [ $this, 'maybe_prevent_deactivation' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the WP_CACHE constant on (de)activation
|
||||
*
|
||||
* @since 3.6.3
|
||||
*
|
||||
* @param int $sites_number Number of WP Rocket config files found.
|
||||
* @return void
|
||||
*/
|
||||
public function update_wp_cache( $sites_number = 0 ) {
|
||||
if ( ! rocket_valid_key() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = true;
|
||||
|
||||
if ( 'rocket_deactivation' === current_filter() ) {
|
||||
if ( is_multisite() && 0 !== $sites_number ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = false;
|
||||
}
|
||||
|
||||
$this->set_wp_cache_constant( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the causes array on deactivation if needed
|
||||
*
|
||||
* @since 3.6.3
|
||||
*
|
||||
* @param array $causes Array of causes to pass to the notice.
|
||||
*/
|
||||
public function maybe_prevent_deactivation( $causes ) {
|
||||
if (
|
||||
$this->find_wpconfig_path()
|
||||
||
|
||||
// This filter is documented in inc/Engine/Cache/WPCache.php.
|
||||
! (bool) apply_filters( 'rocket_set_wp_cache_constant', true )
|
||||
) {
|
||||
return $causes;
|
||||
}
|
||||
|
||||
$causes[] = 'wpconfig';
|
||||
|
||||
return $causes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set WP_CACHE constant to true if needed
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_set_wp_cache() {
|
||||
if (
|
||||
rocket_get_constant( 'DOING_AJAX' )
|
||||
||
|
||||
rocket_get_constant( 'DOING_AUTOSAVE' )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( rocket_get_constant( 'WP_CACHE' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_wp_cache_constant( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the WP_CACHE constant in wp-config.php
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param bool $value The value to set for WP_CACHE constant.
|
||||
* @return void
|
||||
*/
|
||||
public function set_wp_cache_constant( $value ) {
|
||||
if ( ! $this->should_set_wp_cache_constant( $value ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config_file_path = $this->find_wpconfig_path();
|
||||
|
||||
if ( ! $config_file_path ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config_file_contents = $this->filesystem->get_contents( $config_file_path );
|
||||
$value = $value ? 'true' : 'false';
|
||||
|
||||
/**
|
||||
* Filter allow to change the value of WP_CACHE constant
|
||||
*
|
||||
* @since 2.1
|
||||
*
|
||||
* @param string $value The value of WP_CACHE constant.
|
||||
*/
|
||||
$value = apply_filters( 'set_rocket_wp_cache_define', $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
|
||||
$wp_cache_found = preg_match( '/^\s*define\(\s*\'WP_CACHE\'\s*,\s*(?<value>[^\s\)]*)\s*\)/m', $config_file_contents, $matches );
|
||||
|
||||
if (
|
||||
! empty( $matches['value'] )
|
||||
&&
|
||||
$matches['value'] === $value
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$constant = $this->get_wp_cache_content( $value );
|
||||
|
||||
if ( ! $wp_cache_found ) {
|
||||
$config_file_contents = preg_replace( '/(<\?php)/i', "<?php\r\n{$constant}\r\n", $config_file_contents );
|
||||
} elseif ( ! empty( $matches['value'] ) && $matches['value'] !== $value ) {
|
||||
$config_file_contents = preg_replace( '/^\s*define\(\s*\'WP_CACHE\'\s*,\s*([^\s\)]*)\s*\).+/m', $constant, $config_file_contents );
|
||||
}
|
||||
|
||||
$this->filesystem->put_contents( $config_file_path, $config_file_contents, rocket_get_filesystem_perms( 'file' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we should set the WP_CACHE constant
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param bool $value The value to set for WP_CACHE constant.
|
||||
* @return bool
|
||||
*/
|
||||
private function should_set_wp_cache_constant( $value ) {
|
||||
if ( ! $this->is_user_allowed() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
true === $value
|
||||
&&
|
||||
rocket_get_constant( 'WP_CACHE' )
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the writing of the WP_CACHE constant in wp-config.php
|
||||
*
|
||||
* @since 3.6.1
|
||||
* @param bool $set True to allow writing, false otherwise.
|
||||
*/
|
||||
return (bool) apply_filters( 'rocket_set_wp_cache_constant', true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to find the correct wp-config.php file, support one level up in file tree.
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return string|bool The path of wp-config.php file or false if not found.
|
||||
*/
|
||||
private function find_wpconfig_path() {
|
||||
/**
|
||||
* Filter the wp-config's filename.
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param string $filename The WP Config filename, without the extension.
|
||||
*/
|
||||
$config_file_name = apply_filters( 'rocket_wp_config_name', 'wp-config' );
|
||||
$abspath = rocket_get_constant( 'ABSPATH' );
|
||||
$config_file = "{$abspath}{$config_file_name}.php";
|
||||
|
||||
if ( $this->filesystem->is_writable( $config_file ) ) {
|
||||
return $config_file;
|
||||
}
|
||||
|
||||
$abspath_parent = dirname( $abspath ) . DIRECTORY_SEPARATOR;
|
||||
$config_file_alt = "{$abspath_parent}{$config_file_name}.php";
|
||||
|
||||
if (
|
||||
$this->filesystem->exists( $config_file_alt )
|
||||
&&
|
||||
$this->filesystem->is_writable( $config_file_alt )
|
||||
&&
|
||||
! $this->filesystem->exists( "{$abspath_parent}wp-settings.php" )
|
||||
) {
|
||||
return $config_file_alt;
|
||||
}
|
||||
|
||||
// No writable file found.
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This warning is displayed when the wp-config.php file isn't writable
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice_wp_config_permissions() {
|
||||
global $pagenow;
|
||||
|
||||
if (
|
||||
'plugins.php' === $pagenow
|
||||
||
|
||||
isset( $_GET['activate'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $this->is_user_allowed() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( rocket_get_constant( 'WP_CACHE' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This filter is documented in inc/Engine/Cache/WPCache.php.
|
||||
if ( ! (bool) apply_filters( 'rocket_set_wp_cache_constant', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->find_wpconfig_path() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notice_name = 'rocket_warning_wp_config_permissions';
|
||||
|
||||
if (
|
||||
in_array(
|
||||
$notice_name,
|
||||
(array) get_user_meta( get_current_user_id(), 'rocket_boxes', true ),
|
||||
true
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => rocket_notice_writing_permissions( 'wp-config.php' ),
|
||||
'dismiss_button' => $notice_name,
|
||||
'readonly_content' => $this->get_wp_cache_content(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current user can perform the action
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_user_allowed() {
|
||||
return current_user_can( 'rocket_manage_options' ) && rocket_valid_key();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the content to add to the wp-config.php file
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param string $value Value for the WP_CACHE constant.
|
||||
* @return string
|
||||
*/
|
||||
private function get_wp_cache_content( $value = 'true' ) {
|
||||
$plugin_name = rocket_get_constant( 'WP_ROCKET_PLUGIN_NAME' );
|
||||
|
||||
return "define( 'WP_CACHE', {$value} ); // Added by {$plugin_name}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Site Health check for the WP_CACHE constant value
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @param array $tests An array of tests to perform.
|
||||
* @return array
|
||||
*/
|
||||
public function add_wp_cache_status_test( $tests ) {
|
||||
$tests['direct']['wp_cache_status'] = [
|
||||
'label' => __( 'WP_CACHE value', 'rocket' ),
|
||||
'test' => [ $this, 'check_wp_cache_value' ],
|
||||
];
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the WP_CACHE constant value and return the result for Site Health
|
||||
*
|
||||
* @since 3.6.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function check_wp_cache_value() {
|
||||
$result = [
|
||||
'badge' => [
|
||||
'label' => __( 'Cache', 'rocket' ),
|
||||
],
|
||||
'description' => sprintf(
|
||||
'<p>%s</p>',
|
||||
__( 'The WP_CACHE constant needs to be set to true for WP Rocket cache to work properly', 'rocket' )
|
||||
),
|
||||
'actions' => '',
|
||||
'test' => 'wp_cache_status',
|
||||
];
|
||||
|
||||
$value = rocket_get_constant( 'WP_CACHE' );
|
||||
|
||||
if ( true === $value ) {
|
||||
$result['label'] = __( 'WP_CACHE is set to true', 'rocket' );
|
||||
$result['status'] = 'good';
|
||||
$result['badge']['color'] = 'green';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( null === $value ) {
|
||||
$result['label'] = __( 'WP_CACHE is not set', 'rocket' );
|
||||
$result['status'] = 'critical';
|
||||
$result['badge']['color'] = 'red';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( false === $value ) {
|
||||
$result['label'] = __( 'WP_CACHE is set to false', 'rocket' );
|
||||
$result['status'] = 'critical';
|
||||
$result['badge']['color'] = 'red';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user