Skip to content

Commit f0dc9a4

Browse files
committed
Adds basic block
1 parent f4d0c86 commit f0dc9a4

File tree

10 files changed

+2436
-131
lines changed

10 files changed

+2436
-131
lines changed

includes/blocks/mailchimp/block.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
"reusable": true,
16+
"align": [ "wide", "full" ],
17+
"__experimentalBorder": {
18+
"color": true,
19+
"radius": true,
20+
"style": true,
21+
"width": true,
22+
"__experimentalDefaultControls": {
23+
"radius": true,
24+
"style": true,
25+
"width": true
26+
}
27+
},
28+
"spacing": {
29+
"margin": true,
30+
"padding": true
31+
},
32+
"color": {
33+
"background": true,
34+
"text": true
35+
}
36+
},
37+
"textdomain": "mailchimp_i18n",
38+
"editorScript": "file:./index.js",
39+
"render": "file:./markup.php",
40+
"editorStyle": "file:./editor.css"
41+
}

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 blank - 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
@@ -817,6 +817,38 @@ function mailchimp_sf_shortcode() {
817817
}
818818
add_shortcode( 'mailchimpsf_form', 'mailchimp_sf_shortcode' );
819819

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

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)