PluginProbe
Import Social Events / 1.1.0
Import Social Events v1.1.0
1.9.1 1.9.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 All 61 releases
import-facebook-events / includes / class-import-facebook-events-aioec.php

class-import-facebook-events-aioec.php in Import Social Events 1.1.0, at includes/class-import-facebook-events-aioec.php

273 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class for Import Events into All in One Event Calendar
4 *
5 * @link http://xylusthemes.com/
6 * @since 1.0.0
7 *
8 * @package Import_Facebook_Events
9 * @subpackage Import_Facebook_Events/includes
10 */
11 // Exit if accessed directly
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14 class Import_Facebook_Events_Aioec {
15
16 // The Events Calendar Event Taxonomy
17 protected $taxonomy;
18
19 // The Events Calendar Event Posttype
20 protected $event_posttype;
21
22 // The Events Calendar Event Custom Table
23 protected $event_db_table;
24
25 // The Events Calendar Event Instance custom table
26 protected $event_instances_table;
27
28 /**
29 * Initialize the class and set its properties.
30 *
31 * @since 1.0.0
32 */
33 public function __construct() {
34
35 global $wpdb;
36 $this->event_posttype = 'ai1ec_event';
37 $this->taxonomy = 'events_categories';
38 $this->event_db_table = "{$wpdb->prefix}ai1ec_events";
39 $this->event_instances_table = "{$wpdb->prefix}ai1ec_event_instances";
40
41 }
42
43
44 /**
45 * Get Posttype and Taxonomy Functions
46 *
47 * @return string
48 */
49 public function get_event_posttype(){
50 return $this->event_posttype;
51 }
52 public function get_taxonomy(){
53 return $this->taxonomy;
54 }
55
56 /**
57 * import event into TEC
58 *
59 * @since 1.0.0
60 * @param array $centralize event array.
61 * @return array
62 */
63 public function import_event( $centralize_array, $event_args ){
64 global $wpdb, $ife_events;
65
66 if( empty( $centralize_array ) || !isset( $centralize_array['ID'] ) ){
67 return false;
68 }
69
70 $is_exitsing_event = $ife_events->common->get_event_by_event_id( $this->event_posttype, $centralize_array['ID'] );
71
72 if ( $is_exitsing_event ) {
73 // Update event or not?
74 $options = ife_get_import_options( $centralize_array['origin'] );
75 $update_events = isset( $options['update_events'] ) ? $options['update_events'] : 'no';
76 if ( 'yes' != $update_events ) {
77 return array( 'status'=> 'skipped' );
78 }
79 }
80
81 $origin_event_id = $centralize_array['ID'];
82 $post_title = isset( $centralize_array['name'] ) ? $centralize_array['name'] : '';
83 $post_description = isset( $centralize_array['description'] ) ? $centralize_array['description'] : '';
84 $start_time = $centralize_array['starttime_local'];
85 $end_time = $centralize_array['endtime_local'];
86 $event_uri = $centralize_array['url'];
87
88 $eo_eventdata = array(
89 'post_title' => $post_title,
90 'post_content' => $post_description,
91 'post_type' => $this->event_posttype,
92 'post_status' => 'pending',
93 );
94 if ( $is_exitsing_event ) {
95 $eo_eventdata['ID'] = $is_exitsing_event;
96 }
97
98 if( isset( $event_args['event_status'] ) && $event_args['event_status'] != '' ){
99 $eo_eventdata['post_status'] = $event_args['event_status'];
100 }
101
102 $inserted_event_id = wp_insert_post( $eo_eventdata, true );
103
104 if ( ! is_wp_error( $inserted_event_id ) ) {
105 $inserted_event = get_post( $inserted_event_id );
106 if ( empty( $inserted_event ) ) { return '';}
107
108 // Asign event category.
109 $ife_cats = isset( $event_args['event_cats'] ) ? $event_args['event_cats'] : array();
110 if ( ! empty( $ife_cats ) ) {
111 foreach ( $ife_cats as $ife_catk => $ife_catv ) {
112 $ife_cats[ $ife_catk ] = (int) $ife_catv;
113 }
114 }
115 if ( ! empty( $ife_cats ) ) {
116 wp_set_object_terms( $inserted_event_id, $ife_cats, $this->taxonomy );
117 }
118
119 // Assign Featured images
120 $event_image = $centralize_array['image_url'];
121 if( $event_image != '' ){
122 $ife_events->common->setup_featured_image_to_event( $inserted_event_id, $event_image );
123 }else{
124 if( $is_exitsing_event ){
125 delete_post_thumbnail( $inserted_event_id );
126 }
127 }
128
129 // Save Meta.
130 update_post_meta( $inserted_event_id, 'ife_facebook_event_id', $centralize_array['ID'] );
131 update_post_meta( $inserted_event_id, 'ife_event_link', esc_url( $event_uri ) );
132 update_post_meta( $inserted_event_id, 'ife_event_origin', $event_args['import_origin'] );
133
134 // Custom table Details
135 $event_array = array(
136 'post_id' => $inserted_event_id,
137 'start' => $start_time,
138 'end' => $end_time,
139 );
140
141 $event_count = $wpdb->get_var( "SELECT COUNT(*) FROM $this->event_instances_table WHERE `post_id` = ".absint( $inserted_event_id ) );
142 if( $event_count > 0 && is_numeric( $event_count ) ){
143 $where = array( 'post_id' => absint( $inserted_event_id ) );
144 $wpdb->update( $this->event_instances_table , $event_array, $where );
145 }else{
146 $wpdb->insert( $this->event_instances_table , $event_array );
147 }
148
149 $venue = isset( $centralize_array['location'] ) ? $centralize_array['location'] : '';
150 $location_name = isset( $venue['name'] ) ? $venue['name'] : '';
151 $address = isset( $venue['full_address'] ) ? $venue['full_address'] : $venue['address_1'];
152 $city = isset( $venue['city'] ) ? $venue['city'] : '';
153 $state = isset( $venue['state'] ) ? $venue['state'] : '';
154 $zip = isset( $venue['zip'] ) ? $venue['zip'] : '';
155 $lat = isset( $venue['lat'] ) ? $venue['lat'] : '';
156 $lon = isset( $venue['long'] ) ? $venue['long'] : '';
157 $country = isset( $venue['country'] ) ? $venue['country'] : '';
158 $show_map = $show_coordinates = 0;
159 if( $lat != '' && $lon != '' ){
160 $show_map = $show_coordinates = 1;
161 }
162
163 $organizer = isset( $centralize_array['organizer'] ) ? $centralize_array['organizer'] : '';
164 $org_name = isset( $organizer['name'] ) ? $organizer['name'] : '';
165 $org_phone = isset( $organizer['phone'] ) ? $organizer['phone'] : '';
166 $org_email = isset( $organizer['email'] ) ? $organizer['email'] : '';
167 $org_url = isset( $organizer['url'] ) ? $organizer['url'] : '';
168
169 $event_table_array = array(
170 'post_id' => $inserted_event_id,
171 'start' => $start_time,
172 'end' => $end_time,
173 'timezone_name' => '',
174 'allday' => 0,
175 'instant_event' => 0,
176 'venue' => $location_name,
177 'country' => $country,
178 'address' => $address,
179 'city' => $city,
180 'province' => $state,
181 'postal_code' => $zip,
182 'show_map' => $show_map,
183 'contact_name' => $org_name,
184 'contact_phone' => $org_phone,
185 'contact_email' => $org_email,
186 'contact_url' => $event_uri,
187 'cost' => '',
188 'ticket_url' => '',
189 'ical_uid' => $this->get_ical_uid_for_event( $inserted_event_id ),
190 'show_coordinates' => $show_coordinates,
191 );
192 if( $lat != '' ){
193 $event_table_array['latitude'] = $lat;
194 }
195 if( $lon != '' ){
196 $event_table_array['longitude'] = $lon;
197 }
198
199
200 $event_format = array(
201 '%d', // post_id
202 '%d', // start
203 '%d', // end
204 '%s', // timezone_name
205 '%d', // allday
206 '%d', // instant_event
207 '%s', // venue
208 '%s', // country
209 '%s', // address
210 '%s', // city
211 '%s', // province
212 '%s', // postal_code
213 '%d', // show_map
214 '%s', // contact_name
215 '%s', // contact_phone
216 '%s', // contact_email
217 '%s', // contact_url
218 '%s', // cost
219 '%s', // ticket_url
220 '%s', // ical_uid
221 '%d', // show_coordinates
222 );
223 if( $lat != '' ){
224 $event_format[] = '%f'; // latitude
225 }
226 if( $lon != '' ){
227 $event_format[] = '%f'; // longitude
228 }
229
230 $event_exist_count = $wpdb->get_var( "SELECT COUNT(*) FROM $this->event_db_table WHERE `post_id` = ".absint( $inserted_event_id ) );
231 if( $event_exist_count > 0 && is_numeric( $event_exist_count ) ){
232 $where = array( 'post_id' => absint( $inserted_event_id ) );
233 $wpdb->update( $this->event_db_table, $event_table_array, $where, $event_format );
234 }else{
235 $wpdb->insert( $this->event_db_table, $event_table_array, $event_format );
236 }
237
238 if ( $is_exitsing_event ) {
239 do_action( 'ife_after_update_event_organizer_'.$centralize_array["origin"].'_event', $inserted_event_id, $centralize_array );
240 return array(
241 'status' => 'updated',
242 'id' => $inserted_event_id
243 );
244 }else{
245 do_action( 'ife_after_create_event_organizer_'.$centralize_array["origin"].'_event', $inserted_event_id, $centralize_array );
246 return array(
247 'status' => 'created',
248 'id' => $inserted_event_id
249 );
250 }
251
252 }else{
253 return array( 'status'=> 0, 'message'=> 'Something went wrong, please try again.' );
254 }
255 }
256
257 /**
258 * Get Uid for ai1ec event.
259 *
260 * @since 1.0.0
261 * @param int $event_id event id.
262 * @return str
263 */
264 public function get_ical_uid_for_event( $event_id ){
265 $site_url = parse_url( ai1ec_get_site_url() );
266 $format = 'ai1ec-%d@' . $site_url['host'];
267 if ( isset( $site_url['path'] ) ) {
268 $format .= $site_url['path'];
269 }
270 return sprintf( $format, $event_id );
271 }
272 }
273