prefix . 'schema_ai_schemas' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table. $schemas = $wpdb->get_results( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is sanitized. "SELECT schema_type, schema_data, validation_status FROM {$table} WHERE post_id = %d", $post_id ) ); $score = 0; $issues = array(); if ( empty( $schemas ) ) { $issues[] = __( 'No schema generated for this page yet', 'ai-schema-gen' ); return array( 'score' => 0, 'grade' => 'poor', 'issues' => $issues ); } // ── Schema exists (25 pts) ──────────────────────────────────────── $score += 25; // ── Validation status (25 pts) ──────────────────────────────────── $has_invalid = false; $has_warning = false; foreach ( $schemas as $schema ) { if ( 'invalid' === $schema->validation_status ) { $has_invalid = true; } elseif ( 'warning' === $schema->validation_status ) { $has_warning = true; } } if ( $has_invalid ) { $issues[] = __( 'One or more schemas have validation errors — regenerate to fix', 'ai-schema-gen' ); } elseif ( $has_warning ) { $score += 12; $issues[] = __( 'One or more schemas have validation warnings', 'ai-schema-gen' ); } else { $score += 25; } // ── Deprecated type check (15 pts) ─────────────────────────────── $deprecated = class_exists( 'Schema_AI_Entity_Settings' ) ? Schema_AI_Entity_Settings::$deprecated_types : array(); $has_deprecated = false; foreach ( $schemas as $schema ) { if ( in_array( $schema->schema_type, $deprecated, true ) ) { $has_deprecated = true; break; } } if ( $has_deprecated ) { $issues[] = __( 'Uses a deprecated schema type — regenerate to update', 'ai-schema-gen' ); } else { $score += 15; } // ── Entity signals (20 pts) ─────────────────────────────────────── $has_entity = false; $has_image = false; $has_desc = false; foreach ( $schemas as $schema ) { $data = json_decode( $schema->schema_data, true ); if ( ! is_array( $data ) ) { continue; } if ( ! empty( $data['sameAs'] ) || ! empty( $data['legalName'] ) || ! empty( $data['knowsAbout'] ) ) { $has_entity = true; } if ( ! empty( $data['image'] ) ) { $has_image = true; } if ( ! empty( $data['description'] ) ) { $has_desc = true; } } if ( $has_entity ) { $score += 20; } else { $issues[] = __( 'No entity signals — configure Entity Authority in Settings', 'ai-schema-gen' ); } // ── Image (10 pts) ─────────────────────────────────────────────── if ( $has_image ) { $score += 10; } else { $issues[] = __( 'Schema has no image — set a featured image', 'ai-schema-gen' ); } // ── Description (5 pts) ───────────────────────────────────────── if ( $has_desc ) { $score += 5; } $grade = $score >= 80 ? 'good' : ( $score >= 50 ? 'fair' : 'poor' ); return array( 'score' => $score, 'grade' => $grade, 'issues' => $issues, ); } }