abstracts
2 years ago
admin
2 years ago
elementor
4 years ago
export
3 years ago
fields
2 years ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
4 years ago
shortcodes
2 years ago
stats
3 years ago
templates
5 years ago
class-everest-forms.php
2 years ago
class-evf-ajax.php
2 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
6 years ago
class-evf-cron.php
3 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
2 years ago
class-evf-fields.php
2 years ago
class-evf-form-block.php
4 years ago
class-evf-form-handler.php
3 years ago
class-evf-form-task.php
2 years ago
class-evf-forms-features.php
2 years ago
class-evf-frontend-scripts.php
2 years ago
class-evf-install.php
2 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
5 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
2 years ago
class-evf-template-loader.php
2 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
3 years ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-frontend-scripts.php
339 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handle frontend scripts |
| 4 | * |
| 5 | * @class EVF_Frontend_Scripts |
| 6 | * @version 1.0.0 |
| 7 | * @package EverestForms/Classes/ |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * EVF_Frontend_Scripts Class. |
| 14 | */ |
| 15 | class EVF_Frontend_Scripts { |
| 16 | |
| 17 | /** |
| 18 | * Contains an array of script handles registered by EVF. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | private static $scripts = array(); |
| 23 | |
| 24 | /** |
| 25 | * Contains an array of script handles registered by EVF. |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | private static $styles = array(); |
| 30 | |
| 31 | /** |
| 32 | * Contains an array of script handles localized by EVF. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | private static $wp_localize_scripts = array(); |
| 37 | |
| 38 | /** |
| 39 | * Hook in methods. |
| 40 | */ |
| 41 | public static function init() { |
| 42 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) ); |
| 43 | add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 44 | add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get styles for the frontend. |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public static function get_styles() { |
| 53 | return apply_filters( |
| 54 | 'everest_forms_enqueue_styles', |
| 55 | array( |
| 56 | 'everest-forms-general' => array( |
| 57 | 'src' => self::get_asset_url( 'assets/css/everest-forms.css' ), |
| 58 | 'deps' => '', |
| 59 | 'version' => EVF_VERSION, |
| 60 | 'media' => 'all', |
| 61 | 'has_rtl' => true, |
| 62 | ), |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return asset URL. |
| 69 | * |
| 70 | * @param string $path Assets path. |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | private static function get_asset_url( $path ) { |
| 75 | return apply_filters( 'everest_forms_get_asset_url', plugins_url( $path, EVF_PLUGIN_FILE ), $path ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Register a script for use. |
| 80 | * |
| 81 | * @uses wp_register_script() |
| 82 | * @param string $handle Name of the script. Should be unique. |
| 83 | * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory. |
| 84 | * @param string[] $deps An array of registered script handles this script depends on. |
| 85 | * @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 86 | * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
| 87 | */ |
| 88 | private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = EVF_VERSION, $in_footer = true ) { |
| 89 | self::$scripts[] = $handle; |
| 90 | wp_register_script( $handle, $path, $deps, $version, $in_footer ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Register and enqueue a script for use. |
| 95 | * |
| 96 | * @uses wp_enqueue_script() |
| 97 | * @param string $handle Name of the script. Should be unique. |
| 98 | * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory. |
| 99 | * @param string[] $deps An array of registered script handles this script depends on. |
| 100 | * @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 101 | * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
| 102 | */ |
| 103 | private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = EVF_VERSION, $in_footer = true ) { |
| 104 | if ( ! in_array( $handle, self::$scripts, true ) && $path ) { |
| 105 | self::register_script( $handle, $path, $deps, $version, $in_footer ); |
| 106 | } |
| 107 | wp_enqueue_script( $handle ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Register a style for use. |
| 112 | * |
| 113 | * @uses wp_register_style() |
| 114 | * @param string $handle Name of the stylesheet. Should be unique. |
| 115 | * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 116 | * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on. |
| 117 | * @param string $version String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 118 | * @param string $media The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 119 | * @param boolean $has_rtl If has RTL version to load too. |
| 120 | */ |
| 121 | private static function register_style( $handle, $path, $deps = array(), $version = EVF_VERSION, $media = 'all', $has_rtl = false ) { |
| 122 | self::$styles[] = $handle; |
| 123 | wp_register_style( $handle, $path, $deps, $version, $media ); |
| 124 | |
| 125 | if ( $has_rtl ) { |
| 126 | wp_style_add_data( $handle, 'rtl', 'replace' ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Register and enqueue a styles for use. |
| 132 | * |
| 133 | * @uses wp_enqueue_style() |
| 134 | * @param string $handle Name of the stylesheet. Should be unique. |
| 135 | * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 136 | * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on. |
| 137 | * @param string $version String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 138 | * @param string $media The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 139 | * @param boolean $has_rtl If has RTL version to load too. |
| 140 | */ |
| 141 | private static function enqueue_style( $handle, $path = '', $deps = array(), $version = EVF_VERSION, $media = 'all', $has_rtl = false ) { |
| 142 | if ( ! in_array( $handle, self::$styles, true ) && $path ) { |
| 143 | self::register_style( $handle, $path, $deps, $version, $media, $has_rtl ); |
| 144 | } |
| 145 | wp_enqueue_style( $handle ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Register all EVF scripts. |
| 150 | */ |
| 151 | private static function register_scripts() { |
| 152 | if ( evf_is_amp() ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 157 | $register_scripts = array( |
| 158 | 'inputmask' => array( |
| 159 | 'src' => self::get_asset_url( 'assets/js/inputmask/jquery.inputmask.bundle' . $suffix . '.js' ), |
| 160 | 'deps' => array( 'jquery' ), |
| 161 | 'version' => '4.0.0-beta.58', |
| 162 | ), |
| 163 | 'flatpickr' => array( |
| 164 | 'src' => self::get_asset_url( 'assets/js/flatpickr/flatpickr' . $suffix . '.js' ), |
| 165 | 'deps' => array( 'jquery' ), |
| 166 | 'version' => '4.6.3', |
| 167 | ), |
| 168 | 'mailcheck' => array( |
| 169 | 'src' => self::get_asset_url( 'assets/js/mailcheck/mailcheck' . $suffix . '.js' ), |
| 170 | 'deps' => array( 'jquery' ), |
| 171 | 'version' => '1.1.2', |
| 172 | ), |
| 173 | 'selectWoo' => array( |
| 174 | 'src' => self::get_asset_url( 'assets/js/selectWoo/selectWoo.full' . $suffix . '.js' ), |
| 175 | 'deps' => array( 'jquery' ), |
| 176 | 'version' => '1.0.8', |
| 177 | ), |
| 178 | 'jquery-validate' => array( |
| 179 | 'src' => self::get_asset_url( 'assets/js/jquery-validate/jquery.validate' . $suffix . '.js' ), |
| 180 | 'deps' => array( 'jquery' ), |
| 181 | 'version' => '1.19.2', |
| 182 | ), |
| 183 | 'everest-forms' => array( |
| 184 | 'src' => self::get_asset_url( 'assets/js/frontend/everest-forms' . $suffix . '.js' ), |
| 185 | 'deps' => array( 'jquery', 'inputmask', 'jquery-validate' ), |
| 186 | 'version' => EVF_VERSION, |
| 187 | ), |
| 188 | 'everest-forms-text-limit' => array( |
| 189 | 'src' => self::get_asset_url( 'assets/js/frontend/text-limit' . $suffix . '.js' ), |
| 190 | 'deps' => array(), |
| 191 | 'version' => EVF_VERSION, |
| 192 | ), |
| 193 | 'everest-forms-ajax-submission' => array( |
| 194 | 'src' => self::get_asset_url( 'assets/js/frontend/ajax-submission' . $suffix . '.js' ), |
| 195 | 'deps' => array( 'jquery', 'inputmask', 'jquery-validate' ), |
| 196 | 'version' => EVF_VERSION, |
| 197 | ), |
| 198 | ); |
| 199 | foreach ( $register_scripts as $name => $props ) { |
| 200 | self::register_script( $name, $props['src'], $props['deps'], $props['version'] ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Register all EVF sty;es. |
| 206 | */ |
| 207 | private static function register_styles() { |
| 208 | $register_styles = array( |
| 209 | 'evf_select2' => array( |
| 210 | 'src' => self::get_asset_url( 'assets/css/select2.css' ), |
| 211 | 'deps' => array(), |
| 212 | 'version' => EVF_VERSION, |
| 213 | 'has_rtl' => false, |
| 214 | ), |
| 215 | 'flatpickr' => array( |
| 216 | 'src' => self::get_asset_url( 'assets/css/flatpickr.css' ), |
| 217 | 'deps' => array(), |
| 218 | 'version' => EVF_VERSION, |
| 219 | 'has_rtl' => false, |
| 220 | ), |
| 221 | ); |
| 222 | foreach ( $register_styles as $name => $props ) { |
| 223 | self::register_style( $name, $props['src'], $props['deps'], $props['version'], 'all', $props['has_rtl'] ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Register/queue frontend scripts. |
| 229 | */ |
| 230 | public static function load_scripts() { |
| 231 | global $post; |
| 232 | |
| 233 | if ( ! did_action( 'before_everest_forms_init' ) ) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | self::register_scripts(); |
| 238 | self::register_styles(); |
| 239 | |
| 240 | // Enqueue dashicons. |
| 241 | wp_enqueue_style( 'dashicons' ); |
| 242 | |
| 243 | // CSS Styles. |
| 244 | $enqueue_styles = self::get_styles(); |
| 245 | if ( $enqueue_styles ) { |
| 246 | foreach ( $enqueue_styles as $handle => $args ) { |
| 247 | if ( ! isset( $args['has_rtl'] ) ) { |
| 248 | $args['has_rtl'] = false; |
| 249 | } |
| 250 | |
| 251 | self::enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'], $args['has_rtl'] ); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Localize a EVF script once. |
| 258 | * |
| 259 | * @param string $handle Script handle the data will be attached to. |
| 260 | */ |
| 261 | private static function localize_script( $handle ) { |
| 262 | if ( ! in_array( $handle, self::$wp_localize_scripts, true ) && wp_script_is( $handle ) ) { |
| 263 | $data = self::get_script_data( $handle ); |
| 264 | |
| 265 | if ( ! $data ) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | $name = str_replace( '-', '_', $handle ) . '_params'; |
| 270 | self::$wp_localize_scripts[] = $handle; |
| 271 | wp_localize_script( $handle, $name, apply_filters( $name, $data ) ); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Return data for script handles. |
| 277 | * |
| 278 | * @param string $handle Script handle the data will be attached to. |
| 279 | * @return array|bool |
| 280 | */ |
| 281 | private static function get_script_data( $handle ) { |
| 282 | switch ( $handle ) { |
| 283 | case 'everest-forms': |
| 284 | $params = array( |
| 285 | 'ajax_url' => evf()->ajax_url(), |
| 286 | 'submit' => esc_html__( 'Submit', 'everest-forms' ), |
| 287 | 'disable_user_details' => get_option( 'everest_forms_disable_user_details' ), |
| 288 | 'everest_forms_data_save' => wp_create_nonce( 'everest_forms_data_save_nonce' ), |
| 289 | 'everest_forms_slot_booking' => wp_create_nonce( 'everest_forms_slot_booking_nonce' ), |
| 290 | 'i18n_messages_required' => get_option( 'everest_forms_required_validation' ), |
| 291 | 'i18n_messages_url' => get_option( 'everest_forms_url_validation' ), |
| 292 | 'i18n_messages_email' => get_option( 'everest_forms_email_validation' ), |
| 293 | 'i18n_messages_email_suggestion' => get_option( 'everest_forms_email_suggestion', esc_html__( 'Did you mean {suggestion}?', 'everest-forms' ) ), |
| 294 | 'i18n_messages_email_suggestion_title' => esc_attr__( 'Click to accept this suggestion.', 'everest-forms' ), |
| 295 | 'i18n_messages_confirm' => get_option( 'everest_forms_confirm_validation', __( 'Field values do not match.', 'everest-forms' ) ), |
| 296 | 'i18n_messages_check_limit' => get_option( 'everest_forms_check_limit_validation', esc_html__( 'You have exceeded number of allowed selections: {#}.', 'everest-forms' ) ), |
| 297 | 'i18n_messages_number' => get_option( 'everest_forms_number_validation' ), |
| 298 | 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'everest-forms' ), |
| 299 | 'mailcheck_enabled' => (bool) apply_filters( 'everest_forms_mailcheck_enabled', true ), |
| 300 | 'mailcheck_domains' => array_map( 'sanitize_text_field', (array) apply_filters( 'everest_forms_mailcheck_domains', array() ) ), |
| 301 | 'mailcheck_toplevel_domains' => array_map( 'sanitize_text_field', (array) apply_filters( 'everest_forms_mailcheck_toplevel_domains', array( 'dev' ) ) ), |
| 302 | 'il8n_min_word_length_err_msg' => esc_html__( 'Please enter at least {0} words.', 'everest-forms' ), |
| 303 | ); |
| 304 | break; |
| 305 | case 'everest-forms-text-limit': |
| 306 | $params = array( |
| 307 | 'i18n_messages_limit_characters' => esc_html__( '{count} of {limit} max characters.', 'everest-forms' ), |
| 308 | 'i18n_messages_limit_words' => esc_html__( '{count} of {limit} max words.', 'everest-forms' ), |
| 309 | ); |
| 310 | break; |
| 311 | case 'everest-forms-ajax-submission': |
| 312 | $params = array( |
| 313 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 314 | 'evf_ajax_submission' => wp_create_nonce( 'everest_forms_ajax_form_submission' ), |
| 315 | 'submit' => esc_html__( 'Submit', 'everest-forms' ), |
| 316 | 'error' => esc_html__( 'Something went wrong while making an AJAX submission', 'everest-forms' ), |
| 317 | 'required' => esc_html__( 'This field is required.', 'everest-forms' ), |
| 318 | 'pdf_download' => esc_html__( 'Click here to download your pdf submission', 'everest-forms' ), |
| 319 | ); |
| 320 | break; |
| 321 | default: |
| 322 | $params = false; |
| 323 | } |
| 324 | |
| 325 | return apply_filters( 'everest_forms_get_script_data', $params, $handle ); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Localize scripts only when enqueued. |
| 330 | */ |
| 331 | public static function localize_printed_scripts() { |
| 332 | foreach ( self::$scripts as $handle ) { |
| 333 | self::localize_script( $handle ); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | EVF_Frontend_Scripts::init(); |
| 339 |