PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.37
OttoKit: All-in-One Automation Platform v1.1.37
1.1.37 1.1.36 1.1.35 1.1.34 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 / SureDash / actions / suredash-create-event.php
suretriggers / src / Integrations / SureDash / actions Last commit date
suredash-add-comment-to-post.php 4 months ago suredash-create-event.php 2 weeks ago suredash-fetch-post.php 4 months ago
suredash-create-event.php
257 lines
1 <?php
2 /**
3 * SureDashCreateEvent.
4 * php version 5.6
5 *
6 * @category SureDashCreateEvent
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\SureDash\Actions;
15
16 use SureDashboard\Core\Routers\Backend as SureDashBackendRouter;
17 use SureTriggers\Integrations\AutomateAction;
18 use SureTriggers\Traits\SingletonLoader;
19 use WP_REST_Request;
20 use Exception;
21
22 /**
23 * SureDashCreateEvent
24 *
25 * @category SureDashCreateEvent
26 * @package SureTriggers
27 * @author BSF <username@example.com>
28 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
29 * @link https://www.brainstormforce.com/
30 * @since 1.0.0
31 */
32 class SureDashCreateEvent extends AutomateAction {
33
34 /**
35 * Integration type.
36 *
37 * @var string
38 */
39 public $integration = 'SureDash';
40
41 /**
42 * Action name.
43 *
44 * @var string
45 */
46 public $action = 'suredash_create_event';
47
48 use SingletonLoader;
49
50 /**
51 * Register a action.
52 *
53 * @param array $actions actions.
54 * @return array
55 */
56 public function register( $actions ) {
57 $actions[ $this->integration ][ $this->action ] = [
58 'label' => __( 'Create Event in Space', 'suretriggers' ),
59 'action' => 'suredash_create_event',
60 'function' => [ $this, 'action_listener' ],
61 ];
62
63 return $actions;
64 }
65
66 /**
67 * Action listener.
68 *
69 * @param int $user_id user_id.
70 * @param int $automation_id automation_id.
71 * @param array $fields fields.
72 * @param array $selected_options selectedOptions.
73 *
74 * @return array|bool
75 * @throws Exception Error.
76 */
77 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
78 if ( ! defined( 'SUREDASHBOARD_VER' ) || ! defined( 'SUREDASHBOARD_SUB_CONTENT_POST_TYPE' ) || ! class_exists( SureDashBackendRouter::class ) ) {
79 return [
80 'status' => 'error',
81 'message' => __( 'SureDash plugin is not active or properly configured.', 'suretriggers' ),
82 ];
83 }
84
85 $event_title = ! empty( $selected_options['post_title'] ) ? sanitize_text_field( $selected_options['post_title'] ) : '';
86 $space_id = ! empty( $selected_options['space_id'] ) ? absint( $selected_options['space_id'] ) : 0;
87
88 if ( empty( $event_title ) ) {
89 return [
90 'status' => 'error',
91 'message' => __( 'Event title is required.', 'suretriggers' ),
92 ];
93 }
94
95 if ( empty( $space_id ) ) {
96 return [
97 'status' => 'error',
98 'message' => __( 'Event/Calendar space is required.', 'suretriggers' ),
99 ];
100 }
101
102 $space_post_type = defined( 'SUREDASHBOARD_POST_TYPE' ) ? SUREDASHBOARD_POST_TYPE : 'portal';
103 $space = get_post( $space_id );
104
105 if ( ! $space || $space_post_type !== $space->post_type ) {
106 return [
107 'status' => 'error',
108 'message' => __( 'Selected space could not be found.', 'suretriggers' ),
109 ];
110 }
111
112 if ( 'events' !== get_post_meta( $space_id, 'integration', true ) ) {
113 return [
114 'status' => 'error',
115 'message' => __( 'Selected space is not an Event/Calendar space.', 'suretriggers' ),
116 ];
117 }
118
119 $post_status = ! empty( $selected_options['post_status'] ) && in_array( $selected_options['post_status'], [ 'publish', 'draft' ], true )
120 ? $selected_options['post_status']
121 : 'publish';
122
123 $post_data = [
124 'post_title' => $event_title,
125 'post_status' => $post_status,
126 'space_id' => $space_id,
127 'context' => 'events',
128 'space_type' => 'events',
129 ];
130
131 if ( ! empty( $selected_options['post_slug'] ) ) {
132 $post_data['post_slug'] = sanitize_title( $selected_options['post_slug'] );
133 }
134
135 if ( ! empty( $selected_options['comment_status'] ) && in_array( $selected_options['comment_status'], [ 'open', 'closed' ], true ) ) {
136 $post_data['comment_status'] = $selected_options['comment_status'];
137 }
138
139 if ( ! empty( $selected_options['event_date'] ) ) {
140 $post_data['event_date'] = sanitize_text_field( $selected_options['event_date'] );
141 }
142
143 if ( ! empty( $selected_options['event_start_time'] ) ) {
144 $post_data['event_start_time'] = sanitize_text_field( $selected_options['event_start_time'] );
145 }
146
147 if ( ! empty( $selected_options['event_duration'] ) ) {
148 $post_data['event_duration'] = sanitize_text_field( $selected_options['event_duration'] );
149 }
150
151 if ( ! empty( $selected_options['event_timezone'] ) ) {
152 $post_data['event_timezone'] = sanitize_text_field( $selected_options['event_timezone'] );
153 }
154
155 if ( ! empty( $selected_options['rsvp_link'] ) ) {
156 $post_data['rsvp_link'] = esc_url_raw( $selected_options['rsvp_link'] );
157 }
158
159 if ( ! empty( $selected_options['event_joining_link'] ) ) {
160 $post_data['event_joining_link'] = esc_url_raw( $selected_options['event_joining_link'] );
161 }
162
163 if ( ! empty( $selected_options['recorded_video_link'] ) ) {
164 $post_data['recorded_video_link'] = esc_url_raw( $selected_options['recorded_video_link'] );
165 }
166
167 if ( ! empty( $selected_options['visibility_scope'] ) ) {
168 $visibility_scope = $selected_options['visibility_scope'];
169 if ( ! is_array( $visibility_scope ) ) {
170 $visibility_scope = explode( ',', $visibility_scope );
171 }
172 $post_data['visibility_scope'] = implode(
173 ',',
174 array_values( array_filter( array_map( 'sanitize_text_field', $visibility_scope ) ) )
175 );
176 }
177
178 if ( ! empty( $selected_options['custom_post_cover_image'] ) ) {
179 $post_data['custom_post_cover_image'] = esc_url_raw( $selected_options['custom_post_cover_image'] );
180 }
181
182 if ( ! empty( $selected_options['custom_post_embed_media'] ) ) {
183 $post_data['custom_post_embed_media'] = esc_url_raw( $selected_options['custom_post_embed_media'] );
184 }
185
186 // Set the current user context so the event is attributed to the automation's user.
187 wp_set_current_user( $user_id );
188
189 $request = new WP_REST_Request( 'POST' );
190 $request->set_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
191
192 foreach ( $post_data as $key => $value ) {
193 $_POST[ $key ] = $value; //phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified inside create_post_for_space().
194 }
195
196 try {
197 $result = SureDashBackendRouter::get_instance()->create_post_for_space( $request );
198 } finally {
199 foreach ( array_keys( $post_data ) as $key ) {
200 unset( $_POST[ $key ] ); //phpcs:ignore WordPress.Security.NonceVerification.Missing
201 }
202 }
203
204 if ( empty( $result['success'] ) ) {
205 $this->set_error(
206 [
207 'post_data' => $post_data,
208 'msg' => ! empty( $result['data']['message'] ) ? $result['data']['message'] : __( 'Failed to create event.', 'suretriggers' ),
209 ]
210 );
211 return false;
212 }
213
214 $response_data = $result['data'];
215 $response_data['status'] = 'success';
216 $response_data['message'] = __( 'Event created successfully.', 'suretriggers' );
217
218 // create_post_for_space() doesn't add the event to the space's 'event_ids' list,
219 // so the Events tab won't show it unless we register it here too.
220 $new_event_id = ! empty( $response_data['id'] ) ? absint( $response_data['id'] ) : 0;
221
222 if ( $new_event_id ) {
223 $event_ids = function_exists( 'sd_get_post_meta' )
224 ? sd_get_post_meta( $space_id, 'event_ids', true )
225 : get_post_meta( $space_id, 'event_ids', true );
226
227 if ( ! is_array( $event_ids ) ) {
228 $event_ids = [];
229 }
230
231 if ( ! in_array( (string) $new_event_id, array_map( 'strval', $event_ids ), true ) ) {
232 $old_event_ids = $event_ids;
233 $event_ids[] = $new_event_id;
234
235 if ( function_exists( 'sd_update_post_meta' ) ) {
236 sd_update_post_meta( $space_id, 'event_ids', $event_ids );
237 } else {
238 update_post_meta( $space_id, 'event_ids', $event_ids );
239 }
240
241 // Mirror the hooks SureDash's update_a_space() save flow fires.
242 do_action(
243 'suredash_space_data_updated',
244 $space_id,
245 [ 'event_ids' => $old_event_ids ],
246 [ 'event_ids' => $event_ids ]
247 );
248 do_action( 'suredash_space_saved', $space_id, [ 'event_ids' => $event_ids ] );
249 }
250 }
251
252 return $response_data;
253 }
254 }
255
256 SureDashCreateEvent::get_instance();
257