| 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 |
|
| 25 |
use Get_Instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* JS Assets. |
| 29 |
* |
| 30 |
* @since 0.0.11 |
| 31 |
* @var array<string> |
| 32 |
*/ |
| 33 |
public static $js_assets = [ |
| 34 |
'form-submit' => 'formSubmit.js', |
| 35 |
'frontend' => 'frontend.min.js', |
| 36 |
]; |
| 37 |
|
| 38 |
/** |
| 39 |
* CSS Assets. |
| 40 |
* |
| 41 |
* @since 0.0.11 |
| 42 |
* @var array<string> |
| 43 |
*/ |
| 44 |
public static $css_assets = [ |
| 45 |
'frontend-default' => 'blocks/default/frontend', |
| 46 |
'common' => 'common', |
| 47 |
'form' => 'frontend/form', |
| 48 |
'single' => 'single', |
| 49 |
]; |
| 50 |
|
| 51 |
/** |
| 52 |
* External CSS Assets. |
| 53 |
* |
| 54 |
* @since 0.0.11 |
| 55 |
* @var array<string> |
| 56 |
*/ |
| 57 |
public static $css_external_assets = [ |
| 58 |
'tom-select' => 'tom-select', |
| 59 |
'intl-tel-input' => 'intl/intlTelInput.min', |
| 60 |
]; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Constructor |
| 65 |
* |
| 66 |
* @since 0.0.1 |
| 67 |
*/ |
| 68 |
public function __construct() { |
| 69 |
add_filter( 'template_include', [ $this, 'page_template' ], PHP_INT_MAX ); |
| 70 |
add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] ); |
| 71 |
add_filter( 'render_block', [ $this, 'generate_render_script' ], 10, 2 ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Enqueue Script. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
* @since 0.0.1 |
| 79 |
*/ |
| 80 |
public function register_scripts() { |
| 81 |
$file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 82 |
$dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 83 |
$js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/'; |
| 84 |
$css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/'; |
| 85 |
$css_vendor = SRFM_URL . 'assets/css/minified/deps/'; |
| 86 |
|
| 87 |
/* RTL */ |
| 88 |
if ( is_rtl() ) { |
| 89 |
$file_prefix .= '-rtl'; |
| 90 |
} |
| 91 |
|
| 92 |
$security_setting_options = get_option( 'srfm_security_settings_options' ); |
| 93 |
$is_set_v2_site_key = false; |
| 94 |
if ( is_array( $security_setting_options ) && isset( $security_setting_options['srfm_v2_invisible_site_key'] ) && ! empty( $security_setting_options['srfm_v2_invisible_site_key'] ) ) { |
| 95 |
$is_set_v2_site_key = true; |
| 96 |
} |
| 97 |
|
| 98 |
// Styles based on meta style. |
| 99 |
foreach ( self::$css_assets as $handle => $path ) { |
| 100 |
wp_register_style( SRFM_SLUG . '-' . $handle, $css_uri . $path . $file_prefix . '.css', [], SRFM_VER ); |
| 101 |
} |
| 102 |
|
| 103 |
// External styles. |
| 104 |
foreach ( self::$css_external_assets as $handle => $path ) { |
| 105 |
wp_register_style( SRFM_SLUG . '-' . $handle, $css_vendor . $path . '.css', [], SRFM_VER ); |
| 106 |
} |
| 107 |
|
| 108 |
// Scripts. |
| 109 |
foreach ( self::$js_assets as $handle => $name ) { |
| 110 |
if ( 'form-submit' === $handle ) { |
| 111 |
wp_register_script( |
| 112 |
SRFM_SLUG . '-' . $handle, |
| 113 |
SRFM_URL . 'assets/build/' . $name, |
| 114 |
[], |
| 115 |
SRFM_VER, |
| 116 |
true |
| 117 |
); |
| 118 |
} else { |
| 119 |
wp_register_script( |
| 120 |
SRFM_SLUG . '-' . $handle, |
| 121 |
$js_uri . $name, |
| 122 |
[], |
| 123 |
SRFM_VER, |
| 124 |
true |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
wp_localize_script( |
| 130 |
SRFM_SLUG . '-form-submit', |
| 131 |
SRFM_SLUG . '_submit', |
| 132 |
[ |
| 133 |
'site_url' => site_url(), |
| 134 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 135 |
] |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Enqueue scripts and styles. |
| 141 |
* |
| 142 |
* @return void |
| 143 |
* @since 0.0.11 |
| 144 |
*/ |
| 145 |
public static function enqueue_scripts_and_styles() { |
| 146 |
// Load the styles. |
| 147 |
foreach ( self::$css_assets as $handle => $path ) { |
| 148 |
|
| 149 |
// Skip single form styles if not on single form page. |
| 150 |
if ( 'single' === $handle && ! is_singular( SRFM_FORMS_POST_TYPE ) ) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
wp_enqueue_style( SRFM_SLUG . '-' . $handle ); |
| 155 |
} |
| 156 |
|
| 157 |
// Load the external styles. Like Phone and Tom Select. |
| 158 |
foreach ( self::$css_external_assets as $handle => $path ) { |
| 159 |
wp_enqueue_style( SRFM_SLUG . '-' . $handle ); |
| 160 |
} |
| 161 |
|
| 162 |
// Load the scripts. |
| 163 |
foreach ( self::$js_assets as $handle => $path ) { |
| 164 |
wp_enqueue_script( SRFM_SLUG . '-' . $handle ); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Enqueue block scripts |
| 170 |
* |
| 171 |
* @param string $block_type block name. |
| 172 |
* @since 0.0.1 |
| 173 |
* @return void |
| 174 |
*/ |
| 175 |
public function enqueue_srfm_script( $block_type ) { |
| 176 |
$block_name = str_replace( 'srfm/', '', $block_type ); |
| 177 |
// associative array to keep the count of block that requires scripts to work. |
| 178 |
$script_dep_blocks = [ |
| 179 |
'dropdown' => 0, |
| 180 |
'multi-choice' => 0, |
| 181 |
'number' => 0, |
| 182 |
'textarea' => 0, |
| 183 |
'url' => 0, |
| 184 |
'phone' => 0, |
| 185 |
'input' => 0, |
| 186 |
]; |
| 187 |
|
| 188 |
$file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 189 |
$dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 190 |
|
| 191 |
// Check if block is in the array and check if block is already enqueued. |
| 192 |
if ( |
| 193 |
in_array( $block_name, array_keys( $script_dep_blocks ), true ) && |
| 194 |
0 === $script_dep_blocks[ $block_name ] |
| 195 |
) { |
| 196 |
$script_dep_blocks[ $block_name ] += 1; |
| 197 |
$js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/blocks/'; |
| 198 |
$js_vendor_uri = SRFM_URL . 'assets/js/minified/deps/'; |
| 199 |
|
| 200 |
if ( 'phone' === $block_name |
| 201 |
) { |
| 202 |
wp_enqueue_script( SRFM_SLUG . "-{$block_name}-intl-input-deps", $js_vendor_uri . 'intl/intTelInput.min.js', [], SRFM_VER, true ); |
| 203 |
wp_enqueue_script( SRFM_SLUG . "-{$block_name}-intl-utils-deps", $js_vendor_uri . 'intl/intTelUtils.min.js', [], SRFM_VER, true ); |
| 204 |
} |
| 205 |
|
| 206 |
if ( 'dropdown' === $block_name ) { |
| 207 |
// 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. |
| 208 |
wp_dequeue_script( SRFM_SLUG . '-form-submit' ); |
| 209 |
wp_enqueue_script( SRFM_SLUG . '-dropdown', $js_uri . 'dropdown' . $file_prefix . '.js', [ 'wp-a11y' ], SRFM_VER, true ); |
| 210 |
wp_enqueue_script( SRFM_SLUG . '-tom-select', $js_vendor_uri . 'tom-select.min.js', [], SRFM_VER, true ); |
| 211 |
// frontend utils using dropdown dependency. |
| 212 |
wp_enqueue_script( |
| 213 |
SRFM_SLUG . '-form-submit', |
| 214 |
SRFM_URL . 'assets/build/formSubmit.js', |
| 215 |
[ |
| 216 |
'srfm-tom-select', |
| 217 |
'srfm-dropdown', |
| 218 |
], |
| 219 |
SRFM_VER, |
| 220 |
true |
| 221 |
); |
| 222 |
|
| 223 |
wp_localize_script( |
| 224 |
SRFM_SLUG . '-form-submit', |
| 225 |
SRFM_SLUG . '_submit', |
| 226 |
[ |
| 227 |
'site_url' => site_url(), |
| 228 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 229 |
] |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
if ( 'dropdown' !== $block_name ) { |
| 234 |
wp_enqueue_script( SRFM_SLUG . "-{$block_name}", $js_uri . $block_name . $file_prefix . '.js', [], SRFM_VER, true ); |
| 235 |
} |
| 236 |
|
| 237 |
if ( 'input' === $block_name ) { |
| 238 |
// Input mask JS. |
| 239 |
wp_enqueue_script( SRFM_SLUG . '-inputmask', $js_vendor_uri . 'inputmask.min.js', [], SRFM_VER, true ); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Render function. |
| 246 |
* |
| 247 |
* @param string $block_content Entire Block Content. |
| 248 |
* @param array<string> $block Block Properties As An Array. |
| 249 |
* @return string |
| 250 |
*/ |
| 251 |
public function generate_render_script( $block_content, $block ) { |
| 252 |
|
| 253 |
if ( isset( $block['blockName'] ) ) { |
| 254 |
self::enqueue_srfm_script( $block['blockName'] ); |
| 255 |
} |
| 256 |
return $block_content; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Form Template filter. |
| 261 |
* |
| 262 |
* @param string $template Template. |
| 263 |
* @return string Template. |
| 264 |
* @since 0.0.1 |
| 265 |
*/ |
| 266 |
public function page_template( $template ) { |
| 267 |
|
| 268 |
if ( ! is_singular( SRFM_FORMS_POST_TYPE ) ) { |
| 269 |
// Bail if not SureForms post type. |
| 270 |
return $template; |
| 271 |
} |
| 272 |
|
| 273 |
$file_name = 'single-form.php'; |
| 274 |
$template = locate_template( $file_name ); |
| 275 |
|
| 276 |
/** |
| 277 |
* Hook: srfm_form_template filter. |
| 278 |
* |
| 279 |
* @since 0.0.1 |
| 280 |
*/ |
| 281 |
return apply_filters( 'srfm_form_template', $template ? $template : SRFM_DIR . '/templates/' . $file_name ); |
| 282 |
} |
| 283 |
|
| 284 |
} |
| 285 |
|