Skip to content

Commit

Permalink
[BUGS-8452] remove nonce_life filter (#293)
Browse files Browse the repository at this point in the history
* add a new function that will get the filter callback name
this way it's easier to debug because we can show people where they've added the filter

* alter the filtered message with the filter callback name(s)
if it's in an anonymous function, we note that.

* handle the anonymous function string early
so we don't need to reset it to the string we want to output later

* if one callaback, wrap in code tags or just output as plaintext (if anonymous)

* drop handling the 'an anonymous function' transform

* add test filter callbacks

* add tests

* adjust linting

* remove $priority
not used

* bump tested-up-to

* apparently this is a gutenberg thing

* WP 6.7 changes the output of human_time_diff

* strip out alpha, beta, rc, etc tags from version
these aren't valid versions, so version_compare freaks out and (incorrectly) flags 6.7-alpha-whatever as less than 6.7 🤦‍♂️

* don't filter nonce_life
instead define an action that can be called by users

* alter and simplify filter_nonce_cache_lifetime
reframe this to be used in a `do_action` by customers who are need to make this change to alter the cache lifetime for nonces

* update readmes
remove duplicated section about the cache max age filter

* remove var_dump

* update the nonce test

* fix readme spacing

* set the initial cache value to 600
so it's set when we start the tests

* remove broken tests and add tests for each available option

* remove behat tests that fail because we can't change the setting

* fix the wp cli command

* use set, since the option doesn't exist

* add the pantheon-cache option if it doesn't exist

* add the value of the option to add if the option didn't exist

* remove the conditional
we can assume the setting doesn't exist

* remove the updated test
we're testing this with unit tests and this is being annoying in behat

* update composer deps
  • Loading branch information
jazzsequence authored Aug 6, 2024
1 parent 8ef69e1 commit 2b55db2
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 292 deletions.
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

0 comments on commit 2b55db2

Please sign in to comment.