Skip to content

Commit 3c0f4b9

Browse files
authored
Merge pull request #137 from mailchimp/fix/34
Make the form field visibility dependent on the WordPress "Include?" settings instead of the Mailchimp settings.
2 parents 54cdcd4 + 486cede commit 3c0f4b9

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

mailchimp.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,15 @@ function mailchimp_sf_get_merge_vars( $list_id, $new_list, $update_option = true
680680

681681
foreach ( $mv['merge_fields'] as $mv_var ) {
682682
$opt = 'mc_mv_' . $mv_var['tag'];
683-
// turn them all on by default
684683
if ( $new_list ) {
685-
update_option( $opt, 'on' );
684+
$public = $mv_var['public'] ?? false;
685+
if ( ! $public ) {
686+
// This is a hidden field, so we don't want to include it.
687+
update_option( $opt, 'off' );
688+
} else {
689+
// We need to set the option to 'on' so that it shows up in the form.
690+
update_option( $opt, 'on' );
691+
}
686692
}
687693
}
688694
return $mv['merge_fields'];

mailchimp_upgrade.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function mailchimp_version_check() {
2222
mailchimp_update_1_6_0();
2323
}
2424

25+
if ( false === $db_option || version_compare( '1.7.0', $db_option, '>' ) ) {
26+
mailchimp_update_1_7_0();
27+
}
28+
2529
update_option( 'mc_version', MCSF_VER );
2630
}
2731

@@ -36,3 +40,24 @@ function mailchimp_version_check() {
3640
function mailchimp_update_1_6_0() {
3741
delete_option( 'mc_rewards' );
3842
}
43+
44+
/**
45+
* Version 1.7.0 update routine
46+
* - Set "Include?" value to "off" for hidden fields
47+
*
48+
* @return void
49+
*/
50+
function mailchimp_update_1_7_0() {
51+
$form_fields = get_option( 'mc_merge_vars' );
52+
53+
if ( ! empty( $form_fields ) && is_array( $form_fields ) ) {
54+
foreach ( $form_fields as $field ) {
55+
if ( ! $field['required'] && ! $field['public'] ) {
56+
$option = 'mc_mv_' . $field['tag'];
57+
// This is a hidden field, so we don't want to include it.
58+
// We need to set the option to 'off' so that it doesn't show up in the form.
59+
update_option( $option, 'off' );
60+
}
61+
}
62+
}
63+
}

mailchimp_widget.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
205205

206206
// Loop over our vars, and output the ones that are set to display
207207
foreach ( $mv as $mv_var ) {
208-
if ( ! $mv_var['public'] ) {
209-
echo '<div style="display:none;">' . mailchimp_form_field( $mv_var, $num_fields ) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Ignoring because form field is escaped in function
210-
} else {
211-
echo mailchimp_form_field( $mv_var, $num_fields ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Ignoring because form field is escaped in function
212-
}
208+
echo mailchimp_form_field( $mv_var, $num_fields ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Ignoring because form field is escaped in function
213209
}
214210

215211
// Show an explanation of the * if there's more than one field

views/setup_page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function ( $ele ) {
297297
<td><?php echo esc_html( ( 1 === intval( $mv_var['required'] ) ) ? 'Y' : 'N' ); ?></td>
298298
<td>
299299
<?php
300-
if ( ! $mv_var['required'] && $mv_var['public'] ) {
300+
if ( ! $mv_var['required'] ) {
301301
$opt = 'mc_mv_' . $mv_var['tag'];
302302
?>
303303
<label class="screen-reader-text" for="<?php echo esc_attr( $opt ); ?>">

0 commit comments

Comments
 (0)