Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #5848: Elementor templates clearing cache in full #5956

Open
wants to merge 51 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9d0d67d
Added check against full cache clearing
jeawhanlee May 31, 2023
071569a
Added new filter to exclude specific post types from cache clearing
jeawhanlee May 31, 2023
b3b2acf
Exclude elementor library post type from being cleared
jeawhanlee May 31, 2023
aeb2b23
Clear related post cache
jeawhanlee May 31, 2023
207b6da
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
jeawhanlee Jun 13, 2023
5b22e7a
Updated tests
jeawhanlee Jun 13, 2023
3ef24ea
Fixed failing test
jeawhanlee Jun 14, 2023
a7df8a8
Fixed test
jeawhanlee Jun 14, 2023
fdd5818
Updated mocked class
jeawhanlee Jun 15, 2023
9a490e6
Added tests
jeawhanlee Jun 15, 2023
7c09904
Clear used css of related posts
jeawhanlee Jul 13, 2023
a148609
Updated service provider
jeawhanlee Jul 13, 2023
3b0fd69
Updated tests
jeawhanlee Jul 13, 2023
7180a24
Updated service provider
jeawhanlee Jul 13, 2023
e3a2b41
Merged develop into enhancement/5848-elementor-templates-clearing-cac…
jeawhanlee Jul 13, 2023
e1063c7
Revert changes
jeawhanlee Jul 13, 2023
cae61f0
Updated filter
jeawhanlee Jul 14, 2023
0f904fb
Updated filter callback and apply CR suggestions
jeawhanlee Jul 14, 2023
e842f30
Updated tests
jeawhanlee Jul 14, 2023
8d21f15
Fixed failing test
jeawhanlee Jul 14, 2023
014c173
Apply suggestions from code review
remyperona Jul 14, 2023
457d4c5
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Jul 17, 2023
e67baac
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Jul 18, 2023
44fa808
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Jul 19, 2023
33a6c23
Added tests
CrochetFeve0251 Sep 8, 2023
e4b379c
Added logic
CrochetFeve0251 Sep 8, 2023
992f52d
Added changes in the bootstrap
CrochetFeve0251 Sep 8, 2023
d0fd6f7
Added hooks
CrochetFeve0251 Sep 8, 2023
f3f4984
Added new event to match the right ones
CrochetFeve0251 Sep 8, 2023
11a8567
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
CrochetFeve0251 Sep 8, 2023
619dad3
Fixed tests
CrochetFeve0251 Sep 8, 2023
3c450cb
Added fix for the CR
CrochetFeve0251 Sep 13, 2023
e245ed2
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Sep 18, 2023
3a27e92
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Sep 18, 2023
0747f76
Added new version to match the AC better
CrochetFeve0251 Sep 21, 2023
9b4976d
Added tests
CrochetFeve0251 Sep 21, 2023
27d1795
Fixed tests
CrochetFeve0251 Sep 22, 2023
1c8a5c6
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Sep 26, 2023
cf56164
Added fix to clear also used css inside the db
CrochetFeve0251 Sep 26, 2023
ed871ea
Fixed tests
CrochetFeve0251 Sep 26, 2023
865eb18
Fixed tests
CrochetFeve0251 Sep 26, 2023
a3769ab
Update inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
CrochetFeve0251 Sep 29, 2023
ec3b02f
Update inc/ThirdParty/Plugins/PageBuilder/Elementor.php
CrochetFeve0251 Sep 29, 2023
5fead12
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Oct 4, 2023
fcc408a
Added new logic for new AC
CrochetFeve0251 Oct 11, 2023
45b4ab3
Fixed PHPCS
CrochetFeve0251 Oct 11, 2023
a5db63b
Added message changing on whenever RUCSS is activated
CrochetFeve0251 Oct 11, 2023
aa0050e
Merge remote-tracking branch 'origin/enhancement/5848-elementor-templ…
CrochetFeve0251 Oct 11, 2023
9fea433
Fixed tests
CrochetFeve0251 Oct 11, 2023
9d79288
Fixed code standards
CrochetFeve0251 Oct 11, 2023
74b6e2b
Merge branch 'develop' into enhancement/5848-elementor-templates-clea…
vmanthos Oct 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions inc/Engine/Optimization/RUCSS/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,7 @@ public function truncate_used_css() {
* @return void
*/
private function delete_used_css_rows() {
$this->used_css->delete_all_used_css();

if ( 0 < $this->used_css->get_not_completed_count() ) {
$this->database->remove_all_completed_rows();
} else {
$this->database->truncate_used_css_table();
}
$this->used_css->delete_used_css_rows();

/**
* Fires after the used CSS has been cleaned in the database
Expand Down
30 changes: 29 additions & 1 deletion inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use WP_Rocket\Engine\Optimization\CSSTrait;
use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\DataManager;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Engine\Optimization\RUCSS\Admin\Database;
use WP_Rocket\Engine\Optimization\RUCSS\Database\Queries\UsedCSS as UsedCSS_Query;
use WP_Rocket\Engine\Optimization\RUCSS\Frontend\APIClient;
use WP_Rocket\Logger\Logger;
Expand Down Expand Up @@ -59,6 +60,14 @@ class UsedCSS {
*/
private $filesystem;

/**
* Database instance
*
* @var Database
*/
private $database;


/**
* External exclusions list, can be urls or attributes.
*
Expand Down Expand Up @@ -89,21 +98,24 @@ class UsedCSS {
* @param QueueInterface $queue Queue instance.
* @param DataManager $data_manager DataManager instance.
* @param Filesystem $filesystem Filesystem instance.
* @param Database $database Database instance.
*/
public function __construct(
Options_Data $options,
UsedCSS_Query $used_css_query,
APIClient $api,
QueueInterface $queue,
DataManager $data_manager,
Filesystem $filesystem
Filesystem $filesystem,
Database $database
) {
$this->options = $options;
$this->used_css_query = $used_css_query;
$this->api = $api;
$this->queue = $queue;
$this->data_manager = $data_manager;
$this->filesystem = $filesystem;
$this->database = $database;
}

/**
Expand Down Expand Up @@ -1100,4 +1112,20 @@ static function ( $item ) {
public function has_one_completed_row_at_least() {
return $this->used_css_query->get_completed_count() > 0;
}

/**
* Delete RUCSS rows.
*
* @return void
*/
public function delete_used_css_rows() {
$this->delete_all_used_css();

if ( 0 < $this->get_not_completed_count() ) {
$this->database->remove_all_completed_rows();
return;
}

$this->database->truncate_used_css_table();
}
}
4 changes: 3 additions & 1 deletion inc/Engine/Optimization/RUCSS/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ServiceProvider extends AbstractServiceProvider {
'rucss_frontend_api_client',
'rucss_used_css',
'rucss_used_css_query',
'rucss_used_css_controller',
'rucss_frontend_subscriber',
'rucss_queue',
'rucss_filesystem',
Expand Down Expand Up @@ -73,7 +74,8 @@ public function register() {
->addArgument( $this->getContainer()->get( 'rucss_frontend_api_client' ) )
->addArgument( $this->getContainer()->get( 'rucss_queue' ) )
->addArgument( $this->getContainer()->get( 'dynamic_lists_defaultlists_data_manager' ) )
->addArgument( $this->getContainer()->get( 'rucss_filesystem' ) );
->addArgument( $this->getContainer()->get( 'rucss_filesystem' ) )
->addArgument( $this->getContainer()->get( 'rucss_database' ) );

$this->getContainer()->share( 'rucss_option_subscriber', OptionSubscriber::class )
->addArgument( $this->getContainer()->get( 'rucss_settings' ) );
Expand Down
207 changes: 206 additions & 1 deletion inc/ThirdParty/Plugins/PageBuilder/Elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
namespace WP_Rocket\ThirdParty\Plugins\PageBuilder;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\Ajax\AjaxHandler;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Engine\Optimization\DelayJS\HTML;
use WP_Rocket\Engine\Optimization\RUCSS\Controller\UsedCSS;

/**
* Compatibility file for Elementor plugin
*/
class Elementor implements Subscriber_Interface {

/**
* WP Rocket options.
*
Expand All @@ -32,17 +35,35 @@
*/
private $delayjs_html;

/**
* UsedCSS instance
*
* @var UsedCSS
*/
private $used_css;

/**
* Handle basic Ajax operations.
*
* @var AjaxHandler
*/
protected $ajax_handler;

/**
* Constructor
*
* @param Options_Data $options WP Rocket options.
* @param \WP_Filesystem_Direct $filesystem The Filesystem object.
* @param HTML $delayjs_html DelayJS HTML class.
* @param UsedCSS $used_css UsedCSS class.
* @param AjaxHandler $ajax_handler Handle basic Ajax operations.
*/
public function __construct( Options_Data $options, $filesystem, HTML $delayjs_html ) {
public function __construct( Options_Data $options, $filesystem, HTML $delayjs_html, UsedCSS $used_css, AjaxHandler $ajax_handler ) {
$this->options = $options;
$this->filesystem = $filesystem;
$this->delayjs_html = $delayjs_html;
$this->used_css = $used_css;
$this->ajax_handler = $ajax_handler;
}

/**
Expand All @@ -69,6 +90,12 @@
'rocket_skip_admin_bar_cache_purge_option' => [ 'skip_admin_bar_option', 1, 2 ],
'rocket_submitbox_options_post_types' => 'remove_rocket_option',
'rocket_skip_admin_bar_clear_used_css_option' => [ 'skip_admin_bar_option', 1, 2 ],
'rocket_pre_clean_post' => [ 'exclude_post_type_cache_clearing', 10, 2 ],
'elementor/editor/after_save' => 'clear_related_post_cache',
'admin_notices' => 'maybe_clear_cache_change_notice',
'update_post_metadata' => [ 'setup_transient', 10, 5 ],
'rocket_notice_args' => 'add_clear_action',
'admin_post_rocket_elementor_clear_usedcss' => 'clear_action',
];
}

Expand Down Expand Up @@ -205,4 +232,182 @@

return $should_skip;
}

/**
* Exclude elementor library post type from cache clearing.
*
* @param mixed $clear_post A preemptive return value. Default null.
* @param Post $post Post Object.
* @return boolean
*/
public function exclude_post_type_cache_clearing( $clear_post, $post ) {
if ( 'elementor_library' === $post->post_type ) {
return true;
}

return $clear_post;
}

/**
* Clear cache of related posts.
*
* @param integer $post_id Post ID.
* @return void
*/
public function clear_related_post_cache( int $post_id ) {
global $wpdb;

$template_id = '%' . $wpdb->esc_like( $post_id ) . '%';

// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id FROM `$wpdb->postmeta` WHERE `meta_key` = %s AND `meta_value` LIKE %s",
[ '_elementor_data', $template_id ]
)
);

if ( ! $results ) {
return;
}

foreach ( $results as $result ) {
if ( 'publish' !== get_post_status( $result->post_id ) ) {
continue;
}

rocket_clean_post( $result->post_id );

if ( ! $this->options->get( 'remove_unused_css', 0 ) ) {
continue;
}

$url = get_permalink( $result->post_id );
$this->used_css->delete_used_css( $url );
}
}

/**
* Display a notice when clear is needed.
*
* @return void
*/
public function maybe_clear_cache_change_notice() {

$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );

if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
return;
}

if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}

$notice = get_transient( 'wpr_elementor_need_purge' );
if ( ! $notice ) {
return;
}

$message = $this->options->get( 'remove_unused_css', false ) ?
// translators: %1$s = <strong>, %2$s = </strong>, %3$s = <a>, %4$s = </a>.
__( '%1$sWP Rocket:%2$s Your Elementor template was updated. Clear the Used CSS if the layout, design or CSS styles were changed.', 'rocket' )
:
// translators: %1$s = <strong>, %2$s = </strong>, %3$s = <a>, %4$s = </a>.
__( '%1$sWP Rocket:%2$s Your Elementor template was updated. Clear the cache if the display conditions were changed.', 'rocket' );

$args = [
'status' => 'warning',
'dismissible' => '',
'dismiss_button' => __FUNCTION__,
'message' => sprintf(
$message,
'<strong>',
'</strong>',
'</a>'
),
'action' => 'elementor_clear_usedcss',
];

rocket_notice_html( $args );
}

/**
* Set up the transient when needed.
*
* @param bool $check Check the filed.
* @param int $object_id Id from the object.
* @param string $meta_key Key from the meta.
* @param string $meta_value Current value from the meta.
* @param string $prev_value Previous value from the meta.
* @return void
*/
public function setup_transient( $check, $object_id, $meta_key, $meta_value, $prev_value ) {

Check warning on line 345 in inc/ThirdParty/Plugins/PageBuilder/Elementor.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/ThirdParty/Plugins/PageBuilder/Elementor.php#L345

Avoid unused parameters such as '$check'.
if ( '_elementor_conditions' !== $meta_key || $meta_value === $prev_value ) {
return;
}
set_transient( 'wpr_elementor_need_purge', true );

$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
$boxes = array_filter(
$boxes,
function ( $box ) {
return 'maybe_clear_cache_change_notice' !== $box;
}
);
update_user_meta( get_current_user_id(), 'rocket_boxes', $boxes );
}

/**
* Add an action to clear the RUCSS.
*
* @param array $args Arguments to pass to the view.
* @return array
*/
public function add_clear_action( $args ) {

if ( ! key_exists( 'action', $args ) || 'elementor_clear_usedcss' !== $args['action'] ) {
return $args;
}

$params = [
'action' => 'rocket_elementor_clear_usedcss',
];

if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
$referer_url = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
$params['_wp_http_referer'] = rawurlencode( $referer_url );
}

$message = $this->options->get( 'remove_unused_css', false ) ? __( 'Clear Used CSS', 'rocket' ) : __( 'Clear cache', 'rocket' );

$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), $params['action'] ) ) . '">' . $message . '</a>';

return $args;
}

/**
* Clear the cache and RUCSS.
*
* @return void
*/
public function clear_action() {

if ( ! $this->ajax_handler->validate_referer( 'rocket_elementor_clear_usedcss', 'rocket_remove_unused_css' ) ) {
wp_nonce_ays( '' );
return;
}

if ( $this->options->get( 'remove_unused_css', false ) ) {
$this->used_css->delete_used_css_rows();
}

rocket_clean_domain();

$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
$boxes[] = 'maybe_clear_cache_change_notice';
update_user_meta( get_current_user_id(), 'rocket_boxes', $boxes );

$this->ajax_handler->redirect();
}
}
5 changes: 5 additions & 0 deletions inc/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace WP_Rocket\ThirdParty;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Common\Ajax\AjaxHandler;
use WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp\EWWW_Subscriber;
use WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp\Imagify_Subscriber;
use WP_Rocket\Subscriber\Third_Party\Plugins\Images\Webp\Optimus_Subscriber;
Expand Down Expand Up @@ -113,6 +114,8 @@ class ServiceProvider extends AbstractServiceProvider {
public function register() {
$options = $this->getContainer()->get( 'options' );

$this->getContainer()->add( 'ajax_handler', AjaxHandler::class );

$this->getContainer()
->share( 'mobile_subscriber', Mobile_Subscriber::class )
->addTag( 'common_subscriber' );
Expand All @@ -121,6 +124,8 @@ public function register() {
->addArgument( $options )
->addArgument( rocket_direct_filesystem() )
->addArgument( $this->getContainer()->get( 'delay_js_html' ) )
->addArgument( $this->getContainer()->get( 'rucss_used_css_controller' ) )
->addArgument( $this->getContainer()->get( 'ajax_handler' ) )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'woocommerce_subscriber', WooCommerceSubscriber::class )
Expand Down
12 changes: 12 additions & 0 deletions inc/common/purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ function rocket_clean_post( $post_id, $post = null ) {
return false;
}

/**
* Filters the preemptive return value of clean post
*
* @param mixed $clear_post $clear_post A preemptive return value. Default null.
* @param $post Post Object.
*/
$pre = apply_filters( 'rocket_pre_clean_post', null, $post );

if ( null !== $pre ) {
return $pre;
}

// No purge for specific conditions.
if ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status || empty( $post->post_type ) || 'nav_menu_item' === $post->post_type || 'attachment' === $post->post_type ) {
return false;
Expand Down
Loading
Loading