Skip to content

Commit 44d2566

Browse files
committed
Move attributes to the block.json file
1 parent 94fcb22 commit 44d2566

File tree

4 files changed

+68
-65
lines changed

4 files changed

+68
-65
lines changed

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

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,70 +55,23 @@ public function register_blocks() {
5555
}
5656
}
5757

58-
$attributes = array(
59-
'header' => array(
60-
'type' => 'string',
61-
'default' => get_option( 'mc_header_content', '' ),
62-
),
63-
'sub_header' => array(
64-
'type' => 'string',
65-
'default' => get_option( 'mc_subheader_content', '' ),
66-
),
67-
'list_id' => array(
68-
'type' => 'string',
69-
),
70-
'submit_text' => array(
71-
'type' => 'string',
72-
'default' => esc_html__( 'Subscribe', 'mailchimp' ),
73-
),
74-
'interest_groups_visibility' => array(
75-
'type' => 'object',
76-
),
77-
'double_opt_in' => array(
78-
'type' => 'boolean',
79-
'default' => true,
80-
),
81-
'update_existing_subscribers' => array(
82-
'type' => 'boolean',
83-
'default' => true,
84-
),
85-
'show_unsubscribe_link' => array(
86-
'type' => 'boolean',
87-
'default' => false,
88-
),
89-
'unsubscribe_link_text' => array(
90-
'type' => 'string',
91-
'default' => esc_html__( 'unsubscribe from list', 'mailchimp' ),
92-
),
93-
'show_required_indicator' => array(
94-
'type' => 'boolean',
95-
'default' => true,
96-
),
97-
);
98-
99-
// Register the Mailchimp List Subscribe Form block.
100-
$block_json_file = MCSF_DIR . 'dist/blocks/mailchimp/block.json';
101-
if ( file_exists( $block_json_file ) ) {
102-
$block_folder = dirname( $block_json_file );
103-
register_block_type(
104-
$block_folder,
105-
array(
106-
'attributes' => $attributes,
107-
)
108-
);
109-
}
110-
111-
// Register the Mailchimp form field block.
112-
$form_field_block_json_file = MCSF_DIR . 'dist/blocks/mailchimp-form-field/block.json';
113-
if ( file_exists( $form_field_block_json_file ) ) {
114-
$form_field_block_folder = dirname( $form_field_block_json_file );
115-
register_block_type( $form_field_block_folder );
58+
// Register the Mailchimp List Subscribe Form blocks.
59+
$blocks_dist_path = MCSF_DIR . 'dist/blocks/';
60+
61+
if ( file_exists( $blocks_dist_path ) ) {
62+
$block_json_files = glob( $blocks_dist_path . '*/block.json' );
63+
foreach ( $block_json_files as $filename ) {
64+
$block_folder = dirname( $filename );
65+
register_block_type( $block_folder );
66+
}
11667
}
11768

11869
$data = array(
11970
'admin_settings_url' => esc_url_raw( admin_url( 'admin.php?page=mailchimp_sf_options' ) ),
12071
'lists' => $this->get_lists(),
12172
'list_id' => get_option( 'mc_list_id', '' ),
73+
'header_text' => get_option( 'mc_header_content', '' ),
74+
'sub_header_text' => get_option( 'mc_subheader_content', '' ),
12275
'merge_fields_visibility' => $merge_fields_visibility,
12376
);
12477
$data = 'window.mailchimp_sf_block_data = ' . wp_json_encode( $data );

includes/blocks/mailchimp/block.json

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,44 @@
55
"title": "Mailchimp List Subscribe Form",
66
"category": "widgets",
77
"description": "Mailchimp List Subscribe Form",
8-
"attributes": {},
8+
"attributes": {
9+
"list_id": {
10+
"type": "string"
11+
},
12+
"header": {
13+
"type": "string"
14+
},
15+
"sub_header": {
16+
"type": "string"
17+
},
18+
"submit_text": {
19+
"type": "string",
20+
"default": "Subscribe"
21+
},
22+
"interest_groups_visibility": {
23+
"type": "object"
24+
},
25+
"double_opt_in": {
26+
"type": "boolean",
27+
"default": true
28+
},
29+
"update_existing_subscribers": {
30+
"type": "boolean",
31+
"default": true
32+
},
33+
"show_unsubscribe_link": {
34+
"type": "boolean",
35+
"default": false
36+
},
37+
"unsubscribe_link_text": {
38+
"type": "string",
39+
"default": "unsubscribe from list"
40+
},
41+
"show_required_indicator": {
42+
"type": "boolean",
43+
"default": true
44+
}
45+
},
946
"supports": {
1047
"html": false,
1148
"multiple": false,

includes/blocks/mailchimp/edit.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ const SelectListPlaceholder = () => {
3737
export const BlockEdit = (props) => {
3838
const { clientId, attributes, setAttributes } = props;
3939
const { mailchimp_sf_block_data } = window;
40-
const { lists, merge_fields_visibility, list_id: listId } = mailchimp_sf_block_data;
40+
const {
41+
lists,
42+
merge_fields_visibility,
43+
list_id: listId,
44+
header_text,
45+
sub_header_text,
46+
} = mailchimp_sf_block_data;
4147

4248
const {
43-
header,
44-
sub_header,
49+
header = header_text,
50+
sub_header = sub_header_text,
4551
list_id = listId,
4652
submit_text,
4753
double_opt_in,
@@ -186,9 +192,17 @@ export const BlockEdit = (props) => {
186192

187193
updateList(list_id, false);
188194

189-
// Set list_id attribute initially, if it's already not set.
195+
// Set the attributes initially, if it's already not set.
190196
if (attributes.list_id === undefined) {
191-
setAttributes({ list_id: listId });
197+
const attributeUpdates = { list_id: listId };
198+
if (attributes.header === undefined) {
199+
attributeUpdates.header = header_text;
200+
}
201+
if (attributes.sub_header === undefined) {
202+
attributeUpdates.sub_header = sub_header_text;
203+
}
204+
205+
setAttributes(attributeUpdates);
192206
}
193207
}, []); // eslint-disable-line react-hooks/exhaustive-deps -- Only run on initial render.
194208

includes/blocks/mailchimp/editor.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
opacity: 0.5;
1212
}
1313

14-
1514
.mailchimp_merge_field_hidden {
1615
display: none;
1716
}

0 commit comments

Comments
 (0)