Skip to content

Instantly share code, notes, and snippets.

@billerickson
Forked from amberhinds/candidate-import.php
Created October 4, 2018 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/6f75e0bc687bb033c523c6b25ab813b4 to your computer and use it in GitHub Desktop.
Save billerickson/6f75e0bc687bb033c523c6b25ab813b4 to your computer and use it in GitHub Desktop.
This is an example of a file that creates new posts in a custom post type from the Eventbrite Attendee API
<?php
function candidate_import($continuation, $event_id, $event_name, $token){
$log;
$log .= 'Event: '.$event_name.'<br />';
//check if API call should be paginated and if so get the continuation key
if(empty($continuation)){
//first page call
$request = wp_remote_get( 'https://www.eventbriteapi.com/v3/events/'.$event_id.'/attendees/?token='.$token );
$log .= 'Initial API Call: https://www.eventbriteapi.com/v3/events/'.$event_id.'/attendees/?token='.$token.'<br />';
} else {
//paginated call
$request = wp_remote_get( 'https://www.eventbriteapi.com/v3/events/'.$event_id.'/attendees/?token='.$token.'&continuation='.$continuation );
$log .= 'Continuation API Call: https://www.eventbriteapi.com/v3/events/'.$event_id.'/attendees/?token='.$token.'&continuation='.$continuation.'<br />';
}
//retrieve data
$body = wp_remote_retrieve_body( $request );
//conver data to jSON
$data = json_decode( $body );
//check if data
if( ! empty( $data ) ) {
if($data->pagination->has_more_items){
$log .= 'Pagination Has More Items <br />';
}
$log .= 'Pagination '.$data->pagination->page_number.' of '.$data->pagination->page_count.'<br />';
//loop though attendees
foreach( $data->attendees as $attendee ) {
$log .= '-----------------------< CANDIDATE IMPORT >-----------------------<br />';
//check if post already exist
$args = array(
'post_type' => 'candidate',
'posts_per_page' => 1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'email',
'value' => array( $attendee->profile->email ),
'compare' => 'IN',
),
),
);
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();
$log .= '<span style="color: blue;">Candidate Exist: '.$attendee->profile->email.'<br /></span>';
$log .= ' - Stored Date Changed: '.get_field('date_changed').'<br />';
$log .= ' - Eventbrite Date Changed: '.$attendee->changed.'<br />';
//get existing event terms
$existing_event_terms = wp_get_post_terms(get_the_ID(), 'candidate_event', array('fields' => 'names'));
//post id
$post_id = get_the_ID();
//if post exist compare date changed.
if(get_field('date_changed') < $attendee->changed){
// update the post into the database
$post_action = 'update';
}else{
$post_action = '';
}
endwhile; else:
// insert the post into the database
$post_action = 'insert';
endif; wp_reset_query();
//check if new terms exist
if(!empty($existing_event_terms)){
//there are existing terms
//check if term exist in new terms array
if (!in_array($event_name, $existing_event_terms)) {
//if term doesn exist push term into new term array
array_push($existing_event_terms, $event_name);
$candidate_event_terms = $existing_event_terms;
}else{
//exist in existing terms
$candidate_event_terms = $existing_event_terms;
}
} else {
//there are NOT existing terms so set current event term
$candidate_event_terms = $event_name;
}
$existing_event_terms = null;
//var_dump($attendee->answers);
//var_dump($attendee->answers[0]->question);
//reset question vars
$question_contact = null;
$question_identification = null;
$question_skills = null;
$question_experience = null;
$question_facebook = null;
$question_twitter = null;
$question_linkedin = null;
$question_other_identifications = null;
$question_other_skills = null;
$question_other_pronouns = null;
$question_other_experience = null;
$questions = $attendee->answers;
$questions_count = count($questions);
for ($i=0; $i < $questions_count; $i++) {
if($questions[$i]->question == 'Are you looking for a job?' && !empty($questions[$i]->answer)){
$question_looking = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'I am open to being contacted by companies.' && !empty($questions[$i]->answer)){
$question_contact = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'I identify as (check all that apply)' && !empty($questions[$i]->answer)){
$question_identification = explode('|', $questions[$i]->answer);
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
//var_dump($question_identification);
//echo '<br />';
}
if($questions[$i]->question == 'Skills you have at any level (choose all that apply)' && !empty($questions[$i]->answer)){
$question_skills = explode('|', $questions[$i]->answer);
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
//var_dump($question_skills);
//echo '<br />';
}
if($questions[$i]->question == 'Years of experience' && !empty($questions[$i]->answer)){
$question_experience = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'Facebook Link' && !empty($questions[$i]->answer)){
$question_facebook = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'Twitter Handle' && !empty($questions[$i]->answer)){
$question_twitter = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'LinkedIn Account Link' && !empty($questions[$i]->answer)){
$question_linkedin = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'Please list other identifications' && !empty($questions[$i]->answer)){
$question_other_identifications = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'Please list other skills' && !empty($questions[$i]->answer)){
$question_other_skills = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'Please list other pronouns' && !empty($questions[$i]->answer)){
$question_other_pronouns = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
if($questions[$i]->question == 'Please list other experience' && !empty($questions[$i]->answer)){
$question_other_experience = $questions[$i]->answer;
$log .= $questions[$i]->question.'<br />';
$log .= $questions[$i]->answer.'<br />';
}
}
//Has candidate given permission to share data
if($question_contact == 'Yes'):
// create post object
$my_post = array(
'post_type' => 'candidate',
'post_title' => $attendee->profile->name,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
//'tax_input' => $custom_tax,
'meta_input' => array(
'first_name' => $attendee->profile->first_name,
'last_name' => $attendee->profile->last_name,
'home_street_address' => $attendee->profile->addresses->home->address_1,
'home_address_line_2' => $attendee->profile->addresses->home->address_2,
'home_city' => $attendee->profile->addresses->home->city,
'home_state' => $attendee->profile->addresses->home->region,
'home_zip' => $attendee->profile->addresses->home->postal_code,
'home_country' => $attendee->profile->addresses->home->country,
'job_title' => $attendee->profile->job_title,
'company' => $attendee->profile->company,
'email' => $attendee->profile->email,
'phone' => $attendee->profile->cell_phone,
'linkedin_url' => $question_linkedin,
'twitter_profile' => $question_twitter,
'my_dream_job_is' => '',
'online_profile_or_bio_url' => $question_facebook,
'eventbrite_attendee_id' => $attendee->id,
'date_changed' => $attendee->changed,
'other_identifications' => $question_other_identifications,
'other_skills' => $question_other_skills,
'other_pronouns' => $question_other_pronouns,
'other_experience' => $question_other_experience,
),
);
// Insert or update post and store results
if($post_action == 'insert'){
$result = wp_insert_post( $my_post );
$log .= '<span style="color: green;">Candidate Added: '.$attendee->profile->email.'<br /></span>';
if ( function_exists("SimpleLogger") ) {
SimpleLogger()->debug("Candidate Added: ".$attendee->profile->email);
}
}elseif($post_action == 'update'){
$result = wp_update_post( $my_post );
$log .= '<span style="color: orange;">Candidate Updated: '.$attendee->profile->email.'<br /></span>';
if ( function_exists("SimpleLogger") ) {
SimpleLogger()->debug("Candidate Updated: ".$attendee->profile->email);
}
} else {
$log .= '<span style="color: red;">Candidate Not Updated or Added: '.$attendee->profile->email.'<br /></span>';
}
// Insert or update taxonomy terms
if($post_action == 'insert' || $post_action == 'update'){
if ( $result && ! is_wp_error( $result ) ) {
$post_id = $result;
wp_set_object_terms($post_id, $question_skills, 'candidate_skill');
wp_set_object_terms($post_id, $question_identification, 'candidate_identification');
//wp_set_object_terms($post_id, $question_city, 'candidate_location');
wp_set_object_terms($post_id, $question_experience, 'candidate_experience');
$log .= '<span style="color: green;">Set Terms: '.$attendee->profile->email.'<br /></span>';
}
}
//Update Candidate Event term everytime so multiple event terms can be attached to a candidate
wp_set_object_terms($post_id, $candidate_event_terms, 'candidate_event');
else:
$log .= '<span style="color: red;">Candidate Import Skipped: Candidate did not give permission to share information<br /></span>';
endif;
$log .= '-----------------------< /CANDIDATE IMPORT >-----------------------<br />';
}//end foreach
//check if has more items
if($data->pagination->has_more_items == true){
//sleep for 10 seconds
sleep(10);
//pass continuation back to the function
candidate_import($data->pagination->continuation, $event_id, $event_name, $token);
} else {
/*$lwt_sp_log_post = array(
'post_type' => 'lwt_sp_logs',
'post_title' => date("Y-m-d H:i:s"),
'post_content' => $lwt_sp_logs,
'post_status' => 'publish',
'post_author' => 1,
);
$result = wp_insert_post( $lwt_sp_log_post );*/
}
//echo $log;
}
}
/*
Eventbrite Get Attendees
*/
//function eventbrite_get_attendees($lwt_token, $tjt_token){
function eventbrite_get_attendees(){
$lwt_token = get_field('lwt_eventbrite_api_token','options');
$tjt_token = get_field('tjt_eventbrite_api_token','options');
//get events from custom settings
$lwt_events = get_option('lwt_options');
//check if events
if( ! empty( $lwt_events ) ) {
//loop events
foreach( $lwt_events as $event_id => $event_checked ) {
$request = wp_remote_get( 'https://www.eventbriteapi.com/v3/events/'.$event_id.'/?token='.$lwt_token );
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
$event_name = $data->name->text;
$event_end_date = $data->end->local;
$event_name = create_event_terms_and_meta($event_id,$event_name,$event_end_date);
//pass event to candidate import func
candidate_import('', $event_id, $event_name, $lwt_token);
}
}
//get events from custom settings
$tjt_events = get_option('tjt_options');
//check if events
if( ! empty( $tjt_events ) ) {
//loop events
foreach( $tjt_events as $event_id => $event_checked ) {
$request = wp_remote_get( 'https://www.eventbriteapi.com/v3/events/'.$event_id.'/?token='.$tjt_token );
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
$event_name = $data->name->text;
$event_end_date = $data->end->local;
$event_name = create_event_terms_and_meta($event_id,$event_name,$event_end_date);
//pass event to candidate import func
candidate_import('', $event_id, $event_name, $tjt_token);
}
}
}
add_action('eventbrite_get_attendees_hook','eventbrite_get_attendees');
if(!is_admin()){
//do_action( 'eventbrite_get_attendees_hook');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment