blocks->registerBlock( 'pro/recipe', [ 'render_callback' => [ $this, 'render' ] ] ); } /** * Render the block. * * @since 4.9.0 * * @param array $attributes The attributes for the block. * * @return string */ public function render( $attributes ) { $name = $attributes['name'] ?? ''; $description = $attributes['description'] ?? ''; $image = $attributes['image'] ?? ''; $nutrition = ! empty( $attributes['nutrition'] ) ? ( is_array( $attributes['nutrition'] ) ? $attributes['nutrition'] : json_decode( $attributes['nutrition'], true ) ) : ''; $ingredients = ! empty( $attributes['ingredients'] ) ? ( is_array( $attributes['ingredients'] ) ? $attributes['ingredients'] : json_decode( $attributes['ingredients'], true ) ) : []; $keywords = ! empty( $attributes['keywords'] ) ? ( is_array( $attributes['keywords'] ) ? $attributes['keywords'] : json_decode( $attributes['keywords'], true ) ) : ''; $timeRequired = ! empty( $attributes['timeRequired'] ) ? ( is_array( $attributes['timeRequired'] ) ? $attributes['timeRequired'] : json_decode( $attributes['timeRequired'], true ) ) : []; $instructions = ! empty( $attributes['instructions'] ) ? ( is_array( $attributes['instructions'] ) ? $attributes['instructions'] : json_decode( $attributes['instructions'], true ) ) : []; $rating = ! empty( $attributes['rating'] ) ? ( is_array( $attributes['rating'] ) ? $attributes['rating'] : json_decode( $attributes['rating'], true ) ) : []; $reviews = ! empty( $attributes['reviews'] ) ? ( is_array( $attributes['reviews'] ) ? $attributes['reviews'] : json_decode( $attributes['reviews'], true ) ) : []; $dishType = $attributes['dishType'] ?? ''; $cuisineType = $attributes['cuisineType'] ?? ''; $maximum = ! empty( $rating['maximum'] ) ? $rating['maximum'] : 5; $result = '
'; if ( ! empty( $name ) || ! empty( $description ) || ! empty( $image ) ) { $result .= '

' . esc_html( aioseo()->tags->replaceTags( $name ) ) . '

'; if ( ! empty( $description ) ) { $result .= '

' . esc_html( aioseo()->tags->replaceTags( $description ) ) . '

'; } $result .= '
'; if ( ! empty( $image ) ) { $result .= '
' . esc_attr( $name ) . '
'; } $result .= '
'; } if ( ! empty( $nutrition['servings'] ) || ! empty( $timeRequired['preparation'] ) || ! empty( $timeRequired['cooking'] ) || ! empty( $nutrition['calories'] ) || ! empty( $cuisineType ) || ! empty( $dishType ) ) { $result .= '
'; if ( ! empty( $nutrition['servings'] ) ) { $result .= '
' . __( 'Serves', 'aioseo-pro' ) . ' ' . esc_html( $nutrition['servings'] ) . '
'; } if ( ! empty( $timeRequired['preparation'] ) ) { $result .= '
' . __( 'Prep Time', 'aioseo-pro' ) . ' ' . esc_html( $timeRequired['preparation'] ) . ' ' . __( 'Minutes', 'aioseo-pro' ) . '
'; } if ( ! empty( $timeRequired['cooking'] ) ) { $result .= '
' . __( 'Cooks in', 'aioseo-pro' ) . ' ' . esc_html( $timeRequired['cooking'] ) . ' ' . __( 'Minutes', 'aioseo-pro' ) . '
'; } $result .= '
'; $result .= '
'; if ( ! empty( $nutrition['calories'] ) ) { $result .= '
' . __( 'Calories', 'aioseo-pro' ) . ' ' . esc_html( $nutrition['calories'] ) . '
'; } if ( ! empty( $cuisineType ) ) { $result .= '
' . __( 'Cuisine', 'aioseo-pro' ) . ' ' . esc_html( $cuisineType ) . '
'; } if ( ! empty( $dishType ) ) { $result .= '
' . __( 'Dish type', 'aioseo-pro' ) . ' ' . esc_html( $dishType ) . '
'; } $result .= '
'; } if ( ! empty( $keywords ) ) { $result .= '
' . __( 'Recipe Tags', 'aioseo-pro' ) . ': '; foreach ( $keywords as $keyword ) { $result .= '' . esc_html( $keyword['label'] ) . ''; } $result .= '
'; } if ( ! empty( $ingredients ) ) { $result .= '

' . __( 'Ingredients', 'aioseo-pro' ) . '

'; } $filteredInstructions = empty( $instructions ) ? [] : array_filter( $instructions, function ( $instruction ) { return ! empty( $instruction['name'] ) || ! empty( $instruction['text'] ) || ! empty( $instruction['image'] ); } ); if ( ! empty( $filteredInstructions ) ) { $result .= '

' . __( 'Instructions', 'aioseo-pro' ) . '

    '; foreach ( $filteredInstructions as $instruction ) { $result .= '
  1. ' . esc_html( $instruction['name'] ) . '

    ' . esc_html( $instruction['text'] ) . '

    '; if ( ! empty( $instruction['image'] ) ) { $result .= '
    ' . esc_attr( $instruction['name'] ) . '
    '; } $result .= '
  2. '; } $result .= '
'; } if ( ! empty( $reviews ) ) { $result .= '
' . __( 'Reviews', 'aioseo-pro' ) . ': '; $filteredReviews = array_filter($reviews, function ( $review ) { return ! empty( $review['headline'] ) && ! empty( $review['rating'] ) && ! empty( $review['author'] ); }); foreach ( $filteredReviews as $review ) { $result .= '

' . __( 'Review Headline', 'aioseo-pro' ) . ': ' . esc_html( $review['headline'] ) . '

' . __( 'Review Description', 'aioseo-pro' ) . ': ' . esc_html( $review['content'] ) . '
' . __( 'Reviewed by', 'aioseo-pro' ) . ': - ' . esc_html( $review['author'] ) . '
' . __( 'Total Rating', 'aioseo-pro' ) . ': ' . esc_html( $review['rating'] ) . ' ' . __( 'out of ', 'aioseo-pro' ) . ' ' . esc_html( $maximum ) . ''; foreach ( range( 1, $maximum ) as $i ) { //phpcs:ignore $result .= '' . __( '★', 'aioseo-pro' ) . ''; } $result .= '
'; } $result .= '
'; } $result .= '
'; return $result; } }