Created
September 15, 2022 09:47
-
-
Save mawiswiss/f7dc557caaefc9a3a393fd358d5aedc8 to your computer and use it in GitHub Desktop.
Clear WP REST API Cache on certain interactions from WP Admin Panel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use WP_Rest_Cache_Plugin\Includes\Caching\Caching; | |
| class WpRestCache | |
| { | |
| public function __construct() | |
| { | |
| add_filter('wp_rest_cache/allowed_endpoints', [$this, 'cacheEndpoints'], 10, 1); | |
| add_filter('wp_rest_cache/determine_object_type', [$this, 'determinteObjectType'], 10, 4); | |
| add_action('acf/save_post', [$this, 'handleCacheOnSaveOptions'], 10, 1); | |
| add_action('pll_save_strings_translations', [$this, 'handleOnSaveTranslation']); | |
| add_action('transition_post_status', [$this, 'handleCacheOnPostSchedule'], 10, 3); | |
| } | |
| public function cacheEndpoints($allowed_endpoints) | |
| { | |
| if (!isset($allowed_endpoints['NAMESPACE/v1']) || !in_array('options', $allowed_endpoints['NAMESPACE/v1'])) { | |
| $allowed_endpoints['NAMESPACE/v1'][] = 'options'; | |
| } | |
| return $allowed_endpoints; | |
| } | |
| public function determinteObjectType($object_type, $cache_key, $data, $uri) | |
| { | |
| if ('unknown' !== $object_type || strpos($uri, '/wp-json/NAMESPACE/v1/options') === false) { | |
| return $object_type; | |
| } | |
| if (false !== strpos($uri, '/wp-json/NAMESPACE/v1/options')) { | |
| $object_type = 'options'; | |
| } | |
| return $object_type; | |
| } | |
| public function handleCacheOnSaveOptions($post_id) | |
| { | |
| self::deleteOptionsCache(); | |
| } | |
| public function handleOnSaveTranslation() | |
| { | |
| self::deleteOptionsCache(); | |
| } | |
| public function handleCacheOnPostSchedule($new, $old, $post) | |
| { | |
| if ($post->post_type === 'post' && $new === 'publish' && $old === 'future') { | |
| self::deletePostArchiveCache(); | |
| self::deleteHomepagesCache(); | |
| } | |
| } | |
| public static function deleteOptionsCache() | |
| { | |
| if (class_exists(Caching::class)) { | |
| Caching::get_instance()->delete_object_type_caches('options'); | |
| } | |
| } | |
| public static function deleteCompleteCache() | |
| { | |
| if (class_exists(Caching::class)) { | |
| Caching::get_instance()->delete_all_caches(false); | |
| } | |
| } | |
| public static function deletePageAndPostCache() | |
| { | |
| if (class_exists(Caching::class)) { | |
| Caching::get_instance()->delete_object_type_caches('post'); | |
| Caching::get_instance()->delete_object_type_caches('page'); | |
| } | |
| } | |
| public static function deletePostArchiveCache() | |
| { | |
| if (class_exists(Caching::class)) { | |
| Caching::get_instance()->delete_cache_by_endpoint('/wp-json/wp/v2/posts', Caching::FLUSH_PARAMS); | |
| } | |
| } | |
| public static function deleteHomepagesCache() | |
| { | |
| // The frontend uses the data from the option instead of the frontpage itself | |
| self::deleteOptionsCache(); | |
| // We run this just in case | |
| if (class_exists(Caching::class)) { | |
| $languages = pll_languages_list([ | |
| 'fields' => 'slug', | |
| ]); | |
| $homeId = get_option('page_on_front'); | |
| foreach ($languages as $lang) { | |
| $postId = pll_get_post($homeId, $lang); | |
| Caching::get_instance()->delete_cache_by_endpoint('/wp-json/wp/v2/pages/' . $postId); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment