| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cooked SEO Functions |
| 4 |
* |
| 5 |
* @package Cooked |
| 6 |
* @subpackage SEO Functions |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
class Cooked_SEO { |
| 14 |
|
| 15 |
public static function json_ld( $recipe = false ) { |
| 16 |
global $_cooked_settings; |
| 17 |
|
| 18 |
if ( !isset($_cooked_settings['advanced']) || (isset($_cooked_settings['advanced']) && !in_array( 'disable_schema_output', $_cooked_settings['advanced'] )) ) { |
| 19 |
$schema_values_json = wp_json_encode( self::schema_values( $recipe ) ); |
| 20 |
|
| 21 |
$schema_html = '<script type="application/ld+json">'; |
| 22 |
$schema_html .= $schema_values_json; |
| 23 |
$schema_html .= '</script>'; |
| 24 |
|
| 25 |
return apply_filters( 'cooked_schema_html', $schema_html, $recipe ); |
| 26 |
} else { |
| 27 |
return ''; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public static function schema_values( $recipe = false ) { |
| 32 |
global $_cooked_settings; |
| 33 |
|
| 34 |
if ( !$recipe ): |
| 35 |
global $post; |
| 36 |
$recipe = Cooked_Recipes::get_settings( $post->ID ); |
| 37 |
$rpost = get_post( $post->ID ); |
| 38 |
else: |
| 39 |
$rpost = get_post( $recipe['id'] ); |
| 40 |
endif; |
| 41 |
|
| 42 |
$_nutrition_facts = Cooked_Measurements::nutrition_facts(); |
| 43 |
|
| 44 |
$recipe_thumbnail = has_post_thumbnail($rpost) ? get_the_post_thumbnail_url( $rpost, 'cooked-medium' ) : ''; |
| 45 |
if ( !$recipe_author = Cooked_Users::format_author_name( get_the_author_meta( 'display_name', $rpost->post_author ) ) ): |
| 46 |
$recipe_author = ''; |
| 47 |
endif; |
| 48 |
|
| 49 |
$ingredients = []; |
| 50 |
if ( isset($recipe['ingredients']) && !empty($recipe['ingredients']) ): |
| 51 |
foreach ( $recipe['ingredients'] as $ing ): |
| 52 |
if ( isset( $ing['section_heading_name'] ) ): continue; endif; |
| 53 |
$ingredient = Cooked_Recipes::single_ingredient( $ing, false, true ); |
| 54 |
if ( !empty( $ingredient ) ): |
| 55 |
$ingredient_cleaned = wp_strip_all_tags( preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $ingredient) ); |
| 56 |
$ingredients[] = $ingredient_cleaned; |
| 57 |
endif; |
| 58 |
endforeach; |
| 59 |
endif; |
| 60 |
|
| 61 |
$directions = []; |
| 62 |
if ( isset($recipe['directions']) && !empty($recipe['directions']) ): |
| 63 |
$number = 1; |
| 64 |
|
| 65 |
foreach ( $recipe['directions'] as $dir ): |
| 66 |
if ( isset( $dir['section_heading_name'] ) ): |
| 67 |
continue; |
| 68 |
endif; |
| 69 |
|
| 70 |
$direction = Cooked_Recipes::single_direction( $dir, false, true ); |
| 71 |
if ( !empty( $direction ) ): |
| 72 |
$direction_cleaned = wp_strip_all_tags( preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $direction) ); |
| 73 |
$image_id = isset($dir['image']) ? $dir['image'] : false; |
| 74 |
|
| 75 |
$image = ''; |
| 76 |
if ( $image_id ): |
| 77 |
$image = wp_get_attachment_image_src( $image_id, 'full' ); |
| 78 |
$image = $image[0]; |
| 79 |
endif; |
| 80 |
|
| 81 |
$directions[] = [ |
| 82 |
'@type' => 'HowToStep', |
| 83 |
'name' => sprintf(__('Step %d', 'cooked'), $number), |
| 84 |
'text' => $direction_cleaned, |
| 85 |
'url' => get_permalink($rpost) . '#cooked-single-direction-step-' . $number, |
| 86 |
'image' => $image, |
| 87 |
]; |
| 88 |
endif; |
| 89 |
$number++; |
| 90 |
|
| 91 |
endforeach; |
| 92 |
endif; |
| 93 |
|
| 94 |
$category_name = ''; |
| 95 |
if (in_array('cp_recipe_category', $_cooked_settings['recipe_taxonomies'])): |
| 96 |
$categories = get_the_terms( $rpost->ID, 'cp_recipe_category' ); |
| 97 |
if (!empty($categories)): |
| 98 |
$category = $categories[0]; |
| 99 |
$category_name = $category->name; |
| 100 |
endif; |
| 101 |
endif; |
| 102 |
|
| 103 |
$cook_time = isset($recipe['cook_time']) && $recipe['cook_time'] ? esc_html( $recipe['cook_time'] ) : 0; |
| 104 |
$prep_time = isset($recipe['prep_time']) && $recipe['prep_time'] ? esc_html( $recipe['prep_time'] ) : 0; |
| 105 |
$total_time = $cook_time + $prep_time; |
| 106 |
|
| 107 |
$unsaturatedFatAmount = (isset($recipe['nutrition']['monounsaturated_fat']) && $recipe['nutrition']['monounsaturated_fat'] ? $recipe['nutrition']['monounsaturated_fat'] : 0) + (isset($recipe['nutrition']['polyunsaturated_fat']) && $recipe['nutrition']['polyunsaturated_fat'] ? $recipe['nutrition']['polyunsaturated_fat'] : 0); |
| 108 |
|
| 109 |
if ($unsaturatedFatAmount): |
| 110 |
$unsaturatedFatContent = $unsaturatedFatAmount . ' ' . $_nutrition_facts['main']['fat']['subs']['monounsaturated_fat']['measurement']; |
| 111 |
else: |
| 112 |
$unsaturatedFatContent = ''; |
| 113 |
endif; |
| 114 |
|
| 115 |
$description = ''; |
| 116 |
if (!empty($recipe['seo_description'])): |
| 117 |
$description = wp_strip_all_tags( preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $recipe['seo_description']) ); ; |
| 118 |
elseif (!empty($recipe['excerpt'])): |
| 119 |
$description = wp_strip_all_tags( preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $recipe['excerpt']) ); |
| 120 |
elseif (!empty($recipe['title'])): |
| 121 |
$description = $recipe['title']; |
| 122 |
endif; |
| 123 |
|
| 124 |
$schema_array = false; |
| 125 |
$schema_data = [ |
| 126 |
'@context' => 'http://schema.org', |
| 127 |
'@type' => 'Recipe', |
| 128 |
'author' => [ |
| 129 |
'@type' => 'Person', |
| 130 |
'name' => $recipe_author |
| 131 |
], |
| 132 |
'datePublished' => get_the_date('Y-m-d', $recipe['id']), |
| 133 |
'name' => (isset($recipe['title']) ? $recipe['title'] : ''), |
| 134 |
'image' => $recipe_thumbnail, |
| 135 |
'description' => $description, |
| 136 |
'recipeIngredient' => $ingredients, |
| 137 |
'recipeCategory' => $category_name, |
| 138 |
'recipeYield' => (isset($recipe['nutrition']['servings']) && $recipe['nutrition']['servings'] ? $recipe['nutrition']['servings'] . ' ' . strtolower($_nutrition_facts['top']['servings']['name']) : ''), |
| 139 |
'cookTime' => Cooked_Measurements::time_format($cook_time, 'iso'), |
| 140 |
'prepTime' => Cooked_Measurements::time_format($prep_time, 'iso'), |
| 141 |
'totalTime' => Cooked_Measurements::time_format($total_time, 'iso'), |
| 142 |
'nutrition' => [ |
| 143 |
'@type' => 'NutritionInformation', |
| 144 |
'calories' => (isset($recipe['nutrition']['calories']) && $recipe['nutrition']['calories'] ? $recipe['nutrition']['calories'] . ' ' . strtolower($_nutrition_facts['mid']['calories']['name']) : 0), |
| 145 |
'carbohydrateContent' => (isset($recipe['nutrition']['carbs']) && $recipe['nutrition']['carbs'] ? $recipe['nutrition']['carbs'] . ' ' . $_nutrition_facts['main']['carbs']['measurement'] : ''), |
| 146 |
'cholesterolContent' => (isset($recipe['nutrition']['cholesterol']) && $recipe['nutrition']['cholesterol'] ? $recipe['nutrition']['cholesterol'] . ' ' . $_nutrition_facts['main']['cholesterol']['measurement'] : ''), |
| 147 |
'fatContent' => (isset($recipe['nutrition']['fat']) && $recipe['nutrition']['fat'] ? $recipe['nutrition']['fat'] . ' ' . $_nutrition_facts['main']['fat']['measurement'] : ''), |
| 148 |
'fiberContent' => (isset($recipe['nutrition']['fiber']) && $recipe['nutrition']['fiber'] ? $recipe['nutrition']['fiber'] . ' ' . $_nutrition_facts['main']['carbs']['subs']['fiber']['measurement'] : ''), |
| 149 |
'proteinContent' => (isset($recipe['nutrition']['protein']) && $recipe['nutrition']['protein'] ? $recipe['nutrition']['protein'] . ' ' . $_nutrition_facts['main']['protein']['measurement'] : ''), |
| 150 |
'saturatedFatContent' => (isset($recipe['nutrition']['sat_fat']) && $recipe['nutrition']['sat_fat'] ? $recipe['nutrition']['sat_fat'] . ' ' . $_nutrition_facts['main']['fat']['subs']['sat_fat']['measurement'] : ''), |
| 151 |
'servingSize' => (isset($recipe['nutrition']['serving_size']) && $recipe['nutrition']['serving_size'] ? $recipe['nutrition']['serving_size'] . ' ' . strtolower($_nutrition_facts['top']['servings']['name']) : ''), |
| 152 |
'sodiumContent' => (isset($recipe['nutrition']['sodium']) && $recipe['nutrition']['sodium'] ? $recipe['nutrition']['sodium'] . ' ' . $_nutrition_facts['main']['sodium']['measurement'] : ''), |
| 153 |
'sugarContent' => (isset($recipe['nutrition']['sugars']) && $recipe['nutrition']['sugars'] ? $recipe['nutrition']['sugars'] . ' ' . $_nutrition_facts['main']['carbs']['subs']['sugars']['measurement'] : ''), |
| 154 |
'transFatContent' => (isset($recipe['nutrition']['trans_fat']) && $recipe['nutrition']['trans_fat'] ? $recipe['nutrition']['trans_fat'] . ' ' . $_nutrition_facts['main']['fat']['subs']['trans_fat']['measurement'] : ''), |
| 155 |
'unsaturatedFatContent' => $unsaturatedFatContent, |
| 156 |
], |
| 157 |
'recipeInstructions' => $directions, |
| 158 |
]; |
| 159 |
|
| 160 |
$schema_array = apply_filters('cooked_schema_array', $schema_data, $rpost, $recipe); |
| 161 |
|
| 162 |
return $schema_array; |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|