SubscriptionFormElementorWidget.php
233 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Integrations\Elementor; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Widget_Base; |
| 7 | use Hostinger\Reach\Blocks\SubscriptionFormBlock; |
| 8 | |
| 9 | class SubscriptionFormElementorWidget extends Widget_Base { |
| 10 | |
| 11 | public const FORM_ID_PREFIX = 'elementor-hostinger-reach-form-'; |
| 12 | public const WIDGET_NAME = 'hostinger-reach'; |
| 13 | |
| 14 | public function get_name(): string { |
| 15 | return self::WIDGET_NAME; |
| 16 | } |
| 17 | |
| 18 | |
| 19 | public function get_title(): ?string { |
| 20 | return __( 'Hostinger Reach', 'hostinger-reach' ); |
| 21 | } |
| 22 | |
| 23 | public function get_icon(): string { |
| 24 | return 'eicon-envelope'; |
| 25 | } |
| 26 | |
| 27 | public function get_keywords(): array { |
| 28 | return array( |
| 29 | 'form', |
| 30 | 'forms', |
| 31 | 'reach', |
| 32 | 'contact form', |
| 33 | 'hostinger', |
| 34 | 'email', |
| 35 | 'newsletter', |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | public function get_categories(): array { |
| 40 | return array( |
| 41 | 'basic', |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | protected function register_controls(): void { |
| 46 | |
| 47 | $this->start_controls_section( |
| 48 | 'form', |
| 49 | array( |
| 50 | 'label' => esc_html__( 'Form', 'hostinger-reach' ), |
| 51 | 'tab' => Controls_Manager::TAB_CONTENT, |
| 52 | ) |
| 53 | ); |
| 54 | |
| 55 | $this->add_control( |
| 56 | 'formId', |
| 57 | array( |
| 58 | 'label' => esc_html__( 'Form ID', 'hostinger-reach' ), |
| 59 | 'type' => Controls_Manager::HIDDEN, |
| 60 | 'input_type' => 'hidden', |
| 61 | 'default' => self::FORM_ID_PREFIX . random_int( 1, PHP_INT_MAX ), |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | $form_builder_description = sprintf( |
| 66 | /* translators: %s: "Learn more" link. */ |
| 67 | esc_html__( 'Choose a Reach Form Builder template to embed instead. The Reach script loads it automatically. %s', 'hostinger-reach' ), |
| 68 | '<a href="https://www.hostinger.com/support/hostinger-reach-form-builder-overview-setup-guide/" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Learn more', 'hostinger-reach' ) . '</a>' |
| 69 | ); |
| 70 | |
| 71 | $this->add_control( |
| 72 | 'formBuilderSelector', |
| 73 | array( |
| 74 | 'type' => Controls_Manager::RAW_HTML, |
| 75 | 'raw' => '<div class="hostinger-reach-elementor-selector"></div>', |
| 76 | ) |
| 77 | ); |
| 78 | |
| 79 | $this->add_control( |
| 80 | 'formBuilderId', |
| 81 | array( |
| 82 | 'label' => esc_html__( 'Form Builder Template', 'hostinger-reach' ), |
| 83 | 'type' => Controls_Manager::HIDDEN, |
| 84 | 'default' => '', |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | $builder_mode_conditions = array( |
| 89 | 'relation' => 'or', |
| 90 | 'terms' => array( |
| 91 | array( |
| 92 | 'name' => 'formBuilderManual', |
| 93 | 'operator' => '===', |
| 94 | 'value' => 'yes', |
| 95 | ), |
| 96 | array( |
| 97 | 'name' => 'formBuilderId', |
| 98 | 'operator' => '!==', |
| 99 | 'value' => '', |
| 100 | ), |
| 101 | ), |
| 102 | ); |
| 103 | |
| 104 | $this->add_control( |
| 105 | 'formBuilderManual', |
| 106 | array( |
| 107 | 'label' => esc_html__( 'Enter template ID manually', 'hostinger-reach' ), |
| 108 | 'type' => Controls_Manager::SWITCHER, |
| 109 | 'label_on' => esc_html__( 'Yes', 'hostinger-reach' ), |
| 110 | 'label_off' => esc_html__( 'No', 'hostinger-reach' ), |
| 111 | 'return_value' => 'yes', |
| 112 | 'default' => '', |
| 113 | 'conditions' => $builder_mode_conditions, |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | $this->add_control( |
| 118 | 'formBuilderIdManual', |
| 119 | array( |
| 120 | 'label' => esc_html__( 'Form Builder Template ID', 'hostinger-reach' ), |
| 121 | 'type' => Controls_Manager::TEXT, |
| 122 | 'default' => '', |
| 123 | 'description' => $form_builder_description, |
| 124 | 'condition' => array( |
| 125 | 'formBuilderManual' => 'yes', |
| 126 | ), |
| 127 | ) |
| 128 | ); |
| 129 | |
| 130 | $no_template_conditions = array( |
| 131 | 'relation' => 'and', |
| 132 | 'terms' => array( |
| 133 | array( |
| 134 | 'name' => 'formBuilderManual', |
| 135 | 'operator' => '!==', |
| 136 | 'value' => 'yes', |
| 137 | ), |
| 138 | array( |
| 139 | 'name' => 'formBuilderId', |
| 140 | 'operator' => '===', |
| 141 | 'value' => '', |
| 142 | ), |
| 143 | ), |
| 144 | ); |
| 145 | |
| 146 | $this->add_control( |
| 147 | 'showName', |
| 148 | array( |
| 149 | 'label' => esc_html__( 'Show Name', 'hostinger-reach' ), |
| 150 | 'type' => Controls_Manager::SWITCHER, |
| 151 | 'label_on' => esc_html__( 'Yes', 'hostinger-reach' ), |
| 152 | 'label_off' => esc_html__( 'No', 'hostinger-reach' ), |
| 153 | 'return_value' => 1, |
| 154 | 'default' => 0, |
| 155 | 'conditions' => $no_template_conditions, |
| 156 | ) |
| 157 | ); |
| 158 | |
| 159 | $this->add_control( |
| 160 | 'showSurname', |
| 161 | array( |
| 162 | 'label' => esc_html__( 'Show Surname', 'hostinger-reach' ), |
| 163 | 'type' => Controls_Manager::SWITCHER, |
| 164 | 'label_on' => esc_html__( 'Yes', 'hostinger-reach' ), |
| 165 | 'label_off' => esc_html__( 'No', 'hostinger-reach' ), |
| 166 | 'return_value' => 1, |
| 167 | 'default' => 0, |
| 168 | 'conditions' => $no_template_conditions, |
| 169 | ) |
| 170 | ); |
| 171 | |
| 172 | $this->end_controls_section(); |
| 173 | } |
| 174 | |
| 175 | protected function render(): void { |
| 176 | $settings = $this->get_settings_for_display(); |
| 177 | |
| 178 | if ( ( $settings['formBuilderManual'] ?? '' ) === 'yes' && ! empty( $settings['formBuilderIdManual'] ) ) { |
| 179 | $settings['formBuilderId'] = $settings['formBuilderIdManual']; |
| 180 | } |
| 181 | |
| 182 | SubscriptionFormBlock::render_block_html( $settings, ElementorIntegration::INTEGRATION_NAME ); |
| 183 | } |
| 184 | |
| 185 | protected function content_template(): void { |
| 186 | ?> |
| 187 | <# var reachFormBuilderId = 'yes' === settings.formBuilderManual ? settings.formBuilderIdManual : settings.formBuilderId; #> |
| 188 | <# reachFormBuilderId = reachFormBuilderId && /^[a-zA-Z0-9-]+$/.test( reachFormBuilderId ) ? reachFormBuilderId : ''; #> |
| 189 | <# if ( reachFormBuilderId ) { #> |
| 190 | <div data-reach-form="{{ reachFormBuilderId }}"></div> |
| 191 | <# } else { #> |
| 192 | <div class="hostinger-reach-block-subscription-form-wrapper"> |
| 193 | <form id="{{{ settings.formId }}}" class="hostinger-reach-block-subscription-form"> |
| 194 | <input type="hidden" name="id" value="{{{ settings.formId }}}"> |
| 195 | <input type="hidden" name="metadata.plugin" value="elementor"> |
| 196 | |
| 197 | <div class="hostinger-reach-block-form-field"> |
| 198 | <label |
| 199 | for="{{{ settings.formId }}}-email"><?php esc_html_e( 'Email', 'hostinger-reach' ); ?> |
| 200 | <span class="required">*</span></label> |
| 201 | <input type="email" id="{{{ settings.formId }}}-email" name="email" required> |
| 202 | </div> |
| 203 | |
| 204 | <# if ( settings.showName ) { #> |
| 205 | <div class="hostinger-reach-block-form-field"> |
| 206 | <label |
| 207 | for="{{{ settings.formId }}}-name"><?php esc_html_e( 'Name', 'hostinger-reach' ); ?></label> |
| 208 | <input type="text" id="{{{ settings.formId }}}-name" name="name"> |
| 209 | </div> |
| 210 | <# } #> |
| 211 | |
| 212 | <# if ( settings.showSurname ) { #> |
| 213 | <div class="hostinger-reach-block-form-field"> |
| 214 | <label |
| 215 | for="{{{ settings.formId }}}-surname"><?php esc_html_e( 'Surname', 'hostinger-reach' ); ?></label> |
| 216 | <input type="text" id="{{{ settings.formId }}}-surname" name="surname"> |
| 217 | </div> |
| 218 | <# } #> |
| 219 | |
| 220 | <button |
| 221 | type="submit" |
| 222 | class="hostinger-reach-block-submit wp-block-button__link has-light-color has-color-3-background-color has-text-color has-background has-link-color has-medium-font-size wp-element-button"> |
| 223 | <?php esc_html_e( 'Subscribe', 'hostinger-reach' ); ?> |
| 224 | </button> |
| 225 | |
| 226 | <div class="reach-subscription-message" style="display: none;"></div> |
| 227 | </form> |
| 228 | </div> |
| 229 | <# } #> |
| 230 | <?php |
| 231 | } |
| 232 | } |
| 233 |