suredash-add-comment-to-post.php
3 months ago
suredash-create-event.php
3 weeks ago
suredash-fetch-post.php
3 months ago
suredash-create-event.php
223 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 | return $response_data; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | SureDashCreateEvent::get_instance(); |
| 223 |