Нет описания

revue.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Revue Block.
  4. *
  5. * @since 8.3.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Revue;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Gutenberg;
  12. const FEATURE_NAME = 'revue';
  13. const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
  14. /**
  15. * Registers the block for use in Gutenberg
  16. * This is done via an action so that we can disable
  17. * registration if we need to.
  18. */
  19. function register_block() {
  20. Blocks::jetpack_register_block(
  21. BLOCK_NAME,
  22. array( 'render_callback' => __NAMESPACE__ . '\render_block' )
  23. );
  24. }
  25. add_action( 'init', __NAMESPACE__ . '\register_block' );
  26. /**
  27. * Revue block render callback.
  28. *
  29. * @param array $attributes Array containing the Revue block attributes.
  30. * @param string $content The Revue block content.
  31. *
  32. * @return string
  33. */
  34. function render_block( $attributes, $content ) {
  35. if ( ! array_key_exists( 'revueUsername', $attributes ) ) {
  36. return '';
  37. }
  38. $email_label = get_revue_attribute( 'emailLabel', $attributes );
  39. $email_placeholder = get_revue_attribute( 'emailPlaceholder', $attributes );
  40. $first_name_label = get_revue_attribute( 'firstNameLabel', $attributes );
  41. $first_name_placeholder = get_revue_attribute( 'firstNamePlaceholder', $attributes );
  42. $first_name_show = get_revue_attribute( 'firstNameShow', $attributes );
  43. $last_name_label = get_revue_attribute( 'lastNameLabel', $attributes );
  44. $last_name_placeholder = get_revue_attribute( 'lastNamePlaceholder', $attributes );
  45. $last_name_show = get_revue_attribute( 'lastNameShow', $attributes );
  46. $url = sprintf( 'https://www.getrevue.co/profile/%s/add_subscriber', $attributes['revueUsername'] );
  47. $base_class = Blocks::classes( FEATURE_NAME, array() ) . '__';
  48. $classes = Blocks::classes( FEATURE_NAME, $attributes );
  49. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  50. ob_start();
  51. ?>
  52. <div class="<?php echo esc_attr( $classes ); ?>">
  53. <form
  54. action="<?php echo esc_url( $url ); ?>"
  55. class="<?php echo esc_attr( $base_class . 'form is-visible' ); ?>"
  56. method="post"
  57. name="revue-form"
  58. target="_blank"
  59. >
  60. <div>
  61. <label>
  62. <?php echo esc_html( $email_label ); ?>
  63. <span class="required"><?php esc_html_e( '(required)', 'jetpack' ); ?></span>
  64. <input
  65. class="<?php echo esc_attr( $base_class . 'email' ); ?>"
  66. name="member[email]"
  67. placeholder="<?php echo esc_attr( $email_placeholder ); ?>"
  68. required
  69. type="email"
  70. />
  71. </label>
  72. </div>
  73. <?php if ( $first_name_show ) : ?>
  74. <div>
  75. <label>
  76. <?php echo esc_html( $first_name_label ); ?>
  77. <input
  78. class="<?php echo esc_attr( $base_class . 'first-name' ); ?>"
  79. name="member[first_name]"
  80. placeholder="<?php echo esc_attr( $first_name_placeholder ); ?>"
  81. type="text"
  82. />
  83. </label>
  84. </div>
  85. <?php
  86. endif;
  87. if ( $last_name_show ) :
  88. ?>
  89. <div>
  90. <label>
  91. <?php echo esc_html( $last_name_label ); ?>
  92. <input
  93. class="<?php echo esc_attr( $base_class . 'last-name' ); ?>"
  94. name="member[last_name]"
  95. placeholder="<?php echo esc_attr( $last_name_placeholder ); ?>"
  96. type="text"
  97. />
  98. </label>
  99. </div>
  100. <?php
  101. endif;
  102. // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
  103. if ( false !== strpos( $content, 'wp-block-jetpack-revue__fallback' ) ) {
  104. echo $content;
  105. } else {
  106. echo get_deprecated_v1_revue_button( $attributes );
  107. }
  108. // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
  109. ?>
  110. </form>
  111. <div class="<?php echo esc_attr( $base_class . 'message' ); ?>">
  112. <p>
  113. <strong><?php esc_html_e( 'Subscription received!', 'jetpack' ); ?></strong>
  114. </p>
  115. <p>
  116. <?php esc_html_e( 'Please check your email to confirm your newsletter subscription.', 'jetpack' ); ?>
  117. </p>
  118. </div>
  119. </div>
  120. <?php
  121. return ob_get_clean();
  122. }
  123. /**
  124. * Get Revue block attribute.
  125. *
  126. * @param string $attribute String containing the attribute name to get.
  127. * @param array $attributes Array containing the Revue block attributes.
  128. *
  129. * @return mixed
  130. */
  131. function get_revue_attribute( $attribute, $attributes ) {
  132. if ( array_key_exists( $attribute, $attributes ) ) {
  133. return $attributes[ $attribute ];
  134. }
  135. $default_attributes = array(
  136. 'text' => __( 'Subscribe', 'jetpack' ),
  137. 'emailLabel' => __( 'Email address', 'jetpack' ),
  138. 'emailPlaceholder' => __( 'Enter your email address', 'jetpack' ),
  139. 'firstNameLabel' => __( 'First name', 'jetpack' ),
  140. 'firstNamePlaceholder' => __( 'Enter your first name', 'jetpack' ),
  141. 'firstNameShow' => true,
  142. 'lastNameLabel' => __( 'Last name', 'jetpack' ),
  143. 'lastNamePlaceholder' => __( 'Enter your last name', 'jetpack' ),
  144. 'lastNameShow' => true,
  145. );
  146. if ( array_key_exists( $attribute, $default_attributes ) ) {
  147. return $default_attributes[ $attribute ];
  148. }
  149. }
  150. /**
  151. * DEPRECATED V1
  152. */
  153. /**
  154. * Create the Revue subscribe button.
  155. *
  156. * @param array $attributes Array containing the Revue block attributes.
  157. *
  158. * @return string
  159. */
  160. function get_deprecated_v1_revue_button( $attributes ) {
  161. $classes = array( 'wp-block-button__link' );
  162. $styles = array();
  163. $text = get_revue_attribute( 'text', $attributes );
  164. $has_class_name = array_key_exists( 'className', $attributes );
  165. $has_named_text_color = array_key_exists( 'textColor', $attributes );
  166. $has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
  167. $has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
  168. $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
  169. $has_named_gradient = array_key_exists( 'gradient', $attributes );
  170. $has_custom_gradient = array_key_exists( 'customGradient', $attributes );
  171. $has_border_radius = array_key_exists( 'borderRadius', $attributes );
  172. if ( $has_class_name ) {
  173. $classes[] = $attributes['className'];
  174. }
  175. if ( $has_named_text_color || $has_custom_text_color ) {
  176. $classes[] = 'has-text-color';
  177. }
  178. if ( $has_named_text_color ) {
  179. $classes[] = sprintf( 'has-%s-color', $attributes['textColor'] );
  180. } elseif ( $has_custom_text_color ) {
  181. $styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] );
  182. }
  183. if (
  184. $has_named_background_color ||
  185. $has_custom_background_color ||
  186. $has_named_gradient ||
  187. $has_custom_gradient
  188. ) {
  189. $classes[] = 'has-background';
  190. }
  191. if ( $has_named_background_color && ! $has_custom_gradient ) {
  192. $classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
  193. }
  194. if ( $has_named_gradient ) {
  195. $classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
  196. } elseif ( $has_custom_gradient ) {
  197. $styles[] = sprintf( 'background: %s;', $attributes['customGradient'] );
  198. }
  199. if (
  200. $has_custom_background_color &&
  201. ! $has_named_background_color &&
  202. ! $has_named_gradient &&
  203. ! $has_custom_gradient
  204. ) {
  205. $styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
  206. }
  207. if ( $has_border_radius ) {
  208. // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
  209. if ( 0 == $attributes['borderRadius'] ) {
  210. $classes[] = 'no-border-radius';
  211. } else {
  212. $styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] );
  213. }
  214. }
  215. ob_start();
  216. ?>
  217. <div class="wp-block-button">
  218. <button
  219. class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
  220. name="member[subscribe]"
  221. style="<?php echo esc_attr( implode( ' ', $styles ) ); ?>"
  222. type="submit"
  223. >
  224. <?php echo wp_kses_post( $text ); ?>
  225. </button>
  226. </div>
  227. <?php
  228. return ob_get_clean();
  229. }