Skip to content

Commit a7baeed

Browse files
authored
Merge pull request #35 from mailchimp/feat/form-block
Adds basic block
2 parents 2660485 + f960aed commit a7baeed

File tree

12 files changed

+2449
-131
lines changed

12 files changed

+2449
-131
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ WordPress.com compatibility is limited to Business tier users only. [How to add
1818

1919
![Configuring extra fields on your Signup Form (optional)](https://github.com/mailchimp/wordpress/blob/develop/.wordpress-org/screenshot-4.jpg?raw=true)
2020

21+
## Frequently Asked Questions
22+
23+
### Can I have multiple forms on one page?
24+
25+
No, only one form should exist per page, no matter the display type (widget, shortcode, or block).
26+
2127
## Installation
2228

2329
This section describes how to install the plugin and get started using it.

includes/blocks/mailchimp/block.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 2,
4+
"name": "mailchimp/mailchimp",
5+
"title": "Mailchimp List Subscribe Form",
6+
"category": "text",
7+
"description": "Mailchimp List Subscribe Form",
8+
"attributes": {
9+
"json": {
10+
"type": "string"
11+
}
12+
},
13+
"supports": {
14+
"html": false,
15+
"multiple": false,
16+
"reusable": true,
17+
"align": [ "wide", "full" ],
18+
"__experimentalBorder": {
19+
"color": true,
20+
"radius": true,
21+
"style": true,
22+
"width": true,
23+
"__experimentalDefaultControls": {
24+
"radius": true,
25+
"style": true,
26+
"width": true
27+
}
28+
},
29+
"spacing": {
30+
"margin": true,
31+
"padding": true
32+
},
33+
"color": {
34+
"background": true,
35+
"text": true
36+
}
37+
},
38+
"textdomain": "mailchimp_i18n",
39+
"editorScript": "file:./index.js",
40+
"render": "file:./markup.php",
41+
"editorStyle": "file:./editor.css"
42+
}

includes/blocks/mailchimp/edit.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useBlockProps } from '@wordpress/block-editor';
2+
import { __ } from '@wordpress/i18n';
3+
import { Placeholder, Button, Disabled } from '@wordpress/components';
4+
import ServerSideRender from '@wordpress/server-side-render';
5+
import Icon from './icon';
6+
7+
export const BlockEdit = ({ isSelected }) => {
8+
const blockProps = useBlockProps();
9+
10+
return (
11+
<div {...blockProps}>
12+
{isSelected ? (
13+
<Placeholder
14+
icon={Icon}
15+
label={__('Mailchimp Block', 'mailchimp_i18n')}
16+
instructions={__('Great work! Your block is ready to go.', 'mailchimp_i18n')}
17+
>
18+
<div>
19+
<Button
20+
style={{ paddingLeft: 0 }}
21+
variant="link"
22+
href={window.MAILCHIMP_ADMIN_SETTINGS_URL}
23+
>
24+
{__(
25+
"Head over here if you'd like to adjust your settings.",
26+
'mailchimp_i18n',
27+
)}
28+
</Button>
29+
</div>
30+
</Placeholder>
31+
) : (
32+
<Disabled>
33+
<ServerSideRender block="mailchimp/mailchimp" />
34+
</Disabled>
35+
)}
36+
</div>
37+
);
38+
};

includes/blocks/mailchimp/editor.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Leave this file - wp_add_inline_style relies on this file existing */
2+
#mc_message {
3+
display: none;
4+
}

includes/blocks/mailchimp/icon.js

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/blocks/mailchimp/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { registerBlockType } from '@wordpress/blocks';
2+
3+
import { BlockEdit } from './edit';
4+
import metadata from './block.json';
5+
import Icon from './icon';
6+
7+
registerBlockType(metadata, {
8+
icon: Icon,
9+
edit: BlockEdit,
10+
save: () => null,
11+
});

includes/blocks/mailchimp/markup.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Displays a signup form.
4+
*
5+
* @package Mailchimp
6+
*/
7+
8+
?>
9+
<div <?php echo get_block_wrapper_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
10+
<?php
11+
mailchimp_sf_signup_form();
12+
?>
13+
</div>

mailchimp.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,38 @@ function mailchimp_sf_shortcode() {
816816
}
817817
add_shortcode( 'mailchimpsf_form', 'mailchimp_sf_shortcode' );
818818

819+
/**
820+
* Add block
821+
*
822+
* @return void
823+
*/
824+
function mailchimp_sf_block() {
825+
// In line with conditional register of the widget.
826+
if ( ! mailchimp_sf_get_api() ) {
827+
return;
828+
}
829+
830+
$blocks_dist_path = plugin_dir_path( __FILE__ ) . 'dist/blocks/';
831+
832+
if ( file_exists( $blocks_dist_path ) ) {
833+
$block_json_files = glob( $blocks_dist_path . '*/block.json' );
834+
foreach ( $block_json_files as $filename ) {
835+
$block_folder = dirname( $filename );
836+
register_block_type( $block_folder );
837+
}
838+
}
839+
840+
$data = 'window.MAILCHIMP_ADMIN_SETTINGS_URL = "' . esc_js( esc_url( admin_url( 'admin.php?page=mailchimp_sf_options' ) ) ) . '";';
841+
wp_add_inline_script( 'mailchimp-mailchimp-editor-script', $data, 'before' );
842+
843+
ob_start();
844+
require_once MCSF_DIR . '/views/css/frontend.php';
845+
$data = ob_get_clean();
846+
wp_add_inline_style( 'mailchimp-mailchimp-editor-style', $data );
847+
}
848+
849+
add_action( 'init', 'mailchimp_sf_block' );
850+
819851
/**
820852
* Attempts to signup a user, per the $_POST args.
821853
*

mailchimp_widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public function widget( $args, $instance ) {
526526
*/
527527
public function form( $instance ) {
528528
?>
529-
<p>Great work! Your widget is ready to go — just head <a href="<?php echo esc_url( admin_url( 'options-general.php?page=mailchimp_sf_options' ) ); ?>">over here</a> if you'd like to adjust your settings.</p>
529+
<p>Great work! Your widget is ready to go — just head <a href="<?php echo esc_url( admin_url( 'admin.php?page=mailchimp_sf_options' ) ); ?>">over here</a> if you'd like to adjust your settings.</p>
530530
<?php
531531
}
532532
}

0 commit comments

Comments
 (0)