Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/FraudPro...
File: SessionDataCollector.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* SessionDataCollector class file.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
declare( strict_types=1 );
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\WooCommerce\Internal\FraudProtection;
[7] Fix | Delete
[8] Fix | Delete
defined( 'ABSPATH' ) || exit;
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Collects comprehensive session and order data for fraud protection analysis.
[12] Fix | Delete
*
[13] Fix | Delete
* This class provides manual data collection for fraud protection events, gathering
[14] Fix | Delete
* session, customer, order, address, and payment information in the exact nested format
[15] Fix | Delete
* required by the WPCOM fraud protection service. All data collection is designed to
[16] Fix | Delete
* degrade gracefully when fields are unavailable, ensuring checkout never fails due to
[17] Fix | Delete
* missing fraud protection data.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 10.5.0
[20] Fix | Delete
* @internal This class is part of the internal API and is subject to change without notice.
[21] Fix | Delete
*/
[22] Fix | Delete
class SessionDataCollector {
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* SessionClearanceManager instance.
[26] Fix | Delete
*
[27] Fix | Delete
* @var SessionClearanceManager
[28] Fix | Delete
*/
[29] Fix | Delete
private SessionClearanceManager $session_clearance_manager;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Initialize with dependencies.
[33] Fix | Delete
*
[34] Fix | Delete
* @internal
[35] Fix | Delete
*
[36] Fix | Delete
* @param SessionClearanceManager $session_clearance_manager The session clearance manager instance.
[37] Fix | Delete
*/
[38] Fix | Delete
final public function init( SessionClearanceManager $session_clearance_manager ): void {
[39] Fix | Delete
$this->session_clearance_manager = $session_clearance_manager;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Collect comprehensive session and order data for fraud protection.
[44] Fix | Delete
*
[45] Fix | Delete
* This method is called manually at specific points in the checkout/payment flow
[46] Fix | Delete
* to gather all relevant data for fraud analysis. It returns data in the nested
[47] Fix | Delete
* format expected by the WPCOM fraud protection service.
[48] Fix | Delete
*
[49] Fix | Delete
* @since 10.5.0
[50] Fix | Delete
*
[51] Fix | Delete
* @param string|null $event_type Optional event type identifier (e.g., 'checkout_started', 'payment_attempt').
[52] Fix | Delete
* @param array $event_data Optional event-specific additional context data.
[53] Fix | Delete
*/
[54] Fix | Delete
public function collect( ?string $event_type = null, array $event_data = array() ): void {
[55] Fix | Delete
// Ensure cart and session are loaded.
[56] Fix | Delete
$this->session_clearance_manager->ensure_cart_loaded();
[57] Fix | Delete
[58] Fix | Delete
$data = array(
[59] Fix | Delete
'event_type' => $event_type,
[60] Fix | Delete
'timestamp' => gmdate( 'Y-m-d H:i:s' ),
[61] Fix | Delete
'event_data' => $event_data,
[62] Fix | Delete
);
[63] Fix | Delete
[64] Fix | Delete
// Save the collected data in the session for fraud analysis tracking, preserving multiple calls.
[65] Fix | Delete
if ( WC()->session instanceof \WC_Session ) {
[66] Fix | Delete
// Retrieve existing data array or initialize if not present.
[67] Fix | Delete
$collected_data = WC()->session->get( 'fraud_protection_collected_data' );
[68] Fix | Delete
if ( ! is_array( $collected_data ) ) {
[69] Fix | Delete
$collected_data = array();
[70] Fix | Delete
}
[71] Fix | Delete
$collected_data[] = $data;
[72] Fix | Delete
$collected_data = $this->trim_to_max_size( $collected_data );
[73] Fix | Delete
WC()->session->set( 'fraud_protection_collected_data', $collected_data );
[74] Fix | Delete
} else {
[75] Fix | Delete
FraudProtectionController::log(
[76] Fix | Delete
'error',
[77] Fix | Delete
'Attempted to save fraud protection data, but no valid WooCommerce session exists.',
[78] Fix | Delete
array(
[79] Fix | Delete
'context' => 'SessionDataCollector::collect',
[80] Fix | Delete
'event_type' => $event_type,
[81] Fix | Delete
'event_data' => $event_data,
[82] Fix | Delete
)
[83] Fix | Delete
);
[84] Fix | Delete
}
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Get all collected fraud protection data from the session.
[89] Fix | Delete
*
[90] Fix | Delete
* Retrieves the array of collected event data stored during this session.
[91] Fix | Delete
* Returns an empty array if no data has been collected or session is unavailable.
[92] Fix | Delete
*
[93] Fix | Delete
* @since 10.5.0
[94] Fix | Delete
*
[95] Fix | Delete
* @param int|null $order_id Optional order ID to include order data in the response.
[96] Fix | Delete
* @return array Array of collected fraud protection event data.
[97] Fix | Delete
*/
[98] Fix | Delete
public function get_collected_data( ?int $order_id = null ): array {
[99] Fix | Delete
$data = array(
[100] Fix | Delete
'wc_version' => WC()->version,
[101] Fix | Delete
'session' => $this->get_session_data(),
[102] Fix | Delete
'customer' => $this->get_customer_data(),
[103] Fix | Delete
'order' => array(),
[104] Fix | Delete
'shipping_address' => $this->get_shipping_address(),
[105] Fix | Delete
'billing_address' => $this->get_billing_address(),
[106] Fix | Delete
'collected_events' => array(),
[107] Fix | Delete
);
[108] Fix | Delete
[109] Fix | Delete
if ( $order_id ) {
[110] Fix | Delete
$data['order'] = $this->get_order_data( $order_id );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
// Calculate base data size to ensure total response stays under limit.
[114] Fix | Delete
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Used for size calculation only.
[115] Fix | Delete
$base_size = strlen( serialize( $data ) );
[116] Fix | Delete
[117] Fix | Delete
if ( WC()->session instanceof \WC_Session ) {
[118] Fix | Delete
$collected_data = WC()->session->get( 'fraud_protection_collected_data' );
[119] Fix | Delete
if ( is_array( $collected_data ) ) {
[120] Fix | Delete
$data['collected_events'] = $this->trim_to_max_size( $collected_data, $base_size );
[121] Fix | Delete
}
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
return $data;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Get current billing country from customer data.
[129] Fix | Delete
*
[130] Fix | Delete
* Reuses the same logic as get_billing_address() but returns only the country.
[131] Fix | Delete
* Tries WC_Customer first, falls back to session data, with graceful error handling.
[132] Fix | Delete
*
[133] Fix | Delete
* @since 10.5.0
[134] Fix | Delete
*
[135] Fix | Delete
* @return string|null Current billing country code or null if unavailable.
[136] Fix | Delete
*/
[137] Fix | Delete
public function get_current_billing_country(): ?string {
[138] Fix | Delete
try {
[139] Fix | Delete
if ( WC()->customer instanceof \WC_Customer ) {
[140] Fix | Delete
$country = WC()->customer->get_billing_country();
[141] Fix | Delete
return ! empty( $country ) ? \sanitize_text_field( $country ) : null;
[142] Fix | Delete
} elseif ( WC()->session instanceof \WC_Session ) {
[143] Fix | Delete
$customer_data = WC()->session->get( 'customer' );
[144] Fix | Delete
if ( is_array( $customer_data ) && ! empty( $customer_data['country'] ) ) {
[145] Fix | Delete
return \sanitize_text_field( $customer_data['country'] );
[146] Fix | Delete
}
[147] Fix | Delete
}
[148] Fix | Delete
} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
[149] Fix | Delete
// Graceful degradation.
[150] Fix | Delete
}
[151] Fix | Delete
return null;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Get current shipping country from customer data.
[156] Fix | Delete
*
[157] Fix | Delete
* Reuses the same logic as get_shipping_address() but returns only the country.
[158] Fix | Delete
* Tries WC_Customer first, falls back to session data, with graceful error handling.
[159] Fix | Delete
*
[160] Fix | Delete
* @since 10.5.0
[161] Fix | Delete
*
[162] Fix | Delete
* @return string|null Current shipping country code or null if unavailable.
[163] Fix | Delete
*/
[164] Fix | Delete
public function get_current_shipping_country(): ?string {
[165] Fix | Delete
try {
[166] Fix | Delete
if ( WC()->customer instanceof \WC_Customer ) {
[167] Fix | Delete
$country = WC()->customer->get_shipping_country();
[168] Fix | Delete
return ! empty( $country ) ? \sanitize_text_field( $country ) : null;
[169] Fix | Delete
} elseif ( WC()->session instanceof \WC_Session ) {
[170] Fix | Delete
$customer_data = WC()->session->get( 'customer' );
[171] Fix | Delete
if ( is_array( $customer_data ) && ! empty( $customer_data['shipping_country'] ) ) {
[172] Fix | Delete
return \sanitize_text_field( $customer_data['shipping_country'] );
[173] Fix | Delete
}
[174] Fix | Delete
}
[175] Fix | Delete
} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
[176] Fix | Delete
// Graceful degradation.
[177] Fix | Delete
}
[178] Fix | Delete
return null;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Get session data including session ID, IP address, email, and user agent.
[183] Fix | Delete
*
[184] Fix | Delete
* Collects session identification and tracking data with graceful degradation
[185] Fix | Delete
* for unavailable fields. Email collection follows the fallback chain:
[186] Fix | Delete
* logged-in user email → session customer data → WC_Customer billing email.
[187] Fix | Delete
*
[188] Fix | Delete
* @since 10.5.0
[189] Fix | Delete
*
[190] Fix | Delete
* @return array Session data array with 6 keys.
[191] Fix | Delete
*/
[192] Fix | Delete
private function get_session_data(): array {
[193] Fix | Delete
try {
[194] Fix | Delete
$session_id = $this->session_clearance_manager->get_session_id();
[195] Fix | Delete
$ip_address = $this->get_ip_address();
[196] Fix | Delete
$email = $this->get_email();
[197] Fix | Delete
$user_agent = $this->get_user_agent();
[198] Fix | Delete
[199] Fix | Delete
/**
[200] Fix | Delete
* $is_user_session is flag that we have a real browser session vs API-based interaction.
[201] Fix | Delete
* We start with a very basic check, but we might need a more sophisticated way to detect it in the future.
[202] Fix | Delete
*/
[203] Fix | Delete
$is_user_session = 'no-session' !== $session_id;
[204] Fix | Delete
[205] Fix | Delete
return array(
[206] Fix | Delete
'session_id' => $session_id,
[207] Fix | Delete
'ip_address' => $ip_address,
[208] Fix | Delete
'email' => $email,
[209] Fix | Delete
'ja3_hash' => null,
[210] Fix | Delete
'user_agent' => $user_agent,
[211] Fix | Delete
'is_user_session' => $is_user_session,
[212] Fix | Delete
);
[213] Fix | Delete
} catch ( \Exception $e ) {
[214] Fix | Delete
// Graceful degradation - return structure with null values.
[215] Fix | Delete
return array(
[216] Fix | Delete
'session_id' => null,
[217] Fix | Delete
'ip_address' => null,
[218] Fix | Delete
'email' => null,
[219] Fix | Delete
'ja3_hash' => null,
[220] Fix | Delete
'user_agent' => null,
[221] Fix | Delete
'is_user_session' => false,
[222] Fix | Delete
);
[223] Fix | Delete
}
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
/**
[227] Fix | Delete
* Get customer data including name, billing email, and order history.
[228] Fix | Delete
*
[229] Fix | Delete
* Collects customer identification and history data with graceful degradation.
[230] Fix | Delete
* Tries WC_Customer object first, then falls back to session data if values are empty.
[231] Fix | Delete
* Includes lifetime_order_count which counts all orders regardless of status.
[232] Fix | Delete
*
[233] Fix | Delete
* @since 10.5.0
[234] Fix | Delete
*
[235] Fix | Delete
* @return array Customer data array with 4 keys.
[236] Fix | Delete
*/
[237] Fix | Delete
private function get_customer_data(): array {
[238] Fix | Delete
$customer_data = array(
[239] Fix | Delete
'first_name' => null,
[240] Fix | Delete
'last_name' => null,
[241] Fix | Delete
'billing_email' => null,
[242] Fix | Delete
'lifetime_order_count' => 0,
[243] Fix | Delete
);
[244] Fix | Delete
try {
[245] Fix | Delete
$lifetime_order_count = 0;
[246] Fix | Delete
[247] Fix | Delete
// Try WC_Customer object first.
[248] Fix | Delete
if ( WC()->customer instanceof \WC_Customer ) {
[249] Fix | Delete
if ( WC()->customer->get_id() > 0 ) {
[250] Fix | Delete
// We need to reload the customer so it uses the correct data store to count the orders.
[251] Fix | Delete
$customer = new \WC_Customer( WC()->customer->get_id() );
[252] Fix | Delete
$lifetime_order_count = $customer->get_order_count();
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
$customer_data = array_merge(
[256] Fix | Delete
$customer_data,
[257] Fix | Delete
array(
[258] Fix | Delete
'first_name' => \sanitize_text_field( WC()->customer->get_billing_first_name() ),
[259] Fix | Delete
'last_name' => \sanitize_text_field( WC()->customer->get_billing_last_name() ),
[260] Fix | Delete
'billing_email' => \sanitize_email( \WC()->customer->get_billing_email() ),
[261] Fix | Delete
'lifetime_order_count' => $lifetime_order_count,
[262] Fix | Delete
)
[263] Fix | Delete
);
[264] Fix | Delete
[265] Fix | Delete
} elseif ( WC()->session instanceof \WC_Session ) {
[266] Fix | Delete
// Fallback to session customer data if WC_Customer not available.
[267] Fix | Delete
$customer_session_data = WC()->session->get( 'customer' );
[268] Fix | Delete
if ( is_array( $customer_session_data ) ) {
[269] Fix | Delete
$customer_data = array_merge(
[270] Fix | Delete
$customer_data,
[271] Fix | Delete
array(
[272] Fix | Delete
'first_name' => \sanitize_text_field( $customer_session_data['first_name'] ?? null ),
[273] Fix | Delete
'last_name' => \sanitize_text_field( $customer_session_data['last_name'] ?? null ),
[274] Fix | Delete
'billing_email' => \sanitize_email( $customer_session_data['email'] ?? null ),
[275] Fix | Delete
)
[276] Fix | Delete
);
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
[280] Fix | Delete
// Graceful degradation - return as much data as possible.
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
return $customer_data;
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
/**
[287] Fix | Delete
* Get order data including totals, currency, cart hash, and cart items.
[288] Fix | Delete
*
[289] Fix | Delete
* Collects comprehensive order information from the cart with graceful degradation.
[290] Fix | Delete
* Calculates shipping_tax_rate from shipping tax and shipping total. Sets customer_id
[291] Fix | Delete
* to 'guest' for non-logged-in users.
[292] Fix | Delete
*
[293] Fix | Delete
* @since 10.5.0
[294] Fix | Delete
*
[295] Fix | Delete
* @param int|null $order_id_from_event Optional order ID from event data.
[296] Fix | Delete
* @return array Order data array with 11 keys including items array.
[297] Fix | Delete
*/
[298] Fix | Delete
private function get_order_data( ?int $order_id_from_event = null ): array {
[299] Fix | Delete
try {
[300] Fix | Delete
// Initialize default values.
[301] Fix | Delete
$order_id = $order_id_from_event;
[302] Fix | Delete
$customer_id = 'guest';
[303] Fix | Delete
$total = 0;
[304] Fix | Delete
$items_total = 0;
[305] Fix | Delete
$shipping_total = 0;
[306] Fix | Delete
$tax_total = 0;
[307] Fix | Delete
$shipping_tax_rate = null;
[308] Fix | Delete
$discount_total = 0;
[309] Fix | Delete
$currency = WC()->call_function( 'get_woocommerce_currency' );
[310] Fix | Delete
$cart_hash = null;
[311] Fix | Delete
$items = array();
[312] Fix | Delete
[313] Fix | Delete
// Get customer ID from WooCommerce customer object if available.
[314] Fix | Delete
// We don't need to fallback to session data here, because customer id won't be stored there.
[315] Fix | Delete
if ( WC()->customer instanceof \WC_Customer ) {
[316] Fix | Delete
$id = WC()->customer->get_id();
[317] Fix | Delete
if ( $id ) {
[318] Fix | Delete
$customer_id = $id;
[319] Fix | Delete
}
[320] Fix | Delete
}
[321] Fix | Delete
// Get cart data if available.
[322] Fix | Delete
if ( WC()->cart instanceof \WC_Cart ) {
[323] Fix | Delete
$items_total = (float) WC()->cart->get_subtotal();
[324] Fix | Delete
$shipping_total = (float) WC()->cart->get_shipping_total();
[325] Fix | Delete
$tax_total = (float) WC()->cart->get_cart_contents_tax();
[326] Fix | Delete
$discount_total = (float) WC()->cart->get_discount_total();
[327] Fix | Delete
$cart_hash = WC()->cart->get_cart_hash();
[328] Fix | Delete
$items = $this->get_cart_items();
[329] Fix | Delete
$total = (float) WC()->cart->get_total( 'edit' );
[330] Fix | Delete
[331] Fix | Delete
// Calculate shipping_tax_rate.
[332] Fix | Delete
$shipping_tax = (float) WC()->cart->get_shipping_tax();
[333] Fix | Delete
if ( $shipping_total > 0 && $shipping_tax > 0 ) {
[334] Fix | Delete
$shipping_tax_rate = $shipping_tax / $shipping_total;
[335] Fix | Delete
}
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
return array(
[339] Fix | Delete
'order_id' => $order_id,
[340] Fix | Delete
'customer_id' => $customer_id,
[341] Fix | Delete
'total' => $total,
[342] Fix | Delete
'items_total' => $items_total,
[343] Fix | Delete
'shipping_total' => $shipping_total,
[344] Fix | Delete
'tax_total' => $tax_total,
[345] Fix | Delete
'shipping_tax_rate' => $shipping_tax_rate,
[346] Fix | Delete
'discount_total' => $discount_total,
[347] Fix | Delete
'currency' => $currency,
[348] Fix | Delete
'cart_hash' => $cart_hash,
[349] Fix | Delete
'items' => $items,
[350] Fix | Delete
);
[351] Fix | Delete
} catch ( \Exception $e ) {
[352] Fix | Delete
// Graceful degradation - return structure with default values.
[353] Fix | Delete
return array(
[354] Fix | Delete
'order_id' => null,
[355] Fix | Delete
'customer_id' => 'guest',
[356] Fix | Delete
'total' => 0,
[357] Fix | Delete
'items_total' => 0,
[358] Fix | Delete
'shipping_total' => 0,
[359] Fix | Delete
'tax_total' => 0,
[360] Fix | Delete
'shipping_tax_rate' => null,
[361] Fix | Delete
'discount_total' => 0,
[362] Fix | Delete
'currency' => WC()->call_function( 'get_woocommerce_currency' ),
[363] Fix | Delete
'cart_hash' => null,
[364] Fix | Delete
'items' => array(),
[365] Fix | Delete
);
[366] Fix | Delete
}
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
/**
[370] Fix | Delete
* Get cart items with detailed product information.
[371] Fix | Delete
*
[372] Fix | Delete
* Iterates through cart items and extracts comprehensive product data including
[373] Fix | Delete
* name, description, category, SKU, pricing, quantities, and WooCommerce-specific
[374] Fix | Delete
* attributes. Returns array of item objects with 12 fields each.
[375] Fix | Delete
*
[376] Fix | Delete
* @since 10.5.0
[377] Fix | Delete
*
[378] Fix | Delete
* @return array Array of cart item objects with detailed product information.
[379] Fix | Delete
*/
[380] Fix | Delete
private function get_cart_items(): array {
[381] Fix | Delete
$items = array();
[382] Fix | Delete
[383] Fix | Delete
try {
[384] Fix | Delete
if ( ! WC()->cart instanceof \WC_Cart ) {
[385] Fix | Delete
return $items;
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
foreach ( WC()->cart->get_cart() as $cart_item ) {
[389] Fix | Delete
try {
[390] Fix | Delete
$product = $cart_item['data'] ?? null;
[391] Fix | Delete
[392] Fix | Delete
if ( ! $product instanceof \WC_Product ) {
[393] Fix | Delete
continue;
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
$quantity = $cart_item['quantity'] ?? 1;
[397] Fix | Delete
[398] Fix | Delete
// Calculate per-unit amounts.
[399] Fix | Delete
$unit_price = (float) $product->get_price();
[400] Fix | Delete
$line_tax = $cart_item['line_tax'] ?? 0;
[401] Fix | Delete
$unit_tax_amount = $quantity > 0 ? ( (float) $line_tax / $quantity ) : 0;
[402] Fix | Delete
$line_discount = $cart_item['line_subtotal'] - $cart_item['line_total'];
[403] Fix | Delete
$unit_discount_amount = $quantity > 0 ? ( (float) $line_discount / $quantity ) : 0;
[404] Fix | Delete
$category = $this->get_product_category_names( $product );
[405] Fix | Delete
[406] Fix | Delete
$items[] = array(
[407] Fix | Delete
'name' => $product->get_name() ? $product->get_name() : null,
[408] Fix | Delete
'description' => $product->get_description() ? $product->get_description() : null,
[409] Fix | Delete
'category' => $category,
[410] Fix | Delete
'sku' => $product->get_sku() ? $product->get_sku() : null,
[411] Fix | Delete
'quantity' => $quantity,
[412] Fix | Delete
'unit_price' => $unit_price,
[413] Fix | Delete
'unit_tax_amount' => $unit_tax_amount,
[414] Fix | Delete
'unit_discount_amount' => $unit_discount_amount,
[415] Fix | Delete
'product_type' => $product->get_type() ? $product->get_type() : null,
[416] Fix | Delete
'is_virtual' => $product->is_virtual(),
[417] Fix | Delete
'is_downloadable' => $product->is_downloadable(),
[418] Fix | Delete
'attributes' => $product->get_attributes() ? $product->get_attributes() : array(),
[419] Fix | Delete
);
[420] Fix | Delete
} catch ( \Exception $e ) {
[421] Fix | Delete
// Skip this item if there's an error, continue with next item.
[422] Fix | Delete
continue;
[423] Fix | Delete
}
[424] Fix | Delete
}
[425] Fix | Delete
} catch ( \Exception $e ) {
[426] Fix | Delete
// Return empty array on error.
[427] Fix | Delete
return array();
[428] Fix | Delete
}
[429] Fix | Delete
[430] Fix | Delete
return $items;
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
/**
[434] Fix | Delete
* Get billing address from customer data.
[435] Fix | Delete
*
[436] Fix | Delete
* Collects billing address fields from WC_Customer object with graceful degradation.
[437] Fix | Delete
* Returns array with 6 address fields, sanitized with sanitize_text_field().
[438] Fix | Delete
*
[439] Fix | Delete
* @since 10.5.0
[440] Fix | Delete
*
[441] Fix | Delete
* @return array Billing address array with 6 keys.
[442] Fix | Delete
*/
[443] Fix | Delete
private function get_billing_address(): array {
[444] Fix | Delete
$billing_data = array(
[445] Fix | Delete
'first_name' => null,
[446] Fix | Delete
'last_name' => null,
[447] Fix | Delete
'address' => null,
[448] Fix | Delete
'address_1' => null,
[449] Fix | Delete
'address_2' => null,
[450] Fix | Delete
'city' => null,
[451] Fix | Delete
'state' => null,
[452] Fix | Delete
'country' => null,
[453] Fix | Delete
'phone' => null,
[454] Fix | Delete
'postcode' => null,
[455] Fix | Delete
);
[456] Fix | Delete
[457] Fix | Delete
try {
[458] Fix | Delete
// Try WC_Customer object first.
[459] Fix | Delete
if ( WC()->customer instanceof \WC_Customer ) {
[460] Fix | Delete
$billing_data = array_merge(
[461] Fix | Delete
$billing_data,
[462] Fix | Delete
array(
[463] Fix | Delete
'first_name' => \sanitize_text_field( WC()->customer->get_billing_first_name() ),
[464] Fix | Delete
'last_name' => \sanitize_text_field( WC()->customer->get_billing_last_name() ),
[465] Fix | Delete
'address_1' => \sanitize_text_field( WC()->customer->get_billing_address_1() ),
[466] Fix | Delete
'address_2' => \sanitize_text_field( WC()->customer->get_billing_address_2() ),
[467] Fix | Delete
'city' => \sanitize_text_field( WC()->customer->get_billing_city() ),
[468] Fix | Delete
'state' => \sanitize_text_field( WC()->customer->get_billing_state() ),
[469] Fix | Delete
'country' => \sanitize_text_field( WC()->customer->get_billing_country() ),
[470] Fix | Delete
'phone' => \sanitize_text_field( WC()->customer->get_billing_phone() ),
[471] Fix | Delete
'postcode' => \sanitize_text_field( WC()->customer->get_billing_postcode() ),
[472] Fix | Delete
)
[473] Fix | Delete
);
[474] Fix | Delete
} elseif ( WC()->session instanceof \WC_Session ) {
[475] Fix | Delete
// Fallback to session customer data if WC_Customer not available.
[476] Fix | Delete
$customer_data = WC()->session->get( 'customer' );
[477] Fix | Delete
if ( is_array( $customer_data ) ) {
[478] Fix | Delete
$billing_data = array_merge(
[479] Fix | Delete
$billing_data,
[480] Fix | Delete
array(
[481] Fix | Delete
'first_name' => \sanitize_text_field( $customer_data['first_name'] ?? null ),
[482] Fix | Delete
'last_name' => \sanitize_text_field( $customer_data['last_name'] ?? null ),
[483] Fix | Delete
'address' => \sanitize_text_field( $customer_data['address'] ?? null ),
[484] Fix | Delete
'address_1' => \sanitize_text_field( $customer_data['address_1'] ?? null ),
[485] Fix | Delete
'address_2' => \sanitize_text_field( $customer_data['address_2'] ?? null ),
[486] Fix | Delete
'city' => \sanitize_text_field( $customer_data['city'] ?? null ),
[487] Fix | Delete
'state' => \sanitize_text_field( $customer_data['state'] ?? null ),
[488] Fix | Delete
'country' => \sanitize_text_field( $customer_data['country'] ?? null ),
[489] Fix | Delete
'phone' => \sanitize_text_field( $customer_data['phone'] ?? null ),
[490] Fix | Delete
'postcode' => \sanitize_text_field( $customer_data['postcode'] ?? null ),
[491] Fix | Delete
)
[492] Fix | Delete
);
[493] Fix | Delete
}
[494] Fix | Delete
}
[495] Fix | Delete
} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
[496] Fix | Delete
// Graceful degradation - prevents any errors from being thrown.
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function