class-platform-checkout-session.php
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* Class WC_Payments_Session.
*
* @package WooCommerce\Payments
*/
namespace WCPay\Platform_Checkout;
/**
* Class responsible for handling platform checkout sessions.
* This class should be loaded as soon as possible so the correct session is loaded.
* So don't load it in the WC_Payments::init() function.
*/
class Platform_Checkout_Session {
const PLATFORM_CHECKOUT_SESSION_COOKIE_NAME = 'platform_checkout_session';
/**
* Init the hooks.
*
* @return void
*/
public static function init() {
add_filter( 'determine_current_user', [ __CLASS__, 'determine_current_user_for_platform_checkout' ] );
add_filter( 'woocommerce_cookie', [ __CLASS__, 'determine_session_cookie_for_platform_checkout' ] );
}
/**
* Sets the current user as the user sent via the api from WooPay if present.
*
* @param \WP_User|null|int $user user to be used during the request.
*
* @return \WP_User|null|int
*/
public static function determine_current_user_for_platform_checkout( $user ) {
if ( $user ) {
return $user;
}
if ( ! isset( $_SERVER['HTTP_X_WCPAY_PLATFORM_CHECKOUT_USER'] ) || ! is_numeric( $_SERVER['HTTP_X_WCPAY_PLATFORM_CHECKOUT_USER'] ) ) {
return null;
}
return (int) $_SERVER['HTTP_X_WCPAY_PLATFORM_CHECKOUT_USER'];
}
/**
* Tells WC to use platform checkout session cookie if the header is present.
*
* @param string $cookie_hash Default cookie hash.
*
* @return string
*/
public static function determine_session_cookie_for_platform_checkout( $cookie_hash ) {
if ( isset( $_SERVER['HTTP_X_WCPAY_PLATFORM_CHECKOUT_USER'] ) && 0 === (int) $_SERVER['HTTP_X_WCPAY_PLATFORM_CHECKOUT_USER'] ) {
return self::PLATFORM_CHECKOUT_SESSION_COOKIE_NAME;
}
return $cookie_hash;
}
}