| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module: Checkout. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Checkout module class. |
| 12 |
*/ |
| 13 |
class Orderable_Checkout { |
| 14 |
/** |
| 15 |
* Init. |
| 16 |
*/ |
| 17 |
public static function run() { |
| 18 |
add_filter( 'wpsf_register_settings_orderable', array( __CLASS__, 'register_settings' ) ); |
| 19 |
|
| 20 |
// @phpstan-ignore-next-line |
| 21 |
add_action( 'woocommerce_checkout_process', array( __CLASS__, 'is_order_date_valid' ), 5 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register settings. |
| 26 |
* |
| 27 |
* @param array $settings |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public static function register_settings( $settings = array() ) { |
| 32 |
$settings['tabs'][] = array( |
| 33 |
'id' => 'checkout', |
| 34 |
'title' => __( 'Checkout Settings', 'orderable-pro' ), |
| 35 |
'priority' => 20, |
| 36 |
); |
| 37 |
|
| 38 |
$settings['sections'][] = array( |
| 39 |
'tab_id' => 'checkout', |
| 40 |
'section_id' => 'general', |
| 41 |
'section_title' => __( 'Checkout Settings', 'orderable' ), |
| 42 |
'section_description' => '', |
| 43 |
'section_order' => 0, |
| 44 |
'fields' => array( |
| 45 |
array( |
| 46 |
'id' => 'pro', |
| 47 |
'title' => __( 'Enable Custom Checkout', 'orderable' ), |
| 48 |
'subtitle' => __( "When enabled, your theme's checkout will be replaced by Orderable's optimized checkout experience.", 'orderable' ), |
| 49 |
'type' => 'custom', |
| 50 |
'output' => Orderable_Helpers::get_pro_button( 'checkout' ), |
| 51 |
), |
| 52 |
), |
| 53 |
); |
| 54 |
|
| 55 |
return $settings; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check if the order date is valid. |
| 60 |
* |
| 61 |
* The order date is sent via POST. |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
public static function is_order_date_valid() { |
| 66 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 67 |
$order_date = empty( $_POST['orderable_order_date'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['orderable_order_date'] ) ); |
| 68 |
|
| 69 |
if ( empty( $order_date ) || 'asap' === $order_date ) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
if ( self::is_order_date_past( $order_date ) ) { |
| 74 |
wc_add_notice( __( 'Sorry, the date you selected is no longer available. Please choose another.', 'orderable' ), 'error' ); |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check if the given order date is in the past. |
| 84 |
* |
| 85 |
* @param int $order_date The order date to be checked in timestamp format. |
| 86 |
* @return bool Whether the order date is in the past or not. |
| 87 |
*/ |
| 88 |
protected static function is_order_date_past( $order_date ) { |
| 89 |
$now = new DateTime( 'now', wp_timezone() ); |
| 90 |
$now->setTime( 0, 0, 0 ); |
| 91 |
|
| 92 |
return $order_date < $now->getTimestamp(); |
| 93 |
} |
| 94 |
} |
| 95 |
|