422 ) ); } $header_base_revision = self::unquote_entity_tag( self::header_value( $request, 'if-match' ) ); // A non-string baseRevision (a malformed body) is treated as absent, not cast to "Array" with a notice. $body_revision = is_string( $body_base_revision ) ? $body_base_revision : ''; if ( null !== $header_base_revision && $header_base_revision !== $body_revision ) { return new WP_Error( 'woo_rxdb_sync_header_body_mismatch', 'If-Match header disagrees with body baseRevision.', array( 'status' => 422 ) ); } return null; } private static function header_value( WP_REST_Request $request, string $name ): ?string { $value = $request->get_header( $name ); if ( null === $value ) { return null; } $value = trim( (string) $value ); return '' === $value ? null : $value; } /** Strip an optional weak indicator (W/) and surrounding double quotes from an entity-tag, so the bare * revision compares equal even if an intermediary weakened a strong tag (RFC 9110 ยง8.8.3). */ private static function unquote_entity_tag( ?string $value ): ?string { if ( null === $value ) { return null; } $value = preg_replace( '/^W\//', '', trim( $value ) ); if ( strlen( $value ) >= 2 && '"' === $value[0] && '"' === substr( $value, -1 ) ) { $value = substr( $value, 1, -1 ); } return $value; } }