| 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 |
* Constructor |
| 29 |
* |
| 30 |
* @since 0.0.1 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
add_filter( 'template_include', [ $this, 'page_template' ], PHP_INT_MAX ); |
| 34 |
|
| 35 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 36 |
add_filter( 'render_block', [ $this, 'generate_render_script' ], 10, 2 ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue Script. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* @since 0.0.1 |
| 44 |
*/ |
| 45 |
public function enqueue_scripts() { |
| 46 |
$file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 47 |
$dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 48 |
$js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/'; |
| 49 |
$css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/'; |
| 50 |
$css_vendor = SRFM_URL . 'assets/css/minified/deps/'; |
| 51 |
|
| 52 |
/* RTL */ |
| 53 |
if ( is_rtl() ) { |
| 54 |
$file_prefix .= '-rtl'; |
| 55 |
} |
| 56 |
|
| 57 |
$security_setting_options = get_option( 'srfm_security_settings_options' ); |
| 58 |
$is_set_v2_site_key = false; |
| 59 |
if ( is_array( $security_setting_options ) && isset( $security_setting_options['srfm_v2_invisible_site_key'] ) && ! empty( $security_setting_options['srfm_v2_invisible_site_key'] ) ) { |
| 60 |
$is_set_v2_site_key = true; |
| 61 |
} |
| 62 |
|
| 63 |
// Styles based on meta style. |
| 64 |
wp_enqueue_style( SRFM_SLUG . '-frontend-default', $css_uri . '/blocks/default/frontend' . $file_prefix . '.css', [], SRFM_VER ); |
| 65 |
|
| 66 |
// Common styles for all meta styles. |
| 67 |
wp_enqueue_style( SRFM_SLUG . '-common', $css_uri . 'common' . $file_prefix . '.css', [], SRFM_VER, 'all' ); |
| 68 |
wp_enqueue_style( SRFM_SLUG . '-form', $css_uri . 'frontend/form' . $file_prefix . '.css', [], SRFM_VER, 'all' ); |
| 69 |
|
| 70 |
if ( is_single() ) { |
| 71 |
wp_enqueue_style( SRFM_SLUG . '-single', $css_uri . 'single' . $file_prefix . '.css', [], SRFM_VER ); |
| 72 |
} |
| 73 |
|
| 74 |
// Dependencies |
| 75 |
// Nice Select CSS. |
| 76 |
wp_enqueue_style( SRFM_SLUG . '-tom-select', $css_vendor . 'tom-select.css', [], SRFM_VER ); |
| 77 |
// Int-tel-input CSS. |
| 78 |
wp_enqueue_style( SRFM_SLUG . '-intl-tel-input', $css_vendor . 'intl/intlTelInput.min.css', [], SRFM_VER ); |
| 79 |
|
| 80 |
wp_enqueue_script( SRFM_SLUG . '-form-submit', SRFM_URL . 'assets/build/formSubmit.js', [], SRFM_VER, true ); |
| 81 |
// Frontend common and validation before submit. |
| 82 |
wp_enqueue_script( SRFM_SLUG . '-frontend', $js_uri . 'frontend.min.js', [], SRFM_VER, true ); |
| 83 |
|
| 84 |
wp_localize_script( |
| 85 |
SRFM_SLUG . '-form-submit', |
| 86 |
SRFM_SLUG . '_submit', |
| 87 |
[ |
| 88 |
'site_url' => site_url(), |
| 89 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 90 |
] |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Enqueue block scripts |
| 96 |
* |
| 97 |
* @param string $block_type block name. |
| 98 |
* @since 0.0.1 |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function enqueue_srfm_script( $block_type ) { |
| 102 |
$block_name = str_replace( 'srfm/', '', $block_type ); |
| 103 |
$script_dep_blocks = [ 'address-compact', 'checkbox', 'dropdown', 'multi-choice', 'number', 'textarea', 'url', 'phone' ]; |
| 104 |
|
| 105 |
$file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 106 |
$dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 107 |
|
| 108 |
if ( in_array( $block_name, $script_dep_blocks, true ) ) { |
| 109 |
$js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/blocks/'; |
| 110 |
$js_vendor_uri = SRFM_URL . 'assets/js/minified/deps/'; |
| 111 |
|
| 112 |
if ( 'phone' === $block_name ) { |
| 113 |
wp_enqueue_script( SRFM_SLUG . "-{$block_name}-intl-input-deps", $js_vendor_uri . 'intl/intTelInput.min.js', [], SRFM_VER, true ); |
| 114 |
wp_enqueue_script( SRFM_SLUG . "-{$block_name}-intl-utils-deps", $js_vendor_uri . 'intl/intTelUtils.min.js', [], SRFM_VER, true ); |
| 115 |
} |
| 116 |
|
| 117 |
if ( 'dropdown' === $block_name || 'address-compact' === $block_name ) { |
| 118 |
wp_enqueue_script( SRFM_SLUG . '-dropdown', $js_uri . 'dropdown' . $file_prefix . '.js', [], SRFM_VER, true ); |
| 119 |
wp_enqueue_script( SRFM_SLUG . '-tom-select', $js_vendor_uri . 'tom-select.min.js', [], SRFM_VER, true ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( 'dropdown' !== $block_name ) { |
| 123 |
wp_enqueue_script( SRFM_SLUG . "-{$block_name}", $js_uri . $block_name . $file_prefix . '.js', [], SRFM_VER, true ); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Render function. |
| 130 |
* |
| 131 |
* @param string $block_content Entire Block Content. |
| 132 |
* @param array<string> $block Block Properties As An Array. |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function generate_render_script( $block_content, $block ) { |
| 136 |
|
| 137 |
if ( isset( $block['blockName'] ) ) { |
| 138 |
self::enqueue_srfm_script( $block['blockName'] ); |
| 139 |
} |
| 140 |
return $block_content; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Form Template filter. |
| 145 |
* |
| 146 |
* @param string $template Template. |
| 147 |
* @return string Template. |
| 148 |
* @since 0.0.1 |
| 149 |
*/ |
| 150 |
public function page_template( $template ) { |
| 151 |
if ( is_singular( SRFM_FORMS_POST_TYPE ) ) { |
| 152 |
$file_name = 'single-form.php'; |
| 153 |
$template = locate_template( $file_name ) ? locate_template( $file_name ) : SRFM_DIR . '/templates/' . $file_name; |
| 154 |
$template = apply_filters( 'srfm_form_template', $template ); |
| 155 |
} |
| 156 |
return $template; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|