HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/insiders/wp-load/wp-content/plugins/breadly/lib/amp/class-gtm-amp-css-sanitizer.php
<?php
if ( ! class_exists( 'AMP_Base_Sanitizer' ) ) {
	return;
}

class GTM_AMP_Css_Sanitizer extends AMP_Base_Sanitizer {

	/**
	 * Main sanitze function
	 */
	public function sanitize() {
		$this->sanitize_style_tags();
		$this->sanitize_class_names();
		$this->sanitize_inline_style();
	}

	/**
	 * To convert all Link tags to inline style tags
	 * BUT there is some problem, The block stylesheet is disappeared in the dom
	 * I don't known why. It may caused by the AMP WP plugin.
	 * If can be fixed, there no need to make inline style tags with GTM_AMP_Css->create_inline_styles()
	 */
	public function inline_link_tags() {
		$base_plugin_url = plugins_url();
		$links           = $this->dom->html->getElementsByTagName( 'link' );

		foreach ( $links as $link ) {
			$id   = $this->get_node_attribue_value( $link, 'id' );
			$href = $this->get_node_attribue_value( $link, 'href' );

			if ( gtm_startsWith( $id, 'gtm-' ) || gtm_startsWith( $id, 'gutenmate-' ) ) {
				if ( $href ) {
					$url_parse = wp_parse_url( $href );
					if ( gtm_startsWith( $href, $base_plugin_url ) && ! empty( $url_parse['path'] ) && gtm_endsWith( $url_parse['path'], '.css' ) ) {
						// Stripe query string
						$rawUrl  = sprintf( '%s://%s%s', $url_parse['scheme'], $url_parse['host'], $url_parse['path'] );
						$rawPath = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $rawUrl );

						if ( file_exists( $rawPath ) ) {
							$style = $this->dom->createElement( 'style' );
							$style->setAttribute( 'id', $this->get_node_attribue_value( $link, 'id' ) );
							$rawCss = $this->compressName( gtm_get_file_content( $rawPath ) );
							$style->appendChild( $this->dom->createTextNode( $rawCss ) );

							$this->dom->head->appendChild( $style );
							$link->parentNode->removeChild( $link );
						}
					}
				}
			}
		}
	}

	public function sanitize_style_tags() {
		// Style tags
		$styles = $this->dom->html->getElementsByTagName( 'style' );
		foreach ( $styles as $style ) {
			$style->nodeValue = $this->compressName( $style->nodeValue );
		}
	}

	public function sanitize_class_names() {
		// Elements has class
		$classes = $this->dom->xpath->query( '//*[@class]' );

		foreach ( $classes as $class ) {
			foreach ( $class->attributes as $attr ) {
				if ( $attr->nodeName === 'class' ) {
					// Add double-quotes and remove later for easier replacing the name
					$attr->nodeValue = ltrim( $this->compressName( '"' . $attr->nodeValue ), '"' );
				}
			}
		}
	}

	public function sanitize_inline_style() {
		// Elements has inline style
		$styles = $this->dom->xpath->query( '//*[@style]' );

		foreach ( $styles as $style ) {
			foreach ( $style->attributes as $attr ) {
				if ( $attr->nodeName === 'style' ) {
					// Add double-quotes and remove later for easier replacing the name
					$attr->nodeValue = ltrim( $this->compressName( '"' . $attr->nodeValue ), '"' );
				}
			}
		}
	}

	/**
	 * Proxy to GTM_AMP_Css
	 */
	public function compressName( $css ) {
		$css = GTM_AMP_Css::get_instance()->compress_css_name( $css );

		return $css;
	}

	/**
	 * For easier getting the value of a tag's attribute
	 */
	public function get_node_attribue_value( $node, $attrName ) {
		if ( isset( $node->attributes ) ) {
			foreach ( $node->attributes as $attr ) {
				if ( $attr->nodeName === $attrName ) {
					return $attr->nodeValue;
				}
			}
		}
	}
}