declared ) { // A declaration is the lane's own word that this is a POS write; only // the ad-hoc observation below has to learn that from the request. $matches = $intent->is_create() ? $creating : ( ! $creating && $order->get_id() === $intent->id() ); if ( null === $intent->subject && $matches ) { $intent->subject = $order; foreach ( $intent->data['fill_meta'] ?? array() as $key => $value ) { $order->update_meta_data( $key, $value ); } if ( isset( $intent->data['created_gmt'] ) ) { $order->set_date_created( $intent->data['created_gmt'] ); } } } elseif ( \wcpos_request() ) { $intent = new self(); $intent->data = array( 'operation' => $creating ? 'create' : 'update', 'id' => $order->get_id(), 'requested_status' => $request instanceof \WP_REST_Request ? (string) $request->get_param( 'status' ) : '', 'set_paid' => $request instanceof \WP_REST_Request && $request->has_param( 'set_paid' ) && rest_sanitize_boolean( $request->get_param( 'set_paid' ) ), ); $intent->subject = $order; self::$stack = array( $intent ); } return $order; } /** * Declare a write, restoring the enclosing intent even if the write throws. * * @param array $declared Operation, id, requested_status, set_paid, created_gmt, fill_meta. * @param callable $write The order write. * @return mixed The write result. */ public static function open( array $declared, callable $write ) { $intent = new self(); $intent->declared = true; $intent->data = $declared; self::$stack[] = $intent; try { return $write(); } finally { array_pop( self::$stack ); } } /** Return the enclosing declaration or last ad-hoc observation. */ public static function current(): ?self { return self::$stack ? end( self::$stack ) : null; } /** Return the declared or observed operation. */ public function operation(): string { return $this->data['operation']; } /** Whether this write creates an order. */ public function is_create(): bool { return 'create' === $this->operation(); } /** Return the declared or observed order ID (zero for an unsaved create). */ public function id(): int { return (int) ( $this->data['id'] ?? 0 ); } /** Return the client's raw status, before stock neutralisation. */ public function requested_status(): string { return (string) ( $this->data['requested_status'] ?? '' ); } /** Whether the client asked to mark the order paid. */ public function set_paid(): bool { return (bool) ( $this->data['set_paid'] ?? false ); } /** Return the prepared subject, or null before binding. */ public function subject(): ?\WC_Abstract_Order { return $this->subject; } /** * Test exact prepared-order identity, never an unbound context. * * @param mixed $order Candidate subject. * @return bool */ public function is_subject( $order ): bool { return null !== $this->subject && $this->subject === $order; } }