SubscriptionFormBlock.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Blocks; |
| 4 | |
| 5 | |
| 6 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 7 | use Hostinger\Reach\Functions; |
| 8 | use Hostinger\Reach\Integrations\Reach\ReachFormIntegration; |
| 9 | use Hostinger\Reach\Setup\Assets; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | class SubscriptionFormBlock extends Block { |
| 16 | public string $name = 'subscription'; |
| 17 | private ReachApiHandler $reach_api_handler; |
| 18 | |
| 19 | public function __construct( Assets $assets, Functions $functions, ReachApiHandler $reach_api_handler ) { |
| 20 | parent::__construct( $assets, $functions ); |
| 21 | $this->reach_api_handler = $reach_api_handler; |
| 22 | } |
| 23 | |
| 24 | public function data(): array { |
| 25 | return array( |
| 26 | 'endpoint' => esc_url_raw( rest_url() ) . 'hostinger-reach/v1/contact', |
| 27 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 28 | 'translations' => array( |
| 29 | 'thanks' => __( 'Thanks for subscribing.', 'hostinger-reach' ), |
| 30 | 'error' => __( 'Something went wrong. Please try again.', 'hostinger-reach' ), |
| 31 | ), |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | public function autoloader(): void { |
| 36 | if ( ! is_admin() || empty( $_GET['hostinger_reach_add_block'] ) ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if ( $this->functions->block_file_exists( "$this->name-autoloader.js" ) === false ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $handler = parent::get_block_name() . '-autoloader'; |
| 45 | |
| 46 | wp_enqueue_script( |
| 47 | $handler, |
| 48 | $this->functions->get_blocks_url() . "$this->name-autoloader.js", |
| 49 | array( parent::get_block_name() . '-editor' ), |
| 50 | filemtime( $this->functions->get_block_file_name( "$this->name-autoloader.js" ) ), |
| 51 | array( 'in_footer' => true ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | public function render( array $attributes ): bool|string { |
| 56 | ob_start(); |
| 57 | $is_connected = $this->reach_api_handler->is_connected(); |
| 58 | $this->render_block_html( $attributes, null, $is_connected ); |
| 59 | |
| 60 | return ob_get_clean(); |
| 61 | } |
| 62 | |
| 63 | public static function render_block_html( array $attributes, ?string $plugin = null, bool $is_connected = true ): void { |
| 64 | $form_id = $attributes['formId'] ?? ''; |
| 65 | $show_name = $attributes['showName'] ?? false; |
| 66 | $show_surname = $attributes['showSurname'] ?? false; |
| 67 | $contact_list = $attributes['contactList'] ?? ''; |
| 68 | $tags = $attributes['tags'] ?? array(); |
| 69 | $layout = $attributes['layout'] ?? 'default'; |
| 70 | $is_inline = $layout === 'inline'; |
| 71 | $plugin = $plugin ?? ReachFormIntegration::INTEGRATION_NAME; |
| 72 | ?> |
| 73 | <div class="hostinger-reach-block-subscription-form-wrapper"> |
| 74 | |
| 75 | <form id="<?php echo esc_attr( $form_id ); ?>" class="hostinger-reach-block-subscription-form"> |
| 76 | <input type="hidden" name="group" value="<?php echo esc_attr( $contact_list ); ?>"> |
| 77 | <input type="hidden" name="id" value="<?php echo esc_attr( $form_id ); ?>"> |
| 78 | <input type="hidden" name="tags" value="<?php echo esc_attr( implode( ',', $tags ) ); ?>"> |
| 79 | <input type="hidden" name="metadata.plugin" value="<?php echo esc_attr( $plugin ); ?>"> |
| 80 | |
| 81 | <div |
| 82 | class="hostinger-reach-block-form-fields <?php echo esc_attr( $is_inline ? 'hostinger-reach-block-form-fields--inline' : '' ); ?>"> |
| 83 | <div class="hostinger-reach-block-form-field"> |
| 84 | <label |
| 85 | for="<?php echo esc_attr( $form_id ); ?>-email"><?php esc_html_e( 'Email', 'hostinger-reach' ); ?> |
| 86 | <span class="required">*</span></label> |
| 87 | <input type="email" id="<?php echo esc_attr( $form_id ); ?>-email" name="email" required> |
| 88 | </div> |
| 89 | |
| 90 | <?php if ( $show_name ) : ?> |
| 91 | <div class="hostinger-reach-block-form-field"> |
| 92 | <label |
| 93 | for="<?php echo esc_attr( $form_id ); ?>-name"><?php esc_html_e( 'Name', 'hostinger-reach' ); ?></label> |
| 94 | <input type="text" id="<?php echo esc_attr( $form_id ); ?>-name" name="name"> |
| 95 | </div> |
| 96 | <?php endif; ?> |
| 97 | |
| 98 | <?php if ( $show_surname ) : ?> |
| 99 | <div class="hostinger-reach-block-form-field"> |
| 100 | <label |
| 101 | for="<?php echo esc_attr( $form_id ); ?>-surname"><?php esc_html_e( 'Surname', 'hostinger-reach' ); ?></label> |
| 102 | <input type="text" id="<?php echo esc_attr( $form_id ); ?>-surname" name="surname"> |
| 103 | </div> |
| 104 | <?php endif; ?> |
| 105 | |
| 106 | <button |
| 107 | type="submit" |
| 108 | class="hostinger-reach-block-submit has-light-color has-color-3-background-color has-text-color has-background has-link-color"> |
| 109 | <?php esc_html_e( 'Subscribe', 'hostinger-reach' ); ?> |
| 110 | </button> |
| 111 | </div> |
| 112 | |
| 113 | <div class="reach-subscription-message" style="display: none;"> |
| 114 | <div class="reach-subscription-message__icon-success" style="display: none;"> |
| 115 | <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 116 | <path |
| 117 | d="M21.0455 5.95463C21.4848 6.39397 21.4848 7.10628 21.0455 7.54562L11.5076 17.0835C10.9511 17.64 10.0489 17.64 9.49237 17.0835L5.2045 12.7956C4.76517 12.3563 4.76517 11.644 5.2045 11.2046C5.64384 10.7653 6.35616 10.7653 6.7955 11.2046L10.5 14.9091L19.4545 5.95463C19.8938 5.51529 20.6062 5.51529 21.0455 5.95463Z" |
| 118 | fill="#18181A"/> |
| 119 | </svg> |
| 120 | </div> |
| 121 | <div class="reach-subscription-message__icon-error" style="display: none;"> |
| 122 | <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 123 | <path fill="#a10223" fill-rule="evenodd" |
| 124 | d="M11.279 4.721a.75.75 0 0 1 0 1.06L5.782 11.28a.75.75 0 0 1-1.06-1.06l5.496-5.498a.75.75 0 0 1 1.06 0" |
| 125 | clip-rule="evenodd"></path> |
| 126 | <path fill="#a10223" fill-rule="evenodd" |
| 127 | d="M4.721 4.721a.75.75 0 0 0 0 1.06l5.497 5.498a.75.75 0 0 0 1.06-1.06L5.783 4.72a.75.75 0 0 0-1.06 0" |
| 128 | clip-rule="evenodd"></path> |
| 129 | <path fill="#a10223" fill-rule="evenodd" |
| 130 | d="M8 1.75a6.25 6.25 0 1 0 0 12.5 6.25 6.25 0 0 0 0-12.5M.25 8a7.75 7.75 0 1 1 15.5 0A7.75 7.75 0 0 1 .25 8" |
| 131 | clip-rule="evenodd"></path> |
| 132 | </svg> |
| 133 | </div> |
| 134 | <div class="reach-subscription-message__text"> |
| 135 | <?php |
| 136 | if ( ! $is_connected && current_user_can( 'manage_options' ) ) { |
| 137 | esc_html_e( 'Your site is not connected to Reach. Connect to Reach and try again.', 'hostinger-reach' ); |
| 138 | } else { |
| 139 | esc_html_e( 'Subscription form is not available at the moment', 'hostinger-reach' ); |
| 140 | } |
| 141 | ?> |
| 142 | </div> |
| 143 | </div> |
| 144 | </form> |
| 145 | </div> |
| 146 | <?php |
| 147 | } |
| 148 | } |
| 149 |