Skip to content

Commit 33fe00b

Browse files
committed
Make sure block has valid ID stored.
1 parent d88d84c commit 33fe00b

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

includes/blocks/class-mailchimp-list-subscribe-form-blocks.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function register_blocks() {
119119

120120
$data = array(
121121
'admin_settings_url' => esc_url_raw( admin_url( 'admin.php?page=mailchimp_sf_options' ) ),
122-
'lists' => $this->mailchimp_sf_get_lists(),
122+
'lists' => $this->get_lists(),
123123
'merge_fields_visibility' => $merge_fields_visibility,
124124
);
125125
$data = 'window.mailchimp_sf_block_data = ' . wp_json_encode( $data );
@@ -136,8 +136,14 @@ public function register_blocks() {
136136
*
137137
* @return array List of Mailchimp lists.
138138
*/
139-
public function mailchimp_sf_get_lists() {
140-
// Just get out if nothing else matters...
139+
public function get_lists() {
140+
$lists = get_option( 'mailchimp_sf_lists', array() );
141+
// If we have lists, return them.
142+
if ( ! empty( $lists ) ) {
143+
return $lists;
144+
}
145+
146+
// If we don't have any lists, get them from the API.
141147
$api = mailchimp_sf_get_api();
142148
if ( ! $api ) {
143149
return array();
@@ -148,7 +154,13 @@ public function mailchimp_sf_get_lists() {
148154
if ( is_wp_error( $lists ) ) {
149155
return array();
150156
}
151-
return $lists['lists'] ?? array();
157+
158+
$lists = $lists['lists'] ?? array();
159+
160+
// Update the option with the lists.
161+
update_option( 'mailchimp_sf_lists', $lists );
162+
163+
return $lists;
152164
}
153165

154166
/**

includes/blocks/mailchimp/markup.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@
1313
return;
1414
}
1515

16-
$block_instance = $block->parsed_block;
16+
// Make sure we have a list ID and it's valid.
17+
$lists = ( new Mailchimp_List_Subscribe_Form_Blocks() )->get_lists();
18+
$list_ids = array_map(
19+
function ( $single_list ) {
20+
return $single_list['id'];
21+
},
22+
$lists
23+
);
24+
25+
if ( ! in_array( $attributes['list_id'], $list_ids, true ) ) {
26+
return;
27+
}
1728

1829
// Backwards compatibility for old block.
19-
$inner_blocks = $block_instance['innerBlocks'] ?? [];
30+
$block_instance = $block->parsed_block;
31+
$inner_blocks = $block_instance['innerBlocks'] ?? [];
2032
if ( empty( $inner_blocks ) ) {
2133
mailchimp_sf_signup_form();
2234
?>
@@ -36,7 +48,7 @@
3648
$unsubscribe_link_text = $attributes['unsubscribe_link_text'] ?? __( 'unsubscribe from list', 'mailchimp' );
3749
$update_existing_subscribers = ( $attributes['update_existing_subscribers'] ?? get_option( 'mc_update_existing' ) === 'on' ) ? 'yes' : 'no';
3850
$double_opt_in = ( $attributes['double_opt_in'] ?? get_option( 'mc_double_optin' ) === 'on' ) ? 'yes' : 'no';
39-
$show_required_indicator = $attributes['show_required_indicator'] ?? true;
51+
$show_required_indicator = $attributes['show_required_indicator'] ?? true;
4052
$hash = wp_hash(
4153
serialize( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
4254
array(

includes/class-mailchimp-admin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function verify_and_save_oauth_token( $access_token, $data_center ) {
309309
update_option( 'mc_user', $this->sanitize_data( $user ) );
310310

311311
// Clear Mailchimp List ID if saved list is not available.
312-
$lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id' ) );
312+
$lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) );
313313
if ( ! is_wp_error( $lists ) ) {
314314
$lists = $lists['lists'] ?? array();
315315
$saved_list_id = get_option( 'mc_list_id' );
@@ -322,6 +322,11 @@ function ( $ele ) {
322322
if ( ! in_array( $saved_list_id, $list_ids, true ) ) {
323323
delete_option( 'mc_list_id' );
324324
}
325+
326+
// Update lists option.
327+
if ( ! empty( $lists ) ) {
328+
update_option( 'mailchimp_sf_lists', $lists );
329+
}
325330
}
326331
return true;
327332
} else {

mailchimp.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,15 @@ function mailchimp_sf_change_list_if_necessary() {
609609
$list_name = $list['name'];
610610
$list_key = $key;
611611
}
612+
613+
$merge_fields = mailchimp_sf_get_merge_vars( $list['id'], false, false );
614+
$interests = mailchimp_sf_get_interest_categories( $list['id'], false, false );
615+
if ( ! empty( $merge_fields ) ) {
616+
update_option( 'mailchimp_sf_merge_fields_' . $list['id'], $merge_fields );
617+
}
618+
if ( ! empty( $interests ) ) {
619+
update_option( 'mailchimp_sf_interest_groups_' . $list['id'], $interests );
620+
}
612621
}
613622

614623
$orig_list = get_option( 'mc_list_id' );
@@ -647,6 +656,9 @@ function mailchimp_sf_change_list_if_necessary() {
647656

648657
mailchimp_sf_global_msg( $msg );
649658
}
659+
660+
// Update the lists option.
661+
update_option( 'mailchimp_sf_lists', $lists );
650662
}
651663
}
652664

@@ -655,9 +667,10 @@ function mailchimp_sf_change_list_if_necessary() {
655667
*
656668
* @param string $list_id List ID
657669
* @param bool $new_list Whether this is a new list
670+
* @param bool $update_option Whether to update the option
658671
* @return array
659672
*/
660-
function mailchimp_sf_get_merge_vars( $list_id, $new_list ) {
673+
function mailchimp_sf_get_merge_vars( $list_id, $new_list, $update_option = true ) {
661674
$api = mailchimp_sf_get_api();
662675
$mv = $api->get( 'lists/' . $list_id . '/merge-fields', 80 );
663676

@@ -667,7 +680,10 @@ function mailchimp_sf_get_merge_vars( $list_id, $new_list ) {
667680
}
668681

669682
$mv['merge_fields'] = mailchimp_sf_add_email_field( $mv['merge_fields'] );
670-
update_option( 'mc_merge_vars', $mv['merge_fields'] );
683+
if ( $update_option ) {
684+
update_option( 'mc_merge_vars', $mv['merge_fields'] );
685+
}
686+
671687
foreach ( $mv['merge_fields'] as $mv_var ) {
672688
$opt = 'mc_mv_' . $mv_var['tag'];
673689
// turn them all on by default
@@ -703,9 +719,10 @@ function mailchimp_sf_add_email_field( $merge ) {
703719
*
704720
* @param string $list_id List ID
705721
* @param bool $new_list Whether this is a new list
722+
* @param bool $update_option Whether to update the option
706723
* @return array
707724
*/
708-
function mailchimp_sf_get_interest_categories( $list_id, $new_list ) {
725+
function mailchimp_sf_get_interest_categories( $list_id, $new_list, $update_option = true ) {
709726
$api = mailchimp_sf_get_api();
710727
$igs = $api->get( 'lists/' . $list_id . '/interest-categories', 60 );
711728

@@ -728,7 +745,11 @@ function mailchimp_sf_get_interest_categories( $list_id, $new_list ) {
728745
++$key;
729746
}
730747
}
731-
update_option( 'mc_interest_groups', $igs['categories'] );
748+
749+
if ( $update_option ) {
750+
update_option( 'mc_interest_groups', $igs['categories'] );
751+
}
752+
732753
return $igs['categories'];
733754
}
734755

0 commit comments

Comments
 (0)