exact normalized labels this bootstrap may adopt */ private array $labels = []; private function __construct( array $labels ) { foreach ( $labels as $label ) { $normalized = Variable_Ownership_Ledger::normalize_label( (string) $label ); if ( '' === $normalized ) { continue; } $this->labels[ $normalized ] = true; } } public static function state(): string { $stored = get_option( self::OPTION, null ); if ( ! is_array( $stored ) ) { return self::NOT_STARTED; } $state = (string) ( $stored['state'] ?? '' ); // An unrecognised or future state is no grounds to adopt. return in_array( $state, [ self::IN_PROGRESS, self::COMPLETE ], true ) ? $state : self::NOT_STARTED; } private static function set_state( string $state ): void { update_option( self::OPTION, [ 'schema' => self::SCHEMA, 'state' => $state ], false ); } /** * May the explicit full reset adopt existing records on this installation? * * Not once a complete ledger exists: after that, ordinary ownership, collision, update, * restore and deletion rules are the whole story. An INCOMPLETE bootstrap stays eligible even * though the ledger is no longer empty, which is the case a boolean would get wrong. */ public static function eligible( Variable_Ownership_Ledger $ledger ): bool { $state = self::state(); if ( self::COMPLETE === $state ) { return false; } if ( self::IN_PROGRESS === $state ) { return true; } return 0 === $ledger->count( Variable_Ownership_Ledger::STATE_ACTIVE ) && 0 === $ledger->count( Variable_Ownership_Ledger::STATE_TOMBSTONE ); } /** * Authorise adoption of exactly these labels, and mark the attempt started. * * Marking IN_PROGRESS before any mutation is deliberate: if the pass dies halfway, the state * already says so and the explicit reset can resume it. * * @param string[] $default_labels Exact labels from the bundled current defaults. */ public static function begin( array $default_labels ): self { self::set_state( self::IN_PROGRESS ); return new self( $default_labels ); } /** * May this exact label be adopted from an existing unmapped active record? */ public function may_adopt( string $label ): bool { return isset( $this->labels[ Variable_Ownership_Ledger::normalize_label( $label ) ] ); } /** * Record completion -- only ever called once the caller has VERIFIED the result. * * Completion is not "the loop finished". It means every expected label has one active mapped * id and the whole set is listed, emitted and value-verified, which is why this takes the * verified counts rather than deciding for itself. */ public static function complete_if_verified( int $expected, int $listed, int $emitted, int $verified ): bool { if ( $expected < 1 || $listed !== $expected || $emitted !== $expected || $verified !== $expected ) { return false; } self::set_state( self::COMPLETE ); return true; } }