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

[CMSP-995] one more notice #271

Merged
merged 23 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
10f8f42
add a global admin notice that appears once
jazzsequence May 17, 2024
e64cdec
remove seconds from the notice
jazzsequence May 17, 2024
a88295a
test the new notice
jazzsequence May 17, 2024
3cb25eb
set dismissable depending on which notice we're showing
jazzsequence May 17, 2024
868ebe4
show the max age rank html comment
jazzsequence May 17, 2024
b440367
fix the conditionals for cs
jazzsequence May 17, 2024
35afd76
update behat tests for new notice language
jazzsequence May 17, 2024
83e095d
bugfix: don't update user meta that max_age was updated if it hasn't …
jazzsequence May 17, 2024
e563086
add one more thing to test in behat
jazzsequence May 20, 2024
ac1f985
add a test for the error, specifically
jazzsequence May 20, 2024
2d6b35c
add a test for the warning
jazzsequence May 20, 2024
ffc3c0b
simplify the conditionals
jazzsequence May 20, 2024
3843a26
lint
jazzsequence May 20, 2024
9438b72
specify the pages that we're on?
jazzsequence May 20, 2024
ad4b900
remove specific element
jazzsequence May 20, 2024
42e37e8
fix typo
jazzsequence May 20, 2024
d878914
take out the dashboard main page goto
jazzsequence May 20, 2024
6b0325e
validate the setting was saved
jazzsequence May 20, 2024
1e090af
let's just remove the 5 days notice
jazzsequence May 20, 2024
3573171
just remove the test
jazzsequence May 20, 2024
1e05a11
we can actually remove current_max_age >= WEEK_IN_SECONDS
jazzsequence May 20, 2024
c946d92
if we're only comparing two things, we don't need to break the if cla…
jazzsequence May 20, 2024
08f3349
we do want to warn users if they have manually set their max age to 600
jazzsequence May 20, 2024
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
42 changes: 33 additions & 9 deletions inc/admin-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,39 +111,59 @@ function admin_notice_old_mu_plugin() {
*/
function admin_notice_maybe_recommend_higher_max_age() {
$current_screen = get_current_screen();
$global_warning_shown = current_user_can( 'manage_options' ) ? get_user_meta( get_current_user_id(), 'pantheon_max_age_global_warning_notice', true ) : true;

if ( apply_filters( 'pantheon_apc_disable_admin_notices', false, __FUNCTION__ ) || 'settings_page_pantheon-cache' !== $current_screen->id ) {
if ( apply_filters( 'pantheon_apc_disable_admin_notices', false, __FUNCTION__ ) || $global_warning_shown ) {
return;
}

$message = '';
$dismissable = true;
$max_age_rank = max_age_compare();
$current_max_age = get_current_max_age();

// Check if the max-age rank is acceptable or if current max-age is 600 seconds and we haven't yet reset it to the default.
if (
$max_age_rank > 0 &&
$current_max_age < WEEK_IN_SECONDS &&
$current_max_age !== 600
$max_age_rank === 0 ||
( $current_max_age === 600 && ! get_option( 'pantheon_max_age_updated', false ) )
) {
// If the current max-age value has a rank of 3 or more (10 is the highest), we'll note that it's very low.
return;
}

if ( isset( $current_screen->id ) && 'settings_page_pantheon-cache' === $current_screen->id ) {
// If the current max-age value has a rank of 3 or more (10 is the highest), we'll note that it's very low.
$very_low = $max_age_rank > 3 ? __( 'This is a very low value and may not be optimal for your site.', 'pantheon-advanced-page-cache' ) : '';
$message = sprintf(
// translators: %1$s is the current max-age, %2$d is the current max-age in seconds, %3$s is a message that displays if the value is very low, %44d is the recommended max age in seconds, %5$s is the humanized recommended max age, %6$s is debug information that is written to the HTML DOM but not displayed.
__( 'The cache max-age is currently set to %1$s (%2$s seconds). %3$s Consider increasing the cache max-age to at least %4$d seconds (%5$s).%6$s', 'pantheon-advanced-page-cache' ),
__( 'The cache max-age is currently set to %1$s. %2$s Consider increasing the cache max-age to at least %3$s.%4$s', 'pantheon-advanced-page-cache' ),
humanized_max_age(),
$current_max_age,
$very_low,
WEEK_IN_SECONDS,
humanized_max_age( true ),
sprintf( '<!-- Max Age Rank: %d -->', $max_age_rank )
);
}

// Global notice on all pages _except_ the Pantheon cache settings page.
if ( ! isset( $current_screen->id ) || 'settings_page_pantheon-cache' !== $current_screen->id ) {
$message = sprintf(
// translators: %s is a link to the Pantheon GCDN configuration page.
__( 'Your site\'s cache max-age is set below the recommendation (1 week). Visit the <a href="%1$s">Pantheon GCDN configuration page</a> to update the setting.%2$s' ),
admin_url( 'options-general.php?page=pantheon-cache' ),
sprintf( '<!-- Max Age Rank: %d -->', $max_age_rank )
);
$dismissable = false;
update_user_meta( get_current_user_id(), 'pantheon_max_age_global_warning_notice', true );
}

if ( ! empty( $message ) ) {
// Escalating notice types based on the max-age rank.
$notice_type = ( $max_age_rank === 1 ? 'info' : $max_age_rank > 3 ) ? 'error' : 'warning';

wp_admin_notice(
$message,
[
'type' => $notice_type,
'dismissible' => true,
'dismissible' => $dismissable,
]
);
}
Expand Down Expand Up @@ -400,6 +420,10 @@ function max_age_updated_admin_notice() {
]
);

if ( ! $max_age_updated ) {
return;
}

// Update the user meta to prevent this notice from showing again after they've seen it once.
update_user_meta( $current_user_id, 'pantheon_max_age_updated_notice', true );
}
8 changes: 1 addition & 7 deletions tests/behat/admin-interface.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ Scenario: Change the cache max age
And I fill in "pantheon-cache[default_ttl]" with "300"
And I press "Save Changes"
Then I should see "This is a very low value and may not be optimal for your site" in the ".notice" element
And I should see "Consider increasing the cache max-age to at least 604800 seconds (1 week)" in the ".notice" element

Scenario: Change the cache max age to 5 days
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
And I fill in "pantheon-cache[default_ttl]" with "432000"
And I press "Save Changes"
Then I should see "Consider increasing the cache max-age to at least 604800 seconds (1 week)" in the ".notice" element
And I should see "Consider increasing the cache max-age to at least 1 week" in the ".notice" element

Scenario: Change the cache max age to 1 week
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/test-admin-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,39 @@ function test_max_age_updated_admin_notice() {
// The user meta should have been updated in the process.
$this->assertEquals( 1, get_user_meta( $current_user_id, 'pantheon_max_age_updated_notice', true ) );
}

/**
* Test that the user meta for the global admin notice is created.
*/
function test_low_max_age_admin_notice_user_meta() {
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved
// Switch to admin.
wp_set_current_user( 1 );
delete_user_meta( 1, 'pantheon_max_age_global_warning_notice' );
ob_start();
admin_notice_maybe_recommend_higher_max_age();
$notice = ob_get_clean();

$notice_shown = get_user_meta( 1, 'pantheon_max_age_global_warning_notice', true );
$this->assertStringContainsString( 'notice-error', $notice );
$this->assertStringContainsString( 'Your site\'s cache max-age is set below the recommendation (1 week).' , $notice );
$this->assertEquals( 1, $notice_shown );
}

/**
* Test that the user meta for the global admin notice is created.
*/
function test_low_max_age_admin_notice_user_meta_warning() {
// Switch to admin.
wp_set_current_user( 1 );
delete_user_meta( 1, 'pantheon_max_age_global_warning_notice' );
update_option( 'pantheon-cache', [ 'default_ttl' => 432000 ] );
ob_start();
admin_notice_maybe_recommend_higher_max_age();
$notice = ob_get_clean();

$notice_shown = get_user_meta( 1, 'pantheon_max_age_global_warning_notice', true );
$this->assertStringContainsString( 'notice-warning', $notice );
$this->assertStringContainsString( 'Your site\'s cache max-age is set below the recommendation (1 week).' , $notice );
$this->assertEquals( 1, $notice_shown );
}
}
Loading