'pending', 'new_status' => 'processing'] */ public $args; public function __construct( $type, $object, array $args = array() ) { $this->type = $type; $this->object = $object; $this->args = $args; } public function get_order() { return 'order' === $this->type && $this->object instanceof WC_Order ? $this->object : null; } public function get_user() { if ( 'user' === $this->type && $this->object instanceof WP_User ) { return $this->object; } // An order context still has a customer if it belongs to a registered account. $order = $this->get_order(); if ( $order && $order->get_customer_id() ) { return get_userdata( $order->get_customer_id() ); } return null; } public function get_product() { return 'product' === $this->type && $this->object instanceof WC_Product ? $this->object : null; } public function get_arg( $key, $default = null ) { return array_key_exists( $key, $this->args ) ? $this->args[ $key ] : $default; } /** * Minimal, storable representation used to schedule a delayed run and * to rebuild the context once the delay elapses (never serialize the * live WC_Order/WP_User/WC_Product objects themselves). */ public function to_payload() { return array( 'type' => $this->type, 'object_id' => $this->get_object_id(), 'args' => $this->args, ); } public function get_object_id() { if ( $this->get_order() ) { return $this->get_order()->get_id(); } if ( $this->type === 'user' && $this->object instanceof WP_User ) { return $this->object->ID; } if ( $this->get_product() ) { return $this->get_product()->get_id(); } return 0; } public static function rehydrate( array $payload ) { $type = $payload['type'] ?? ''; $object_id = (int) ( $payload['object_id'] ?? 0 ); $args = (array) ( $payload['args'] ?? array() ); switch ( $type ) { case 'order': $order = $object_id ? wc_get_order( $object_id ) : false; return $order ? new self( 'order', $order, $args ) : null; case 'user': $user = $object_id ? get_userdata( $object_id ) : false; return $user ? new self( 'user', $user, $args ) : null; case 'product': $product = $object_id ? wc_get_product( $object_id ) : false; return $product ? new self( 'product', $product, $args ) : null; } return null; } }