| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Cart; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Cart_Session |
| 7 |
* Handles cart session management for the Booktics plugin |
| 8 |
* |
| 9 |
* @package Booktics\Cart |
| 10 |
*/ |
| 11 |
class Cart_Session { |
| 12 |
|
| 13 |
/** |
| 14 |
* Session key used to store cart data in a PHP session |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
const SESSION_KEY = 'booktics_cart'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Get cart data from session |
| 22 |
* |
| 23 |
* @return array{items: array, total: float} |
| 24 |
*/ |
| 25 |
public static function get() { |
| 26 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized via sanitize_session_data() on next line. |
| 27 |
$raw_data = ! empty( $_SESSION[ self::SESSION_KEY ] ) ? wp_unslash( $_SESSION[ self::SESSION_KEY ] ) : null; |
| 28 |
$session_data = self::sanitize_session_data( $raw_data ); |
| 29 |
|
| 30 |
if ( ! is_array( $session_data ) ) { |
| 31 |
return array( |
| 32 |
'items' => array(), |
| 33 |
'total' => 0.00, |
| 34 |
'coupon' => '', |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
return $session_data; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Sanitize raw cart session data |
| 43 |
* |
| 44 |
* @param mixed $data Raw data retrieved from the session. |
| 45 |
* |
| 46 |
* @return mixed Sanitized array or null if input is not an array. |
| 47 |
*/ |
| 48 |
private static function sanitize_session_data( $data ) { |
| 49 |
if ( ! is_array( $data ) ) { |
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
$sanitized_items = array(); |
| 54 |
|
| 55 |
if ( ! empty( $data['items'] ) && is_array( $data['items'] ) ) { |
| 56 |
foreach ( $data['items'] as $item ) { |
| 57 |
if ( ! is_array( $item ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$sanitized_item = array( |
| 62 |
'uuid' => sanitize_text_field( $item['uuid'] ?? '' ), |
| 63 |
'service_id' => absint( $item['service_id'] ?? 0 ), |
| 64 |
'team_member_id' => absint( $item['team_member_id'] ?? 0 ), |
| 65 |
'date' => sanitize_text_field( $item['date'] ?? '' ), |
| 66 |
'start_time' => sanitize_text_field( $item['start_time'] ?? '' ), |
| 67 |
'end_time' => sanitize_text_field( $item['end_time'] ?? '' ), |
| 68 |
'price' => floatval( $item['price'] ?? 0 ), |
| 69 |
'subtotal' => floatval( $item['subtotal'] ?? 0 ), |
| 70 |
'total' => floatval( $item['total'] ?? 0 ), |
| 71 |
); |
| 72 |
|
| 73 |
$sanitized_item = apply_filters( 'booktics_pro_sanitize_cart_item', $sanitized_item, $item ); |
| 74 |
|
| 75 |
$sanitized_items[] = $sanitized_item; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return array( |
| 80 |
'items' => $sanitized_items, |
| 81 |
'total' => floatval( $data['total'] ?? 0.00 ), |
| 82 |
'coupon' => sanitize_text_field( $data['coupon'] ?? '' ), |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Set cart data in session |
| 88 |
* |
| 89 |
* @param array $cart Cart data to store in session |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public static function set( array $cart ) { |
| 94 |
$_SESSION[ self::SESSION_KEY ] = $cart; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Clear cart data from session |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public static function clear() { |
| 103 |
unset( $_SESSION[ self::SESSION_KEY ] ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Add an item to the cart session |
| 108 |
* |
| 109 |
* @param array $item |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public static function add_item( array $item ): array { |
| 114 |
$cart = self::get(); |
| 115 |
$cart['items'][] = $item; |
| 116 |
$cart['total'] += $item['subtotal']; |
| 117 |
$cart = apply_filters( 'booktics_pro_cart_session', $cart ); |
| 118 |
|
| 119 |
self::set( $cart ); |
| 120 |
|
| 121 |
return end( $cart['items'] ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Delete an item from the cart session |
| 126 |
* |
| 127 |
* @param string $id |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public static function delete_item( string $id ) { |
| 132 |
$cart = self::get(); |
| 133 |
$cart['items'] = array_values( |
| 134 |
array_filter( |
| 135 |
$cart['items'], |
| 136 |
function ( $item ) use ( $id ) { |
| 137 |
return $item['uuid'] != $id; |
| 138 |
} |
| 139 |
) |
| 140 |
); |
| 141 |
$cart['total'] = array_reduce( |
| 142 |
$cart['items'], function ( $total, $item ) { |
| 143 |
return $total + $item['price']; |
| 144 |
}, 0.00 |
| 145 |
); |
| 146 |
|
| 147 |
self::set( $cart ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Edit an item in the cart session |
| 152 |
* |
| 153 |
* @param string $uuid UUID of the item to edit |
| 154 |
* @param array $new_data New data to update the item with |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public static function edit_item( string $uuid, array $new_data ) { |
| 159 |
$cart = self::get(); |
| 160 |
$cart['total'] = 0; |
| 161 |
|
| 162 |
$cart['items'] = array_map( |
| 163 |
function ( $item ) use ( $uuid, $new_data, &$cart ) { |
| 164 |
if ( $item['uuid'] === $uuid ) { |
| 165 |
$item = array_merge( $item, $new_data ); |
| 166 |
} |
| 167 |
$cart['total'] += $item['subtotal']; |
| 168 |
|
| 169 |
return $item; |
| 170 |
}, $cart['items'] |
| 171 |
); |
| 172 |
$cart = apply_filters( 'booktics_pro_cart_session', $cart ); |
| 173 |
self::set( $cart ); |
| 174 |
} |
| 175 |
} |
| 176 |
|