From 1e671c9ea7ce0d4dc0db33496d556012b8e888b7 Mon Sep 17 00:00:00 2001 From: Rajat Patel Date: Sat, 23 Nov 2024 14:23:29 +0530 Subject: [PATCH 1/2] import event into tec without orm --- .../class-import-facebook-events-common.php | 17 ++ ...ass-import-facebook-events-ical_parser.php | 22 +- includes/class-import-facebook-events-tec.php | 285 +++++++++--------- 3 files changed, 183 insertions(+), 141 deletions(-) diff --git a/includes/class-import-facebook-events-common.php b/includes/class-import-facebook-events-common.php index 7f9a2f9..cc1d96a 100644 --- a/includes/class-import-facebook-events-common.php +++ b/includes/class-import-facebook-events-common.php @@ -651,6 +651,23 @@ public function get_utc_offset( $datetime ) { return 'UTC' . $offset; } + + /** + * Converts a given date and time in a specific timezone to a UNIX timestamp in UTC. + * + * @param string $datetime The date and time string + * @param string $timezone The timezone of the given date and time + * @return int UNIX timestamp in UTC. + */ + public function ife_convert_to_utc_timestamp( $datetime, $timezone ) { + try { + $date = new DateTime( $datetime, new DateTimeZone( $timezone ) ); + $date->setTimezone( new DateTimeZone( 'UTC' ) ); + return $date->getTimestamp(); + } catch ( Exception $e ) { + return 0; + } + } /** * Render dropdown for Imported event status. diff --git a/includes/class-import-facebook-events-ical_parser.php b/includes/class-import-facebook-events-ical_parser.php index e1cdbea..3e96471 100644 --- a/includes/class-import-facebook-events-ical_parser.php +++ b/includes/class-import-facebook-events-ical_parser.php @@ -228,6 +228,20 @@ public function generate_centralize_event_array( $event, $event_data = array() $timezone = $system_timezone; }*/ + //get calendar timezone + $sstartime = $start->format( 'Y-m-d H:i:s' ); + $sendtime = $end->format( 'Y-m-d H:i:s' ); + + if( $timezone == 'UTC' ){ + $startime_utc = $sstartime; + $endtime_utc = $sendtime; + }else{ + $utc_start_time = $ife_events->common->ife_convert_to_utc_timestamp( $sstartime, $timezone ); + $startime_utc = date('Y-m-d H:i:s', $utc_start_time ); + $end_time_utc = $ife_events->common->ife_convert_to_utc_timestamp( $sendtime, $timezone ); + $endtime_utc = date('Y-m-d H:i:s', $end_time_utc ); + } + $start_time = strtotime( $this->convert_datetime_to_timezone_wise_datetime( $start, $force_timezone ) ); $end_time = strtotime( $this->convert_datetime_to_timezone_wise_datetime( $end, $force_timezone ) ); /*$start_time = strtotime( $start ); @@ -297,8 +311,8 @@ public function generate_centralize_event_array( $event, $event_data = array() $match = 'https://www.facebook.com/events/'; if ( strpos( $url, $match ) !== false ) { $timezone = $wordpress_timezone; - $cwt_start = $this->convert_fb_ical_timezone( $start->format('Y-m-d H:i:s') ); - $cwt_end = $this->convert_fb_ical_timezone( $end->format('Y-m-d H:i:s') ); + $cwt_start = $this->convert_fb_ical_timezone( $sstartime ); + $cwt_end = $this->convert_fb_ical_timezone( $sendtime ); $timezone_name = $cwt_start['timezone_name']; $start_time = strtotime( $cwt_start['date_format'] ); $timezone_name = $cwt_end['timezone_name']; @@ -318,8 +332,8 @@ public function generate_centralize_event_array( $event, $event_data = array() 'endtime_local' => $end_time, 'starttime' => date('Ymd\THis', $start_time), 'endtime' => date('Ymd\THis', $end_time), - 'startime_utc' => '', - 'endtime_utc' => '', + 'startime_utc' => $startime_utc, + 'endtime_utc' => $endtime_utc, 'timezone' => $timezone, 'timezone_name' => $timezone_name, 'utc_offset' => '', diff --git a/includes/class-import-facebook-events-tec.php b/includes/class-import-facebook-events-tec.php index 49a31bf..23fb500 100644 --- a/includes/class-import-facebook-events-tec.php +++ b/includes/class-import-facebook-events-tec.php @@ -141,6 +141,7 @@ public function import_event( $centralize_array, $event_args ) { global $ife_events; $is_exitsing_event = $ife_events->common->get_event_by_event_id( $this->event_posttype, $centralize_array['ID'] ); + $formated_args = array(); if ( $is_exitsing_event && is_numeric( $is_exitsing_event ) && $is_exitsing_event > 0 ) { @@ -156,24 +157,10 @@ public function import_event( $centralize_array, $event_args ) { } if ( 'yes' === $update_events ) { - if( function_exists( 'tribe_events' ) ){ - $formated_args = $this->format_event_args_for_tec_orm( $centralize_array ); - if ( isset( $event_args['event_status'] ) && ! empty( $event_args['event_status'] ) ) { - $formated_args['status'] = $event_args['event_status']; - } - }else{ - $formated_args = $this->format_event_args_for_tec( $centralize_array ); - if ( isset( $event_args['event_status'] ) && ! empty( $event_args['event_status'] ) ) { - $formated_args['post_status'] = $event_args['event_status']; - } - } + $formated_args['post_status'] = $event_args['event_status']; $formated_args['post_author'] = isset($event_args['event_author']) ? $event_args['event_author'] : get_current_user_id(); if ( ! $ife_events->common->ife_is_updatable( 'status' ) ) { - if( function_exists( 'tribe_events' ) ){ - $formated_args['status'] = get_post_status( $is_exitsing_event ); - } else { - $formated_args['post_status'] = get_post_status( $is_exitsing_event ); - } + $formated_args['post_status'] = get_post_status( $is_exitsing_event ); } return $this->update_event( $is_exitsing_event, $centralize_array, $formated_args, $event_args ); @@ -185,26 +172,13 @@ public function import_event( $centralize_array, $event_args ) { } } else { - if( function_exists( 'tribe_events' ) ){ - $formated_args = $this->format_event_args_for_tec_orm( $centralize_array ); - if ( isset( $event_args['event_status'] ) && ! empty( $event_args['event_status'] ) ) { - $formated_args['status'] = $event_args['event_status']; - } - }else{ - $formated_args = $this->format_event_args_for_tec( $centralize_array ); - if ( isset( $event_args['event_status'] ) && ! empty( $event_args['event_status'] ) ) { - $formated_args['post_status'] = $event_args['event_status']; - } + if ( isset( $event_args['event_status'] ) && ! empty( $event_args['event_status'] ) ) { + $formated_args['post_status'] = $event_args['event_status']; } $formated_args['post_author'] = isset($event_args['event_author']) ? $event_args['event_author'] : get_current_user_id(); - if ( ! $ife_events->common->ife_is_updatable( 'status' ) ) { - if( function_exists( 'tribe_events' ) ){ - $formated_args['status'] = get_post_status( $is_exitsing_event ); - } else { - $formated_args['post_status'] = get_post_status( $is_exitsing_event ); - } + $formated_args['post_status'] = get_post_status( $is_exitsing_event ); } return $this->create_event( $centralize_array, $formated_args, $event_args ); @@ -223,24 +197,37 @@ public function import_event( $centralize_array, $event_args ) { */ public function create_event( $centralize_array = array(), $formated_args = array(), $event_args = array() ) { // Create event using TEC advanced functions. - global $ife_events; - if( function_exists( 'tribe_events' ) ){ - $new_event_id = tribe_events()->set_args( $formated_args )->create()->ID; - }else{ - if( function_exists( 'tribe_create_event' ) ){ - $new_event_id = tribe_create_event( $formated_args ); - } - } + global $ife_events ,$wpdb; + + $event_title = isset( $centralize_array['name'] ) ? $centralize_array['name'] : ''; + $event_content = isset( $centralize_array['description'] ) ? $centralize_array['description'] : ''; + $event_status = $formated_args['post_status']; + $event_author = $formated_args['post_author']; + + $tec_event = array( + 'post_title' => $event_title, + 'post_content' => $event_content, + 'post_status' => $event_status, + 'post_author' => $event_author, + 'post_type' => $this->event_posttype, + ); + + $new_event_id = wp_insert_post( $tec_event, true ); + + if ( $new_event_id ) { - $timezone = isset( $centralize_array['timezone'] ) ? sanitize_text_field( $centralize_array['timezone'] ) : 'UTC'; - $timezone_name = isset( $centralize_array['timezone_name'] ) ? sanitize_text_field( $centralize_array['timezone_name'] ) : 'Africa/Abidjan'; + + //update all metadata + $allmetas = $this->format_event_args_for_tec( $centralize_array ); + if( !empty( $allmetas ) ){ + foreach( $allmetas as $key => $value ){ + if( !empty( $value ) ){ + update_post_meta( $new_event_id, $key, $value ); + } + } + } - update_post_meta( $new_event_id, '_EventTimezone', $timezone_name ); - update_post_meta( $new_event_id, 'ife_facebook_event_id', $centralize_array['ID'] ); update_post_meta( $new_event_id, 'ife_event_origin', $event_args['import_origin'] ); - update_post_meta( $new_event_id, 'ife_event_link', esc_url( $centralize_array['url'] ) ); - update_post_meta( $new_event_id, 'ife_event_timezone', $timezone ); - update_post_meta( $new_event_id, 'ife_event_timezone_name', $timezone_name ); // Asign event category. $ife_cats = isset( $event_args['event_cats'] ) ? $event_args['event_cats'] : array(); @@ -269,6 +256,35 @@ public function create_event( $centralize_array = array(), $formated_args = arra $ife_events->common->setup_featured_image_to_event( $new_event_id, $event_featured_image ); } + //Insert in Custom Table + $esource_id = $centralize_array['ID']; + $start_time = date( 'Y-m-d H:i:s', $centralize_array['starttime_local'] ); + $end_time = date( 'Y-m-d H:i:s', $centralize_array['endtime_local'] ); + + if( $centralize_array['origin'] == 'ical' ){ + $start_date_utc = $allmetas['_EventStartDateUTC']; + $end_date_utc = $allmetas['_EventEndDateUTC']; + }else{ + $start_date_utc = date( 'Y-m-d H:i:s', $allmetas['_EventStartDateUTC'] ); + $end_date_utc = date( 'Y-m-d H:i:s', $allmetas['_EventEndDateUTC'] ); + } + + $timezone = isset( $allmetas['timezone'] ) ? $allmetas['timezone'] : 'UTC'; + $duration = 0; + + $hash = sha1( $new_event_id . $duration . $start_time . $end_time . $start_date_utc . $end_date_utc . $timezone ); + + $totable_name = $wpdb->prefix . 'tec_occurrences'; + $todata = array( 'event_id' => $esource_id, 'post_id' => $new_event_id, 'start_date' => $start_time, 'start_date_utc' => $start_date_utc, 'end_date' => $end_time, 'end_date_utc' => $end_date_utc, 'duration' => $duration, 'hash' => $hash, 'has_recurrence' => 0, 'is_rdate' => 0, ); + $wpdb->insert( $totable_name, $todata ); + + $tetable_name = $wpdb->prefix . 'tec_events'; + $tedata = array( + 'event_id' => $esource_id, 'post_id' => $new_event_id, 'start_date' => $start_time, 'end_date' => $end_time, 'timezone' => $timezone, 'start_date_utc' => $start_date_utc, 'end_date_utc' => $end_date_utc, 'duration' => $duration, 'rset' => null, + ); + $wpdb->insert( $tetable_name, $tedata ); + + do_action( 'ife_after_create_tec_' . $centralize_array['origin'] . '_event', $new_event_id, $formated_args, $centralize_array ); return array( 'status' => 'created', @@ -294,35 +310,37 @@ public function create_event( $centralize_array = array(), $formated_args = arra */ public function update_event( $event_id, $centralize_array, $formated_args = array(), $event_args = array() ) { // Update event using TEC advanced functions. - global $ife_events; - - if( function_exists( 'tribe_events' ) ){ - $update_event_id = tribe_events()->where( 'id', $event_id )->set_args( $formated_args )->save(); - $update_event_id = $event_id; - $tec_event = array( 'ID' => $event_id, 'post_status' => $formated_args['status'] ); - wp_update_post( $tec_event ); - }else{ - if( function_exists( 'tribe_update_event' ) ){ - $update_event_id = tribe_update_event( $event_id, $formated_args ); - } - } + global $ife_events, $wpdb; + + $event_title = isset( $centralize_array['name'] ) ? $centralize_array['name'] : ''; + $event_content = isset( $centralize_array['description'] ) ? $centralize_array['description'] : ''; + $event_status = $formated_args['post_status']; + $event_author = $formated_args['post_author']; + + $tec_event = array( + 'ID' => $event_id, + 'post_title' => $event_title, + 'post_content' => $event_content, + 'post_status' => $event_status, + 'post_author' => $event_author, + 'post_type' => $this->event_posttype, + ); + + $update_event_id = wp_update_post( $tec_event, true ); if ( $update_event_id ) { - $start_time = $centralize_array['starttime_local']; - $end_time = $centralize_array['endtime_local']; - $timezone = isset( $centralize_array['timezone'] ) ? sanitize_text_field( $centralize_array['timezone'] ) : 'UTC'; - $timezone_name = isset( $centralize_array['timezone_name'] ) ? $centralize_array['timezone_name'] : 'Africa/Abidjan'; - - update_post_meta( $update_event_id, '_EventStartDate', date( 'Y-m-d H:i:s', $start_time ) ); - update_post_meta( $update_event_id, '_EventEndDate', date( 'Y-m-d H:i:s', $end_time ) ); - update_post_meta( $update_event_id, '_EventTimezone', $timezone_name ); + //update all metadata + $allmetas = $this->format_event_args_for_tec( $centralize_array ); + if( !empty( $allmetas ) ){ + foreach( $allmetas as $key => $value ){ + if( !empty( $value ) ){ + update_post_meta( $update_event_id, $key, $value ); + } + } + } - update_post_meta( $update_event_id, 'ife_facebook_event_id', $centralize_array['ID'] ); update_post_meta( $update_event_id, 'ife_event_origin', $event_args['import_origin'] ); - update_post_meta( $update_event_id, 'ife_event_link', esc_url( $centralize_array['url'] ) ); - update_post_meta( $update_event_id, 'ife_event_timezone', $timezone ); - update_post_meta( $update_event_id, 'ife_event_timezone_name', $timezone_name ); // Asign event category. $ife_cats = isset( $event_args['event_cats'] ) ? (array) $event_args['event_cats'] : array(); @@ -357,6 +375,36 @@ public function update_event( $event_id, $centralize_array, $formated_args = arr delete_post_thumbnail( $update_event_id ); } + + + //Update in Custom Table + $esource_id = $centralize_array['ID']; + $start_time = date( 'Y-m-d H:i:s', $centralize_array['starttime_local'] ); + $end_time = date( 'Y-m-d H:i:s', $centralize_array['endtime_local'] ); + + + if( $centralize_array['origin'] == 'ical' ){ + $start_date_utc = $allmetas['_EventStartDateUTC']; + $end_date_utc = $allmetas['_EventEndDateUTC']; + }else{ + $start_date_utc = date( 'Y-m-d H:i:s', $allmetas['_EventStartDateUTC'] ); + $end_date_utc = date( 'Y-m-d H:i:s', $allmetas['_EventEndDateUTC'] ); + } + + $timezone = isset( $allmetas['timezone'] ) ? $allmetas['timezone'] : 'UTC'; + + $totable_name = $wpdb->prefix . 'tec_occurrences'; + $todata = array( 'event_id' => $esource_id, 'post_id' => $update_event_id, 'start_date' => $start_time, 'start_date_utc' => $start_date_utc, 'end_date' => $end_time, 'end_date_utc' => $end_date_utc ); + $where = array( 'event_id' => $esource_id, 'post_id' => $update_event_id ); + $wpdb->update( $totable_name, $todata, $where ); + + // Update the wp_tec_events table + $tetable_name = $wpdb->prefix . 'tec_events'; + $tedata = array( 'event_id' => $esource_id, 'post_id' => $update_event_id, 'start_date' => $start_time, 'end_date' => $end_time, 'timezone' => $timezone, 'start_date_utc' => $start_date_utc, 'end_date_utc' => $end_date_utc ); + $where = array( 'event_id' => $esource_id, 'post_id' => $update_event_id ); + $wpdb->update( $tetable_name, $tedata, $where ); + + do_action( 'ife_after_update_tec_' . $centralize_array['origin'] . '_event', $update_event_id, $formated_args, $centralize_array ); return array( 'status' => 'updated', @@ -368,51 +416,6 @@ public function update_event( $event_id, $centralize_array, $formated_args = arr } } - - /** - * Format events arguments as per TEC - * - * @since 1.0.0 - * @param array $centralize_array Facebook event. - * @return array - */ - public function format_event_args_for_tec_orm( $centralize_array ) { - - if ( empty( $centralize_array ) ) { - return; - } - $start_time = $centralize_array['starttime_local']; - $end_time = $centralize_array['endtime_local']; - $timezone_name = isset( $centralize_array['timezone_name'] ) ? $centralize_array['timezone_name'] : 'Africa/Abidjan'; - $event_args = array( - 'title' => $centralize_array['name'], - 'post_content' => $centralize_array['description'], - 'status' => 'pending', - 'url' => $centralize_array['url'], - 'timezone' => $timezone_name, - 'start_date' => date( 'Y-m-d H:i:s', $start_time ), - 'end_date' => date( 'Y-m-d H:i:s', $end_time ), - ); - - if( isset( $centralize_array['is_all_day'] ) && true === $centralize_array['is_all_day'] ){ - $event_args['_EventAllDay'] = 'yes'; - } - - if ( array_key_exists( 'organizer', $centralize_array ) ) { - $organizer = $this->get_organizer_args( $centralize_array['organizer'] ); - $event_args['organizer'] = $organizer['OrganizerID']; - } - - if( isset( $centralize_array['is_online'] ) && $centralize_array['is_online'] == true ){ - $centralize_array['location']['name'] = 'Online Event'; - } - if ( array_key_exists( 'location', $centralize_array ) ) { - $venue = $this->get_venue_args( $centralize_array['location'] ); - $event_args['venue'] = $venue['VenueID']; - } - return $event_args; - } - /** * Format event arguments as per TEC less than V4.9 * @@ -425,26 +428,32 @@ public function format_event_args_for_tec( $centralize_array ) { if ( empty( $centralize_array ) ) { return; } - $start_time = $centralize_array['starttime_local']; - $end_time = $centralize_array['endtime_local']; + $start_time = $centralize_array['starttime_local']; + $end_time = $centralize_array['endtime_local']; + $timezone = isset( $centralize_array['timezone'] ) ? sanitize_text_field( $centralize_array['timezone'] ) : 'UTC'; + $timezone_name = isset( $centralize_array['timezone_name'] ) ? $centralize_array['timezone_name'] : 'Africa/Abidjan'; + $esource_url = isset( $centralize_array['url'] ) ? esc_url( $centralize_array['url'] ) : ''; + $esource_id = $centralize_array['ID']; + $event_args = array( - 'post_type' => $this->event_posttype, - 'post_title' => $centralize_array['name'], - 'post_status' => 'pending', - 'post_content' => $centralize_array['description'], - 'EventStartDate' => date( 'Y-m-d', $start_time ), - 'EventStartHour' => date( 'h', $start_time ), - 'EventStartMinute' => date( 'i', $start_time ), - 'EventStartMeridian' => date( 'a', $start_time ), - 'EventEndDate' => date( 'Y-m-d', $end_time ), - 'EventEndHour' => date( 'h', $end_time ), - 'EventEndMinute' => date( 'i', $end_time ), - 'EventEndMeridian' => date( 'a', $end_time ), - 'EventStartDateUTC' => ! empty( $centralize_array['startime_utc'] ) ? date( 'Y-m-d H:i:s', $centralize_array['startime_utc'] ) : '', - 'EventEndDateUTC' => ! empty( $centralize_array['endtime_utc'] ) ? date( 'Y-m-d H:i:s', $centralize_array['endtime_utc'] ) : '', - 'EventURL' => $centralize_array['url'], - 'EventShowMap' => 1, - 'EventShowMapLink' => 1, + '_EventStartDate' => date( 'Y-m-d', $start_time ), + '_EventStartHour' => date( 'h', $start_time ), + '_EventStartMinute' => date( 'i', $start_time ), + '_EventStartMeridian' => date( 'a', $start_time ), + '_EventEndDate' => date( 'Y-m-d', $end_time ), + '_EventEndHour' => date( 'h', $end_time ), + '_EventEndMinute' => date( 'i', $end_time ), + '_EventEndMeridian' => date( 'a', $end_time ), + '_EventStartDateUTC' => ! empty( $centralize_array['startime_utc'] ) ? $centralize_array['startime_utc'] : '', + '_EventEndDateUTC' => ! empty( $centralize_array['endtime_utc'] ) ? $centralize_array['endtime_utc'] : '', + '_EventURL' => $centralize_array['url'], + '_EventShowMap' => 1, + '_EventShowMapLink' => 1, + 'ife_event_timezone' => $timezone, + '_EventTimezone' => $timezone_name, + 'ife_event_link' => $esource_url, + 'ife_facebook_event_id' => $esource_id, + 'ife_event_timezone_name' => $timezone_name, ); if( isset( $centralize_array['is_all_day'] ) && true === $centralize_array['is_all_day'] ){ @@ -452,11 +461,13 @@ public function format_event_args_for_tec( $centralize_array ) { } if ( array_key_exists( 'organizer', $centralize_array ) ) { - $event_args['organizer'] = $this->get_organizer_args( $centralize_array['organizer'] ); + $get_organizer = $this->get_organizer_args( $centralize_array['organizer'] ); + $event_args['_EventOrganizerID'] = $get_organizer['OrganizerID']; } if ( array_key_exists( 'location', $centralize_array ) ) { - $event_args['venue'] = $this->get_venue_args( $centralize_array['location'] ); + $get_location = $this->get_venue_args( $centralize_array['location'] ); + $event_args['_EventVenueID'] = $get_location['VenueID']; } return $event_args; } From ed88afa1a3504d4f9b2a200ae473db16d991ae60 Mon Sep 17 00:00:00 2001 From: "WordPress .pot File Generator" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 23 Nov 2024 09:02:46 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=84=20Generated=20POT=20File?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- languages/import-facebook-events.pot | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/languages/import-facebook-events.pot b/languages/import-facebook-events.pot index 29c4fe0..6228525 100644 --- a/languages/import-facebook-events.pot +++ b/languages/import-facebook-events.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2024-10-04T11:26:43+00:00\n" +"POT-Creation-Date: 2024-11-23T09:02:45+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.11.0\n" "X-Domain: import-facebook-events\n" @@ -476,35 +476,35 @@ msgstr "" msgid "Scheduled Import" msgstr "" -#: includes/class-import-facebook-events-common.php:666 +#: includes/class-import-facebook-events-common.php:683 msgid "Status" msgstr "" -#: includes/class-import-facebook-events-common.php:671 +#: includes/class-import-facebook-events-common.php:688 msgid "Published" msgstr "" -#: includes/class-import-facebook-events-common.php:674 +#: includes/class-import-facebook-events-common.php:691 msgid "Pending" msgstr "" -#: includes/class-import-facebook-events-common.php:677 +#: includes/class-import-facebook-events-common.php:694 msgid "Draft" msgstr "" -#: includes/class-import-facebook-events-common.php:769 +#: includes/class-import-facebook-events-common.php:786 msgid "Your current \"Import Facebok Event Pro\" add-on is not competible with Free plugin. Please Upgrade Pro latest to work event importing Flawlessly." msgstr "" -#: includes/class-import-facebook-events-common.php:786 +#: includes/class-import-facebook-events-common.php:803 msgid "The Access Token has been invalidated because the user changed their password or Facebook has changed the session for security reasons. Can you please Authorize/Reauthorize your Facebook account from Facebook Import >