inlinebutton-markup.php
| 1 | <?php |
| 2 | /** |
| 3 | * Sureforms Inline Button Markup Class file. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | * @since 0.0.1 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Fields; |
| 10 | |
| 11 | use SRFM\Inc\Helper; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Sureforms Inline Button Markup Class. |
| 19 | * |
| 20 | * @since 0.0.2 |
| 21 | */ |
| 22 | class Inlinebutton_Markup extends Base { |
| 23 | /** |
| 24 | * Text displayed on the button. |
| 25 | * |
| 26 | * @var string |
| 27 | * @since 0.0.2 |
| 28 | */ |
| 29 | protected $button_text; |
| 30 | |
| 31 | /** |
| 32 | * Button style inherited from the theme. |
| 33 | * |
| 34 | * @var string |
| 35 | * @since 0.0.2 |
| 36 | */ |
| 37 | protected $btn_from_theme; |
| 38 | |
| 39 | /** |
| 40 | * Used as a flag which decides whether page break is added. |
| 41 | * |
| 42 | * @var bool |
| 43 | * @since 0.0.2 |
| 44 | */ |
| 45 | protected $is_page_break; |
| 46 | |
| 47 | /** |
| 48 | * Version of reCAPTCHA to use. |
| 49 | * |
| 50 | * @var string |
| 51 | * @since 0.0.2 |
| 52 | */ |
| 53 | protected $recaptcha_version; |
| 54 | |
| 55 | /** |
| 56 | * Site key for Google reCAPTCHA. |
| 57 | * |
| 58 | * @var string |
| 59 | * @since 0.0.2 |
| 60 | */ |
| 61 | protected $google_captcha_site_key; |
| 62 | |
| 63 | /** |
| 64 | * Global setting options for security settings. |
| 65 | * |
| 66 | * @var array<mixed>|mixed |
| 67 | * @since 0.0.2 |
| 68 | */ |
| 69 | protected $global_setting_options; |
| 70 | |
| 71 | /** |
| 72 | * Flag indicating if padding should be added to the button. |
| 73 | * |
| 74 | * @var bool |
| 75 | * @since 0.0.2 |
| 76 | */ |
| 77 | protected $add_button_padding; |
| 78 | |
| 79 | /** |
| 80 | * Security type. |
| 81 | * |
| 82 | * @var string |
| 83 | * @since 0.0.5 |
| 84 | */ |
| 85 | protected $captcha_security_type; |
| 86 | |
| 87 | /** |
| 88 | * Cloudflare Turnstile site key. |
| 89 | * |
| 90 | * @var string |
| 91 | * @since 0.0.5 |
| 92 | */ |
| 93 | protected $cf_turnstile_site_key; |
| 94 | |
| 95 | /** |
| 96 | * Cloudflare Turnstile appearance mode |
| 97 | * |
| 98 | * @var string |
| 99 | * @since 0.0.5 |
| 100 | */ |
| 101 | protected $cf_appearance_mode; |
| 102 | |
| 103 | /** |
| 104 | * Security - hCaptcha site key. |
| 105 | * |
| 106 | * @var string |
| 107 | * @since 0.0.5 |
| 108 | */ |
| 109 | protected $hcaptcha_site_key; |
| 110 | |
| 111 | /** |
| 112 | * Initialize the properties based on block attributes. |
| 113 | * |
| 114 | * @param array<mixed> $attributes Block attributes. |
| 115 | * @since 0.0.2 |
| 116 | */ |
| 117 | public function __construct( $attributes ) { |
| 118 | $this->set_properties( $attributes ); |
| 119 | $this->slug = 'inline-button'; |
| 120 | $this->button_text = $attributes['buttonText'] ?? ''; |
| 121 | $this->btn_from_theme = Helper::get_meta_value( $this->form_id, '_srfm_inherit_theme_button' ); |
| 122 | $page_break_settings = defined( 'SRFM_PRO_VER' ) ? get_post_meta( (int) $this->form_id, '_srfm_page_break_settings', true ) : []; |
| 123 | $page_break_settings = is_array( $page_break_settings ) ? $page_break_settings : []; |
| 124 | $this->is_page_break = $page_break_settings['is_page_break'] ?? false; |
| 125 | $this->captcha_security_type = Helper::get_meta_value( $this->form_id, '_srfm_captcha_security_type' ); |
| 126 | $this->recaptcha_version = Helper::get_meta_value( $this->form_id, '_srfm_form_recaptcha' ); |
| 127 | $this->google_captcha_site_key = ''; |
| 128 | $this->global_setting_options = []; |
| 129 | if ( 'none' !== $this->captcha_security_type ) { |
| 130 | $this->global_setting_options = get_option( 'srfm_security_settings_options' ); |
| 131 | } |
| 132 | |
| 133 | if ( is_array( $this->global_setting_options ) ) { |
| 134 | switch ( $this->recaptcha_version ) { |
| 135 | case 'v2-checkbox': |
| 136 | $this->google_captcha_site_key = $this->global_setting_options['srfm_v2_checkbox_site_key'] ?? ''; |
| 137 | break; |
| 138 | case 'v2-invisible': |
| 139 | $this->google_captcha_site_key = $this->global_setting_options['srfm_v2_invisible_site_key'] ?? ''; |
| 140 | break; |
| 141 | case 'v3-reCAPTCHA': |
| 142 | $this->google_captcha_site_key = $this->global_setting_options['srfm_v3_site_key'] ?? ''; |
| 143 | break; |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | if ( 'cf-turnstile' === $this->captcha_security_type ) { |
| 148 | $this->cf_turnstile_site_key = $this->global_setting_options['srfm_cf_turnstile_site_key'] ?? ''; |
| 149 | $this->cf_appearance_mode = $this->global_setting_options['srfm_cf_appearance_mode'] ?? 'auto'; |
| 150 | } |
| 151 | if ( 'hcaptcha' === $this->captcha_security_type ) { |
| 152 | $this->hcaptcha_site_key = $this->global_setting_options['srfm_hcaptcha_site_key'] ?? ''; |
| 153 | } |
| 154 | } |
| 155 | $theme_name = wp_get_theme()->get( 'Name' ); |
| 156 | $this->add_button_padding = true; |
| 157 | if ( 'Astra' === $theme_name || 'Blocksy' === $theme_name ) { |
| 158 | $this->add_button_padding = false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Render inline button markup |
| 164 | * |
| 165 | * @since 0.0.2 |
| 166 | * @return string|bool|void |
| 167 | */ |
| 168 | public function markup() { |
| 169 | ob_start(); ?> |
| 170 | <?php if ( ! $this->is_page_break ) { ?> |
| 171 | <?php if ( $this->captcha_security_type && 'none' !== $this->captcha_security_type ) { ?> |
| 172 | <div class="srfm-captcha-container <?php echo esc_attr( 'v3-reCAPTCHA' === $this->recaptcha_version || 'v2-invisible' === $this->recaptcha_version ? 'srfm-display-none' : '' ); ?>"> |
| 173 | <?php if ( 'g-recaptcha' === $this->captcha_security_type && 'v2-checkbox' === $this->recaptcha_version ) { ?> |
| 174 | <?php echo "<div class='g-recaptcha' data-callback='onSuccess' recaptcha-type='" . esc_attr( $this->recaptcha_version ) . "' data-sitekey='" . esc_attr( strval( $this->google_captcha_site_key ) ) . "'></div>"; ?> |
| 175 | <?php } ?> |
| 176 | <?php if ( 'cf-turnstile' === $this->captcha_security_type && $this->cf_turnstile_site_key ) { ?> |
| 177 | <?php echo "<div id='srfm-cf-sitekey' class='cf-turnstile' data-callback='onSuccess' data-theme='" . esc_attr( strval( $this->cf_appearance_mode ) ) . "' data-sitekey='" . esc_attr( strval( $this->cf_turnstile_site_key ) ) . "'></div>"; ?> |
| 178 | <?php } ?> |
| 179 | <?php if ( 'hcaptcha' === $this->captcha_security_type && $this->hcaptcha_site_key ) { ?> |
| 180 | <?php echo "<div id='srfm-hcaptcha-sitekey' data-callback='onSuccess' class='h-captcha' data-sitekey='" . esc_attr( strval( $this->hcaptcha_site_key ) ) . "'></div>"; ?> |
| 181 | <?php } ?> |
| 182 | <div class="srfm-validation-error" id="captcha-error" style="display: none;"><?php echo esc_attr__( 'Please verify that you are not a robot.', 'sureforms' ); ?></div> |
| 183 | </div> |
| 184 | <?php } ?> |
| 185 | <div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" class="<?php echo esc_attr( $this->class_name ); ?> <?php echo esc_attr( $this->conditional_class ); ?> srf-<?php echo esc_attr( $this->slug ); ?>-<?php echo esc_attr( $this->block_id ); ?>-block<?php echo esc_attr( $this->block_width ); ?> srfm-block srfm-custom-button-ctn <?php echo esc_attr( '1' === $this->btn_from_theme ? 'wp-block-button' : '' ); ?>"> |
| 186 | <?php |
| 187 | if ( 'g-recaptcha' === $this->captcha_security_type ) { |
| 188 | if ( 'v3-reCAPTCHA' === $this->recaptcha_version ) { |
| 189 | wp_enqueue_script( 'srfm-google-recaptchaV3', 'https://www.google.com/recaptcha/api.js', [], SRFM_VER, true ); |
| 190 | } |
| 191 | |
| 192 | if ( 'v2-checkbox' === $this->recaptcha_version ) { |
| 193 | wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', [], SRFM_VER, true ); |
| 194 | } |
| 195 | |
| 196 | if ( 'v2-invisible' === $this->recaptcha_version ) { |
| 197 | wp_enqueue_script( 'google-recaptcha-invisible', 'https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit', [ SRFM_SLUG . '-form-submit' ], SRFM_VER, true ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if ( 'cf-turnstile' === $this->captcha_security_type ) { |
| 202 | // Cloudflare Turnstile script. |
| 203 | wp_enqueue_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 204 | SRFM_SLUG . '-cf-turnstile', |
| 205 | 'https://challenges.cloudflare.com/turnstile/v0/api.js', |
| 206 | [], |
| 207 | null, |
| 208 | [ |
| 209 | false, |
| 210 | 'defer' => true, |
| 211 | ] |
| 212 | ); |
| 213 | } |
| 214 | if ( 'hcaptcha' === $this->captcha_security_type ) { |
| 215 | wp_enqueue_script( 'hcaptcha', 'https://js.hcaptcha.com/1/api.js', [], null, [ 'strategy' => 'defer' ] ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 216 | } |
| 217 | $srfm_custom_button_classes = apply_filters( 'srfm_add_button_classes', [ 'v2-invisible' === $this->recaptcha_version || 'v3-reCAPTCHA' === $this->recaptcha_version ? 'g-recaptcha ' : '', '1' === $this->btn_from_theme ? 'wp-block-button__link' : 'srfm-button srfm-submit-button srfm-btn-frontend srfm-custom-button' ], $this->form_id ); |
| 218 | ?> |
| 219 | <button style="<?php echo $this->btn_from_theme ? '' : ' font-family: inherit; font-weight: var(--wp--custom--font-weight--medium); line-height: normal;'; ?>width:100%;" id="srfm-submit-btn" class="<?php echo esc_attr( Helper::join_strings( $srfm_custom_button_classes ) ); ?>" <?php echo 'v2-invisible' === $this->recaptcha_version || 'v3-reCAPTCHA' === $this->recaptcha_version ? esc_attr( 'recaptcha-type=' . $this->recaptcha_version . ' data-sitekey=' . $this->google_captcha_site_key ) . ' data-callback="recaptchaCallback"' : ''; ?>> |
| 220 | <div class="srfm-submit-wrap"> |
| 221 | <?php echo esc_html( $this->button_text ); ?> |
| 222 | <div class="srfm-loader"></div> |
| 223 | </div> |
| 224 | </button> |
| 225 | <div class="srfm-error-wrap"></div> |
| 226 | </div> |
| 227 | <?php |
| 228 | } |
| 229 | return ob_get_clean(); |
| 230 | } |
| 231 | |
| 232 | } |
| 233 |