Created
June 19, 2020 14:48
-
-
Save stevegrunwell/f9cc978cd5c23f744142798b7b772ba6 to your computer and use it in GitHub Desktop.
Add a "Forever" option to Limit Orders for WooCommerce - https://wordpress.org/support/topic/all-time-interval/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Limit Orders for WooCommerce - Never-ending Interval | |
* Description: Add a "Forever" option to Limit Orders for WooCommerce. | |
* Author: Nexcess | |
* Author URI: https://nexcess.net | |
*/ | |
/** | |
* Add "Forever" to the list of intervals. | |
* | |
* @param array $intervals Available time intervals. | |
* | |
* @return array The filtered array of intervals. | |
*/ | |
add_filter( 'limit_orders_interval_select', function ( $intervals ) { | |
// Return early if it already exists. | |
if ( isset( $intervals['forever'] ) ) { | |
return $intervals; | |
} | |
$intervals['forever'] = __( 'Forever (never resets)', 'limit-orders' ); | |
return $intervals; | |
} ); | |
/** | |
* Get a DateTime object representing some time in the past. | |
* | |
* @param \DateTime $start The DateTime representing the start of the current interval. | |
* @param string $interval The type of interval being calculated. | |
* | |
* @return \DateTime A DateTime object some time in the past or $start, if the current $interval is | |
* not "forever". | |
*/ | |
add_filter( 'limit_orders_interval_start', function ( $start, $interval ) { | |
if ( 'forever' !== $interval ) { | |
return $start; | |
} | |
// Start at the top of the previous hour. | |
return $start->setTime( (int) $start->format( 'G' ) - 1, 0, 0 ); | |
}, 10, 2 ); | |
/** | |
* Filter the DateTime at which the next interval should begin. | |
* | |
* @param \DateTime $start A DateTime representing the start time for the next interval. | |
* @param \DateTime $current A DateTime representing the beginning of the current interval. | |
* @param string $interval The specified interval. | |
* | |
* @return \DateTime The DateTime at which the next interval should begin, or $start if the | |
* current $interval is not "forever". | |
*/ | |
add_filter( 'limit_orders_next_interval', function ( $start, $current, $interval ) { | |
if ( 'forever' !== $interval ) { | |
return $start; | |
} | |
return $current->add( new \DateInterval( 'P100Y' ) ); | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Forever" is relative; if your store is online for 100 years, well, here's to your health! 🍻