Skip to content

Commit

Permalink
Allow configuring cart expiration time
Browse files Browse the repository at this point in the history
See #13
  • Loading branch information
joedolson committed Nov 23, 2023
1 parent 5afc3b4 commit d1e05f7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/includes/data-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ function mt_save_data( $passed, $type = 'cart', $override = false ) {
}
$current_user = wp_get_current_user();
mt_refresh_cache();
/**
* Filter the length of time transient data (shopping carts) are stored for non-logged in users.
*
* @hook mt_cart_expiration_window
*
* @param {int} $time Number of seconds before cart data will expire. Default WEEK_IN_SECONDS.
*
* @return {int}
*/
$expiration = apply_filters( 'mt_cart_expiration_window', WEEK_IN_SECONDS );
$expiration = mt_expiration_window();
if ( is_user_logged_in() ) {
$data_age = get_user_meta( $current_user->ID, "_mt_user_init_$type", true );
if ( ! $data_age ) {
Expand Down Expand Up @@ -101,16 +92,7 @@ function mt_get_data( $type, $user_ID = false ) {
$current_user = wp_get_current_user();
$data_age = get_user_meta( $current_user->ID, "_mt_user_init_$type", true );
if ( ! $data_age ) {
/**
* Filter the length of time transient data (shopping carts) are stored for non-logged in users.
*
* @hook mt_cart_expiration_window
*
* @param {int} $time Number of seconds before cart data will expire. Default WEEK_IN_SECONDS.
*
* @return {int}
*/
$expiration = apply_filters( 'mt_cart_expiration_window', WEEK_IN_SECONDS );
$expiration = mt_expiration_window();
update_user_meta( $current_user->ID, "_mt_user_init_$type", time() + $expiration );
}
if ( time() > $data_age ) {
Expand Down Expand Up @@ -150,17 +132,8 @@ function mt_get_data( $type, $user_ID = false ) {
*/
function mt_set_user_unique_id() {
if ( ! defined( 'DOING_CRON' ) ) {
$unique_id = mt_get_unique_id();
/**
* Filter the length of time unique ID cookies are stored for non-logged in users.
*
* @hook mt_id_expiration_window
*
* @param {int} $time Number of seconds before cart data will expire. Default WEEK_IN_SECONDS.
*
* @return {int}
*/
$expiration = apply_filters( 'mt_id_expiration_window', WEEK_IN_SECONDS );
$unique_id = mt_get_unique_id();
$expiration = mt_expiration_window();
if ( ! $unique_id ) {
$unique_id = mt_generate_unique_id();
if ( version_compare( PHP_VERSION, '7.3.0', '>' ) ) {
Expand Down Expand Up @@ -207,3 +180,30 @@ function mt_get_unique_id() {

return $unique_id;
}

/**
* Get cart expiration time.
*
* @return int Number of seconds cart will last.
*/
function mt_expiration_window() {
$options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) );
$expiration = $options['mt_expiration'];
// Doesn't support less than 10 minutes.
if ( ! $expiration || (int) $expiration < 600 ) {
$return = WEEK_IN_SECONDS;
}
$return = absint( $expiration );
/**
* Filter the length of time data is stored. (Shopping carts, unique IDs).
*
* @hook mt_expiration_window
*
* @param {int} $time Number of seconds before data will expire. Default WEEK_IN_SECONDS.
*
* @return {int}
*/
$expiration = apply_filters( 'mt_expiration_window', $return );

return $expiration;
}
17 changes: 17 additions & 0 deletions src/mt-payment-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function mt_update_payment_settings( $post ) {
$mt_phone = ( isset( $post['mt_phone'] ) ) ? 'on' : 'off';
$mt_vat = ( isset( $post['mt_vat'] ) ) ? 'on' : 'off';
$mt_redirect = ( isset( $post['mt_redirect'] ) ) ? '1' : '0';
$mt_expiration = ( isset( $post['mt_expiration'] ) ) ? '' : absint( $post['mt_expiration'] );

$mt_default_gateway = ( isset( $post['mt_default_gateway'] ) ) ? $post['mt_default_gateway'] : 'offline';
$mt_gateway = ( isset( $post['mt_gateway'] ) ) ? $post['mt_gateway'] : array( 'offline' );
Expand Down Expand Up @@ -78,6 +79,7 @@ function mt_update_payment_settings( $post ) {
'mt_receipt_page' => $mt_receipt_page,
'mt_tickets_page' => $mt_tickets_page,
'mt_redirect' => $mt_redirect,
'mt_expiration' => $mt_expiration,
),
$post
);
Expand Down Expand Up @@ -181,6 +183,21 @@ function mt_payment_settings() {
<input type="checkbox" name="mt_redirect" id="mt_redirect" value="on" <?php echo checked( $options['mt_redirect'], '1' ); ?> />
<label for="mt_redirect"><?php _e( 'Redirect to cart when tickets added', 'my-tickets' ); ?></label>
</li>
<li>
<label for="mt_expiration"><?php _e( 'Cart Expiration Window', 'my-tickets' ); ?></label>
<select name="mt_expiration" id="mt_expiration" aria-describedby="mt_expiration_info">
<option value=""><?php _e( 'Default (1 week)', 'my-tickets' ); ?></option>
<option value="600"<?php selected( 600, $options['mt_expiration'] ); ?>><?php _e( '10 minutes', 'my-tickets' ); ?></option>
<option value="3600"<?php selected( 3600, $options['mt_expiration'] ); ?>><?php _e( '1 hour', 'my-tickets' ); ?></option>
<option value="<?php echo ( 3 * HOUR_IN_SECONDS ); ?>"<?php selected( ( 3 * HOUR_IN_SECONDS ), $options['mt_expiration'] ); ?>><?php _e( '3 hours', 'my-tickets' ); ?></option>
<option value="<?php echo ( 12 * HOUR_IN_SECONDS ); ?>"<?php selected( ( 12 * HOUR_IN_SECONDS ), $options['mt_expiration'] ); ?>><?php _e( '12 hours', 'my-tickets' ); ?></option>
<option value="<?php echo ( DAY_IN_SECONDS ); ?>"<?php selected( ( DAY_IN_SECONDS ), $options['mt_expiration'] ); ?>><?php _e( '1 day', 'my-tickets' ); ?></option>
<option value="<?php echo ( 3 * DAY_IN_SECONDS ); ?>"<?php selected( ( 3 * DAY_IN_SECONDS ), $options['mt_expiration'] ); ?>><?php _e( '3 days', 'my-tickets' ); ?></option>
<option value="<?php echo ( 14 * DAY_IN_SECONDS ); ?>"<?php selected( ( 14 * DAY_IN_SECONDS ), $options['mt_expiration'] ); ?>><?php _e( '2 weeks', 'my-tickets' ); ?></option>
<option value="<?php echo ( 30 * DAY_IN_SECONDS ); ?>"<?php selected( ( 30 * DAY_IN_SECONDS ), $options['mt_expiration'] ); ?>><?php _e( '1 month', 'my-tickets' ); ?></option>
</select><br />
<span class="aria-description" id="mt_expiration_info"><?php _e( 'How long tickets will remain in a shopping cart.', 'my-tickets' ); ?></span>
</li>
<?php
/**
* Add payment settings fields.
Expand Down
1 change: 1 addition & 0 deletions src/my-tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ function mt_default_settings() {
'mt_ticket_image' => 'ticket',
'symbol_order' => 'symbol-first',
'mt_hide_empty_short_cart' => 'false',
'mt_expiration' => '',
);

return $defaults;
Expand Down

0 comments on commit d1e05f7

Please sign in to comment.