Skip to content

Commit

Permalink
better person-by-wpid search
Browse files Browse the repository at this point in the history
  • Loading branch information
liedekef committed Dec 22, 2023
1 parent 0819d58 commit 1da0701
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 28 deletions.
4 changes: 2 additions & 2 deletions eme-discounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ function eme_calc_booking_discount( $discount, $booking ) {
return false;
}
$current_userid = get_current_user_id();
$person = eme_get_person_by_wp_id( $current_userid, 0 );
$person = eme_get_person_by_wp_id( $current_userid );
if ( empty( $person ) ) {
return false;
}
Expand Down Expand Up @@ -1475,7 +1475,7 @@ function eme_calc_member_discount( $discount, $member ) {
return false;
}
$current_userid = get_current_user_id();
$person = eme_get_person_by_wp_id( $current_userid, 0 );
$person = eme_get_person_by_wp_id( $current_userid );
if ( empty( $person ) ) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions eme-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -3395,7 +3395,7 @@ function eme_replace_event_placeholders( $format, $event, $target = 'html', $lan
$contact = eme_get_event_contact( $event );
}
if ( ! empty( $contact ) && is_null( $contact_person ) ) {
$contact_person = eme_get_person_by_wp_id( $contact->ID, 0 );
$contact_person = eme_get_person_by_wp_id( $contact->ID );
}
$t_contact = $contact;
$t_person = $contact_person;
Expand All @@ -3404,7 +3404,7 @@ function eme_replace_event_placeholders( $format, $event, $target = 'html', $lan
$author = eme_get_author( $event );
}
if ( ! empty( $author ) && is_null( $author_person ) ) {
$author_person = eme_get_person_by_wp_id( $author->ID, 0 );
$author_person = eme_get_person_by_wp_id( $author->ID );
}
$t_contact = $author;
$t_person = $author_person;
Expand Down
28 changes: 23 additions & 5 deletions eme-formfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,10 @@ function eme_replace_task_signupformfields_placeholders( $format ) {
$gdpr = 0;
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$person = eme_get_person_by_wp_id( $current_user->ID, 1 );
$person = eme_get_person_by_wp_id( $current_user->ID );
if ( empty( $person ) ) {
$person = eme_fake_person_by_wp_id( $current_user->ID );
}
$bookerLastName = eme_esc_html( $person['lastname'] );
$bookerFirstName = eme_esc_html( $person['firstname'] );
$bookerBirthdate = eme_is_date( $person['birthdate'] ) ? eme_esc_html( $person['birthdate'] ) : '';
Expand Down Expand Up @@ -1441,7 +1444,10 @@ function eme_replace_cancelformfields_placeholders( $event ) {
$bookerCancelComment = '';
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$person = eme_get_person_by_wp_id( $current_user->ID, 1 );
$person = eme_get_person_by_wp_id( $current_user->ID );
if ( empty( $person ) ) {
$person = eme_fake_person_by_wp_id( $current_user->ID );
}
$bookerLastName = eme_esc_html($person['lastname']);
$bookerFirstName = eme_esc_html($person['firstname']);
$bookerEmail = eme_esc_html($person['email']);
Expand Down Expand Up @@ -1757,7 +1763,10 @@ function eme_replace_extra_multibooking_formfields_placeholders( $format, $event
$allow_clear = 0;
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$person = eme_get_person_by_wp_id( $current_user->ID, 1 );
$person = eme_get_person_by_wp_id( $current_user->ID );
if ( empty( $person ) ) {
$person = eme_fake_person_by_wp_id( $current_user->ID );
}
$bookerLastName = eme_esc_html( $person['lastname'] );
$bookerFirstName = eme_esc_html( $person['firstname'] );
$bookerBirthdate = eme_is_date( $person['birthdate'] ) ? eme_esc_html( $person['birthdate'] ) : '';
Expand Down Expand Up @@ -2443,7 +2452,10 @@ function eme_replace_rsvp_formfields_placeholders( $event, $booking, $format = '

// don't fill out the basic info if in the backend, but do it only if in the frontend
if ( is_user_logged_in() && ! $eme_is_admin_request ) {
$person = eme_get_person_by_wp_id( $current_user->ID, 1 );
$person = eme_get_person_by_wp_id( $current_user->ID );
if ( empty( $person ) ) {
$person = eme_fake_person_by_wp_id( $current_user->ID );
}
$bookerLastName = eme_esc_html( $person['lastname'] );
$bookerFirstName = eme_esc_html( $person['firstname'] );
$bookerBirthdate = eme_is_date( $person['birthdate'] ) ? eme_esc_html( $person['birthdate'] ) : '';
Expand Down Expand Up @@ -3710,7 +3722,10 @@ function eme_replace_membership_formfields_placeholders( $membership, $member, $
$person = eme_get_person( $member['person_id'] );
} elseif (! empty( $current_userid ) && !$eme_is_admin_request) {
// this will also fill person with wp info if logged in and person doesn't exist in EME
$person = eme_get_person_by_wp_id( $current_userid, 1 );
$person = eme_get_person_by_wp_id( $current_userid );
if ( empty( $person ) ) {
$person = eme_fake_person_by_wp_id( $current_userid );
}
}
if ( ! empty( $person ) ) {
$bookerLastName = eme_esc_html( $person['lastname'] );
Expand Down Expand Up @@ -4301,6 +4316,9 @@ function eme_replace_subscribeform_placeholders( $format, $unsubscribe = 0 ) {
$readonly = "readonly='readonly'";
$current_user = wp_get_current_user();
$person = eme_get_person_by_wp_id( $current_user->ID );
if ( empty( $person ) ) {
$person = eme_fake_person_by_wp_id( $current_user->ID );
}
$bookerLastName = $person['lastname'];
$bookerFirstName = $person['firstname'];
$bookerEmail = $person['email'];
Expand Down
2 changes: 1 addition & 1 deletion eme-gdpr.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function eme_rpi_shortcode( $atts ) {
// for logged in users that are linked to an EME user, immediately show the info
if ( $show_info_if_logged_in && is_user_logged_in() ) {
$current_userid = get_current_user_id();
$person = eme_get_person_by_wp_id( $current_userid, 0 );
$person = eme_get_person_by_wp_id( $current_userid );
if ( ! empty( $person ) ) {
return eme_show_personal_info( $person['email'] );
}
Expand Down
5 changes: 4 additions & 1 deletion eme-members.php
Original file line number Diff line number Diff line change
Expand Up @@ -5307,7 +5307,10 @@ function eme_replace_membership_placeholders( $format, $membership, $target = 'h
}
$t_format = str_replace( '#_CONTACT', '#_', $t_format );
$t_format = str_replace( '#_AUTHOR', '#_', $t_format );
$person = eme_get_person_by_wp_id( $contact->ID, 1 );
$person = eme_get_person_by_wp_id( $contact->ID );
if (empty($person)) {
$person = eme_fake_person_by_wp_id( $contact->ID );
}
// to be consistent: #_CONTACTNAME returns the full name if not linked to an EME user, so we do that here too
if ( $t_format == '#_NAME' ) {
$t_format = '#_FULLNAME';
Expand Down
39 changes: 24 additions & 15 deletions eme-people.php
Original file line number Diff line number Diff line change
Expand Up @@ -2716,11 +2716,11 @@ function eme_count_persons_with_wp_id( $wp_id ) {
return $wpdb->get_var( $sql );
}

function eme_get_person_by_wp_id( $wp_id, $use_wp_info = 1 ) {
function eme_get_person_by_wp_id( $wp_id ) {
global $wpdb;
$people_table = EME_DB_PREFIX . EME_PEOPLE_TBNAME;
$user_info = get_userdata( $wp_id );
if (!$user_info && !$use_wp_info) {
if (!$user_info ) {
return false;
}
$lastname = $user_info->user_lastname;
Expand All @@ -2730,7 +2730,7 @@ function eme_get_person_by_wp_id( $wp_id, $use_wp_info = 1 ) {
$lastname = $user_info->display_name;
}

$person = wp_cache_get( "eme_person_wpid $use_wp_info $wp_id" );
$person = wp_cache_get( "eme_person_wpid $wp_id" );
if ( $person === false ) {
$sql = $wpdb->prepare( "SELECT * FROM $people_table WHERE wp_id = %d AND status=%d", $wp_id, EME_PEOPLE_STATUS_ACTIVE);
$lines = $wpdb->get_results( $sql, ARRAY_A );
Expand Down Expand Up @@ -2763,22 +2763,31 @@ function eme_get_person_by_wp_id( $wp_id, $use_wp_info = 1 ) {
}
if ( ! empty( $person ) ) {
$res = eme_update_person_wp_id( $person['person_id'], $wp_id );
wp_cache_delete( "eme_person_wpid $use_wp_info $wp_id" );
wp_cache_delete( "eme_person_wpid $wp_id" );
if ( $res !== false ) {
$person['wp_id'] = $wp_id;
}
} else {
if ( $use_wp_info ) {
$person = eme_new_person();
$person['lastname'] = $lastname;
$person['firstname'] = $firstname;
$person['email'] = $email;
$person['phone'] = eme_get_user_phone( $wp_id );
$person['wp_id'] = $wp_id;
}
}
}
wp_cache_set( "eme_person_wpid $use_wp_info $wp_id", $person, '', 10 );
wp_cache_set( "eme_person_wpid $wp_id", $person, '', 10 );
return $person;
}

function eme_fake_person_by_wp_id( $wp_id ) {
global $wpdb;
$user_info = get_userdata( $wp_id );
$person = eme_new_person();
if ($user_info ) {
$lastname = $user_info->user_lastname;
if ( empty( $lastname ) ) {
$lastname = $user_info->display_name;
}
$person['lastname'] = $lastname;
$person['firstname'] = $user_info->user_firstname;
$person['email'] = $user_info->user_email;
$person['phone'] = eme_get_user_phone( $wp_id );
$person['wp_id'] = $wp_id;
}
return $person;
}

Expand Down Expand Up @@ -4210,7 +4219,7 @@ function eme_after_profile_update( $wp_id, $old_user_data ) {
$firstname = $user_info->user_firstname;
$email = $user_info->user_email;
$phone = eme_get_user_phone( $wp_id );
$person = eme_get_person_by_wp_id( $wp_id, 0 );
$person = eme_get_person_by_wp_id( $wp_id );
if ( ! empty( $person ) ) {
if ( ! empty( $lastname ) ) {
$person['lastname'] = $lastname;
Expand Down
2 changes: 1 addition & 1 deletion eme-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function eme_add_multibooking_form( $events, $template_id_header = 0, $template_

// check group memberships
if ( ! empty( $event['event_properties']['rsvp_required_group_ids'] ) ) {
$person = eme_get_person_by_wp_id( $current_userid, 0 );
$person = eme_get_person_by_wp_id( $current_userid );
if ( empty( $person ) ) {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion eme-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ function eme_mytasks_signups_shortcode( $atts ) {
} else {
return;
}
$person = eme_get_person_by_wp_id( $wp_id, 0 );
$person = eme_get_person_by_wp_id( $wp_id );
if ( empty( $person ) ) {
return;
}
Expand Down

0 comments on commit 1da0701

Please sign in to comment.