Nenhuma Descrição

mailchimp.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Mailchimp Block.
  4. *
  5. * @since 7.1.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Mailchimp;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack;
  12. use Jetpack_Gutenberg;
  13. use Jetpack_Options;
  14. const FEATURE_NAME = 'mailchimp';
  15. const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
  16. /**
  17. * Registers the block for use in Gutenberg
  18. * This is done via an action so that we can disable
  19. * registration if we need to.
  20. */
  21. function register_block() {
  22. if (
  23. ( defined( 'IS_WPCOM' ) && IS_WPCOM )
  24. || Jetpack::is_connection_ready()
  25. ) {
  26. Blocks::jetpack_register_block(
  27. BLOCK_NAME,
  28. array(
  29. 'render_callback' => __NAMESPACE__ . '\load_assets',
  30. )
  31. );
  32. }
  33. }
  34. add_action( 'init', __NAMESPACE__ . '\register_block' );
  35. /**
  36. * Mailchimp block registration/dependency declaration.
  37. *
  38. * @param array $attr - Array containing the Mailchimp block attributes.
  39. * @param string $content - Mailchimp block content.
  40. *
  41. * @return string
  42. */
  43. function load_assets( $attr, $content ) {
  44. if ( ! verify_connection() ) {
  45. return null;
  46. }
  47. $values = get_attributes_with_defaults( $attr );
  48. $blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
  49. ? get_current_blog_id()
  50. : Jetpack_Options::get_option( 'id' );
  51. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  52. $classes = Blocks::classes( FEATURE_NAME, $attr );
  53. $amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id );
  54. $is_amp_request = Blocks::is_amp_request();
  55. ob_start();
  56. ?>
  57. <div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
  58. <form
  59. aria-describedby="wp-block-jetpack-mailchimp_consent-text"
  60. <?php if ( $is_amp_request ) : ?>
  61. action-xhr="<?php echo esc_url( $amp_form_action ); ?>"
  62. method="post"
  63. id="mailchimp_form"
  64. target="_top"
  65. on="submit-success:AMP.setState( { mailing_list_status: 'subscribed', mailing_list_email: event.response.email } )"
  66. <?php endif; ?>
  67. >
  68. <p>
  69. <input
  70. aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
  71. placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
  72. required
  73. title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
  74. type="email"
  75. name="email"
  76. />
  77. </p>
  78. <?php foreach ( is_array( $values['interests'] ) ? $values['interests'] : array() as $interest ) : ?>
  79. <input
  80. name="interests[<?php echo esc_attr( $interest ); ?>]"
  81. type="hidden"
  82. class="mc-submit-param"
  83. value="1"
  84. />
  85. <?php endforeach; ?>
  86. <?php
  87. if (
  88. ! empty( $values['signupFieldTag'] )
  89. && ! empty( $values['signupFieldValue'] )
  90. ) :
  91. ?>
  92. <input
  93. name="merge_fields[<?php echo esc_attr( $values['signupFieldTag'] ); ?>]"
  94. type="hidden"
  95. class="mc-submit-param"
  96. value="<?php echo esc_attr( $values['signupFieldValue'] ); ?>"
  97. />
  98. <?php endif; ?>
  99. <?php echo render_button( $attr, $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
  100. <p id="wp-block-jetpack-mailchimp_consent-text">
  101. <?php echo wp_kses_post( $values['consentText'] ); ?>
  102. </p>
  103. <?php if ( $is_amp_request ) : ?>
  104. <div submit-success>
  105. <template type="amp-mustache">
  106. <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp">
  107. <?php echo esc_html( $values['successLabel'] ); ?>
  108. </div>
  109. </template>
  110. </div>
  111. <div submit-error>
  112. <template type="amp-mustache">
  113. <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp">
  114. <?php echo esc_html( $values['errorLabel'] ); ?>
  115. </div>
  116. </template>
  117. </div>
  118. <div submitting>
  119. <template type="amp-mustache">
  120. <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status">
  121. <?php echo esc_html( $values['processingLabel'] ); ?>
  122. </div>
  123. </template>
  124. </div>
  125. <?php endif; ?>
  126. </form>
  127. <?php if ( ! $is_amp_request ) : ?>
  128. <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
  129. <?php echo esc_html( $values['processingLabel'] ); ?>
  130. </div>
  131. <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
  132. <?php echo esc_html( $values['successLabel'] ); ?>
  133. </div>
  134. <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
  135. <?php echo esc_html( $values['errorLabel'] ); ?>
  136. </div>
  137. <?php endif; ?>
  138. </div>
  139. <?php
  140. $html = ob_get_clean();
  141. return $html;
  142. }
  143. /**
  144. * Mailchimp connection/list selection verification.
  145. *
  146. * @return boolean
  147. */
  148. function verify_connection() {
  149. $option = get_option( 'jetpack_mailchimp' );
  150. if ( ! $option ) {
  151. return false;
  152. }
  153. $data = json_decode( $option, true );
  154. if ( ! $data ) {
  155. return false;
  156. }
  157. return isset( $data['follower_list_id'], $data['keyring_id'] );
  158. }
  159. /**
  160. * Builds complete set of attributes using default values where needed.
  161. *
  162. * @param array $attr Saved set of attributes for the Mailchimp block.
  163. * @return array
  164. */
  165. function get_attributes_with_defaults( $attr ) {
  166. $values = array();
  167. $defaults = array(
  168. 'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
  169. 'consentText' => esc_html__( 'By clicking submit, you agree to share your email address with the site owner and Mailchimp to receive marketing, updates, and other emails from the site owner. Use the unsubscribe link in those emails to opt out at any time.', 'jetpack' ),
  170. 'processingLabel' => esc_html__( 'Processing…', 'jetpack' ),
  171. 'successLabel' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
  172. 'errorLabel' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
  173. 'interests' => array(),
  174. 'signupFieldTag' => '',
  175. 'signupFieldValue' => '',
  176. );
  177. foreach ( $defaults as $id => $default ) {
  178. $values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
  179. }
  180. return $values;
  181. }
  182. /**
  183. * Renders the Mailchimp block button using inner block content if available
  184. * otherwise generating the HTML button from deprecated attributes.
  185. *
  186. * @param array $attr Attributes for the Mailchimp block.
  187. * @param string $content Mailchimp block content.
  188. *
  189. * @return string
  190. */
  191. function render_button( $attr, $content ) {
  192. if ( ! empty( $content ) ) {
  193. $block_id = wp_unique_id( 'mailchimp-button-block-' );
  194. return str_replace( 'mailchimp-widget-id', $block_id, $content );
  195. }
  196. return render_deprecated_button( $attr );
  197. }
  198. /**
  199. * Renders HTML button from deprecated Mailchimp block attributes.
  200. *
  201. * @param array $attr Mailchimp block attributes.
  202. * @return string
  203. */
  204. function render_deprecated_button( $attr ) {
  205. $default = esc_html__( 'Join my email list', 'jetpack' );
  206. $text = empty( $attr['submitButtonText'] ) ? $default : $attr['submitButtonText'];
  207. $button_styles = array();
  208. if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
  209. array_push(
  210. $button_styles,
  211. sprintf(
  212. 'background-color: %s',
  213. sanitize_hex_color( $attr['customBackgroundButtonColor'] )
  214. )
  215. );
  216. }
  217. if ( ! empty( $attr['customTextButtonColor'] ) ) {
  218. array_push(
  219. $button_styles,
  220. sprintf(
  221. 'color: %s',
  222. sanitize_hex_color( $attr['customTextButtonColor'] )
  223. )
  224. );
  225. }
  226. $button_styles = implode( ';', $button_styles );
  227. $button_classes = 'components-button is-button is-primary ';
  228. if ( ! empty( $attr['submitButtonClasses'] ) ) {
  229. $button_classes .= $attr['submitButtonClasses'];
  230. }
  231. return sprintf(
  232. '<p><button type="submit" class="%s" style="%s">%s</button></p>',
  233. esc_attr( $button_classes ),
  234. esc_attr( $button_styles ),
  235. wp_kses_post( $text )
  236. );
  237. }