generate-form-markup.php
| 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( |
| 238 | 'srfm_form_css_variables', |
| 239 | [ |
| 240 | 'id' => $id, |
| 241 | 'primary_color' => $primary_color_var, |
| 242 | 'help_color' => $help_color_var, |
| 243 | ] |
| 244 | ); |
| 245 | // echo custom css on page/post. |
| 246 | if ( 'sureforms_form' !== $current_post_type ) : |
| 247 | echo wp_kses_post( $custom_css ); |
| 248 | endif; |
| 249 | ?> |
| 250 | } |
| 251 | </style> |
| 252 | <?php |
| 253 | if ( 'sureforms_form' !== $current_post_type && true === $show_title_current_page ) { |
| 254 | $title = ! empty( get_the_title( (int) $id ) ) ? get_the_title( (int) $id ) : ''; |
| 255 | ?> |
| 256 | <h2 class="srfm-form-title"><?php echo esc_html( $title ); ?></h2> |
| 257 | <?php |
| 258 | } |
| 259 | ?> |
| 260 | <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 ' : '' ); ?>" |
| 261 | 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' ) ); ?>" |
| 262 | > |
| 263 | <?php |
| 264 | wp_nonce_field( 'srfm-form-submit', 'sureforms_form_submit' ); |
| 265 | $global_setting_options = get_option( 'srfm_general_settings_options' ); |
| 266 | $honeypot_spam = is_array( $global_setting_options ) && isset( $global_setting_options['srfm_honeypot'] ) ? $global_setting_options['srfm_honeypot'] : ''; |
| 267 | |
| 268 | if ( $is_page_break && 'none' !== $page_break_progress_type ) { |
| 269 | do_action( 'srfm_page_break_header', $id ); |
| 270 | } |
| 271 | ?> |
| 272 | |
| 273 | <input type="hidden" value="<?php echo esc_attr( Helper::get_string_value( $id ) ); ?>" name="form-id"> |
| 274 | <input type="hidden" value="" name="srfm-sender-email-field" id="srfm-sender-email"> |
| 275 | <input type="hidden" value="<?php echo esc_attr( Helper::get_string_value( $is_page_break ) ); ?>" id="srfm-page-break"> |
| 276 | <?php if ( $honeypot_spam ) : ?> |
| 277 | <input type="hidden" value="" name="srfm-honeypot-field"> |
| 278 | <?php endif; ?> |
| 279 | <?php |
| 280 | |
| 281 | if ( $is_page_break ) { |
| 282 | do_action( 'srfm_page_break_pagination', $post, $id ); |
| 283 | } else { |
| 284 | // phpcs:ignore |
| 285 | echo $content; |
| 286 | // phpcs:ignoreEnd |
| 287 | } |
| 288 | ?> |
| 289 | <?php if ( 0 !== $block_count && ! $is_inline_button || $is_page_break ) : ?> |
| 290 | <?php if ( ! empty( $security_type ) && 'none' !== $security_type ) : ?> |
| 291 | <div class="srfm-captcha-container <?php echo esc_attr( 'v3-reCAPTCHA' === $recaptcha_version || 'v2-invisible' === $recaptcha_version ? 'srfm-display-none' : '' ); ?>"> |
| 292 | <?php if ( is_string( $google_captcha_site_key ) && ! empty( $google_captcha_site_key ) && 'g-recaptcha' === $security_type ) : ?> |
| 293 | |
| 294 | <?php if ( 'v2-checkbox' === $recaptcha_version ) : ?> |
| 295 | <?php |
| 296 | wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js', [], SRFM_VER, true ); |
| 297 | ?> |
| 298 | <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> |
| 299 | <?php endif; ?> |
| 300 | |
| 301 | <?php if ( 'v2-invisible' === $recaptcha_version ) : ?> |
| 302 | <?php |
| 303 | wp_enqueue_script( 'google-recaptcha-invisible', 'https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit', [ SRFM_SLUG . '-form-submit' ], SRFM_VER, true ); |
| 304 | ?> |
| 305 | <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> |
| 306 | <?php endif; ?> |
| 307 | |
| 308 | <?php if ( 'v3-reCAPTCHA' === $recaptcha_version ) : ?> |
| 309 | <?php wp_enqueue_script( 'srfm-google-recaptchaV3', 'https://www.google.com/recaptcha/api.js?render=' . esc_js( $google_captcha_site_key ), [], SRFM_VER, true ); ?> |
| 310 | <?php endif; ?> |
| 311 | |
| 312 | <?php endif; ?> |
| 313 | <?php |
| 314 | |
| 315 | if ( 'cf-turnstile' === $security_type ) : |
| 316 | // Cloudflare Turnstile script. |
| 317 | wp_enqueue_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 318 | SRFM_SLUG . '-cf-turnstile', |
| 319 | 'https://challenges.cloudflare.com/turnstile/v0/api.js', |
| 320 | [], |
| 321 | null, |
| 322 | [ |
| 323 | false, |
| 324 | 'defer' => true, |
| 325 | ] |
| 326 | ); |
| 327 | ?> |
| 328 | <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> |
| 329 | <?php |
| 330 | endif; |
| 331 | |
| 332 | if ( 'hcaptcha' === $security_type ) : |
| 333 | // hCaptcha script. |
| 334 | wp_enqueue_script( 'hcaptcha', 'https://js.hcaptcha.com/1/api.js', [], null, [ 'strategy' => 'defer' ] ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 335 | ?> |
| 336 | <div id="srfm-hcaptcha-sitekey" data-callback="onSuccess" class="h-captcha" data-sitekey="<?php echo esc_attr( $srfm_hcaptcha_site_key ); ?>"></div> |
| 337 | <?php |
| 338 | endif; |
| 339 | ?> |
| 340 | <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> |
| 341 | </div> |
| 342 | <?php endif; ?> |
| 343 | |
| 344 | <?php |
| 345 | if ( $is_page_break ) { |
| 346 | do_action( 'srfm_page_break_btn', $id ); |
| 347 | } |
| 348 | ?> |
| 349 | |
| 350 | <div class="srfm-submit-container <?php echo esc_attr( $is_page_break ? 'hide' : '' ); ?>"> |
| 351 | <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"> |
| 352 | <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' : ''; ?>" |
| 353 | <?php if ( 'v3-reCAPTCHA' === $recaptcha_version ) : ?> |
| 354 | recaptcha-type="<?php echo esc_attr( $recaptcha_version ); ?>" |
| 355 | data-sitekey="<?php echo esc_attr( $google_captcha_site_key ); ?>" |
| 356 | <?php endif; ?> |
| 357 | > |
| 358 | <div class="srfm-submit-wrap"> |
| 359 | <?php echo esc_html( $button_text ); ?> |
| 360 | <div class="srfm-loader"></div> |
| 361 | </div> |
| 362 | </button> |
| 363 | </div> |
| 364 | </div> |
| 365 | <?php endif; ?> |
| 366 | <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> |
| 367 | </form> |
| 368 | <div class="srfm-single-form srfm-success-box in-page"> |
| 369 | <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> |
| 370 | </div> |
| 371 | <?php |
| 372 | $page_url = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 373 | $path = Helper::get_string_value( wp_parse_url( $page_url, PHP_URL_PATH ) ); |
| 374 | $segments = explode( '/', $path ); |
| 375 | $form_path = isset( $segments[1] ) ? $segments[1] : ''; |
| 376 | } |
| 377 | ?> |
| 378 | </div> |
| 379 | <?php |
| 380 | return ob_get_clean(); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Generate form confirmation markup |
| 385 | * |
| 386 | * @param array<mixed> $form_data contains form data. |
| 387 | * @param array<mixed> $submission_data contains submission data. |
| 388 | * @since 0.0.3 |
| 389 | * @return string|false |
| 390 | */ |
| 391 | public static function get_confirmation_markup( $form_data = [], $submission_data = [] ) { |
| 392 | |
| 393 | $confirmation_message = ''; |
| 394 | |
| 395 | if ( empty( $form_data ) ) { |
| 396 | return $confirmation_message; |
| 397 | } |
| 398 | |
| 399 | $form_confirmation = isset( $form_data['form-id'] ) ? |
| 400 | get_post_meta( Helper::get_integer_value( $form_data['form-id'] ), '_srfm_form_confirmation' ) : null; |
| 401 | |
| 402 | if ( ! is_array( $form_confirmation ) ) { |
| 403 | return $confirmation_message; |
| 404 | } |
| 405 | |
| 406 | $confirmation_data = is_array( $form_confirmation[0] ) && isset( $form_confirmation[0][0] ) ? $form_confirmation[0][0] : null; |
| 407 | |
| 408 | if ( is_array( $form_confirmation ) && isset( $confirmation_data['message'] ) && is_string( $confirmation_data['message'] ) ) { |
| 409 | $confirmation_message = $confirmation_data['message']; |
| 410 | } |
| 411 | if ( empty( $submission_data ) ) { |
| 412 | return $confirmation_message; |
| 413 | } |
| 414 | $smart_tags = new Smart_Tags(); |
| 415 | $confirmation_message = $smart_tags->process_smart_tags( $confirmation_data['message'], $submission_data, $form_data ); |
| 416 | |
| 417 | return apply_filters( 'srfm_after_submit_confirmation_message', $confirmation_message ); |
| 418 | |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Get redirect url for form incase of different page or custom url is selected. |
| 423 | * |
| 424 | * @param array<mixed> $form_data contains form data. |
| 425 | * @param array<mixed> $submission_data contains submission data. |
| 426 | * @since 1.0.2 |
| 427 | * @return string|false |
| 428 | */ |
| 429 | public static function get_redirect_url( $form_data = [], $submission_data = [] ) { |
| 430 | $redirect_url = ''; |
| 431 | |
| 432 | if ( empty( $form_data ) ) { |
| 433 | return $redirect_url; |
| 434 | } |
| 435 | |
| 436 | $form_confirmation = isset( $form_data['form-id'] ) ? |
| 437 | get_post_meta( Helper::get_integer_value( $form_data['form-id'] ), '_srfm_form_confirmation' ) : null; |
| 438 | |
| 439 | if ( ! is_array( $form_confirmation ) ) { |
| 440 | return $redirect_url; |
| 441 | } |
| 442 | |
| 443 | $confirmation_data = is_array( $form_confirmation[0] ) && isset( $form_confirmation[0][0] ) ? $form_confirmation[0][0] : null; |
| 444 | |
| 445 | $page_url = isset( $confirmation_data['page_url'] ) ? $confirmation_data['page_url'] : ''; |
| 446 | $custom_url = isset( $confirmation_data['custom_url'] ) ? $confirmation_data['custom_url'] : ''; |
| 447 | $confirmation_type = isset( $confirmation_data['confirmation_type'] ) ? $confirmation_data['confirmation_type'] : ''; |
| 448 | if ( 'different page' === $confirmation_type ) { |
| 449 | $redirect_url = esc_url( $page_url ); |
| 450 | } elseif ( 'custom url' === $confirmation_type ) { |
| 451 | $redirect_url = esc_url( $custom_url ); |
| 452 | } |
| 453 | |
| 454 | if ( empty( $redirect_url ) ) { |
| 455 | return $redirect_url; |
| 456 | } |
| 457 | |
| 458 | if ( empty( $confirmation_data['enable_query_params'] ) || true !== $confirmation_data['enable_query_params'] ) { |
| 459 | return $redirect_url; |
| 460 | } |
| 461 | |
| 462 | if ( empty( $confirmation_data['query_params'] ) && ! is_array( $confirmation_data['query_params'] ) ) { |
| 463 | return $redirect_url; |
| 464 | } |
| 465 | |
| 466 | $query_params = []; |
| 467 | foreach ( $confirmation_data['query_params'] as $params ) { |
| 468 | if ( is_array( $params ) && ! empty( array_keys( $params ) ) && ! empty( array_values( $params ) ) ) { |
| 469 | $query_params[ esc_attr( array_keys( $params )[0] ) ] = esc_attr( array_values( $params )[0] ); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | $redirect_url = add_query_arg( $query_params, $redirect_url ); |
| 474 | |
| 475 | if ( ! empty( $submission_data ) ) { |
| 476 | $smart_tags = new Smart_Tags(); |
| 477 | // Adding upload_format_type = 'raw' to retrieve urls as comma separated values. |
| 478 | $form_data['upload_format_type'] = 'raw'; |
| 479 | $redirect_url = html_entity_decode( $smart_tags->process_smart_tags( $redirect_url, $submission_data, $form_data ) ); |
| 480 | } |
| 481 | |
| 482 | return apply_filters( 'srfm_after_submit_redirect_url', $redirect_url ); |
| 483 | } |
| 484 | } |
| 485 |