| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2016-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoJsonTypeRecipe' ) ) { |
| 14 |
|
| 15 |
class WpssoJsonTypeRecipe { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
/* |
| 20 |
* Instantiated by Wpsso->init_json_filters(). |
| 21 |
*/ |
| 22 |
public function __construct( &$plugin ) { |
| 23 |
|
| 24 |
$this->p =& $plugin; |
| 25 |
|
| 26 |
if ( $this->p->debug->enabled ) { |
| 27 |
|
| 28 |
$this->p->debug->mark(); |
| 29 |
} |
| 30 |
|
| 31 |
$this->p->util->add_plugin_filters( $this, array( |
| 32 |
'json_data_https_schema_org_recipe' => 5, |
| 33 |
) ); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* See https://developers.google.com/search/docs/appearance/structured-data/recipe. |
| 38 |
*/ |
| 39 |
public function filter_json_data_https_schema_org_recipe( $json_data, $mod, $mt_og, $page_type_id, $is_main ) { |
| 40 |
|
| 41 |
if ( $this->p->debug->enabled ) { |
| 42 |
|
| 43 |
$this->p->debug->mark(); |
| 44 |
} |
| 45 |
|
| 46 |
$json_ret = array(); |
| 47 |
$md_opts = array(); |
| 48 |
|
| 49 |
WpssoSchema::add_type_opts_md_pad( $md_opts, $mod ); |
| 50 |
|
| 51 |
/* |
| 52 |
* See https://schema.org/recipeCuisine. |
| 53 |
*/ |
| 54 |
if ( ! empty( $md_opts[ 'schema_recipe_cuisine' ] ) ) { |
| 55 |
|
| 56 |
$json_ret[ 'recipeCuisine' ] = (string) $md_opts[ 'schema_recipe_cuisine' ]; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* See https://schema.org/recipeCategory. |
| 61 |
*/ |
| 62 |
if ( ! empty( $md_opts[ 'schema_recipe_course' ] ) ) { |
| 63 |
|
| 64 |
$json_ret[ 'recipeCategory' ] = (string) $md_opts[ 'schema_recipe_course' ]; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* See https://schema.org/recipeYield. |
| 69 |
*/ |
| 70 |
if ( ! empty( $md_opts[ 'schema_recipe_yield' ] ) ) { |
| 71 |
|
| 72 |
$json_ret[ 'recipeYield' ] = (string) $md_opts[ 'schema_recipe_yield' ]; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* See https://schema.org/cookingMethod. |
| 77 |
*/ |
| 78 |
if ( ! empty( $md_opts[ 'schema_recipe_cook_method' ] ) ) { |
| 79 |
|
| 80 |
$json_ret[ 'cookingMethod' ] = (string) $md_opts[ 'schema_recipe_cook_method' ]; |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* See https://schema.org/prepTime. |
| 85 |
* See https://schema.org/cookTime. |
| 86 |
* See https://schema.org/totalTime. |
| 87 |
*/ |
| 88 |
WpssoSchema::add_data_time_from_assoc( $json_ret, $md_opts, array( |
| 89 |
'prepTime' => 'schema_recipe_prep', |
| 90 |
'cookTime' => 'schema_recipe_cook', |
| 91 |
'totalTime' => 'schema_recipe_total', |
| 92 |
) ); |
| 93 |
|
| 94 |
/* |
| 95 |
* See https://schema.org/recipeIngredient. |
| 96 |
*/ |
| 97 |
$recipe_ingredients = SucomUtil::preg_grep_keys( '/^schema_recipe_ingredient_([0-9])+$/', $md_opts, $invert = false, $replace = '$1' ); |
| 98 |
|
| 99 |
foreach ( $recipe_ingredients as $md_num => $md_val ) { |
| 100 |
|
| 101 |
$json_ret[ 'recipeIngredient' ][] = $md_val; |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* See https://schema.org/recipeInstructions. |
| 106 |
*/ |
| 107 |
WpssoSchema::add_howto_steps_data( $json_ret, $mod, $md_opts, $opt_pre = 'schema_recipe_instruction', $prop_name = 'recipeInstructions' ); |
| 108 |
|
| 109 |
/* |
| 110 |
* See https://schema.org/nutrition as https://schema.org/NutritionInformation |
| 111 |
*/ |
| 112 |
if ( ! empty( $md_opts[ 'schema_recipe_nutri_serv' ] ) ) { // serving size is required |
| 113 |
|
| 114 |
if ( false !== ( $nutrition = WpssoSchema::get_data_itemprop_from_assoc( $md_opts, array( |
| 115 |
'servingSize' => 'schema_recipe_nutri_serv', |
| 116 |
'calories' => 'schema_recipe_nutri_cal', |
| 117 |
'proteinContent' => 'schema_recipe_nutri_prot', |
| 118 |
'fiberContent' => 'schema_recipe_nutri_fib', |
| 119 |
'carbohydrateContent' => 'schema_recipe_nutri_carb', |
| 120 |
'sugarContent' => 'schema_recipe_nutri_sugar', |
| 121 |
'sodiumContent' => 'schema_recipe_nutri_sod', |
| 122 |
'fatContent' => 'schema_recipe_nutri_fat', |
| 123 |
'saturatedFatContent' => 'schema_recipe_nutri_sat_fat', |
| 124 |
'unsaturatedFatContent' => 'schema_recipe_nutri_unsat_fat', |
| 125 |
'transFatContent' => 'schema_recipe_nutri_trans_fat', |
| 126 |
'cholesterolContent' => 'schema_recipe_nutri_chol', |
| 127 |
) ) ) ) { |
| 128 |
|
| 129 |
self::add_nutrition_measures( $nutrition ); |
| 130 |
|
| 131 |
$json_ret[ 'nutrition' ] = WpssoSchema::get_schema_type_context( 'https://schema.org/NutritionInformation', $nutrition ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if ( $this->p->debug->enabled ) { |
| 136 |
|
| 137 |
$this->p->debug->log( 'merging json_data and json_ret arrays' ); |
| 138 |
} |
| 139 |
|
| 140 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 141 |
} |
| 142 |
|
| 143 |
private static function add_nutrition_measures( array &$nutrition ) { |
| 144 |
|
| 145 |
$measures = array( |
| 146 |
'calories' => 'calories', |
| 147 |
'proteinContent' => 'grams protein', |
| 148 |
'fiberContent' => 'grams fiber', |
| 149 |
'carbohydrateContent' => 'grams carbohydrates', |
| 150 |
'sugarContent' => 'grams sugar', |
| 151 |
'sodiumContent' => 'milligrams sodium', |
| 152 |
'fatContent' => 'grams fat', |
| 153 |
'saturatedFatContent' => 'grams saturated fat', |
| 154 |
'unsaturatedFatContent' => 'grams unsaturated fat', |
| 155 |
'transFatContent' => 'grams trans fat', |
| 156 |
'cholesterolContent' => 'milligrams cholesterol', |
| 157 |
); |
| 158 |
|
| 159 |
foreach ( $nutrition as $prop_name => &$prop_val ) { // Update value by reference. |
| 160 |
|
| 161 |
if ( isset( $measures[ $prop_name ] ) ) { |
| 162 |
|
| 163 |
$prop_val .= ' ' . $measures[ $prop_name ]; // Add measure unit. |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|