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( '/
]*>(.*?)<\/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;
}
}