Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active August 4, 2020 18:33
Show Gist options
  • Save westonruter/c3d4d73418b310f12d3cba7245fccebc to your computer and use it in GitHub Desktop.
Save westonruter/c3d4d73418b310f12d3cba7245fccebc to your computer and use it in GitHub Desktop.
Plugin shim to make Geo Mashup plugin work with the AMP plugin. See https://wordpress.org/support/topic/shortcodes-for-geo-mashup-not-working/
<?php
/**
* Plugin Name: Geo Mashup AMP Compat
*
* @package Geo_Mashup_AMP_Compat
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @wordpress-plugin
* Plugin Name: Geo Mashup AMP Compat
* Description: Plugin shim to make Geo Mashup plugin work with the AMP plugin. See <a href="https://wordpress.org/support/topic/shortcodes-for-geo-mashup-not-working/">support topic</a>.
* Plugin URI: https://gist.github.com/westonruter/c3d4d73418b310f12d3cba7245fccebc
* Version: 0.2.0
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Gist Plugin URI: https://gist.github.com/westonruter/c3d4d73418b310f12d3cba7245fccebc
*/
namespace Geo_Mashup_AMP_Compat;
add_filter( 'amp_is_enabled', __NAMESPACE__ . '\filter_amp_is_enabled' );
add_filter( 'the_content', __NAMESPACE__ . '\filter_content_to_fix_iframe', 100 );
/**
* On standard mode sites, prevent the page in the Geo Mashup iframe from being served as AMP, since it requires JavaScript.
*
* @param bool $enabled Enabled.
* @return bool Filtered enabled.
*/
function filter_amp_is_enabled( $enabled ) {
if ( isset( $_GET['geo_mashup_content'] ) ) {
$enabled = false;
}
return $enabled;
}
/**
* Inject sandbox=allow-scripts attribute on Geo Mashup iframe and remove invalid name attribute.
*
* @param string $content Content.
* @return string Filtered content.
*/
function filter_content_to_fix_iframe( $content ) {
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
$content = preg_replace(
'/(<div class="gm-map"[^>]+?><iframe)( name="gm-map-[^"]+?")/',
'$1 sandbox="allow-scripts" ',
$content
);
}
return $content;
}
@westonruter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment