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'], ); // Image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['image'] = $image; } // Total time. if ( ! empty( $fields['totalTime'] ) ) { $schema['totalTime'] = $this->format_duration( $fields['totalTime'] ); } // Estimated cost. if ( ! empty( $fields['estimatedCost'] ) ) { $cost = $fields['estimatedCost']; if ( is_array( $cost ) && ! isset( $cost['@type'] ) ) { $cost['@type'] = 'MonetaryAmount'; } $schema['estimatedCost'] = $cost; } // Supply. if ( ! empty( $fields['supply'] ) ) { $schema['supply'] = $this->format_supply_or_tool( $fields['supply'], 'HowToSupply' ); } // Tool. if ( ! empty( $fields['tool'] ) ) { $schema['tool'] = $this->format_supply_or_tool( $fields['tool'], 'HowToTool' ); } // Steps - from AI fields or content extraction. $steps = $this->extract_steps( $post_data['content'], $fields ); if ( ! empty( $steps ) ) { $schema['step'] = $steps; } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'image', 'yield', ) ); return $schema; } /** * Extract HowTo steps 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_steps( $content, $fields ) { // Prefer AI-detected steps. if ( ! empty( $fields['step'] ) && is_array( $fields['step'] ) ) { return $this->format_steps( $fields['step'] ); } // Extract from ordered lists in content. $steps = array(); if ( preg_match_all( '/]*>(.*?)<\/ol>/is', $content, $ol_matches ) ) { $position = 1; foreach ( $ol_matches[1] as $ol_content ) { if ( preg_match_all( '/]*>(.*?)<\/li>/is', $ol_content, $li_matches ) ) { foreach ( $li_matches[1] as $step_html ) { $step_text = wp_strip_all_tags( $step_html ); $step_text = trim( $step_text ); if ( ! empty( $step_text ) ) { $step = array( '@type' => 'HowToStep', 'text' => $step_text, 'url' => get_permalink( get_the_ID() ) . '#step-' . $position, ); // Check for image inside the step. if ( preg_match( '/]+src=["\']([^"\']+)["\']/', $step_html, $img_match ) ) { $step['image'] = $img_match[1]; } $steps[] = $step; $position++; } } } } } // Fallback: extract from headings and paragraphs. if ( empty( $steps ) ) { $steps = $this->extract_steps_from_headings( $content ); } return $steps; } /** * Extract steps from headings (h2/h3) and following content. * * @param string $content Post content HTML. * @return array Array of HowToStep objects. */ private function extract_steps_from_headings( $content ) { $steps = array(); $pattern = '/]*>(.*?)<\/h\1>/is'; if ( ! preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ) ) { return $steps; } $total = count( $matches[0] ); for ( $i = 0; $i < $total; $i++ ) { $heading_text = wp_strip_all_tags( $matches[2][ $i ][0] ); $heading_text = trim( $heading_text ); if ( empty( $heading_text ) ) { continue; } // Get content between this heading and the next. $start = $matches[0][ $i ][1] + strlen( $matches[0][ $i ][0] ); $end = ( $i + 1 < $total ) ? $matches[0][ $i + 1 ][1] : strlen( $content ); $between = substr( $content, $start, $end - $start ); // Extract text. $text = ''; $image = ''; if ( preg_match_all( '/]*>(.*?)<\/p>/is', $between, $p_matches ) ) { $paragraphs = array(); foreach ( $p_matches[1] as $p ) { $clean = wp_strip_all_tags( $p ); $clean = trim( $clean ); if ( ! empty( $clean ) ) { $paragraphs[] = $clean; } } $text = implode( ' ', $paragraphs ); } // Check for image. if ( preg_match( '/]+src=["\']([^"\']+)["\']/', $between, $img_match ) ) { $image = $img_match[1]; } if ( ! empty( $text ) ) { $step = array( '@type' => 'HowToStep', 'name' => $heading_text, 'text' => $text, ); if ( ! empty( $image ) ) { $step['image'] = $image; } $steps[] = $step; } } return $steps; } /** * Format raw step data into HowToStep objects. * * @param array $raw_steps Array of step data. * @return array Array of HowToStep schema objects. */ private function format_steps( $raw_steps ) { $steps = array(); foreach ( $raw_steps as $step ) { if ( is_string( $step ) ) { $steps[] = array( '@type' => 'HowToStep', 'text' => $step, ); } elseif ( is_array( $step ) ) { if ( ! isset( $step['@type'] ) ) { $step['@type'] = 'HowToStep'; } $steps[] = $step; } } return $steps; } /** * Format supply or tool items. * * @param array|string $items Items to format. * @param string $type Schema type (HowToSupply or HowToTool). * @return array Formatted items. */ private function format_supply_or_tool( $items, $type ) { if ( ! is_array( $items ) ) { return array( '@type' => $type, 'name' => $items, ); } $formatted = array(); foreach ( $items as $item ) { if ( is_string( $item ) ) { $formatted[] = array( '@type' => $type, 'name' => $item, ); } elseif ( is_array( $item ) ) { if ( ! isset( $item['@type'] ) ) { $item['@type'] = $type; } $formatted[] = $item; } } return $formatted; } /** * Format a time value into ISO 8601 duration. * * @param mixed $time Time in minutes or ISO 8601 string. * @return string ISO 8601 duration string. */ private function format_duration( $time ) { if ( is_string( $time ) && 0 === strpos( $time, 'PT' ) ) { return $time; } $minutes = (int) $time; if ( $minutes <= 0 ) { return (string) $time; } $hours = floor( $minutes / 60 ); $mins = $minutes % 60; $duration = 'PT'; if ( $hours > 0 ) { $duration .= $hours . 'H'; } if ( $mins > 0 ) { $duration .= $mins . 'M'; } return $duration; } }