Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Mailchimp Block Updates #126

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions includes/blocks/mailchimp/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"apiVersion": 2,
"name": "mailchimp/mailchimp",
"title": "Mailchimp List Subscribe Form",
"category": "text",
"category": "widgets",
"description": "Mailchimp List Subscribe Form",
"attributes": {
"json": {
Expand Down Expand Up @@ -32,11 +32,17 @@
},
"color": {
"background": true,
"text": true
}
"text": true,
"border": true
},
"typography": {
"fontSize": true,
"lineHeight": true,
"textAlign": true
}
},
"textdomain": "mailchimp",
"editorScript": "file:./index.js",
"render": "file:./markup.php",
"editorStyle": "file:./editor.css"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php
/**
* Class responsible for Mailchimp List Subscribe Form block.
*
* @package Mailchimp
*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Mailchimp_Admin
*
* @since 1.6.0
*/
class Mailchimp_List_Subscribe_Form_Block {

/**
* Initialize the class.
*/
public function init() {
// In line with conditional register of the widget.
if ( ! mailchimp_sf_get_api() ) {
return;
}

add_action( 'init', array( $this, 'register_block' ) );

add_action( 'rest_api_init', array( $this, 'register_rest_endpoints' ) );
}

/**
* Register the block.
*/
public function register_block() {
$block_json_file = MCSF_DIR . 'dist/blocks/mailchimp/block.json';

// Get the default visibility of merge fields.
$merge_fields_visibility = array();
$merge_fields = get_option( 'mc_merge_vars', array() );
if ( ! empty( $merge_fields ) ) {
foreach ( $merge_fields as $field ) {
$visible = 'on' === get_option( 'mc_mv_' . $field['tag'], 'on' ) || $field['required'];
$merge_fields_visibility[ $field['tag'] ] = $visible ? 'on' : 'off';
}
}

// Get the default visibility of interest groups.
$interest_groups_visibility = array();
$interest_groups = get_option( 'mc_interest_groups', array() );
if ( ! empty( $interest_groups ) ) {
foreach ( $interest_groups as $group ) {
$visible = 'on' === get_option( 'mc_show_interest_groups_' . $group['id'], 'on' ) && 'hidden' !== $group['type'];
$interest_groups_visibility[ $group['id'] ] = $visible ? 'on' : 'off';
}
}

if ( file_exists( $block_json_file ) ) {
$block_folder = dirname( $block_json_file );
register_block_type(
$block_folder,
array(
'attributes' => array(
'header' => array(
'type' => 'string',
'default' => get_option( 'mc_header_content', '' ),
),
'sub_header' => array(
'type' => 'string',
'default' => get_option( 'mc_subheader_content', '' ),
),
'list_id' => array(
'type' => 'string',
'default' => get_option( 'mc_list_id', '' ),
),
'submit_text' => array(
'type' => 'string',
'default' => get_option( 'mc_submit_text', esc_html__( 'Subscribe', 'mailchimp' ) ),
),
'show_default_fields' => array(
'type' => 'boolean',
'default' => false,
),
'merge_fields_visibility' => array(
'type' => 'object',
'default' => $merge_fields_visibility,
),
'interest_groups_visibility' => array(
'type' => 'object',
'default' => $interest_groups_visibility,
),
'is_preview' => array(
'type' => 'boolean',
'default' => false,
),
'double_opt_in' => array(
'type' => 'boolean',
'default' => (bool) get_option( 'mc_double_optin', true ),
),
'update_existing_subscribers' => array(
'type' => 'boolean',
'default' => (bool) get_option( 'mc_update_existing', true ),
),
'show_unsubscribe_link' => array(
'type' => 'boolean',
'default' => (bool) get_option( 'mc_use_unsub_link', 'off' ) === 'on',
),
'unsubscribe_link_text' => array(
'type' => 'string',
'default' => esc_html__( 'unsubscribe from list', 'mailchimp' ),
),
),
)
);
}

$data = array(
'admin_settings_url' => esc_url_raw( admin_url( 'admin.php?page=mailchimp_sf_options' ) ),
'lists' => $this->mailchimp_sf_get_lists(),
);
$data = 'window.mailchimp_sf_block_data = ' . wp_json_encode( $data );
wp_add_inline_script( 'mailchimp-mailchimp-editor-script', $data, 'before' );

ob_start();
require_once MCSF_DIR . '/views/css/frontend.php';
$data = ob_get_clean();
wp_add_inline_style( 'mailchimp-mailchimp-editor-style', $data );
}

/**
* Get Mailchimp lists.
*
* @return array List of Mailchimp lists.
*/
public function mailchimp_sf_get_lists() {
// Just get out if nothing else matters...
$api = mailchimp_sf_get_api();
if ( ! $api ) {
return array();
}

// we *could* support paging, but few users have that many lists (and shouldn't)
$lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) );
if ( is_wp_error( $lists ) ) {
return array();
}
return $lists['lists'] ?? array();
}

/**
* Register REST API endpoints.
*/
public function register_rest_endpoints() {
register_rest_route(
'mailchimp/v1',
'/list-data/(?P<list_id>[a-zA-Z0-9]+)/',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_list_data' ),
'args' => array(
'list_id' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'description' => esc_html__( 'Mailchimp list ID to get data', 'mailchimp' ),
),
),
'permission_callback' => array( $this, 'get_list_data_permissions_check' ),
)
);
}

/**
* Get list data.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error
*/
public function get_list_data( $request ) {
$list_id = $request->get_param( 'list_id' );

$fields_key = 'mailchimp_sf_merge_fields_' . $list_id;
$merge_fields = get_option( $fields_key, array() );
$groups_key = 'mailchimp_sf_interest_groups_' . $list_id;
$interest_groups = get_option( $groups_key, array() );

// If we don't have any merge fields, get them from the API.
if ( empty( $merge_fields ) ) {
$api = mailchimp_sf_get_api();
$response = $api->get( 'lists/' . $list_id . '/merge-fields', 80 );

// if we get an error back from the api, return it.
if ( is_wp_error( $response ) ) {
return $response;
}

$merge_fields = mailchimp_sf_add_email_field( $response['merge_fields'] );
update_option( $fields_key, $merge_fields );
}

// If we don't have any interest groups, get them from the API.
if ( empty( $interest_groups ) ) {
$api = mailchimp_sf_get_api();
$response = $api->get( 'lists/' . $list_id . '/interest-categories', 60 );

// if we get an error back from the api, return it.
if ( is_wp_error( $response ) ) {
return $response;
}

if ( is_array( $response ) ) {
foreach ( $response['categories'] as $key => $ig ) {
$groups = $api->get( 'lists/' . $list_id . '/interest-categories/' . $ig['id'] . '/interests', 60 );
$response['categories'][ $key ]['groups'] = $groups['interests'];
}
}

$interest_groups = $response['categories'];
update_option( $groups_key, $interest_groups );
}

$data = array(
'merge_fields' => $merge_fields,
'interest_groups' => $interest_groups,
);

return rest_ensure_response( $data );
}

/**
* Check permissions for the list data.
*
* @return bool
*/
public function get_list_data_permissions_check() {
return current_user_can( 'edit_posts' );
}
}
Loading
Loading