Completed
Push — try/allow-embeds-in-amp ( 2692ec...369d73 )
by
unknown
26s
created

Jetpack_AMP_Feature_Assets_Sanitizer::sanitize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Provides AMP validation carve-outs for Jetpack features
4
 *
5
 * @see https://github.com/Automattic/amp-wp
6
 * @package Jetpack
7
 */
8
9
/**
10
 * This sanitizer class sets AMP devmode on the html element and each other element to be ignored (and not stripped out) for validation purposes.
11
 */
12
class Jetpack_AMP_Feature_Assets_Sanitizer extends AMP_Base_Sanitizer {
13
	/**
14
	 * List of xpaths selecting assets to be ignored
15
	 *
16
	 * @var asset_xpaths.
17
	 */
18
	private static $asset_xpaths = array(
19
		'//script[contains(@src,\'//www.opentable.com/widget\')]',
20
		'//script[contains(@src,\'//assets.pinterest.com/js/pinit.js\')]',
21
		'//script[ @id = \'eventbrite-widget-js\' ]',
22
		'//script[ @id = \'eventbrite-widget-js-after\' ]',
23
		'//script[ @id = \'jetpack-block-calendly-js-extra\' ]',
24
		'//script[ @id = \'jetpack-block-calendly-js\' ]',
25
		'//script[ @id = \'jetpack-calendly-external-js-js\' ]',
26
		'//script[ @id = \'jetpack-calendly-external-js-js-after\' ]',
27
	);
28
29
	/**
30
	 * Sanitize document for dev mode.
31
	 *
32
	 * @since 1.3
33
	 */
34
	public function sanitize() {
35
		$this->dom->documentElement->setAttribute( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, '' );
36
37
		$xpath = new DOMXPath( $this->dom );
38
		foreach ( self::$asset_xpaths as $element_xpath ) {
39
			foreach ( $xpath->query( $element_xpath ) as $node ) {
40
				if ( $node instanceof DOMElement ) {
41
					$node->setAttribute( AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, '' );
42
				}
43
			}
44
		}
45
	}
46
}
47