orders = $orders ?? Orders::reader(); } /** * The facts this provider declares. * * @param StoreCurrency $currency The store's money. * @return array> */ public static function declarations( StoreCurrency $currency ): array { return array( 'order.total_minor' => Orders::money( $currency, 'Order total' ), 'order.status_from' => array( 'type' => 'string', 'label' => 'Order status before', ), 'order.status_to' => array( 'type' => 'string', 'label' => 'Order status requested', ), 'order.customer_id' => array( 'type' => 'number', 'label' => 'Customer id', ), 'order.sets_paid' => array( 'type' => 'bool', 'label' => 'Marks the order paid', ), ); } /** * Facts about an order write. * * @param ActionDescriptor $action The classified action. * @return array */ public function __invoke( ActionDescriptor $action ): array { if ( 'read' === $action->action_cat ) { return array(); } $order = null === $action->entity_id ? null : ( $this->orders )( $action->entity_id ); if ( null !== $action->entity_id && null === $order ) { return array(); } $facts = array(); $from = null; if ( null !== $order ) { $from = self::status( $order->get_status() ); $facts['order.status_from'] = $from; $total = Money::to_minor( (string) $order->get_total(), $this->currency->decimals() ); $base = null === $total ? null : $this->currency->to_base( $total, $order ); if ( null !== $base ) { $facts['order.total_minor'] = $base; } } $to = isset( $action->input['status'] ) && is_string( $action->input['status'] ) ? self::status( $action->input['status'] ) : null; if ( null !== $to ) { $facts['order.status_to'] = $to; } $customer = WcBatchExpander::id( $action->input['customer_id'] ?? null ) ?? ( null === $order ? null : (int) $order->get_customer_id() ); if ( null !== $customer ) { $facts['order.customer_id'] = $customer; } if ( 'delete' !== $action->action_cat ) { $facts['order.sets_paid'] = WcRouteClassifier::truthy( $action->input['set_paid'] ?? null ) || ( in_array( $to, self::PAID, true ) && ( null === $order || 'pending' === $from ) ); } return $facts; } /** * A status without WooCommerce's "wc-" prefix. * * @param string $status Status. */ private static function status( string $status ): string { return substr( str_starts_with( $status, 'wc-' ) ? substr( $status, 3 ) : $status, 0, 1024 ); } }