elementor
1 year ago
logs
1 month ago
strong-testimonials-beaver-block
1 year ago
submodules
8 months ago
class-strong-gutemberg.php
1 year ago
class-strong-log.php
1 year ago
class-strong-mail.php
1 year ago
class-strong-testimonials-average-shortcode.php
1 year ago
class-strong-testimonials-count-shortcode.php
1 year ago
class-strong-testimonials-defaults.php
1 month ago
class-strong-testimonials-form.php
1 month ago
class-strong-testimonials-order.php
1 year ago
class-strong-testimonials-privacy.php
1 year ago
class-strong-testimonials-render.php
1 month ago
class-strong-testimonials-templates.php
1 year ago
class-strong-testimonials-view-shortcode.php
1 week ago
class-strong-testimonials-view-widget.php
1 year ago
class-strong-view-display.php
1 day ago
class-strong-view-form.php
1 day ago
class-strong-view-slideshow.php
1 day ago
class-strong-view.php
1 day ago
class-walker-strong-category-checklist-front.php
1 year ago
deprecated.php
1 year ago
filters.php
1 month ago
functions-activation.php
1 month ago
functions-content.php
11 months ago
functions-image.php
5 months ago
functions-rating.php
1 year ago
functions-template-form.php
1 week ago
functions-template.php
4 months ago
functions-views.php
1 year ago
functions.php
1 month ago
l10n-polylang.php
1 year ago
l10n-wpml.php
1 year ago
post-types.php
1 week ago
retro.php
1 year ago
scripts.php
1 month ago
class-strong-view-form.php
270 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Strong_View_Form |
| 4 | * |
| 5 | * @since 2.16.0 |
| 6 | */ |
| 7 | |
| 8 | // Exit if accessed directly |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | if ( ! class_exists( 'Strong_View_Form' ) ) : |
| 14 | |
| 15 | class Strong_View_Form extends Strong_View { |
| 16 | |
| 17 | /** |
| 18 | * Strong_View_Form constructor. |
| 19 | * |
| 20 | * @param array $atts |
| 21 | */ |
| 22 | public function __construct( $atts = array() ) { |
| 23 | parent::__construct( $atts ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Process the view. |
| 28 | * |
| 29 | * Used by main class to load the scripts and styles for this View. |
| 30 | */ |
| 31 | public function process() { |
| 32 | $this->build_classes(); |
| 33 | $this->find_stylesheet(); |
| 34 | $this->load_dependent_scripts(); |
| 35 | $this->load_extra_stylesheets(); |
| 36 | $this->load_validator(); |
| 37 | |
| 38 | // If we can preprocess, we can add the inline style in the <head>. |
| 39 | add_action( 'wp_enqueue_scripts', array( $this, 'add_custom_style' ), 20 ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Print overlay while form data is submitted. |
| 44 | * |
| 45 | * This helps when uploading large files and on slow connections. |
| 46 | * |
| 47 | * @since 2.31.5 |
| 48 | */ |
| 49 | public function print_overlay() { |
| 50 | if ( apply_filters( 'wpmtst_form_wait', true ) ) { |
| 51 | ?> |
| 52 | <div class="strong-form-wait" data-formid="<?php echo esc_attr( WPMST()->atts( 'form_id' ) ); ?>"> |
| 53 | <div class="message"> |
| 54 | <?php echo wp_kses_post( apply_filters( 'wpmtst_form_wait_message', '<img src="' . WPMTST_PUBLIC_URL . 'svg/spinner-solid.svg" alt="' . esc_attr__( 'Strong Testimonials form submission spinner.', 'strong-testimonials' ) . '" >' ) ); ?> |
| 55 | </div> |
| 56 | </div> |
| 57 | <?php |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Load resources on form success. |
| 63 | * |
| 64 | * When using normal form submission (not Ajax) |
| 65 | * and displaying a success message (not redirecting). |
| 66 | */ |
| 67 | public function success() { |
| 68 | $form_options = get_option( 'wpmtst_form_options' ); |
| 69 | |
| 70 | // Remember: top level is converted to strings! |
| 71 | $args = array( |
| 72 | 'display' => array( |
| 73 | 'successMessage' => true, |
| 74 | ), |
| 75 | 'scroll' => array( |
| 76 | 'onSuccess' => $form_options['scrolltop_success'], |
| 77 | 'onSuccessOffset' => $form_options['scrolltop_success_offset'], |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | WPMST()->render->add_script( 'wpmtst-form-validation' ); |
| 82 | WPMST()->render->add_script_var( 'wpmtst-form-validation', 'strongForm', $args ); |
| 83 | |
| 84 | $this->find_stylesheet(); |
| 85 | $this->html = wpmtst_get_success_message( $this->atts ); |
| 86 | |
| 87 | do_action( 'wpmtst_form_success', $this->atts ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Build the view. |
| 92 | */ |
| 93 | public function build() { |
| 94 | |
| 95 | do_action( 'wpmtst_view_build_before', $this ); |
| 96 | |
| 97 | $this->build_classes(); |
| 98 | $this->find_stylesheet(); |
| 99 | $this->load_dependent_scripts(); |
| 100 | $this->load_extra_stylesheets(); |
| 101 | $this->custom_background(); |
| 102 | $this->load_validator(); |
| 103 | |
| 104 | // If we cannot preprocess, add the inline style to the footer. |
| 105 | add_action( 'wp_footer', array( $this, 'add_custom_style' ) ); |
| 106 | |
| 107 | $form_values = isset( $this->atts['category'] ) ? array( 'category' => $this->atts['category'] ) : array(); |
| 108 | |
| 109 | $fields = isset( $this->atts['form_id'] ) ? wpmtst_get_form_fields( $this->atts['form_id'] ) : false; |
| 110 | if ( $fields ) { |
| 111 | foreach ( $fields as $field ) { |
| 112 | $form_values[ $field['name'] ] = ''; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $previous_values = WPMST()->form->get_form_values(); |
| 117 | if ( $previous_values ) { |
| 118 | $form_values = array_merge( $form_values, $previous_values ); |
| 119 | } |
| 120 | |
| 121 | WPMST()->form->set_form_values( $form_values ); |
| 122 | |
| 123 | /** |
| 124 | * Add filters here. |
| 125 | */ |
| 126 | add_action( 'wpmtst_before_form', array( $this, 'print_overlay' ) ); |
| 127 | |
| 128 | /** |
| 129 | * Locate template. |
| 130 | */ |
| 131 | $form_atts = $this->atts; |
| 132 | if ( isset( $form_atts['form_template'] ) && $form_atts['form_template'] ) { |
| 133 | $form_atts['template'] = $form_atts['form_template']; |
| 134 | } else { |
| 135 | $form_atts['template'] = 'default-form'; |
| 136 | } |
| 137 | $template_file = WPMST()->templates->get_template_attr( $form_atts, 'template' ); |
| 138 | if ( ! $template_file ) { |
| 139 | $template_file = WPMST()->templates->get_template_attr( array( 'template' => 'default-form' ), 'template' ); |
| 140 | } |
| 141 | $this->template_file = apply_filters( 'wpmtst_view_template_file_form', $template_file ); |
| 142 | |
| 143 | /** |
| 144 | * Allow add-ons to hijack the output generation. |
| 145 | */ |
| 146 | $atts = $this->atts; |
| 147 | if ( has_filter( 'wpmtst_render_view_template' ) ) { |
| 148 | $html = apply_filters( 'wpmtst_render_view_template', '', $this ); |
| 149 | } else { |
| 150 | |
| 151 | /** |
| 152 | * Gutenberg. Yay. |
| 153 | * @since 2.31.9 |
| 154 | */ |
| 155 | global $post; |
| 156 | $post_before = $post; |
| 157 | |
| 158 | ob_start(); |
| 159 | /** @noinspection PhpIncludeInspection */ |
| 160 | include $this->template_file; |
| 161 | $html = ob_get_clean(); |
| 162 | |
| 163 | $post = $post_before; |
| 164 | |
| 165 | } |
| 166 | // TODO apply content filters |
| 167 | |
| 168 | $html = $this->template_not_found_notice( $form_atts['template'] ) . $html; |
| 169 | |
| 170 | /** |
| 171 | * Remove filters here. |
| 172 | */ |
| 173 | remove_action( 'wpmtst_before_form', array( $this, 'print_overlay' ) ); |
| 174 | |
| 175 | /** |
| 176 | * Trigger stuff. |
| 177 | */ |
| 178 | do_action( 'wpmtst_form_rendered', $this->atts ); |
| 179 | |
| 180 | $this->html = apply_filters( 'strong_view_form_html', $html, $this ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Build class list based on view attributes. |
| 185 | */ |
| 186 | public function build_classes() { |
| 187 | |
| 188 | $container_class_list = isset( $this->atts['view'] ) ? array( 'strong-view-id-' . $this->atts['view'] ) : array(); |
| 189 | $container_class_list = array_merge( $container_class_list, $this->get_template_css_class() ); |
| 190 | |
| 191 | if ( is_rtl() ) { |
| 192 | $container_class_list[] = 'rtl'; |
| 193 | } |
| 194 | |
| 195 | if ( isset( $this->atts['class'] ) && $this->atts['class'] ) { |
| 196 | $container_class_list[] = $this->atts['class']; |
| 197 | } |
| 198 | |
| 199 | $container_data_list = array(); |
| 200 | $container_data_list['state'] = 'idle'; |
| 201 | |
| 202 | /** |
| 203 | * Filter classes. |
| 204 | */ |
| 205 | $this->atts['container_data'] = apply_filters( 'wpmtst_view_container_data', $container_data_list, $this->atts ); |
| 206 | $this->atts['container_class'] = implode( ' ', apply_filters( 'wpmtst_view_container_class', $container_class_list, $this->atts ) ); |
| 207 | |
| 208 | /** |
| 209 | * Store updated atts. |
| 210 | */ |
| 211 | WPMST()->set_atts( $this->atts ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Load validator script. |
| 216 | */ |
| 217 | public function load_validator() { |
| 218 | |
| 219 | // Assemble list of field properties for validation script. |
| 220 | $form_id = isset( $this->atts['form_id'] ) ? $this->atts['form_id'] : 1; |
| 221 | $form_fields = wpmtst_get_form_fields( $form_id ); |
| 222 | $fields = array(); |
| 223 | |
| 224 | foreach ( $form_fields as $field ) { |
| 225 | |
| 226 | $fields[] = array( |
| 227 | 'name' => $field['name'], |
| 228 | 'type' => $field['input_type'], |
| 229 | 'required' => $field['required'] ?? null, |
| 230 | ); |
| 231 | |
| 232 | // Load rating stylesheet if necessary. |
| 233 | if ( isset( $field['input_type'] ) && 'rating' === $field['input_type'] ) { |
| 234 | WPMST()->render->add_style( 'wpmtst-rating-form' ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Load validation scripts |
| 239 | WPMST()->render->add_script( 'wpmtst-form-validation' ); |
| 240 | if ( wp_script_is( 'wpmtst-validation-lang', 'registered' ) ) { |
| 241 | WPMST()->render->add_script( 'wpmtst-validation-lang' ); |
| 242 | } |
| 243 | |
| 244 | $form_options = get_option( 'wpmtst_form_options' ); |
| 245 | |
| 246 | // Assemble script variable. |
| 247 | // Remember: top level is converted to strings! |
| 248 | $scroll = array( |
| 249 | 'onError' => $form_options['scrolltop_error'] ? true : false, |
| 250 | 'onErrorOffset' => $form_options['scrolltop_error_offset'], |
| 251 | 'onSuccess' => $form_options['scrolltop_success'] ? true : false, |
| 252 | 'onSuccessOffset' => $form_options['scrolltop_success_offset'], |
| 253 | ); |
| 254 | |
| 255 | $args = array( |
| 256 | 'scroll' => $scroll, |
| 257 | 'fields' => $fields, |
| 258 | ); |
| 259 | |
| 260 | if ( isset( $this->atts['form_ajax'] ) && $this->atts['form_ajax'] ) { |
| 261 | $args['ajaxUrl'] = admin_url( 'admin-ajax.php' ); |
| 262 | } |
| 263 | |
| 264 | WPMST()->render->add_script_var( 'wpmtst-form-validation', 'strongForm', $args ); |
| 265 | WPMST()->render->add_script( 'wpmtst-controller' ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | endif; |
| 270 |