PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.7.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.7.2
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 0.0.3 All 96 releases
sureforms / inc / frontend-assets.php
frontend-assets.php
318 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SureForms Public Class.
4 *
5 * Class file for public functions.
6 *
7 * @package SureForms
8 */
9
10 namespace SRFM\Inc;
11
12 use SRFM\Inc\Traits\Get_Instance;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 /**
19 * Public Class
20 *
21 * @since 0.0.1
22 */
23 class Frontend_Assets {
24 use Get_Instance;
25
26 /**
27 * JS Assets.
28 *
29 * @since 0.0.11
30 * @var array<string>
31 */
32 public static $js_assets = [
33 'form-submit' => 'formSubmit',
34 'frontend' => 'frontend',
35 ];
36
37 /**
38 * CSS Assets.
39 *
40 * @since 0.0.11
41 * @var array<string>
42 */
43 public static $css_assets = [
44 'frontend-default' => 'blocks/default/frontend',
45 'common' => 'common',
46 'form' => 'frontend/form',
47 'single' => 'single',
48 ];
49
50 /**
51 * External CSS Assets.
52 *
53 * @since 0.0.11
54 * @var array<string>
55 */
56 public static $css_external_assets = [
57 'tom-select' => 'tom-select',
58 'intl-tel-input' => 'intl/intlTelInput.min',
59 ];
60
61 /**
62 * Constructor
63 *
64 * @since 0.0.1
65 */
66 public function __construct() {
67 add_filter( 'template_include', [ $this, 'page_template' ], PHP_INT_MAX );
68 add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] );
69 add_filter( 'render_block', [ $this, 'generate_render_script' ], 10, 2 );
70 }
71
72 /**
73 * Enqueue Script.
74 *
75 * @return void
76 * @since 0.0.1
77 */
78 public function register_scripts() {
79 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
80 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
81 $js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/';
82 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
83 $css_vendor = SRFM_URL . 'assets/css/minified/deps/';
84 $is_rtl = is_rtl();
85 $rtl = $is_rtl ? '-rtl' : '';
86
87 $security_setting_options = get_option( 'srfm_security_settings_options' );
88 $is_set_v2_site_key = false;
89 if ( is_array( $security_setting_options ) && isset( $security_setting_options['srfm_v2_invisible_site_key'] ) && ! empty( $security_setting_options['srfm_v2_invisible_site_key'] ) ) {
90 $is_set_v2_site_key = true;
91 }
92
93 // Styles based on meta style.
94 foreach ( self::$css_assets as $handle => $path ) {
95 wp_register_style( SRFM_SLUG . '-' . $handle, $css_uri . $path . $file_prefix . $rtl . '.css', [], SRFM_VER );
96 }
97
98 // External styles.
99 foreach ( self::$css_external_assets as $handle => $path ) {
100 wp_register_style( SRFM_SLUG . '-' . $handle, $css_vendor . $path . '.css', [], SRFM_VER );
101 }
102
103 // Scripts.
104 foreach ( self::$js_assets as $handle => $name ) {
105 if ( 'form-submit' === $handle ) {
106 wp_register_script(
107 SRFM_SLUG . '-' . $handle,
108 SRFM_URL . 'assets/build/' . $name . '.js',
109 [ 'wp-api-fetch' ],
110 SRFM_VER,
111 true
112 );
113 } else {
114 wp_register_script(
115 SRFM_SLUG . '-' . $handle,
116 $js_uri . $name . $file_prefix . '.js',
117 [],
118 SRFM_VER,
119 true
120 );
121 }
122 }
123
124 wp_localize_script(
125 SRFM_SLUG . '-form-submit',
126 SRFM_SLUG . '_submit',
127 [
128 'site_url' => site_url(),
129 'nonce' => wp_create_nonce( 'wp_rest' ),
130 'messages' => array_merge(
131 Translatable::get_frontend_validation_messages(),
132 [
133 'srfm_turnstile_error_message' => __( 'Turnstile sitekey verification failed. Please contact your site administrator.', 'sureforms' ),
134 'srfm_google_captcha_error_message' => __( 'Google Captcha sitekey verification failed. Please contact your site administrator.', 'sureforms' ),
135 'srfm_captcha_h_error_message' => __( 'HCaptcha sitekey verification failed. Please contact your site administrator.', 'sureforms' ),
136 ]
137 ),
138 'is_rtl' => $is_rtl,
139 ]
140 );
141
142 $current_post = get_post();
143
144 // Let's conditionally load form assets if current requested page has our forms.
145 if ( $current_post instanceof \WP_Post ) {
146 // Handles condition for Instant Form, Block Embedded, and Shortcode Embedded forms.
147 $load_assets = ( SRFM_FORMS_POST_TYPE === $current_post->post_type || ( false !== strpos( $current_post->post_content, 'wp:srfm/form' ) || has_shortcode( $current_post->post_content, 'sureforms' ) ) );
148
149 if ( $load_assets ) {
150 // Load needed styles in head tag if current requested page has SureForms form.
151 self::enqueue_scripts_and_styles();
152 }
153 }
154 }
155
156 /**
157 * Enqueue scripts and styles.
158 *
159 * @return void
160 * @since 0.0.11
161 */
162 public static function enqueue_scripts_and_styles() {
163 // Load the styles.
164 foreach ( self::$css_assets as $handle => $path ) {
165
166 // Skip single form styles if not on single form page.
167 if ( 'single' === $handle && ! is_singular( SRFM_FORMS_POST_TYPE ) ) {
168 continue;
169 }
170
171 wp_enqueue_style( SRFM_SLUG . '-' . $handle );
172 }
173
174 // Load the external styles. Like Phone and Tom Select.
175 foreach ( self::$css_external_assets as $handle => $path ) {
176 wp_enqueue_style( SRFM_SLUG . '-' . $handle );
177 }
178
179 // Load the scripts.
180 foreach ( self::$js_assets as $handle => $path ) {
181 wp_enqueue_script( SRFM_SLUG . '-' . $handle );
182 }
183 }
184
185 /**
186 * Enqueue block scripts
187 *
188 * @param string $block_type block name.
189 * @param array<string, mixed> $attr Array of block attributes.
190 * @since 0.0.1
191 * @return void
192 */
193 public function enqueue_srfm_script( $block_type, $attr ) {
194 $block_name = str_replace( 'srfm/', '', $block_type );
195 // associative array to keep the count of block that requires scripts to work.
196 $script_dep_blocks = [
197 'dropdown' => 0,
198 'multi-choice' => 0,
199 'number' => 0,
200 'textarea' => 0,
201 'url' => 0,
202 'phone' => 0,
203 'input' => 0,
204 ];
205
206 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
207 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
208
209 // Check if block is in the array and check if block is already enqueued.
210 if (
211 in_array( $block_name, array_keys( $script_dep_blocks ), true ) &&
212 0 === $script_dep_blocks[ $block_name ]
213 ) {
214 $script_dep_blocks[ $block_name ] += 1;
215 $js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/blocks/';
216 $js_vendor_uri = SRFM_URL . 'assets/js/minified/deps/';
217 $css_vendor_uri = SRFM_URL . 'assets/css/minified/deps/';
218 if ( 'phone' === $block_name
219 ) {
220 wp_enqueue_script( SRFM_SLUG . "-{$block_name}-intl-input-deps", $js_vendor_uri . 'intl/intTelInputWithUtils.min.js', [], SRFM_VER, true );
221 }
222
223 if ( 'dropdown' === $block_name ) {
224 // if the dropdown / address-compact block is after any other block, then we need to dequeue the srfm-form-submit script and enqueue it again and load it with tom-select dependency.
225 wp_dequeue_script( SRFM_SLUG . '-form-submit' );
226 wp_enqueue_script( SRFM_SLUG . '-dropdown', $js_uri . 'dropdown' . $file_prefix . '.js', [ 'wp-a11y' ], SRFM_VER, true );
227 wp_enqueue_script( SRFM_SLUG . '-tom-select', $js_vendor_uri . 'tom-select.min.js', [], SRFM_VER, true );
228 // frontend utils using dropdown dependency.
229 wp_enqueue_script(
230 SRFM_SLUG . '-form-submit',
231 SRFM_URL . 'assets/build/formSubmit.js',
232 [
233 'srfm-tom-select',
234 'srfm-dropdown',
235 'wp-api-fetch',
236 ],
237 SRFM_VER,
238 true
239 );
240 }
241
242 if ( 'dropdown' !== $block_name ) {
243 wp_enqueue_script( SRFM_SLUG . "-{$block_name}", $js_uri . $block_name . $file_prefix . '.js', [], SRFM_VER, true );
244 }
245
246 if ( 'input' === $block_name ) {
247 // Input mask JS.
248 wp_enqueue_script( SRFM_SLUG . '-inputmask', $js_vendor_uri . 'inputmask.min.js', [], SRFM_VER, true );
249 }
250
251 // Adding js for the input textarea block.
252 if ( 'textarea' === $block_name && ! empty( $attr['isRichText'] ) ) {
253 wp_enqueue_script( SRFM_SLUG . '-quill-editor', $js_vendor_uri . '/quill.min.js', [], SRFM_VER, true );
254
255 wp_enqueue_style( SRFM_SLUG . '-quill-editor', $css_vendor_uri . 'quill/quill.snow.css', [], SRFM_VER );
256 }
257 }
258 /**
259 * Enqueueing the input mask JS for input and date-picker blocks.
260 * This is a workaround for the input mask JS to work with the date-picker block.
261 * Not adding in the above existing condition because code only runs when free block are added in the form.
262 * Aim is to reduce redundant code and library file duplication.
263 */
264 if ( 'date-picker' === $block_name ) {
265 // Input mask JS.
266 wp_enqueue_script( SRFM_SLUG . '-inputmask', SRFM_URL . 'assets/js/minified/deps/inputmask.min.js', [], SRFM_VER, true );
267 }
268 }
269
270 /**
271 * Render function.
272 *
273 * @param string $block_content Entire Block Content.
274 * @param array<string> $block Block Properties As An Array.
275 * @return string
276 */
277 public function generate_render_script( $block_content, $block ) {
278
279 if ( isset( $block['attrs']['isEditing'] ) ) {
280 // Only load block assets on the frontend.
281 return $block_content;
282 }
283
284 if ( isset( $block['blockName'] ) ) {
285 $attr = is_array( $block['attrs'] ) ? $block['attrs'] : [];
286
287 self::enqueue_srfm_script( $block['blockName'], $attr );
288 }
289 return $block_content;
290 }
291
292 /**
293 * Form Template filter.
294 *
295 * @param string $template Template.
296 * @return string Template.
297 * @since 0.0.1
298 */
299 public function page_template( $template ) {
300
301 if ( ! is_singular( SRFM_FORMS_POST_TYPE ) ) {
302 // Bail if not SureForms post type.
303 return $template;
304 }
305
306 $file_name = 'single-form.php';
307 $template = locate_template( $file_name );
308
309 /**
310 * Hook: srfm_form_template filter.
311 *
312 * @since 0.0.1
313 */
314 return apply_filters( 'srfm_form_template', $template ? $template : SRFM_DIR . '/templates/' . $file_name );
315 }
316
317 }
318