|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class responsible for Mailchimp List Subscribe Form blocks. |
| 4 | + * |
| 5 | + * @package Mailchimp |
| 6 | + */ |
| 7 | + |
| 8 | +// Exit if accessed directly. |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { |
| 10 | + exit; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Class Mailchimp_List_Subscribe_Form_Blocks |
| 15 | + * |
| 16 | + * @since x.x.x |
| 17 | + */ |
| 18 | +class Mailchimp_List_Subscribe_Form_Blocks { |
| 19 | + |
| 20 | + /** |
| 21 | + * Initialize the class. |
| 22 | + */ |
| 23 | + public function init() { |
| 24 | + // In line with conditional register of the widget. |
| 25 | + if ( ! mailchimp_sf_get_api() ) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + add_action( 'init', array( $this, 'register_blocks' ) ); |
| 30 | + |
| 31 | + add_action( 'rest_api_init', array( $this, 'register_rest_endpoints' ) ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Register the block. |
| 36 | + */ |
| 37 | + public function register_blocks() { |
| 38 | + // Get the default visibility of interest groups. |
| 39 | + $interest_groups_visibility = array(); |
| 40 | + $interest_groups = get_option( 'mc_interest_groups', array() ); |
| 41 | + if ( ! empty( $interest_groups ) ) { |
| 42 | + foreach ( $interest_groups as $group ) { |
| 43 | + $visible = 'on' === get_option( 'mc_show_interest_groups_' . $group['id'], 'on' ) && 'hidden' !== $group['type']; |
| 44 | + $interest_groups_visibility[ $group['id'] ] = $visible ? 'on' : 'off'; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Get the default visibility of merge fields. |
| 49 | + $merge_fields_visibility = array(); |
| 50 | + $merge_fields = get_option( 'mc_merge_vars', array() ); |
| 51 | + if ( ! empty( $merge_fields ) ) { |
| 52 | + foreach ( $merge_fields as $field ) { |
| 53 | + $visible = 'on' === get_option( 'mc_mv_' . $field['tag'], 'on' ) || $field['required']; |
| 54 | + $merge_fields_visibility[ $field['tag'] ] = $visible ? 'on' : 'off'; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 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 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $data = array( |
| 70 | + 'admin_settings_url' => esc_url_raw( admin_url( 'admin.php?page=mailchimp_sf_options' ) ), |
| 71 | + 'lists' => $this->get_lists(), |
| 72 | + 'list_id' => get_option( 'mc_list_id', '' ), |
| 73 | + 'header_text' => get_option( 'mc_header_content', '' ), |
| 74 | + 'sub_header_text' => get_option( 'mc_subheader_content', '' ), |
| 75 | + 'submit_text' => get_option( 'mc_submit_text', __( 'Subscribe', 'mailchimp' ) ), |
| 76 | + 'show_unsubscribe_link' => get_option( 'mc_use_unsub_link', 'off' ) === 'on', |
| 77 | + 'update_existing_subscribers' => (bool) get_option( 'mc_update_existing', true ), |
| 78 | + 'double_opt_in' => (bool) get_option( 'mc_double_optin', true ), |
| 79 | + 'merge_fields_visibility' => $merge_fields_visibility, |
| 80 | + 'interest_groups_visibility' => $interest_groups_visibility, |
| 81 | + ); |
| 82 | + $data = 'window.mailchimp_sf_block_data = ' . wp_json_encode( $data ); |
| 83 | + wp_add_inline_script( 'mailchimp-mailchimp-editor-script', $data, 'before' ); |
| 84 | + |
| 85 | + ob_start(); |
| 86 | + require_once MCSF_DIR . '/views/css/frontend.php'; |
| 87 | + $data = ob_get_clean(); |
| 88 | + wp_add_inline_style( 'mailchimp-mailchimp-editor-style', $data ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get Mailchimp lists. |
| 93 | + * |
| 94 | + * @return array List of Mailchimp lists. |
| 95 | + */ |
| 96 | + public function get_lists() { |
| 97 | + $lists = get_option( 'mailchimp_sf_lists', array() ); |
| 98 | + // If we have lists, return them. |
| 99 | + if ( ! empty( $lists ) ) { |
| 100 | + return $lists; |
| 101 | + } |
| 102 | + |
| 103 | + // If we don't have any lists, get them from the API. |
| 104 | + $api = mailchimp_sf_get_api(); |
| 105 | + if ( ! $api ) { |
| 106 | + return array(); |
| 107 | + } |
| 108 | + |
| 109 | + // we *could* support paging, but 100 is more than enough for now. |
| 110 | + $lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) ); |
| 111 | + if ( is_wp_error( $lists ) ) { |
| 112 | + return array(); |
| 113 | + } |
| 114 | + |
| 115 | + $lists = $lists['lists'] ?? array(); |
| 116 | + |
| 117 | + // Update the option with the lists. |
| 118 | + update_option( 'mailchimp_sf_lists', $lists ); |
| 119 | + |
| 120 | + return $lists; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Register REST API endpoints. |
| 125 | + */ |
| 126 | + public function register_rest_endpoints() { |
| 127 | + register_rest_route( |
| 128 | + 'mailchimp/v1', |
| 129 | + '/list-data/(?P<list_id>[a-zA-Z0-9]+)/', |
| 130 | + array( |
| 131 | + 'methods' => WP_REST_Server::READABLE, |
| 132 | + 'callback' => array( $this, 'get_list_data' ), |
| 133 | + 'args' => array( |
| 134 | + 'list_id' => array( |
| 135 | + 'required' => true, |
| 136 | + 'type' => 'string', |
| 137 | + 'sanitize_callback' => 'sanitize_text_field', |
| 138 | + 'description' => esc_html__( 'Mailchimp list ID to get data', 'mailchimp' ), |
| 139 | + ), |
| 140 | + ), |
| 141 | + 'permission_callback' => array( $this, 'get_list_data_permissions_check' ), |
| 142 | + ) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get list data. |
| 148 | + * |
| 149 | + * @param WP_REST_Request $request Request object. |
| 150 | + * @return WP_REST_Response|WP_Error |
| 151 | + */ |
| 152 | + public function get_list_data( $request ) { |
| 153 | + $list_id = $request->get_param( 'list_id' ); |
| 154 | + |
| 155 | + $fields_key = 'mailchimp_sf_merge_fields_' . $list_id; |
| 156 | + $merge_fields = get_option( $fields_key, array() ); |
| 157 | + $groups_key = 'mailchimp_sf_interest_groups_' . $list_id; |
| 158 | + $interest_groups = get_option( $groups_key, array() ); |
| 159 | + |
| 160 | + // If we don't have any merge fields, get them from the API. |
| 161 | + if ( empty( $merge_fields ) ) { |
| 162 | + $api = mailchimp_sf_get_api(); |
| 163 | + $response = $api->get( 'lists/' . $list_id . '/merge-fields', 80 ); |
| 164 | + |
| 165 | + // if we get an error back from the api, return it. |
| 166 | + if ( is_wp_error( $response ) ) { |
| 167 | + return $response; |
| 168 | + } |
| 169 | + |
| 170 | + $merge_fields = mailchimp_sf_add_email_field( $response['merge_fields'] ); |
| 171 | + update_option( $fields_key, $merge_fields ); |
| 172 | + } |
| 173 | + |
| 174 | + // If we don't have any interest groups, get them from the API. |
| 175 | + if ( empty( $interest_groups ) ) { |
| 176 | + $api = mailchimp_sf_get_api(); |
| 177 | + $response = $api->get( 'lists/' . $list_id . '/interest-categories', 60 ); |
| 178 | + |
| 179 | + // if we get an error back from the api, return it. |
| 180 | + if ( is_wp_error( $response ) ) { |
| 181 | + return $response; |
| 182 | + } |
| 183 | + |
| 184 | + if ( is_array( $response ) ) { |
| 185 | + foreach ( $response['categories'] as $key => $ig ) { |
| 186 | + $groups = $api->get( 'lists/' . $list_id . '/interest-categories/' . $ig['id'] . '/interests', 60 ); |
| 187 | + $response['categories'][ $key ]['groups'] = $groups['interests']; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + $interest_groups = $response['categories']; |
| 192 | + update_option( $groups_key, $interest_groups ); |
| 193 | + } |
| 194 | + |
| 195 | + $data = array( |
| 196 | + 'merge_fields' => $merge_fields, |
| 197 | + 'interest_groups' => $interest_groups, |
| 198 | + ); |
| 199 | + |
| 200 | + return rest_ensure_response( $data ); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Check permissions for the list data. |
| 205 | + * |
| 206 | + * @return bool |
| 207 | + */ |
| 208 | + public function get_list_data_permissions_check() { |
| 209 | + return current_user_can( 'edit_posts' ); |
| 210 | + } |
| 211 | +} |
0 commit comments