| 1 |
<?php |
| 2 |
|
| 3 |
namespace RTFL\App\Controllers\Store; |
| 4 |
|
| 5 |
|
| 6 |
defined( 'ABSPATH' ) or exit; |
| 7 |
|
| 8 |
|
| 9 |
use RTFL\App\Controllers\Controller; |
| 10 |
use RTFL\App\Helpers\Customer as CustomerHelper; |
| 11 |
use RTFL\App\Helpers\Platform; |
| 12 |
use RTFL\App\Helpers\Settings; |
| 13 |
|
| 14 |
|
| 15 |
class Users implements Controller { |
| 16 |
public static function register() { |
| 17 |
add_action( 'wp_login', [ self::class, 'setUserDateOnLogin' ], 10, 2 ); |
| 18 |
add_action( 'user_register', [ self::class, 'setUserData' ] ); |
| 19 |
add_action( 'wp_logout', [ self::class, 'removeUserData' ] ); |
| 20 |
} |
| 21 |
|
| 22 |
public static function setUserDateOnLogin( $user_login, $user ) { |
| 23 |
if ( ! empty( $user ) && is_object( $user ) && isset( $user->ID ) ) { |
| 24 |
self::setUserData( $user->ID ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Set the user data. |
| 31 |
* |
| 32 |
* @param string $user_name User name. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public static function setUserData( $user_id ) { |
| 36 |
if ( empty( $user_id ) && ! is_int( $user_id ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
$session = Settings::getStorage(); |
| 40 |
$cart_token = $session->get( '_retainful_user_cart_token' ); |
| 41 |
if ( ! empty( $cart_token ) ) { |
| 42 |
Platform::updateUserMeta( $user_id, '_retainful_user_cart_token', $cart_token ); |
| 43 |
} |
| 44 |
$cart_created_at = $session->get( 'retainful_cart_created_at' ); |
| 45 |
if ( ! empty( $cart_created_at ) ) { |
| 46 |
Platform::updateUserMeta( $user_id, '_retainful_cart_tracking_started_at', $cart_created_at ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Remove user data on local storage. |
| 53 |
*/ |
| 54 |
public static function removeUserData( $user_id ) { |
| 55 |
if ( empty( $user_id ) && ! is_int( $user_id ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
$session = Settings::getStorage(); |
| 59 |
$session->remove( '_retainful_user_cart_token' ); |
| 60 |
$session->remove( 'retainful_cart_created_at' ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Order payment completed - This is a paying customer. |
| 65 |
* |
| 66 |
* @param int $order_id Order id. |
| 67 |
* |
| 68 |
*/ |
| 69 |
public static function setCustomerPayingForOrder( $order_id ) { |
| 70 |
if ( function_exists( 'wc_paying_customer' ) ) { |
| 71 |
wc_paying_customer( intval( $order_id ) ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
} |