Skip to content

Commit

Permalink
Fix multiple occurences of state and country input fields on one page
Browse files Browse the repository at this point in the history
  • Loading branch information
liedekef committed Jan 22, 2025
1 parent 0b94fd3 commit ad07a7b
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 30 deletions.
76 changes: 62 additions & 14 deletions eme-formfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ function eme_replace_eventtaskformfields_placeholders( $format, $task, $event )
return $format;
}

function eme_replace_task_signupformfields_placeholders( $format ) {
function eme_replace_task_signupformfields_placeholders( $form_id, $format ) {
$eme_is_admin_request = eme_is_admin_request();

if ( is_user_logged_in() ) {
Expand Down Expand Up @@ -1290,20 +1290,30 @@ function eme_replace_task_signupformfields_placeholders( $format ) {
$replacement = "<input $required_att type='text' name='$fieldname' id='$fieldname' value='$bookerZip' placeholder='$placeholder_text' $readonly >";
} elseif ( preg_match( '/#_STATE$/', $result ) ) {
$fieldname = 'task_state_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerState_code ) ) {
$state_arr = [ $bookerState_code => eme_get_state_name( $bookerState_code, $bookerCountry_code ) ];
} else {
$state_arr = [];
}
$replacement = eme_ui_select( $bookerState_code, 'state_code', $state_arr, '', $required, "eme_select2_state_class" );
$replacement = eme_form_select( $bookerState_code, $fieldname, $fieldid, $state_arr, '', $required, "eme_select2_state_class" );
} elseif ( preg_match( '/#_COUNTRY$/', $result ) ) {
$fieldname = 'task_country_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerCountry_code ) ) {
$country_arr = [ $bookerCountry_code => eme_get_country_name( $bookerCountry_code ) ];
} else {
$country_arr = [];
}
$replacement = eme_ui_select( $bookerCountry_code, 'country_code', $country_arr, '', $required, "eme_select2_country_class" );
$replacement = eme_form_select( $bookerCountry_code, $fieldname, $fieldid, $country_arr, '', $required, "eme_select2_country_class" );
} elseif ( preg_match( '/#_BIRTHDAY_EMAIL$/', $result ) ) {
$replacement = eme_ui_select_binary( $bd_email, 'task_bd_email' );
} elseif ( preg_match( '/#_OPT_OUT$/', $result ) ) {
Expand Down Expand Up @@ -1683,7 +1693,7 @@ function eme_replace_cancel_payment_placeholders( $format, $person, $booking_ids

// the event param in eme_replace_extra_multibooking_formfields_placeholders
// is only there for generic replacements, like e.g. currency
function eme_replace_extra_multibooking_formfields_placeholders( $format, $event ) {
function eme_replace_extra_multibooking_formfields_placeholders( $form_id, $format, $event ) {
$bookerLastName = '';
$bookerFirstName = '';
$bookerBirthdate = '';
Expand Down Expand Up @@ -1866,12 +1876,18 @@ function eme_replace_extra_multibooking_formfields_placeholders( $format, $event
}
$replacement = "<input $required_att type='text' name='city' id='city' value='$bookerCity' placeholder='$placeholder_text'>";
} elseif ( preg_match( '/#_STATE$/', $result ) ) {
$fieldname = 'state_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerState_code ) ) {
$state_arr = [ $bookerState_code => eme_get_state_name( $bookerState_code, $bookerCountry_code ) ];
} else {
$state_arr = [];
}
$replacement = eme_ui_select( $bookerState_code, 'state_code', $state_arr, '', $required, 'eme_select2_state_class' );
$replacement = eme_form_select( $bookerState_code, $fieldname, $fieldid, $state_arr, '', $required, 'eme_select2_state_class' );
} elseif ( preg_match( '/#_(ZIP|POSTAL)(\{.+?\})?$/', $result, $matches ) ) {
if ( isset( $matches[2] ) ) {
// remove { and } (first and last char of second match)
Expand All @@ -1882,24 +1898,36 @@ function eme_replace_extra_multibooking_formfields_placeholders( $format, $event
}
$replacement = "<input $required_att type='text' name='zip' id='zip' value='$bookerZip' placeholder='$placeholder_text'>";
} elseif ( preg_match( '/#_COUNTRY\{(.+)\}$/', $result, $matches ) ) {
$fieldname = 'country_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerCountry_code ) ) {
$country_arr = [ $bookerCountry_code => eme_get_country_name( $bookerCountry_code ) ];
$replacement = eme_ui_select( $bookerCountry_code, 'country_code', $country_arr, '', $required, 'eme_select2_country_class' );
$replacement = eme_ui_select( $bookerCountry_code, $fieldname, $fieldid, $country_arr, '', $required, 'eme_select2_country_class' );
} else {
$country_code = $matches[1];
$country_name = eme_get_country_name( $country_code );
if ( ! empty( $country_name ) ) {
$country_arr = [ $country_code => $country_name ];
$replacement = eme_ui_select( $country_code, 'country_code', $country_arr, '', $required, 'eme_select2_country_class' );
$replacement = eme_ui_select( $country_code, $fieldname, $fieldid, $country_arr, '', $required, 'eme_select2_country_class' );
}
}
} elseif ( preg_match( '/#_COUNTRY$/', $result ) ) {
$fieldname = 'country_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerCountry_code ) ) {
$country_arr = [ $bookerCountry_code => eme_get_country_name( $bookerCountry_code ) ];
} else {
$country_arr = [];
}
$replacement = eme_ui_select( $bookerCountry_code, 'country_code', $country_arr, '', $required, 'eme_select2_country_class' );
$replacement = eme_ui_select( $bookerCountry_code, $fieldname, $fieldid, $country_arr, '', $required, 'eme_select2_country_class' );
} elseif ( preg_match( '/#_(EMAIL|HTML5_EMAIL)(\{.+?\})?$/', $result, $matches ) ) {
$this_readonly = '';
if ( is_user_logged_in() ) {
Expand Down Expand Up @@ -2322,7 +2350,7 @@ function eme_replace_dynamic_membership_formfields_placeholders( $membership, $m
return $format;
}

function eme_replace_rsvp_formfields_placeholders( $event, $booking, $format = '', $is_multibooking = 0 ) {
function eme_replace_rsvp_formfields_placeholders( $form_id, $event, $booking, $format = '', $is_multibooking = 0 ) {
$eme_is_admin_request = eme_is_admin_request();
// the next can happen if we would be editing a booking where the event has been deleted but somehow the booking remains
if ( isset( $event['event_id'] ) ) {
Expand Down Expand Up @@ -2947,22 +2975,32 @@ function eme_replace_rsvp_formfields_placeholders( $event, $booking, $format = '
} elseif ( preg_match( '/#_STATE$/', $result ) ) {
if ( ! $is_multibooking ) {
$fieldname = 'state_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerState_code ) ) {
$state_arr = [ $bookerState_code => eme_get_state_name( $bookerState_code, $bookerCountry_code ) ];
} else {
$state_arr = [];
}
$replacement = eme_ui_select( $bookerState_code, 'state_code', $state_arr, '', $required, "eme_select2_state_class $dynamic_field_class_basic", $disabled );
$replacement = eme_form_select( $bookerState_code, $fieldname, $fieldid, $state_arr, '', $required, "eme_select2_state_class $dynamic_field_class_basic", $disabled );
}
} elseif ( preg_match( '/#_COUNTRY$/', $result ) ) {
if ( ! $is_multibooking ) {
$fieldname = 'country_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerCountry_code ) ) {
$country_arr = [ $bookerCountry_code => eme_get_country_name( $bookerCountry_code ) ];
} else {
$country_arr = [];
}
$replacement = eme_ui_select( $bookerCountry_code, 'country_code', $country_arr, '', $required, "eme_select2_country_class $dynamic_field_class_basic", $disabled );
$replacement = eme_form_select( $bookerCountry_code, $fieldname, $fieldid, $country_arr, '', $required, "eme_select2_country_class $dynamic_field_class_basic", $disabled );
}
} elseif ( preg_match( '/#_(EMAIL|HTML5_EMAIL)(\{.+?\})?$/', $result, $matches ) ) {
if ( ! $is_multibooking ) {
Expand Down Expand Up @@ -3587,7 +3625,7 @@ function eme_replace_membership_familyformfields_placeholders( $format, $counter
}
}

function eme_replace_membership_formfields_placeholders( $membership, $member, $format ) {
function eme_replace_membership_formfields_placeholders( $form_id, $membership, $member, $format ) {
$eme_is_admin_request = eme_is_admin_request();
$membership_id = $membership['membership_id'];

Expand Down Expand Up @@ -3897,20 +3935,30 @@ function eme_replace_membership_formfields_placeholders( $membership, $member, $
$replacement = "<input $required_att type='text' name='$fieldname' id='$fieldname' value='$bookerZip' $readonly $dynamic_field_personal_info_class placeholder='$placeholder_text'>";
} elseif ( preg_match( '/#_STATE$/', $result ) ) {
$fieldname = 'state_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerState_code ) ) {
$state_arr = [ $bookerState_code => eme_get_state_name( $bookerState_code, $bookerCountry_code ) ];
} else {
$state_arr = [];
}
$replacement = "<div class=$personal_info_class>" . eme_ui_select( $bookerState_code, 'state_code', $state_arr, '', $required, "eme_select2_state_class $dynamic_field_class_basic", $disabled ) . '</div>';
$replacement = "<div class=$personal_info_class>" . eme_form_select( $bookerState_code, $fieldname, $fieldid, $state_arr, '', $required, "eme_select2_state_class $dynamic_field_class_basic", $disabled ) . '</div>';
} elseif ( preg_match( '/#_COUNTRY$/', $result ) ) {
$fieldname = 'country_code';
if (!empty($form_id)) {
$fieldid = $form_id.'-'.$fieldname;
} else {
$fieldid = $fieldname;
}
if ( ! empty( $bookerCountry_code ) ) {
$country_arr = [ $bookerCountry_code => eme_get_country_name( $bookerCountry_code ) ];
} else {
$country_arr = [];
}
$replacement = "<div class=$personal_info_class>" . eme_ui_select( $bookerCountry_code, 'country_code', $country_arr, '', $required, "eme_select2_country_class $dynamic_field_class_basic", $disabled ) . '</div>';
$replacement = "<div class=$personal_info_class>" . eme_form_select( $bookerCountry_code, $fieldname, $fieldid, $country_arr, '', $required, "eme_select2_country_class $dynamic_field_class_basic", $disabled ) . '</div>';
} elseif ( preg_match( '/#_(EMAIL|HTML5_EMAIL)(\{.+?\})?$/', $result, $matches ) ) {
$fieldname = 'email';
if ( is_user_logged_in() && ! $eme_is_admin_request ) {
Expand Down
2 changes: 1 addition & 1 deletion eme-members.php
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,7 @@ function eme_member_form( $member, $membership_id, $from_backend = 0 ) {
";
}

$form_html .= eme_replace_membership_formfields_placeholders( $membership, $member, $format_prefix.$format );
$form_html .= eme_replace_membership_formfields_placeholders( $form_id, $membership, $member, $format_prefix.$format );

if ( ! $from_backend ) {
if (!empty($member['member_id'])) {
Expand Down
14 changes: 7 additions & 7 deletions eme-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function eme_add_multibooking_form( $events, $template_id_header = 0, $template_
$form_html .= "<input type='hidden' name='person_id' value=''>";

if ( $is_multibooking ) {
$form_html .= eme_replace_extra_multibooking_formfields_placeholders( $format_header, $event );
$form_html .= eme_replace_extra_multibooking_formfields_placeholders( $form_id, $format_header, $event );
}

if ( $is_multibooking && $only_one_event && ! $simple ) {
Expand Down Expand Up @@ -252,12 +252,12 @@ function eme_add_multibooking_form( $events, $template_id_header = 0, $template_
// regular formfield replacement here, but indicate that it is for multibooking
if ( $is_multibooking ) {
if ( $simple ) {
$form_html .= eme_replace_rsvp_formfields_placeholders( $tmp_event, $new_booking, '#_SEATS <br>', $is_multibooking );
$form_html .= eme_replace_rsvp_formfields_placeholders( $form_id, $tmp_event, $new_booking, '#_SEATS <br>', $is_multibooking );
} else {
$form_html .= eme_replace_rsvp_formfields_placeholders( $tmp_event, $new_booking, $event_booking_format_entry, $is_multibooking );
$form_html .= eme_replace_rsvp_formfields_placeholders( $form_id, $tmp_event, $new_booking, $event_booking_format_entry, $is_multibooking );
}
} else {
$form_html .= eme_replace_rsvp_formfields_placeholders( $tmp_event, $new_booking );
$form_html .= eme_replace_rsvp_formfields_placeholders( $form_id, $tmp_event, $new_booking );
}
}
}
Expand All @@ -272,7 +272,7 @@ function eme_add_multibooking_form( $events, $template_id_header = 0, $template_
$form_html .= "<input type='hidden' name='$fieldname' value='1'>";
}
}
$form_html .= eme_replace_extra_multibooking_formfields_placeholders( $format_footer, $event );
$form_html .= eme_replace_extra_multibooking_formfields_placeholders( $form_id, $format_footer, $event );
}
$form_html .= '</form></div>';
if ( has_filter( 'eme_add_booking_form_filter' ) ) {
Expand Down Expand Up @@ -4696,7 +4696,7 @@ function eme_registration_seats_page( $pending = 0 ) {
$ret_string .= __( 'Send mails for new booking?', 'events-made-easy' ) . eme_ui_select_binary( 1, 'send_mail', 0, 'nodynamicupdates' );
$ret_string .= '<br>';
$new_booking = eme_new_booking();
$ret_string .= eme_replace_rsvp_formfields_placeholders( $event, $new_booking );
$ret_string .= eme_replace_rsvp_formfields_placeholders( '', $event, $new_booking );
$ret_string .= "
<input type='hidden' name='eme_admin_action' value='addBooking'>
<input type='hidden' name='event_id' value='$event_id'>
Expand Down Expand Up @@ -4748,7 +4748,7 @@ function eme_registration_seats_page( $pending = 0 ) {
} elseif ( $booking['event_price'] != $event['price'] ) {
$ret_string .= "<br><img style='vertical-align: middle;' src='" . esc_url(EME_PLUGIN_URL) . "images/warning.png' alt='warning'>" . __( 'Warning: the price of the event has changed compared to this booking, the new price will be taken into account for changes!', 'events-made-easy' ) . '<br>';
}
$ret_string .= eme_replace_rsvp_formfields_placeholders( $event, $booking );
$ret_string .= eme_replace_rsvp_formfields_placeholders( '', $event, $booking );
if ( ! empty( $event['event_id'] ) ) {
$ret_string .= "
<input type='hidden' name='eme_admin_action' value='updateBooking'>
Expand Down
2 changes: 1 addition & 1 deletion eme-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ function eme_tasks_signupform_shortcode( $atts ) {
if (empty($signupform_format)) {
$signupform_format = get_option( 'eme_task_form_format' );
}
$result .= eme_replace_task_signupformfields_placeholders( $signupform_format );
$result .= eme_replace_task_signupformfields_placeholders( $form_id, $signupform_format );
} else {
$result = "<div id='eme-tasks-message' class='eme-message-info eme-tasks-message eme-no-tasks'>" . __( 'There are no tasks to sign up for right now', 'events-made-easy' ) . '</div>';
}
Expand Down
43 changes: 43 additions & 0 deletions eme-ui-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,49 @@ function eme_ui_select_binary( $option_value, $name, $required = 0, $class = '',
return $val;
}

function eme_form_select( $option_value, $name, $id, $list, $add_empty_first = '', $required = 0, $class = '', $extra_attributes = '' ) {
// make sure it is an array, otherwise just go back
if ( ! is_array( $list ) ) {
return;
}

if ( $required ) {
$required_att = "required='required'";
} else {
$required_att = '';
}
if ( $class ) {
$class_att = "class='$class'";
} else {
$class_att = '';
}

$name = wp_strip_all_tags( $name );
if ( ! strstr( $extra_attributes, 'aria-label' ) ) {
$extra_attributes .= ' aria-label="' . $name . '"';
}

$val = "<select $class_att $required_att id='$id' name='$name' $extra_attributes >";
if ( $add_empty_first != '' ) {
$val .= "<option value=''>$add_empty_first</option>";
}
foreach ( $list as $key => $value ) {
if ( is_array( $value ) ) {
$t_key = $value[0];
$t_value = eme_esc_html( $value[1] );
} else {
$t_key = $key;
$t_value = eme_esc_html( $value );
}
if ( empty( $t_value ) && $t_value !== '0' ) {
$t_value = '&nbsp;';
}
"$t_key" === "$option_value" ? $selected = "selected='selected' " : $selected = '';
$val .= "<option value='" . eme_esc_html( $t_key ) . "' $selected>$t_value</option>";
}
$val .= ' </select>';
return $val;
}
function eme_ui_select( $option_value, $name, $list, $add_empty_first = '', $required = 0, $class = '', $extra_attributes = '' ) {
// make sure it is an array, otherwise just go back
if ( ! is_array( $list ) ) {
Expand Down
15 changes: 8 additions & 7 deletions js/eme.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,8 @@ jQuery(document).ready( function($) {
if ($('.eme_select2_width50_class').length) {
$('.eme_select2_width50_class').select2({width: '50%'});
}
if ($('#country_code.eme_select2_country_class').length) {
$('#country_code.eme_select2_country_class').select2({
if ($('.eme_select2_country_class').length) {
$('.eme_select2_country_class').select2({
// ajax based results mess up the width, so we need to set it
width: '100%',
ajax: {
Expand Down Expand Up @@ -1064,13 +1064,14 @@ jQuery(document).ready( function($) {
// if the country_code changes, clear the state_code if present
$('#country_code.eme_select2_country_class').on('change', function (e) {
// Do something
if ($('#state_code.eme_select2_state_class').length) {
$('#state_code.eme_select2_state_class').val(null).trigger('change');
let statefield = $(this).closest("form").find('[name=state_code]');
if (statefield.length) {
statefield.val(null).trigger('change');
}
});
}
if ($('#state_code.eme_select2_state_class').length) {
$('#state_code.eme_select2_state_class').select2({
if ($('.eme_select2_state_class').length) {
$('.eme_select2_state_class').select2({
// ajax based results mess up the width, so we need to set it
width: '100%',
ajax: {
Expand All @@ -1083,7 +1084,7 @@ jQuery(document).ready( function($) {
q: params.term, // search term
page: params.page || 1,
pagesize: 30,
country_code: $('#country_code').val(),
country_code: $(this).closest("form").find('[name=country_code]').val(),
action: 'eme_select_state',
eme_frontend_nonce: emebasic.translate_frontendnonce
};
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ See the FAQ section at the [Official site](https://www.e-dynamics.be/wordpress/)
= 2.5.34 (2025//) =
* Add filter eme_fs_validate_event_filter, which should return empty if all FS event data validation is ok
Parameter: $event_data (array containing all data entered via the frontend submit form)
* Fix multiple occurences of state and country input fields on one page

= 2.5.33 (2025/01/19) =
* Small bugfix due to the eme_mybookings and eme_bookings change
Expand Down

0 comments on commit ad07a7b

Please sign in to comment.