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 16 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
130 changes: 130 additions & 0 deletions inc/ThirdParty/Themes/Divi.php
Expand Up @@ -70,6 +70,10 @@ 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';

$events['rocket_after_clean_used_css'] = 'clear_divi_notice';

return $events;
}
Expand Down Expand Up @@ -214,4 +218,130 @@ 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 ) {
if ( ! $this->is_allowed_for_rucss() ) {
return;
}

/**
* 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;
}

$used_on = get_post_meta( $layout_post_id, '_et_use_on' );
if ( empty( $used_on ) ) {
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' => 'info',
'dismiss_button' => 'rocket_divi_notice',
'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' );
}
}
@@ -0,0 +1,68 @@
<?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' => 'info',
'dismiss_button' => 'rocket_divi_notice',
'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-info is-dismissible">
<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="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
],
],
],

];
154 changes: 154 additions & 0 deletions tests/Fixtures/inc/ThirdParty/Themes/Divi/handleSaveTemplate.php
@@ -0,0 +1,154 @@
<?php

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

'test_data' => [
'bailoutWhenRUCSSDisabled' => [
'config' => [
'rucss_option' => false,
'template_post' => [
'post_type' => 'et_header_layout',
]
],
'expected' => [
'transient_set' => false,
],
],

'bailoutWhenUserDoesnotHaveCapability' => [
'config' => [
'rucss_option' => true,
'capability' => false,
'template_post' => [
'post_type' => 'et_header_layout',
]
],
'expected' => [
'transient_set' => false,
],
],

'bailoutWhenFilterReturnsTrue' => [
'config' => [
'rucss_option' => true,
'capability' => false,
'filter_return' => true,
'template_post' => [
'post_type' => 'et_header_layout',
]
],
'expected' => [
'transient_set' => false,
],
],

'bailoutWhenTransientIsThere' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'filter_return' => false,
'transient_return' => true,
'template_post' => [
'post_type' => 'et_header_layout',
]
],
'expected' => [
'transient_set' => true,
],
],

'bailoutWhenAnotherPostType' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'filter_return' => false,
'transient_return' => false,
'template_post' => [
'post_type' => 'test',
]
],
'expected' => [
'transient_set' => false,
],
],

'bailoutWhenPostStatusNotPublish' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'filter_return' => false,
'transient_return' => false,
'template_post' => [
'post_type' => 'et_header_layout',
'post_status' => 'trash',
]
],
'expected' => [
'transient_set' => false,
],
],

'bailoutWhenNoLayoutInDB' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'filter_return' => false,
'transient_return' => false,
'template_post' => [
'post_type' => 'et_header_layout',
]
],
'expected' => [
'transient_set' => false,
],
],

'bailoutWhenLayoutIsnotUsedByPages' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'filter_return' => false,
'transient_return' => false,
'template_post' => [
'post_type' => 'et_header_layout',
'post_status' => 'publish',
],
'layout_post' => [
'post_type' => 'et_template',
'post_status' => 'publish',
],
],
'expected' => [
'transient_set' => false,
],
],

'success' => [
'config' => [
'rucss_option' => true,
'capability' => true,
'filter_return' => false,
'transient_return' => false,
'template_post' => [
'post_type' => 'et_header_layout',
'post_status' => 'publish',
],
'layout_post' => [
'post_type' => 'et_template',
'post_status' => 'publish',
'metas' => [
[
'meta_key' => '_et_use_on',
'meta_value' => 'anyvalue',
],
],
],
],
'expected' => [
'transient_set' => true,
],
],
],

];
1 change: 1 addition & 0 deletions tests/Fixtures/wpdb.php
Expand Up @@ -3,6 +3,7 @@
if ( ! class_exists( 'wpdb' ) ) {
class wpdb {
public $posts = 'posts';
public $postmeta = 'postmeta';
public $term_taxonomy = 'terms';
public $posts_results = [];
public $terms_results = [];
Expand Down