From 6258c15930094c6c43ff7b6a4da3aeca8e6509cf Mon Sep 17 00:00:00 2001 From: NiclasNorin Date: Tue, 31 Oct 2023 12:12:13 +0100 Subject: [PATCH 1/5] feat: clear oembed cache --- library/Content/Cache.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/Content/Cache.php b/library/Content/Cache.php index 08d77d90d..e3aafb923 100644 --- a/library/Content/Cache.php +++ b/library/Content/Cache.php @@ -17,5 +17,15 @@ public function __construct() $post = get_post($postId); \Municipio\Helper\Cache::clearCache($postId, $post); }); + + $this->clearWPOembedCache(); + } + + public function clearWPOembedCache() { + global $wpdb; + if (empty(get_option('cleared_wp_oembed_cache'))) { + $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%' AND meta_value LIKE ' Date: Tue, 31 Oct 2023 13:36:06 +0100 Subject: [PATCH 2/5] Feat: clear cache on post save --- library/Content/Cache.php | 10 ---------- library/Helper/Cache.php | 4 ++++ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/library/Content/Cache.php b/library/Content/Cache.php index e3aafb923..08d77d90d 100644 --- a/library/Content/Cache.php +++ b/library/Content/Cache.php @@ -17,15 +17,5 @@ public function __construct() $post = get_post($postId); \Municipio\Helper\Cache::clearCache($postId, $post); }); - - $this->clearWPOembedCache(); - } - - public function clearWPOembedCache() { - global $wpdb; - if (empty(get_option('cleared_wp_oembed_cache'))) { - $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%' AND meta_value LIKE 'post_type, self::getKeyGroup()); + $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%' AND post_id LIKE {$postId}"); + // Empty post type for all sites in network (?) if (function_exists('is_multisite') && is_multisite() && apply_filters('Municipio\Cache\EmptyForAllBlogs', false, $post)) { $blogs = get_sites(); From 0beed9c99975a4f7c4b1319d32835ecfcb12b85f Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Tue, 31 Oct 2023 14:33:33 +0100 Subject: [PATCH 3/5] Fix: safety first --- library/Helper/Cache.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/library/Helper/Cache.php b/library/Helper/Cache.php index 386c1542f..2075d020d 100644 --- a/library/Helper/Cache.php +++ b/library/Helper/Cache.php @@ -68,8 +68,6 @@ public static function getKeyGroup($blogId = null) */ public static function clearCache($postId, $post) { - global $wpdb; - if (wp_is_post_revision($postId) || get_post_status($postId) != 'publish') { return false; } @@ -77,7 +75,7 @@ public static function clearCache($postId, $post) wp_cache_delete($postId, self::getKeyGroup()); wp_cache_delete($post->post_type, self::getKeyGroup()); - $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_oembed_%' AND post_id LIKE {$postId}"); + self::clearOembedCache($postId); // Empty post type for all sites in network (?) if (function_exists('is_multisite') && is_multisite() && apply_filters('Municipio\Cache\EmptyForAllBlogs', false, $post)) { @@ -91,6 +89,19 @@ public static function clearCache($postId, $post) return true; } + public function clearOembedCache($postId) { + global $wpdb; + + $metaKeyPattern = '_oembed_%'; + $wpdb->query( + $wpdb->prepare( + "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s AND post_id = %d", + $metaKeyPattern, + $postId + ) + ); + } + /** * Starts the "cache engine" * @return boolean Returns true if engine started or inactivated, returns false if previous cache is loaded From 5dba23748934c42aed312373a56a9628b0d82961 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Tue, 31 Oct 2023 14:35:00 +0100 Subject: [PATCH 4/5] Fix: naming --- library/Helper/Cache.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/Helper/Cache.php b/library/Helper/Cache.php index 2075d020d..eac7205d8 100644 --- a/library/Helper/Cache.php +++ b/library/Helper/Cache.php @@ -89,7 +89,11 @@ public static function clearCache($postId, $post) return true; } - public function clearOembedCache($postId) { + /** + * Clears Oembed cache + * @param integer $postId Post id to clear + */ + public function clearOembedCache(int $postId) { global $wpdb; $metaKeyPattern = '_oembed_%'; From 295ca2d5f1e31e4bad13cde667850cc6980e4939 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Tue, 31 Oct 2023 14:44:44 +0100 Subject: [PATCH 5/5] fix: static --- library/Helper/Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Helper/Cache.php b/library/Helper/Cache.php index eac7205d8..ee33d6aef 100644 --- a/library/Helper/Cache.php +++ b/library/Helper/Cache.php @@ -93,7 +93,7 @@ public static function clearCache($postId, $post) * Clears Oembed cache * @param integer $postId Post id to clear */ - public function clearOembedCache(int $postId) { + public static function clearOembedCache(int $postId) { global $wpdb; $metaKeyPattern = '_oembed_%';