| @@ -1,0 +1,108 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Mailchimp Block. | |
| 4 | + * | |
| 5 | + * @since 7.1.0 | |
| 6 | + * | |
| 7 | + * @package automattic/jetpack | |
| 8 | + */ | |
| 9 | + | |
| 10 | +namespace Automattic\Jetpack\Extensions\Mailchimp; | |
| 11 | + | |
| 12 | +use Automattic\Jetpack\Assets; | |
| 13 | +use Automattic\Jetpack\Blocks; | |
| 14 | +use Automattic\Jetpack\External_Connections; | |
| 15 | +use Jetpack; | |
| 16 | +use Jetpack_Gutenberg; | |
| 17 | + | |
| 18 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 19 | + exit( 0 ); | |
| 20 | +} | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Registers the block for use in Gutenberg | |
| 24 | + * This is done via an action so that we can disable | |
| 25 | + * registration if we need to. | |
| 26 | + */ | |
| 27 | +function register_block() { | |
| 28 | + if ( | |
| 29 | + ( defined( 'IS_WPCOM' ) && IS_WPCOM ) | |
| 30 | + || Jetpack::is_connection_ready() | |
| 31 | + ) { | |
| 32 | + Blocks::jetpack_register_block( | |
| 33 | + __DIR__, | |
| 34 | + array( | |
| 35 | + 'render_callback' => __NAMESPACE__ . '\load_assets', | |
| 36 | + ) | |
| 37 | + ); | |
| 38 | + | |
| 39 | + register_admin_settings(); | |
| 40 | + } | |
| 41 | +} | |
| 42 | +add_action( 'init', __NAMESPACE__ . '\register_block' ); | |
| 43 | + | |
| 44 | +/** | |
| 45 | + * Mailchimp block registration/dependency declaration. | |
| 46 | + * | |
| 47 | + * The render implementation lives in render.php and is only loaded when the | |
| 48 | + * block is actually rendered, keeping it out of the eager front-end path. | |
| 49 | + * | |
| 50 | + * @param array $attr - Array containing the Mailchimp block attributes. | |
| 51 | + * @param string $content - Mailchimp block content. | |
| 52 | + * | |
| 53 | + * @return string | |
| 54 | + */ | |
| 55 | +function load_assets( $attr, $content ) { | |
| 56 | + require_once __DIR__ . '/render.php'; | |
| 57 | + return load_assets_implementation( $attr, $content ); | |
| 58 | +} | |
| 59 | + | |
| 60 | +/** | |
| 61 | + * Registers the settings to manage the Mailchimp connection. | |
| 62 | + */ | |
| 63 | +function register_admin_settings() { | |
| 64 | + Assets::register_script( | |
| 65 | + 'jetpack-mailchimp-admin-extra-settings', | |
| 66 | + Jetpack_Gutenberg::get_blocks_directory() . '/mailchimp/admin.js', | |
| 67 | + JETPACK__PLUGIN_FILE, | |
| 68 | + array( | |
| 69 | + 'textdomain' => 'jetpack', | |
| 70 | + ) | |
| 71 | + ); | |
| 72 | + | |
| 73 | + External_Connections::add_settings_for_service( | |
| 74 | + 'writing', | |
| 75 | + array( | |
| 76 | + 'service' => 'mailchimp', | |
| 77 | + 'title' => __( 'Mailchimp', 'jetpack' ), | |
| 78 | + 'signup_link' => 'https://public-api.wordpress.com/rest/v1.1/sharing/mailchimp/signup', | |
| 79 | + 'description' => __( 'Allow users to sign up to your Mailchimp mailing list.', 'jetpack' ), | |
| 80 | + 'script' => 'jetpack-mailchimp-admin-extra-settings', | |
| 81 | + 'support_link' => array( | |
| 82 | + 'wpcom' => 'https://wordpress.com/support/wordpress-editor/blocks/mailchimp-block/', | |
| 83 | + 'jetpack' => 'mailchimp-block', | |
| 84 | + ), | |
| 85 | + ) | |
| 86 | + ); | |
| 87 | + | |
| 88 | + add_action( 'load-options.php', __NAMESPACE__ . '\update_settings' ); | |
| 89 | +} | |
| 90 | + | |
| 91 | +/** | |
| 92 | + * Update the site options that are related to Mailchimp. | |
| 93 | + */ | |
| 94 | +function update_settings() { | |
| 95 | + $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : ''; | |
| 96 | + $option_page = ! empty( $_REQUEST['option_page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['option_page'] ) ) : ''; | |
| 97 | + $audience = ! empty( $_REQUEST['jetpack-mailchimp-audience'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['jetpack-mailchimp-audience'] ) ) : ''; | |
| 98 | + if ( $action !== 'update' || $option_page !== 'writing' || ! current_user_can( 'manage_options' ) || $audience === '' ) { | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + | |
| 102 | + check_admin_referer( 'writing-options' ); | |
| 103 | + | |
| 104 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-mailchimp-helper.php'; | |
| 105 | + if ( is_wp_error( \Jetpack_Mailchimp_Helper::save_audience( $audience ) ) ) { | |
| 106 | + add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.', 'jetpack' ), 'error' ); | |
| 107 | + } | |
| 108 | +} | |