schema = $schema; } public function defaults() { return $this->schema->defaults(); } public function get() { $stored = get_option( self::OPTION, array() ); $document = is_array( $stored ) ? $this->schema->normalize( $stored ) : array(); return empty( $document ) ? $this->defaults() : $document; } public function replace( array $normalized ) { if ( ! $this->is_complete_document( $normalized, $this->schema->defaults() ) || ! $this->schema->validate( $normalized )->is_valid() ) { return false; } $document = $this->schema->normalize( $normalized ); if ( empty( $document ) || strlen( wp_json_encode( $document ) ) > 32768 ) { return false; } $updated = update_option( self::OPTION, $document, true ); if ( $updated ) { update_option( self::VERSION_OPTION, (int) get_option( self::VERSION_OPTION, 0 ) + 1, true ); do_action( 'artbees_qv/settings/replaced', $document ); } return $updated || $this->get() === $document; } private function is_complete_document( array $candidate, array $defaults ) { if ( array_keys( $candidate ) !== array_keys( $defaults ) ) { return false; } foreach ( $defaults as $key => $default ) { if ( is_array( $default ) && $this->is_associative( $default ) && ( ! is_array( $candidate[ $key ] ) || ! $this->is_complete_document( $candidate[ $key ], $default ) ) ) { return false; } } return true; } private function is_associative( array $value ) { return ! empty( $value ) && array_keys( $value ) !== range( 0, count( $value ) - 1 ); } }