Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/wp-travel-engine/dist/admin/coupon-edit.asset.php
+++ b/wp-travel-engine/dist/admin/coupon-edit.asset.php
@@ -0,0 +1 @@
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'cbb40cb3e53fc6f917b7');
--- a/wp-travel-engine/dist/admin/dashboard-analytics/index.asset.php
+++ b/wp-travel-engine/dist/admin/dashboard-analytics/index.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '94f6e6b111f943e9d7f0');
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '2a0be29ebae5dee8d185');
--- a/wp-travel-engine/dist/admin/exports.asset.php
+++ b/wp-travel-engine/dist/admin/exports.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'c4769f2d3b27ca153610');
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '4dcd36ec68dc5fa9d454');
--- a/wp-travel-engine/dist/admin/global-settings.asset.php
+++ b/wp-travel-engine/dist/admin/global-settings.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '63107d5e430674436b51');
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'd34ac4eb704f182b0395');
--- a/wp-travel-engine/dist/admin/plugins-php.asset.php
+++ b/wp-travel-engine/dist/admin/plugins-php.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array(), 'version' => 'a84c7a3463105e63d518');
+<?php return array('dependencies' => array(), 'version' => 'b32292009889559501bb');
--- a/wp-travel-engine/dist/admin/trip-edit.asset.php
+++ b/wp-travel-engine/dist/admin/trip-edit.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-url'), 'version' => '2d540b9286945d323351');
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-url'), 'version' => '8d2be6779e318da8951d');
--- a/wp-travel-engine/dist/public/components/style-trip-booking-modal.asset.php
+++ b/wp-travel-engine/dist/public/components/style-trip-booking-modal.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array(), 'version' => '81d344d5eecd634e41b7');
+<?php return array('dependencies' => array(), 'version' => 'bbf031b12179877cfb8e');
--- a/wp-travel-engine/dist/public/components/trip-booking-modal.asset.php
+++ b/wp-travel-engine/dist/public/components/trip-booking-modal.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'c9dc808736746c60bc49');
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'bb391bbe9b695eebd22a');
--- a/wp-travel-engine/dist/public/coupon-banner.asset.php
+++ b/wp-travel-engine/dist/public/coupon-banner.asset.php
@@ -0,0 +1 @@
+<?php return array('dependencies' => array(), 'version' => 'a701d87d456f314d2079');
--- a/wp-travel-engine/dist/public/exports.asset.php
+++ b/wp-travel-engine/dist/public/exports.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '45a12699a803cb0e127b');
+<?php return array('dependencies' => array('lodash', 'moment', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '29a6ba999d5359e5da39');
--- a/wp-travel-engine/dist/public/my-account.asset.php
+++ b/wp-travel-engine/dist/public/my-account.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array(), 'version' => 'e06a485dfe90218a1795');
+<?php return array('dependencies' => array(), 'version' => 'cf8f54981dc96d4507ad');
--- a/wp-travel-engine/dist/public/trip-checkout.asset.php
+++ b/wp-travel-engine/dist/public/trip-checkout.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('lodash'), 'version' => '70d7c5dab145654852bc');
+<?php return array('dependencies' => array('lodash'), 'version' => '5e5ce07b6fa43207cee7');
--- a/wp-travel-engine/dist/public/wte-public.asset.php
+++ b/wp-travel-engine/dist/public/wte-public.asset.php
@@ -1 +1 @@
-<?php return array('dependencies' => array('wp-i18n'), 'version' => '5804a72255f68700dc08');
+<?php return array('dependencies' => array('wp-i18n'), 'version' => 'c7ee60b9ce6951406995');
--- a/wp-travel-engine/includes/class-wp-travel-engine-archive-hooks.php
+++ b/wp-travel-engine/includes/class-wp-travel-engine-archive-hooks.php
@@ -162,7 +162,7 @@
*/
public function archive_pre_get_posts( $query ) {
- $post_not_in = $query->get( 'post__not_in', array() );
+ $post_not_in = (array) $query->get( 'post__not_in', array() );
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_post_type_archive( WP_TRAVEL_ENGINE_POST_TYPE ) || $query->is_tax ) {
@@ -199,7 +199,7 @@
$post_not_in = array_merge( $post_not_in, self::$featured_trip_ids );
}
- $custom_trips = get_option( 'wptravelengine_custom_trips', array() );
+ $custom_trips = (array) get_option( 'wptravelengine_custom_trips', array() );
$query->set( 'post__not_in', array_unique( array_merge( $post_not_in, $custom_trips ) ) );
}
--- a/wp-travel-engine/includes/class-wte-ajax.php
+++ b/wp-travel-engine/includes/class-wte-ajax.php
@@ -102,6 +102,13 @@
*/
$ajax_registry->register( AjaxSendTestEmail::class );
+ /**
+ * Set Difficulty Term AJAX action
+ *
+ * @since 6.8.2
+ */
+ $ajax_registry->register( AjaxSetDifficultyTerm::class );
+
add_action(
'wp_ajax_nopriv_email_test',
function () {
@@ -291,10 +298,10 @@
'callback' => array( 'Wp_Travel_Engine_Public', 'wte_payment_gateway' ),
'nonce_action' => 'wp_rest',
),
- 'wte_set_difficulty_term_level' => array(
- 'callback' => array( __CLASS__, 'set_difficulty_term_level' ),
- 'nonce_action' => 'wp_xhr',
- ),
+ // 'wte_set_difficulty_term_level' => array(
+ // 'callback' => array( __CLASS__, 'set_difficulty_term_level' ),
+ // 'nonce_action' => 'wp_xhr',
+ // ),
// 'wte_user_wishlist' => array(
// 'callback' => array( __CLASS__, 'wte_user_wishlist' ),
// 'nonce_action' => 'wp_xhr',
@@ -403,10 +410,18 @@
/**
- *
* @since 5.5.7
+ * @deprecated 6.8.2 Use {@see WPTravelEngineCoreControllersAjaxSetDifficultyTerm} instead.
*/
public static function set_difficulty_term_level( $post_data ) {
+ _deprecated_function( __METHOD__, '6.8.2', 'WPTravelEngineCoreControllersAjaxSetDifficultyTerm' );
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error(
+ new WP_Error( 'insufficient_permissions', __( 'You do not have permission to perform this action.', 'wp-travel-engine' ) ),
+ 403
+ );
+ return;
+ }
// save in options.
$difficulty_level = get_option( 'difficulty_level_by_terms', array() );
$term = get_term( $post_data['term_id'] );
--- a/wp-travel-engine/includes/class-wte-session.php
+++ b/wp-travel-engine/includes/class-wte-session.php
@@ -37,16 +37,31 @@
* Setup the WP_Session instance
*
* @access public
- * @return array
+ * @return array|void
* @since 1.5
+ * @since 6.8.2 Skip eager creation for visitors without an existing session cookie (#2507).
*/
public function init() {
+ if ( ! isset( $_COOKIE[ WP_TRAVEL_ENGINE_SESSION_COOKIE ] ) ) {
+ return;
+ }
$this->session = WP_Session::get_instance();
return $this->session;
}
/**
+ * Ensure a session exists, creating one if needed. Called before any write operation.
+ *
+ * @since 6.8.2
+ */
+ private function ensure_session() {
+ if ( empty( $this->session ) ) {
+ $this->session = WP_Session::get_instance();
+ }
+ }
+
+ /**
* Get session data.
*
* @param string $key session data key.
@@ -54,6 +69,9 @@
* @return mixed session data.
*/
public function get( string $key ) {
+ if ( empty( $this->session ) ) {
+ return false;
+ }
$key = sanitize_key( $key );
$value = $this->session[ $key ] ?? null;
@@ -75,6 +93,7 @@
* @since 6.3.3
*/
public function set_json( $key, $value ) {
+ $this->ensure_session();
$key = sanitize_key( $key );
if ( is_array( $value ) ) {
$this->session[ $key ] = wp_json_encode( $value );
@@ -92,6 +111,7 @@
* @return mixed
*/
public function set( $key, $value ) {
+ $this->ensure_session();
$key = sanitize_key( $key );
if ( is_array( $value ) ) {
$this->session[ $key ] = serialize( $value );
@@ -110,9 +130,33 @@
* @return boolean
*/
public function delete( $key ) {
+ if ( empty( $this->session ) ) {
+ return true;
+ }
$key = sanitize_key( $key );
unset( $this->session[ $key ] );
return ! isset( $this->session[ $key ] );
}
+
+ /**
+ * Destroy the session: clear DB records and expire the browser cookie.
+ * Called after booking confirmation so subsequent page visits are not blocked from cache.
+ *
+ * @since 6.8.2
+ */
+ public function destroy() {
+ if ( ! empty( $this->session ) ) {
+ $session_id = $this->session->session_id;
+ $this->session->reset();
+ delete_option( "_wp_session_{$session_id}" );
+ delete_option( "_wp_session_expires_{$session_id}" );
+ $this->session = null;
+ }
+
+ if ( isset( $_COOKIE[ WP_TRAVEL_ENGINE_SESSION_COOKIE ] ) ) {
+ setcookie( WP_TRAVEL_ENGINE_SESSION_COOKIE, '', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
+ unset( $_COOKIE[ WP_TRAVEL_ENGINE_SESSION_COOKIE ] );
+ }
+ }
}
--- a/wp-travel-engine/includes/classes/Assets.php
+++ b/wp-travel-engine/includes/classes/Assets.php
@@ -155,6 +155,7 @@
$this->register_script( Asset::register( 'wptravelengine-settings', 'admin/global-settings.js' ) );
$this->register_script( Asset::register( 'wptravelengine-trip-edit', 'admin/trip-edit.js' ) );
$this->register_script( Asset::register( 'wptravelengine-booking-edit', 'admin/booking-edit.js' ) );
+ $this->register_script( Asset::register( 'wptravelengine-coupon-edit', 'admin/coupon-edit.js' )->dependencies( [ 'wte-fpickr' ] ) )->localize( 'wptravelengine-coupon-edit', 'wteL10n' );
$this->register_script( Asset::register( 'wptravelengine-booking-legacy-edit', 'admin/booking-legacy-edit.js' ) );
$this->register_script( Asset::register( 'wptravelengine-customer-edit', 'admin/customer-edit.js' ) );
$this->register_script( Asset::register( 'wptravelengine-upcoming-tours', 'admin/upcoming-tours.js' ) );
@@ -1233,6 +1234,7 @@
wp_set_script_translations( 'wp-travel-engine', 'wp-travel-engine', $languages_path );
wp_set_script_translations( 'wptravelengine-trip-edit', 'wp-travel-engine', $languages_path );
wp_set_script_translations( 'wptravelengine-booking-edit', 'wp-travel-engine', $languages_path );
+ wp_set_script_translations( 'wptravelengine-coupon-edit', 'wp-travel-engine', $languages_path );
wp_set_script_translations( 'wptravelengine-customer-edit', 'wp-travel-engine', $languages_path );
wp_set_script_translations( 'wptravelengine-upcoming-tours', 'wp-travel-engine', $languages_path );
}
--- a/wp-travel-engine/includes/classes/Builders/FormFields/DefaultFormFields.php
+++ b/wp-travel-engine/includes/classes/Builders/FormFields/DefaultFormFields.php
@@ -259,8 +259,9 @@
foreach ( $payment_gateways as $key => $payment_gateway ) {
$payment_gateways_options[ $key ] = $payment_gateway['label'];
}
- $payment_gateways_options = array( '' => __( 'Choose a Payment Gateway', 'wp-travel-engine' ) ) + $payment_gateways_options;
- $payment_gateways_options = apply_filters( 'wptravelengine_payments_form_fields_options', $payment_gateways_options );
+ $payment_gateways_options['other'] = __( 'Other', 'wp-travel-engine' );
+ $payment_gateways_options = array( '' => __( 'Choose a Payment Gateway', 'wp-travel-engine' ) ) + $payment_gateways_options;
+ $payment_gateways_options = apply_filters( 'wptravelengine_payments_form_fields_options', $payment_gateways_options );
$fields = apply_filters(
'wptravelengine_payments_form_fields',
--- a/wp-travel-engine/includes/classes/Core/Booking/SaveBooking.php
+++ b/wp-travel-engine/includes/classes/Core/Booking/SaveBooking.php
@@ -761,6 +761,7 @@
$cart_info['totals']['deposit'] = '0.00';
$cart_info['totals']['total_extra_charges'] = '0.00';
+ $total_paid_amt = '0.00';
$success_status = wptravelengine_success_payment_status();
foreach ( $items as $payment_data ) {
@@ -847,6 +848,8 @@
)
);
+ $total_paid_amt = $calc->add( $total_paid_amt, (string) $paid_amount );
+
if ( is_numeric( $due_amount = $this->request->get_param( 'due_amount' ) ) ) {
$payment_model->set_meta(
'payable',
@@ -901,7 +904,7 @@
*
* @since 6.7.6
*/
- $this->booking->set_meta( 'total_paid_amount', (float) $paid_amount );
+ $this->booking->set_meta( 'total_paid_amount', (float) $total_paid_amt );
if ( is_numeric( $paid_amount = $this->request->get_param( 'paid_amount' ) ) ) {
$this->booking->set_meta( 'paid_amount', (float) $paid_amount );
--- a/wp-travel-engine/includes/classes/Core/Cart/Item.php
+++ b/wp-travel-engine/includes/classes/Core/Cart/Item.php
@@ -791,6 +791,7 @@
new PricingCategory(
$this->cart,
array(
+ 'id' => $package_traveler->id,
'label' => $package_traveler->label,
'quantity' => $pax,
'price' => apply_filters(
--- a/wp-travel-engine/includes/classes/Core/Controllers/Ajax/ApplyCoupon.php
+++ b/wp-travel-engine/includes/classes/Core/Controllers/Ajax/ApplyCoupon.php
@@ -23,11 +23,13 @@
/**
* Process Request.
* Coupon code is applied.
+ *
+ * @since 6.8.2 Source trip IDs from server-side cart only; client-supplied trip_ids removed to prevent coupon restriction bypass.
*/
public function process_request() {
global $wte_cart;
$apply_coupon_code = $this->request->get_param( 'CouponCode' );
- $apply_trip_ids = $this->request->get_param( 'trip_ids' ) ?? wp_json_encode( $wte_cart->get_cart_trip_ids() );
+ $apply_trip_ids = wp_json_encode( $wte_cart->get_cart_trip_ids() );
if ( empty( $apply_coupon_code ) ) { // phpcs:ignore
wp_send_json_error(
new WP_Error( 'WTE_INVALID_REQUEST', __( 'Coupon Code is required.', 'wp-travel-engine' ) )
@@ -70,6 +72,30 @@
die;
}
+ // Trip departure date window validation.
+ if ( $coupon_instance->has_trip_date_restriction() ) {
+ $trip_date = '';
+ foreach ( $wte_cart->getItems( true ) as $cart_item ) {
+ if ( (int) $cart_item->trip_id === (int) $trip_id ) {
+ $trip_date = $cart_item->trip_date ?? '';
+ break;
+ }
+ }
+ if ( ! $coupon_instance->is_valid_for_trip_date( $trip_date ) ) {
+ wp_send_json_error(
+ new WP_Error(
+ 'WTE_COUPON_TRIP_DATE_INVALID',
+ sprintf(
+ /* translators: 1: coupon code. */
+ __( 'Coupon "%1$s" is not valid for the selected trip departure date.', 'wp-travel-engine' ),
+ sanitize_text_field( wp_unslash( $apply_coupon_code ) )
+ )
+ )
+ );
+ die;
+ }
+ }
+
$coupon_limit_number = $coupon_instance->get_coupon_limit_number();
if ( (bool) $coupon_limit_number && ( + $coupon_limit_number <= $coupon_instance->get_coupon_usage_count() ) ) {
--- a/wp-travel-engine/includes/classes/Core/Controllers/Ajax/SetDifficultyTerm.php
+++ b/wp-travel-engine/includes/classes/Core/Controllers/Ajax/SetDifficultyTerm.php
@@ -27,15 +27,24 @@
*/
public function process_request() {
$post_data = $this->request->get_params();
+ $term_id = absint( $post_data['term_id'] ?? 0 );
+ $level = sanitize_text_field( $post_data['level'] ?? '' );
+
+ $term = get_term( $term_id );
+ if ( ! $term || is_wp_error( $term ) ) {
+ wp_send_json_error( new WP_Error( 'invalid_term', __( 'Invalid term.', 'wp-travel-engine' ) ), 400 );
+ return;
+ }
+
// save in options.
- $difficulty_level = get_option( 'difficulty_level_by_terms', array() );
- $difficulty_level[ $post_data['level'] ] = array(
- 'level' => $post_data['level'],
- 'term_id' => $post_data['term_id'],
- 'label' => get_term( $post_data['term_id'] )->name,
+ $difficulty_level = get_option( 'difficulty_level_by_terms', array() );
+ $difficulty_level[ $level ] = array(
+ 'level' => $level,
+ 'term_id' => $term_id,
+ 'label' => $term->name,
);
foreach ( $difficulty_level as $key => $val ) {
- if ( ( $post_data['level'] != $key && $post_data['term_id'] === $val['term_id'] ) || ( '' === $post_data['level'] && $post_data['term_id'] === $val['term_id'] ) ) {
+ if ( ( $level != $key && $term_id === $val['term_id'] ) || ( '' === $level && $term_id === $val['term_id'] ) ) {
unset( $difficulty_level[ $key ] );
}
}
@@ -44,7 +53,22 @@
}
update_option( 'difficulty_level_by_terms', $difficulty_level );
wp_send_json_success( $difficulty_level );
+ }
+
+ /**
+ * @inheritDoc
+ * @since 6.8.2
+ */
+ protected function authorize_request() {
+ parent::authorize_request();
+
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_send_json_error(
+ new WP_Error( 'insufficient_permissions', __( 'You do not have permission to perform this action.', 'wp-travel-engine' ) ),
+ 403
+ );
+ }
- wp_die();
+ return true;
}
}
--- a/wp-travel-engine/includes/classes/Core/Controllers/RestAPI/V2/Trip.php
+++ b/wp-travel-engine/includes/classes/Core/Controllers/RestAPI/V2/Trip.php
@@ -234,11 +234,20 @@
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error True if the request has access to delete the package, WP_Error object otherwise.
* @since 6.5.2
+ * @since 6.8.2 Validate package_id ownership and type to prevent arbitrary post deletion.
*/
public function delete_package_permissions_check( $request ) {
- $post = get_post( $request['id'] );
+ $trip = get_post( $request['id'] );
- if ( ! current_user_can( 'edit_post', $post->ID ) ) {
+ if ( ! $trip || 'trip' !== $trip->post_type ) {
+ return new WP_Error(
+ 'rest_post_not_found',
+ __( 'Trip not found.', 'wp-travel-engine' ),
+ array( 'status' => 404 )
+ );
+ }
+
+ if ( ! current_user_can( 'edit_post', $trip->ID ) ) {
return new WP_Error(
'rest_cannot_delete',
__( 'Sorry, you are not allowed to delete packages for this trip.', 'wp-travel-engine' ),
@@ -246,11 +255,11 @@
);
}
- if ( ! $post ) {
+ if ( ! PostTrip::package_exists( (int) $trip->ID, (int) $request['package_id'] ) || ! current_user_can( 'delete_post', (int) $request['package_id'] ) ) {
return new WP_Error(
- 'rest_post_not_found',
- __( 'Trip not found.', 'wp-travel-engine' ),
- array( 'status' => 404 )
+ 'rest_cannot_delete',
+ __( 'Invalid package for this trip.', 'wp-travel-engine' ),
+ array( 'status' => 403 )
);
}
@@ -263,11 +272,21 @@
* @param WP_REST_Request $request
*
* @return mixed
+ * @since 6.8.2 Re-assert package ownership before deletion (defense in depth).
*/
public function delete_package( WP_REST_Request $request ) {
try {
+ $trip = wptravelengine_get_trip( $request->get_param( 'id' ) );
+
+ if ( ! $trip || ! PostTrip::package_exists( (int) $request->get_param( 'id' ), (int) $request['package_id'] ) ) {
+ return new WP_Error(
+ 'rest_cannot_delete',
+ __( 'Invalid package for this trip.', 'wp-travel-engine' ),
+ array( 'status' => 403 )
+ );
+ }
+
if ( wp_delete_post( $request['package_id'], true ) ) {
- $trip = new PostTrip( $request->get_param( 'id' ) );
if ( (int) $trip->get_meta( 'primary_package' ) === (int) $request['package_id'] ) {
$trip->delete_meta( 'primary_package' );
}
--- a/wp-travel-engine/includes/classes/Core/Coupon.php
+++ b/wp-travel-engine/includes/classes/Core/Coupon.php
@@ -23,17 +23,17 @@
/**
* @var string
*/
- protected string $name;
+ public string $name;
/**
* @var int
*/
- protected int $id;
+ public int $id;
/**
* @var string
*/
- protected $code;
+ public $code;
/**
* @var array
@@ -44,13 +44,13 @@
* @var string
* @since 6.0.0
*/
- protected $type = '';
+ public $type = '';
/**
* @var float
* @since 6.0.0
*/
- protected float $value = 0;
+ public float $value = 0;
public function __construct( int $coupon_id ) {
$coupon = get_post( $coupon_id );
@@ -76,6 +76,8 @@
* @param string $discount_code The code of the coupon to retrieve.
*
* @return WP_Error|self Returns a coupon object if found, or a WP_Error object if not found.
+ *
+ * @since 6.8.2 Match is now case-sensitive; collation matches are filtered with a strict comparison.
*/
public static function by_code( string $discount_code ) {
$args = array(
@@ -87,14 +89,20 @@
),
),
'fields' => 'ids',
- 'posts_per_page' => 1,
+ 'posts_per_page' => -1,
);
+ // The meta_query comparison relies on the column collation, which is
+ // case-insensitive by default. Fetch all collation matches and enforce a
+ // strict, case-sensitive comparison below.
$post_ids = get_posts( $args );
- if ( isset( $post_ids[0] ) ) {
+ foreach ( $post_ids as $post_id ) {
+ if ( 0 !== strcmp( (string) get_post_meta( (int) $post_id, 'wp_travel_engine_coupon_code', true ), $discount_code ) ) {
+ continue;
+ }
try {
- return new self( (int) $post_ids[0] );
+ return new self( (int) $post_id );
} catch ( InvalidArgumentException $e ) {
return new WP_Error( 'coupon_not_found', __( 'Coupon not found', 'wp-travel-engine' ) );
}
@@ -107,26 +115,6 @@
return get_post_status( $this->id ) === 'publish';
}
- public function id() {
- return $this->id;
- }
-
- public function code() {
- return $this->code;
- }
-
- public function name(): string {
- return $this->name;
- }
-
- public function type() {
- return $this->type;
- }
-
- public function value() {
- return $this->value;
- }
-
public function start_date() {
return $this->settings['general']['coupon_start_date'] ?? '';
}
@@ -143,6 +131,41 @@
return $this->settings['restriction']['restricted_trips'] ?? array();
}
+ /**
+ * @since 6.8.2
+ */
+ public function has_trip_date_restriction(): bool {
+ return wptravelengine_toggled( $this->settings['general']['trip_date_restriction_enabled'] ?? 'no' );
+ }
+
+ /**
+ * @since 6.8.2
+ */
+ public function trip_starts_after() {
+ return $this->settings['general']['trip_starts_after'] ?? '';
+ }
+
+ /**
+ * @since 6.8.2
+ */
+ public function trip_starts_before() {
+ return $this->settings['general']['trip_starts_before'] ?? '';
+ }
+
+ /**
+ * Validate coupon against a trip's departure date.
+ *
+ * @since 6.8.2
+ */
+ public function is_valid_for_trip_date( $trip_date ): bool {
+ return wptravelengine_coupon_is_restriction_date(
+ $this->has_trip_date_restriction(),
+ $this->trip_starts_after(),
+ $this->trip_starts_before(),
+ $trip_date
+ );
+ }
+
public function is_expired(): bool {
$expiry_date = $this->expiry_date();
if ( empty( $expiry_date ) ) {
@@ -178,10 +201,7 @@
}
public function calculated_value( $total ) {
- $coupon_type = $this->type();
- $coupon_value = $this->value();
-
- return static::calculate_value( $total, $coupon_type, $coupon_value );
+ return static::calculate_value( $total, $this->type, $this->value );
}
/**
--- a/wp-travel-engine/includes/classes/Core/Coupons.php
+++ b/wp-travel-engine/includes/classes/Core/Coupons.php
@@ -15,15 +15,18 @@
class Coupons {
/**
- * Checks available coupons in specific trip.
+ * Whether at least one active coupon exists site-wide for the checkout form to render.
+ *
+ * Trip-id restriction, trip-date window, minimum spend etc. are enforced when the
+ * coupon is actually applied (see ApplyCoupon controller). Hiding the form based on
+ * restricted_trips here prevented users on non-restricted-trip pages from seeing the
+ * form even when they had a valid code for another trip; we now only gate on
+ * coupon-level validity (status, usage cap, start/expiry window).
*
- * @return boolean
* @since 6.7.1 Refined the query to improve performance.
+ * @since 6.8.2 Stopped gating on restricted_trips so the form always appears when any active coupon exists.
*/
public static function is_coupon_available() {
- global $wte_cart;
- $trip_id = $wte_cart->get_cart_trip_ids()[0] ?? '';
-
$args = array(
'post_type' => 'wte-coupon',
'post_status' => 'publish',
@@ -38,50 +41,36 @@
$coupons = get_posts( $args );
- $valid_coupon_exists = false;
- $today = wp_date( 'Y-m-d' );
+ $today = wp_date( 'Y-m-d' );
foreach ( $coupons as $coupon ) {
-
$meta = get_post_meta( $coupon->ID, 'wp_travel_engine_coupon_metas', true );
-
if ( ! is_array( $meta ) ) {
continue;
}
- // Usage count
+ // Usage count vs limit.
$usage_count = (int) get_post_meta( $coupon->ID, 'wp_travel_engine_coupon_usage_count', true );
-
- // Coupon limit
- $limit = isset( $meta['restriction']['coupon_limit_number'] ) ? (int) $meta['restriction']['coupon_limit_number'] : '';
-
- if ( $limit !== '' && $limit !== 0 && $usage_count >= $limit ) {
+ $limit = isset( $meta['restriction']['coupon_limit_number'] ) ? (int) $meta['restriction']['coupon_limit_number'] : 0;
+ if ( $limit > 0 && $usage_count >= $limit ) {
continue;
}
- // Expiry date
+ // Expiry date.
$expiry = $meta['general']['coupon_expiry_date'] ?? '';
if ( $expiry && $expiry < $today ) {
continue;
}
- // Start date
+ // Start date.
$start = $meta['general']['coupon_start_date'] ?? '';
if ( $start && $start > $today ) {
continue;
}
- // Trip restriction
- $restricted_trips = $meta['restriction']['restricted_trips'] ?? array();
-
- if ( ! empty( $restricted_trips ) && ! in_array( (string) $trip_id, array_map( 'strval', (array) $restricted_trips ), true ) ) {
- continue;
- }
-
- $valid_coupon_exists = true;
- break;
+ return true;
}
- return $valid_coupon_exists;
+ return false;
}
}
--- a/wp-travel-engine/includes/classes/Core/Models/Post/Coupons.php
+++ b/wp-travel-engine/includes/classes/Core/Models/Post/Coupons.php
@@ -94,6 +94,52 @@
}
/**
+ * Whether the coupon enforces a trip-departure-date window.
+ *
+ * @since 6.8.2
+ */
+ public function has_trip_date_restriction(): bool {
+ $general = $this->get_coupon_general_values();
+ return wptravelengine_toggled( $general['trip_date_restriction_enabled'] ?? 'no' );
+ }
+
+ /**
+ * Earliest allowed trip departure date (inclusive).
+ *
+ * @since 6.8.2
+ */
+ public function get_trip_starts_after(): string {
+ $general = $this->get_coupon_general_values();
+ return $general['trip_starts_after'] ?? '';
+ }
+
+ /**
+ * Latest allowed trip departure date (inclusive).
+ *
+ * @since 6.8.2
+ */
+ public function get_trip_starts_before(): string {
+ $general = $this->get_coupon_general_values();
+ return $general['trip_starts_before'] ?? '';
+ }
+
+ /**
+ * Validate coupon against a trip departure date.
+ *
+ * Returns true when no restriction is set or when the given trip date falls within the configured window.
+ *
+ * @since 6.8.2
+ */
+ public function is_valid_for_trip_date( $trip_date ): bool {
+ return wptravelengine_coupon_is_restriction_date(
+ $this->has_trip_date_restriction(),
+ $this->get_trip_starts_after(),
+ $this->get_trip_starts_before(),
+ $trip_date
+ );
+ }
+
+ /**
* Retrieves restriction values of the coupon.
*
* @return array Coupon restriction values
--- a/wp-travel-engine/includes/classes/Core/Shortcodes/ThankYou.php
+++ b/wp-travel-engine/includes/classes/Core/Shortcodes/ThankYou.php
@@ -34,6 +34,7 @@
*/
public function __construct() {
add_filter( 'body_class', array( $this, 'body_class' ) );
+ add_action( 'wp', array( $this, 'maybe_destroy_session' ), 1 );
}
/**
@@ -48,6 +49,27 @@
}
/**
+ * Clear the booking session on thank-you page load, preserving wishlist data.
+ * Expires the session cookie so subsequent pages can be served from cache.
+ *
+ * @since 6.8.2
+ */
+ public function maybe_destroy_session() {
+ if ( ! isset( $_GET['payment_key'] ) ) {
+ return;
+ }
+ global $post;
+ if ( ! $post || ! has_shortcode( $post->post_content, self::TAG ) ) {
+ return;
+ }
+ $wishlists = WTE()->session->get( 'user_wishlists' );
+ WTE()->session->destroy();
+ if ( ! empty( $wishlists ) ) {
+ WTE()->session->set( 'user_wishlists', $wishlists );
+ }
+ }
+
+ /**
* Add thank you body class.
*/
public function body_class( $classes ) {
--- a/wp-travel-engine/includes/classes/Legacy/Cart.php
+++ b/wp-travel-engine/includes/classes/Legacy/Cart.php
@@ -749,12 +749,11 @@
'name' => 'coupon',
'label' => sprintf(
__( 'Coupon: %1$s (%2$s)', 'wp-travel-engine' ),
- $coupon->code(),
- $coupon->type() === 'percentage' ? $coupon->value() . '%' : '',
- // wptravelengine_the_price( $coupon->value(), false, false ),
+ $coupon->code,
+ 'percentage' === $coupon->type ? ( $coupon->value . '%' ) : wptravelengine_the_price( $coupon->value, false, false ),
),
- 'adjustment_type' => $coupon->type(),
- 'percentage' => $coupon->value(),
+ 'adjustment_type' => $coupon->type,
+ 'percentage' => $coupon->value,
)
)
);
--- a/wp-travel-engine/includes/classes/Modules/CouponCode.php
+++ b/wp-travel-engine/includes/classes/Modules/CouponCode.php
@@ -7,12 +7,14 @@
namespace WPTravelEngineModules;
use WPTravelEngineModulesCouponCodeAjax;
+use WPTravelEngineModulesCouponCodeBanner;
class CouponCode {
public function __construct() {
defined( 'WP_TRAVEL_ENGINE_COUPONS_POST_TYPE' ) || define( 'WP_TRAVEL_ENGINE_COUPONS_POST_TYPE', 'wte-coupon' );
new Ajax();
+ new Banner();
$this->init_hooks();
}
@@ -62,7 +64,7 @@
$messages['wte-coupon'] = array(
0 => '', // Unused. Messages start at index 1.
- 1 => sprintf( __( '%1$s updated. <a href="%2$s">View %3$s</a>', 'wp-travel-engine' ), $post_object->labels->singular_name, esc_url( get_permalink( $post_ID ) ), $post_object->labels->singular_name ),
+ 1 => sprintf( __( '%s updated.', 'wp-travel-engine' ), $post_object->labels->singular_name ),
2 => __( 'Custom field updated.', 'wp-travel-engine' ),
3 => __( 'Custom field deleted.', 'wp-travel-engine' ),
4 => sprintf( __( '%s updated.', 'wp-travel-engine' ), $post_object->labels->singular_name ),
@@ -83,13 +85,6 @@
if ( WP_TRAVEL_ENGINE_COUPONS_POST_TYPE !== $post_type ) {
return;
}
- wp_enqueue_style( 'jquery-ui' );
- add_action(
- 'admin_head',
- function () {
- echo '<style>span.wp-travel-engine-info-msg{background:#1eb823;padding:10px;color:#fff;font-weight:800}span.wp-travel-engine-error-msg{background:#e63333;padding:10px;color:#fff;font-weight:800}.select2-container-multi .select2-choices{min-height:26px;max-width:440px}</style>';
- }
- );
add_meta_box( WP_TRAVEL_ENGINE_COUPONS_POST_TYPE . '-details', __( 'Coupon Options', 'wp-travel-engine' ), array( __CLASS__, 'add_coupon_metabox_field_callback' ), WP_TRAVEL_ENGINE_COUPONS_POST_TYPE, 'normal', 'high' );
},
10,
@@ -127,6 +122,9 @@
return ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) && current_user_can( 'edit_post', $post_id ) && ! wp_is_post_revision( $post_id );
}
+ /**
+ * @since 6.8.2 Added max discount, behavior, trip date restriction, min travelers, min spend fields.
+ */
public static function get_coupon_edit_form_schema() {
$schema = array(
'wp_travel_engine_coupon_code' => array(
@@ -154,7 +152,34 @@
'type' => 'text',
'sanitize_callback' => 'wp_filter_nohtml_kses',
),
-
+ 'trip_date_restriction_enabled' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'wp_filter_nohtml_kses',
+ ),
+ 'trip_starts_after' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'wp_filter_nohtml_kses',
+ ),
+ 'trip_starts_before' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'wp_filter_nohtml_kses',
+ ),
+ 'show_banner' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'wp_filter_nohtml_kses',
+ ),
+ 'banner_message' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'wp_filter_nohtml_kses',
+ ),
+ 'banner_bg_color' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'sanitize_hex_color',
+ ),
+ 'banner_text_color' => array(
+ 'type' => 'text',
+ 'sanitize_callback' => 'sanitize_hex_color',
+ ),
),
),
'restriction' => array(
@@ -172,40 +197,78 @@
return $schema;
}
+ /**
+ * Save coupon metabox.
+ *
+ * @since 6.8.2 Save new coupon fields: max discount cap, behavior, trip date restriction window, min travelers, min spend.
+ */
public static function save_wte_coupon_metabox( $coupon_id ) {
+ if ( ! self::can_update_coupon( $coupon_id ) ) {
+ return;
+ }
+
$coupon = get_post( $coupon_id );
- if ( ! is_null( $coupon ) && WP_TRAVEL_ENGINE_COUPONS_POST_TYPE !== $coupon->post_type ) {
- return; // No works here.
+ if ( is_null( $coupon ) || WP_TRAVEL_ENGINE_COUPONS_POST_TYPE !== $coupon->post_type ) {
+ return;
+ }
+
+ if ( ! isset( $_POST['wte_coupon_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wte_coupon_nonce'] ) ), 'wte_save_coupon' ) ) {
+ return;
}
- // phpcs:disable
+
empty( $_POST['wp_travel_engine_coupon_code'] ) || update_post_meta( $coupon->ID, 'wp_travel_engine_coupon_code', wp_filter_nohtml_kses( wp_unslash( $_POST['wp_travel_engine_coupon_code'] ) ) );
- if ( isset( $_POST['wp_travel_engine_coupon'] ) ) {
- $_data = array();
- $coupon_data = wte_clean(wp_unslash( $_POST['wp_travel_engine_coupon'] ));
- if ( isset( $coupon_data['general'] ) ) {
- $general = $coupon_data['general'];
- array_walk( $general, function( &$item ) {
- $item = wp_filter_nohtml_kses( $item );
- } );
- $_data['general'] = $general;
- }
- if ( isset( $coupon_data['restriction'] ) ) {
- $restriction = $coupon_data['restriction'];
- array_walk( $restriction, function( &$item ) {
- if ( is_array( $item ) ) {
- $item = array_map( 'wp_filter_nohtml_kses', $item );
- } else {
- $item = wp_filter_nohtml_kses( $item );
- }
- } );
- $_data['restriction'] = $restriction;
- }
- update_post_meta( $coupon->ID, 'wp_travel_engine_coupon_metas', $_data );
+ if ( ! isset( $_POST['wp_travel_engine_coupon'] ) ) {
+ return;
+ }
+
+ $coupon_data = wte_clean( wp_unslash( $_POST['wp_travel_engine_coupon'] ) );
+ $_data = array();
+
+ if ( isset( $coupon_data['general'] ) && is_array( $coupon_data['general'] ) ) {
+ $general = $coupon_data['general'];
+
+ $allowed_types = array( 'fixed', 'percentage' );
+
+ $_data['general'] = array(
+ 'coupon_type' => in_array( $general['coupon_type'] ?? '', $allowed_types, true ) ? $general['coupon_type'] : 'fixed',
+ 'coupon_value' => isset( $general['coupon_value'] ) ? wp_filter_nohtml_kses( $general['coupon_value'] ) : '',
+ 'coupon_start_date' => isset( $general['coupon_start_date'] ) ? wp_filter_nohtml_kses( $general['coupon_start_date'] ) : '',
+ 'coupon_expiry_date' => isset( $general['coupon_expiry_date'] ) ? wp_filter_nohtml_kses( $general['coupon_expiry_date'] ) : '',
+ 'trip_date_restriction_enabled' => ( isset( $general['trip_date_restriction_enabled'] ) && in_array( $general['trip_date_restriction_enabled'], array( 'yes', 'on', '1', 1, true ), true ) ) ? 'yes' : 'no',
+ 'trip_starts_after' => isset( $general['trip_starts_after'] ) ? wp_filter_nohtml_kses( $general['trip_starts_after'] ) : '',
+ 'trip_starts_before' => isset( $general['trip_starts_before'] ) ? wp_filter_nohtml_kses( $general['trip_starts_before'] ) : '',
+ 'show_banner' => ( isset( $general['show_banner'] ) && in_array( $general['show_banner'], array( 'yes', 'on', '1', 1, true ), true ) ) ? 'yes' : 'no',
+ 'banner_message' => isset( $general['banner_message'] ) ? wp_filter_nohtml_kses( $general['banner_message'] ) : '',
+ 'banner_bg_color' => isset( $general['banner_bg_color'] ) ? ( sanitize_hex_color( $general['banner_bg_color'] ) ?? '' ) : '',
+ 'banner_text_color' => isset( $general['banner_text_color'] ) ? ( sanitize_hex_color( $general['banner_text_color'] ) ?? '' ) : '',
+ );
+ }
+
+ if ( isset( $coupon_data['restriction'] ) && is_array( $coupon_data['restriction'] ) ) {
+ $restriction = $coupon_data['restriction'];
+
+ $_data['restriction'] = array(
+ 'restricted_trips' => isset( $restriction['restricted_trips'] ) ? array_map( 'absint', (array) $restriction['restricted_trips'] ) : array(),
+ 'coupon_limit_number' => isset( $restriction['coupon_limit_number'] ) && '' !== $restriction['coupon_limit_number'] ? absint( $restriction['coupon_limit_number'] ) : '',
+ );
}
- // phpcs:enable
+
+ update_post_meta( $coupon->ID, 'wp_travel_engine_coupon_metas', $_data );
+
+ // Dedicated indexed key so the banner query avoids a serialized LIKE scan.
+ if ( 'yes' === ( $_data['general']['show_banner'] ?? 'no' ) ) {
+ update_post_meta( $coupon->ID, 'wp_travel_engine_coupon_show_banner', 'yes' );
+ } else {
+ delete_post_meta( $coupon->ID, 'wp_travel_engine_coupon_show_banner' );
+ }
+
+ delete_transient( Banner::TRANSIENT_KEY );
}
+ /**
+ * @since 6.8.2 Falls back to post creation date when coupon start date is blank; normalises start/end to midnight before comparing.
+ */
public static function is_coupon_date_valid( $coupon_id ) {
if ( empty( $coupon_id ) ) {
return false;
@@ -214,8 +277,14 @@
$coupon_metas = get_post_meta( $coupon_id, 'wp_travel_engine_coupon_metas', true );
$general_tab = isset( $coupon_metas['general'] ) ? $coupon_metas['general'] : array();
$coupon_expiry_date = isset( $general_tab['coupon_expiry_date'] ) ? $general_tab['coupon_expiry_date'] : '';
+ $coupon_start_date = isset( $general_tab['coupon_start_date'] ) ? $general_tab['coupon_start_date'] : '';
- $coupon_start_date = isset( $general_tab['coupon_start_date'] ) ? $general_tab['coupon_start_date'] : '';
+ if ( empty( $coupon_start_date ) ) {
+ $post_date = get_post_field( 'post_date', $coupon_id );
+ if ( ! empty( $post_date ) ) {
+ $coupon_start_date = $post_date;
+ }
+ }
// Check Coupon Status.
$coupon_status = get_post_status( $coupon_id );
@@ -226,28 +295,25 @@
$date_now = new DateTime();
$date_now->setTime( 0, 0, 0, 0 );
- if ( ! empty( $coupon_expiry_date ) ) {
- try {
- $coupon_start_date = new DateTime( $coupon_start_date );
- } catch ( Exception $e ) {
- return false;
- }
- try {
- $coupon_end_date = new DateTime( $coupon_expiry_date );
- } catch ( Exception $e ) {
- return false;
- }
+ try {
+ $start = new DateTime( $coupon_start_date );
+ $start->setTime( 0, 0, 0, 0 );
+ } catch ( Exception $e ) {
+ return false;
+ }
- return $date_now <= $coupon_end_date && $date_now >= $coupon_start_date;
- } else {
+ if ( ! empty( $coupon_expiry_date ) ) {
try {
- $coupon_start_date = new DateTime( $coupon_start_date );
- return $date_now >= $coupon_start_date;
+ $end = new DateTime( $coupon_expiry_date );
+ $end->setTime( 0, 0, 0, 0 );
} catch ( Exception $e ) {
return false;
}
+ return $date_now <= $end && $date_now >= $start;
}
+
+ return $date_now >= $start;
}
public static function coupon_can_be_applied( $coupon_id, $trip_id ) {
@@ -357,10 +423,10 @@
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
- AND meta_value = %s
+ AND meta_value = BINARY %s
",
$meta_key,
- esc_sql( $code )
+ $code
);
$results = $wpdb->get_results( $sql );
--- a/wp-travel-engine/includes/classes/Modules/CouponCode/Banner.php
+++ b/wp-travel-engine/includes/classes/Modules/CouponCode/Banner.php
@@ -0,0 +1,280 @@
+<?php
+/**
+ * Site-wide coupon promotional banner.
+ *
+ * @since 6.8.2
+ */
+
+namespace WPTravelEngineModulesCouponCode;
+
+use WPTravelEngineModulesCouponCode;
+
+class Banner {
+
+ const COOKIE_PREFIX = 'wte_coupon_banner_dismissed_';
+
+ const TRANSIENT_KEY = 'wte_banner_coupon_ids';
+
+ /**
+ * Whether banners have already been output this request.
+ *
+ * @since 6.8.2
+ * @var bool
+ */
+ protected bool $rendered = false;
+
+ public function __construct() {
+ add_action( 'wp_body_open', array( $this, 'render' ) );
+ // Fallback for themes that do not fire wp_body_open; JS relocates the banner to the top of <body>.
+ add_action( 'wp_footer', array( $this, 'render_fallback' ) );
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
+ }
+
+ /**
+ * Enqueue site-wide coupon banner stylesheet.
+ *
+ * @since 6.8.2
+ */
+ public function enqueue_styles(): void {
+ if ( is_admin() ) {
+ return;
+ }
+
+ $rel_path = 'dist/public/coupon-banner.css';
+ $abs_path = plugin_dir_path( WP_TRAVEL_ENGINE_FILE_PATH ) . $rel_path;
+ if ( ! file_exists( $abs_path ) ) {
+ return;
+ }
+
+ wp_enqueue_style(
+ 'wptravelengine-coupon-banner',
+ plugin_dir_url( WP_TRAVEL_ENGINE_FILE_PATH ) . $rel_path,
+ array(),
+ filemtime( $abs_path )
+ );
+ }
+
+ /**
+ * Active coupons that have the banner enabled.
+ *
+ * @since 6.8.2 Query a dedicated indexed meta key and cache the candidate IDs;
+ * active/show_banner state is still re-checked live per request.
+ *
+ * @return int[]
+ */
+ public function get_banner_coupon_ids(): array {
+ $candidate_ids = get_transient( self::TRANSIENT_KEY );
+ if ( false === $candidate_ids ) {
+ $query = new WP_Query(
+ array(
+ 'post_type' => defined( 'WP_TRAVEL_ENGINE_COUPONS_POST_TYPE' ) ? WP_TRAVEL_ENGINE_COUPONS_POST_TYPE : 'wte-coupon',
+ 'post_status' => 'publish',
+ 'posts_per_page' => 20,
+ 'fields' => 'ids',
+ 'orderby' => 'date',
+ 'order' => 'DESC',
+ 'no_found_rows' => true,
+ 'meta_query' => array(
+ array(
+ 'key' => 'wp_travel_engine_coupon_show_banner',
+ 'value' => 'yes',
+ 'compare' => '=',
+ ),
+ ),
+ )
+ );
+
+ $candidate_ids = array_map( 'intval', $query->posts );
+ set_transient( self::TRANSIENT_KEY, $candidate_ids, 5 * MINUTE_IN_SECONDS );
+ }
+
+ $ids = array();
+ foreach ( $candidate_ids as $coupon_id ) {
+ if ( 'active' !== CouponCode::get_coupon_status( $coupon_id ) ) {
+ continue;
+ }
+ if ( 'yes' !== CouponCode::get_coupon_meta( $coupon_id, 'general', 'show_banner' ) ) {
+ continue;
+ }
+ $ids[] = (int) $coupon_id;
+ }
+
+ /**
+ * Filter the coupon IDs eligible for the site-wide banner.
+ *
+ * @since 6.8.2
+ *
+ * @param int[] $ids
+ */
+ return apply_filters( 'wptravelengine_coupon_banner_ids', $ids );
+ }
+
+ /**
+ * Render banners via wp_footer for themes lacking wp_body_open.
+ *
+ * Skips when wp_body_open already rendered them. Output is relocated to the
+ * top of <body> on the client so placement matches the wp_body_open path.
+ *
+ * @since 6.8.2
+ */
+ public function render_fallback() {
+ if ( $this->rendered ) {
+ return;
+ }
+ $this->render( true );
+ }
+
+ /**
+ * @since 6.8.2 Added $is_footer to support the wp_footer fallback render.
+ *
+ * @param bool $is_footer Whether output is on wp_footer (requires client-side relocation).
+ */
+ public function render( bool $is_footer = false ) {
+ if ( $this->rendered || is_admin() ) {
+ return;
+ }
+
+ $ids = $this->get_banner_coupon_ids();
+ if ( empty( $ids ) ) {
+ return;
+ }
+
+ $banners = array();
+ foreach ( $ids as $coupon_id ) {
+ if ( ! empty( $_COOKIE[ self::COOKIE_PREFIX . $coupon_id ] ) ) {
+ continue;
+ }
+ $message = $this->build_message( $coupon_id );
+ if ( '' === $message ) {
+ continue;
+ }
+ $banners[ $coupon_id ] = $message;
+ }
+
+ if ( empty( $banners ) ) {
+ return;
+ }
+
+ $this->rendered = true;
+
+ foreach ( $banners as $coupon_id => $message ) {
+ $bg = CouponCode::get_coupon_meta( $coupon_id, 'general', 'banner_bg_color' );
+ $text = CouponCode::get_coupon_meta( $coupon_id, 'general', 'banner_text_color' );
+ $style = '';
+ $classes = 'wte-coupon-banner';
+ if ( ! empty( $bg ) ) {
+ $style .= 'background:' . esc_attr( $bg ) . ';';
+ $classes .= ' wte-coupon-banner--custom-bg';
+ }
+ if ( ! empty( $text ) ) {
+ $style .= 'color:' . esc_attr( $text ) . ';';
+ }
+ printf(
+ '<div class="%7$s" data-coupon-id="%1$d" role="region" aria-label="%2$s"%6$s>'
+ . '<div class="wte-coupon-banner__inner">'
+ . '<span class="wte-coupon-banner__icon" aria-hidden="true">%3$s</span>'
+ . '<span class="wte-coupon-banner__text">%4$s</span>'
+ . '</div>'
+ . '<button type="button" class="wte-coupon-banner__close" aria-label="%5$s">×</button>'
+ . '</div>',
+ (int) $coupon_id,
+ esc_attr__( 'Promotional offer', 'wp-travel-engine' ),
+ $this->bell_icon(),
+ wp_kses_post( $message ),
+ esc_attr__( 'Dismiss banner', 'wp-travel-engine' ),
+ $style ? ' style="' . $style . '"' : '',
+ esc_attr( $classes )
+ );
+ }
+
+ $this->print_dismiss_script();
+
+ if ( $is_footer ) {
+ $this->print_relocate_script();
+ }
+ }
+
+ protected function build_message( int $coupon_id ): string {
+ $code = get_post_meta( $coupon_id, 'wp_travel_engine_coupon_code', true );
+ if ( empty( $code ) ) {
+ return '';
+ }
+
+ $template = CouponCode::get_coupon_meta( $coupon_id, 'general', 'banner_message' );
+ if ( empty( $template ) ) {
+ return '';
+ }
+
+ $type = CouponCode::get_discount_type( $coupon_id );
+ $value = (float) CouponCode::get_discount_value( $coupon_id );
+
+ if ( 'percentage' === $type ) {
+ $value_str = ( floor( $value ) == $value ? (int) $value : $value ) . '%';
+ } else {
+ $value_str = function_exists( 'wte_get_formated_price' ) ? wte_get_formated_price( $value ) : (string) $value;
+ }
+
+ $message = strtr(
+ $template,
+ array(
+ '{code}' => $code,
+ '{value}' => $value_str,
+ )
+ );
+
+ /**
+ * Filter the rendered banner message.
+ *
+ * @since 6.8.2
+ *
+ * @param string $message Rendered message.
+ * @param int $coupon_id Coupon ID.
+ */
+ return (string) apply_filters( 'wptravelengine_coupon_banner_message', $message, $coupon_id );
+ }
+
+ protected function bell_icon(): string {
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"/><path d="M10.3 21a1.94 1.94 0 0 0 3.4 0"/></svg>';
+ }
+
+ protected function print_dismiss_script(): void {
+ static $printed = false;
+ if ( $printed ) {
+ return;
+ }
+ $printed = true;
+ ?>
+<script>
+(function(){
+ document.addEventListener('click', function(e){
+ var btn = e.target.closest('.wte-coupon-banner__close');
+ if (!btn) return;
+ var banner = btn.closest('.wte-coupon-banner');
+ if (!banner) return;
+ var id = banner.getAttribute('data-coupon-id');
+ document.cookie = '<?php echo esc_js( self::COOKIE_PREFIX ); ?>' + id + '=1; path=/; max-age=' + (60*60*24*30) + '; SameSite=Lax';
+ banner.parentNode.removeChild(banner);
+ });
+})();
+</script>
+ <?php
+ }
+
+ /**
+ * Relocate footer-rendered banners to the top of <body>.
+ *
+ * @since 6.8.2
+ */
+ protected function print_relocate_script(): void {
+ ?>
+<script>
+(function(){
+ var banners = document.querySelectorAll('.wte-coupon-banner');
+ for (var i = banners.length - 1; i >= 0; i--) {
+ document.body.insertBefore(banners[i], document.body.firstChild);
+ }
+})();
+</script>
+ <?php
+ }
+}
--- a/wp-travel-engine/includes/classes/Modules/CouponCode/views/coupon-metabox-content.php
+++ b/wp-travel-engine/includes/classes/Modules/CouponCode/views/coupon-metabox-content.php
@@ -1,265 +1,107 @@
<?php
-
-use WPTravelEngineHelpersFunctions;
-use WPTravelEngineModulesCouponCode;
-
/**
- * Coupons General Tab Contents
+ * Coupon edit metabox — React app mount.
*
* @package WP Travel Engine Coupons
*/
-global $post;
-wp_enqueue_script( 'wte-edit--coupon' );
-
-// General Tab Data.
-$coupon_metas = get_post_meta( $post->ID, 'wp_travel_engine_coupon_metas', true );
-$general_tab = isset( $coupon_metas['general'] ) ? $coupon_metas['general'] : array();
-$coupon_code = get_post_meta( $post->ID, 'wp_travel_engine_coupon_code', true );
-// Field Values.
-$coupon_active = isset( $general_tab['coupon_active'] ) ? $general_tab['coupon_active'] : 'yes';
-$coupon_code = ! empty( $coupon_code ) ? $coupon_code : '';
-$coupon_type = isset( $general_tab['coupon_type'] ) ? $general_tab['coupon_type'] : 'fixed';
-$coupon_value = isset( $general_tab['coupon_value'] ) ? $general_tab['coupon_value'] : '';
+use WPTravelEngineHelpersFunctions;
+use WPTravelEngineModulesCouponCode;
-$date_format = get_option( 'date_format' );
+global $post;
-$coupon_expiry_date = isset( $general_tab['coupon_expiry_date'] ) ? $general_tab['coupon_expiry_date'] : '';
+$coupon_metas = get_post_meta( $post->ID, 'wp_travel_engine_coupon_metas', true );
+$coupon_metas = is_array( $coupon_metas ) ? $coupon_metas : array();
+$general_tab = isset( $coupon_metas['general'] ) ? $coupon_metas['general'] : array();
+$restrictions_tab = isset( $coupon_metas['restriction'] ) ? $coupon_metas['restriction'] : array();
+$coupon_code = get_post_meta( $post->ID, 'wp_travel_engine_coupon_code', true );
try {
- $coupon_expiry_date = ! empty( $general_tab['coupon_expiry_date'] ) ? ( new DateTime( $general_tab['coupon_expiry_date'] ) )->format( 'Y-m-d' ) : '';
+ $start_date = ! empty( $general_tab['coupon_start_date'] )
+ ? ( new DateTime( $general_tab['coupon_start_date'] ) )->format( 'Y-m-d' )
+ : '';
} catch ( Exception $e ) {
- $coupon_expiry_date = '';
+ $start_date = '';
}
try {
- $coupon_start_date = isset( $general_tab['coupon_start_date'] ) ? ( new DateTime( $general_tab['coupon_start_date'] ) )->format( 'Y-m-d' ) : gmdate( 'Y-m-d' );
+ $expiry_date = ! empty( $general_tab['coupon_expiry_date'] )
+ ? ( new DateTime( $general_tab['coupon_expiry_date'] ) )->format( 'Y-m-d' )
+ : '';
} catch ( Exception $e ) {
- $coupon_start_date = gmdate( 'Y-m-d' );
+ $expiry_date = '';
+}
+try {
+ $trip_starts_after = ! empty( $general_tab['trip_starts_after'] )
+ ? ( new DateTime( $general_tab['trip_starts_after'] ) )->format( 'Y-m-d' )
+ : '';
+} catch ( Exception $e ) {
+ $trip_starts_after = '';
+}
+try {
+ $trip_starts_before = ! empty( $general_tab['trip_starts_before'] )
+ ? ( new DateTime( $general_tab['trip_starts_before'] ) )->format( 'Y-m-d' )
+ : '';
+} catch ( Exception $e ) {
+ $trip_starts_before = '';
}
-$coupon_id = CouponCode::coupon_id_by_code( $coupon_code );
-
-$wp_travel_engine_settings = get_option( 'wp_travel_engine_settings', true );
+$coupon_id = ! empty( $coupon_code ) ? CouponCode::coupon_id_by_code( $coupon_code ) : false;
+$coupon_status_key = $coupon_id ? CouponCode::get_coupon_status( $coupon_id ) : 'inactive';
+$is_active = 'active' === $coupon_status_key;
+$status_label = $is_active ? __( 'Active', 'wp-travel-engine' ) : __( 'Inactive', 'wp-travel-engine' );
+
+$wte_settings = get_option( 'wp_travel_engine_settings', array() );
+$currency_code = ! empty( $wte_settings['currency_code'] ) ? $wte_settings['currency_code'] : 'USD';
+$currency = Functions::currency_symbol_by_code( $currency_code );
+
+$trips_raw = wp_travel_engine_get_trips_array();
+$trips_data = array();
+foreach ( $trips_raw as $tid => $ttitle ) {
+ $trips_data[] = array(
+ 'value' => (string) $tid,
+ 'label' => $ttitle,
+ );
+}
-$code = ! empty( $wp_travel_engine_settings['currency_code'] ) ? $wp_travel_engine_settings['currency_code'] : 'USD';
+$restricted_trips = isset( $restrictions_tab['restricted_trips'] ) ? array_map( 'strval', (array) $restrictions_tab['restricted_trips'] ) : array();
-$currency = Functions::currency_symbol_by_code( $code );
+$config = array(
+ 'couponId' => $coupon_id ? (int) $coupon_id : 0,
+ 'currency' => html_entity_decode( $currency ),
+ 'isActive' => $is_active,
+ 'statusLabel' => $coupon_id ? $status_label : '',
+ 'postTitle' => $post ? $post->post_title : '',
+ 'addNewUrl' => admin_url( 'post-new.php?post_type=' . WP_TRAVEL_ENGINE_COUPONS_POST_TYPE ),
+ 'listUrl' => admin_url( 'edit.php?post_type=' . WP_TRAVEL_ENGINE_COUPONS_POST_TYPE ),
+ 'trips' => $trips_data,
+ 'coupon' => array(
+ 'code' => $coupon_code,
+ 'general' => array(
+ 'coupon_type' => isset( $general_tab['coupon_type'] ) ? $general_tab['coupon_type'] : 'fixed',
+ 'coupon_value' => isset( $general_tab['coupon_value'] ) ? $general_tab['coupon_value'] : '',
+ 'coupon_start_date' => $start_date,
+ 'coupon_expiry_date' => $expiry_date,
+ 'trip_date_restriction_enabled' => ! empty( $general_tab['trip_date_restriction_enabled'] ) && 'yes' === $general_tab['trip_date_restriction_enabled'],
+ 'trip_starts_after' => $trip_starts_after,
+ 'trip_starts_before' => $trip_starts_before,
+ 'show_banner' => ! empty( $general_tab['show_banner'] ) && 'yes' === $general_tab['show_banner'],
+ 'banner_message' => isset( $general_tab['banner_message'] ) ? $general_tab['banner_message'] : '',
+ 'banner_bg_color' => isset( $general_tab['banner_bg_color'] ) && $general_tab['banner_bg_color'] ? $general_tab['banner_bg_color'] : '#f97316',
+ 'banner_text_color' => isset( $general_tab['banner_text_color'] ) && $general_tab['banner_text_color'] ? $general_tab['banner_text_color'] : '#ffffff',
+ ),
+ 'restriction' => array(
+ 'restricted_trips' => $restricted_trips,
+ 'coupon_limit_number' => isset( $restrictions_tab['coupon_limit_number'] ) ? $restrictions_tab['coupon_limit_number'] : '',
+ ),
+ ),
+);
+
+wp_enqueue_script( 'wptravelengine-coupon-edit' );
+wp_add_inline_script(
+ 'wptravelengine-coupon-edit',
+ 'var wptravelengineCouponEdit = ' . wp_json_encode( $config ) . ';',
+ 'before'
+);
+wp_nonce_field( 'wte_save_coupon', 'wte_coupon_nonce' );
?>
-<div class="wpte-block-content"><!-- Start .wpte-block-content -->
- <div class="wpte-form-block">
- <?php if ( $coupon_id ) : ?>
- <div class="wpte-field wpte-floated departure-dates-options">
- <label
- class="wpte-field-label"
- for="currency"
- >
- <?php esc_html_e( 'Coupon Status ', 'wp-travel-engine' ); ?>
- </label>
- <?php
- $coupon_status = CouponCode::get_coupon_status( $coupon_id );
- if ( 'active' === $coupon_status ) {
- ?>
- <span class="wp-travel-engine-info-msg">
- <?php echo esc_html__( 'Active', 'wp-travel-engine' ); ?>
- </span>
- <?php
- } else {
- ?>
- <span class="wp-travel-engine-error-msg">
- <?php echo esc_html__( 'Inactive', 'wp-travel-engine' ); ?>
- </span>
- <?php
- }
- ?>
- <span
- class="wpte-tooltip"><?php esc_html_e( 'Either the coupon is enabled in site or not.', 'wp-travel-engine' ); ?></span>
- </div>
- <?php endif; ?>
- <div class="wpte-field wpte-floated departure-dates-options">
- <label
- class="wpte-field-label"
- for="coupon-code"
- ><?php esc_html_e( 'Coupon Code', 'wp-travel-engine' ); ?></label>
- <input
- required="required"
- type="text"
- id="coupon-code"
- name="wp_travel_engine_coupon_code"
- placeholder="<?php echo esc_attr__( 'WP-TRAVEL-ENGINE-SALE', 'wp-travel-engine' ); ?>"
- value="<?php echo esc_attr( $coupon_code ); ?>"
- data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_travel_engine_check_coupon_code' ) ); ?>"
- >
- <input
- id="wp-travel-coupon-id"
- type="hidden"
- value="<?php echo esc_attr( $coupon_id ); ?>"
- >
- <span
- class="wpte-tooltip"><?php esc_html_e( 'Unique Identifier for the coupon.', 'wp-travel-engine' ); ?></span>
- <span
- class="wpte-tooltip wp-travel-coupon_code-error wp-travel-error"
- style="display:none;"
- ><strong><?php echo esc_html__( 'Warning :', 'wp-travel-engine' ); ?></strong><?php esc_html_e( ' Coupon Code already in use. Multiple coupouns with same code results to only latest coupon settings being applied.', 'wp-travel-engine' ); ?></span>
- </div>
-
- <div class="wpte-field wpte-floated wpte-select departure-dates-options">
- <label
- class="wpte-field-label"
- for="coupon-type"
- ><?php esc_html_e( 'Discount Type', 'wp-travel-engine' ); ?></label>
- <select
- class="wpte-enhanced-select wte-coupon-code-type"
- id="coupon-type"
- name="wp_travel_engine_coupon[general][coupon_type]"
- >
- <option
- value="fixed"
- <?php selected( $coupon_type, 'fixed' ); ?>
- ><?php esc_html_e( 'Fixed', 'wp-travel-engine' ); ?></option>
- <option
- value="percentage"
- <?php selected( $coupon_type, 'percentage' ); ?>
- ><?php esc_html_e( 'Percentage', 'wp-travel-engine' ); ?></option>
- </select>
- <span
- class="wpte-tooltip"><?php esc_html_e( 'Coupon Type: Fixed Discount Amount or Percentage discount( Applies to cart total price ).', 'wp-travel-engine' ); ?></span>
-
- </div>
-
- <div class="wpte-field wpte-number wpte-floated departure-dates-options">
- <label
- class="wpte-field-label"
- for="coupon-code"
- ><?php esc_html_e( 'Discount Value', 'wp-travel-engine' ); ?></label>
- <div class="wpte-floated">
- <input
- required="required"
- type="number"
- min="1"
- <?php echo 'percentage' === $coupon_type ? 'max="100"' : ''; ?>
- step="0.01"
- id="coupon-value"
- name="wp_travel_engine_coupon[general][coupon_value]"
- placeholder="<?php echo esc_attr__( 'Discount Value', 'wp-travel-engine' ); ?>"
- value="<?ph