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

Divi compatibility: show admin notice to clear the used CSS when a template is updated #6074

Merged
merged 33 commits into from Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
271671f
change the ingredient checkbox id attribute to be unique
engahmeds3ed May 2, 2023
5d557ee
Merge branch 'develop' into enhancement/5837-divi_save_templates_clea…
engahmeds3ed Jul 26, 2023
852b4da
add the query and basic structure
engahmeds3ed Jul 28, 2023
ac4d038
adjust the notice a bit
engahmeds3ed Jul 28, 2023
8e0b489
add the message and fix phpcs
engahmeds3ed Jul 31, 2023
050f614
fix the ordering or the code
engahmeds3ed Jul 31, 2023
8dcc771
Adds missing licence-data.php file
engahmeds3ed Jul 31, 2023
7925e58
remove not used dependency
engahmeds3ed Jul 31, 2023
9035e14
Merge remote-tracking branch 'origin/enhancement/5837-divi_save_templ…
engahmeds3ed Jul 31, 2023
570f5d5
start fixing code review
engahmeds3ed Jul 31, 2023
830079d
adjusting the code a bit
engahmeds3ed Jul 31, 2023
0d0e09c
add integration test for handle_save_template
engahmeds3ed Aug 1, 2023
edc6d6d
the unit test
engahmeds3ed Aug 1, 2023
013d54e
add handleDiviAdminNotice unit test
engahmeds3ed Aug 1, 2023
955a7cf
final tests
engahmeds3ed Aug 1, 2023
75f187b
fix code review comments in tests
engahmeds3ed Aug 1, 2023
797ed8e
remove notice when enabling RUCSS
engahmeds3ed Aug 2, 2023
7c325ce
fix phpcs
engahmeds3ed Aug 2, 2023
10fdf7a
make it warning
engahmeds3ed Aug 2, 2023
b349458
add the button to the notice
engahmeds3ed Aug 2, 2023
868eea3
adjust the notice button structure
engahmeds3ed Aug 2, 2023
1108278
check if we have at least one completed row in the database before cl…
engahmeds3ed Aug 2, 2023
92f067b
start fixing tests
engahmeds3ed Aug 2, 2023
71408c9
handle save changes button
engahmeds3ed Aug 4, 2023
ce62a79
delete transients with uninstall
engahmeds3ed Aug 4, 2023
27b7058
change ajax priority
engahmeds3ed Aug 4, 2023
2f65443
Merge branch 'develop' into enhancement/5837-divi_save_templates_clea…
engahmeds3ed Aug 4, 2023
a8e4aeb
Merge branch 'develop' into enhancement/5837-divi_save_templates_clea…
vmanthos Aug 7, 2023
65fed7d
fix unit tests
engahmeds3ed Aug 10, 2023
525f128
Merge remote-tracking branch 'origin/enhancement/5837-divi_save_templ…
engahmeds3ed Aug 10, 2023
c565221
fix integratio tests
engahmeds3ed Aug 10, 2023
3fae548
fix admin test
engahmeds3ed Aug 10, 2023
ad1d641
adjust the notice html
engahmeds3ed Aug 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
Expand Up @@ -1068,4 +1068,13 @@ static function ( $item ) {
$items_array
);
}

/**
* Check if database has at least one completed row.
*
* @return bool
*/
public function has_one_completed_row_at_least() {
return $this->used_css_query->get_completed_count() > 0;
}
}
18 changes: 18 additions & 0 deletions inc/Engine/Optimization/RUCSS/Database/Queries/UsedCSS.php
Expand Up @@ -404,6 +404,24 @@ public function get_not_completed_count() {
);
}

/**
* Get the count of completed rows.
*
* @return int
*/
public function get_completed_count() {
if ( ! self::$table_exists && ! $this->table_exists() ) {
return 0;
}

return $this->query(
[
'count' => true,
'status' => 'completed',
]
);
}

/**
* Get all failed rows.
*
Expand Down
1 change: 1 addition & 0 deletions inc/Engine/Optimization/RUCSS/ServiceProvider.php
Expand Up @@ -41,6 +41,7 @@ class ServiceProvider extends AbstractServiceProvider {
'rucss_queue',
'rucss_filesystem',
'rucss_cron_subscriber',
'rucss_used_css_controller',
];

/**
Expand Down
1 change: 1 addition & 0 deletions inc/Engine/WPRocketUninstall.php
Expand Up @@ -72,6 +72,7 @@ class WPRocketUninstall {
'rocket_domain_changed',
'wp_rocket_rucss_errors_count',
'wpr_dynamic_lists_incompatible_plugins',
'rocket_divi_notice',
];

/**
Expand Down
176 changes: 175 additions & 1 deletion inc/ThirdParty/Themes/Divi.php
Expand Up @@ -4,6 +4,7 @@

use WP_Rocket\Admin\{Options, Options_Data};
use WP_Rocket\Engine\Optimization\DelayJS\HTML;
use WP_Rocket\Engine\Optimization\RUCSS\Controller\UsedCSS;

class Divi extends ThirdpartyTheme {
/**
Expand Down Expand Up @@ -34,17 +35,26 @@ class Divi extends ThirdpartyTheme {
*/
private $delayjs_html;

/**
* Used CSS controller instance.
*
* @var UsedCSS
*/
private $used_css;

/**
* Instantiate the class
*
* @param Options $options_api Options API instance.
* @param Options_Data $options WP Rocket options instance.
* @param HTML $delayjs_html DelayJS HTML class.
* @param UsedCSS $used_css Used CSS controller instance.
*/
public function __construct( Options $options_api, Options_Data $options, HTML $delayjs_html ) {
public function __construct( Options $options_api, Options_Data $options, HTML $delayjs_html, UsedCSS $used_css ) {
$this->options_api = $options_api;
$this->options = $options;
$this->delayjs_html = $delayjs_html;
$this->used_css = $used_css;
}

/**
Expand All @@ -70,6 +80,14 @@ public static function get_subscribed_events() {

$events['wp'] = 'disable_dynamic_css_on_rucss';
$events['after_setup_theme'] = 'remove_assets_generated';
$events['et_save_post'] = 'handle_save_template';
$events['admin_notices'] = 'handle_divi_admin_notice';

$slug = rocket_get_constant( 'WP_ROCKET_SLUG', 'wp_rocket_settings' );
$events[ 'update_option_' . $slug ] = [ 'remove_transient_when_enabling_rucss_option', 10, 2 ];

$events['rocket_after_clean_used_css'] = 'clear_divi_notice';
$events['wp_ajax_et_theme_builder_api_save'] = [ 'show_rucss_notice_with_save_changes', 9 ];

return $events;
}
Expand Down Expand Up @@ -214,4 +232,160 @@ public function disable_dynamic_css_on_rucss() {
public function remove_assets_generated() {
remove_all_actions( 'et_dynamic_late_assets_generated' );
}

/**
* Get layout IDs for a template.
*
* @param int $template_post_id Template post ID.
*
* @return array
*/
private function get_layout_ids( $template_post_id ) {
$allowed_post_types = [
'et_header_layout',
'et_footer_layout',
'et_body_layout',
];
$current_post_type = get_post_type( $template_post_id );

if ( ! in_array( $current_post_type, $allowed_post_types, true ) ) {
return [];
}

global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery
return (array) $wpdb->get_col(
$wpdb->prepare(
'SELECT post_id from ' . $wpdb->postmeta . ' WHERE meta_key = %s AND meta_value = %d',
'_' . $current_post_type . '_id',
$template_post_id
)
);
}

/**
* Is allowed for RUCSS notice functionality when saving templates.
*
* @return bool
*/
private function is_allowed_for_rucss() {
return $this->options->get( 'remove_unused_css', 0 )
&&
current_user_can( 'rocket_manage_options' );
}

/**
* Save template handler.
*
* @param int $template_post_id Template post ID.
*
* @return void
*/
public function handle_save_template( $template_post_id ) {
/**
* Filters Bypassing saving template functionality.
*
* @param bool $bypass Bypass save template functionality.
* @param int $template_post_id Currently saved template post id.
*/
if ( apply_filters( 'rocket_divi_bypass_save_template', false, $template_post_id ) ) {
return;
}

// If the transient is there, we don't need to do the check again as the admin notice will be there already.
if ( false !== get_transient( 'rocket_divi_notice' ) ) {
return;
}

$layout_post_ids = $this->get_layout_ids( $template_post_id );

if ( empty( $layout_post_ids ) ) {
return;
}

foreach ( $layout_post_ids as $layout_post_id ) {
if ( 'publish' !== get_post_status( $layout_post_id ) ) {
continue;
}

set_transient( 'rocket_divi_notice', true );
return;
}
}

/**
* Admin notices handler.
*
* @return void
*/
public function handle_divi_admin_notice() {
engahmeds3ed marked this conversation as resolved.
Show resolved Hide resolved
if ( ! $this->is_allowed_for_rucss() ) {
return;
}

$notice = get_transient( 'rocket_divi_notice' );

if ( ! $notice ) {
return;
}

rocket_notice_html(
[
'status' => 'warning',
'dismissible' => '',
'dismiss_button' => 'rocket_divi_notice',
'action' => 'clear_used_css',
'message' =>
sprintf( '%1$sWP Rocket:%2$s ', '<strong>', '</strong>' ) . // Splitting it because I think the plugin name is not a translatable string.
esc_html__( 'Your Divi template was updated. Clear the Used CSS if the layout, design or CSS styles were changed.', 'rocket' ),
]
);
}

/**
* Clear divi notice.
*
* @return void
*/
public function clear_divi_notice() {
delete_transient( 'rocket_divi_notice' );
}

/**
* Remove notice transient when enabling RUCSS.
*
* @param array $old_value An array of submitted values for the settings.
* @param array $new_value An array of previous values for the settings.
*
* @return void
*/
public function remove_transient_when_enabling_rucss_option( $old_value, $new_value ) {
if ( ! isset( $new_value['remove_unused_css'], $old_value['remove_unused_css'] ) ) {
return;
}

if ( $new_value['remove_unused_css'] === $old_value['remove_unused_css'] ) {
return;
}

if ( ! $new_value['remove_unused_css'] ) {
return;
}

if ( $this->used_css->has_one_completed_row_at_least() ) {
return;
}

$this->clear_divi_notice();
}

/**
* Set the transient when save changes button is clicked.
*
* @return void
*/
public function show_rucss_notice_with_save_changes() {
set_transient( 'rocket_divi_notice', true );
}
}
1 change: 1 addition & 0 deletions inc/ThirdParty/Themes/ServiceProvider.php
Expand Up @@ -49,6 +49,7 @@ public function register() {
->addArgument( $this->getContainer()->get( 'options_api' ) )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'delay_js_html' ) )
->addArgument( $this->getContainer()->get( 'rucss_used_css_controller' ) )
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'flatsome', Flatsome::class )
Expand Down
11 changes: 11 additions & 0 deletions inc/admin/ui/notices.php
Expand Up @@ -670,6 +670,17 @@ function rocket_notice_html( $args ) {
case 'clear_cache':
$args['action'] = '<a class="wp-core-ui button" href="' . wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=all' ), 'purge_cache_all' ) . '">' . __( 'Clear cache', 'rocket' ) . '</a>';
break;
case 'clear_used_css':
$params = [
'action' => 'rocket_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 );
}
$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), $params['action'] ) ) . '">' . __( 'Clear Used CSS', 'rocket' ) . '</a>';
break;
case 'stop_preload':
$args['action'] = '<a class="wp-core-ui button" href="' . wp_nonce_url( admin_url( 'admin-post.php?action=rocket_stop_preload&type=all' ), 'rocket_stop_preload' ) . '">' . __( 'Stop Preload', 'rocket' ) . '</a>';
break;
Expand Down
@@ -0,0 +1,73 @@
<?php

return [
'vfs_dir' => 'wp-content/themes/',

'test_data' => [
'bailoutWhenRUCSSDisabled' => [
'config' => [
'rucss_option' => false,
],
'expected' => [
'notice_show' => false,
],
],

'bailoutWhenUserDoesnotHaveCapability' => [
'config' => [
'rucss_option' => true,
'capability' => false,
],
'expected' => [
'notice_show' => false,
],
],

'bailoutWhenTransientIsNotThere' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'transient_return' => false,
],
'expected' => [
'notice_show' => false,
],
],

'success' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'transient_return' => true,
],
'expected' => [
'notice_show' => true,
'notice_details' => [
'status' => 'warning',
'dismissible' => '',
'dismiss_button' => 'rocket_divi_notice',
'action' => 'clear_used_css',
'message' =>
'<strong>WP Rocket:</strong> Your Divi template was updated. Clear the Used CSS if the layout, design or CSS styles were changed.'
],
'notice_html' => <<<HTML
<div class="notice notice-warning ">
<p>
<strong>WP Rocket:</strong>
Your Divi template was updated. Clear the Used CSS if the layout, design or CSS styles were changed.
</p>
<p>
<a class="wp-core-ui button" href="http://example.org/wp-admin/admin-post.php?_wpnonce=12345&action=rocket_clear_usedcss&_wp_http_referer=%2Fwp-admin%2Foptions-general.php">
Clear Used CSS
</a>
<a class="rocket-dismiss " href="http://example.org/wp-admin/admin-post.php?action=rocket_ignore&amp;box=rocket_divi_notice&amp;_wpnonce=12345">
Dismiss this notice
</a>
</p>
</div>
HTML
],
],
],

];