repair_all(); } /** * Repair every eligible record on this site. * * @since 0.0.41 * @return array{examined: int, repaired: int, skipped: int} Counts, for tests and WP-CLI callers. */ public function repair_all(): array { $examined = 0; $repaired = 0; $skipped = 0; foreach ( Global_Styles_Db::list_all( 500 ) as $post ) { if ( ! Global_Styles_Db::is_main_record( $post ) ) { continue; } ++$examined; if ( ! self::needs_repair( (string) $post->post_content ) ) { continue; } $result = Global_Styles_Db::repair( $post ); if ( is_wp_error( $result ) || empty( $result['changed'] ) ) { ++$skipped; continue; } ++$repaired; } return array( 'examined' => $examined, 'repaired' => $repaired, 'skipped' => $skipped, ); } /** * Whether stored content is a record this migration should rewrite. * * Split out and public so the selection rule — the only thing standing between this and an * unattended rewrite of content it should not touch — is testable without a database. * * @since 0.0.41 * @param string $content Raw post_content. * @return bool */ public static function needs_repair( string $content ): bool { $decoded = Global_Styles_Writer::decode( $content ); // Not JSON: nothing safe to infer, and rewriting would destroy a recoverable record. if ( is_wp_error( $decoded ) ) { return false; } // Empty: core creates this record lazily, and seeding an empty one here would make the // detector report a DB copy that overrides a theme.json the site is happily serving. if ( array() === $decoded ) { return false; } if ( ! Global_Styles_Writer::has_user_flags( $decoded ) ) { return true; } return ! isset( $decoded['version'] ) || Global_Styles_Writer::latest_schema() !== (int) $decoded['version']; } }