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

[BUGS-8452] remove nonce_life filter #293

Merged
merged 30 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d7e6742
add a new function that will get the filter callback name
jazzsequence Aug 5, 2024
26b080f
alter the filtered message with the filter callback name(s)
jazzsequence Aug 5, 2024
d374a9b
handle the anonymous function string early
jazzsequence Aug 5, 2024
d53eaac
if one callaback, wrap in code tags or just output as plaintext (if a…
jazzsequence Aug 5, 2024
a3dcb0c
drop handling the 'an anonymous function' transform
jazzsequence Aug 5, 2024
fe90b47
add test filter callbacks
jazzsequence Aug 5, 2024
9175659
add tests
jazzsequence Aug 5, 2024
3dc71f8
adjust linting
jazzsequence Aug 5, 2024
8fc5896
remove $priority
jazzsequence Aug 5, 2024
15e41ba
bump tested-up-to
jazzsequence Aug 5, 2024
c9a45f0
apparently this is a gutenberg thing
jazzsequence Aug 5, 2024
b13e43f
WP 6.7 changes the output of human_time_diff
jazzsequence Aug 5, 2024
ea38399
strip out alpha, beta, rc, etc tags from version
jazzsequence Aug 6, 2024
5ad7027
don't filter nonce_life
jazzsequence Aug 6, 2024
cd0f461
alter and simplify filter_nonce_cache_lifetime
jazzsequence Aug 6, 2024
5dfb378
update readmes
jazzsequence Aug 6, 2024
89b3740
remove var_dump
jazzsequence Aug 6, 2024
3c88522
update the nonce test
jazzsequence Aug 6, 2024
af613a1
fix readme spacing
jazzsequence Aug 6, 2024
bf02609
Merge branch 'develop' into bugs-8452-remove-nonce-life-filter
jazzsequence Aug 6, 2024
9b859cf
set the initial cache value to 600
jazzsequence Aug 6, 2024
a570571
remove broken tests and add tests for each available option
jazzsequence Aug 6, 2024
e6792a9
remove behat tests that fail because we can't change the setting
jazzsequence Aug 6, 2024
cac5737
fix the wp cli command
jazzsequence Aug 6, 2024
4ac41a7
use set, since the option doesn't exist
jazzsequence Aug 6, 2024
263a537
add the pantheon-cache option if it doesn't exist
jazzsequence Aug 6, 2024
8584b5e
add the value of the option to add if the option didn't exist
jazzsequence Aug 6, 2024
078ae37
remove the conditional
jazzsequence Aug 6, 2024
de14f71
remove the updated test
jazzsequence Aug 6, 2024
f809490
update composer deps
jazzsequence Aug 6, 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
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,15 @@ When the cache max age is filtered in this way, the admin option is disabled and

![Page Cache Max Age with filtered value](.wordpress-org/screenshots/page-cache-max-age-filtered.png)

### Setting the Cache Max Age with a filter
### Updating the cache max age based on nonces

The cache max age setting is controlled by the [Pantheon Page Cache](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-pluginhttps://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin) admin page. As of 2.0.0, there are three cache age options by default — 1 week, 1 month, 1 year. Pantheon Advanced Page Cache automatically purges the cache of updated and related posts and pages, but you might want to override the cache max age value and set it programmatically. In this case, you can use the `pantheon_cache_default_max_age` filter added in [Pantheon MU plugin 1.4.0+](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin#override-the-default-max-age). For example:
Nonces created on the front-end, often used to secure forms and other data, have a lifetime, and if the cache max age is longer than the nonce lifetime, the nonce may expire before the cache does. To avoid this, you can use the `pantheon_cache_nonce_lifetime` action to set the `pantheon_cache_default_max_age` to less than the nonce lifetime. For example:

```php
add_filter( 'pantheon_cache_default_max_age', function() {
return 10 * DAY_IN_SECONDS;
} );
do_action( 'pantheon_cache_nonce_lifetime' );
```

When the cache max age is filtered in this way, the admin option is disabled and a notice is displayed.

![Page Cache Max Age with filtered value](.wordpress-org/screenshots/page-cache-max-age-filtered.png)
It's important to wrap your `do_action` in the appropriate conditionals to ensure that the action is only called when necessary and not filtering the cache max age in cases when it's not necessary. This might mean only running on certain pages or in certain contexts in your code.

## WP-CLI Commands ##

Expand Down
3 changes: 3 additions & 0 deletions bin/behat-prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ terminus wp $SITE_ENV -- cache flush
terminus wp $SITE_ENV -- plugin activate pantheon-advanced-page-cache
terminus wp $SITE_ENV -- theme activate twentytwentythree
terminus wp $SITE_ENV -- rewrite structure '/%year%/%monthnum%/%day%/%postname%/'
# Add the pantheon-cache option. We're assuming it doesn't already exist.
terminus wp $SITE_ENV -- option add pantheon-cache '{"default_ttl":600,"maintenance_mode":"disabled"}' --format=json

416 changes: 179 additions & 237 deletions composer.lock

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions inc/admin-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function bootstrap() {
add_filter( 'pantheon_cache_max_age_field_after_html', __NAMESPACE__ . '\\add_max_age_setting_description' );
add_filter( 'pantheon_cache_max_age_input', __NAMESPACE__ . '\\update_default_ttl_input' );
add_filter( 'pantheon_cache_max_age_input_allowed_html', __NAMESPACE__ . '\\max_age_input_allowed_html' );
add_filter( 'nonce_life', __NAMESPACE__ . '\\filter_nonce_cache_lifetime' );
add_action( 'pantheon_cache_nonce_lifetime', __NAMESPACE__ . '\\filter_nonce_cache_lifetime' );
}

/**
Expand Down Expand Up @@ -648,24 +648,26 @@ function max_age_updated_admin_notice() {
update_user_meta( $current_user_id, 'pantheon_max_age_updated_notice', true );
}


/**
* Filter the nonce cache lifetime.
* Filter the cache lifetime for nonces.
*
* Hooked to pantheon_cache_nonce_lifetime action. Use this to filter the cache lifetime for nonces using the action, e.g.:
*
* @param int $lifetime The lifetime of the nonce.
* do_action( 'pantheon_cache_nonce_lifetime' );
*
* @since 2.0.0
* @return int
* @return void
*/
function filter_nonce_cache_lifetime( $lifetime ) {
function filter_nonce_cache_lifetime() {
// Bail early if we're in the admin.
if ( is_admin() ) {
return $lifetime;
return;
}

// Filter the cache default max age to less than the nonce lifetime when creating nonces on the front-end. This prevents the cache from keeping the nonce around longer than it should.
add_filter( 'pantheon_cache_default_max_age', function () use ( $lifetime ) {
add_filter( 'pantheon_cache_default_max_age', function () {
$lifetime = apply_filters( 'nonce_life', DAY_IN_SECONDS );
return $lifetime - HOUR_IN_SECONDS;
} );

return $lifetime;
}
10 changes: 4 additions & 6 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,13 @@ The cache max age setting is controlled by the [Pantheon Page Cache](https://doc

When the cache max age is filtered in this way, the admin option is disabled and a notice is displayed.

= Setting the Cache Max Age with a filter =
= Updating the cache max age based on nonces =

The cache max age setting is controlled by the [Pantheon Page Cache](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin) admin page. As of 2.0.0, there are three cache age options by default — 1 week, 1 month, 1 year. Pantheon Advanced Page Cache automatically purges the cache of updated and related posts and pages, but you might want to override the cache max age value and set it programmatically. In this case, you can use the `pantheon_cache_default_max_age` filter added in [Pantheon MU plugin 1.4.0+](https://docs.pantheon.io/guides/wordpress-configurations/wordpress-cache-plugin#override-the-default-max-age). For example:
Nonces created on the front-end, often used to secure forms and other data, have a lifetime, and if the cache max age is longer than the nonce lifetime, the nonce may expire before the cache does. To avoid this, you can use the `pantheon_cache_nonce_lifetime` action to set the `pantheon_cache_default_max_age` to less than the nonce lifetime. For example:

add_filter( 'pantheon_cache_default_max_age', function() {
return 10 * DAY_IN_SECONDS;
} );
do_action( 'pantheon_cache_nonce_lifetime' );

When the cache max age is filtered in this way, the admin option is disabled and a notice is displayed.
It's important to wrap your `do_action` in the appropriate conditionals to ensure that the action is only called when necessary and not filtering the cache max age in cases when it's not necessary. This might mean only running on certain pages or in certain contexts in your code.

== WP-CLI Commands ==

Expand Down
22 changes: 11 additions & 11 deletions tests/behat/admin-interface.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ Feature: Adjust the Default Max Age setting
Background:
Given I log in as an admin

Scenario: Set max age to 600 and auto-update to the default value
Scenario: Change the cache max age to 1 week
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
And I fill in "pantheon-cache[default_ttl]" with "600"
And I fill in "pantheon-cache[default_ttl]" with "604800"
And I press "Save Changes"
Then I should see "The Pantheon GCDN cache max age has been updated. The previous value was 10 minutes. The new value is 1 week."
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
Then the "pantheon-cache[default_ttl]" field should contain "604800"
Then I should see "Settings saved."
And the "pantheon-cache[default_ttl]" field should contain "604800"

Scenario: Change the cache max age
Scenario: Change the cache max age to 1 month
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
And I fill in "pantheon-cache[default_ttl]" with "300"
And I fill in "pantheon-cache[default_ttl]" with "2592000"
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 1 week" in the ".notice" element
Then I should see "Settings saved."
And the "pantheon-cache[default_ttl]" field should contain "2592000"

Scenario: Change the cache max age to 1 week
Scenario: Change the cache max age to 1 year
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
And I fill in "pantheon-cache[default_ttl]" with "604800"
And I fill in "pantheon-cache[default_ttl]" with "31536000"
And I press "Save Changes"
Then I should see "Settings saved."
And the "pantheon-cache[default_ttl]" field should contain "31536000"
16 changes: 0 additions & 16 deletions tests/behat/site-health.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@ Feature: Site Health tests based on Cache Max Age
Background:
Given I log in as an admin

Scenario: Site Health should report when Max Age is a low value
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
And I fill in "pantheon-cache[default_ttl]" with "300"
And I press "Save Changes"
And I go to "/wp-admin/site-health.php"
Then I should see "Pantheon GCDN Cache Max Age"
And I should see "The Pantheon GCDN cache max age is currently set to 5 mins. We recommend increasing to 1 week"

Scenario: Site Health should report when Max age is less than the recommendation
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"
And I go to "/wp-admin/site-health.php"
Then I should see "Pantheon GCDN Cache Max Age"
And I should see "The Pantheon GCDN cache max age is currently set to 5 days. We recommend increasing to 1 week"

Scenario: Site Health check should pass when Max Age is the recommneded value
When I go to "/wp-admin/options-general.php?page=pantheon-cache"
And I fill in "pantheon-cache[default_ttl]" with "604800"
Expand Down
20 changes: 15 additions & 5 deletions tests/phpunit/test-admin-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function test_humanized_max_age( $max_age, $expected ) {
*/
public function humanized_max_age_provider() {
$five_mins = $this->get_five_minutes();
var_dump( $five_mins );

return [
[ 300, $five_mins ], // 300 seconds is humanized to 5 mins.
[ 5 * DAY_IN_SECONDS, '5 days' ],
Expand Down Expand Up @@ -503,10 +503,20 @@ public function test_filter_nonce_cache_lifetime( $screen, $expected ) {
}

$nonce_life = apply_filters( 'nonce_life', DAY_IN_SECONDS );
filter_nonce_cache_lifetime( $nonce_life );
$nonce_cache_lifetime = apply_filters( 'pantheon_cache_default_max_age', $nonce_life );

$this->assertEquals( $expected, $nonce_cache_lifetime, sprintf( '%s test failed to assert that %s was equal to %s', $screen, humanized_max_age( $nonce_cache_lifetime ), humanized_max_age( $expected ) ) );
do_action( 'pantheon_cache_nonce_lifetime' );
$cache_max_age = apply_filters( 'pantheon_cache_default_max_age', $nonce_life );

$this->assertEquals(
$expected,
$cache_max_age,
sprintf(
// 1: Screen, 2: Cache max age, 3: Expected max age.
'%s test failed to assert that %s was equal to %s',
$screen,
humanized_max_age( $cache_max_age ),
humanized_max_age( $expected )
)
);
}

/**
Expand Down
Loading