create-coupon.php
10 months ago
create-trip.php
5 months ago
duplicate-trip.php
5 months ago
list-bookings.php
10 months ago
list-trips.php
10 months ago
retrieve-booking.php
10 months ago
retrieve-enquiry.php
1 month ago
update-booking-status.php
10 months ago
update-trip.php
5 months ago
create-coupon.php
170 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateCoupon. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateCoupon |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\WPTravelEngine\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | |
| 19 | /** |
| 20 | * CreateCoupon |
| 21 | * |
| 22 | * @category CreateCoupon |
| 23 | * @package SureTriggers |
| 24 | * @author BSF <username@example.com> |
| 25 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 26 | * @link https://www.brainstormforce.com/ |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | class CreateCoupon extends AutomateAction { |
| 30 | |
| 31 | use SingletonLoader; |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'WPTravelEngine'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'wte_create_coupon'; |
| 46 | |
| 47 | /** |
| 48 | * Register the action. |
| 49 | * |
| 50 | * @param array $actions List of registered actions. |
| 51 | * @return array Modified list of actions. |
| 52 | */ |
| 53 | public function register( $actions ) { |
| 54 | $actions[ $this->integration ][ $this->action ] = [ |
| 55 | 'label' => __( 'Create a Coupon', 'suretriggers' ), |
| 56 | 'description' => __( 'Create a new coupon for WP Travel Engine', 'suretriggers' ), |
| 57 | 'action' => $this->action, |
| 58 | 'function' => [ $this, '_action_listener' ], |
| 59 | ]; |
| 60 | return $actions; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Action listener that creates a coupon post and meta. |
| 65 | * |
| 66 | * @param int $user_id User ID. |
| 67 | * @param int $automation_id Automation ID. |
| 68 | * @param array $fields Trigger fields. |
| 69 | * @param array $selected_options Selected options for coupon. |
| 70 | * @return array Result with status and data or error. |
| 71 | */ |
| 72 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 73 | |
| 74 | if ( ! function_exists( 'wp_travel_engine_get_settings' ) ) { |
| 75 | return [ |
| 76 | 'status' => 'error', |
| 77 | 'message' => __( 'WP Travel Engine plugin is not active.', 'suretriggers' ), |
| 78 | |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | if ( empty( $selected_options['coupon_code'] ) ) { |
| 83 | return [ |
| 84 | 'status' => 'error', |
| 85 | 'message' => __( 'Coupon code is required.', 'suretriggers' ), |
| 86 | |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | $coupon_data = [ |
| 91 | 'post_title' => sanitize_text_field( $selected_options['coupon_title'] ), |
| 92 | 'post_status' => isset( $selected_options['status'] ) ? sanitize_text_field( $selected_options['status'] ) : 'publish', |
| 93 | 'post_type' => 'wte-coupon', |
| 94 | ]; |
| 95 | |
| 96 | $coupon_id = wp_insert_post( $coupon_data, true ); |
| 97 | |
| 98 | if ( is_wp_error( $coupon_id ) ) { |
| 99 | return [ |
| 100 | 'status' => 'error', |
| 101 | 'message' => $coupon_id->get_error_message(), |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | update_post_meta( $coupon_id, 'wp_travel_engine_coupon_code', sanitize_text_field( $selected_options['coupon_code'] ) ); |
| 106 | |
| 107 | $coupon_metas = []; |
| 108 | |
| 109 | $coupon_metas['general'] = [ |
| 110 | 'coupon_active' => 'yes', |
| 111 | 'coupon_type' => isset( $selected_options['discount_type'] ) ? sanitize_text_field( $selected_options['discount_type'] ) : 'fixed', |
| 112 | 'coupon_value' => isset( $selected_options['discount_value'] ) ? floatval( $selected_options['discount_value'] ) : 0, |
| 113 | ]; |
| 114 | |
| 115 | if ( ! empty( $selected_options['start_date'] ) ) { |
| 116 | $coupon_metas['general']['coupon_start_date'] = sanitize_text_field( $selected_options['start_date'] ); |
| 117 | } else { |
| 118 | $coupon_metas['general']['coupon_start_date'] = gmdate( 'Y-m-d' ); |
| 119 | } |
| 120 | |
| 121 | if ( ! empty( $selected_options['expiry_date'] ) ) { |
| 122 | $coupon_metas['general']['coupon_expiry_date'] = sanitize_text_field( $selected_options['expiry_date'] ); |
| 123 | } |
| 124 | |
| 125 | $coupon_metas['restriction'] = []; |
| 126 | |
| 127 | if ( ! empty( $selected_options['usage_limit'] ) ) { |
| 128 | $coupon_metas['restriction']['coupon_limit_number'] = absint( $selected_options['usage_limit'] ); |
| 129 | } |
| 130 | |
| 131 | if ( ! empty( $selected_options['trip_ids'] ) ) { |
| 132 | if ( is_array( $selected_options['trip_ids'] ) ) { |
| 133 | if ( isset( $selected_options['trip_ids'][0]['value'] ) ) { |
| 134 | $trip_ids = array_map( |
| 135 | function( $item ) { |
| 136 | return absint( $item['value'] ); |
| 137 | }, |
| 138 | $selected_options['trip_ids'] |
| 139 | ); |
| 140 | } else { |
| 141 | $trip_ids = array_map( 'absint', $selected_options['trip_ids'] ); |
| 142 | } |
| 143 | } else { |
| 144 | $trip_ids = array_map( 'absint', array_map( 'trim', explode( ',', $selected_options['trip_ids'] ) ) ); |
| 145 | } |
| 146 | $coupon_metas['restriction']['restricted_trips'] = $trip_ids; |
| 147 | } |
| 148 | |
| 149 | update_post_meta( $coupon_id, 'wp_travel_engine_coupon_metas', $coupon_metas ); |
| 150 | |
| 151 | return [ |
| 152 | 'status' => 'success', |
| 153 | 'data' => [ |
| 154 | 'coupon_id' => $coupon_id, |
| 155 | 'coupon_code' => sanitize_text_field( $selected_options['coupon_code'] ), |
| 156 | 'coupon_title' => html_entity_decode( get_the_title( $coupon_id ) ), |
| 157 | 'discount_type' => $coupon_metas['general']['coupon_type'], |
| 158 | 'discount_value' => $coupon_metas['general']['coupon_value'], |
| 159 | 'start_date' => $coupon_metas['general']['coupon_start_date'], |
| 160 | 'expiry_date' => isset( $coupon_metas['general']['coupon_expiry_date'] ) ? $coupon_metas['general']['coupon_expiry_date'] : '', |
| 161 | 'usage_limit' => isset( $coupon_metas['restriction']['coupon_limit_number'] ) ? $coupon_metas['restriction']['coupon_limit_number'] : '', |
| 162 | 'trip_ids' => isset( $coupon_metas['restriction']['restricted_trips'] ) ? $coupon_metas['restriction']['restricted_trips'] : [], |
| 163 | 'status' => $coupon_data['post_status'], |
| 164 | ], |
| 165 | ]; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | CreateCoupon::get_instance(); |
| 170 |