| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCart\Api\Hasher\Hash; |
| 6 |
use FluentCart\App\App; |
| 7 |
|
| 8 |
class CartCookieHandler |
| 9 |
{ |
| 10 |
public static function handle() |
| 11 |
{ |
| 12 |
$app = App::getInstance(); |
| 13 |
//Set up the cookie before any header info is sent to a client |
| 14 |
$app->addAction('init', function () use ($app) { |
| 15 |
|
| 16 |
if (App::doingRestRequest()) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$cartCookie = \FluentCart\Api\Cookie\Cookie::getCartHash(); |
| 21 |
if (!$cartCookie) { |
| 22 |
\FluentCart\Api\Cookie\Cookie::setCartHash( |
| 23 |
md5(time() . wp_generate_uuid4()) |
| 24 |
); |
| 25 |
} |
| 26 |
}); |
| 27 |
|
| 28 |
|
| 29 |
add_filter('rest_request_before_callbacks', function ($response, $handler, $request) { |
| 30 |
// Only set cookie if not already present |
| 31 |
$cartCookie = \FluentCart\Api\Cookie\Cookie::getCartHash(); |
| 32 |
if (!$cartCookie) { |
| 33 |
\FluentCart\Api\Cookie\Cookie::setCartHash( |
| 34 |
md5(time() . wp_generate_uuid4()) |
| 35 |
); |
| 36 |
} |
| 37 |
// Return null to let the normal REST flow continue |
| 38 |
return null; |
| 39 |
}, 10, 3); |
| 40 |
} |
| 41 |
} |