Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active January 31, 2020 16:29
Show Gist options
  • Save webaware/8949605 to your computer and use it in GitHub Desktop.
Save webaware/8949605 to your computer and use it in GitHub Desktop.

Revisions

  1. webaware revised this gist Feb 16, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions flxmap-no-rocketscript.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@
    @ref: http://wordpress.org/support/topic/map-wont-appear
    @ref: http://stackoverflow.com/a/10611827/911083
    @ref: https://support.cloudflare.com/hc/en-us/articles/200169436
    */

    /**
  2. webaware revised this gist Feb 16, 2014. 1 changed file with 38 additions and 12 deletions.
    50 changes: 38 additions & 12 deletions flxmap-no-rocketscript.php
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,53 @@
    <?php
    /*
    Plugin Name: Flxmap No Rocketscript
    Plugin URI:
    Plugin URI: https://gist.github.com/webaware/8949605
    Description: stop CloudFlare Rockscript messing up the map plugin!
    Version: 2
    Version: 3
    Author: WebAware
    Author URI: http://webaware.com.au/
    @ref: http://wordpress.org/support/topic/map-wont-appear
    @ref: http://stackoverflow.com/a/10611827/911083
    @ref: http://wordpress.stackexchange.com/a/38335/24260
    */

    add_filter('clean_url', function($url) {
    if (class_exists('FlxMapPlugin')) {
    // only for WP Flexible Map scripts
    $plugin = FlxMapPlugin::getInstance();
    /**
    * output a script tag that won't be replaced by Rocketscript
    * @param string $handle
    */
    function flxmap_no_rocketscript($handle) {
    global $wp_scripts;

    if (preg_match('#maps.google.com/maps/api/js|' . $plugin->urlBase . '.*\.js#', $url)) {
    // it's one of our JavaScript URIs, so tell CloudFlare to bugger off!
    $url .= "' data-cfasync='false";
    }
    $script = $wp_scripts->query($handle);
    $src = $script->src;
    if ( !empty($script->ver) ) {
    $src = add_query_arg('ver', $script->ver, $src);
    }
    $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );

    echo "<script data-cfasync='false' type='text/javascript' src='$src'></script>\n";
    }

    /**
    * intercept call to print footer scripts,
    * replace Google Maps API and Flexible Map script
    */
    add_action('wp_print_footer_scripts', function() {
    if (wp_script_is('flxmap')) {
    wp_dequeue_script('flxmap');
    wp_dequeue_script('google-maps');

    return $url;
    flxmap_no_rocketscript('google-maps');
    flxmap_no_rocketscript('flxmap');
    }
    }, 1);

    /**
    * add data-cfasync attribute to Flexible Map script tag
    * @param string $html
    * @return string
    */
    add_filter('flexmap_shortcode_html', function($html) {
    $html = str_replace('<script', '<script data-cfasync="false"', $html);
    return $html;
    });
  3. webaware revised this gist Feb 12, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions flxmap-no-rocketscript.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    <?php
    /*
    Plugin Name: Flxmap No Rocketscript
    Plugin URI: https://gist.github.com/webaware/8949605
    Plugin URI:
    Description: stop CloudFlare Rockscript messing up the map plugin!
    Version: 1
    Version: 2
    Author: WebAware
    Author URI: http://webaware.com.au/
    @@ -17,7 +17,7 @@
    // only for WP Flexible Map scripts
    $plugin = FlxMapPlugin::getInstance();

    if (stripos($url, $plugin->urlBase) !== false && stripos($url, '.js') !== false) {
    if (preg_match('#maps.google.com/maps/api/js|' . $plugin->urlBase . '.*\.js#', $url)) {
    // it's one of our JavaScript URIs, so tell CloudFlare to bugger off!
    $url .= "' data-cfasync='false";
    }
  4. webaware revised this gist Feb 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion flxmap-no-rocketscript.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    /*
    Plugin Name: Flxmap No Rocketscript
    Plugin URI:
    Plugin URI: https://gist.github.com/webaware/8949605
    Description: stop CloudFlare Rockscript messing up the map plugin!
    Version: 1
    Author: WebAware
  5. webaware created this gist Feb 12, 2014.
    27 changes: 27 additions & 0 deletions flxmap-no-rocketscript.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php
    /*
    Plugin Name: Flxmap No Rocketscript
    Plugin URI:
    Description: stop CloudFlare Rockscript messing up the map plugin!
    Version: 1
    Author: WebAware
    Author URI: http://webaware.com.au/
    @ref: http://wordpress.org/support/topic/map-wont-appear
    @ref: http://stackoverflow.com/a/10611827/911083
    @ref: http://wordpress.stackexchange.com/a/38335/24260
    */

    add_filter('clean_url', function($url) {
    if (class_exists('FlxMapPlugin')) {
    // only for WP Flexible Map scripts
    $plugin = FlxMapPlugin::getInstance();

    if (stripos($url, $plugin->urlBase) !== false && stripos($url, '.js') !== false) {
    // it's one of our JavaScript URIs, so tell CloudFlare to bugger off!
    $url .= "' data-cfasync='false";
    }
    }

    return $url;
    });