PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.13
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.13
2.12.7 2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 All 97 releases
sureforms / inc / generate-form-markup.php

generate-form-markup.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.13, at inc/generate-form-markup.php

414 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms Generate Form Class file.
4 *
5 * @package sureforms.
6 * @since 0.0.1
7 */
8
9 namespace SRFM\Inc;
10
11 use WP_REST_Response;
12 use WP_Error;
13 use SRFM\Inc\Traits\Get_Instance;
14 use SRFM\Inc\Helper;
15 use SRFM\Inc\Smart_Tags;
16 use SRFM\Inc\Frontend_Assets;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit; // Exit if accessed directly.
20 }
21
22 /**
23 * Load Defaults Class.
24 *
25 * @since 0.0.1
26 */
27 class Generate_Form_Markup {
28 use Get_Instance;
29
30 /**
31 * Constructor
32 *
33 * @since 0.0.1
34 */
35 public function __construct() {
36 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
37 }
38
39 /**
40 * Add custom API Route to generate form markup.
41 *
42 * @return void
43 * @since 0.0.1
44 */
45 public function register_custom_endpoint() {
46 register_rest_route(
47 'sureforms/v1',
48 '/generate-form-markup',
49 [
50 'methods' => 'GET',
51 'callback' => [ $this, 'get_form_markup' ],
52 'permission_callback' => '__return_true',
53 ]
54 );
55 }
56
57 /**
58 * Handle Form status
59 *
60 * @param int|string $id Contains form ID.
61 * @param boolean $show_title_current_page Boolean to show/hide form title.
62 * @param string $sf_classname additional class_name.
63 * @param string $post_type Contains post type.
64 * @param boolean $do_blocks Boolean to enable/disable parsing dynamic blocks.
65 *
66 * @return string|false
67 * @since 0.0.1
68 */
69 public static function get_form_markup( $id, $show_title_current_page = true, $sf_classname = '', $post_type = 'post', $do_blocks = false ) {
70 if ( isset( $_GET['id'] ) && isset( $_GET['srfm_form_markup_nonce'] ) ) {
71 $nonce = isset( $_GET['srfm_form_markup_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['srfm_form_markup_nonce'] ) ) : '';
72 $id = wp_verify_nonce( $nonce, 'srfm_form_markup' ) && ! empty( $_GET['srfm_form_markup_nonce'] ) ? Helper::get_integer_value( sanitize_text_field( wp_unslash( $_GET['id'] ) ) ) : '';
73 } else {
74 $id = Helper::get_integer_value( $id );
75 }
76 do_action( 'srfm_localize_conditional_logic_data', $id );
77 $post = get_post( Helper::get_integer_value( $id ) );
78
79 $content = '';
80
81 if ( $post && ! empty( $post->post_content ) ) {
82 if ( ! empty( $do_blocks ) ) {
83 $content = do_blocks( $post->post_content );
84 } else {
85 $content = apply_filters( 'the_content', $post->post_content ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook
86 }
87 }
88
89 $blocks = parse_blocks( $content );
90 $block_count = count( $blocks );
91 $current_post_type = get_post_type();
92
93 // load all the frontend assets.
94 Frontend_Assets::enqueue_scripts_and_styles();
95
96 ob_start();
97 if ( '' !== $id && 0 !== $block_count ) {
98
99 $container_id = 'srfm-form-container-' . Helper::get_string_value( $id );
100
101 $form_classes = [
102 'srfm-form-container',
103 $container_id,
104 $sf_classname,
105 ];
106
107 $form_classes[] = Helper::get_string_value( Helper::get_meta_value( $id, '_srfm_additional_classes' ) );
108
109 $form_styling = get_post_meta( $id, '_srfm_forms_styling', true );
110 $form_styling = ! empty( $form_styling ) && is_array( $form_styling ) ? $form_styling : [];
111 $page_break_settings = defined( 'SRFM_PRO_VER' ) ? get_post_meta( $id, '_srfm_page_break_settings', true ) : [];
112 $page_break_settings = ! empty( $page_break_settings ) && is_array( $page_break_settings ) ? $page_break_settings : [];
113 $is_page_break = ! empty( $page_break_settings ) ? $page_break_settings['is_page_break'] : false;
114 $page_break_progress_type = ! empty( $page_break_settings ) ? $page_break_settings['progress_indicator_type'] : 'none';
115 $form_confirmation = get_post_meta( $id, '_srfm_form_confirmation' );
116 $confirmation_type = '';
117 $submission_action = '';
118 $success_url = '';
119 if ( is_array( $form_confirmation ) && isset( $form_confirmation[0][0] ) ) {
120 $confirmation_data = $form_confirmation[0][0];
121 $page_url = isset( $confirmation_data['page_url'] ) ? $confirmation_data['page_url'] : '';
122 $custom_url = isset( $confirmation_data['custom_url'] ) ? $confirmation_data['custom_url'] : '';
123 $confirmation_type = isset( $confirmation_data['confirmation_type'] ) ? $confirmation_data['confirmation_type'] : '';
124 $submission_action = isset( $confirmation_data['submission_action'] ) ? $confirmation_data['submission_action'] : '';
125 $success_url = '';
126 if ( 'different page' === $confirmation_type ) {
127 $success_url = $page_url;
128 } elseif ( 'custom url' === $confirmation_type ) {
129 $success_url = $custom_url;
130 }
131 }
132
133 // Submit button.
134 $button_text = Helper::get_meta_value( $id, '_srfm_submit_button_text' );
135 $submit_button_alignment = $form_styling['submit_button_alignment'];
136 $btn_from_theme = Helper::get_meta_value( $id, '_srfm_inherit_theme_button' );
137 $is_inline_button = Helper::get_meta_value( $id, '_srfm_is_inline_button' );
138 $security_type = Helper::get_meta_value( $id, '_srfm_captcha_security_type' );
139 $form_custom_css_meta = Helper::get_meta_value( $id, '_srfm_form_custom_css' );
140 $custom_css = ! empty( $form_custom_css_meta ) && is_string( $form_custom_css_meta ) ? $form_custom_css_meta : '';
141
142 $full = 'justify' === $submit_button_alignment ? true : false;
143 $recaptcha_version = 'g-recaptcha' === $security_type ? Helper::get_meta_value( $id, '_srfm_form_recaptcha' ) : '';
144 $srfm_cf_appearance_mode = '';
145 $srfm_cf_turnstile_site_key = '';
146 $srfm_hcaptcha_site_key = '';
147
148 $google_captcha_site_key = '';
149
150 if ( 'none' !== $security_type ) {
151 $global_setting_options = get_option( 'srfm_security_settings_options' );
152 } else {
153 $global_setting_options = [];
154 }
155
156 if ( is_array( $global_setting_options ) && 'cf-turnstile' === $security_type ) {
157 $srfm_cf_turnstile_site_key = isset( $global_setting_options['srfm_cf_turnstile_site_key'] ) ? $global_setting_options['srfm_cf_turnstile_site_key'] : '';
158 $srfm_cf_appearance_mode = isset( $global_setting_options['srfm_cf_appearance_mode'] ) ? $global_setting_options['srfm_cf_appearance_mode'] : 'auto';
159 }
160
161 if ( is_array( $global_setting_options ) && 'hcaptcha' === $security_type ) {
162 $srfm_hcaptcha_site_key = isset( $global_setting_options['srfm_hcaptcha_site_key'] ) ? $global_setting_options['srfm_hcaptcha_site_key'] : '';
163 }
164
165 if ( is_array( $global_setting_options ) && 'g-recaptcha' === $security_type ) {
166 switch ( $recaptcha_version ) {
167 case 'v2-checkbox':
168 $google_captcha_site_key = isset( $global_setting_options['srfm_v2_checkbox_site_key'] ) ? $global_setting_options['srfm_v2_checkbox_site_key'] : '';
169 break;
170 case 'v2-invisible':
171 $google_captcha_site_key = isset( $global_setting_options['srfm_v2_invisible_site_key'] ) ? $global_setting_options['srfm_v2_invisible_site_key'] : '';
172 break;
173 case 'v3-reCAPTCHA':
174 $google_captcha_site_key = isset( $global_setting_options['srfm_v3_site_key'] ) ? $global_setting_options['srfm_v3_site_key'] : '';
175 break;
176 default:
177 break;
178 }
179 }
180
181 $primary_color = $form_styling['primary_color'];
182 $help_color_var = $form_styling['text_color'];
183 $label_text_color = $form_styling['text_color_on_primary'];
184 $field_spacing = $form_styling['field_spacing'];
185
186 // New colors.
187
188 $primary_color_var = $primary_color ? $primary_color : '#046bd2';
189 $label_text_color_var = $label_text_color ? $label_text_color : '#111827';
190
191 $selected_size = Helper::get_css_vars( $field_spacing );
192 ?>
193 <div class="<?php echo esc_attr( implode( ' ', array_filter( $form_classes ) ) ); ?>">
194 <style>
195 /* Need to check and remove the input variables related to the Style Tab. */
196 <?php echo esc_html( ".{$container_id}" ); ?> {
197 /* New test variables */
198 --srfm-color-scheme-primary: <?php echo esc_html( $primary_color_var ); ?>;
199 --srfm-color-scheme-text-on-primary: <?php echo esc_html( $label_text_color_var ); ?>;
200 --srfm-color-scheme-text: <?php echo esc_html( $help_color_var ); ?>;
201
202 --srfm-color-input-label: <?php echo esc_html( $help_color_var ); ?>;
203 --srfm-color-input-description: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.65 );
204 --srfm-color-input-placeholder: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.5 );
205 --srfm-color-input-text: <?php echo esc_html( $help_color_var ); ?>;
206 --srfm-color-input-prefix: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.65 );
207 --srfm-color-input-background: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.02 );
208 --srfm-color-input-background-hover: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.05 );
209 --srfm-color-input-background-disabled: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.05 );
210 --srfm-color-input-border: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.25 );
211 --srfm-color-input-border-disabled: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.15 );
212 --srfm-color-multi-choice-svg: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.7 );
213 --srfm-color-input-border-hover: hsl( from <?php echo esc_html( $primary_color_var ); ?> h s l / 0.65 );
214 --srfm-color-input-border-focus-glow: hsl( from <?php echo esc_html( $primary_color_var ); ?> h s l / 0.15 );
215 --srfm-color-input-selected: hsl( from <?php echo esc_html( $primary_color_var ); ?> h s l / 0.1 );
216 --srfm-btn-color-hover: hsl( from <?php echo esc_html( $primary_color_var ); ?> h s l / 0.9 );
217 --srfm-btn-color-disabled: hsl( from <?php echo esc_html( $primary_color_var ); ?> h s l / 0.25 );
218
219 /* Dropdown Variables */
220 --srfm-dropdown-input-background-hover: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.05 );
221 --srfm-dropdown-option-background-hover: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.10 );
222 --srfm-dropdown-option-background-selected: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.05 );
223 --srfm-dropdown-option-selected-icon: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.65 );
224 --srfm-dropdown-option-text-color: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.80 );
225 --srfm-dropdown-option-selected-text: <?php echo esc_html( $help_color_var ); ?>;
226 --srfm-dropdown-badge-background: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.05 );
227 --srfm-dropdown-badge-background-hover: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.10 );
228 --srfm-dropdown-menu-border-color: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.10 );
229 --srfm-dropdown-placeholder-color: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.50 );
230 --srfm-dropdown-icon-color: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.65 );
231 --srfm-dropdown-icon-disabled: hsl( from <?php echo esc_html( $help_color_var ); ?> h s l / 0.25 );
232 <?php
233 // Echo the CSS variables for the form according to the field spacing selected.
234 foreach ( $selected_size as $variable => $value ) {
235 echo esc_html( Helper::get_string_value( $variable ) ) . ': ' . esc_html( Helper::get_string_value( $value ) ) . ';';
236 }
237 do_action( 'srfm_form_css_variables', $id, $primary_color_var, $help_color_var );
238 // echo custom css on page/post.
239 if ( 'sureforms_form' !== $current_post_type ) :
240 echo wp_kses_post( $custom_css );
241 endif;
242 ?>
243 }
244 </style>
245 <?php
246 if ( 'sureforms_form' !== $current_post_type && true === $show_title_current_page ) {
247 $title = ! empty( get_the_title( (int) $id ) ) ? get_the_title( (int) $id ) : '';
248 ?>
249 <h2 class="srfm-form-title"><?php echo esc_html( $title ); ?></h2>
250 <?php
251 }
252 ?>
253 <form method="post" enctype="multipart/form-data" id="srfm-form-<?php echo esc_attr( Helper::get_string_value( $id ) ); ?>" class="srfm-form <?php echo esc_attr( 'sureforms_form' === $post_type ? 'srfm-single-form ' : '' ); ?>"
254 form-id="<?php echo esc_attr( Helper::get_string_value( $id ) ); ?>" after-submission="<?php echo esc_attr( $submission_action ); ?>" message-type="<?php echo esc_attr( $confirmation_type ? $confirmation_type : 'same page' ); ?>" success-url="<?php echo esc_attr( $success_url ? $success_url : '' ); ?>" ajaxurl="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" nonce="<?php echo esc_attr( wp_create_nonce( 'unique_validation_nonce' ) ); ?>"
255 >
256 <?php
257 wp_nonce_field( 'srfm-form-submit', 'sureforms_form_submit' );
258 $global_setting_options = get_option( 'srfm_general_settings_options' );
259 $honeypot_spam = is_array( $global_setting_options ) && isset( $global_setting_options['srfm_honeypot'] ) ? $global_setting_options['srfm_honeypot'] : '';
260
261 if ( $is_page_break && 'none' !== $page_break_progress_type ) {
262 do_action( 'srfm_page_break_header', $id );
263 }
264 ?>
265
266 <input type="hidden" value="<?php echo esc_attr( Helper::get_string_value( $id ) ); ?>" name="form-id">
267 <input type="hidden" value="" name="srfm-sender-email-field" id="srfm-sender-email">
268 <input type="hidden" value="<?php echo esc_attr( Helper::get_string_value( $is_page_break ) ); ?>" id="srfm-page-break">
269 <?php if ( $honeypot_spam ) : ?>
270 <input type="hidden" value="" name="srfm-honeypot-field">
271 <?php endif; ?>
272 <?php
273
274 if ( $is_page_break ) {
275 do_action( 'srfm_page_break_pagination', $post, $id );
276 } else {
277 // phpcs:ignore
278 echo $content;
279 // phpcs:ignoreEnd
280 }
281 ?>
282 <?php if ( 0 !== $block_count && ! $is_inline_button || $is_page_break ) : ?>
283 <?php if ( ! empty( $security_type ) && 'none' !== $security_type ) : ?>
284 <div class="srfm-captcha-container">
285 <?php if ( is_string( $google_captcha_site_key ) && ! empty( $google_captcha_site_key ) && 'g-recaptcha' === $security_type ) : ?>
286
287 <?php if ( 'v2-checkbox' === $recaptcha_version ) : ?>
288 <?php
289 wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', [], SRFM_VER, true );
290 ?>
291 <div class='g-recaptcha' data-callback="onSuccess" recaptcha-type="<?php echo esc_attr( $recaptcha_version ); ?>" data-sitekey="<?php echo esc_attr( strval( $google_captcha_site_key ) ); ?>" ></div>
292 <?php endif; ?>
293
294 <?php if ( 'v2-invisible' === $recaptcha_version ) : ?>
295 <?php
296 wp_enqueue_script( 'google-recaptcha-invisible', 'https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit', [ SRFM_SLUG . '-form-submit' ], SRFM_VER, true );
297 ?>
298 <div class='g-recaptcha' recaptcha-type="<?php echo esc_attr( $recaptcha_version ); ?>" data-sitekey="<?php echo esc_attr( $google_captcha_site_key ); ?>" data-size="invisible"></div>
299 <?php endif; ?>
300
301 <?php if ( 'v3-reCAPTCHA' === $recaptcha_version ) : ?>
302 <?php wp_enqueue_script( 'srfm-google-recaptchaV3', 'https://www.google.com/recaptcha/api.js?render=' . esc_js( $google_captcha_site_key ), [], SRFM_VER, true ); ?>
303 <?php endif; ?>
304
305 <?php endif; ?>
306 <?php
307
308 if ( 'cf-turnstile' === $security_type ) :
309 // Cloudflare Turnstile script.
310 wp_enqueue_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
311 SRFM_SLUG . '-cf-turnstile',
312 'https://challenges.cloudflare.com/turnstile/v0/api.js',
313 [],
314 null,
315 [
316 false,
317 'defer' => true,
318 ]
319 );
320 ?>
321 <div id="srfm-cf-sitekey" class="cf-turnstile" data-callback="onSuccess" data-theme="<?php echo esc_attr( $srfm_cf_appearance_mode ); ?>" data-sitekey="<?php echo esc_attr( $srfm_cf_turnstile_site_key ); ?>"></div>
322 <?php
323 endif;
324
325 if ( 'hcaptcha' === $security_type ) :
326 // hCaptcha script.
327 wp_enqueue_script( 'hcaptcha', 'https://js.hcaptcha.com/1/api.js', [], null, [ 'strategy' => 'defer' ] ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
328 ?>
329 <div id="srfm-hcaptcha-sitekey" data-callback="onSuccess" class="h-captcha" data-sitekey="<?php echo esc_attr( $srfm_hcaptcha_site_key ); ?>"></div>
330 <?php
331 endif;
332 ?>
333 <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>
334 </div>
335 <?php endif; ?>
336
337 <?php
338 if ( $is_page_break ) {
339 do_action( 'srfm_page_break_btn', $id );
340 }
341 ?>
342
343 <div class="srfm-submit-container <?php echo esc_attr( $is_page_break ? 'hide' : '' ); ?>">
344 <div style="width: <?php echo esc_attr( $full ? '100%;' : ';' ); ?> text-align: <?php echo esc_attr( $submit_button_alignment ? $submit_button_alignment : 'left' ); ?>" class="wp-block-button">
345 <button style="width:<?php echo esc_attr( $full ? '100%;' : '' ); ?>" id="srfm-submit-btn"class="<?php echo esc_attr( '1' === $btn_from_theme ? 'wp-block-button__link' : 'srfm-btn-frontend srfm-button srfm-submit-button' ); ?><?php echo 'v3-reCAPTCHA' === $recaptcha_version ? ' g-recaptcha' : ''; ?>"
346 <?php if ( 'v3-reCAPTCHA' === $recaptcha_version ) : ?>
347 recaptcha-type="<?php echo esc_attr( $recaptcha_version ); ?>"
348 data-sitekey="<?php echo esc_attr( $google_captcha_site_key ); ?>"
349 <?php endif; ?>
350 >
351 <div class="srfm-submit-wrap">
352 <?php echo esc_html( $button_text ); ?>
353 <div class="srfm-loader"></div>
354 </div>
355 </button>
356 </div>
357 </div>
358 <?php endif; ?>
359 <p id="srfm-error-message" class="srfm-error-message" hidden="true"><?php echo esc_html__( 'There was an error trying to submit your form. Please try again.', 'sureforms' ); ?></p>
360 </form>
361 <div class="srfm-single-form srfm-success-box in-page">
362 <div aria-live="polite" aria-atomic="true" role="alert" id="srfm-success-message-page-<?php echo esc_attr( Helper::get_string_value( $id ) ); ?>" class="srfm-success-box-description"></div>
363 </div>
364 <?php
365 $page_url = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
366 $path = Helper::get_string_value( wp_parse_url( $page_url, PHP_URL_PATH ) );
367 $segments = explode( '/', $path );
368 $form_path = isset( $segments[1] ) ? $segments[1] : '';
369 }
370 ?>
371 </div>
372 <?php
373 return ob_get_clean();
374 }
375
376 /**
377 * Generate form confirmation markup
378 *
379 * @param array<mixed> $form_data contains form data.
380 * @param array<mixed> $submission_data contains submission data.
381 * @since 0.0.3
382 * @return string|false
383 */
384 public static function get_confirmation_markup( $form_data = [], $submission_data = [] ) {
385
386 $confirmation_message = '';
387
388 if ( empty( $form_data ) ) {
389 return $confirmation_message;
390 }
391
392 $form_confirmation = isset( $form_data['form-id'] ) ?
393 get_post_meta( Helper::get_integer_value( $form_data['form-id'] ), '_srfm_form_confirmation' ) : null;
394
395 if ( ! is_array( $form_confirmation ) ) {
396 return $confirmation_message;
397 }
398
399 $confirmation_data = is_array( $form_confirmation[0] ) && isset( $form_confirmation[0][0] ) ? $form_confirmation[0][0] : null;
400
401 if ( is_array( $form_confirmation ) && isset( $confirmation_data['message'] ) && is_string( $confirmation_data['message'] ) ) {
402 $confirmation_message = $confirmation_data['message'];
403 }
404 if ( empty( $submission_data ) ) {
405 return $confirmation_message;
406 }
407 $smart_tags = new Smart_Tags();
408 $confirmation_message = $smart_tags->process_smart_tags( $confirmation_data['message'], $submission_data, $form_data );
409
410 return $confirmation_message;
411
412 }
413 }
414