Skip to content

Commit

Permalink
Move mt_get_cart to data utilities; handle cart expiration in mt_get_…
Browse files Browse the repository at this point in the history
…cart
  • Loading branch information
joedolson committed Jan 25, 2024
1 parent 63b2887 commit b6e07e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
44 changes: 44 additions & 0 deletions src/includes/data-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function mt_delete_data( $data = 'cart' ) {
* @return array|mixed
*/
function mt_get_data( $type, $user_ID = false ) {
// Get information about a specific user.
if ( $user_ID ) {
$data = get_user_meta( $user_ID, "_mt_user_$type", true );
} else {
Expand Down Expand Up @@ -170,6 +171,49 @@ function mt_get_data( $type, $user_ID = false ) {
return $data;
}


/**
* Get saved cart data for user.
*
* @param bool|int $user_ID User ID.
* @param bool|string $cart_id Cart identifier.
*
* @return array|mixed
*/
function mt_get_cart( $user_ID = false, $cart_id = false ) {
$cart = array();
$unique_id = mt_get_unique_id();
if ( $user_ID ) {
// Logged-in user data is saved in user meta.
$cart = get_user_meta( $user_ID, '_mt_user_cart', true );
} elseif ( ! $user_ID && $cart_id ) {
// Public data is saved in transients.
$cart = get_transient( 'mt_' . $cart_id . '_cart' );
} else {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$data_age = get_user_meta( $current_user->ID, '_mt_user_init_expiration', true );
if ( $data_age && time() > $data_age ) {
delete_user_meta( $current_user->ID, '_mt_user_cart' );
delete_user_meta( $current_user->ID, '_mt_user_init_expiration' );
} else {
$cart = get_user_meta( $current_user->ID, '_mt_user_cart', true );
}
} else {
if ( $unique_id ) {
$cart = get_transient( 'mt_' . $unique_id . '_cart' );
}
}
}
if ( is_user_logged_in() && ! $cart ) {
if ( $unique_id ) {
$cart = get_transient( 'mt_' . $unique_id . '_cart' );
}
}

return ( $cart ) ? $cart : array();
}

add_action( 'init', 'mt_set_user_unique_id' );
/**
* Set a cookie with a random ID for the current user.
Expand Down
43 changes: 4 additions & 39 deletions src/mt-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,21 +674,22 @@ function mt_gateways() {

/**
* Display time remaining before cart expiration.
*
* @return string
*/
function mt_generate_expiration() {
// If this is a post-payment page, remove data and don't display message.
if ( isset( $_GET['payment_id'] ) ) {
mt_delete_data( 'cart' );
return;
return '';
}
$expiration = mt_get_expiration();
$output = '';
if ( 0 === $expiration ) {
return;
return '';
}
if ( ( $expiration - time() ) > 24 * HOUR_IN_SECONDS ) {
// No message if more than a day.
$output = '';
} elseif ( ( $expiration - time() ) > 30 * MINUTE_IN_SECONDS ) {
// translators: amount of time remaining before the cart expires.
$output = '<div class="mt-expiration-notice"><p>' . sprintf( __( 'Your shopping cart will be saved for another %s.', 'my-tickets' ), human_time_diff( time(), $expiration ) ) . '</p></div>';
Expand Down Expand Up @@ -1173,39 +1174,3 @@ function mt_expired( $event, $react = false ) {

return false;
}

/**
* Get saved cart data for user.
*
* @param bool|int $user_ID User ID.
* @param bool|string $cart_id Cart identifier.
*
* @return array|mixed
*/
function mt_get_cart( $user_ID = false, $cart_id = false ) {
$cart = array();
$unique_id = mt_get_unique_id();
if ( $user_ID ) {
// Logged-in user data is saved in user meta.
$cart = get_user_meta( $user_ID, '_mt_user_cart', true );
} elseif ( ! $user_ID && $cart_id ) {
// Public data is saved in transients.
$cart = get_transient( 'mt_' . $cart_id . '_cart' );
} else {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$cart = get_user_meta( $current_user->ID, '_mt_user_cart', true );
} else {
if ( $unique_id ) {
$cart = get_transient( 'mt_' . $unique_id . '_cart' );
}
}
}
if ( is_user_logged_in() && ! $cart ) {
if ( $unique_id ) {
$cart = get_transient( 'mt_' . $unique_id . '_cart' );
}
}

return ( $cart ) ? $cart : array();
}

0 comments on commit b6e07e6

Please sign in to comment.