Last active
February 4, 2022 18:19
-
-
Save westonruter/fefdcda37df7bde61c7b0f690fafc01c to your computer and use it in GitHub Desktop.
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 | |
/** | |
* AMP Auto Image Lightbox plugin bootstrap. | |
* | |
* @package Google\AMP_Auto_Image_Lightbox | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2020 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Auto Image Lightbox | |
* Plugin URI: https://gist.github.com/westonruter/ | |
* Description: For images which are contained by a link to the same image file, automatically remove the link and put a lightbox attribute on the image to use amp-lightbox-gallery instead. | |
* Version: 0.1.1 | |
* 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/fefdcda37df7bde61c7b0f690fafc01c | |
*/ | |
namespace Google\AMP_Auto_Image_Lightbox; | |
/** | |
* Filter sanitizers. | |
* | |
* @param array $sanitizers Sanitizers. | |
* @return array Sanitizers. | |
*/ | |
function filter_sanitizers( $sanitizers ) { | |
if ( version_compare( AMP__VERSION, '1.5', '<=' ) ) { | |
return $sanitizers; | |
} | |
require_once __DIR__ . '/class-sanitizer.php'; | |
return array_merge( | |
[ | |
__NAMESPACE__ . '\Sanitizer' => [], | |
], | |
$sanitizers | |
); | |
} | |
add_filter( 'amp_content_sanitizers', __NAMESPACE__ . '\filter_sanitizers' ); |
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 | |
/** | |
* Sanitizer | |
* | |
* @package Google\AMP_Auto_Image_Lightbox | |
*/ | |
namespace Google\AMP_Auto_Image_Lightbox; | |
use AMP_Base_Sanitizer; | |
use DOMElement; | |
/** | |
* Class Sanitizer | |
*/ | |
class Sanitizer extends AMP_Base_Sanitizer { | |
/** | |
* Sanitize. | |
*/ | |
public function sanitize() { | |
/** | |
* Element. | |
* | |
* @var DOMElement $img | |
*/ | |
foreach ( $this->dom->xpath->query( '//a[ @href ]/img[ @src = parent::a/@href ]' ) as $img ) { | |
$link = $img->parentNode; | |
$link->parentNode->replaceChild( $img, $link ); | |
$img->setAttribute( 'lightbox', '' ); | |
} | |
} | |
} |
@westonruter (FYI) This code does not work on WP 5.9. It's like it doesn't use this sanitizer at all.
In the xpath I replaced amp-img
with img
and it works. It looks like this sanitizer was executing before AMP replaced img with amp-img.
<?php
/**
* Sanitizer
*
* @package Google\AMP_Auto_Image_Lightbox
*/
namespace Google\AMP_Auto_Image_Lightbox;
use AMP_Base_Sanitizer;
use DOMElement;
/**
* Class Sanitizer
*/
class Sanitizer extends AMP_Base_Sanitizer {
/**
* Sanitize.
*/
public function sanitize() {
/**
* Element.
*
* @var DOMElement $img
*/
foreach ( $this->dom->xpath->query( '//a[ @href ]/img[ @src = parent::a/@href ]' ) as $img ) {
$link = $img->parentNode;
$link->parentNode->replaceChild( $img, $link );
$img->setAttribute( 'lightbox', '' );
}
}
}
@rafaucau Thanks, updated. This will also work out better in the future as amp-img
is being deprecated. The img
element will be used by default as of ampproject/amp-wp#6805.
The reason why it didn't work anymore is that in 2.2 the builtin sanitizers are now always run in a set order after any user-supplied transformers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For context, see https://wordpress.org/support/topic/lightbox-not-working-on-amp-pages/
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c