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/gutenmate/lib/amp/class-gtm-amp.php
<?php

class GTM_AMP {
	private static $instance;
	public $template;

	public static function get_instance() {
		if ( ! self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	public function __construct() {
		add_action( 'wp', [$this, 'init'], 10 );
	}

	public function init() {
		// The script may required by some blocks on non-AMP pages
		add_action( 'wp_enqueue_scripts', [$this, 'may_register_amp_scripts'] );

		if ( ! is_admin() && gtm_is_amp_request() ) {
			$this->setup();
		}
	}

	/**
	 * Setup for serving AMP page
	 */
	public function setup() {
		add_action( 'wp_enqueue_scripts', [$this, 'dequeue_scripts'], 99 );

		// Setup AMP template renderer
		$this->template = $this->get_amp_template();
		if ( get_option( 'gtm_amp_enable_templates', true ) && $this->template ) {
			add_filter( 'template_include', [$this, 'template_include'], 11 );
		}

		if ( get_option( 'gtm_amp_enable_compress_gtm_css', false ) ) {
			// Setup for AMP WP plugin
			include_once __DIR__ . '/class-gtm-amp-css-sanitizer.php';
			add_filter( 'amp_content_sanitizers', [$this, 'add_content_sanitizer'] );

			// Setup css sanitizer
			include_once __DIR__ . '/class-gtm-amp-css.php';
			$ampCss = GTM_AMP_Css::get_instance();
		}

		// Setup css sanitizer
		include_once __DIR__ . '/class-gtm-amp-css-dequeue.php';
		$ampCss = GTM_AMP_Css_Dequeue::get_instance();
	}

	/**
	 * Add content sanitizer for AMP WP plugin
	 */
	public function add_content_sanitizer( $sanitizer_classes ) {
		return array_merge( $sanitizer_classes, [GTM_AMP_Css_Sanitizer::class => []] );
	}

	public function template_include( $template ) {
		return __DIR__ . '/template-canvas.php';
	}

	/**
	 * Get AMP template slug for current page
	 *
	 * @return string
	 */
	public function get_amp_template() {
		$slugs = [];

		// The same logic as wp-includes\template-loader.php
		if ( is_404() ) {
			$slugs = ['404'];

		} else if ( is_search() ) {
			$slugs = ['search'];

		} else if ( is_front_page() ) {
			$slugs = ['front-page'];

		} else if ( is_home() ) {
			$slugs = ['home'];

		} else if ( is_tax() ) {
			$slugs = ['taxonomy', 'archive'];

		} else if ( is_category() ) {
			$slugs = ['category', 'archive'];

		} else if ( is_tag() ) {
			$slugs = ['tag', 'archive'];

		} else if ( is_author() ) {
			$slugs = ['author', 'archive'];

		} else if ( is_date() ) {
			$slugs = ['date', 'archive'];

		} else if ( is_archive() ) {
			$slugs = ['archive'];

		} else if ( is_attachment() ) {
			$slugs = ['attachment'];

		} else if ( is_single() ) {
			$slugs = ['single-post', 'single', 'singular'];

		} else if ( is_page() ) {
			$slugs = ['page', 'singular'];

		} else if ( is_singular() ) {
			$slugs = ['singular'];
		}

		// Add custom template
		if ( is_singular() ) {
			$custom_template = get_page_template_slug();

			if ( $custom_template ) {
				array_unshift( $slugs, $custom_template );
			}
		}

		// Add AMP template prefix
		// Also looking for "gutenmate-" prefix is to prevent collision from actual AMP plugin in the future
		// and still use short prefix "amp-" for easier to users
		// input: ['page', 'singular']
		// output: ['gutenmate-amp-page', 'amp-page', 'gutenmate-amp-singular', 'amp-singular']
		$slugs = array_reduce( $slugs, function ( $carry, $template ) {
			$carry[] = "gutenmate-amp-$template";
			$carry[] = "amp-$template";

			return $carry;
		}, [] );

		$templates = get_block_templates( ['slug__in' => $slugs], 'wp_template' );

		// Ref: Sorting in the same way as WP's resolve_block_template()
		$slug_priorities = array_flip( $slugs );
		usort(
			$templates,
			static function ( $template_a, $template_b ) use ( $slug_priorities ) {
				return $slug_priorities[$template_a->slug] - $slug_priorities[$template_b->slug];
			}
		);

		return apply_filters( 'gtm_get_amp_template', count( $templates ) ? $templates[0] : null );
	}

	public function may_register_amp_scripts() {
		if ( ! gtm_has_amp_plugin() ) {
			if ( ! wp_script_is( 'amp-runtime', 'registered' ) ) {
				wp_register_script( 'amp-runtime', 'https://cdn.ampproject.org/v0.js' );
			}
		}
	}

	public function dequeue_scripts() {
		wp_dequeue_script( 'gutenmate-photoswipe' );
		wp_dequeue_script( 'gutenmate-tippy' );
		wp_dequeue_script( 'gtm-block-script' );
	}
}