create-event.php
252 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EventCalendarCreateEvent. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category EventCalendarCreateEvent |
| 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 | use SureTriggers\Integrations\AutomateAction; |
| 15 | use SureTriggers\Traits\SingletonLoader; |
| 16 | |
| 17 | /** |
| 18 | * EventCalendarCreateEvent |
| 19 | * |
| 20 | * @category EventCalendarCreateEvent |
| 21 | * @package SureTriggers |
| 22 | * @author BSF <username@example.com> |
| 23 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 24 | * @link https://www.brainstormforce.com/ |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class EventCalendarCreateEvent extends AutomateAction { |
| 28 | |
| 29 | /** |
| 30 | * Integration type. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $integration = 'TheEventCalendar'; |
| 35 | |
| 36 | /** |
| 37 | * Action name. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $action = 'event_calendar_create_event'; |
| 42 | |
| 43 | use SingletonLoader; |
| 44 | |
| 45 | /** |
| 46 | * Register a action. |
| 47 | * |
| 48 | * @param array $actions actions. |
| 49 | * @return array |
| 50 | */ |
| 51 | public function register( $actions ) { |
| 52 | $actions[ $this->integration ][ $this->action ] = [ |
| 53 | 'label' => __( 'Create an event', 'suretriggers' ), |
| 54 | 'action' => $this->action, |
| 55 | 'function' => [ $this, 'action_listener' ], |
| 56 | ]; |
| 57 | return $actions; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Action listener. |
| 62 | * |
| 63 | * @param int $user_id user_id. |
| 64 | * @param int $automation_id automation_id. |
| 65 | * @param array $fields fields. |
| 66 | * @param array $selected_options selectedOptions. |
| 67 | * @throws Exception Exception. |
| 68 | * |
| 69 | * @return array|bool |
| 70 | */ |
| 71 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 72 | |
| 73 | if ( ! class_exists( 'Tribe__Events__Main' ) || ! function_exists( 'tribe_create_event' ) ) { |
| 74 | return [ |
| 75 | 'status' => 'error', |
| 76 | 'message' => 'The Events Calendar plugin is not installed or activated.', |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | $event_title = isset( $selected_options['event_title'] ) ? sanitize_text_field( $selected_options['event_title'] ) : ''; |
| 81 | |
| 82 | if ( empty( $event_title ) ) { |
| 83 | return [ |
| 84 | 'status' => 'error', |
| 85 | 'message' => 'Event title is required.', |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | $start_date = isset( $selected_options['event_start_date'] ) ? sanitize_text_field( $selected_options['event_start_date'] ) : ''; |
| 90 | $end_date = isset( $selected_options['event_end_date'] ) ? sanitize_text_field( $selected_options['event_end_date'] ) : ''; |
| 91 | |
| 92 | if ( empty( $start_date ) ) { |
| 93 | return [ |
| 94 | 'status' => 'error', |
| 95 | 'message' => 'Event start date is required.', |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | $args = [ |
| 100 | 'post_title' => $event_title, |
| 101 | 'post_status' => 'publish', |
| 102 | 'EventAllDay' => false, |
| 103 | ]; |
| 104 | |
| 105 | // Description. |
| 106 | if ( ! empty( $selected_options['event_description'] ) ) { |
| 107 | $args['post_content'] = wp_kses_post( $selected_options['event_description'] ); |
| 108 | } |
| 109 | |
| 110 | // Start date/time — expects format "Y-m-d H:i:s" or "Y-m-d". |
| 111 | $args['EventStartDate'] = $start_date; |
| 112 | |
| 113 | if ( ! empty( $selected_options['event_start_time'] ) ) { |
| 114 | $args['EventStartDate'] = $start_date . ' ' . sanitize_text_field( $selected_options['event_start_time'] ); |
| 115 | } |
| 116 | |
| 117 | // End date/time. |
| 118 | if ( ! empty( $end_date ) ) { |
| 119 | $args['EventEndDate'] = $end_date; |
| 120 | |
| 121 | if ( ! empty( $selected_options['event_end_time'] ) ) { |
| 122 | $args['EventEndDate'] = $end_date . ' ' . sanitize_text_field( $selected_options['event_end_time'] ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // All day event. |
| 127 | if ( ! empty( $selected_options['event_all_day'] ) && 'yes' === strtolower( sanitize_text_field( $selected_options['event_all_day'] ) ) ) { |
| 128 | $args['EventAllDay'] = true; |
| 129 | } |
| 130 | |
| 131 | // Event URL / website. |
| 132 | if ( ! empty( $selected_options['event_url'] ) ) { |
| 133 | $args['EventURL'] = esc_url_raw( $selected_options['event_url'] ); |
| 134 | } |
| 135 | |
| 136 | // Event cost. |
| 137 | if ( isset( $selected_options['event_cost'] ) && '' !== $selected_options['event_cost'] ) { |
| 138 | $args['EventCost'] = sanitize_text_field( $selected_options['event_cost'] ); |
| 139 | } |
| 140 | |
| 141 | if ( ! empty( $selected_options['event_currency_symbol'] ) ) { |
| 142 | $args['EventCurrencySymbol'] = sanitize_text_field( $selected_options['event_currency_symbol'] ); |
| 143 | } |
| 144 | |
| 145 | // Post status override. |
| 146 | if ( ! empty( $selected_options['event_status'] ) ) { |
| 147 | $allowed_statuses = [ 'publish', 'draft', 'pending', 'private' ]; |
| 148 | $status = sanitize_text_field( $selected_options['event_status'] ); |
| 149 | if ( in_array( $status, $allowed_statuses, true ) ) { |
| 150 | $args['post_status'] = $status; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $event_id = tribe_create_event( $args ); |
| 155 | |
| 156 | if ( ! $event_id || is_wp_error( $event_id ) ) { |
| 157 | return [ |
| 158 | 'status' => 'error', |
| 159 | 'message' => 'Failed to create the event.', |
| 160 | ]; |
| 161 | } |
| 162 | |
| 163 | // Venue — set after creation via post meta. |
| 164 | if ( ! empty( $selected_options['event_venue_id'] ) ) { |
| 165 | $venue_id = absint( $selected_options['event_venue_id'] ); |
| 166 | if ( $venue_id > 0 && 'tribe_venue' === get_post_type( $venue_id ) ) { |
| 167 | update_post_meta( $event_id, '_EventVenueID', $venue_id ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Organizer — set after creation via post meta. |
| 172 | if ( ! empty( $selected_options['event_organizer_id'] ) ) { |
| 173 | $organizer_id = absint( $selected_options['event_organizer_id'] ); |
| 174 | if ( $organizer_id > 0 && 'tribe_organizer' === get_post_type( $organizer_id ) ) { |
| 175 | update_post_meta( $event_id, '_EventOrganizerID', $organizer_id ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Category — set after creation via wp_set_object_terms. |
| 180 | if ( ! empty( $selected_options['event_category_id'] ) ) { |
| 181 | $raw_category = $selected_options['event_category_id']; |
| 182 | // Extract value from select field array format. |
| 183 | if ( is_array( $raw_category ) ) { |
| 184 | $first = reset( $raw_category ); |
| 185 | if ( is_array( $first ) && isset( $first['value'] ) ) { |
| 186 | $raw_category = $first['value']; |
| 187 | } elseif ( is_object( $first ) && isset( $first->value ) ) { |
| 188 | $raw_category = $first->value; |
| 189 | } else { |
| 190 | $raw_category = $first; |
| 191 | } |
| 192 | } |
| 193 | $category_id = absint( $raw_category ); |
| 194 | if ( $category_id > 0 && term_exists( $category_id, 'tribe_events_cat' ) ) { |
| 195 | wp_set_object_terms( $event_id, [ $category_id ], 'tribe_events_cat' ); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Featured image — download from URL and set as post thumbnail. |
| 200 | if ( ! empty( $selected_options['event_featured_image'] ) ) { |
| 201 | $image_url = esc_url_raw( $selected_options['event_featured_image'] ); |
| 202 | if ( ! function_exists( 'media_sideload_image' ) ) { |
| 203 | require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 204 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 205 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 206 | } |
| 207 | $attachment_id = media_sideload_image( $image_url, $event_id, $event_title, 'id' ); |
| 208 | if ( ! is_wp_error( $attachment_id ) && is_int( $attachment_id ) ) { |
| 209 | set_post_thumbnail( $event_id, $attachment_id ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | $context = [ |
| 214 | 'event_id' => $event_id, |
| 215 | 'event_title' => $event_title, |
| 216 | 'event_url' => get_permalink( $event_id ), |
| 217 | 'event_start_date' => $start_date, |
| 218 | 'event_end_date' => $end_date, |
| 219 | ]; |
| 220 | |
| 221 | if ( ! empty( $selected_options['event_description'] ) ) { |
| 222 | $context['event_description'] = $selected_options['event_description']; |
| 223 | } |
| 224 | |
| 225 | // Read back venue/organizer/category from DB to confirm they were saved. |
| 226 | $saved_venue = get_post_meta( $event_id, '_EventVenueID', true ); |
| 227 | if ( ! empty( $saved_venue ) && is_numeric( $saved_venue ) ) { |
| 228 | $context['event_venue_id'] = $saved_venue; |
| 229 | $context['event_venue_name'] = get_the_title( (int) $saved_venue ); |
| 230 | } |
| 231 | |
| 232 | $saved_organizer = get_post_meta( $event_id, '_EventOrganizerID', true ); |
| 233 | if ( ! empty( $saved_organizer ) && is_numeric( $saved_organizer ) ) { |
| 234 | $context['event_organizer_id'] = $saved_organizer; |
| 235 | $context['event_organizer_name'] = get_the_title( (int) $saved_organizer ); |
| 236 | } |
| 237 | |
| 238 | $saved_terms = wp_get_object_terms( $event_id, 'tribe_events_cat' ); |
| 239 | if ( ! is_wp_error( $saved_terms ) && ! empty( $saved_terms ) ) { |
| 240 | $context['event_category_id'] = $saved_terms[0]->term_id; |
| 241 | $context['event_category_name'] = $saved_terms[0]->name; |
| 242 | } |
| 243 | |
| 244 | $context['event_featured_image_id'] = get_post_meta( $event_id, '_thumbnail_id', true ); |
| 245 | $context['event_featured_image_url'] = get_the_post_thumbnail_url( $event_id ); |
| 246 | |
| 247 | return $context; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | EventCalendarCreateEvent::get_instance(); |
| 252 |