Skip to content

Instantly share code, notes, and snippets.

@artikus11
Created June 16, 2020 10:41
Show Gist options
  • Save artikus11/626d91b6f91ab7431d1267cb822bf151 to your computer and use it in GitHub Desktop.
Save artikus11/626d91b6f91ab7431d1267cb822bf151 to your computer and use it in GitHub Desktop.
<?php
class CNWS_Schema {
public function __construct() {
$this->hooks();
}
public function hooks() {
add_action( 'wp_head', [ $this, 'generate' ], 5 );
}
public function generate() {
if ( is_singular( 'recipe' ) ) {
printf(
'<script type="application/ld+json">%1$s</script>%2$s',
wp_json_encode( $this->schema(), JSON_UNESCAPED_SLASHES ),
"\n"
);
}
}
public function recipe() {
return get_post();
}
public function title() {
return esc_html( get_the_title( get_the_ID() ) );
}
public function image( $post_id = 0 ) {
if ( ! isset( $post_id ) ) {
$post_id = get_the_ID();
}
$images = [];
$sizes = [
'1x1' => 'w=320&h=320&post_id=' . $post_id,
'4x3' => 'w=320&h=240&post_id=' . $post_id,
'16x9' => 'w=320&h=180&post_id=' . $post_id,
];
foreach ( $sizes as $key => $size ) {
$images[] = kama_thumb_src( $size );
}
return $images;
}
public function description() {
return get_truncate( wp_strip_all_tags( $this->recipe()->post_content ), 50, '', '' );
}
public function field( $field_name ) {
return get_field( $field_name ) ? wp_strip_all_tags( get_field( $field_name ) ) : '';
}
public function ingredients() {
$ingredients = get_field( 'ingridients' );
$ingredients_schema = [];
if ( $ingredients ) {
foreach ( $ingredients as $ingredient ) {
if ( isset( $ingredient['title'] ) && ! empty( $ingredient['title'] ) ) {
$ingredients_schema[] = wp_strip_all_tags( $ingredient['title'] ) . ' - ' . $ingredient['value'];
}
if ( isset( $ingredient['titile_tag'] ) && ! empty( $ingredient['titile_tag'] ) && ! is_wp_error( $ingredient['titile_tag'] ) ) {
$ingredients_schema[] = wp_strip_all_tags( $ingredient['titile_tag']->name ) . ' - ' . $ingredient['value'];
}
}
}
return $ingredients_schema;
}
public function steps() {
$steps = get_field( 'progress' );
$steps_schema = [];
if ( $steps ) {
foreach ( $steps as $key => $step ) {
$steps_schema[] = [
'@type' => 'HowToStep',
'text' => $step['text'],
'image' => kama_thumb_src( 'w=320&h=320&post_id=' . $step['photo']['url'] ),
'url' => get_permalink() . '#recipe-step-' . esc_attr( $key + 1 ),
];
}
}
return $steps_schema;
}
public function video() {
$video = get_field( 'video' );
$video_desc = get_field( 'video' );
return [];
}
public function recipe_category( $post_id, $tax ) {
$categories = get_the_terms( $post_id, $tax );
$categories_list = [];
if ( false !== $categories && ! is_wp_error( $categories ) && is_array( $categories ) ) {
foreach ( $categories as $category ) {
$categories_list[] = $category->name;
}
}
return implode( ', ', $categories_list );
}
public function recipe_yield() {
$serving = '';
if ( get_field( 'serving' ) ) {
$serving = preg_replace( '/[^0-9]/', '', get_field( 'serving' ) );
}
return $serving;
}
public function schema() {
return [
'@context' => 'https://schema.org/',
'@type' => 'Recipe',
'name' => $this->title(),
'image' => $this->image(),
'author' => [
'@type' => 'Person',
'name' => get_the_author_meta( 'display_name', $this->recipe()->post_author ),
],
'datePublished' => get_the_time( 'Y-m-d' ),
'description' => $this->description(),
'recipeCuisine' => $this->field( 'recipecategory' ),
'prepTime' => recipe_time( 'time_preparation', '', true ),
'totalTime' => recipe_time( 'time_total', '', true ),
'keywords' => $this->field( 'keywords' ),
'recipeYield' => $this->recipe_yield(),
'recipeCategory' => $this->recipe_category( get_the_ID(), 'recipe_tag' ),
'nutrition' => [
'@type' => 'NutritionInformation',
'calories' => $this->field( 'calories' ),
],
'aggregateRating' => [
'@type' => 'AggregateRating',
'ratingValue' => get_post_meta( get_the_ID(), 'ratings_average', true ),
'ratingCount' => get_post_meta( get_the_ID(), 'ratings_users', true ),
],
'recipeIngredient' => $this->ingredients(),
'recipeInstructions' => $this->steps(),
'video' => [],
];
}
}
new CNWS_Schema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment