Skip to content

Commit

Permalink
Start laying in output to handle cart expirations & extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Nov 24, 2023
1 parent bf9e858 commit a0aa471
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
58 changes: 53 additions & 5 deletions src/includes/data-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ function mt_save_data( $passed, $type = 'cart', $override = false ) {
delete_transient( 'mt_' . $unique_id . '_' . $type );
}
set_transient( 'mt_' . $unique_id . '_' . $type, $save, time() + $expiration );

if ( ! get_transient( 'mt_' . $unique_id . '_expiration' ) ) {
set_transient( 'mt_' . $unique_id . '_expiration', time() + $expiration, time() + $expiration );
}
return true;
}
}


/**
* Abstract function to delete data. Defaults to delete user's shopping cart.
*
Expand Down Expand Up @@ -90,10 +91,10 @@ function mt_get_data( $type, $user_ID = false ) {
} else {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$data_age = get_user_meta( $current_user->ID, "_mt_user_init_$type", true );
$data_age = get_user_meta( $current_user->ID, "_mt_user_init_expiration", true );
if ( ! $data_age ) {
$expiration = mt_expiration_window();
update_user_meta( $current_user->ID, "_mt_user_init_$type", time() + $expiration );
update_user_meta( $current_user->ID, "_mt_user_init_expiration", time() + $expiration );
}
if ( time() > $data_age ) {
// Expire user's cart after the data ages out.
Expand Down Expand Up @@ -191,7 +192,7 @@ function mt_expiration_window() {
$expiration = $options['mt_expiration'];
// Doesn't support less than 10 minutes.
if ( ! $expiration || (int) $expiration < 600 ) {
$return = WEEK_IN_SECONDS;
$expiration = WEEK_IN_SECONDS;
}
$return = absint( $expiration );
/**
Expand All @@ -207,3 +208,50 @@ function mt_expiration_window() {

return $expiration;
}

/**
* Get cart expiration timestamp.
*/
function mt_get_expiration() {
$expires_at = 0;
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$expires_at = get_user_meta( $current_user->ID, "_mt_user_init_expiration", true );
} else {
$unique_id = mt_get_unique_id();
if ( $unique_id ) {
$expires_at = get_transient( 'mt_' . $unique_id . '_expiration' );
}
}

return absint( $expires_at );
}

/**
* Check whether a user's cart or payment info is expired at init.
*/
function mt_is_cart_expired() {
$types = array( 'cart', 'payment', 'offline-payment' );
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
foreach ( $types as $type ) {
$data_age = get_user_meta( $current_user->ID, "_mt_user_init_$type", true );
if ( time() > $data_age ) {
// Expire user's cart after the data ages out.
delete_user_meta( $current_user->ID, "_mt_user_$type" );
}
}
} else {
$unique_id = mt_get_unique_id();
if ( $unique_id ) {
$expiration = get_transient( 'mt_' . $unique_id . '_expiration' );
if ( time() > $expiration ) {
delete_transient( 'mt_' . $unique_id . '_expiration' );
foreach ( $types as $type ) {
delete_transient( 'mt_' . $unique_id . '_' . $type );
}
}
}
}
}
add_action( 'init', 'mt_is_cart_expired' );
5 changes: 3 additions & 2 deletions src/mt-add-to-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ function mt_registration_form( $content, $event = false, $view = 'calendar', $ti
* @return array|bool 'false' if ticket is not purchaseable.
*/
function mt_ticket_row( $event_id, $registration, $settings, $type, $available, $tickets_remaining ) {
$options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) );
$pricing = $registration['prices'];
$options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) );
$pricing = $registration['prices'];
$cart_data = array();
if ( is_array( mt_in_cart( $event_id ) ) ) {
$cart_data = mt_in_cart( $event_id );
}
Expand Down
21 changes: 20 additions & 1 deletion src/mt-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,24 @@ function mt_gateways() {
}
}

/**
* Display time remaining before cart expiration.
*/
function mt_generate_expiration() {
$expiration = mt_get_expiration();
$output = '';
if ( 0 === $expiration ) {
return;
}
if ( ( $expiration - time() ) > 30 * MINUTE_IN_SECONDS ) {
$output = '<div class="expiration-notice"><p>' . sprintf( __( 'Your shopping cart will be saved for %s.', 'my-tickets' ), human_time_diff( time(), $expiration ) ) . '</p></div>';;
} else {
$output = '<div class="expiration-extension"><p>' . sprintf( __( 'Your shopping cart will expire in %s.', 'my-tickets' ), human_time_diff( time(), $expiration ) ) . '<button type="button">' . __( 'Extend 5 minutes', 'my-tickets' ) . '</button></p></div>';
}

return $output;
}

/**
* Generate breadcrumb path for cart purchase process
*
Expand Down Expand Up @@ -708,6 +726,7 @@ function mt_generate_cart( $user_ID = false ) {
// if submitted successfully & payment required, toggle to payment form.
$options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) );
$gateway = isset( $_POST['mt_gateway'] ) ? sanitize_text_field( $_POST['mt_gateway'] ) : false;
$expiration = mt_generate_expiration();
$breadcrumbs = mt_generate_path( $gateway );
// TODO: If gateway is offline, mt_generate_gateway is never run. Use mt_generate_gateway to create button in both cases.
// Need to handle the case where multiple gateways are available, however; can't display the gateway until after gateway is selected.
Expand Down Expand Up @@ -776,7 +795,7 @@ function mt_generate_cart( $user_ID = false ) {
}
}

return $breadcrumbs . $output;
return $expiration . $breadcrumbs . $output;
}

/**
Expand Down

0 comments on commit a0aa471

Please sign in to comment.