=== WP REST Cache ===
Contributors: acato, rockfire, yoeridekker
Tags: cache, wp-rest, wp-rest-api, api, rest, rest cache, rest api cache
Requires at least: 4.7
Tested up to: 5.1
Requires PHP: 5.5
Stable tag: trunk
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl.html

Enable caching of the WordPress REST API and auto-flush caches upon wp-admin editing.

== Description ==

Are you facing speed issues, using the WordPress REST API? This plugin will allow WordPress to cache the responses of the REST API, making it much faster.

This plugin offers:

* Caching of all default WordPress REST API `GET`-endpoints.
* Caching of (custom) post type endpoints.
* Caching of (custom) taxonomy endpoints.
* Automated flushing of caches if (some of) its contents are edited.
* Manual flushing of all caches.
* Manual flushing of specific caches.
* A counter how many times a cache has been retrieved.
* Specifying after what time the cache should be timed out.
* Registering custom endpoints for caching.

== Installation ==

=== Installation from within WordPress ===

1. Visit 'Plugins > Add New' (or 'My Sites > Network Admin > Plugins > Add New' if you are on a multisite installation).
1. Search for 'WP REST Cache'.
1. Activate the WP REST Cache plugin through the 'Plugins' menu in WordPress.
1. Go to "after activation" below.

=== Installation manually ===

1. Upload the `wp-rest-cache` folder to the `/wp-content/plugins/` directory.
1. Activate the WP REST Cache plugin through the 'Plugins' menu in WordPress.
1. Go to "after activation" below.

=== After activation ===

1. Visit 'Plugins > Must-Use' (or 'My Sites > Network Admin > Plugins > Must-Use' if you are on a multisite installation).
1. Check if the 'WP REST Cache - Must-Use Plugin' is there, if not copy the file `wp-rest-cache.php` from the `/sources` folder of the WP REST Cache Plugin to the folder `/wp-content/mu-plugins/`.

**Optionally:**
The default timeout for caches generated by the WP REST Cache plugin is set to 1 year. If you want to change this:

1. Visit 'Settings > WP REST Cache'.
1. Change the Cache timeout.

== Frequently Asked Questions ==

= I have edited a page/post, do I need to clear the cache? =

No, the plugin will automatically flush all cache related to the page/post you just edited.

= I have created a custom post type, will the plugin cache the custom post type endpoint? =

Yes, the plugin will automatically cache the endpoint of custom post types. Unless you have created a custom WP_REST_Controller for it, then it will not automatically cache the endpoint.

= I have created a custom taxonomy, will the plugin cache the taxonomy endpoint? =

Yes, the plugin will automatically cache the endpoint of custom taxonomies. Unless you have created a custom WP_REST_Controller for it, then it will not automatically cache the endpoint.

= I have created a custom WP REST endpoint, will the plugin cache this endpoint? =

No, the plugin will not cache your custom endpoint unless you tell it to cache it using the hook `wp_rest_cache/allowed_endpoints` (See 'Can I register my own endpoint for caching?'). Please keep in mind that once you do so the plugin will not automatically flush the cache of that endpoint if something is edited (it has no way of knowing when to flush the cache). It will however try to determine the relations and for the determined relations it will flush the cache automatically once the relation is edited.

= Can I register my own endpoint for caching? =

Yes you can! Use the hook `wp_rest_cache/allowed_endpoints` like this:

`/**
 * Register the /wp-json/acf/v3/posts endpoint so it will be cached.
 */
function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
    if ( ! isset( $allowed_endpoints[ 'acf/v3' ] ) || ! in_array( 'posts', $allowed_endpoints[ 'acf/v3' ] ) ) {
        $allowed_endpoints[ 'acf/v3' ][] = 'posts';
    }
    return $allowed_endpoints;
}
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);`

*Please note:* the WP REST Cache plugin will try to detect relations in the cached data to automatically flush the cache when related items are edited, but this detection is not flawless so your caches might not be flushed automatically.

= Can I unregister an endpoint so it is no longer cached? =

Yes you can! Use the hook `wp_rest_cache/allowed_endpoints` like this:

`/**
 * Unregister the /wp-json/wp/v2/comments endpoint so it will not be cached.
 */
function wprc_unregister_wp_comments_endpoint( $allowed_endpoints ) {
    if ( isset( $allowed_endpoints[ 'wp/v2' ] ) && ( $key = array_search( 'comments', $allowed_endpoints[ 'wp/v2' ] ) ) !== false ) {
        unset( $allowed_endpoints[ 'wp/v2' ][ $key ] );
    }
    return $allowed_endpoints;
}
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_unregister_wp_comments_endpoint', 100, 1);`

= Can I force a call to the REST API to not use caching? =

Yes you can! Add the GET-parameter `skip_cache=1` to your call and no caching will be used.

= On the cache overview page I see the object type is 'unknown'. Can I help the WP REST Cache plugin to detect the object type correctly? =

Yes you can! Use the hook `wp_rest_cache/determine_object_type` like this:

`function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
    if ( $object_type !== 'unknown' || strpos( $uri, $this->namespace . '/' . $this->rest_base ) === false ) {
        return $object_type;
    }
    // Do your magic here
    $object_type = 'website';
    // Do your magic here
    return $object_type;
}
add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );`

== Screenshots ==

1. Settings for the WP REST Cache plugin.
2. An overview of cached endpoint calls.
3. An overview of cached single items.
4. Cache details page - Cache info.
5. Cache details page - Cache data.

== Changelog ==

= 2019.2.1 =
Release Date: April 15th, 2019
Feature: Added option to skip cache using a parameter.

= 2019.2.0 =
Release Date: April 2nd, 2019

Feature: Added function to programatically flush cache records by endpoint path.
Bugfix: Fix correct filtering of allowed endpoints.
Bugfix: Fix fatal error with object in stead of array in cache.

= 2019.1.6 =
Release Date: March 25th, 2019

Feature: Added filters for response header manipulation.

= 2019.1.4 =
Release Date: March 21st, 2019

Bugfix: bug in saving relations for comments endpoint prevented the cache for comments to be flushed automatically.

= 2019.1.3 =
Release Date: February 13th, 2019

Feature: Added support for correctly flushing caches of scheduled posts.

= 2019.1.2 =
Release Date: January 31st, 2019

First public version.
