$changes get_changes(). * @return array */ public static function input( array $changes ): array { $input = array(); foreach ( $changes as $key => $value ) { $safe = self::safe( $value ); if ( null !== $safe || null === $value ) { $input[ (string) $key ] = $safe; } } return $input; } /** * The changes for the replay envelope: dates written out, anything * else kept as it is. * * @param array $changes get_changes(). * @return array */ public static function envelope( array $changes ): array { $envelope = array(); foreach ( $changes as $key => $value ) { $envelope[ $key ] = self::safe( $value, false ) ?? $value; } return $envelope; } /** * Meta changed but not saved yet: key => new value, null for a key * being deleted. get_changes() leaves meta out, and a save with only * meta changes still writes. * * @param object $data A WC_Data object. * @return array */ public static function meta( object $data ): array { $pending = ( fn() => property_exists( $this, 'meta_data' ) && is_array( $this->meta_data ) ? $this->meta_data : array() )->call( $data ); $meta = array(); foreach ( $pending as $entry ) { if ( ! $entry instanceof \WC_Meta_Data ) { continue; } $data = $entry->get_data(); if ( empty( $data['id'] ) || array() !== $entry->get_changes() || null === $data['value'] ) { $meta[ (string) $data['key'] ] = $data['value']; } } return $meta; } /** * An order's line items changed, added or removed but not saved yet. * * @param \WC_Order $order The order. * @return list<\WC_Order_Item> */ public static function items( \WC_Order $order ): array { list( $groups, $deleted ) = ( fn() => array( is_array( $this->items ) ? $this->items : array(), is_array( $this->items_to_delete ) ? $this->items_to_delete : array() ) )->call( $order ); $items = array(); foreach ( array_merge( array_merge( ...array_values( array_map( 'array_values', $groups ) ) ), array_values( $deleted ) ) as $item ) { if ( $item instanceof \WC_Order_Item && ( 0 === (int) $item->get_id() || array() !== $item->get_changes() || array() !== self::meta( $item ) || in_array( $item, $deleted, true ) ) ) { $items[] = $item; } } return $items; } /** * A JSON-safe form of a value, or null when it has none. * * @param mixed $value Value. * @param bool $describe Describe downloads and attributes (input), or * leave them as objects (envelope). */ private static function safe( mixed $value, bool $describe = true ): mixed { if ( $value instanceof \DateTimeInterface ) { return $value->format( DATE_ATOM ); } if ( is_scalar( $value ) ) { return $value; } // What a rule may need to see of a product's downloads and // attributes: where a download points, what an attribute offers. if ( $describe && $value instanceof \WC_Product_Download ) { return array( 'name' => $value->get_name(), 'file' => $value->get_file(), ); } if ( $describe && $value instanceof \WC_Product_Attribute ) { return array( 'name' => $value->get_name(), 'options' => array_map( 'strval', $value->get_options() ), ); } if ( ! is_array( $value ) ) { return null; } $safe = array(); foreach ( $value as $key => $item ) { $converted = self::safe( $item, $describe ); if ( null === $converted && null !== $item ) { return null; } $safe[ $key ] = $converted; } return $safe; } }