File: /var/www/html/insiders/wp-load/wp-content/plugins/breadly/lib/amp/class-gtm-amp-css.php
<?php
class GTM_AMP_Css {
private static $instance;
public $name_hints = [
// First loop replacement
// Any specific class name
[
'gtm-has-block-flex-layout-' => 'gfl',
'gtm-style' => 'gs',
],
// Second loop replacement
// Any remaining class name
[
'gtm-has-block-' => 'ghb',
],
];
public $css_vars = [];
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
if ( ! did_action( 'wp' ) ) {
add_action( 'wp', [$this, 'init'] );
} else {
$this->init();
}
}
public function init() {
add_filter( 'print_styles_array', [$this, 'create_inline_styles'], 99 );
add_filter( 'render_block', [$this, 'collect_block_hints'], 10, 2 );
// Add hints for general css vars
$this->add_css_var_hint( '--gtm-block-flex-layout-' );
$this->add_css_var_hint( '--gtm-block-padding-' );
$this->add_css_var_hint( '--gtm-block-margin-' );
$this->add_css_var_hint( '--gtm--space--' );
$this->add_css_var_hint( '--gtm--typography--' );
// Allow adding custom name hints
$this->name_hints = apply_filters( 'gtm_amp_css_compress_name_hints', $this->name_hints );
}
/**
* Move all gtm block styles to style tags for sanitizing later
*/
public function create_inline_styles( $handles ) {
$wp_styles = wp_styles();
$final_handles = [];
foreach ( $handles as $handle ) {
$isGtmHandle = gtm_startsWith( $handle, 'gtm-' ) || gtm_startsWith( $handle, 'gutenmate-' );
$url = '';
$file = '';
if ( $wp_styles->registered[$handle]->src ) {
$url = $wp_styles->registered[$handle]->src;
$file = gtm_url_to_local_path( $url );
}
if ( $isGtmHandle && ! empty( $file ) && file_exists( $file ) && is_readable( $file ) ) {
$filetype = wp_check_filetype( $file, ['css' => 'text/css'] );
// Allow only css files to be read
if ( 'text/css' === $filetype['type'] ) {
$content = gtm_get_file_content( $file );
if ( $content ) {
// Correct url()
$parent_url = trailingslashit( dirname( $url ) );
$content = preg_replace( '|url\(\s*(["\']*)([^d\\\])|i', 'url($1' . $parent_url . '$2', $content );
// Move to inline style
$wp_styles->registered[$handle]->src = false;
wp_add_inline_style( $handle, $content );
}
}
}
$final_handles[] = $handle;
}
return $final_handles;
}
public function compress_css_name( $style ) {
if ( ! empty( $style ) ) {
// Replace name from hints
foreach ( $this->name_hints as $hints ) {
$style = strtr( $style, $hints );
}
// Replace css vars
foreach ( $this->css_vars as $hint ) {
$style = str_replace( $hint[0], $hint[1], $style );
}
}
return $style;
}
/**
* Generate prefix for new css name
*/
function get_prefix() {
static $i = 1;
return 'z' . base_convert( ++$i, 10, 36 );
}
/**
* Add hints for blocks
*/
public function collect_block_hints( $block_content, $block ) {
if ( ! empty( $block['blockName'] ) ) {
if ( gtm_startsWith( $block['blockName'], 'gtm/' ) ) {
$pureBlockName = str_replace( 'gtm/', '', $block['blockName'] );
$blockName = str_replace( '/', '-', $block['blockName'] );
$this->name_hints[1]['wp-block-' . $blockName] = $this->get_prefix();
$this->name_hints[1]['gtm-has-' . $pureBlockName] = $this->get_prefix();
$this->name_hints[1]['is-style-' . $blockName] = $this->get_prefix();
$this->add_css_var_hint( '--' . $blockName . '-' );
}
}
return $block_content;
}
public function add_css_var_hint( $name ) {
$samePrefix = $this->get_prefix();
$delimeters = [
'(', '{', ';', '"', "'", ' ', "\t", "\n",
];
foreach ( $delimeters as $i => $char ) {
$this->css_vars[$name . $i] = [$char . $name, $char . '--' . $samePrefix];
}
}
}