| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for Facebook Imports. |
| 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_Facebook { |
| 15 |
|
| 16 |
/* |
| 17 |
* Facebook app ID |
| 18 |
*/ |
| 19 |
public $fb_app_id; |
| 20 |
|
| 21 |
/* |
| 22 |
* Facebook app Secret |
| 23 |
*/ |
| 24 |
public $fb_app_secret; |
| 25 |
|
| 26 |
/* |
| 27 |
* Facebook Graph URL |
| 28 |
*/ |
| 29 |
public $fb_graph_url; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize the class and set its properties. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
global $ife_events; |
| 38 |
|
| 39 |
$options = ife_get_import_options( 'facebook' ); |
| 40 |
$this->fb_app_id = isset( $options['facebook_app_id'] ) ? $options['facebook_app_id'] : ''; |
| 41 |
$this->fb_app_secret = isset( $options['facebook_app_secret'] ) ? $options['facebook_app_secret'] : ''; |
| 42 |
$this->fb_graph_url = 'https://graph.facebook.com/v2.5/'; |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* import facebook events by oraganization or facebook page. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @param array $eventdata import event data. |
| 51 |
* @return array/boolean |
| 52 |
*/ |
| 53 |
public function import_events( $event_data = array() ){ |
| 54 |
|
| 55 |
global $ife_errors; |
| 56 |
$imported_events = array(); |
| 57 |
$facebook_event_ids = array(); |
| 58 |
|
| 59 |
if( $this->fb_app_id == '' || $this->fb_app_secret == '' ){ |
| 60 |
$ife_errors[] = __( 'Please insert Facebook app ID and app Secret.', 'import-facebook-events'); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$import_by = isset( $event_data['import_by'] ) ? esc_attr( $event_data['import_by'] ) : ''; |
| 65 |
|
| 66 |
$facebook_event_ids = isset( $event_data['event_ids'] ) ? $event_data['event_ids'] : array(); |
| 67 |
|
| 68 |
if( !empty( $facebook_event_ids ) ){ |
| 69 |
foreach ($facebook_event_ids as $facebook_event_id ) { |
| 70 |
if( $facebook_event_id != '' ){ |
| 71 |
$imported_events[] = $this->import_event_by_event_id( $facebook_event_id, $event_data ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
return $imported_events; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* import facebook event by ID. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
* @param array $eventdata import event data. |
| 83 |
* @return int/boolean |
| 84 |
*/ |
| 85 |
public function import_event_by_event_id( $facebook_event_id, $event_data = array() ){ |
| 86 |
|
| 87 |
global $ife_errors, $ife_events; |
| 88 |
$options = ife_get_import_options( 'facebook' ); |
| 89 |
$update_events = isset( $options['update_events'] ) ? $options['update_events'] : 'no'; |
| 90 |
|
| 91 |
if( $facebook_event_id == '' || $this->fb_app_id == '' || $this->fb_app_secret == '' ){ |
| 92 |
if( $this->fb_app_id == '' || $this->fb_app_secret == '' ){ |
| 93 |
$ife_errors[] = __( 'Please insert Facebook app ID and app Secret.', 'import-facebook-events'); |
| 94 |
return; |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
/*$is_exitsing_event = $this->get_event_by_event_id( $facebook_event_id ); |
| 100 |
if ( $is_exitsing_event && $update_events == 'no' ) { |
| 101 |
$ife_errors[] = __( 'Facebook event is already exists.', 'import-facebook-events'); |
| 102 |
return; |
| 103 |
}*/ |
| 104 |
|
| 105 |
$facebook_event_object = $this->get_facebook_event_by_event_id( $facebook_event_id ); |
| 106 |
|
| 107 |
return $this->save_facebook_event( $facebook_event_object, $event_data ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Save (Create or update) facebook imported to The Event Calendar Events. |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* @param array $facebook_event_object Event object get from facebook.com. |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function save_facebook_event( $facebook_event_object = array(), $event_args = array() ) { |
| 119 |
|
| 120 |
global $ife_events; |
| 121 |
$import_result = ''; |
| 122 |
|
| 123 |
if ( ! empty( $facebook_event_object ) && isset( $facebook_event_object->id ) ) { |
| 124 |
$centralize_array = $this->generate_centralize_array( $facebook_event_object ); |
| 125 |
return $ife_events->common->import_events_into( $centralize_array, $event_args ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* get access token |
| 131 |
* |
| 132 |
* @since 1.0.0 |
| 133 |
*/ |
| 134 |
public function get_access_token(){ |
| 135 |
$args = array( |
| 136 |
'grant_type' => 'client_credentials', |
| 137 |
'client_id' => $this->fb_app_id, |
| 138 |
'client_secret' => $this->fb_app_secret |
| 139 |
); |
| 140 |
$access_token_url = add_query_arg( $args, $this->fb_graph_url . 'oauth/access_token' ); |
| 141 |
$access_token_response = wp_remote_get( $access_token_url ); |
| 142 |
|
| 143 |
$access_token_response_body = wp_remote_retrieve_body( $access_token_response ); |
| 144 |
$access_token_data = json_decode( $access_token_response_body ); |
| 145 |
|
| 146 |
$access_token = ! empty( $access_token_data->access_token ) ? $access_token_data->access_token : null; |
| 147 |
|
| 148 |
return $access_token; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Generate Facebook api URL for grab Event. |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
*/ |
| 156 |
public function generate_facebook_api_url( $path = '', $query_args = array() ) { |
| 157 |
$query_args = array_merge( $query_args, array( 'access_token' => $this->get_access_token() ) ); |
| 158 |
|
| 159 |
$url = add_query_arg( $query_args, $this->fb_graph_url . $path ); |
| 160 |
|
| 161 |
return $url; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* get a facebook object. |
| 166 |
* |
| 167 |
* @since 1.0.0 |
| 168 |
*/ |
| 169 |
public function get_facebook_response_data( $event_id, $args = array() ) { |
| 170 |
$url = $this->generate_facebook_api_url( $event_id, $args ); |
| 171 |
$event_data = $this->get_json_response_from_url( $url ); |
| 172 |
return $event_data; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* get a facebook event object |
| 177 |
* |
| 178 |
* @since 1.0.0 |
| 179 |
*/ |
| 180 |
public function get_facebook_event_by_event_id( $event_id ) { |
| 181 |
return $this->get_facebook_response_data( |
| 182 |
$event_id, |
| 183 |
array( |
| 184 |
'fields' => implode( |
| 185 |
',', |
| 186 |
array( |
| 187 |
'id', |
| 188 |
'name', |
| 189 |
'description', |
| 190 |
'start_time', |
| 191 |
'end_time', |
| 192 |
'updated_time', |
| 193 |
'cover', |
| 194 |
'ticket_uri', |
| 195 |
'timezone', |
| 196 |
'owner', |
| 197 |
'place', |
| 198 |
) |
| 199 |
), |
| 200 |
) |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get body data from url and return decoded data. |
| 206 |
* |
| 207 |
* @since 1.0.0 |
| 208 |
*/ |
| 209 |
public function get_json_response_from_url( $url ) { |
| 210 |
|
| 211 |
$response = wp_remote_get( $url ); |
| 212 |
$response = json_decode( wp_remote_retrieve_body( $response ) ); |
| 213 |
return $response; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* get all events for facebook page or organizer |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
* @return array the events |
| 221 |
*/ |
| 222 |
public function get_events_for_facebook_page( $facebook_page_id ) { |
| 223 |
|
| 224 |
$args = array( |
| 225 |
'limit' => 9999, |
| 226 |
'since' => date( 'Y-m-d' ), |
| 227 |
'fields' => 'id' |
| 228 |
); |
| 229 |
|
| 230 |
$url = $this->generate_facebook_api_url( $facebook_page_id . '/events', $args ); |
| 231 |
|
| 232 |
$response = $this->get_json_response_from_url( $url ); |
| 233 |
$response_data = !empty( $response->data ) ? (array) $response->data : array(); |
| 234 |
|
| 235 |
if ( empty( $response_data ) || empty( $response_data[0] ) ) { |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
$event_ids = array(); |
| 240 |
foreach ( $response_data as $event ) { |
| 241 |
$event_ids[] = $event->id; |
| 242 |
} |
| 243 |
return array_reverse( $event_ids ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Format events arguments as per TEC |
| 248 |
* |
| 249 |
* @since 1.0.0 |
| 250 |
* @param array $facebook_event Facebook event. |
| 251 |
* @return array |
| 252 |
*/ |
| 253 |
public function generate_centralize_array( $facebook_event ) { |
| 254 |
global $ife_events; |
| 255 |
|
| 256 |
if( !isset( $facebook_event->id ) || $facebook_event->id == '' ){ |
| 257 |
return; |
| 258 |
} |
| 259 |
$start_time = $start_time_utc = time(); |
| 260 |
$end_time = $end_time_utc = time(); |
| 261 |
$utc_offset = ''; |
| 262 |
|
| 263 |
$facebook_id = $facebook_event->id; |
| 264 |
$post_title = isset( $facebook_event->name ) ? $facebook_event->name : ''; |
| 265 |
$post_description = isset( $facebook_event->description ) ? $facebook_event->description : ''; |
| 266 |
|
| 267 |
$start_time = isset( $facebook_event->start_time ) ? strtotime( $ife_events->common->convert_datetime_to_db_datetime( $facebook_event->start_time ) ) : date( 'Y-m-d H:i:s'); |
| 268 |
$end_time = isset( $facebook_event->end_time ) ? strtotime( $ife_events->common->convert_datetime_to_db_datetime( $facebook_event->end_time ) ) : $start_time; |
| 269 |
|
| 270 |
$ticket_uri = isset( $facebook_event->ticket_uri ) ? esc_url( $facebook_event->ticket_uri ) : ''; |
| 271 |
$timezone = $this->get_utc_offset( $facebook_event->start_time ); |
| 272 |
$cover_image = isset( $facebook_event->cover->source ) ? $ife_events->common->clean_url( esc_url( $facebook_event->cover->source ) ) : ''; |
| 273 |
|
| 274 |
$xt_event = array( |
| 275 |
'origin' => 'facebook', |
| 276 |
'ID' => $facebook_id, |
| 277 |
'name' => $post_title, |
| 278 |
'description' => $post_description, |
| 279 |
'starttime_local' => $start_time, |
| 280 |
'endtime_local' => $end_time, |
| 281 |
'startime_utc' => '', |
| 282 |
'endtime_utc' => '', |
| 283 |
'timezone' => $timezone, |
| 284 |
'utc_offset' => '', |
| 285 |
'event_duration' => '', |
| 286 |
'is_all_day' => '', |
| 287 |
'url' => $ticket_uri, |
| 288 |
'image_url' => $cover_image, |
| 289 |
); |
| 290 |
|
| 291 |
if ( isset( $facebook_event->owner ) ) { |
| 292 |
$xt_event['organizer'] = $this->get_organizer( $facebook_event ); |
| 293 |
} |
| 294 |
|
| 295 |
if ( isset( $facebook_event->place ) ) { |
| 296 |
$xt_event['location'] = $this->get_location( $facebook_event ); |
| 297 |
} |
| 298 |
|
| 299 |
return $xt_event; |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Get organizer args for event. |
| 305 |
* |
| 306 |
* @since 1.0.0 |
| 307 |
* @param array $eventbrite_event Eventbrite event. |
| 308 |
* @return array |
| 309 |
*/ |
| 310 |
public function get_organizer( $facebook_event ) { |
| 311 |
|
| 312 |
if ( !isset( $facebook_event->owner->id ) ) { |
| 313 |
return null; |
| 314 |
} |
| 315 |
|
| 316 |
$organizer_raw_data = $this->get_facebook_response_data( |
| 317 |
$facebook_event->owner->id, |
| 318 |
array( |
| 319 |
'fields' => implode( |
| 320 |
',', |
| 321 |
array( |
| 322 |
'id', |
| 323 |
'name', |
| 324 |
'link', |
| 325 |
'phone' |
| 326 |
) |
| 327 |
), |
| 328 |
) |
| 329 |
); |
| 330 |
|
| 331 |
if ( !isset( $organizer_raw_data->id ) ) { |
| 332 |
return null; |
| 333 |
} |
| 334 |
|
| 335 |
$event_organizer = array( |
| 336 |
'ID' => isset( $organizer_raw_data->id ) ? $organizer_raw_data->id : '', |
| 337 |
'name' => isset( $organizer_raw_data->name ) ? $organizer_raw_data->name : '', |
| 338 |
'description' => '', |
| 339 |
'email' => '', |
| 340 |
'phone' => isset( $organizer_raw_data->phone ) ? $organizer_raw_data->phone : '', |
| 341 |
'url' => isset( $organizer_raw_data->link ) ? $organizer_raw_data->link : '', |
| 342 |
'image_url' => '', |
| 343 |
/*'image_url' => $image_url,*/ |
| 344 |
); |
| 345 |
return $event_organizer; |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Get location args for event |
| 351 |
* |
| 352 |
* @since 1.0.0 |
| 353 |
* @param array $eventbrite_event Eventbrite event. |
| 354 |
* @return array |
| 355 |
*/ |
| 356 |
public function get_location( $facebook_event ) { |
| 357 |
|
| 358 |
if ( !isset( $facebook_event->place->id ) ) { |
| 359 |
return null; |
| 360 |
} |
| 361 |
$event_venue = $facebook_event->place; |
| 362 |
$event_location = array( |
| 363 |
'ID' => isset( $facebook_event->place->id ) ? $facebook_event->place->id : '', |
| 364 |
'name' => isset( $event_venue->name ) ? $event_venue->name : '', |
| 365 |
'description' => '', |
| 366 |
'address_1' => isset( $event_venue->location->street ) ? $event_venue->location->street : '', |
| 367 |
'address_2' => '', |
| 368 |
'city' => isset( $event_venue->location->city ) ? $event_venue->location->city : '', |
| 369 |
'state' => isset( $event_venue->location->state ) ? $event_venue->location->state : '', |
| 370 |
'country' => isset( $event_venue->location->country ) ? $event_venue->location->country : '', |
| 371 |
'zip' => isset( $event_venue->location->zip ) ? $event_venue->location->zip : '', |
| 372 |
'lat' => isset( $event_venue->location->latitude ) ? $event_venue->location->latitude : '', |
| 373 |
'long' => isset( $event_venue->location->longitude ) ? $event_venue->location->longitude : '', |
| 374 |
'full_address' => isset( $event_venue->location->street ) ? $event_venue->location->street : '', |
| 375 |
'url' => '', |
| 376 |
'image_url' => '' |
| 377 |
); |
| 378 |
return $event_location; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Get organizer Name based on Organiser ID. |
| 383 |
* |
| 384 |
* @since 1.0.0 |
| 385 |
* @param array $organizer_id Organizer event. |
| 386 |
* @return array |
| 387 |
*/ |
| 388 |
public function get_organizer_name_by_id( $organizer_id ) { |
| 389 |
|
| 390 |
if( !$organizer_id || $organizer_id == '' ){ |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
$organizer_raw_data = $this->get_facebook_response_data( $organizer_id, array() ); |
| 395 |
if( ! isset( $organizer_raw_data->name ) ){ |
| 396 |
return ''; |
| 397 |
} |
| 398 |
|
| 399 |
$oraganizer_name = isset( $organizer_raw_data->name ) ? $organizer_raw_data->name : ''; |
| 400 |
return $oraganizer_name; |
| 401 |
|
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Get UTC offset |
| 406 |
* |
| 407 |
* @since 1.0.0 |
| 408 |
*/ |
| 409 |
public function get_utc_offset( $datetime ) { |
| 410 |
try { |
| 411 |
$datetime = new DateTime( $datetime ); |
| 412 |
} catch ( Exception $e ) { |
| 413 |
return ''; |
| 414 |
} |
| 415 |
|
| 416 |
$timezone = $datetime->getTimezone(); |
| 417 |
$offset = $timezone->getOffset( $datetime ) / 60 / 60; |
| 418 |
|
| 419 |
if ( $offset >= 0 ) { |
| 420 |
$offset = '+' . $offset; |
| 421 |
} |
| 422 |
|
| 423 |
return 'UTC' . $offset; |
| 424 |
} |
| 425 |
} |
| 426 |
|