get_post_data( $post_id ); if ( empty( $post_data ) ) { return array( '@type' => $this->type ); } $schema = array( '@type' => $this->type, 'name' => $post_data['title'], 'description' => $post_data['excerpt'], 'datePublished' => $post_data['date'], ); // Image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['image'] = $image; } // Author. $author = $this->get_author_data( $post_id ); if ( ! empty( $author ) ) { $schema['author'] = $author; } // Extract recipe-specific data from content. $content = $post_data['content']; // Ingredients - look for list items within content. $ingredients = $this->extract_ingredients( $content, $fields ); if ( ! empty( $ingredients ) ) { $schema['recipeIngredient'] = $ingredients; } // Instructions. $instructions = $this->extract_instructions( $content, $fields ); if ( ! empty( $instructions ) ) { $schema['recipeInstructions'] = $instructions; } // Time fields from AI or post meta. if ( ! empty( $fields['prepTime'] ) ) { $schema['prepTime'] = $this->format_duration( $fields['prepTime'] ); } if ( ! empty( $fields['cookTime'] ) ) { $schema['cookTime'] = $this->format_duration( $fields['cookTime'] ); } if ( ! empty( $fields['totalTime'] ) ) { $schema['totalTime'] = $this->format_duration( $fields['totalTime'] ); } // Yield. if ( ! empty( $fields['recipeYield'] ) ) { $schema['recipeYield'] = $fields['recipeYield']; } // Category and cuisine. if ( ! empty( $fields['recipeCategory'] ) ) { $schema['recipeCategory'] = $fields['recipeCategory']; } if ( ! empty( $fields['recipeCuisine'] ) ) { $schema['recipeCuisine'] = $fields['recipeCuisine']; } // Nutrition. if ( ! empty( $fields['nutrition'] ) ) { $nutrition = $fields['nutrition']; if ( is_array( $nutrition ) && ! isset( $nutrition['@type'] ) ) { $nutrition['@type'] = 'NutritionInformation'; } $schema['nutrition'] = $nutrition; } // Aggregate rating. if ( ! empty( $fields['aggregateRating'] ) ) { $schema['aggregateRating'] = $fields['aggregateRating']; } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'image', 'author', 'keywords', 'recipeCategory', 'recipeCuisine', 'suitableForDiet', ) ); return $schema; } /** * Extract ingredients from content or AI fields. * * @param string $content Post content HTML. * @param array $fields AI-detected fields. * @return array List of ingredient strings. */ private function extract_ingredients( $content, $fields ) { // Prefer AI-detected ingredients. if ( ! empty( $fields['recipeIngredient'] ) && is_array( $fields['recipeIngredient'] ) ) { return $fields['recipeIngredient']; } // Try to extract from content - look for list items. $ingredients = array(); // Look for an ingredients section. if ( preg_match( '/ingredient/i', $content ) ) { // Find list items after the ingredients heading. if ( preg_match_all( '/