| @@ -45,9 +45,9 @@ | ||
| 45 | 45 | |
| 46 | 46 | wpbc_js_load_libs( $where_to_load ); |
| 47 | 47 | wpbc_js_load_files( $where_to_load ); |
| 48 | 48 | |
| 49 | - if ( wpbc_is_new_booking_page() || wpbc_is_settings_form_page() || wpbc_is_settings_color_themes_page() || wpbc_is_setup_wizard_page() || ( function_exists( 'wpbc_is_add_booking_modal_on_booking_listing_page' ) && wpbc_is_add_booking_modal_on_booking_listing_page() ) ) { | |
| 49 | + if ( wpbc_is_admin_page_with_frontend_booking_preview() ) { | |
| 50 | 50 | $where_to_load = 'both'; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Load JavaScript files in all other versions |
| @@ -64,30 +64,22 @@ | ||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | - * Remove `async` and `defer` ( check more here https://javascript.info/script-async-defer ) | |
| 69 | - * for scripts registered or enqueued, that required for correct working of plugin, like | |
| 70 | - * jquery and all Booking Calendar scripts | |
| 68 | + * Preserve the required execution order for Booking Calendar scripts. | |
| 71 | 69 | * |
| 70 | + * Removes asynchronous loading attributes and excludes the scripts from | |
| 71 | + * Cloudflare Rocket Loader. The filter runs at a very late priority so these | |
| 72 | + * protections remain present after other script-tag optimization filters. | |
| 73 | + * | |
| 72 | 74 | * @param string $tag The script tag. |
| 73 | - * @param string $handle The script handle. | |
| 75 | + * @param string $handle The registered script handle. | |
| 76 | + * @param string $src The script source URL. | |
| 74 | 77 | * |
| 75 | - * @return string Script HTML string. | |
| 76 | - * | |
| 78 | + * @return string Filtered script HTML string. | |
| 77 | 79 | */ |
| 78 | 80 | public function filter_script_loader_tag( $tag, $handle, $src ) { |
| 79 | - | |
| 80 | - $script_handles_prevent_defer = array( | |
| 81 | - 'jquery-core' // exact value | |
| 82 | - , 'jquery-migrate' | |
| 83 | - //, 'wpbc-' //starting from 'wpbc-' it's not the exact value | |
| 84 | - //, 'wpdevelop-' | |
| 85 | - ); | |
| 86 | - | |
| 87 | - | |
| 88 | - // Remove defer and async attribute from the src. | |
| 89 | - if ( | |
| 81 | + $is_required_script = ( | |
| 90 | 82 | ( 'jquery-core' === $handle ) |
| 91 | 83 | || ( 'jquery-migrate' === $handle ) |
| 92 | 84 | || ( false !== strpos( $handle, 'wpbc_' ) ) // Booking Calendar script wpbc_all.js // FixIn: 10.1.2.3. |
| 93 | 85 | || ( false !== strpos( $handle, 'wpbc-' ) ) // Booking Calendar scripts |
| @@ -92,28 +84,20 @@ | ||
| 92 | 84 | || ( false !== strpos( $handle, 'wpbc_' ) ) // Booking Calendar script wpbc_all.js // FixIn: 10.1.2.3. |
| 93 | 85 | || ( false !== strpos( $handle, 'wpbc-' ) ) // Booking Calendar scripts |
| 94 | 86 | || ( false !== strpos( $handle, 'wpdevelop-' ) ) |
| 95 | 87 | || ( false !== strpos( $handle, 'wpbm-' ) ) |
| 96 | - ) { | |
| 88 | + ); | |
| 97 | 89 | |
| 90 | + if ( $is_required_script ) { | |
| 98 | 91 | foreach ( array( 'async', 'defer' ) as $attr ) { |
| 92 | + // Match only a standalone attribute, not the "async" part of data-cfasync. | |
| 93 | + $pattern = '/\s+' . $attr . '(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|[^\s>]+))?/i'; | |
| 94 | + $tag = preg_replace( $pattern, '', $tag ); | |
| 95 | + } | |
| 99 | 96 | |
| 100 | - if ( preg_match( ":\s$attr(=|>|\s):", $tag ) ) { | |
| 101 | - $tag = str_replace($attr, '', $tag); | |
| 102 | - $tag = str_replace('=""', '', $tag); | |
| 103 | - $tag = str_replace("=''", '', $tag); | |
| 104 | - | |
| 105 | - /* | |
| 106 | - * Test here https://regex101.com/ | |
| 107 | - * | |
| 108 | - * Expression: \s+defer(\s*=\s*["']defer["'])?\s? | |
| 109 | - * Test string: <script type='text/javascript' defer = 'defer' defer="defer" src='http://beta/wp-content/plugins/booking-manager/js/wpbm_vars.js?ver=1.1' id='wpbm-global-vars-js'></script> | |
| 110 | - * | |
| 111 | - */ | |
| 112 | - $pattern = ":\s+{$attr}(\s*=\s*[\"']{$attr}[\"'])?\s?:mi"; | |
| 113 | - $replacement = ' '; | |
| 114 | - $tag = preg_replace($pattern, $replacement, $tag); | |
| 115 | - } | |
| 97 | + // Rocket Loader can otherwise execute dependent scripts before wpbc_all.js defines _wpbc. | |
| 98 | + if ( false === strpos( $tag, 'data-cfasync=' ) ) { | |
| 99 | + $tag = preg_replace( '/<script\b/i', '<script data-cfasync="false"', $tag, 1 ); | |
| 116 | 100 | } |
| 117 | 101 | } |
| 118 | 102 | |
| 119 | 103 | return $tag; |
| @@ -203,9 +187,9 @@ | ||
| 203 | 187 | if ( ! empty( $calendar_localization_url ) ) { |
| 204 | 188 | wp_enqueue_script( 'wpbc-datepick-localize', $calendar_localization_url, array( 'wpbc-datepick' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); //FixIn: 9.8.1 |
| 205 | 189 | } |
| 206 | 190 | |
| 207 | - if ( ( $where_to_load == 'client' ) || ( wpbc_is_new_booking_page() ) || ( function_exists( 'wpbc_is_add_booking_modal_on_booking_listing_page' ) && wpbc_is_add_booking_modal_on_booking_listing_page() ) || ( wpbc_is_settings_form_page() || wpbc_is_settings_color_themes_page() || wpbc_is_setup_wizard_page() || wpbc_is_builder_booking_form_page() ) ) { | |
| 191 | + if ( ( $where_to_load == 'client' ) || wpbc_is_admin_page_with_frontend_booking_preview() ) { | |
| 208 | 192 | |
| 209 | 193 | wp_enqueue_script( 'wpbc-main-client', wpbc_plugin_url( '/js/client.js' ), array( 'wpbc-datepick' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); // Client |
| 210 | 194 | wp_enqueue_script( 'wpbc_capacity', wpbc_plugin_url( '/includes/_capacity/_out/create_booking.js' ), array( 'wpbc-main-client' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); // Add new bookings // FixIn: 9.8.0.3. |
| 211 | 195 | wp_enqueue_script( 'wpbc-times', wpbc_plugin_url( '/js/wpbc_times.js' ), array( 'wpbc-main-client' ), WP_BK_VERSION_NUM, array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); // FixIn: TimeFree 2 //UnComment it for Booking Calendar Free version |