get_parent_id(); if ( ! $parent_id ) { return null; } $order = self::to_object( $parent_id ); if ( ! $order ) { // Parent deleted or trashed out from under the refund. return null; } } // Anything still not a full order (a future WC order type, or a refund // whose parent is itself something odd) has no billing getters. Refusing // here is what keeps the callers free of instanceof noise. if ( ! $order instanceof \WC_Order ) { return null; } return $order; } /** * Extract an id from an order/refund object or a raw value. * * WC_Abstract_Order, not WC_Order: a refund object must yield its own id * rather than falling through to an (int) cast, which in PHP 8 warns and * evaluates an object to 1 — quietly syncing whatever order has id 1. * * @param \WC_Abstract_Order|int|mixed $order_or_id Order, refund, or id. * * @return int Order id, or 0 when there is none. */ public static function id_of( $order_or_id ) { if ( $order_or_id instanceof \WC_Abstract_Order ) { return (int) $order_or_id->get_id(); } if ( is_scalar( $order_or_id ) ) { return (int) $order_or_id; } return 0; } /** * Load an order object from an id, passing objects through untouched. * * @param \WC_Abstract_Order|int|mixed $order_or_id Order, refund, or id. * * @return \WC_Abstract_Order|null */ private static function to_object( $order_or_id ) { if ( $order_or_id instanceof \WC_Abstract_Order ) { return $order_or_id; } $id = self::id_of( $order_or_id ); if ( ! $id ) { return null; } $order = wc_get_order( $id ); return $order instanceof \WC_Abstract_Order ? $order : null; } }