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( '/]*>(.*?)<\/li>/is', $content, $matches ) ) { foreach ( $matches[1] as $item ) { $clean = wp_strip_all_tags( $item ); $clean = trim( $clean ); if ( ! empty( $clean ) && strlen( $clean ) < 200 ) { $ingredients[] = $clean; } } } } return $ingredients; } /** * Extract instructions from content or AI fields. * * @param string $content Post content HTML. * @param array $fields AI-detected fields. * @return array Array of HowToStep schema objects. */ private function extract_instructions( $content, $fields ) { // Prefer AI-detected instructions. if ( ! empty( $fields['recipeInstructions'] ) && is_array( $fields['recipeInstructions'] ) ) { $instructions = array(); foreach ( $fields['recipeInstructions'] as $index => $step ) { if ( is_string( $step ) ) { $instructions[] = array( '@type' => 'HowToStep', 'text' => $step, ); } elseif ( is_array( $step ) ) { if ( ! isset( $step['@type'] ) ) { $step['@type'] = 'HowToStep'; } $instructions[] = $step; } } return $instructions; } // Try to extract ordered steps from content. $instructions = array(); if ( preg_match_all( '/]*>(.*?)<\/ol>/is', $content, $ol_matches ) ) { foreach ( $ol_matches[1] as $ol_content ) { if ( preg_match_all( '/]*>(.*?)<\/li>/is', $ol_content, $li_matches ) ) { foreach ( $li_matches[1] as $step_text ) { $clean = wp_strip_all_tags( $step_text ); $clean = trim( $clean ); if ( ! empty( $clean ) ) { $instructions[] = array( '@type' => 'HowToStep', 'text' => $clean, ); } } } } } return $instructions; } /** * Format a time value into ISO 8601 duration. * * Accepts minutes (integer) or already-formatted PT strings. * * @param mixed $time Time value (minutes as int, or ISO 8601 duration string). * @return string ISO 8601 duration (e.g. PT30M). */ private function format_duration( $time ) { if ( is_string( $time ) && 0 === strpos( $time, 'PT' ) ) { return $time; } $minutes = (int) $time; if ( $minutes <= 0 ) { return $time; } $hours = floor( $minutes / 60 ); $mins = $minutes % 60; $duration = 'PT'; if ( $hours > 0 ) { $duration .= $hours . 'H'; } if ( $mins > 0 ) { $duration .= $mins . 'M'; } return $duration; } }