From 3625820ccf760ddfdb389b8778283d909b6fe4f0 Mon Sep 17 00:00:00 2001
From: Henrik Wirth
Date: Tue, 17 Nov 2020 14:44:53 +0100
Subject: [PATCH] Adds WP VIP fixes according to their coding standards. (#72)
* Adds WP VIP fixes according to their coding standards.
* Change function order to fix error
Co-authored-by: Ash Hitchcock
---
wp-graphql-yoast-seo.php | 57 +++++++++++++++++++++++++++++++++-------
1 file changed, 47 insertions(+), 10 deletions(-)
diff --git a/wp-graphql-yoast-seo.php b/wp-graphql-yoast-seo.php
index 4d60a91..fcfea18 100755
--- a/wp-graphql-yoast-seo.php
+++ b/wp-graphql-yoast-seo.php
@@ -1,4 +1,4 @@
-
+ ); ?>
+
@@ -77,7 +78,7 @@ function wp_gql_seo_get_og_image($images)
return __return_empty_string();
}
- return attachment_url_to_postid($image['url']);
+ return wpcom_vip_attachment_url_to_postid($image['url']);
}
}
if (!function_exists('wp_gql_seo_get_field_key')) {
@@ -92,6 +93,43 @@ function wp_gql_seo_get_field_key($field_key)
}
}
+ if (!function_exists('wpcom_vip_attachment_url_to_postid')) {
+ function wpcom_vip_attachment_cache_key($url)
+ {
+ return 'wpcom_vip_attachment_url_post_id_' . md5($url);
+ }
+ }
+
+ if (!function_exists('wpcom_vip_attachment_url_to_postid')) {
+ function wpcom_vip_attachment_url_to_postid($url)
+ {
+ $cache_key = wpcom_vip_attachment_cache_key($url);
+ $id = wp_cache_get($cache_key);
+ if (false === $id) {
+ $id = attachment_url_to_postid($url); // phpcs:ignore
+ if (empty($id)) {
+ wp_cache_set(
+ $cache_key,
+ 'not_found',
+ 'default',
+ 12 * HOUR_IN_SECONDS + mt_rand(0, 4 * HOUR_IN_SECONDS) // phpcs:ignore
+ );
+ } else {
+ wp_cache_set(
+ $cache_key,
+ $id,
+ 'default',
+ 24 * HOUR_IN_SECONDS + mt_rand(0, 12 * HOUR_IN_SECONDS) // phpcs:ignore
+ );
+ }
+ } elseif ('not_found' === $id) {
+ return false;
+ }
+
+ return $id;
+ }
+ }
+
function wp_gql_seo_build_content_types($types)
{
$carry = [];
@@ -458,10 +496,9 @@ function wp_gql_seo_build_content_type_data($types, $all)
'social' => [
'facebook' => [
'url' => wp_gql_seo_format_string($all['facebook_site']),
- 'defaultImage' => DataSource::resolve_post_object(
- $all['og_default_image_id'],
- $context
- ),
+ 'defaultImage' => $context
+ ->get_loader('post')
+ ->load_deferred($all['og_default_image_id']),
],
'twitter' => [
'username' => wp_gql_seo_format_string(
@@ -688,7 +725,7 @@ function wp_gql_seo_build_content_type_data($types, $all)
->twitter_description
),
'twitterImage' => DataSource::resolve_post_object(
- attachment_url_to_postid(
+ wpcom_vip_attachment_url_to_postid(
YoastSEO()->meta->for_post($post->ID)
->twitter_image
),
@@ -858,7 +895,7 @@ function wp_gql_seo_build_content_type_data($types, $all)
$seo = [
'title' => wp_gql_seo_format_string(
html_entity_decode(
- strip_tags(
+ wp_strip_all_tags(
YoastSEO()->meta->for_term($term->term_id)
->title
)