| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-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( 'WpssoIntegRecipeWpRecipeMaker' ) ) { |
| 14 |
|
| 15 |
class WpssoIntegRecipeWpRecipeMaker { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
public function __construct( &$plugin ) { |
| 20 |
|
| 21 |
$this->p =& $plugin; |
| 22 |
|
| 23 |
if ( $this->p->debug->enabled ) { |
| 24 |
|
| 25 |
$this->p->debug->mark(); |
| 26 |
} |
| 27 |
|
| 28 |
$this->p->util->add_plugin_filters( $this, array( |
| 29 |
'get_post_options' => 3, |
| 30 |
'save_post_options' => 3, |
| 31 |
) ); |
| 32 |
|
| 33 |
add_filter( 'wprm_recipe_metadata', '__return_empty_array', PHP_INT_MAX ); |
| 34 |
} |
| 35 |
|
| 36 |
public function filter_get_post_options( array $md_opts, $post_id, array $mod ) { |
| 37 |
|
| 38 |
if ( $this->p->debug->enabled ) { |
| 39 |
|
| 40 |
$this->p->debug->mark(); |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! $recipe_id = $this->get_recipe_id( $post_id ) ) { |
| 44 |
|
| 45 |
return $md_opts; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* Remove old ingredient and instruction lists. |
| 50 |
*/ |
| 51 |
$md_opts = SucomUtil::preg_grep_keys( '/^schema_recipe_(ingredient|instruction)_[0-9]+$/', $md_opts, $invert = true ); |
| 52 |
|
| 53 |
$recipe_opts = self::get_recipe_options( $post_id, $recipe_id ); |
| 54 |
|
| 55 |
foreach ( $recipe_opts as $key => $val ) { |
| 56 |
|
| 57 |
$md_opts[ $key ] = $val; |
| 58 |
$md_opts[ $key . ':disabled' ] = true; |
| 59 |
} |
| 60 |
|
| 61 |
return $md_opts; |
| 62 |
} |
| 63 |
|
| 64 |
public function filter_save_post_options( array $md_opts, $post_id, array $mod ) { |
| 65 |
|
| 66 |
if ( $this->p->debug->enabled ) { |
| 67 |
|
| 68 |
$this->p->debug->mark(); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! $recipe_id = $this->get_recipe_id( $post_id ) ) { |
| 72 |
|
| 73 |
return $md_opts; |
| 74 |
} |
| 75 |
|
| 76 |
$recipe_opts = self::get_recipe_options( $post_id, $recipe_id ); |
| 77 |
|
| 78 |
foreach ( $recipe_opts as $key => $val ) { |
| 79 |
|
| 80 |
unset( $md_opts[ $key ] ); |
| 81 |
} |
| 82 |
|
| 83 |
return $md_opts; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* Returns option names suitable for custom post meta (includes a "schema_" prefix). |
| 88 |
*/ |
| 89 |
public static function get_recipe_options( $post_id, $recipe_id ) { |
| 90 |
|
| 91 |
$wpsso =& Wpsso::get_instance(); |
| 92 |
|
| 93 |
if ( $wpsso->debug->enabled ) { |
| 94 |
|
| 95 |
$wpsso->debug->mark(); |
| 96 |
} |
| 97 |
|
| 98 |
$recipe = WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
| 99 |
|
| 100 |
if ( ! is_object( $recipe ) ) { // Just in case. |
| 101 |
|
| 102 |
return array(); |
| 103 |
} |
| 104 |
|
| 105 |
$data = $recipe->get_data(); |
| 106 |
|
| 107 |
$opts = array( |
| 108 |
'schema_type' => $wpsso->options[ 'schema_type_for_recipe' ], |
| 109 |
'schema_recipe_id' => $recipe_id, |
| 110 |
); |
| 111 |
|
| 112 |
/* |
| 113 |
* Cuisine |
| 114 |
*/ |
| 115 |
$cuisines = $recipe->tags( 'cuisine' ); |
| 116 |
|
| 117 |
if ( count( $cuisines ) > 0 ) { |
| 118 |
|
| 119 |
$opts[ 'schema_recipe_cuisine' ] = implode( $glue = ', ', wp_list_pluck( $cuisines, 'name' ) ); |
| 120 |
} |
| 121 |
|
| 122 |
/* |
| 123 |
* Course |
| 124 |
*/ |
| 125 |
$courses = $recipe->tags( 'course' ); |
| 126 |
|
| 127 |
if ( count( $courses ) > 0 ) { |
| 128 |
|
| 129 |
$opts[ 'schema_recipe_course' ] = implode( $glue = ', ', wp_list_pluck( $courses, 'name' ) ); |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
* Yield |
| 134 |
*/ |
| 135 |
if ( $recipe->servings() ) { |
| 136 |
|
| 137 |
$opts[ 'schema_recipe_yield' ] = trim( $recipe->servings() . ' ' . $recipe->servings_unit() ); |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* Times |
| 142 |
*/ |
| 143 |
foreach( array( |
| 144 |
'schema_recipe_prep', |
| 145 |
'schema_recipe_cook', |
| 146 |
'schema_recipe_total', |
| 147 |
) as $opt_pre ) { |
| 148 |
|
| 149 |
foreach ( array( 'days', 'hours', 'mins', 'secs' ) as $unit ) { // Set the baseline to 0. |
| 150 |
|
| 151 |
$opts[ $opt_pre . '_' . $unit ] = 0; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$opts[ 'schema_recipe_prep_mins' ] = $recipe->prep_time(); |
| 156 |
$opts[ 'schema_recipe_total_mins' ] = $recipe->total_time(); |
| 157 |
$opts[ 'schema_recipe_cook_mins' ] = $recipe->cook_time(); |
| 158 |
|
| 159 |
/* |
| 160 |
* Ingredients |
| 161 |
*/ |
| 162 |
if ( isset( $data[ 'ingredients' ] ) && is_array( $data[ 'ingredients' ] ) ) { |
| 163 |
|
| 164 |
$ingredients = array(); // Start with a fresh array. |
| 165 |
|
| 166 |
foreach( $data[ 'ingredients' ] as $group ) { |
| 167 |
|
| 168 |
if ( isset( $group[ 'name' ] ) ) { |
| 169 |
|
| 170 |
$group_name = trim( $group[ 'name' ] ); |
| 171 |
} |
| 172 |
|
| 173 |
if ( isset( $group[ 'ingredients' ] ) && is_array( $group[ 'ingredients' ] ) ) { |
| 174 |
|
| 175 |
foreach( $group[ 'ingredients' ] as $arr ) { |
| 176 |
|
| 177 |
$md_val = ''; |
| 178 |
|
| 179 |
if ( $group_name ) { |
| 180 |
|
| 181 |
$md_val .= '[' . $group_name . '] '; |
| 182 |
} |
| 183 |
|
| 184 |
$md_val .= $arr[ 'amount' ] . ' ' . $arr[ 'unit' ] . ' ' . $arr[ 'name' ]; |
| 185 |
|
| 186 |
if ( ( $notes = trim( $arr[ 'notes' ] ) ) !== '' ) { |
| 187 |
|
| 188 |
$md_val .= ' (' . $notes . ')'; |
| 189 |
} |
| 190 |
|
| 191 |
$ingredients[] = trim( $md_val ); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
foreach ( $ingredients as $num => $md_val ) { // Start at 0. |
| 197 |
|
| 198 |
$opts[ 'schema_recipe_ingredient_' . $num ] = $md_val; |
| 199 |
} |
| 200 |
|
| 201 |
unset( $ingredients ); |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* Instructions |
| 206 |
*/ |
| 207 |
if ( isset( $data[ 'instructions' ] ) && is_array( $data[ 'instructions' ] ) ) { |
| 208 |
|
| 209 |
$instructions = array(); // Start with a fresh array. |
| 210 |
|
| 211 |
foreach( $data[ 'instructions' ] as $group ) { |
| 212 |
|
| 213 |
if ( ! empty( $group[ 'name' ] ) ) { |
| 214 |
|
| 215 |
$instructions[] = array( |
| 216 |
'section' => 1, |
| 217 |
'name' => $group[ 'name' ], |
| 218 |
'text' => null, |
| 219 |
'img_id' => null, |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
if ( isset( $group[ 'instructions' ] ) && is_array( $group[ 'instructions' ] ) ) { |
| 224 |
|
| 225 |
foreach( $group[ 'instructions' ] as $arr ) { |
| 226 |
|
| 227 |
$instructions[] = array( |
| 228 |
'section' => 0, |
| 229 |
'name' => empty( $arr[ 'name' ] ) ? null : $arr[ 'name' ], |
| 230 |
'text' => empty( $arr[ 'text' ] ) ? null : trim( SucomUtil::strip_html( $arr[ 'text' ] ) ), |
| 231 |
'img_id' => empty( $arr[ 'image' ] ) ? null : $arr[ 'image' ], |
| 232 |
); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
foreach ( $instructions as $num => $arr ) { // Start at 0. |
| 238 |
|
| 239 |
$opts[ 'schema_recipe_instruction_section_' . $num ] = $arr[ 'section' ]; |
| 240 |
$opts[ 'schema_recipe_instruction_' . $num ] = $arr[ 'name' ]; |
| 241 |
$opts[ 'schema_recipe_instruction_text_' . $num ] = $arr[ 'text' ]; |
| 242 |
$opts[ 'schema_recipe_instruction_img_id_' . $num ] = $arr[ 'img_id' ]; |
| 243 |
} |
| 244 |
|
| 245 |
unset( $instructions ); |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* Nutrition Information |
| 250 |
*/ |
| 251 |
if ( isset( $data[ 'nutrition' ] ) && is_array( $data[ 'nutrition' ] ) ) { |
| 252 |
|
| 253 |
foreach ( array( |
| 254 |
'schema_recipe_nutri_cal' => 'calories', |
| 255 |
'schema_recipe_nutri_prot' => 'protein', |
| 256 |
'schema_recipe_nutri_fib' => 'fiber', |
| 257 |
'schema_recipe_nutri_carb' => 'carbohydrates', |
| 258 |
'schema_recipe_nutri_sugar' => 'sugar', |
| 259 |
'schema_recipe_nutri_sod' => 'sodium', |
| 260 |
'schema_recipe_nutri_fat' => 'fat', |
| 261 |
'schema_recipe_nutri_sat_fat' => 'saturated_fat', |
| 262 |
'schema_recipe_nutri_trans_fat' => 'trans_fat', |
| 263 |
'schema_recipe_nutri_chol' => 'cholesterol', |
| 264 |
) as $opt_pre => $nutri_key ) { |
| 265 |
|
| 266 |
if ( isset( $data[ 'nutrition' ][ $nutri_key ] ) ) { |
| 267 |
|
| 268 |
$opts[ $opt_pre ] = trim( $data[ 'nutrition' ][ $nutri_key ] ); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
$nutri_unsat_fat = 0; |
| 273 |
|
| 274 |
$opts[ 'schema_recipe_nutri_unsat_fat' ] = ''; |
| 275 |
|
| 276 |
foreach ( array( |
| 277 |
'monounsaturated_fat', |
| 278 |
'polyunsaturated_fat', |
| 279 |
) as $nutri_fat_key ) { |
| 280 |
|
| 281 |
if ( isset( $data[ 'nutrition' ][ $nutri_fat_key ] ) && trim( $data[ 'nutrition' ][ $nutri_fat_key ] ) !== '' ) { |
| 282 |
|
| 283 |
$nutri_unsat_fat += $data[ 'nutrition' ][ $nutri_fat_key ]; |
| 284 |
|
| 285 |
$opts[ 'schema_recipe_nutri_unsat_fat' ] = $nutri_unsat_fat; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
$serv_size = ''; |
| 290 |
|
| 291 |
foreach ( array( 'serving_size', 'serving_unit' ) as $serv_key ) { |
| 292 |
|
| 293 |
if ( isset( $data[ 'nutrition' ][ $serv_key ] ) && trim( $data[ 'nutrition' ][ $serv_key ] ) !== '' ) { |
| 294 |
|
| 295 |
$serv_size .= trim( ' ' . $data[ 'nutrition' ][ $serv_key ] ); |
| 296 |
|
| 297 |
$opts[ 'schema_recipe_nutri_serv' ] = $serv_size; |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return $opts; |
| 303 |
} |
| 304 |
|
| 305 |
public function get_recipe_id( $post_id ) { |
| 306 |
|
| 307 |
if ( $this->p->debug->enabled ) { |
| 308 |
|
| 309 |
$this->p->debug->mark(); |
| 310 |
} |
| 311 |
|
| 312 |
static $ids_cache = array(); // Cache for $post_id => $recipe_id. |
| 313 |
|
| 314 |
if ( isset( $ids_cache[ $post_id ] ) ) { |
| 315 |
|
| 316 |
return $ids_cache[ $post_id ]; |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
$post_type = get_post_type( $post_id ); |
| 321 |
|
| 322 |
if ( 'attachment' === $post_type || WPRM_POST_TYPE === $post_type ) { // Skip attachments and recipe objects. |
| 323 |
|
| 324 |
$recipe_id = false; |
| 325 |
|
| 326 |
} else { |
| 327 |
|
| 328 |
$post_obj = get_post( $post_id ); |
| 329 |
$shortcode_ids = WPRM_Recipe_Manager::get_recipe_ids_from_content( $post_obj->post_content ); |
| 330 |
$recipe_id = reset( $shortcode_ids ); |
| 331 |
|
| 332 |
if ( empty( $recipe_id ) ) { |
| 333 |
|
| 334 |
if ( $this->p->debug->enabled ) { |
| 335 |
|
| 336 |
$this->p->debug->log( 'post_id ' . $post_id . ' recipe not found in content' ); |
| 337 |
} |
| 338 |
|
| 339 |
$recipe_id = false; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
return $ids_cache[ $post_id ] = $recipe_id; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|