get_type_for_post_type( $post->post_type ); if ( $type_suggestion ) { $suggestions[] = array( 'type' => $type_suggestion, 'confidence' => 0.7, 'reason' => sprintf( /* translators: %s: WordPress post type */ __( 'Based on post type "%s".', 'ai-schema-gen' ), $post->post_type ), ); } // 2. Category and tag hints. $taxonomy_suggestions = $this->detect_from_taxonomy( $post_id ); foreach ( $taxonomy_suggestions as $suggestion ) { $suggestions[] = $suggestion; } // 3. Content pattern matching. $content = wp_strip_all_tags( $post->post_content ); $content_suggestions = $this->detect_from_content( $content ); $is_legal_policy_page = $this->is_legal_policy_title( $post->post_title ); foreach ( $content_suggestions as $suggestion ) { // Legal/policy pages (Terms, Privacy Policy, ...) are structured as // numbered clauses, which reads identically to numbered HowTo steps // to a keyword-pattern check. Drop the false-positive suggestion. if ( $is_legal_policy_page && 'HowTo' === $suggestion['type'] ) { continue; } $suggestions[] = $suggestion; } // Merge duplicates: keep the highest confidence per type. $merged = array(); foreach ( $suggestions as $suggestion ) { $type = $suggestion['type']; if ( ! isset( $merged[ $type ] ) || $suggestion['confidence'] > $merged[ $type ]['confidence'] ) { $merged[ $type ] = $suggestion; } } // Sort by confidence descending. $result = array_values( $merged ); usort( $result, function ( $a, $b ) { return $b['confidence'] <=> $a['confidence']; } ); return $result; } /** * Whether a page title identifies a legal/policy document (Terms of * Service, Privacy Policy, Disclaimer, ...). * * Mirrors isLegalPolicyTitle() in the backend's classifier.ts. * * @param string $title Post title. * @return bool */ private function is_legal_policy_title( $title ) { return (bool) preg_match( '/\b(terms( (of|and) (service|use|conditions?))?|privacy policy|cookie policy|disclaimer|refund policy|return policy|shipping policy|legal notice|acceptable use policy|gdpr)\b/i', $title ); } /** * Map a WordPress post type to a default schema.org type. * * @param string $post_type The WordPress post type slug. * @return string|null The schema type or null if no mapping exists. */ public function get_type_for_post_type( $post_type ) { $map = array( 'post' => 'Article', 'page' => 'WebPage', 'product' => 'Product', 'event' => 'Event', 'tribe_events' => 'Event', 'recipe' => 'Recipe', 'course' => 'Course', 'job_listing' => 'JobPosting', 'download' => 'Product', 'portfolio' => 'CreativeWork', 'testimonial' => 'Review', 'team' => 'Person', 'staff' => 'Person', 'location' => 'LocalBusiness', 'service' => 'Service', ); /** * Filters the post type to schema type mapping. * * @since 1.0.0 * * @param array $map Associative array of post_type => schema_type. */ $map = apply_filters( 'schema_ai_post_type_schema_map', $map ); return isset( $map[ $post_type ] ) ? $map[ $post_type ] : null; } /** * Detect schema types from content patterns. * * Runs a set of regular expression checks against stripped content * to identify likely schema types. * * @param string $content Plain-text post content (tags stripped). * @return array[] Suggestion arrays with type, confidence, and reason. */ public function detect_from_content( $content ) { $suggestions = array(); $content_lower = strtolower( $content ); // FAQ patterns: Q&A, question/answer headings. $faq_patterns = array( '/\bQ\s*[:\.]\s*.+?\bA\s*[:\.]/si', '/\b(question|answer)\s*[:\.]/i', '/\bfaq\b/i', '/\bfrequently\s+asked\s+questions\b/i', ); $faq_matches = 0; foreach ( $faq_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $faq_matches++; } } if ( $faq_matches >= 2 ) { $suggestions[] = array( 'type' => 'FAQPage', 'confidence' => min( 0.9, 0.5 + ( $faq_matches * 0.15 ) ), 'reason' => __( 'Content contains FAQ patterns (Q&A format).', 'ai-schema-gen' ), ); } elseif ( $faq_matches >= 1 ) { $suggestions[] = array( 'type' => 'FAQPage', 'confidence' => 0.5, 'reason' => __( 'Content may contain FAQ patterns.', 'ai-schema-gen' ), ); } // Recipe patterns: ingredients, instructions, cook time, prep time. $recipe_patterns = array( '/\b(ingredients?|ingredient\s+list)\b/i', '/\b(instructions?|directions?|method|preparation)\b/i', '/\b(cook\s*time|cooking\s*time|prep\s*time|bake\s*time|total\s*time)\b/i', '/\b(servings?|yield|serves?)\b/i', '/\b(tablespoons?|teaspoons?|cups?|ounces?|grams?)\b/i', '/\b(preheat|simmer|saute|bake|roast|whisk|stir)\b/i', ); $recipe_matches = 0; foreach ( $recipe_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $recipe_matches++; } } if ( $recipe_matches >= 3 ) { $suggestions[] = array( 'type' => 'Recipe', 'confidence' => min( 0.9, 0.4 + ( $recipe_matches * 0.1 ) ), 'reason' => __( 'Content contains recipe-related patterns (ingredients, instructions).', 'ai-schema-gen' ), ); } // Event patterns: date, venue, location, tickets. $event_patterns = array( '/\b(event|conference|workshop|seminar|webinar|meetup|concert|festival)\b/i', '/\b(venue|location|address|hall|arena|center|centre)\b/i', '/\b(tickets?|registration|register|sign\s*up|rsvp|attend)\b/i', '/\b(date|when|schedule|starts?\s+at|begins?\s+at)\b/i', '/\b(january|february|march|april|may|june|july|august|september|october|november|december)\s+\d{1,2}/i', ); $event_matches = 0; foreach ( $event_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $event_matches++; } } if ( $event_matches >= 3 ) { $suggestions[] = array( 'type' => 'Event', 'confidence' => min( 0.85, 0.4 + ( $event_matches * 0.1 ) ), 'reason' => __( 'Content contains event-related patterns (venue, date, tickets).', 'ai-schema-gen' ), ); } // Product / Price patterns: $, USD, price, buy now. $price_patterns = array( '/\$\s*\d+[\.,]?\d*/i', '/\b(USD|EUR|GBP|price|pricing|cost)\b/i', '/\b(buy\s*now|add\s*to\s*cart|purchase|shop|order\s*now)\b/i', '/\b(product|item|sku|in\s*stock|out\s*of\s*stock)\b/i', ); $price_matches = 0; foreach ( $price_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $price_matches++; } } if ( $price_matches >= 2 ) { $suggestions[] = array( 'type' => 'Product', 'confidence' => min( 0.8, 0.35 + ( $price_matches * 0.12 ) ), 'reason' => __( 'Content contains price and product-related patterns.', 'ai-schema-gen' ), ); } // Review patterns: rating, stars, review. $review_patterns = array( '/\b(rating|ratings?|stars?|score)\b/i', '/\b(review|reviewed|reviewer)\b/i', '/\b(pros?\s*(and|&)\s*cons?)\b/i', '/\b(verdict|recommendation|recommend)\b/i', '/\d\s*out\s*of\s*\d/i', '/\d\s*\/\s*\d\s*(stars?)?/i', ); $review_matches = 0; foreach ( $review_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $review_matches++; } } if ( $review_matches >= 2 ) { $suggestions[] = array( 'type' => 'Review', 'confidence' => min( 0.8, 0.35 + ( $review_matches * 0.12 ) ), 'reason' => __( 'Content contains review-related patterns (rating, stars).', 'ai-schema-gen' ), ); } // HowTo patterns: step-by-step, how to. $howto_patterns = array( '/\bhow\s+to\b/i', '/\bstep\s*\d+/i', '/\bstep[\s\-]*by[\s\-]*step\b/i', '/\b(tutorial|guide|walkthrough)\b/i', '/\b(first|next|then|finally)\s*,?\s*(you\s+)?(need|should|must|can|will)\b/i', ); $howto_matches = 0; foreach ( $howto_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $howto_matches++; } } if ( $howto_matches >= 2 ) { $suggestions[] = array( 'type' => 'HowTo', 'confidence' => min( 0.8, 0.35 + ( $howto_matches * 0.12 ) ), 'reason' => __( 'Content contains how-to patterns (steps, tutorial).', 'ai-schema-gen' ), ); } // LocalBusiness patterns. $business_patterns = array( '/\b(phone|telephone|call\s*us|contact\s*us)\b/i', '/\b(address|street|suite|floor|building)\b/i', '/\b(hours|open|closed|monday|tuesday|wednesday|thursday|friday|saturday|sunday)\b/i', '/\b(directions|map|located\s+at|find\s+us)\b/i', ); $business_matches = 0; foreach ( $business_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $business_matches++; } } if ( $business_matches >= 3 ) { $suggestions[] = array( 'type' => 'LocalBusiness', 'confidence' => min( 0.8, 0.35 + ( $business_matches * 0.12 ) ), 'reason' => __( 'Content contains business-related patterns (address, hours, phone).', 'ai-schema-gen' ), ); } // Person / About page patterns. $person_patterns = array( '/\b(about\s+me|biography|bio)\b/i', '/\b(experience|education|skills|expertise)\b/i', '/\b(linkedin|twitter|github|portfolio)\b/i', ); $person_matches = 0; foreach ( $person_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $person_matches++; } } if ( $person_matches >= 2 ) { $suggestions[] = array( 'type' => 'Person', 'confidence' => min( 0.7, 0.3 + ( $person_matches * 0.15 ) ), 'reason' => __( 'Content contains person/biography patterns.', 'ai-schema-gen' ), ); } // VideoObject patterns. $video_patterns = array( '/youtube\.com\/watch/i', '/vimeo\.com\/\d+/i', '/\b(video|watch|play|stream)\b/i', '/]+(?:youtube|vimeo)/i', ); $video_matches = 0; foreach ( $video_patterns as $pattern ) { if ( preg_match( $pattern, $content ) ) { $video_matches++; } } if ( $video_matches >= 2 ) { $suggestions[] = array( 'type' => 'VideoObject', 'confidence' => min( 0.75, 0.35 + ( $video_matches * 0.12 ) ), 'reason' => __( 'Content contains embedded video patterns.', 'ai-schema-gen' ), ); } return $suggestions; } /** * Detect schema hints from taxonomy terms (categories and tags). * * @param int $post_id WordPress post ID. * @return array[] Suggestion arrays with type, confidence, and reason. */ private function detect_from_taxonomy( $post_id ) { $suggestions = array(); $keyword_map = array( 'recipe' => array( 'type' => 'Recipe', 'confidence' => 0.8 ), 'recipes' => array( 'type' => 'Recipe', 'confidence' => 0.8 ), 'cooking' => array( 'type' => 'Recipe', 'confidence' => 0.7 ), 'event' => array( 'type' => 'Event', 'confidence' => 0.8 ), 'events' => array( 'type' => 'Event', 'confidence' => 0.8 ), 'conference' => array( 'type' => 'Event', 'confidence' => 0.75 ), 'workshop' => array( 'type' => 'Event', 'confidence' => 0.75 ), 'faq' => array( 'type' => 'FAQPage', 'confidence' => 0.85 ), 'faqs' => array( 'type' => 'FAQPage', 'confidence' => 0.85 ), 'product' => array( 'type' => 'Product', 'confidence' => 0.8 ), 'products' => array( 'type' => 'Product', 'confidence' => 0.8 ), 'review' => array( 'type' => 'Review', 'confidence' => 0.8 ), 'reviews' => array( 'type' => 'Review', 'confidence' => 0.8 ), 'tutorial' => array( 'type' => 'HowTo', 'confidence' => 0.75 ), 'how-to' => array( 'type' => 'HowTo', 'confidence' => 0.8 ), 'howto' => array( 'type' => 'HowTo', 'confidence' => 0.8 ), 'guide' => array( 'type' => 'HowTo', 'confidence' => 0.7 ), 'course' => array( 'type' => 'Course', 'confidence' => 0.8 ), 'courses' => array( 'type' => 'Course', 'confidence' => 0.8 ), 'video' => array( 'type' => 'VideoObject', 'confidence' => 0.75 ), 'videos' => array( 'type' => 'VideoObject', 'confidence' => 0.75 ), 'local-business' => array( 'type' => 'LocalBusiness', 'confidence' => 0.8 ), 'service' => array( 'type' => 'Service', 'confidence' => 0.75 ), 'services' => array( 'type' => 'Service', 'confidence' => 0.75 ), ); // Check categories. $categories = get_the_terms( $post_id, 'category' ); if ( $categories && ! is_wp_error( $categories ) ) { foreach ( $categories as $category ) { $slug = strtolower( $category->slug ); if ( isset( $keyword_map[ $slug ] ) ) { $match = $keyword_map[ $slug ]; $suggestions[] = array( 'type' => $match['type'], 'confidence' => $match['confidence'], 'reason' => sprintf( /* translators: %s: category name */ __( 'Post is in the "%s" category.', 'ai-schema-gen' ), $category->name ), ); } } } // Check tags. $tags = get_the_terms( $post_id, 'post_tag' ); if ( $tags && ! is_wp_error( $tags ) ) { foreach ( $tags as $tag ) { $slug = strtolower( $tag->slug ); if ( isset( $keyword_map[ $slug ] ) ) { $match = $keyword_map[ $slug ]; $suggestions[] = array( 'type' => $match['type'], 'confidence' => $match['confidence'] - 0.05, // Slightly lower confidence for tags. 'reason' => sprintf( /* translators: %s: tag name */ __( 'Post is tagged with "%s".', 'ai-schema-gen' ), $tag->name ), ); } } } return $suggestions; } /** * Get all supported schema types with descriptions. * * Returns every schema.org type that this plugin can generate, along * with a short human-readable description of each. * * @return array[] Associative array keyed by type, each containing: * - type (string) Schema.org type name. * - label (string) Display label. * - description (string) Brief description. * - icon (string) Dashicon CSS class. */ public function get_all_types() { $types = array( 'Article' => array( 'type' => 'Article', 'label' => __( 'Article', 'ai-schema-gen' ), 'description' => __( 'A written work such as a blog post, news story, or scholarly article.', 'ai-schema-gen' ), 'icon' => 'dashicons-media-text', ), 'BlogPosting' => array( 'type' => 'BlogPosting', 'label' => __( 'Blog Posting', 'ai-schema-gen' ), 'description' => __( 'A blog post published on a blog or similar platform.', 'ai-schema-gen' ), 'icon' => 'dashicons-edit', ), 'NewsArticle' => array( 'type' => 'NewsArticle', 'label' => __( 'News Article', 'ai-schema-gen' ), 'description' => __( 'A news article published in a news source.', 'ai-schema-gen' ), 'icon' => 'dashicons-megaphone', ), 'WebPage' => array( 'type' => 'WebPage', 'label' => __( 'Web Page', 'ai-schema-gen' ), 'description' => __( 'A generic web page such as an about or contact page.', 'ai-schema-gen' ), 'icon' => 'dashicons-admin-page', ), 'Product' => array( 'type' => 'Product', 'label' => __( 'Product', 'ai-schema-gen' ), 'description' => __( 'A product offered for sale, including price and availability.', 'ai-schema-gen' ), 'icon' => 'dashicons-cart', ), 'Recipe' => array( 'type' => 'Recipe', 'label' => __( 'Recipe', 'ai-schema-gen' ), 'description' => __( 'A cooking recipe with ingredients, instructions, and times.', 'ai-schema-gen' ), 'icon' => 'dashicons-carrot', ), 'Event' => array( 'type' => 'Event', 'label' => __( 'Event', 'ai-schema-gen' ), 'description' => __( 'An event such as a concert, conference, or workshop.', 'ai-schema-gen' ), 'icon' => 'dashicons-calendar-alt', ), 'FAQPage' => array( 'type' => 'FAQPage', 'label' => __( 'FAQ Page', 'ai-schema-gen' ), 'description' => __( 'A page containing frequently asked questions and answers.', 'ai-schema-gen' ), 'icon' => 'dashicons-editor-help', ), 'HowTo' => array( 'type' => 'HowTo', 'label' => __( 'How-To', 'ai-schema-gen' ), 'description' => __( 'A step-by-step guide or tutorial for accomplishing a task.', 'ai-schema-gen' ), 'icon' => 'dashicons-editor-ol', ), 'LocalBusiness' => array( 'type' => 'LocalBusiness', 'label' => __( 'Local Business', 'ai-schema-gen' ), 'description' => __( 'A local business with physical location, hours, and contact info.', 'ai-schema-gen' ), 'icon' => 'dashicons-store', ), 'Organization' => array( 'type' => 'Organization', 'label' => __( 'Organization', 'ai-schema-gen' ), 'description' => __( 'A company, non-profit, school, or other organization.', 'ai-schema-gen' ), 'icon' => 'dashicons-building', ), 'Person' => array( 'type' => 'Person', 'label' => __( 'Person', 'ai-schema-gen' ), 'description' => __( 'A person, including their bio, job, and contact information.', 'ai-schema-gen' ), 'icon' => 'dashicons-admin-users', ), 'Review' => array( 'type' => 'Review', 'label' => __( 'Review', 'ai-schema-gen' ), 'description' => __( 'A review of a product, service, or business with a rating.', 'ai-schema-gen' ), 'icon' => 'dashicons-star-filled', ), 'Service' => array( 'type' => 'Service', 'label' => __( 'Service', 'ai-schema-gen' ), 'description' => __( 'A service offered by a business or individual.', 'ai-schema-gen' ), 'icon' => 'dashicons-admin-tools', ), 'VideoObject' => array( 'type' => 'VideoObject', 'label' => __( 'Video', 'ai-schema-gen' ), 'description' => __( 'A video file or embedded video with metadata.', 'ai-schema-gen' ), 'icon' => 'dashicons-video-alt3', ), 'ImageObject' => array( 'type' => 'ImageObject', 'label' => __( 'Image', 'ai-schema-gen' ), 'description' => __( 'An image with metadata such as caption, author, and license.', 'ai-schema-gen' ), 'icon' => 'dashicons-format-image', ), 'Course' => array( 'type' => 'Course', 'label' => __( 'Course', 'ai-schema-gen' ), 'description' => __( 'An educational course offered by an institution or creator.', 'ai-schema-gen' ), 'icon' => 'dashicons-welcome-learn-more', ), 'BreadcrumbList' => array( 'type' => 'BreadcrumbList', 'label' => __( 'Breadcrumb', 'ai-schema-gen' ), 'description' => __( 'A breadcrumb navigation trail for the page.', 'ai-schema-gen' ), 'icon' => 'dashicons-admin-links', ), ); /** * Filters the supported schema types. * * @since 1.0.0 * * @param array $types Schema types with label, description, and icon. */ return apply_filters( 'schema_ai_supported_types', $types ); } }