Last active
March 30, 2018 13:52
-
-
Save webaware/3806a2dfdf2abcb61f64 to your computer and use it in GitHub Desktop.
attempt to revove extra Google Maps API scripts when showing a map from the Flexible Map plugin for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Flxmap No Conflict | |
Plugin URI: https://gist.github.com/webaware/3806a2dfdf2abcb61f64 | |
Description: attempt to remove extra Google Maps API scripts when showing a Flexible Map | |
Version: 1 | |
Author: WebAware | |
Author URI: http://webaware.com.au/ | |
@link http://wordpress.org/support/topic/auto-route-and-direction-not-always-work | |
*/ | |
/** | |
* encapsulate in a class | |
*/ | |
class FlxmapNoConflict { | |
/** | |
* hook into WordPress | |
*/ | |
public static function run() { | |
add_action('wp_head', array(__CLASS__, 'removeConflicts'), 1); | |
add_action('wp_footer', array(__CLASS__, 'removeConflicts'), 1); | |
} | |
/** | |
* scan list of enqueued scripts checking for Google Maps API script that will conflict | |
*/ | |
public static function removeConflicts() { | |
global $wp_scripts; | |
global $post; | |
// check to see if Flexible Map plugin is activated | |
if (!defined('FLXMAP_PLUGIN_ROOT')) { | |
return; | |
} | |
// check to see if Flexible Map script is enqueued, or post content contains Flexible Map shortcode | |
if (!(wp_script_is('flxmap') || ($post && strpos($post->post_content, '[' . FLXMAP_PLUGIN_TAG_MAP) !== false))) { | |
return; | |
} | |
$queue = self::includeDependencies($wp_scripts->queue); | |
foreach ($queue as $key => $handle) { | |
// skip Google Maps handle that won't conflict | |
if ($handle == 'google-maps') { | |
continue; | |
} | |
// get registered script object | |
if (isset($wp_scripts->registered[$handle])) { | |
$script = $wp_scripts->registered[$handle]; | |
// check for Google Maps API in URL | |
// e.g. http://maps.googleapis.com/maps/api/js, http://maps.google.com/maps/api/js | |
if (preg_match('#maps\.google(?:\w+)?\.com/maps/api/js#', $script->src)) { | |
// remove the API script from the queue | |
$key = array_search($handle, $wp_scripts->queue); | |
if ($key !== false) { | |
unset($wp_scripts->queue[$key]); | |
} | |
// find dependents | |
$deps = self::findDependents($handle); | |
foreach ($deps as $dep) { | |
$dep = array_search($dep, $wp_scripts->queue); | |
if ($dep !== false) { | |
unset($wp_scripts->queue[$dep]); | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* recursively search for script dependencies and add to list of scripts | |
* @param array $queue | |
* @return array | |
*/ | |
protected static function includeDependencies($queue) { | |
global $wp_scripts; | |
$prereqs = array(); | |
foreach ($queue as $handle) { | |
// script must be registered | |
if (!isset($wp_scripts->registered[$handle])) { | |
continue; | |
} | |
$script = $wp_scripts->registered[$handle]; | |
// dependencies must be registered | |
if ($script->deps && array_diff($script->deps, array_keys($wp_scripts->registered))) { | |
continue; | |
} | |
// merge dependencies with list of prerequisites | |
$prereqs = array_merge($prereqs, $script->deps); | |
} | |
// get unique list of prereq's | |
$prereqs = array_unique($prereqs); | |
// remove dependencies already in the queue | |
$prereqs = array_diff($prereqs, $queue); | |
// check for further dependencies | |
if (!empty($prereqs)) { | |
$prereqs = self::includeDependencies($prereqs); | |
} | |
// merge with queue | |
$queue = array_merge($queue, $prereqs); | |
return $queue; | |
} | |
/** | |
* recursively find dependent scripts for a handle | |
* @param string $handle | |
* @return array | |
*/ | |
protected static function findDependents($handle) { | |
global $wp_scripts; | |
$deps = array(); | |
foreach ($wp_scripts->registered as $key => $script) { | |
if ($script->deps && in_array($handle, $script->deps)) { | |
$deps[] = $key; | |
$deps = array_merge($deps, self::findDependents($key)); | |
} | |
} | |
return array_unique($deps); | |
} | |
} | |
FlxmapNoConflict::run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment