PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / trunk
Hostinger Reach – AI-Powered Email Marketing for WordPress vtrunk
1.8.0 1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / src / Blocks / SubscriptionFormBlock.php
hostinger-reach / src / Blocks Last commit date
Block.php 1 day ago SubscriptionFormBlock.php 1 day ago
SubscriptionFormBlock.php
196 lines
1 <?php
2
3 namespace Hostinger\Reach\Blocks;
4
5
6 use Hostinger\Reach\Api\Handlers\ReachApiHandler;
7 use Hostinger\Reach\Api\ResourceIdManager;
8 use Hostinger\Reach\Functions;
9 use Hostinger\Reach\Integrations\Reach\ReachFormIntegration;
10 use Hostinger\Reach\Setup\Assets;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 die;
14 }
15
16 class SubscriptionFormBlock extends Block {
17 public const ADD_BLOCK_QUERY_ARG = 'hostinger_reach_add_block';
18 public const ADD_BLOCK_NONCE = 'hostinger_reach_add_block';
19
20 public string $name = 'subscription';
21 private ReachApiHandler $reach_api_handler;
22
23 public function __construct( Assets $assets, Functions $functions, ReachApiHandler $reach_api_handler ) {
24 parent::__construct( $assets, $functions );
25 $this->reach_api_handler = $reach_api_handler;
26 }
27
28 protected function get_block_editor_data(): array {
29 $resource_id = $this->reach_api_handler->get_resource_id();
30
31 if ( $resource_id === ResourceIdManager::NON_EXISTENT_RESOURCE_ID ) {
32 $resource_id = '';
33 }
34
35 return array_merge(
36 parent::get_block_editor_data(),
37 array(
38 'resource_id' => $resource_id,
39 'reach_domain' => $this->reach_api_handler->get_reach_domain(),
40 'domain' => $this->functions->get_host_info(),
41 )
42 );
43 }
44
45 public function data(): array {
46 return array(
47 'endpoint' => esc_url_raw( rest_url() ) . 'hostinger-reach/v1/contact',
48 'nonce' => wp_create_nonce( 'wp_rest' ),
49 'translations' => array(
50 'thanks' => __( 'Thanks for subscribing.', 'hostinger-reach' ),
51 'error' => __( 'Something went wrong. Please try again.', 'hostinger-reach' ),
52 ),
53 );
54 }
55
56 public function autoloader(): void {
57 if ( ! is_admin() || ! isset( $_GET[ self::ADD_BLOCK_QUERY_ARG ] ) ) {
58 return;
59 }
60
61 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
62 if ( ! wp_verify_nonce( $nonce, self::ADD_BLOCK_NONCE ) ) {
63 return;
64 }
65
66 if ( $this->functions->block_file_exists( "$this->name-autoloader.js" ) === false ) {
67 return;
68 }
69
70 $form_builder_id = sanitize_text_field( wp_unslash( $_GET[ self::ADD_BLOCK_QUERY_ARG ] ) );
71
72 if ( $form_builder_id === '1' ) {
73 $form_builder_id = '';
74 }
75
76 $handler = parent::get_block_name() . '-autoloader';
77
78 wp_enqueue_script(
79 $handler,
80 $this->functions->get_blocks_url() . "$this->name-autoloader.js",
81 array( parent::get_block_name() . '-editor' ),
82 filemtime( $this->functions->get_block_file_name( "$this->name-autoloader.js" ) ),
83 array( 'in_footer' => true )
84 );
85
86 wp_localize_script(
87 $handler,
88 'hostinger_reach_autoloader_data',
89 array(
90 'formBuilderId' => $form_builder_id,
91 )
92 );
93 }
94
95 public function render( array $attributes ): bool|string {
96 ob_start();
97 $is_connected = $this->reach_api_handler->is_connected();
98 $this->render_block_html( $attributes, null, $is_connected );
99
100 return ob_get_clean();
101 }
102
103 public static function render_block_html( array $attributes, ?string $plugin = null, bool $is_connected = true ): void {
104 $form_builder_id = $attributes['formBuilderId'] ?? '';
105 if ( ! empty( $form_builder_id ) ) {
106 printf( '<div data-reach-form="%s"></div>', esc_attr( $form_builder_id ) );
107
108 return;
109 }
110
111 $form_id = $attributes['formId'] ?? '';
112 $show_name = $attributes['showName'] ?? false;
113 $show_surname = $attributes['showSurname'] ?? false;
114 $contact_list = $attributes['contactList'] ?? '';
115 $tags = $attributes['tags'] ?? array();
116 $layout = $attributes['layout'] ?? 'default';
117 $is_inline = $layout === 'inline';
118 $plugin = $plugin ?? ReachFormIntegration::INTEGRATION_NAME;
119 ?>
120 <div class="hostinger-reach-block-subscription-form-wrapper">
121
122 <form id="<?php echo esc_attr( $form_id ); ?>" class="hostinger-reach-block-subscription-form">
123 <input type="hidden" name="group" value="<?php echo esc_attr( $contact_list ); ?>">
124 <input type="hidden" name="id" value="<?php echo esc_attr( $form_id ); ?>">
125 <input type="hidden" name="tags" value="<?php echo esc_attr( implode( ',', $tags ) ); ?>">
126 <input type="hidden" name="metadata.plugin" value="<?php echo esc_attr( $plugin ); ?>">
127
128 <div
129 class="hostinger-reach-block-form-fields <?php echo esc_attr( $is_inline ? 'hostinger-reach-block-form-fields--inline' : '' ); ?>">
130 <div class="hostinger-reach-block-form-field">
131 <label
132 for="<?php echo esc_attr( $form_id ); ?>-email"><?php esc_html_e( 'Email', 'hostinger-reach' ); ?>
133 <span class="required">*</span></label>
134 <input type="email" id="<?php echo esc_attr( $form_id ); ?>-email" name="email" required>
135 </div>
136
137 <?php if ( $show_name ) : ?>
138 <div class="hostinger-reach-block-form-field">
139 <label
140 for="<?php echo esc_attr( $form_id ); ?>-name"><?php esc_html_e( 'Name', 'hostinger-reach' ); ?></label>
141 <input type="text" id="<?php echo esc_attr( $form_id ); ?>-name" name="name">
142 </div>
143 <?php endif; ?>
144
145 <?php if ( $show_surname ) : ?>
146 <div class="hostinger-reach-block-form-field">
147 <label
148 for="<?php echo esc_attr( $form_id ); ?>-surname"><?php esc_html_e( 'Surname', 'hostinger-reach' ); ?></label>
149 <input type="text" id="<?php echo esc_attr( $form_id ); ?>-surname" name="surname">
150 </div>
151 <?php endif; ?>
152
153 <button
154 type="submit"
155 class="hostinger-reach-block-submit has-light-color has-color-3-background-color has-text-color has-background has-link-color">
156 <?php esc_html_e( 'Subscribe', 'hostinger-reach' ); ?>
157 </button>
158 </div>
159
160 <div class="reach-subscription-message" style="display: none;">
161 <div class="reach-subscription-message__icon-success" style="display: none;">
162 <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
163 <path
164 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"
165 fill="#18181A"/>
166 </svg>
167 </div>
168 <div class="reach-subscription-message__icon-error" style="display: none;">
169 <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
170 <path fill="#a10223" fill-rule="evenodd"
171 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"
172 clip-rule="evenodd"></path>
173 <path fill="#a10223" fill-rule="evenodd"
174 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"
175 clip-rule="evenodd"></path>
176 <path fill="#a10223" fill-rule="evenodd"
177 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"
178 clip-rule="evenodd"></path>
179 </svg>
180 </div>
181 <div class="reach-subscription-message__text">
182 <?php
183 if ( ! $is_connected && current_user_can( 'manage_options' ) ) {
184 esc_html_e( 'Your site is not connected to Reach. Connect to Reach and try again.', 'hostinger-reach' );
185 } else {
186 esc_html_e( 'Subscription form is not available at the moment', 'hostinger-reach' );
187 }
188 ?>
189 </div>
190 </div>
191 </form>
192 </div>
193 <?php
194 }
195 }
196