Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active February 4, 2022 18:19
Show Gist options
  • Save westonruter/fefdcda37df7bde61c7b0f690fafc01c to your computer and use it in GitHub Desktop.
Save westonruter/fefdcda37df7bde61c7b0f690fafc01c to your computer and use it in GitHub Desktop.
<?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' );
<?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
Copy link

rafaucau commented Feb 3, 2022

@westonruter (FYI) This code does not work on WP 5.9. It's like it doesn't use this sanitizer at all.

@rafaucau
Copy link

rafaucau commented Feb 4, 2022

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', '' );
		}
	}
}

@westonruter
Copy link
Author

@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.

@westonruter
Copy link
Author

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