address-markup.php
1 month ago
base.php
2 months ago
checkbox-markup.php
1 year ago
countries.json
1 year ago
dropdown-markup.php
1 month ago
email-markup.php
2 months ago
gdpr-markup.php
4 months ago
inlinebutton-markup.php
1 month ago
input-markup.php
4 months ago
multichoice-markup.php
1 month ago
number-markup.php
4 months ago
payment-markup.php
2 months ago
phone-markup.php
1 month ago
textarea-markup.php
1 month ago
url-markup.php
4 months ago
inlinebutton-markup.php
258 lines
| 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\Generate_Form_Markup; |
| 12 | use SRFM\Inc\Helper; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Sureforms Inline Button Markup Class. |
| 20 | * |
| 21 | * @since 0.0.2 |
| 22 | */ |
| 23 | class Inlinebutton_Markup extends Base { |
| 24 | /** |
| 25 | * Text displayed on the button. |
| 26 | * |
| 27 | * @var string |
| 28 | * @since 0.0.2 |
| 29 | */ |
| 30 | protected $button_text; |
| 31 | |
| 32 | /** |
| 33 | * Button style inherited from the theme. |
| 34 | * |
| 35 | * @var string |
| 36 | * @since 0.0.2 |
| 37 | */ |
| 38 | protected $btn_from_theme; |
| 39 | |
| 40 | /** |
| 41 | * Used as a flag which decides whether page break is added. |
| 42 | * |
| 43 | * @var bool |
| 44 | * @since 0.0.2 |
| 45 | */ |
| 46 | protected $is_page_break; |
| 47 | |
| 48 | /** |
| 49 | * Version of reCAPTCHA to use. |
| 50 | * |
| 51 | * @var string |
| 52 | * @since 0.0.2 |
| 53 | */ |
| 54 | protected $recaptcha_version; |
| 55 | |
| 56 | /** |
| 57 | * Site key for Google reCAPTCHA. |
| 58 | * |
| 59 | * @var string |
| 60 | * @since 0.0.2 |
| 61 | */ |
| 62 | protected $google_captcha_site_key; |
| 63 | |
| 64 | /** |
| 65 | * Global setting options for security settings. |
| 66 | * |
| 67 | * @var array<mixed>|mixed |
| 68 | * @since 0.0.2 |
| 69 | */ |
| 70 | protected $global_setting_options; |
| 71 | |
| 72 | /** |
| 73 | * Flag indicating if padding should be added to the button. |
| 74 | * |
| 75 | * @var bool |
| 76 | * @since 0.0.2 |
| 77 | */ |
| 78 | protected $add_button_padding; |
| 79 | |
| 80 | /** |
| 81 | * Security type. |
| 82 | * |
| 83 | * @var string |
| 84 | * @since 0.0.5 |
| 85 | */ |
| 86 | protected $captcha_security_type; |
| 87 | |
| 88 | /** |
| 89 | * Cloudflare Turnstile site key. |
| 90 | * |
| 91 | * @var string |
| 92 | * @since 0.0.5 |
| 93 | */ |
| 94 | protected $cf_turnstile_site_key; |
| 95 | |
| 96 | /** |
| 97 | * Cloudflare Turnstile appearance mode |
| 98 | * |
| 99 | * @var string |
| 100 | * @since 0.0.5 |
| 101 | */ |
| 102 | protected $cf_appearance_mode; |
| 103 | |
| 104 | /** |
| 105 | * Security - hCaptcha site key. |
| 106 | * |
| 107 | * @var string |
| 108 | * @since 0.0.5 |
| 109 | */ |
| 110 | protected $hcaptcha_site_key; |
| 111 | |
| 112 | /** |
| 113 | * Initialize the properties based on block attributes. |
| 114 | * |
| 115 | * @param array<mixed> $attributes Block attributes. |
| 116 | * @since 0.0.2 |
| 117 | */ |
| 118 | public function __construct( $attributes ) { |
| 119 | $this->set_properties( $attributes ); |
| 120 | $this->slug = 'inline-button'; |
| 121 | $this->button_text = $attributes['buttonText'] ?? ''; |
| 122 | $this->btn_from_theme = Helper::get_meta_value( $this->form_id, '_srfm_inherit_theme_button' ); |
| 123 | $page_break_settings = Helper::has_pro() ? get_post_meta( (int) $this->form_id, '_srfm_page_break_settings', true ) : []; |
| 124 | $page_break_settings = is_array( $page_break_settings ) ? $page_break_settings : []; |
| 125 | $this->is_page_break = $page_break_settings['is_page_break'] ?? false; |
| 126 | $this->captcha_security_type = Helper::get_meta_value( $this->form_id, '_srfm_captcha_security_type' ); |
| 127 | $this->recaptcha_version = Helper::get_meta_value( $this->form_id, '_srfm_form_recaptcha' ); |
| 128 | $this->google_captcha_site_key = ''; |
| 129 | $this->global_setting_options = []; |
| 130 | if ( 'none' !== $this->captcha_security_type ) { |
| 131 | $this->global_setting_options = get_option( 'srfm_security_settings_options' ); |
| 132 | } |
| 133 | |
| 134 | if ( is_array( $this->global_setting_options ) ) { |
| 135 | switch ( $this->recaptcha_version ) { |
| 136 | case 'v2-checkbox': |
| 137 | $this->google_captcha_site_key = $this->global_setting_options['srfm_v2_checkbox_site_key'] ?? ''; |
| 138 | break; |
| 139 | case 'v2-invisible': |
| 140 | $this->google_captcha_site_key = $this->global_setting_options['srfm_v2_invisible_site_key'] ?? ''; |
| 141 | break; |
| 142 | case 'v3-reCAPTCHA': |
| 143 | $this->google_captcha_site_key = $this->global_setting_options['srfm_v3_site_key'] ?? ''; |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | if ( 'cf-turnstile' === $this->captcha_security_type ) { |
| 149 | $this->cf_turnstile_site_key = $this->global_setting_options['srfm_cf_turnstile_site_key'] ?? ''; |
| 150 | $this->cf_appearance_mode = $this->global_setting_options['srfm_cf_appearance_mode'] ?? 'auto'; |
| 151 | } |
| 152 | if ( 'hcaptcha' === $this->captcha_security_type ) { |
| 153 | $this->hcaptcha_site_key = $this->global_setting_options['srfm_hcaptcha_site_key'] ?? ''; |
| 154 | } |
| 155 | } |
| 156 | $theme_name = wp_get_theme()->get( 'Name' ); |
| 157 | $this->add_button_padding = true; |
| 158 | if ( 'Astra' === $theme_name || 'Blocksy' === $theme_name ) { |
| 159 | $this->add_button_padding = false; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Render inline button markup |
| 165 | * |
| 166 | * @since 0.0.2 |
| 167 | * @return string|bool|void |
| 168 | */ |
| 169 | public function markup() { |
| 170 | $container_hidden_class = 'g-recaptcha' === $this->captcha_security_type && ( 'v3-reCAPTCHA' === $this->recaptcha_version || 'v2-invisible' === $this->recaptcha_version ) ? 'srfm-display-none' : ''; |
| 171 | ob_start(); ?> |
| 172 | <?php if ( ! $this->is_page_break ) { ?> |
| 173 | <?php if ( $this->captcha_security_type && 'none' !== $this->captcha_security_type ) { ?> |
| 174 | <div class="srfm-captcha-container <?php echo esc_attr( $container_hidden_class ); ?>"> |
| 175 | <?php if ( 'g-recaptcha' === $this->captcha_security_type && 'v2-checkbox' === $this->recaptcha_version ) { ?> |
| 176 | <div class='g-recaptcha' data-callback='onSuccess' data-error-callback='onGCaptchaV2CheckBoxError' recaptcha-type='<?php echo esc_attr( $this->recaptcha_version ); ?>' data-sitekey='<?php echo esc_attr( strval( $this->google_captcha_site_key ) ); ?>'></div> |
| 177 | <?php } ?> |
| 178 | <?php if ( 'cf-turnstile' === $this->captcha_security_type && $this->cf_turnstile_site_key ) { ?> |
| 179 | <div id='srfm-cf-sitekey' class='cf-turnstile' data-callback='onSuccess' data-error-callback='onTurnstileError' data-theme='<?php echo esc_attr( strval( $this->cf_appearance_mode ) ); ?>' data-sitekey='<?php echo esc_attr( strval( $this->cf_turnstile_site_key ) ); ?>'></div> |
| 180 | <?php } ?> |
| 181 | <?php if ( 'hcaptcha' === $this->captcha_security_type && $this->hcaptcha_site_key ) { ?> |
| 182 | <div id='srfm-hcaptcha-sitekey' data-callback='onSuccess' data-error-callback='onHCaptchaError' class='h-captcha' data-sitekey='<?php echo esc_attr( strval( $this->hcaptcha_site_key ) ); ?>'></div> |
| 183 | <?php } ?> |
| 184 | <div class="srfm-validation-error" id="captcha-error" style="display: none;"><?php echo esc_html__( 'Please verify that you are not a robot.', 'sureforms' ); ?></div> |
| 185 | </div> |
| 186 | <?php } ?> |
| 187 | <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' : '' ); ?>"> |
| 188 | <?php |
| 189 | if ( 'g-recaptcha' === $this->captcha_security_type ) { |
| 190 | |
| 191 | if ( empty( $this->google_captcha_site_key ) ) { |
| 192 | Helper::render_missing_sitekey_error( 'Google reCAPTCHA' ); |
| 193 | } else { |
| 194 | if ( 'v3-reCAPTCHA' === $this->recaptcha_version ) { |
| 195 | wp_enqueue_script( 'srfm-google-recaptchaV3', 'https://www.google.com/recaptcha/api.js?render=' . $this->google_captcha_site_key, [], null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- This is a third-party script, and specifying a version may lead to caching issues. Using null ensures the latest version is always loaded. |
| 196 | } |
| 197 | |
| 198 | if ( 'v2-checkbox' === $this->recaptcha_version ) { |
| 199 | wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', [], SRFM_VER, true ); |
| 200 | } |
| 201 | |
| 202 | if ( 'v2-invisible' === $this->recaptcha_version ) { |
| 203 | wp_enqueue_script( 'google-recaptcha-invisible', 'https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit', [ SRFM_SLUG . '-form-submit' ], SRFM_VER, true ); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if ( 'cf-turnstile' === $this->captcha_security_type ) { |
| 209 | if ( ! empty( $this->cf_turnstile_site_key ) ) { |
| 210 | // Cloudflare Turnstile script. |
| 211 | // phpcs:disable WordPress.WP.EnqueuedResourceParameters.MissingVersion, PluginCheck.CodeAnalysis.EnqueuedResourceOffloading.OffloadedContent -- Cloudflare Turnstile must be loaded from Cloudflare's servers for token verification; the version is controlled by Cloudflare. |
| 212 | wp_enqueue_script( |
| 213 | SRFM_SLUG . '-cf-turnstile', |
| 214 | 'https://challenges.cloudflare.com/turnstile/v0/api.js', |
| 215 | [], |
| 216 | null, |
| 217 | [ |
| 218 | false, |
| 219 | 'defer' => true, |
| 220 | ] |
| 221 | ); |
| 222 | // phpcs:enable WordPress.WP.EnqueuedResourceParameters.MissingVersion, PluginCheck.CodeAnalysis.EnqueuedResourceOffloading.OffloadedContent |
| 223 | } else { |
| 224 | Helper::render_missing_sitekey_error( 'Cloudflare Turnstile' ); |
| 225 | } |
| 226 | } |
| 227 | if ( 'hcaptcha' === $this->captcha_security_type ) { |
| 228 | if ( ! empty( $this->hcaptcha_site_key ) ) { |
| 229 | wp_enqueue_script( 'hcaptcha', 'https://js.hcaptcha.com/1/api.js', [], null, [ 'strategy' => 'defer' ] ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion, PluginCheck.CodeAnalysis.EnqueuedResourceOffloading.OffloadedContent -- hCaptcha must be loaded from hCaptcha's servers for token verification; the version is controlled by hCaptcha. |
| 230 | } else { |
| 231 | Helper::render_missing_sitekey_error( 'HCaptcha' ); |
| 232 | } |
| 233 | } |
| 234 | $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, Generate_Form_Markup::get_current_block_attrs() ); |
| 235 | |
| 236 | $button_style = $this->btn_from_theme ? '' : ' font-family: inherit; font-weight: var(--wp--custom--font-weight--medium); line-height: normal;'; |
| 237 | $button_style .= 'width:100%;'; |
| 238 | |
| 239 | $button_attributes = ''; |
| 240 | if ( 'g-recaptcha' === $this->captcha_security_type && ( 'v2-invisible' === $this->recaptcha_version || 'v3-reCAPTCHA' === $this->recaptcha_version ) ) { |
| 241 | $button_attributes = 'recaptcha-type="' . esc_attr( $this->recaptcha_version ) . '" data-sitekey="' . esc_attr( $this->google_captcha_site_key ) . '" data-callback="recaptchaCallback"'; |
| 242 | } |
| 243 | ?> |
| 244 | <button style="<?php echo esc_attr( $button_style ); ?>" id="srfm-submit-btn" class="<?php echo esc_attr( Helper::join_strings( $srfm_custom_button_classes ) ); ?>" <?php echo $button_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Individual attribute values are escaped with esc_attr() at build time above. ?>> |
| 245 | <div class="srfm-submit-wrap"> |
| 246 | <?php echo esc_html( $this->button_text ); ?> |
| 247 | <div class="srfm-loader"></div> |
| 248 | </div> |
| 249 | </button> |
| 250 | <div class="srfm-error-wrap"></div> |
| 251 | </div> |
| 252 | <?php |
| 253 | } |
| 254 | return ob_get_clean(); |
| 255 | } |
| 256 | |
| 257 | } |
| 258 |