]+>)~i', array( $this, 'tag_script' ), $buffer ); if ( ! empty( $this->preload_url ) ) { $preload_html = ''; $buffer = preg_replace( '~]*)*>~Ui', '\\0' . $preload_html, $buffer, 1 ); add_filter( 'w3tc_lazyload_on_initialized_javascript', array( $this, 'w3tc_lazyload_on_initialized_javascript' ) ); } $data['buffer'] = $buffer; $data['modified'] |= ! empty( $this->preload_url ); return $data; } /** * Processes a script tag to identify and handle specific Google Maps scripts. * * Examines the script tag to check if it matches the Google Maps Easy plugin's * frontend script. If a match is found, it updates `$preload_url` and removes * the tag from the buffer. * * @param array $m { * Matches from the regular expression applied to the buffer. * * @type string $0 The full matched script tag. * } * * @return string The modified or original script tag. */ public function tag_script( $m ) { $script_tag = $m[0]; if ( ! preg_match( '~]*src=["\']?([^"\'> ]+)["\'> ]~is', $script_tag, $match ) ) { return $script_tag; } $script_src = $match[1]; $script_src = Util_Environment::url_relative_to_full( $script_src ); if ( ! $this->starts_with( $script_src, WP_PLUGIN_URL . '/google-maps-easy/modules/gmap/js/frontend.gmap.js' ) ) { return $script_tag; } $this->preload_url = $script_src; return ''; } /** * Checks if a string starts with a given prefix. * * Utility function to determine if the provided string begins with the specified prefix. * * @param string $v The string to check. * @param string $prefix The prefix to look for. * * @return bool True if the string starts with the prefix, false otherwise. */ private function starts_with( $v, $prefix ) { return substr( $v, 0, strlen( $prefix ) ) === $prefix; } /** * Generates the JavaScript initialization code for lazy loading Google Maps. * * This method returns the JavaScript code that initializes lazy loading for Google Maps * using the LazyLoad library. It includes a callback to dynamically load the required script * when the map element enters the viewport. * * @return string The JavaScript code for initializing lazy loading. */ public function w3tc_lazyload_on_initialized_javascript() { return 'window.w3tc_lazyLazy_googlemaps_wpmaps = new LazyLoad({' . 'elements_selector: ".gmp_map_opts",' . 'callback_enter: function(e){' . // w3tc_load_js function. 'function w3tc_load_js(t,n){"use strict";var o=document.getElementsByTagName("script")[0],r=document.createElement("script");return r.src=t,r.async=!0,o.parentNode.insertBefore(r,o),n&&"function"==typeof n&&(r.onload=n),r};' . 'w3tc_load_js("' . esc_url( $this->preload_url ) . '");' . '}});'; } }