isset( $settings['entity_same_as'] ) && is_array( $settings['entity_same_as'] ) ? array_filter( array_map( 'esc_url_raw', $settings['entity_same_as'] ) ) : array(), 'knows_about' => isset( $settings['entity_knows_about'] ) && is_array( $settings['entity_knows_about'] ) ? array_filter( array_map( 'sanitize_text_field', $settings['entity_knows_about'] ) ) : array(), 'legal_name' => isset( $settings['entity_legal_name'] ) ? sanitize_text_field( $settings['entity_legal_name'] ) : '', 'founding_date' => isset( $settings['entity_founding_date'] ) ? sanitize_text_field( $settings['entity_founding_date'] ) : '', 'description' => isset( $settings['entity_description'] ) ? sanitize_textarea_field( $settings['entity_description'] ) : '', 'num_employees' => isset( $settings['entity_num_employees'] ) ? sanitize_text_field( $settings['entity_num_employees'] ) : '', ); } /** * Get sameAs URLs as an array. * * @return string[] */ public static function get_same_as() { return self::get_all()['same_as']; } /** * Get knowsAbout topics as an array. * * @return string[] */ public static function get_knows_about() { return self::get_all()['knows_about']; } /** * Calculate the Knowledge Graph Entity Score (0-100). * * Scoring breakdown: * Core identity (25 pts) — site type, org name, logo, URL * Contact info (20 pts) — phone, address * Entity links (25 pts) — sameAs URLs, 5 pts each, max 5 * Topical authority(20 pts) — knowsAbout topics, 5 pts each, max 4 * Formal identity (10 pts) — legalName, foundingDate * * @return array { score: int, breakdown: array, missing: string[] } */ public static function calculate_score() { $settings = get_option( 'schema_ai_settings', array() ); $entity = self::get_all(); $score = 0; $breakdown = array(); $missing = array(); // ── Core identity (25 pts) ───────────────────────────────── $core = 0; if ( ! empty( $settings['site_type'] ) ) { $core += 10; } else { $missing[] = __( 'Site type not configured', 'ai-schema-gen' ); } if ( ! empty( $settings['default_org_name'] ) ) { $core += 5; } else { $missing[] = __( 'Organization name missing', 'ai-schema-gen' ); } if ( ! empty( $settings['default_org_logo'] ) ) { $core += 5; } else { $missing[] = __( 'Organization logo missing', 'ai-schema-gen' ); } if ( ! empty( $settings['default_org_url'] ) ) { $core += 5; } else { $missing[] = __( 'Organization URL missing', 'ai-schema-gen' ); } $breakdown['core_identity'] = array( 'label' => __( 'Core Identity', 'ai-schema-gen' ), 'score' => $core, 'max' => 25 ); $score += $core; // ── Contact info (20 pts) ────────────────────────────────── $contact = 0; if ( ! empty( $settings['default_org_phone'] ) ) { $contact += 10; } else { $missing[] = __( 'Phone number missing', 'ai-schema-gen' ); } $has_address = ! empty( $settings['default_org_address'] ) || ! empty( $settings['default_org_city'] ) || ! empty( $settings['default_org_zip'] ); if ( $has_address ) { $contact += 10; } else { $missing[] = __( 'Business address missing', 'ai-schema-gen' ); } $breakdown['contact'] = array( 'label' => __( 'Contact Info', 'ai-schema-gen' ), 'score' => $contact, 'max' => 20 ); $score += $contact; // ── Entity links / sameAs (25 pts, 5 per URL, max 5) ────── $same_as_count = min( 5, count( $entity['same_as'] ) ); $links = $same_as_count * 5; $breakdown['entity_links'] = array( 'label' => __( 'Entity Links (sameAs)', 'ai-schema-gen' ), 'score' => $links, 'max' => 25 ); $score += $links; if ( $same_as_count === 0 ) { $missing[] = __( 'No sameAs directory links added', 'ai-schema-gen' ); } elseif ( $same_as_count < 3 ) { $missing[] = __( 'Add more sameAs links (3+ recommended)', 'ai-schema-gen' ); } // ── Topical authority / knowsAbout (20 pts, 5 per topic, max 4) ── $topic_count = min( 4, count( $entity['knows_about'] ) ); $topics = $topic_count * 5; $breakdown['topical_authority'] = array( 'label' => __( 'Topical Authority (knowsAbout)', 'ai-schema-gen' ), 'score' => $topics, 'max' => 20 ); $score += $topics; if ( $topic_count === 0 ) { $missing[] = __( 'No knowsAbout topics added', 'ai-schema-gen' ); } elseif ( $topic_count < 3 ) { $missing[] = __( 'Add more topics (3+ recommended)', 'ai-schema-gen' ); } // ── Formal identity (10 pts) ─────────────────────────────── $formal = 0; if ( ! empty( $entity['legal_name'] ) ) { $formal += 5; } else { $missing[] = __( 'Legal name missing', 'ai-schema-gen' ); } if ( ! empty( $entity['founding_date'] ) ) { $formal += 5; } else { $missing[] = __( 'Founding date missing', 'ai-schema-gen' ); } $breakdown['formal_identity'] = array( 'label' => __( 'Formal Identity', 'ai-schema-gen' ), 'score' => $formal, 'max' => 10 ); $score += $formal; return array( 'score' => $score, 'breakdown' => $breakdown, 'missing' => $missing, ); } /** * Check whether a schema type is deprecated. * * @param string $type Schema.org type string. * @return bool */ public static function is_deprecated( $type ) { return in_array( $type, self::$deprecated_types, true ); } }