PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.33
OttoKit: All-in-One Automation Platform v1.1.33
1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / wp-travel-engine / actions / create-coupon.php
suretriggers / src / Integrations / wp-travel-engine / actions Last commit date
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