Site Health > Status: critical when the schema upgrade * failed or a required table is missing, recommended while an upgrade is pending, good * otherwise. * * The upgrade state comes from {@see Migration::schema_status()}; the missing-table check is * shared with {@see DiagnosticsDebugInfo}. A table check that could not run is not critical: * the diagnostics section says "Could not be checked" instead. * * @since 2.8.4 */ final class SchemaSiteHealth { /** Test key under `direct`. */ public const TEST = 'aipt_database_schema'; /** * Hook the test into Site Health. Safe to call more than once. * * @return void */ public static function register(): void { if ( false === has_filter( 'site_status_tests', array( self::class, 'addTest' ) ) ) { add_filter( 'site_status_tests', array( self::class, 'addTest' ) ); } } /** * Add the direct test. * * @param mixed $tests Site Health tests. * @return mixed */ public static function addTest( $tests ) { if ( ! is_array( $tests ) ) { return $tests; } $tests['direct'][ self::TEST ] = array( 'label' => __( 'AI Product Tools database', 'ai-product-tools' ), 'test' => array( self::class, 'run' ), ); return $tests; } /** * Run the test. * * @return array Result in the shape WordPress core's direct tests return. */ public static function run(): array { global $wpdb; $status = Migration::schema_status(); $missing = DiagnosticsDebugInfo::missingTables(); $result = array( 'label' => __( 'The AI Product Tools database is up to date', 'ai-product-tools' ), 'status' => 'good', 'badge' => array( 'label' => __( 'AI Product Tools', 'ai-product-tools' ), 'color' => 'blue', ), 'description' => '

' . esc_html__( 'The plugin database tables are up to date.', 'ai-product-tools' ) . '

', 'actions' => '', 'test' => self::TEST, ); if ( 'failed' === $status['state'] || ( is_array( $missing ) && array() !== $missing ) ) { $result['status'] = 'critical'; $result['label'] = __( 'The AI Product Tools database needs attention', 'ai-product-tools' ); if ( 'failed' === $status['state'] ) { $description = '

' . esc_html__( 'The plugin could not finish its database update. Some features may not work until it does.', 'ai-product-tools' ) . '

'; } else { $description = '

' . esc_html__( 'Some of the plugin database tables are missing. Some features may not work until they exist again.', 'ai-product-tools' ) . '

'; } if ( is_array( $missing ) && array() !== $missing ) { $prefix = is_object( $wpdb ) && isset( $wpdb->prefix ) ? (string) $wpdb->prefix : ''; $names = array(); foreach ( $missing as $table ) { $names[] = '' !== $prefix && 0 === strpos( $table, $prefix ) ? substr( $table, strlen( $prefix ) ) : $table; } /* translators: %s: comma-separated list of table names without the site prefix */ $description .= '

' . esc_html( sprintf( __( 'Missing tables: %s', 'ai-product-tools' ), implode( ', ', $names ) ) ) . '

'; } if ( '' !== $status['last_error'] ) { /* translators: %s: the last logged database error */ $description .= '

' . esc_html( sprintf( __( 'Last error: %s', 'ai-product-tools' ), $status['last_error'] ) ) . '

'; } $result['description'] = $description; $result['actions'] = '

' . esc_html__( 'Open the "AI Product Tools: diagnostics" section on the Info tab for details. The plugin retries automatically.', 'ai-product-tools' ) . '

'; } elseif ( 'pending' === $status['state'] ) { $result['status'] = 'recommended'; $result['label'] = __( 'The AI Product Tools database update is pending', 'ai-product-tools' ); $result['description'] = '

' . esc_html__( 'A database update for the plugin has not finished yet. It is running now or starts with the next page load.', 'ai-product-tools' ) . '

'; } return $result; } }