From 864d076196e2a1861d18f2f4e2d8c4cbcdaaa482 Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Tue, 1 Aug 2023 13:03:36 -0700 Subject: [PATCH 1/8] Prepare 1.4.1-dev --- README.md | 4 +++- package-lock.json | 4 ++-- package.json | 2 +- pantheon-advanced-page-cache.php | 2 +- readme.txt | 4 +++- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 18e8d5c7..cfecf463 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Tags:** pantheon, cdn, cache **Requires at least:** 4.7 **Tested up to:** 6.2 -**Stable tag:** 1.4.0 +**Stable tag:** 1.4.1-dev **License:** GPLv2 or later **License URI:** http://www.gnu.org/licenses/gpl-2.0.html @@ -321,6 +321,8 @@ See [CONTRIBUTING.md](https://github.com/pantheon-systems/pantheon-advanced-page ## Changelog ## +### 1.4.1-dev ### + ### 1.4.0 ### * Bumped Dependencies [[236](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/236)] * Add filter `pantheon_should_add_terms` to allow disabling surrogate keys for posts' taxonomy terms [[239](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/239)] diff --git a/package-lock.json b/package-lock.json index f83ff134..7b9f854c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pantheon-advanced-page-cache", - "version": "1.4.0", + "version": "1.4.1-dev", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pantheon-advanced-page-cache", - "version": "1.4.0", + "version": "1.4.1-dev", "devDependencies": { "grunt": "^1.6.1", "grunt-wp-readme-to-markdown": "~2.1.0" diff --git a/package.json b/package.json index 5d270b52..2cc7db0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pantheon-advanced-page-cache", - "version": "1.4.0", + "version": "1.4.1-dev", "main": "Gruntfile.js", "author": "Pantheon", "devDependencies": { diff --git a/pantheon-advanced-page-cache.php b/pantheon-advanced-page-cache.php index a4e29bc2..e096e063 100644 --- a/pantheon-advanced-page-cache.php +++ b/pantheon-advanced-page-cache.php @@ -7,7 +7,7 @@ * Author URI: https://pantheon.io * Text Domain: pantheon-advanced-page-cache * Domain Path: /languages - * Version: 1.4.0 + * Version: 1.4.1-dev * * @package Pantheon_Advanced_Page_Cache */ diff --git a/readme.txt b/readme.txt index 5295d52c..a87cac0c 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: getpantheon, danielbachhuber, kporras07, jspellman, jazzs3quence, Tags: pantheon, cdn, cache Requires at least: 4.7 Tested up to: 6.2 -Stable tag: 1.4.0 +Stable tag: 1.4.1-dev License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -316,6 +316,8 @@ See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-saml-auth/blob/mast == Changelog == += 1.4.1-dev = + = 1.4.0 = * Bumped Dependencies [[236](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/236)] * Add filter `pantheon_should_add_terms` to allow disabling surrogate keys for posts' taxonomy terms [[239](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/239)] From d1e3e41267e99a2da7b685e31331c87a1f2e781d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 2 Aug 2023 21:55:00 -0700 Subject: [PATCH 2/8] Bump guzzlehttp/psr7 from 1.9.0 to 1.9.1 (#242) Bumps [guzzlehttp/psr7](https://github.com/guzzle/psr7) from 1.9.0 to 1.9.1. - [Release notes](https://github.com/guzzle/psr7/releases) - [Changelog](https://github.com/guzzle/psr7/blob/1.9.1/CHANGELOG.md) - [Commits](https://github.com/guzzle/psr7/compare/1.9.0...1.9.1) --- updated-dependencies: - dependency-name: guzzlehttp/psr7 dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- composer.lock | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/composer.lock b/composer.lock index fd9bb936..5918718a 100644 --- a/composer.lock +++ b/composer.lock @@ -968,16 +968,16 @@ }, { "name": "guzzlehttp/psr7", - "version": "1.9.0", + "version": "1.9.1", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "e98e3e6d4f86621a9b75f623996e6bbdeb4b9318" + "reference": "e4490cabc77465aaee90b20cfc9a770f8c04be6b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/e98e3e6d4f86621a9b75f623996e6bbdeb4b9318", - "reference": "e98e3e6d4f86621a9b75f623996e6bbdeb4b9318", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/e4490cabc77465aaee90b20cfc9a770f8c04be6b", + "reference": "e4490cabc77465aaee90b20cfc9a770f8c04be6b", "shasum": "" }, "require": { @@ -996,11 +996,6 @@ "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.9-dev" - } - }, "autoload": { "files": [ "src/functions_include.php" @@ -1058,7 +1053,7 @@ ], "support": { "issues": "https://github.com/guzzle/psr7/issues", - "source": "https://github.com/guzzle/psr7/tree/1.9.0" + "source": "https://github.com/guzzle/psr7/tree/1.9.1" }, "funding": [ { @@ -1074,7 +1069,7 @@ "type": "tidelift" } ], - "time": "2022-06-20T21:43:03+00:00" + "time": "2023-04-17T16:00:37+00:00" }, { "name": "myclabs/deep-copy", From 726d3cb1c85eebdcb63855c3cbc9e6539216f054 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Mon, 7 Aug 2023 10:39:23 -0400 Subject: [PATCH 3/8] Send the header to the result and not the server (#237) --- inc/class-emitter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/class-emitter.php b/inc/class-emitter.php index 01039fdd..64fdc69d 100644 --- a/inc/class-emitter.php +++ b/inc/class-emitter.php @@ -132,8 +132,8 @@ public static function filter_rest_pre_dispatch( $result, $server, $request ) { */ public static function filter_rest_post_dispatch( $result, $server ) { $keys = self::get_rest_api_surrogate_keys(); - if ( ! empty( $keys ) ) { - $server->send_header( self::HEADER_KEY, implode( ' ', $keys ) ); + if ( ! empty( $keys ) && $result instanceof \WP_REST_Response ) { + $result->header( self::HEADER_KEY, implode( ' ', $keys ) ); } return $result; } From 073d252b6170015d724e1f57230a46cd97326cca Mon Sep 17 00:00:00 2001 From: Chris Reynolds Date: Tue, 8 Aug 2023 09:37:11 -0600 Subject: [PATCH 4/8] fix broken code syntax in readme.txt (#244) --- readme.txt | 776 ++++++++++++++++++++++++++--------------------------- 1 file changed, 387 insertions(+), 389 deletions(-) diff --git a/readme.txt b/readme.txt index a87cac0c..f94df2c8 100644 --- a/readme.txt +++ b/readme.txt @@ -1,389 +1,387 @@ -=== Pantheon Advanced Page Cache === -Contributors: getpantheon, danielbachhuber, kporras07, jspellman, jazzs3quence, ryanshoover, rwagner00 -Tags: pantheon, cdn, cache -Requires at least: 4.7 -Tested up to: 6.2 -Stable tag: 1.4.1-dev -License: GPLv2 or later -License URI: http://www.gnu.org/licenses/gpl-2.0.html - -Automatically clear related pages from Pantheon's Edge when you update content. High TTL. Fresh content. Visitors never wait. - -== Description == - -[![CircleCI](https://circleci.com/gh/pantheon-systems/pantheon-advanced-page-cache.svg?style=svg)](https://circleci.com/gh/pantheon-systems/pantheon-advanced-page-cache) - -For sites wanting fine-grained control over how their responses are represented in their edge cache, Pantheon Advanced Page Cache is the golden ticket. Here's a high-level overview of how the plugin works: - -1. When a response is generated, the plugin uses surrogate keys based on WordPress' main `WP_Query` object to "tag" the response with identifers for the data used in the response. See the "Adding Custom Keys" section for including your own surrogate keys. -2. When WordPress data is modified, the plugin triggers a purge request for the data's corresponding surrogate keys. - -Because of its surrogate key technology, Pantheon Advanced Page Cache empowers WordPress sites with a significantly more accurate cache purge mechanism, and generally higher cache hit rate. It even works with the WordPress REST API. - -Go forth and make awesome! And, once you've built something great, [send us feature requests (or bug reports)](https://github.com/pantheon-systems/pantheon-advanced-page-cache/issues). - -== Installation == - -To install Pantheon Advanced Page Cache, follow these steps: - -1. Install the plugin from WordPress.org using the WordPress dashboard. -2. Activate the plugin. - -To install Pantheon Advanced Page Cache in one line with WP-CLI: - - wp plugin install pantheon-advanced-page-cache --activate - -== How It Works == - -Pantheon Advanced Page Cache makes heavy use of surrogate keys, which enable responses to be "tagged" with identifiers that can then later be used in purge requests. For instance, a home page response might include the `Surrogate-Key` header with these keys: - - Surrogate-Key: front home post-43 user-4 post-41 post-9 post-7 post-1 user-1 - -Similarly, a `GET` requests to `/wp-json/wp/v2/posts` might include the `Surrogate-Key` header with these keys: - - Surrogate-Key: rest-post-collection rest-post-43 rest-post-43 rest-post-9 rest-post-7 rest-post-1 - -Because cached responses include metadata describing the data therein, surrogate keys enable more flexible purging behavior like: - -* When a post is updated, clear the cache for the post's URL, the homepage, any index view the post appears on, and any REST API endpoints the post is present in. -* When an author changes their name, clear the cache for the author's archive and any post they've authored. - -There is a limit to the number of surrogate keys in a response, so we've optimized them based on a user's expectation of a normal WordPress site. See the "Emitted Keys" section for full details on which keys are included, and the "Adding Custom Keys" section following for information on how to add your own. - -= Adding Custom Keys = - -By default, Pantheon Advanced Page Cache generates surrogate keys based on an interpretation of the main `WP_Query` query object. Because WordPress sends headers before the page is rendered, you need to use the `pantheon_wp_main_query_surrogate_keys` filter to include additional surrogate keys for any data present on the page. - -For example, to include surrogate keys for a sidebar rendered on the homepage, you can filter the keys using the `is_home()` template tag: - - /** - * Add surrogate key for the featured content sidebar rendered on the homepage. - */ - add_filter( 'pantheon_wp_main_query_surrogate_keys', function( $keys ){ - if ( is_home() ) { - $keys[] = 'sidebar-home-featured'; - } - return $keys; - }); - -Then, when sidebars are updated, you can use the `pantheon_wp_clear_edge_keys()` helper function to emit a purge event specific to the surrogate key: - - /** - * Trigger a purge event for the featured content sidebar when widgets are updated. - */ - add_action( 'update_option_sidebars_widgets', function() { - pantheon_wp_clear_edge_keys( array( 'sidebar-home-featured' ) ); - }); - -Similarly, to include surrogate keys for posts queried on the homepage, you can pre-fetch the posts before the page is rendered: - - /** - * An example of pre-fetching a WP_Query to tag the - * response with queried data. You'd use `papcx_wp_query()` - * a second time within your template to use the data. - */ - add_filter( 'pantheon_wp_main_query_surrogate_keys', function( $keys ) { - if ( is_home() ) { - $query = papcx_wp_query( array( - 'post_type' => 'page', - ) ); - foreach( $query->posts as $post ) { - $keys[] = 'post-' . $post->ID; - } - } - return $keys; - }); - - /** - * Register a 'papc-non-persistent' cache group to cache data - * in a non-persistent manner. We only want data in this group - * to be cached within the page request. - */ - add_action( 'init', function(){ - wp_cache_add_non_persistent_groups( array( 'papc-non-persistent' ) ); - }); - - /** - * Helper function to instantiate a WP_Query object only - * once per page request. - * - * @param array $args Arguments to pass to WP_Query. - * @return WP_Query - */ - function papcx_wp_query( $args = array() ) { - $cache_key = md5( serialize( $args ) ); - // WP_Query object will be in cache the second time we use the function. - $cache_value = wp_cache_get( $cache_key, 'papc-non-persistent' ); - if ( false !== $cache_value ) { - return $cache_value; - } - $query = new WP_Query( $args ); - wp_cache_set( $cache_key, $query, 'papc-non-persistent' ); - return $query; - } - -Because Pantheon Advanced Page Cache already handles WordPress post purge events, there's no additional call to `pantheon_wp_clear_edge_keys()`. - -Lastly, the `pantheon_wp_rest_api_surrogate_keys` filter lets you filter surrogate keys present in a REST API response. - -Need a bit more power? In addition to `pantheon_wp_clear_edge_keys()`, there are two additional helper functions you can use: - -* `pantheon_wp_clear_edge_paths( $paths = array() )` - Purge cache for one or more paths. -* `pantheon_wp_clear_edge_all()` - Warning! With great power comes great responsibility. Purge the entire cache, but do so wisely. - -== WP-CLI Commands == - -This plugin implements a variety of [WP-CLI](https://wp-cli.org) commands. All commands are grouped into the `wp pantheon cache` namespace. - - $ wp help pantheon cache - - NAME - - wp pantheon cache - - DESCRIPTION - - Manage the Pantheon Advanced Page Cache. - - SYNOPSIS - - wp pantheon cache - - SUBCOMMANDS - - purge-all Purge the entire page cache. - purge-key Purge one or more surrogate keys from cache. - purge-path Purge one or more paths from cache. - -Use `wp help pantheon cache ` to learn more about each command. - -== Debugging == - -By default, Pantheon's infrastructure strips out the `Surrogate-Key` response header before responses are served to clients. The contents of this header can be viewed as `Surrogate-Key-Raw` by adding on a debugging header to the request. - -A direct way of inspecting headers is with `curl -I`. This command will make a request and show just the response headers. Adding `-H "Pantheon-Debug:1"` will result in `Surrogate-Key-Raw` being included in the response headers. The complete command looks like this: - - curl -IH "Pantheon-Debug:1" https://scalewp.io/ - -Piping to `grep` will filter the output down to just the `Surrogate-Key-Raw` header: - - curl -IH "Pantheon-Debug:1" https://scalewp.io/ | grep -i Surrogate-Key-Raw - -Tada! - -== Emitted Keys and Purge Events = - -= Emitted Keys on Traditional Views = - -**Home `/`** - -* Emits surrogate keys: `home`, `front`, `post-` (all posts in main query) - -**Single post `/2016/10/14/surrogate-keys/`** - -* Emits surrogate keys: `single`, `post-`, `post-user-`, `post-term-` (all terms assigned to post) - -**Author archive `/author/pantheon/`** - -* Emits surrogate keys: `archive`, `user-`, `post-` (all posts in main query) - -**Term archive `/tag/cdn/`** - -* Emits surrogate keys: `archive`, `term-`, `post-` (all posts in main query) - -**Day archive `/2016/10/14/`** - -* Emits surrogate keys: `archive`, `date`, `post-` (all posts in main query) - -**Month archive `/2016/10/`** - -* Emits surrogate keys: `archive`, `date`, `post-` (all posts in main query) - -**Year archive `/2016/`** - -* Emits surrogate keys: `archive`, `date`, `post-` (all posts in main query) - -**Search `/?s=`** - -* Emits surrogate keys: `search`, either `search-results` or `search-no-results`, `post-` (all posts in main query) - -**Not found (404)** - -* Emits surrogate keys: `404` - -= Emitted Keys on REST API Endpoints = - -**Posts** - -* `/wp-json/wp/v2/posts` emits surrogate keys: `rest-post-collection`, `rest-post-` -* `/wp-json/wp/v2/posts/` emits surrogate keys: `rest-post-` - -**Pages** - -* `/wp-json/wp/v2/pages` emits surrogate keys: `rest-page-collection`, `rest-post-` -* `/wp-json/wp/v2/pages/` emits surrogate keys: `rest-post-` - -**Categories** - -* `/wp-json/wp/v2/categories` emits surrogate keys: `rest-category-collection`, `rest-term-` -* `/wp-json/wp/v2/categories/` emits surrogate keys: `rest-term-` - -**Tags** - -* `/wp-json/wp/v2/tags` emits surrogate keys: `rest-post_tag-collection`, `rest-term-` -* `/wp-json/wp/v2/tags/` emits surrogate keys: `rest-term-` - -**Comments** - -* `/wp-json/wp/v2/comments` emits surrogate keys: `rest-comment-collection`, `rest-comment-post-`, `rest-comment-` -* `/wp-json/wp/v2/comments/` emits surrogate keys: `rest-comment-post-`, `rest-comment-` - -**Users** - -* `/wp-json/wp/v2/users` emits surrogate keys: `rest-user-collection`, `rest-user-` -* `/wp-json/wp/v2/users/` emits surrogate keys: `rest-user-` - -**Settings** - -* `/wp-json/wp/v2/settings` emits surrogate keys: `rest-setting-` - -= Purge Events = - -Different WordPress actions cause different surrogate keys to be purged, documented here. - -**wp_insert_post / transition_post_status / before_delete_post / delete_attachment** - -* Purges surrogate keys: `home`, `front`, `404`, `post-`, `user-`, `term-`, `rest--collection`, `rest-comment-post-` -* Affected views: homepage, single post, any page with 404 header, any archive where post displays, author archive, term archive, REST API collection and resource endpoints - -**clean_post_cache** - -* Purges surrogate keys: `post-`, `rest-post-` -* Affected views: single post, REST API resource endpoint - -**created_term / edited_term / delete_term** - -* Purges surrogate keys: `term-`, `post-term-`, `rest--collection` -* Affected views: term archive, any post where the term is assigned, REST API collection and resource endpoints - -**clean_term_cache** - -* Purges surrogate keys: `term-`, `rest-term-` -* Affected views: term archive, REST API resource endpoint - -**wp_insert_comment / transition_comment_status** - -* Purges surrogate keys: `rest-comment-collection`, `rest-comment-` -* Affected views: REST API collection and resource endpoints - -**clean_comment_cache** - -* Purges surrogate keys: `rest-comment-` -* Affected views: REST API resource endpoint - -**clean_user_cache** - -* Purges surrogate keys: `user-`, `rest-user-` -* Affected views: author archive, any post where the user is the author - -**updated_option** - -* Purges surrogate keys: `rest-setting-` -* Affected views: REST API resource endpoint - -== Surrogate Keys for taxonomy terms == -Setting surrogate keys for posts with large numbers of taxonomies (such as WooCommerce products with a large number of global attributes) can suffer from slower queries. Surrogate keys can be skipped for 'product' post types' taxonomy terms (or any other criteria you see fit) with the following filter: - -``` -function custom_should_add_terms($should_add_terms, $wp_query) { - if ( $wp_query->is_singular( 'product' ) ) { - return false; - } - return $should_add_terms; -} -add_filter('pantheon_should_add_terms', 'custom_should_add_terms', 10, 2); -``` - -== Plugin Integrations == - -Pantheon Advanced Page Cache integrates with WordPress plugins, including: - -* [WPGraphQL](https://wordpress.org/plugins/wp-graphql/) - -== Contributing == - -See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-saml-auth/blob/master/CONTRIBUTING.md) for information on contributing. - -== Changelog == - -= 1.4.1-dev = - -= 1.4.0 = -* Bumped Dependencies [[236](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/236)] -* Add filter `pantheon_should_add_terms` to allow disabling surrogate keys for posts' taxonomy terms [[239](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/239)] - -= 1.3.0 = -* Adds support for WordPress Multisite which resolves issue where editing a Post on one subsite clears the home page cache of other sites in the Multisite install if it has a Post containing the same ID [[#228](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/228)]. - -= 1.2.4 (April 13, 2023) = -* Adds surrogate key to post-type archive pages (e.g. "portfolio") that's specific to that archive(e.g. "portfolio-archive"), and clears that archive where appropriate [[#225](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/225)]. - -= 1.2.3 (April 5, 2023) = -* Bump tested up to version to 6.2 - -= 1.2.2 (March 14, 2023) = -* Adds PHP 8.2 compatibility [[#218](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/218)]. -* Bump dependencies [[#204](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/204)]. - -= 1.2.1 (February 23, 2023) = -* Handle models that are not instances of the `WPGraphQL\Model\Model` class [[#212](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/212)]. -* Make dependabot target develop branch [[#209](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/209)]. -* Bump dependencies [[#210](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/210)] [[#214](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/214)]. - -= 1.2.0 (November 29, 2022) = -* Adds Github Actions for building tag and deploying to wp.org. Add CONTRIBUTING.md. [[#203](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/203)] - -= 1.1.0 (November 1, 2022) = -* Hook into WPGraphQL to emit surrogate keys [[#199](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/199)]. -* Add Plugin Integrations section to README - -= 1.0.0 (March 2, 2020) = -* Plugin is stable. - -= 0.3.1 (October 27th, 2019) = -* Fixes reversed argument order with use of `implode()` [[#139](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/139)]. -* Various PHPCS cleanup [[#127](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/127)]. - -= 0.3.0 (November 27th, 2017) = -* Emits '404' surrogate key on 404s; purges when purging the homepage [[#107](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/107)]. -* Adds more specific filters for modifying surrogate keys in different contexts [[#109](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/109)]. -* Cleans up codebase according to WordPress Coding Standards [[#110](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/110), [#116](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/116)]. - -= 0.2.1 (October 25th, 2017) = -* Ensures use of `?_embed` emits correct surrogate keys [[#103](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/103)]. - -= 0.2.0 (August 10th, 2017) = -* Automatically trims large lists of surrogate keys that break Nginx and Varnish limits for header size. - -= 0.1.5 (May 24th, 2017) = -* Disables emitting surrogate keys for the admin, unless explicitly added by filter. - -= 0.1.4 (March 7th, 2017) = -* Emits `feed` surrogate key for RSS feeds, and purges when posts are created, modified, or deleted. - -= 0.1.3 (March 1st, 2017) = -* Prevents error notices by only accessing `$rest_base` property of post types and taxonomies when set. - -= 0.1.2 (December 6th, 2016) = -* Permits admins to flush cache for a specific page if the `delete_others_posts` capability has been deleted. - -= 0.1.1 (November 30th, 2016) = -* Drops settings UI in favor of including it in Pantheon's WordPress upstream. - -= 0.1.0 (November 23rd, 2016) = -* Initial release. - -== Upgrade Notice == - -= Latest = -Note that the Pantheon Advanced Page Cache 1.3.0 release now prefixes keys on a WordPress Multisite (WPMS) with the blog ID. For users who already have this plugin installed on a WPMS, they will need to click the Clear Cache button on the settings page to generate the prefixed keys. \ No newline at end of file +=== Pantheon Advanced Page Cache === +Contributors: getpantheon, danielbachhuber, kporras07, jspellman, jazzs3quence, ryanshoover, rwagner00 +Tags: pantheon, cdn, cache +Requires at least: 4.7 +Tested up to: 6.2 +Stable tag: 1.4.1-dev +License: GPLv2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html + +Automatically clear related pages from Pantheon's Edge when you update content. High TTL. Fresh content. Visitors never wait. + +== Description == + +[![CircleCI](https://circleci.com/gh/pantheon-systems/pantheon-advanced-page-cache.svg?style=svg)](https://circleci.com/gh/pantheon-systems/pantheon-advanced-page-cache) + +For sites wanting fine-grained control over how their responses are represented in their edge cache, Pantheon Advanced Page Cache is the golden ticket. Here's a high-level overview of how the plugin works: + +1. When a response is generated, the plugin uses surrogate keys based on WordPress' main `WP_Query` object to "tag" the response with identifers for the data used in the response. See the "Adding Custom Keys" section for including your own surrogate keys. +2. When WordPress data is modified, the plugin triggers a purge request for the data's corresponding surrogate keys. + +Because of its surrogate key technology, Pantheon Advanced Page Cache empowers WordPress sites with a significantly more accurate cache purge mechanism, and generally higher cache hit rate. It even works with the WordPress REST API. + +Go forth and make awesome! And, once you've built something great, [send us feature requests (or bug reports)](https://github.com/pantheon-systems/pantheon-advanced-page-cache/issues). + +== Installation == + +To install Pantheon Advanced Page Cache, follow these steps: + +1. Install the plugin from WordPress.org using the WordPress dashboard. +2. Activate the plugin. + +To install Pantheon Advanced Page Cache in one line with WP-CLI: + + wp plugin install pantheon-advanced-page-cache --activate + +== How It Works == + +Pantheon Advanced Page Cache makes heavy use of surrogate keys, which enable responses to be "tagged" with identifiers that can then later be used in purge requests. For instance, a home page response might include the `Surrogate-Key` header with these keys: + + Surrogate-Key: front home post-43 user-4 post-41 post-9 post-7 post-1 user-1 + +Similarly, a `GET` requests to `/wp-json/wp/v2/posts` might include the `Surrogate-Key` header with these keys: + + Surrogate-Key: rest-post-collection rest-post-43 rest-post-43 rest-post-9 rest-post-7 rest-post-1 + +Because cached responses include metadata describing the data therein, surrogate keys enable more flexible purging behavior like: + +* When a post is updated, clear the cache for the post's URL, the homepage, any index view the post appears on, and any REST API endpoints the post is present in. +* When an author changes their name, clear the cache for the author's archive and any post they've authored. + +There is a limit to the number of surrogate keys in a response, so we've optimized them based on a user's expectation of a normal WordPress site. See the "Emitted Keys" section for full details on which keys are included, and the "Adding Custom Keys" section following for information on how to add your own. + += Adding Custom Keys = + +By default, Pantheon Advanced Page Cache generates surrogate keys based on an interpretation of the main `WP_Query` query object. Because WordPress sends headers before the page is rendered, you need to use the `pantheon_wp_main_query_surrogate_keys` filter to include additional surrogate keys for any data present on the page. + +For example, to include surrogate keys for a sidebar rendered on the homepage, you can filter the keys using the `is_home()` template tag: + + /** + * Add surrogate key for the featured content sidebar rendered on the homepage. + */ + add_filter( 'pantheon_wp_main_query_surrogate_keys', function( $keys ){ + if ( is_home() ) { + $keys[] = 'sidebar-home-featured'; + } + return $keys; + }); + +Then, when sidebars are updated, you can use the `pantheon_wp_clear_edge_keys()` helper function to emit a purge event specific to the surrogate key: + + /** + * Trigger a purge event for the featured content sidebar when widgets are updated. + */ + add_action( 'update_option_sidebars_widgets', function() { + pantheon_wp_clear_edge_keys( array( 'sidebar-home-featured' ) ); + }); + +Similarly, to include surrogate keys for posts queried on the homepage, you can pre-fetch the posts before the page is rendered: + + /** + * An example of pre-fetching a WP_Query to tag the + * response with queried data. You'd use `papcx_wp_query()` + * a second time within your template to use the data. + */ + add_filter( 'pantheon_wp_main_query_surrogate_keys', function( $keys ) { + if ( is_home() ) { + $query = papcx_wp_query( array( + 'post_type' => 'page', + ) ); + foreach( $query->posts as $post ) { + $keys[] = 'post-' . $post->ID; + } + } + return $keys; + }); + + /** + * Register a 'papc-non-persistent' cache group to cache data + * in a non-persistent manner. We only want data in this group + * to be cached within the page request. + */ + add_action( 'init', function(){ + wp_cache_add_non_persistent_groups( array( 'papc-non-persistent' ) ); + }); + + /** + * Helper function to instantiate a WP_Query object only + * once per page request. + * + * @param array $args Arguments to pass to WP_Query. + * @return WP_Query + */ + function papcx_wp_query( $args = array() ) { + $cache_key = md5( serialize( $args ) ); + // WP_Query object will be in cache the second time we use the function. + $cache_value = wp_cache_get( $cache_key, 'papc-non-persistent' ); + if ( false !== $cache_value ) { + return $cache_value; + } + $query = new WP_Query( $args ); + wp_cache_set( $cache_key, $query, 'papc-non-persistent' ); + return $query; + } + +Because Pantheon Advanced Page Cache already handles WordPress post purge events, there's no additional call to `pantheon_wp_clear_edge_keys()`. + +Lastly, the `pantheon_wp_rest_api_surrogate_keys` filter lets you filter surrogate keys present in a REST API response. + +Need a bit more power? In addition to `pantheon_wp_clear_edge_keys()`, there are two additional helper functions you can use: + +* `pantheon_wp_clear_edge_paths( $paths = array() )` - Purge cache for one or more paths. +* `pantheon_wp_clear_edge_all()` - Warning! With great power comes great responsibility. Purge the entire cache, but do so wisely. + +== WP-CLI Commands == + +This plugin implements a variety of [WP-CLI](https://wp-cli.org) commands. All commands are grouped into the `wp pantheon cache` namespace. + + $ wp help pantheon cache + + NAME + + wp pantheon cache + + DESCRIPTION + + Manage the Pantheon Advanced Page Cache. + + SYNOPSIS + + wp pantheon cache + + SUBCOMMANDS + + purge-all Purge the entire page cache. + purge-key Purge one or more surrogate keys from cache. + purge-path Purge one or more paths from cache. + +Use `wp help pantheon cache ` to learn more about each command. + +== Debugging == + +By default, Pantheon's infrastructure strips out the `Surrogate-Key` response header before responses are served to clients. The contents of this header can be viewed as `Surrogate-Key-Raw` by adding on a debugging header to the request. + +A direct way of inspecting headers is with `curl -I`. This command will make a request and show just the response headers. Adding `-H "Pantheon-Debug:1"` will result in `Surrogate-Key-Raw` being included in the response headers. The complete command looks like this: + + curl -IH "Pantheon-Debug:1" https://scalewp.io/ + +Piping to `grep` will filter the output down to just the `Surrogate-Key-Raw` header: + + curl -IH "Pantheon-Debug:1" https://scalewp.io/ | grep -i Surrogate-Key-Raw + +Tada! + +== Emitted Keys and Purge Events = + += Emitted Keys on Traditional Views = + +**Home `/`** + +* Emits surrogate keys: `home`, `front`, `post-` (all posts in main query) + +**Single post `/2016/10/14/surrogate-keys/`** + +* Emits surrogate keys: `single`, `post-`, `post-user-`, `post-term-` (all terms assigned to post) + +**Author archive `/author/pantheon/`** + +* Emits surrogate keys: `archive`, `user-`, `post-` (all posts in main query) + +**Term archive `/tag/cdn/`** + +* Emits surrogate keys: `archive`, `term-`, `post-` (all posts in main query) + +**Day archive `/2016/10/14/`** + +* Emits surrogate keys: `archive`, `date`, `post-` (all posts in main query) + +**Month archive `/2016/10/`** + +* Emits surrogate keys: `archive`, `date`, `post-` (all posts in main query) + +**Year archive `/2016/`** + +* Emits surrogate keys: `archive`, `date`, `post-` (all posts in main query) + +**Search `/?s=`** + +* Emits surrogate keys: `search`, either `search-results` or `search-no-results`, `post-` (all posts in main query) + +**Not found (404)** + +* Emits surrogate keys: `404` + += Emitted Keys on REST API Endpoints = + +**Posts** + +* `/wp-json/wp/v2/posts` emits surrogate keys: `rest-post-collection`, `rest-post-` +* `/wp-json/wp/v2/posts/` emits surrogate keys: `rest-post-` + +**Pages** + +* `/wp-json/wp/v2/pages` emits surrogate keys: `rest-page-collection`, `rest-post-` +* `/wp-json/wp/v2/pages/` emits surrogate keys: `rest-post-` + +**Categories** + +* `/wp-json/wp/v2/categories` emits surrogate keys: `rest-category-collection`, `rest-term-` +* `/wp-json/wp/v2/categories/` emits surrogate keys: `rest-term-` + +**Tags** + +* `/wp-json/wp/v2/tags` emits surrogate keys: `rest-post_tag-collection`, `rest-term-` +* `/wp-json/wp/v2/tags/` emits surrogate keys: `rest-term-` + +**Comments** + +* `/wp-json/wp/v2/comments` emits surrogate keys: `rest-comment-collection`, `rest-comment-post-`, `rest-comment-` +* `/wp-json/wp/v2/comments/` emits surrogate keys: `rest-comment-post-`, `rest-comment-` + +**Users** + +* `/wp-json/wp/v2/users` emits surrogate keys: `rest-user-collection`, `rest-user-` +* `/wp-json/wp/v2/users/` emits surrogate keys: `rest-user-` + +**Settings** + +* `/wp-json/wp/v2/settings` emits surrogate keys: `rest-setting-` + += Purge Events = + +Different WordPress actions cause different surrogate keys to be purged, documented here. + +**wp_insert_post / transition_post_status / before_delete_post / delete_attachment** + +* Purges surrogate keys: `home`, `front`, `404`, `post-`, `user-`, `term-`, `rest--collection`, `rest-comment-post-` +* Affected views: homepage, single post, any page with 404 header, any archive where post displays, author archive, term archive, REST API collection and resource endpoints + +**clean_post_cache** + +* Purges surrogate keys: `post-`, `rest-post-` +* Affected views: single post, REST API resource endpoint + +**created_term / edited_term / delete_term** + +* Purges surrogate keys: `term-`, `post-term-`, `rest--collection` +* Affected views: term archive, any post where the term is assigned, REST API collection and resource endpoints + +**clean_term_cache** + +* Purges surrogate keys: `term-`, `rest-term-` +* Affected views: term archive, REST API resource endpoint + +**wp_insert_comment / transition_comment_status** + +* Purges surrogate keys: `rest-comment-collection`, `rest-comment-` +* Affected views: REST API collection and resource endpoints + +**clean_comment_cache** + +* Purges surrogate keys: `rest-comment-` +* Affected views: REST API resource endpoint + +**clean_user_cache** + +* Purges surrogate keys: `user-`, `rest-user-` +* Affected views: author archive, any post where the user is the author + +**updated_option** + +* Purges surrogate keys: `rest-setting-` +* Affected views: REST API resource endpoint + +== Surrogate Keys for taxonomy terms == +Setting surrogate keys for posts with large numbers of taxonomies (such as WooCommerce products with a large number of global attributes) can suffer from slower queries. Surrogate keys can be skipped for 'product' post types' taxonomy terms (or any other criteria you see fit) with the following filter: + + function custom_should_add_terms($should_add_terms, $wp_query) { + if ( $wp_query->is_singular( 'product' ) ) { + return false; + } + return $should_add_terms; + } + add_filter('pantheon_should_add_terms', 'custom_should_add_terms', 10, 2); + +== Plugin Integrations == + +Pantheon Advanced Page Cache integrates with WordPress plugins, including: + +* [WPGraphQL](https://wordpress.org/plugins/wp-graphql/) + +== Contributing == + +See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-saml-auth/blob/master/CONTRIBUTING.md) for information on contributing. + +== Changelog == + += 1.4.1-dev = + += 1.4.0 = +* Bumped Dependencies [[236](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/236)] +* Add filter `pantheon_should_add_terms` to allow disabling surrogate keys for posts' taxonomy terms [[239](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/239)] + += 1.3.0 = +* Adds support for WordPress Multisite which resolves issue where editing a Post on one subsite clears the home page cache of other sites in the Multisite install if it has a Post containing the same ID [[#228](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/228)]. + += 1.2.4 (April 13, 2023) = +* Adds surrogate key to post-type archive pages (e.g. "portfolio") that's specific to that archive(e.g. "portfolio-archive"), and clears that archive where appropriate [[#225](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/225)]. + += 1.2.3 (April 5, 2023) = +* Bump tested up to version to 6.2 + += 1.2.2 (March 14, 2023) = +* Adds PHP 8.2 compatibility [[#218](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/218)]. +* Bump dependencies [[#204](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/204)]. + += 1.2.1 (February 23, 2023) = +* Handle models that are not instances of the `WPGraphQL\Model\Model` class [[#212](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/212)]. +* Make dependabot target develop branch [[#209](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/209)]. +* Bump dependencies [[#210](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/210)] [[#214](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/214)]. + += 1.2.0 (November 29, 2022) = +* Adds Github Actions for building tag and deploying to wp.org. Add CONTRIBUTING.md. [[#203](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/203)] + += 1.1.0 (November 1, 2022) = +* Hook into WPGraphQL to emit surrogate keys [[#199](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/199)]. +* Add Plugin Integrations section to README + += 1.0.0 (March 2, 2020) = +* Plugin is stable. + += 0.3.1 (October 27th, 2019) = +* Fixes reversed argument order with use of `implode()` [[#139](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/139)]. +* Various PHPCS cleanup [[#127](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/127)]. + += 0.3.0 (November 27th, 2017) = +* Emits '404' surrogate key on 404s; purges when purging the homepage [[#107](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/107)]. +* Adds more specific filters for modifying surrogate keys in different contexts [[#109](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/109)]. +* Cleans up codebase according to WordPress Coding Standards [[#110](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/110), [#116](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/116)]. + += 0.2.1 (October 25th, 2017) = +* Ensures use of `?_embed` emits correct surrogate keys [[#103](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/103)]. + += 0.2.0 (August 10th, 2017) = +* Automatically trims large lists of surrogate keys that break Nginx and Varnish limits for header size. + += 0.1.5 (May 24th, 2017) = +* Disables emitting surrogate keys for the admin, unless explicitly added by filter. + += 0.1.4 (March 7th, 2017) = +* Emits `feed` surrogate key for RSS feeds, and purges when posts are created, modified, or deleted. + += 0.1.3 (March 1st, 2017) = +* Prevents error notices by only accessing `$rest_base` property of post types and taxonomies when set. + += 0.1.2 (December 6th, 2016) = +* Permits admins to flush cache for a specific page if the `delete_others_posts` capability has been deleted. + += 0.1.1 (November 30th, 2016) = +* Drops settings UI in favor of including it in Pantheon's WordPress upstream. + += 0.1.0 (November 23rd, 2016) = +* Initial release. + +== Upgrade Notice == + += Latest = +Note that the Pantheon Advanced Page Cache 1.3.0 release now prefixes keys on a WordPress Multisite (WPMS) with the blog ID. For users who already have this plugin installed on a WPMS, they will need to click the Clear Cache button on the settings page to generate the prefixed keys. From a38aaf566e14cc4a74c002cde003b496f9c4b75d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 8 Aug 2023 10:59:04 -0600 Subject: [PATCH 5/8] Bump guzzlehttp/psr7 from 1.9.0 to 1.9.1 (#245) Bumps [guzzlehttp/psr7](https://github.com/guzzle/psr7) from 1.9.0 to 1.9.1. - [Release notes](https://github.com/guzzle/psr7/releases) - [Changelog](https://github.com/guzzle/psr7/blob/1.9.1/CHANGELOG.md) - [Commits](https://github.com/guzzle/psr7/compare/1.9.0...1.9.1) --- updated-dependencies: - dependency-name: guzzlehttp/psr7 dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> From d57ec62e4e52a779c314c43fd98e45b57370d12a Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Tue, 8 Aug 2023 10:00:17 -0700 Subject: [PATCH 6/8] Update CONTRIBUTING per recent learnings (#243) --- CONTRIBUTING.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 790765c8..c3bf06d0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,17 +25,20 @@ The `master` branch matches the latest stable release deployed to [wp.org](wp.or * Commit these changes with the message `Release X.Y.Z` * Push the release branch up. 1. Open a Pull Request to merge `release_X.Y.Z` into `master`. Your PR should consist of all commits to `develop` since the last release, and one commit to update the version number. The PR name should also be `Release X.Y.Z`. -1. After all tests pass and you have received approval from a [CODEOWNER](./CODEOWNERS), merge the PR into `master`. "Rebase and merge" is preferred in this case. _Never_ squash to `master`. +1. After all tests pass and you have received approval from a [CODEOWNER](./CODEOWNERS), merge the PR into `master`. "Merge" is preferred in this case, not rebase. _Never_ squash to `master`. 1. Pull `master` locally, create a new tag (based on version number from previous steps), and push up. The tag should _only_ be the version number. It _should not_ be prefixed `v` (i.e. `X.Y.Z`, not `vX.Y.X`). 1. Confirm that the necessary assets are present in the newly created tag, and test on a WP install if desired. -1. Create a [new release](https://github.com/pantheon-systems/pantheon-advanced-page-cache/releases/new) using the tag created in the previous steps, naming the release with the new version number, and targeting the tag created in the previous step. Paste the release changelog from the `Changelog` section of [the readme](readme.txt) into the body of the release, including the links to the closed issues if applicable. +1. Create a [new release](https://github.com/pantheon-systems/pantheon-advanced-page-cache/releases/new) using the tag created in the previous steps, naming the release with the new version number, and targeting the tag created in the previous step. Use the "Generate Release Notes" button. + * This can be done with `gh` with the following: `gh release create "X.Y.Z" -t "X.Y.Z" --generate-notes` 1. Wait for the [_Release pantheon-advanced-page-cache plugin to wp.org_ action](https://github.com/pantheon-systems/pantheon-advanced-page-cache/actions/workflows/wordpress-plugin-deploy.yml) to finish deploying to the WordPress.org plugin repository. If all goes well, users with SVN commit access for that plugin will receive an emailed diff of changes. 1. Check WordPress.org: Ensure that the changes are live on [the plugin repository](https://wordpress.org/plugins/pantheon-advanced-page-cache/). This may take a few minutes. 1. Following the release, prepare the next dev version with the following steps: + * `git checkout master` + * `git pull origin master` * `git checkout develop` * `git rebase master` * Update the version number in all locations, incrementing the version by one patch version, and add the `-dev` flag (e.g. after releasing `1.2.3`, the new verison will be `1.2.4-dev`) - * Add a new `** Latest **` heading to the changelog + * Add a new `** X.Y.X-dev **` heading to the changelog in readme.txt and README.md * `git add -A .` * `git commit -m "Prepare X.Y.X-dev"` * `git push origin develop` \ No newline at end of file From 10eba077f34a837b1572ae1b95e6829bb11e9256 Mon Sep 17 00:00:00 2001 From: Chris Reynolds Date: Tue, 8 Aug 2023 11:36:44 -0600 Subject: [PATCH 7/8] [BUGS-5914] Add PHP unit test cases for GraphQL and REST API response headers (#246) * add test for graphql response headers * add test for rest api response headers * change the expected graphql-collection key if we're in multisite * Bump guzzlehttp/psr7 from 1.9.0 to 1.9.1 (#245) Bumps [guzzlehttp/psr7](https://github.com/guzzle/psr7) from 1.9.0 to 1.9.1. - [Release notes](https://github.com/guzzle/psr7/releases) - [Changelog](https://github.com/guzzle/psr7/blob/1.9.1/CHANGELOG.md) - [Commits](https://github.com/guzzle/psr7/compare/1.9.0...1.9.1) --- updated-dependencies: - dependency-name: guzzlehttp/psr7 dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update CONTRIBUTING per recent learnings (#243) * add test for graphql response headers * add test for rest api response headers * change the expected graphql-collection key if we're in multisite * add phil * bump tested up to * add pr to changelog * fix typo * apply all that stuff to the readme.txt, too * fix readme and i'm never editing markdown files in vscode ever again ever --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Phil Tyler --- README.md | 5 ++- readme.txt | 5 ++- tests/phpunit/test-emitter-rest-api.php | 57 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cfecf463..3f6e42fc 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Pantheon Advanced Page Cache # -**Contributors:** [getpantheon](https://profiles.wordpress.org/getpantheon), [danielbachhuber](https://profiles.wordpress.org/danielbachhuber), [kporras07](https://profiles.wordpress.org/kporras07), [jspellman](https://profiles.wordpress.org/jspellman/), [jazzs3quence](https://profiles.wordpress.org/jazzs3quence/), [ryanshoover](https://profiles.wordpress.org/ryanshoover/), [rwagner00](https://profiles.wordpress.org/rwagner00/) +**Contributors:** [getpantheon](https://profiles.wordpress.org/getpantheon), [danielbachhuber](https://profiles.wordpress.org/danielbachhuber), [kporras07](https://profiles.wordpress.org/kporras07), [jspellman](https://profiles.wordpress.org/jspellman/), [jazzs3quence](https://profiles.wordpress.org/jazzs3quence/), [ryanshoover](https://profiles.wordpress.org/ryanshoover/), [rwagner00](https://profiles.wordpress.org/rwagner00/), [pwtyler](https://profiles.wordpress.org/pwtyler) **Tags:** pantheon, cdn, cache **Requires at least:** 4.7 -**Tested up to:** 6.2 +**Tested up to:** 6.3 **Stable tag:** 1.4.1-dev **License:** GPLv2 or later **License URI:** http://www.gnu.org/licenses/gpl-2.0.html @@ -322,6 +322,7 @@ See [CONTRIBUTING.md](https://github.com/pantheon-systems/pantheon-advanced-page ## Changelog ## ### 1.4.1-dev ### +* Send the REST API response header to the result and not the REST server [[#237](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/237)]. Props [@srtfisher](https://github.com/srtfisher) & [@felixarntz](https://github.com/felixarntz). ### 1.4.0 ### * Bumped Dependencies [[236](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/236)] diff --git a/readme.txt b/readme.txt index f94df2c8..4e155a47 100644 --- a/readme.txt +++ b/readme.txt @@ -1,8 +1,8 @@ === Pantheon Advanced Page Cache === -Contributors: getpantheon, danielbachhuber, kporras07, jspellman, jazzs3quence, ryanshoover, rwagner00 +Contributors: getpantheon, danielbachhuber, kporras07, jspellman, jazzs3quence, ryanshoover, rwagner00, pwtyler Tags: pantheon, cdn, cache Requires at least: 4.7 -Tested up to: 6.2 +Tested up to: 6.3 Stable tag: 1.4.1-dev License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -315,6 +315,7 @@ See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-saml-auth/blob/mast == Changelog == = 1.4.1-dev = +* Send the REST API response header to the result and not the REST server [[#237](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/237)]. Props [@srtfisher](https://github.com/srtfisher) & [@felixarntz](https://github.com/felixarntz). = 1.4.0 = * Bumped Dependencies [[236](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/236)] diff --git a/tests/phpunit/test-emitter-rest-api.php b/tests/phpunit/test-emitter-rest-api.php index 2d75397b..5c536010 100644 --- a/tests/phpunit/test-emitter-rest-api.php +++ b/tests/phpunit/test-emitter-rest-api.php @@ -610,4 +610,61 @@ public function test_get_settings() { ); } + /** + * Test headers set on a REST API response. + */ + public function test_rest_api_response_headers() { + // Add the filter to include the test key + add_filter('pantheon_wp_rest_api_surrogate_keys', function ($keys) { + $keys[] = 'test-key'; + return $keys; + }); + + // Create a new instance of WP_REST_Response + $response = new WP_REST_Response( [ 'foo' => 'bar' ] ); + $server = rest_get_server(); + + // Fire up the filter to capture the headers from the response. + $rest_post_dispatch = Emitter::filter_rest_post_dispatch( $response, $server ); + $actual_headers = $rest_post_dispatch->headers; + + // Define your expected surrogate keys + $expected_surrogate_keys = Emitter::get_rest_api_surrogate_keys(); + + // Verify that the headers were set correctly + $this->assertContains( 'test-key', $expected_surrogate_keys ); + $this->assertArrayHasKey( 'Surrogate-Key', $actual_headers ); + $this->assertEquals( $actual_headers['Surrogate-Key'], implode( ' ', $expected_surrogate_keys ) ); + } + + /** + * Test headers set on a GraphQL response. + */ + public function test_graphql_response_headers() { + // Set up the mock response array with the existing headers set by the GraphQL plugin + $existing_headers = [ + 'header1' => 'value1', + 'header2' => 'value2', + ]; + + // Simulate adding a surrogate key via a filter + add_filter('pantheon_wp_graphql_surrogate_keys', function ($keys) { + $keys[] = 'test-key'; + return $keys; + }); + + // Call the filter_graphql_response_headers_to_send() method + $result_headers = Emitter::filter_graphql_response_headers_to_send($existing_headers); + + $graphql_collection_key = is_multisite() ? 'blog-1-graphql-collection' : 'graphql-collection'; + $expected_headers = array_merge( $existing_headers, [ 'Surrogate-Key' => "$graphql_collection_key test-key" ] ); + // Verify that the existing headers are preserved + $this->assertEquals($expected_headers, $result_headers); + + // Verify that the 'Surrogate-Key' header was added with the expected value + $expected_surrogate_keys = Emitter::get_graphql_surrogate_keys(); + $this->assertArrayHasKey(Emitter::HEADER_KEY, $result_headers); + $this->assertEquals(implode(' ', $expected_surrogate_keys), $result_headers[Emitter::HEADER_KEY]); + $this->assertContains( 'test-key', $expected_surrogate_keys ); + } } From d0a595e1fa2a249970ac5ad934eaf7113434834f Mon Sep 17 00:00:00 2001 From: Chris Reynolds Date: Tue, 8 Aug 2023 11:48:07 -0600 Subject: [PATCH 8/8] Release 1.4.1 --- README.md | 4 ++-- package.json | 2 +- pantheon-advanced-page-cache.php | 4 ++-- readme.txt | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3f6e42fc..432c8328 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Tags:** pantheon, cdn, cache **Requires at least:** 4.7 **Tested up to:** 6.3 -**Stable tag:** 1.4.1-dev +**Stable tag:** 1.4.1 **License:** GPLv2 or later **License URI:** http://www.gnu.org/licenses/gpl-2.0.html @@ -321,7 +321,7 @@ See [CONTRIBUTING.md](https://github.com/pantheon-systems/pantheon-advanced-page ## Changelog ## -### 1.4.1-dev ### +### 1.4.1 ### * Send the REST API response header to the result and not the REST server [[#237](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/237)]. Props [@srtfisher](https://github.com/srtfisher) & [@felixarntz](https://github.com/felixarntz). ### 1.4.0 ### diff --git a/package.json b/package.json index 2cc7db0b..f77ab2b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pantheon-advanced-page-cache", - "version": "1.4.1-dev", + "version": "1.4.1", "main": "Gruntfile.js", "author": "Pantheon", "devDependencies": { diff --git a/pantheon-advanced-page-cache.php b/pantheon-advanced-page-cache.php index e096e063..3391396c 100644 --- a/pantheon-advanced-page-cache.php +++ b/pantheon-advanced-page-cache.php @@ -7,7 +7,7 @@ * Author URI: https://pantheon.io * Text Domain: pantheon-advanced-page-cache * Domain Path: /languages - * Version: 1.4.1-dev + * Version: 1.4.1 * * @package Pantheon_Advanced_Page_Cache */ @@ -79,7 +79,7 @@ function pantheon_wp_clear_edge_all() { /** * Prefix surrogate keys with the blog ID to provide compatibility with WPMS. See https://github.com/pantheon-systems/pantheon-advanced-page-cache/issues/196. - * + * * @param array $keys Keys to be prefixed. */ function pantheon_wp_prefix_surrogate_keys_with_blog_id( $keys ) { diff --git a/readme.txt b/readme.txt index 4e155a47..a33e1aa7 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: getpantheon, danielbachhuber, kporras07, jspellman, jazzs3quence, Tags: pantheon, cdn, cache Requires at least: 4.7 Tested up to: 6.3 -Stable tag: 1.4.1-dev +Stable tag: 1.4.1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -314,7 +314,7 @@ See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-saml-auth/blob/mast == Changelog == -= 1.4.1-dev = += 1.4.1 = * Send the REST API response header to the result and not the REST server [[#237](https://github.com/pantheon-systems/pantheon-advanced-page-cache/pull/237)]. Props [@srtfisher](https://github.com/srtfisher) & [@felixarntz](https://github.com/felixarntz). = 1.4.0 =