| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for Import Events into EventON |
| 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_EventON { |
| 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 Location Taxonomy |
| 23 |
protected $location_taxonomy; |
| 24 |
|
| 25 |
// The Events Calendar Location Taxonomy |
| 26 |
protected $organizer_taxonomy; |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialize the class and set its properties. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
|
| 35 |
$this->taxonomy = 'event_type'; |
| 36 |
$this->event_posttype = 'ajde_events'; |
| 37 |
$this->location_taxonomy = 'event_location'; |
| 38 |
$this->organizer_taxonomy = 'event_organizer'; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get Posttype and Taxonomy Functions |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function get_event_posttype(){ |
| 47 |
return $this->event_posttype; |
| 48 |
} |
| 49 |
public function get_location_taxonomy(){ |
| 50 |
return $this->location_taxonomy; |
| 51 |
} |
| 52 |
public function get_organizer_taxonomy(){ |
| 53 |
return $this->organizer_taxonomy; |
| 54 |
} |
| 55 |
public function get_taxonomy(){ |
| 56 |
return $this->taxonomy; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* import event into TEC |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* @param array $centralize event array. |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public function import_event( $centralize_array, $event_args ){ |
| 67 |
global $wpdb, $ife_events; |
| 68 |
|
| 69 |
if( empty( $centralize_array ) || !isset( $centralize_array['ID'] ) ){ |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
$is_exitsing_event = $ife_events->common->get_event_by_event_id( $this->event_posttype, $centralize_array['ID'] ); |
| 74 |
|
| 75 |
if ( $is_exitsing_event ) { |
| 76 |
// Update event or not? |
| 77 |
$options = ife_get_import_options( $centralize_array['origin'] ); |
| 78 |
$update_events = isset( $options['update_events'] ) ? $options['update_events'] : 'no'; |
| 79 |
if ( 'yes' != $update_events ) { |
| 80 |
return array( 'status'=> 'skipped' ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$origin_event_id = $centralize_array['ID']; |
| 85 |
$post_title = isset( $centralize_array['name'] ) ? convert_chars(stripslashes($centralize_array['name'])) : ''; |
| 86 |
$post_description = isset( $centralize_array['description'] ) ? wpautop(convert_chars(stripslashes($centralize_array['description']))) : ''; |
| 87 |
$start_time = $centralize_array['starttime_local']; |
| 88 |
$end_time = $centralize_array['endtime_local']; |
| 89 |
$ticket_uri = $centralize_array['url']; |
| 90 |
|
| 91 |
$evon_eventdata = array( |
| 92 |
'post_title' => $post_title, |
| 93 |
'post_content' => $post_description, |
| 94 |
'post_type' => $this->event_posttype, |
| 95 |
'post_status' => 'pending', |
| 96 |
); |
| 97 |
if ( $is_exitsing_event ) { |
| 98 |
$evon_eventdata['ID'] = $is_exitsing_event; |
| 99 |
} |
| 100 |
if( isset( $event_args['event_status'] ) && $event_args['event_status'] != '' ){ |
| 101 |
$evon_eventdata['post_status'] = $event_args['event_status']; |
| 102 |
} |
| 103 |
/*echo "<pre>"; |
| 104 |
print_r( $centralize_array ); |
| 105 |
print_r( $evon_eventdata ); |
| 106 |
exit(); |
| 107 |
*/ |
| 108 |
$inserted_event_id = wp_insert_post( $evon_eventdata, true ); |
| 109 |
|
| 110 |
if ( ! is_wp_error( $inserted_event_id ) ) { |
| 111 |
$inserted_event = get_post( $inserted_event_id ); |
| 112 |
if ( empty( $inserted_event ) ) { return '';} |
| 113 |
|
| 114 |
// Asign event category. |
| 115 |
$ife_cats = isset( $event_args['event_cats'] ) ? $event_args['event_cats'] : array(); |
| 116 |
if ( ! empty( $ife_cats ) ) { |
| 117 |
foreach ( $ife_cats as $ife_catk => $ife_catv ) { |
| 118 |
$ife_cats[ $ife_catk ] = (int) $ife_catv; |
| 119 |
} |
| 120 |
} |
| 121 |
if ( ! empty( $ife_cats ) ) { |
| 122 |
wp_set_object_terms( $inserted_event_id, $ife_cats, $this->taxonomy ); |
| 123 |
} |
| 124 |
|
| 125 |
// Assign Featured images |
| 126 |
$event_image = $centralize_array['image_url']; |
| 127 |
if( $event_image != '' ){ |
| 128 |
$ife_events->common->setup_featured_image_to_event( $inserted_event_id, $event_image ); |
| 129 |
} |
| 130 |
$address = $centralize_array['location']['address_1']; |
| 131 |
if( $centralize_array['location']['full_address'] != '' ){ |
| 132 |
$address = $centralize_array['location']['full_address']; |
| 133 |
} |
| 134 |
|
| 135 |
update_post_meta( $inserted_event_id, 'ife_facebook_event_id', $centralize_array['ID'] ); |
| 136 |
update_post_meta( $inserted_event_id, 'ife_event_origin', $event_args['import_origin'] ); |
| 137 |
update_post_meta( $inserted_event_id, 'ife_event_link', $centralize_array['url'] ); |
| 138 |
update_post_meta( $inserted_event_id, 'evcal_srow', $start_time ); |
| 139 |
update_post_meta( $inserted_event_id, 'evcal_erow', $end_time ); |
| 140 |
update_post_meta( $inserted_event_id, 'evcal_lmlink', $centralize_array['url'] ); |
| 141 |
|
| 142 |
if( $centralize_array['location']['name'] != '' ){ |
| 143 |
$loc_term = term_exists( $centralize_array['location']['name'], $this->location_taxonomy ); |
| 144 |
if ($loc_term !== 0 && $loc_term !== null) { |
| 145 |
if( is_array( $loc_term ) ){ |
| 146 |
$loc_term_id = (int)$loc_term['term_id']; |
| 147 |
} |
| 148 |
}else{ |
| 149 |
$new_loc_term = wp_insert_term( |
| 150 |
$centralize_array['location']['name'], |
| 151 |
$this->location_taxonomy |
| 152 |
); |
| 153 |
if( !is_wp_error( $new_loc_term ) ){ |
| 154 |
$loc_term_id = (int)$new_loc_term['term_id']; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// latitude and longitude |
| 159 |
$loc_term_meta = array(); |
| 160 |
$loc_term_meta['location_lon'] = (!empty($centralize_array['location']['long']))? $centralize_array['location']['long']: null; |
| 161 |
$loc_term_meta['location_lat'] = (!empty($centralize_array['location']['lat']))? $centralize_array['location']['lat']: null; |
| 162 |
$loc_term_meta['evcal_location_link'] = (isset($centralize_array['location']['url']))?$centralize_array['location']['url']:null; |
| 163 |
$loc_term_meta['location_address'] = $address; |
| 164 |
$loc_term_meta['evo_loc_img'] = (isset($centralize_array['location']['image_url']))?$centralize_array['location']['image_url']:null; |
| 165 |
update_option("taxonomy_".$loc_term_id, $loc_term_meta); |
| 166 |
|
| 167 |
$term_loc_ids = wp_set_object_terms( $inserted_event_id, $loc_term_id, $this->location_taxonomy ); |
| 168 |
update_post_meta( $inserted_event_id, 'evo_location_tax_id', $loc_term_id ); |
| 169 |
update_post_meta( $inserted_event_id, 'evcal_location_name', $centralize_array['location']['name'] ); |
| 170 |
update_post_meta( $inserted_event_id, 'evcal_location_link', $centralize_array['location']['url'] ); |
| 171 |
update_post_meta( $inserted_event_id, 'evcal_location', $address ); |
| 172 |
update_post_meta( $inserted_event_id, 'evcal_lat', $centralize_array['location']['lat'] ); |
| 173 |
update_post_meta( $inserted_event_id, 'evcal_lon', $centralize_array['location']['long'] ); |
| 174 |
if( $centralize_array['location']['long'] != '' && $centralize_array['location']['lat'] != '' ){ |
| 175 |
update_post_meta( $inserted_event_id, 'evcal_gmap_gen', 'yes' ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if( $centralize_array['organizer']['name'] != '' ){ |
| 180 |
|
| 181 |
$org_contact = $centralize_array['organizer']['phone']; |
| 182 |
if( $centralize_array['organizer']['email'] != '' ){ |
| 183 |
$org_contact = $centralize_array['organizer']['email']; |
| 184 |
} |
| 185 |
$org_term = term_exists( $centralize_array['organizer']['name'], $this->organizer_taxonomy ); |
| 186 |
if ($org_term !== 0 && $org_term !== null) { |
| 187 |
if( is_array( $org_term ) ){ |
| 188 |
$org_term_id = (int)$org_term['term_id']; |
| 189 |
} |
| 190 |
}else{ |
| 191 |
$new_org_term = wp_insert_term( |
| 192 |
$centralize_array['organizer']['name'], |
| 193 |
$this->organizer_taxonomy |
| 194 |
); |
| 195 |
if( !is_wp_error( $new_org_term ) ){ |
| 196 |
$org_term_id = (int)$new_org_term['term_id']; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
$org_term_meta = array(); |
| 202 |
$org_term_meta['evcal_org_contact'] = $org_contact; |
| 203 |
$org_term_meta['evcal_org_address'] = null; |
| 204 |
$org_term_meta['evo_org_img'] = (isset($centralize_array['organizer']['image_url']))?$centralize_array['organizer']['image_url']:null; |
| 205 |
$org_term_meta['evcal_org_exlink'] = (isset($centralize_array['organizer']['url']))?$centralize_array['organizer']['url']:null; |
| 206 |
|
| 207 |
update_option("taxonomy_".$org_term_id, $org_term_meta); |
| 208 |
|
| 209 |
$term_org_ids = wp_set_object_terms( $inserted_event_id, $org_term_id, $this->organizer_taxonomy ); |
| 210 |
update_post_meta( $inserted_event_id, 'evo_organizer_tax_id', $org_term_id ); |
| 211 |
update_post_meta( $inserted_event_id, 'evcal_organizer', $centralize_array['organizer']['name'] ); |
| 212 |
update_post_meta( $inserted_event_id, 'evcal_org_contact', $org_contact ); |
| 213 |
update_post_meta( $inserted_event_id, 'evcal_org_exlink', $centralize_array['organizer']['url'] ); |
| 214 |
update_post_meta( $inserted_event_id, 'evo_evcrd_field_org', 'no' ); |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
if ( $is_exitsing_event ) { |
| 219 |
do_action( 'ife_after_update_event_on_'.$centralize_array["origin"].'_event', $inserted_event_id, $centralize_array ); |
| 220 |
return array( |
| 221 |
'status' => 'updated', |
| 222 |
'id' => $inserted_event_id |
| 223 |
); |
| 224 |
}else{ |
| 225 |
do_action( 'ife_after_create_event_on_'.$centralize_array["origin"].'_event', $inserted_event_id, $centralize_array ); |
| 226 |
return array( |
| 227 |
'status' => 'created', |
| 228 |
'id' => $inserted_event_id |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
}else{ |
| 233 |
return array( 'status'=> 0, 'message'=> 'Something went wrong, please try again.' ); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|