Helpers
11 months ago
Integrations
1 week ago
RestApi
2 weeks ago
abilities
2 weeks ago
abstracts
2 weeks ago
admin
2 weeks ago
blocks
1 year ago
elementor
2 years ago
export
2 months ago
fields
2 weeks ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
1 year ago
shortcodes
2 weeks ago
stats
5 months ago
templates
3 months ago
traits
2 weeks ago
class-everest-forms.php
1 week ago
class-evf-addon-upsell.php
2 weeks ago
class-evf-ajax.php
2 weeks ago
class-evf-autoloader.php
7 years ago
class-evf-background-process-import-entries.php
1 year ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
2 months ago
class-evf-cron.php
2 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-email-entries-report.php
3 months ago
class-evf-emails.php
2 weeks ago
class-evf-fields.php
2 weeks ago
class-evf-form-handler.php
2 weeks ago
class-evf-form-task.php
2 weeks ago
class-evf-forms-features.php
2 weeks ago
class-evf-frontend-scripts.php
2 weeks ago
class-evf-install.php
2 months ago
class-evf-integrations.php
3 months ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
1 year ago
class-evf-privacy.php
6 years ago
class-evf-report-cron.php
2 months ago
class-evf-reporting.php
2 months ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
1 year ago
class-evf-smart-tags.php
9 months ago
class-evf-template-loader.php
1 year ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 weeks ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
4 months 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
390 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 | 'jquery-intl-tel-input' => array( |
| 64 | 'src' => self::get_asset_url( 'assets/css/intlTelInput/intlTelInput.css' ), |
| 65 | 'deps' => array(), |
| 66 | 'version' => EVF_VERSION, |
| 67 | 'media' => 'all', |
| 68 | 'has_rtl' => false, |
| 69 | ), |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Return asset URL. |
| 76 | * |
| 77 | * @param string $path Assets path. |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | private static function get_asset_url( $path ) { |
| 82 | return apply_filters( 'everest_forms_get_asset_url', plugins_url( $path, EVF_PLUGIN_FILE ), $path ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Register a script for use. |
| 87 | * |
| 88 | * @uses wp_register_script() |
| 89 | * @param string $handle Name of the script. Should be unique. |
| 90 | * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory. |
| 91 | * @param string[] $deps An array of registered script handles this script depends on. |
| 92 | * @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. |
| 93 | * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
| 94 | */ |
| 95 | private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = EVF_VERSION, $in_footer = true ) { |
| 96 | self::$scripts[] = $handle; |
| 97 | wp_register_script( $handle, $path, $deps, $version, $in_footer ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Register and enqueue a script for use. |
| 102 | * |
| 103 | * @uses wp_enqueue_script() |
| 104 | * @param string $handle Name of the script. Should be unique. |
| 105 | * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory. |
| 106 | * @param string[] $deps An array of registered script handles this script depends on. |
| 107 | * @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. |
| 108 | * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
| 109 | */ |
| 110 | private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = EVF_VERSION, $in_footer = true ) { |
| 111 | if ( ! in_array( $handle, self::$scripts, true ) && $path ) { |
| 112 | self::register_script( $handle, $path, $deps, $version, $in_footer ); |
| 113 | } |
| 114 | wp_enqueue_script( $handle ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Register a style for use. |
| 119 | * |
| 120 | * @uses wp_register_style() |
| 121 | * @param string $handle Name of the stylesheet. Should be unique. |
| 122 | * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 123 | * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on. |
| 124 | * @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. |
| 125 | * @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)'. |
| 126 | * @param boolean $has_rtl If has RTL version to load too. |
| 127 | */ |
| 128 | private static function register_style( $handle, $path, $deps = array(), $version = EVF_VERSION, $media = 'all', $has_rtl = false ) { |
| 129 | self::$styles[] = $handle; |
| 130 | wp_register_style( $handle, $path, $deps, $version, $media ); |
| 131 | |
| 132 | if ( $has_rtl ) { |
| 133 | wp_style_add_data( $handle, 'rtl', 'replace' ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Register and enqueue a styles for use. |
| 139 | * |
| 140 | * @uses wp_enqueue_style() |
| 141 | * @param string $handle Name of the stylesheet. Should be unique. |
| 142 | * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 143 | * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on. |
| 144 | * @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. |
| 145 | * @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)'. |
| 146 | * @param boolean $has_rtl If has RTL version to load too. |
| 147 | */ |
| 148 | private static function enqueue_style( $handle, $path = '', $deps = array(), $version = EVF_VERSION, $media = 'all', $has_rtl = false ) { |
| 149 | if ( ! in_array( $handle, self::$styles, true ) && $path ) { |
| 150 | self::register_style( $handle, $path, $deps, $version, $media, $has_rtl ); |
| 151 | } |
| 152 | wp_enqueue_style( $handle ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Register all EVF scripts. |
| 157 | */ |
| 158 | private static function register_scripts() { |
| 159 | if ( evf_is_amp() ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 164 | $register_scripts = array( |
| 165 | 'inputmask' => array( |
| 166 | 'src' => self::get_asset_url( 'assets/js/inputmask/jquery.inputmask.bundle' . $suffix . '.js' ), |
| 167 | 'deps' => array( 'jquery' ), |
| 168 | 'version' => '4.0.0-beta.58', |
| 169 | ), |
| 170 | 'flatpickr' => array( |
| 171 | 'src' => self::get_asset_url( 'assets/js/flatpickr/flatpickr' . $suffix . '.js' ), |
| 172 | 'deps' => array( 'jquery' ), |
| 173 | 'version' => '4.6.3', |
| 174 | ), |
| 175 | 'mailcheck' => array( |
| 176 | 'src' => self::get_asset_url( 'assets/js/mailcheck/mailcheck' . $suffix . '.js' ), |
| 177 | 'deps' => array( 'jquery' ), |
| 178 | 'version' => '1.1.2', |
| 179 | ), |
| 180 | 'selectWoo' => array( |
| 181 | 'src' => self::get_asset_url( 'assets/js/selectWoo/selectWoo.full' . $suffix . '.js' ), |
| 182 | 'deps' => array( 'jquery' ), |
| 183 | 'version' => '1.0.8', |
| 184 | ), |
| 185 | 'jquery-intl-tel-input' => array( |
| 186 | 'src' => self::get_asset_url( '/assets/js/intlTelInput/jquery.intlTelInput' . $suffix . '.js' ), |
| 187 | 'deps' => array( 'jquery' ), |
| 188 | 'version' => '16.0.7', |
| 189 | ), |
| 190 | 'jquery-validate' => array( |
| 191 | 'src' => self::get_asset_url( 'assets/js/jquery-validate/jquery.validate' . $suffix . '.js' ), |
| 192 | 'deps' => array( 'jquery' ), |
| 193 | 'version' => '1.21.0', |
| 194 | ), |
| 195 | 'everest-forms' => array( |
| 196 | 'src' => self::get_asset_url( 'assets/js/frontend/everest-forms' . $suffix . '.js' ), |
| 197 | 'deps' => array( 'jquery', 'inputmask', 'jquery-validate', 'jquery-intl-tel-input', 'selectWoo' ), |
| 198 | 'version' => EVF_VERSION, |
| 199 | ), |
| 200 | 'everest-forms-text-limit' => array( |
| 201 | 'src' => self::get_asset_url( 'assets/js/frontend/text-limit' . $suffix . '.js' ), |
| 202 | 'deps' => array(), |
| 203 | 'version' => EVF_VERSION, |
| 204 | ), |
| 205 | 'everest-forms-ajax-submission' => array( |
| 206 | 'src' => self::get_asset_url( 'assets/js/frontend/ajax-submission' . $suffix . '.js' ), |
| 207 | 'deps' => array( 'jquery', 'inputmask', 'jquery-validate' ), |
| 208 | 'version' => EVF_VERSION, |
| 209 | ), |
| 210 | 'everest-forms-survey-polls-quiz-script' => array( |
| 211 | 'src' => self::get_asset_url( 'assets/js/frontend/everest-forms-survey-polls-quiz' . $suffix . '.js' ), |
| 212 | 'deps' => array(), |
| 213 | 'version' => EVF_VERSION, |
| 214 | ), |
| 215 | ); |
| 216 | |
| 217 | foreach ( $register_scripts as $name => $props ) { |
| 218 | |
| 219 | self::register_script( $name, $props['src'], $props['deps'], $props['version'] ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Register all EVF sty;es. |
| 225 | */ |
| 226 | private static function register_styles() { |
| 227 | $register_styles = array( |
| 228 | 'evf_select2' => array( |
| 229 | 'src' => self::get_asset_url( 'assets/css/select2.css' ), |
| 230 | 'deps' => array(), |
| 231 | 'version' => EVF_VERSION, |
| 232 | 'has_rtl' => false, |
| 233 | ), |
| 234 | 'flatpickr' => array( |
| 235 | 'src' => self::get_asset_url( 'assets/css/flatpickr.css' ), |
| 236 | 'deps' => array(), |
| 237 | 'version' => EVF_VERSION, |
| 238 | 'has_rtl' => false, |
| 239 | ), |
| 240 | 'jquery-intl-tel-input' => array( |
| 241 | 'src' => self::get_asset_url( 'assets/css/intlTelInput.css' ), |
| 242 | 'deps' => array(), |
| 243 | 'version' => EVF_VERSION, |
| 244 | 'has_rtl' => false, |
| 245 | ), |
| 246 | ); |
| 247 | foreach ( $register_styles as $name => $props ) { |
| 248 | self::register_style( $name, $props['src'], $props['deps'], $props['version'], 'all', $props['has_rtl'] ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Register/queue frontend scripts. |
| 254 | */ |
| 255 | public static function load_scripts() { |
| 256 | global $post; |
| 257 | |
| 258 | if ( ! did_action( 'before_everest_forms_init' ) ) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | self::register_scripts(); |
| 263 | self::register_styles(); |
| 264 | |
| 265 | // Enqueue dashicons. |
| 266 | wp_enqueue_style( 'dashicons' ); |
| 267 | |
| 268 | // CSS Styles. |
| 269 | $enqueue_styles = self::get_styles(); |
| 270 | if ( $enqueue_styles ) { |
| 271 | foreach ( $enqueue_styles as $handle => $args ) { |
| 272 | if ( ! isset( $args['has_rtl'] ) ) { |
| 273 | $args['has_rtl'] = false; |
| 274 | } |
| 275 | |
| 276 | self::enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'], $args['has_rtl'] ); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Whether payment debug logging is enabled for the current request. |
| 283 | * |
| 284 | * @return bool |
| 285 | */ |
| 286 | private static function is_payment_debug_enabled() { |
| 287 | return current_user_can( 'manage_options' ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Localize a EVF script once. |
| 292 | * |
| 293 | * @param string $handle Script handle the data will be attached to. |
| 294 | */ |
| 295 | private static function localize_script( $handle ) { |
| 296 | if ( ! in_array( $handle, self::$wp_localize_scripts, true ) && wp_script_is( $handle ) ) { |
| 297 | $data = self::get_script_data( $handle ); |
| 298 | |
| 299 | if ( ! $data ) { |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | $name = str_replace( '-', '_', $handle ) . '_params'; |
| 304 | self::$wp_localize_scripts[] = $handle; |
| 305 | wp_localize_script( $handle, $name, apply_filters( $name, $data ) ); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Return data for script handles. |
| 311 | * |
| 312 | * @param string $handle Script handle the data will be attached to. |
| 313 | * @return array|bool |
| 314 | */ |
| 315 | private static function get_script_data( $handle ) { |
| 316 | switch ( $handle ) { |
| 317 | case 'everest-forms': |
| 318 | $params = array( |
| 319 | 'ajax_url' => evf()->ajax_url(), |
| 320 | 'submit' => esc_html__( 'Submit', 'everest-forms' ), |
| 321 | 'disable_user_details' => get_option( 'everest_forms_disable_user_details' ), |
| 322 | 'everest_forms_data_save' => wp_create_nonce( 'everest_forms_data_save_nonce' ), |
| 323 | 'everest_forms_slot_booking' => wp_create_nonce( 'everest_forms_slot_booking_nonce' ), |
| 324 | 'i18n_messages_required' => esc_html( get_option( 'everest_forms_required_validation' ) ), |
| 325 | 'i18n_messages_url' => esc_html( get_option( 'everest_forms_url_validation' ) ), |
| 326 | 'i18n_messages_email' => esc_html( get_option( 'everest_forms_email_validation' ) ), |
| 327 | 'i18n_messages_email_suggestion' => esc_html( get_option( 'everest_forms_email_suggestion', __( 'Did you mean {suggestion}?', 'everest-forms' ) ) ), |
| 328 | 'i18n_messages_email_suggestion_title' => esc_attr__( 'Click to accept this suggestion.', 'everest-forms' ), |
| 329 | 'i18n_messages_confirm' => esc_html( get_option( 'everest_forms_confirm_validation', __( 'Field values do not match.', 'everest-forms' ) ) ), |
| 330 | 'i18n_messages_check_limit' => esc_html( get_option( 'everest_forms_check_limit_validation', __( 'You have exceeded number of allowed selections: {#}.', 'everest-forms' ) ) ), |
| 331 | 'i18n_messages_number' => esc_html( get_option( 'everest_forms_number_validation' ) ), |
| 332 | 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'everest-forms' ), |
| 333 | 'mailcheck_enabled' => (bool) apply_filters( 'everest_forms_mailcheck_enabled', true ), |
| 334 | 'mailcheck_domains' => array_map( 'sanitize_text_field', (array) apply_filters( 'everest_forms_mailcheck_domains', array() ) ), |
| 335 | 'mailcheck_toplevel_domains' => array_map( 'sanitize_text_field', (array) apply_filters( 'everest_forms_mailcheck_toplevel_domains', array( 'dev' ) ) ), |
| 336 | 'il8n_min_word_length_err_msg' => esc_html__( 'Please enter at least {0} words.', 'everest-forms' ), |
| 337 | 'il8n_min_character_length_err_msg' => esc_html__( 'Please enter at least {0} characters.', 'everest-forms' ), |
| 338 | 'plugin_url' => plugin_dir_url( EVF_PLUGIN_FILE ), |
| 339 | 'i18n_messages_phone' => esc_html( get_option( 'everest_forms_phone_validation', __( 'Please enter a valid phone number.', 'everest-forms' ) ) ), |
| 340 | 'evf_smart_phone_allowed_countries' => apply_filters( 'everest_forms_smart_phone_allowed_countries', array() ), |
| 341 | 'i18n_field_rating_greater_than_max_value_error' => esc_html__( 'Please enter in a value less than 100.', 'everest-forms' ), |
| 342 | 'evf_checked_image_url' => esc_url( self::get_asset_url( 'assets/images/evf-checked.png' ) ), |
| 343 | 'i18n_evf_success_text' => esc_html__( 'Success!', 'everest-forms' ), |
| 344 | ); |
| 345 | break; |
| 346 | case 'everest-forms-text-limit': |
| 347 | $params = array( |
| 348 | 'i18n_messages_limit_characters' => esc_html__( '{count} of {limit} max characters.', 'everest-forms' ), |
| 349 | 'i18n_messages_limit_words' => esc_html__( '{count} of {limit} max words.', 'everest-forms' ), |
| 350 | ); |
| 351 | break; |
| 352 | case 'everest-forms-ajax-submission': |
| 353 | $params = array( |
| 354 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 355 | 'evf_ajax_submission' => wp_create_nonce( 'everest_forms_ajax_form_submission' ), |
| 356 | 'submit' => esc_html__( 'Submit', 'everest-forms' ), |
| 357 | 'error' => esc_html__( 'Something went wrong while making an AJAX submission', 'everest-forms' ), |
| 358 | 'required' => esc_html__( 'This field is required.', 'everest-forms' ), |
| 359 | 'pdf_download' => esc_html__( 'Click here to download your pdf submission', 'everest-forms' ), |
| 360 | 'evf_checked_image_url' => esc_url( self::get_asset_url( 'assets/images/evf-checked.png' ) ), |
| 361 | 'i18n_evf_success_text' => esc_html__( 'Success!', 'everest-forms' ), |
| 362 | 'payment_debug' => self::is_payment_debug_enabled() ? '1' : '0', |
| 363 | ); |
| 364 | break; |
| 365 | case 'everest-forms-survey-polls-quiz-script': |
| 366 | $params = array( |
| 367 | 'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ), |
| 368 | 'ajax_nonce' => wp_create_nonce( 'process-ajax-nonce' ), |
| 369 | 'form_id' => isset( $_GET['form_id'] ) ? wp_unslash( $_GET['form_id'] ) : '', //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Recommended |
| 370 | ); |
| 371 | break; |
| 372 | default: |
| 373 | $params = false; |
| 374 | } |
| 375 | |
| 376 | return apply_filters( 'everest_forms_get_script_data', $params, $handle ); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Localize scripts only when enqueued. |
| 381 | */ |
| 382 | public static function localize_printed_scripts() { |
| 383 | foreach ( self::$scripts as $handle ) { |
| 384 | self::localize_script( $handle ); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | EVF_Frontend_Scripts::init(); |
| 390 |