Skip to content

Commit 68f5c09

Browse files
committed
Don't require that recipe HTML contain JSON-LD 'name'
1 parent ec79887 commit 68f5c09

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

includes/apple-exporter/components/class-recipe.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ private function matching_schema( $recipe ): ?array {
791791
$items = self::recipe_items( $post_content, 'body' );
792792

793793
foreach ( $items as $item ) {
794-
if ( self::is_recipe_item_for_recipe_html( $item, $recipe ) ) {
794+
if ( self::is_allowed_recipe_item( $item ) ) {
795795
return $item;
796796
}
797797
}
@@ -815,7 +815,7 @@ private function matching_schema( $recipe ): ?array {
815815
$items = self::recipe_items( $html, 'head' );
816816

817817
foreach ( $items as $item ) {
818-
if ( self::is_recipe_item_for_recipe_html( $item, $recipe ) ) {
818+
if ( self::is_allowed_recipe_item( $item ) ) {
819819
return $item;
820820
}
821821
}
@@ -901,19 +901,17 @@ private static function recipe_items_in_graph( array $carry, array $json ) {
901901
}
902902

903903
/**
904-
* Best-guess if the given schema item is for the given recipe HTML.
904+
* Confirm that the given schema item can be used for recipe HTML.
905905
*
906-
* @param array $schema The schema item to check.
907-
* @param string $html The recipe HTML to check.
908-
* @return bool True if the schema item is for the recipe HTML, false otherwise.
906+
* @param array $schema The schema item to check.
907+
* @return bool
909908
*/
910-
private static function is_recipe_item_for_recipe_html( array $schema, string $html ) {
909+
private static function is_allowed_recipe_item( array $schema ) {
910+
// This method used to confirm that the schema 'name' appeared in the HTML, but this was found to be too restrictive.
911911
return (
912912
isset( $schema['name'] )
913913
&& is_string( $schema['name'] )
914914
&& strlen( $schema['name'] ) > 0
915-
// The recipe HTML would have the name of any recipe JSON-LD items found within it, so strip tags to avoid false positives.
916-
&& str_contains( wp_strip_all_tags( $html ), $schema['name'] )
917915
);
918916
}
919917

tests/apple-exporter/components/test-class-recipe.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,8 @@ public function test_recipe_component_find_schema_in_graph(): void {
309309
"@context": "https:\/\/schema.org",
310310
"@graph": [
311311
{
312-
"@type": "Recipe",
313-
"@id": "https:\/\/www.example.com\/recipes\/apple-pie\/",
314-
"name": "Apple Pie"
312+
"@type": "VideoObject",
313+
"@id": "https:\/\/www.example.com\/recipes\/apple-pie\/video"
315314
},
316315
{
317316
"@type": "ImageObject",

0 commit comments

Comments
 (0)