| 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 |
$container_hidden_class = 'g-recaptcha' === $this->captcha_security_type && ( 'v3-reCAPTCHA' === $this->recaptcha_version || 'v2-invisible' === $this->recaptcha_version ) ? 'srfm-display-none' : ''; |
| 170 |
ob_start(); ?> |
| 171 |
<?php if ( ! $this->is_page_break ) { ?> |
| 172 |
<?php if ( $this->captcha_security_type && 'none' !== $this->captcha_security_type ) { ?> |
| 173 |
<div class="srfm-captcha-container <?php echo esc_attr( $container_hidden_class ); ?>"> |
| 174 |
<?php if ( 'g-recaptcha' === $this->captcha_security_type && 'v2-checkbox' === $this->recaptcha_version ) { ?> |
| 175 |
<?php echo "<div class='g-recaptcha' data-callback='onSuccess' data-error-callback='onGCaptchaV2CheckBoxError' recaptcha-type='" . esc_attr( $this->recaptcha_version ) . "' data-sitekey='" . esc_attr( strval( $this->google_captcha_site_key ) ) . "'></div>"; ?> |
| 176 |
<?php } ?> |
| 177 |
<?php if ( 'cf-turnstile' === $this->captcha_security_type && $this->cf_turnstile_site_key ) { ?> |
| 178 |
<?php echo "<div id='srfm-cf-sitekey' class='cf-turnstile' data-callback='onSuccess' data-error-callback='onTurnstileError' data-theme='" . esc_attr( strval( $this->cf_appearance_mode ) ) . "' data-sitekey='" . esc_attr( strval( $this->cf_turnstile_site_key ) ) . "'></div>"; ?> |
| 179 |
<?php } ?> |
| 180 |
<?php if ( 'hcaptcha' === $this->captcha_security_type && $this->hcaptcha_site_key ) { ?> |
| 181 |
<?php echo "<div id='srfm-hcaptcha-sitekey' data-callback='onSuccess' data-error-callback='onHCaptchaError' class='h-captcha' data-sitekey='" . esc_attr( strval( $this->hcaptcha_site_key ) ) . "'></div>"; ?> |
| 182 |
<?php } ?> |
| 183 |
<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> |
| 184 |
</div> |
| 185 |
<?php } ?> |
| 186 |
<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' : '' ); ?>"> |
| 187 |
<?php |
| 188 |
if ( 'g-recaptcha' === $this->captcha_security_type ) { |
| 189 |
|
| 190 |
if ( empty( $this->google_captcha_site_key ) ) { |
| 191 |
Helper::render_missing_sitekey_error( 'Google reCAPTCHA' ); |
| 192 |
} else { |
| 193 |
if ( 'v3-reCAPTCHA' === $this->recaptcha_version ) { |
| 194 |
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. |
| 195 |
} |
| 196 |
|
| 197 |
if ( 'v2-checkbox' === $this->recaptcha_version ) { |
| 198 |
wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', [], SRFM_VER, true ); |
| 199 |
} |
| 200 |
|
| 201 |
if ( 'v2-invisible' === $this->recaptcha_version ) { |
| 202 |
wp_enqueue_script( 'google-recaptcha-invisible', 'https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit', [ SRFM_SLUG . '-form-submit' ], SRFM_VER, true ); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if ( 'cf-turnstile' === $this->captcha_security_type ) { |
| 208 |
if ( ! empty( $this->cf_turnstile_site_key ) ) { |
| 209 |
// Cloudflare Turnstile script. |
| 210 |
wp_enqueue_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 211 |
SRFM_SLUG . '-cf-turnstile', |
| 212 |
'https://challenges.cloudflare.com/turnstile/v0/api.js', |
| 213 |
[], |
| 214 |
null, |
| 215 |
[ |
| 216 |
false, |
| 217 |
'defer' => true, |
| 218 |
] |
| 219 |
); |
| 220 |
} else { |
| 221 |
Helper::render_missing_sitekey_error( 'Cloudflare Turnstile' ); |
| 222 |
} |
| 223 |
} |
| 224 |
if ( 'hcaptcha' === $this->captcha_security_type ) { |
| 225 |
if ( ! empty( $this->hcaptcha_site_key ) ) { |
| 226 |
wp_enqueue_script( 'hcaptcha', 'https://js.hcaptcha.com/1/api.js', [], null, [ 'strategy' => 'defer' ] ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 227 |
} else { |
| 228 |
Helper::render_missing_sitekey_error( 'HCaptcha' ); |
| 229 |
} |
| 230 |
} |
| 231 |
$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 ); |
| 232 |
|
| 233 |
$button_style = $this->btn_from_theme ? '' : ' font-family: inherit; font-weight: var(--wp--custom--font-weight--medium); line-height: normal;'; |
| 234 |
$button_style .= 'width:100%;'; |
| 235 |
|
| 236 |
$button_attributes = ''; |
| 237 |
if ( 'g-recaptcha' === $this->captcha_security_type && ( 'v2-invisible' === $this->recaptcha_version || 'v3-reCAPTCHA' === $this->recaptcha_version ) ) { |
| 238 |
$button_attributes = 'recaptcha-type=' . $this->recaptcha_version . ' data-sitekey=' . $this->google_captcha_site_key . ' data-callback="recaptchaCallback"'; |
| 239 |
} |
| 240 |
?> |
| 241 |
<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 esc_attr( $button_attributes ); ?>> |
| 242 |
<div class="srfm-submit-wrap"> |
| 243 |
<?php echo esc_html( $this->button_text ); ?> |
| 244 |
<div class="srfm-loader"></div> |
| 245 |
</div> |
| 246 |
</button> |
| 247 |
<div class="srfm-error-wrap"></div> |
| 248 |
</div> |
| 249 |
<?php |
| 250 |
} |
| 251 |
return ob_get_clean(); |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|