Skip to content

Commit af6c094

Browse files
committed
Merge branch 'master' of github.com:ashhitch/wp-graphql-yoast-seo
2 parents 6840951 + fa59f84 commit af6c094

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

includes/helpers/functions.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function wp_gql_seo_get_og_image($images)
6464
if (isset($image['id']) && $url === $image['url']) {
6565
return $image['id'];
6666
}
67-
return wpcom_vip_attachment_url_to_postid($url);
67+
return wp_gql_seo_attachment_url_to_postid($url);
6868
}
6969

7070
/**
@@ -85,48 +85,69 @@ function wp_gql_seo_get_field_key($field_key)
8585

8686
/**
8787
* Generate cache key for attachment URL.
88+
* Only declare if not already defined (e.g., by VIP platform).
8889
*
8990
* @param string $url URL to generate cache key for.
9091
* @return string
9192
*/
92-
function wpcom_vip_attachment_cache_key($url)
93-
{
94-
return 'wpcom_vip_attachment_url_post_id_' . md5($url);
93+
if (!function_exists('wpcom_vip_attachment_cache_key')) {
94+
function wpcom_vip_attachment_cache_key($url)
95+
{
96+
return 'wpcom_vip_attachment_url_post_id_' . md5($url);
97+
}
9598
}
9699

97100
/**
98101
* Get post ID from attachment URL with caching.
102+
* Only declare if not already defined (e.g., by VIP platform).
99103
*
100104
* @param string $url URL to get post ID for.
101105
* @return int|null
102106
*/
103-
function wpcom_vip_attachment_url_to_postid($url)
104-
{
105-
$cache_key = wpcom_vip_attachment_cache_key($url);
106-
$id = wp_cache_get($cache_key);
107-
if (false === $id) {
108-
$id = attachment_url_to_postid($url); // phpcs:ignore
109-
if (empty($id)) {
110-
wp_cache_set(
111-
$cache_key,
112-
'not_found',
113-
'default',
114-
12 * HOUR_IN_SECONDS + mt_rand(0, 4 * HOUR_IN_SECONDS) // phpcs:ignore
115-
);
116-
$id = null; // Set $id to null instead of false
117-
} else {
118-
wp_cache_set(
119-
$cache_key,
120-
$id,
121-
'default',
122-
24 * HOUR_IN_SECONDS + mt_rand(0, 12 * HOUR_IN_SECONDS) // phpcs:ignore
123-
);
107+
if (!function_exists('wpcom_vip_attachment_url_to_postid')) {
108+
function wpcom_vip_attachment_url_to_postid($url)
109+
{
110+
$cache_key = wpcom_vip_attachment_cache_key($url);
111+
$id = wp_cache_get($cache_key);
112+
if (false === $id) {
113+
$id = attachment_url_to_postid($url); // phpcs:ignore
114+
if (empty($id)) {
115+
wp_cache_set(
116+
$cache_key,
117+
'not_found',
118+
'default',
119+
12 * HOUR_IN_SECONDS + mt_rand(0, 4 * HOUR_IN_SECONDS) // phpcs:ignore
120+
);
121+
$id = null; // Set $id to null instead of false
122+
} else {
123+
wp_cache_set(
124+
$cache_key,
125+
$id,
126+
'default',
127+
24 * HOUR_IN_SECONDS + mt_rand(0, 12 * HOUR_IN_SECONDS) // phpcs:ignore
128+
);
129+
}
130+
} elseif ('not_found' === $id) {
131+
return false;
124132
}
125-
} elseif ('not_found' === $id) {
126-
return null; // Return null instead of false
133+
134+
return $id;
127135
}
136+
}
128137

129-
return $id;
138+
/**
139+
* Wrapper for wpcom_vip_attachment_url_to_postid that returns null instead of false.
140+
* This is needed for GraphQL compatibility - fixes
141+
* "Cannot return null for non-nullable field 'MediaItem.id'" errors.
142+
*
143+
* @see https://github.com/ashhitch/wp-graphql-yoast-seo/issues/132
144+
* @param string $url URL to get post ID for.
145+
* @return int|null
146+
*/
147+
function wp_gql_seo_attachment_url_to_postid($url)
148+
{
149+
$id = wpcom_vip_attachment_url_to_postid($url);
150+
return (false === $id || empty($id)) ? null : $id;
130151
}
131152

132153
/**

includes/resolvers/post-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function wp_gql_seo_get_post_type_graphql_fields($post, array $args, AppContext
7373
return null;
7474
}
7575

76-
$id = wpcom_vip_attachment_url_to_postid($twitter_image);
76+
$id = wp_gql_seo_attachment_url_to_postid($twitter_image);
7777

7878
return $context->get_loader('post')->load_deferred(absint($id));
7979
},

0 commit comments

Comments
 (0)