create-event.php
282 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateEvent. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateEvent |
| 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\EventOn\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Integrations\EventOn\EventOn; |
| 19 | use SureTriggers\Traits\SingletonLoader; |
| 20 | |
| 21 | /** |
| 22 | * CreateEvent |
| 23 | * |
| 24 | * @category CreateEvent |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class CreateEvent extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'EventOn'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'eon_create_event'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | |
| 57 | $actions[ $this->integration ][ $this->action ] = [ |
| 58 | 'label' => __( 'Create an Event', 'suretriggers' ), |
| 59 | 'action' => $this->action, |
| 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|mixed |
| 75 | * |
| 76 | * @throws Exception Exception. |
| 77 | */ |
| 78 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 79 | unset( $user_id, $automation_id, $fields ); |
| 80 | |
| 81 | $event_title = isset( $selected_options['event_title'] ) ? sanitize_text_field( $selected_options['event_title'] ) : ''; |
| 82 | |
| 83 | if ( empty( $event_title ) ) { |
| 84 | return [ |
| 85 | 'status' => 'error', |
| 86 | 'message' => __( 'Event title is required.', 'suretriggers' ), |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | $start_date_input = isset( $selected_options['event_start_date'] ) ? sanitize_text_field( $selected_options['event_start_date'] ) : ''; |
| 91 | |
| 92 | if ( empty( $start_date_input ) ) { |
| 93 | return [ |
| 94 | 'status' => 'error', |
| 95 | 'message' => __( 'Start date is required.', 'suretriggers' ), |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | $start_time_input = isset( $selected_options['event_start_time'] ) ? sanitize_text_field( $selected_options['event_start_time'] ) : ''; |
| 100 | $end_date_input = isset( $selected_options['event_end_date'] ) ? sanitize_text_field( $selected_options['event_end_date'] ) : ''; |
| 101 | $end_time_input = isset( $selected_options['event_end_time'] ) ? sanitize_text_field( $selected_options['event_end_time'] ) : ''; |
| 102 | |
| 103 | $start_ts = strtotime( trim( $start_date_input . ' ' . $start_time_input ) ); |
| 104 | if ( false === $start_ts ) { |
| 105 | return [ |
| 106 | 'status' => 'error', |
| 107 | 'message' => __( 'Invalid start date or time format.', 'suretriggers' ), |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | $end_ts = $start_ts; |
| 112 | if ( ! empty( $end_date_input ) ) { |
| 113 | $end_candidate = strtotime( trim( $end_date_input . ' ' . $end_time_input ) ); |
| 114 | if ( false === $end_candidate ) { |
| 115 | return [ |
| 116 | 'status' => 'error', |
| 117 | 'message' => __( 'Invalid end date or time format.', 'suretriggers' ), |
| 118 | ]; |
| 119 | } |
| 120 | $end_ts = $end_candidate; |
| 121 | } elseif ( ! empty( $end_time_input ) ) { |
| 122 | // Same day, but with an explicit end time. |
| 123 | $end_candidate = strtotime( trim( $start_date_input . ' ' . $end_time_input ) ); |
| 124 | if ( false !== $end_candidate ) { |
| 125 | $end_ts = $end_candidate; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Determine post status. |
| 130 | $post_status = 'publish'; |
| 131 | $allowed_statuses = [ 'publish', 'draft', 'pending', 'private' ]; |
| 132 | if ( ! empty( $selected_options['event_status'] ) ) { |
| 133 | $status = sanitize_text_field( $selected_options['event_status'] ); |
| 134 | if ( in_array( $status, $allowed_statuses, true ) ) { |
| 135 | $post_status = $status; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Create the event post. |
| 140 | $event_args = [ |
| 141 | 'post_title' => $event_title, |
| 142 | 'post_type' => 'ajde_events', |
| 143 | 'post_status' => $post_status, |
| 144 | 'post_content' => '', |
| 145 | ]; |
| 146 | |
| 147 | if ( ! empty( $selected_options['event_description'] ) ) { |
| 148 | $event_args['post_content'] = wp_kses_post( $selected_options['event_description'] ); |
| 149 | } |
| 150 | |
| 151 | $event_id = wp_insert_post( $event_args, true ); |
| 152 | |
| 153 | if ( is_wp_error( $event_id ) ) { |
| 154 | return [ |
| 155 | 'status' => 'error', |
| 156 | 'message' => $event_id->get_error_message(), |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | // Core EventOn meta — unix start/end (used everywhere by EventOn). |
| 161 | update_post_meta( $event_id, 'evcal_srow', (string) $start_ts ); |
| 162 | update_post_meta( $event_id, 'evcal_erow', (string) $end_ts ); |
| 163 | update_post_meta( $event_id, '_unix_start_ev', (string) $start_ts ); |
| 164 | update_post_meta( $event_id, '_unix_end_ev', (string) $end_ts ); |
| 165 | |
| 166 | // All day. |
| 167 | $all_day_value = 'no'; |
| 168 | if ( ! empty( $selected_options['event_all_day'] ) && 'yes' === strtolower( sanitize_text_field( $selected_options['event_all_day'] ) ) ) { |
| 169 | $all_day_value = 'yes'; |
| 170 | } |
| 171 | update_post_meta( $event_id, 'evcal_allday', $all_day_value ); |
| 172 | |
| 173 | // Featured flag (EventOn stores 'yes'/'no'). |
| 174 | $featured_value = 'no'; |
| 175 | if ( ! empty( $selected_options['event_featured'] ) && 'yes' === strtolower( sanitize_text_field( $selected_options['event_featured'] ) ) ) { |
| 176 | $featured_value = 'yes'; |
| 177 | } |
| 178 | update_post_meta( $event_id, '_featured', $featured_value ); |
| 179 | |
| 180 | // Language default. |
| 181 | update_post_meta( $event_id, '_evo_lang', 'L1' ); |
| 182 | |
| 183 | // Optional subtitle. |
| 184 | if ( ! empty( $selected_options['event_subtitle'] ) ) { |
| 185 | update_post_meta( $event_id, 'evcal_subtitle', sanitize_text_field( $selected_options['event_subtitle'] ) ); |
| 186 | } |
| 187 | |
| 188 | // Optional event color. |
| 189 | if ( ! empty( $selected_options['event_color'] ) ) { |
| 190 | $color = sanitize_hex_color( $selected_options['event_color'] ); |
| 191 | if ( is_string( $color ) && '' !== $color ) { |
| 192 | update_post_meta( $event_id, 'evcal_event_color', $color ); |
| 193 | update_post_meta( $event_id, 'evcal_event_color_n', 1 ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Optional external link. |
| 198 | if ( ! empty( $selected_options['event_exlink'] ) ) { |
| 199 | update_post_meta( $event_id, 'evcal_exlink', esc_url_raw( $selected_options['event_exlink'] ) ); |
| 200 | } |
| 201 | |
| 202 | // Optional timezone. |
| 203 | if ( ! empty( $selected_options['event_timezone'] ) ) { |
| 204 | update_post_meta( $event_id, 'evo_event_timezone', sanitize_text_field( $selected_options['event_timezone'] ) ); |
| 205 | } |
| 206 | |
| 207 | // Location taxonomy (accepts term ID or name). |
| 208 | if ( ! empty( $selected_options['event_location'] ) && taxonomy_exists( 'event_location' ) ) { |
| 209 | $this->assign_term( $event_id, 'event_location', $selected_options['event_location'] ); |
| 210 | } |
| 211 | |
| 212 | // Organizer taxonomy (accepts term ID or name). |
| 213 | if ( ! empty( $selected_options['event_organizer'] ) && taxonomy_exists( 'event_organizer' ) ) { |
| 214 | $this->assign_term( $event_id, 'event_organizer', $selected_options['event_organizer'] ); |
| 215 | } |
| 216 | |
| 217 | // Event type taxonomy (accepts term ID or name). |
| 218 | if ( ! empty( $selected_options['event_type'] ) && taxonomy_exists( 'event_type' ) ) { |
| 219 | $this->assign_term( $event_id, 'event_type', $selected_options['event_type'] ); |
| 220 | } |
| 221 | |
| 222 | // Featured image. |
| 223 | if ( ! empty( $selected_options['event_featured_image'] ) ) { |
| 224 | $image_url = esc_url_raw( $selected_options['event_featured_image'] ); |
| 225 | if ( ! function_exists( 'media_sideload_image' ) ) { |
| 226 | require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 227 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 228 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 229 | } |
| 230 | $attachment_id = media_sideload_image( $image_url, $event_id, $event_title, 'id' ); |
| 231 | if ( ! is_wp_error( $attachment_id ) && is_int( $attachment_id ) ) { |
| 232 | set_post_thumbnail( $event_id, $attachment_id ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return EventOn::get_event_context( $event_id ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Assign a term to an event, creating it if passed a name that does not exist yet. |
| 241 | * |
| 242 | * @param int $event_id Event post ID. |
| 243 | * @param string $taxonomy Taxonomy slug. |
| 244 | * @param mixed $value Term ID (numeric) or name. |
| 245 | * @return void |
| 246 | */ |
| 247 | private function assign_term( $event_id, $taxonomy, $value ) { |
| 248 | $term_id = 0; |
| 249 | |
| 250 | if ( is_numeric( $value ) ) { |
| 251 | $candidate = (int) $value; |
| 252 | if ( $candidate > 0 && term_exists( $candidate, $taxonomy ) ) { |
| 253 | $term_id = $candidate; |
| 254 | } |
| 255 | } else { |
| 256 | if ( ! is_string( $value ) ) { |
| 257 | return; |
| 258 | } |
| 259 | $name = sanitize_text_field( $value ); |
| 260 | if ( '' === $name ) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | $existing = term_exists( $name, $taxonomy ); |
| 265 | if ( is_array( $existing ) && isset( $existing['term_id'] ) ) { |
| 266 | $term_id = (int) $existing['term_id']; |
| 267 | } else { |
| 268 | $created = wp_insert_term( $name, $taxonomy ); |
| 269 | if ( ! is_wp_error( $created ) && isset( $created['term_id'] ) ) { |
| 270 | $term_id = (int) $created['term_id']; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if ( $term_id > 0 ) { |
| 276 | wp_set_object_terms( $event_id, [ $term_id ], $taxonomy, false ); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | CreateEvent::get_instance(); |
| 282 |