roles ) : array(); $labels = array(); foreach ( $slugs as $slug ) { $labels[] = wp_roles()->roles[ $slug ]['name'] ?? ucwords( str_replace( array( '_', '-' ), ' ', $slug ) ); } return array( 'user_role' => $labels[0] ?? 'GUEST', 'user_role_slug' => $slugs[0] ?? 'guest', 'user_roles' => $labels ); } /** Keep JSON-safe item metadata, not serialized PHP objects or product internals. */ public static function primitives( $value, $depth = 0 ) { if ( $depth > 12 ) { return null; } if ( is_array( $value ) ) { $result = array(); foreach ( $value as $key => $item ) { if ( ! is_object( $item ) && ! is_resource( $item ) ) { $result[ $key ] = self::primitives( $item, $depth + 1 ); } } return $result; } return is_scalar( $value ) || null === $value ? $value : null; } public static function item_data( array $line ) { if ( 'minimal' === ACPW_Store::settings()['item_metadata'] ) { $keys = array( 'key', 'product_id', 'variation_id', 'variation', 'quantity', 'data_hash', 'line_tax_data', 'line_subtotal', 'line_subtotal_tax', 'line_total', 'line_tax', 'wdr_free_product', 'wdr_for_rule', 'customer_choice' ); $line = array_intersect_key( $line, array_flip( $keys ) ); } return self::primitives( $line ); } public static function snapshot( array $contact = array() ) { $customer = WC()->customer; $billing = $customer ? $customer->get_billing() : array(); $shipping = $customer ? $customer->get_shipping() : array(); foreach ( $contact as $key => $value ) { if ( str_starts_with( $key, 'billing_' ) ) { $billing[ substr( $key, 8 ) ] = $value; } elseif ( str_starts_with( $key, 'shipping_' ) ) { $shipping[ substr( $key, 9 ) ] = $value; } } $cart = array(); $items = array(); foreach ( WC()->cart->get_cart() as $key => $line ) { $product = $line['data'] ?? null; $line = self::item_data( $line ); $line['data'] = new stdClass(); // Existing The receiver payload compatibility. $cart[ $key ] = $line; $items[] = array_merge( $line, array( 'product_name' => $product instanceof WC_Product ? $product->get_name() : '', 'sku' => $product instanceof WC_Product ? $product->get_sku() : '', 'product_url' => $product instanceof WC_Product ? $product->get_permalink() : '', 'image_url' => $product instanceof WC_Product ? ( wp_get_attachment_image_url( $product->get_image_id(), 'woocommerce_thumbnail' ) ?: '' ) : '', ) ); } $entry = array( 'user_id' => get_current_user_id() ?: null, 'email' => $contact['email'] ?? ( $billing['email'] ?? '' ), 'phone' => $billing['phone'] ?? '', 'cart' => $cart, 'billing_address' => $billing, 'shipping_address' => $shipping, 'timestamp' => time(), 'cart_items' => $items, 'currency' => get_woocommerce_currency(), 'totals' => self::primitives( WC()->cart->get_totals() ), 'coupons' => array_values( WC()->cart->get_applied_coupons() ), ); return self::augment( $entry ); } public static function augment( array $entry ) { $entry['email'] = self::email( $entry ); $entry = array_merge( $entry, self::role( $entry['email'], (int) ( $entry['user_id'] ?? 0 ) ) ); $countries = WC()->countries ? WC()->countries->get_countries() : array(); foreach ( array( 'billing', 'shipping' ) as $type ) { $code = strtoupper( (string) ( $entry[ $type . '_address' ]['country'] ?? '' ) ); if ( 'shipping' === $type && '' === $code ) { $code = $entry['billing_country_code']; } $entry[ $type . '_country_code' ] = $code; $entry[ $type . '_country' ] = isset( $countries[ $code ] ) ? preg_replace( '/\s*\([A-Z]{2}\)\s*$/', '', $countries[ $code ] ) : ''; } return $entry; } public static function hash( array $entry ) { // Product presentation and capture time must not turn background refreshes into activity. unset( $entry['timestamp'], $entry['cart_items'] ); return hash( 'sha256', wp_json_encode( $entry ) ); } /** Order suppression follows basket contents, not price, address or contact refreshes. */ public static function basket_hash( array $entry ) { $lines = array(); foreach ( (array) ( $entry['cart'] ?? array() ) as $line ) { $variation = (array) ( $line['variation'] ?? array() ); ksort( $variation ); $lines[] = wp_json_encode( array( (int) ( $line['product_id'] ?? 0 ), (int) ( $line['variation_id'] ?? 0 ), (float) ( $line['quantity'] ?? 0 ), $variation ) ); } sort( $lines ); $coupons = (array) ( $entry['coupons'] ?? array() ); sort( $coupons ); return hash( 'sha256', wp_json_encode( array( $lines, $coupons ) ) ); } public static function decode( $json ) { $entry = json_decode( $json, true ); if ( ! is_array( $entry ) ) { return null; } foreach ( array( 'cart', 'cart_items' ) as $field ) { foreach ( (array) ( $entry[ $field ] ?? array() ) as $key => $line ) { if ( isset( $line['data'] ) && array() === $line['data'] ) { $entry[ $field ][ $key ]['data'] = new stdClass(); } } } return $entry; } }