<?php
/**
* Cartflows Gb Helper.
*
* @package Cartflows
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'Cartflows_Gb_Helper' ) ) {
/**
* Class Cartflows_Gb_Helper.
*/
final class Cartflows_Gb_Helper {
/**
* Member Variable
*
* @since x.x.x
* @var instance
*/
private static $instance;
/**
* Member Variable
*
* @since x.x.x
* @var instance
*/
public static $block_list;
/**
* Current Block List
*
* @since x.x.x
* @var current_block_list
*/
public static $current_block_list = array();
/**
* Page Blocks Variable
*
* @since x.x.x
* @var instance
*/
public static $page_blocks;
/**
* Stylesheet
*
* @since x.x.x
* @var stylesheet
*/
public static $stylesheet;
/**
* Script
*
* @since x.x.x
* @var script
*/
public static $script;
/**
* Cartflows Block Flag
*
* @since x.x.x
* @var cf_flag
*/
public static $cf_flag = false;
/**
* Google fonts to enqueue
*
* @var array
*/
public static $gfonts = array();
/**
* Initiator
*
* @since x.x.x
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
require CARTFLOWS_DIR . 'modules/gutenberg/classes/class-cartflows-block-config.php';
require CARTFLOWS_DIR . 'modules/gutenberg/classes/class-cartflows-block-helper.php';
require CARTFLOWS_DIR . 'modules/gutenberg/classes/class-cartflows-block-js.php';
self::$block_list = Cartflows_Block_Config::get_block_attributes();
add_action( 'wp', array( $this, 'wp_actions' ), 10 );
}
/**
* WP Actions.
*/
public function wp_actions() {
if ( wcf()->utils->is_step_post_type() ) {
$this->generate_assets();
add_action( 'wp_enqueue_scripts', array( $this, 'block_assets' ), 10 );
add_action( 'wp_head', array( $this, 'frontend_gfonts' ), 120 );
add_action( 'wp_head', array( $this, 'print_stylesheet' ), 80 );
add_action( 'wp_footer', array( $this, 'print_script' ), 1000 );
}
}
/**
* Load the front end Google Fonts.
*/
public function frontend_gfonts() {
if ( empty( self::$gfonts ) ) {
return;
}
$show_google_fonts = apply_filters( 'cf_blocks_show_google_fonts', true );
if ( ! $show_google_fonts ) {
return;
}
$link = '';
$subsets = array();
foreach ( self::$gfonts as $key => $gfont_values ) {
if ( ! empty( $link ) ) {
$link .= '%7C'; // Append a new font to the string.
}
$link .= $gfont_values['fontfamily'];
if ( ! empty( $gfont_values['fontvariants'] ) ) {
$link .= ':';
$link .= implode( ',', $gfont_values['fontvariants'] );
}
if ( ! empty( $gfont_values['fontsubsets'] ) ) {
foreach ( $gfont_values['fontsubsets'] as $subset ) {
if ( ! in_array( $subset, $subsets, true ) ) {
array_push( $subsets, $subset );
}
}
}
}
if ( ! empty( $subsets ) ) {
$link .= '&subset=' . implode( ',', $subsets );
}
if ( isset( $link ) && ! empty( $link ) ) {
echo '<link id="cf_show_google_fonts" href="//fonts.googleapis.com/css?family=' . esc_attr( str_replace( '|', '%7C', $link ) ) . '" rel="stylesheet">'; //phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
}
}
/**
* Print the Script in footer.
*/
public function print_script() {
if ( is_null( self::$script ) || '' === self::$script ) {
return;
}
ob_start();
?>
<script type="text/javascript" id="cf-script-frontend"><?php echo self::$script; //phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?></script>
<?php
ob_end_flush();
}
/**
* Print the Stylesheet in header.
*/
public function print_stylesheet() {
if ( is_null( self::$stylesheet ) || '' === self::$stylesheet ) {
return;
}
ob_start();
?>
<style id="cf-style-frontend"><?php echo self::$stylesheet; //phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?></style>
<?php
ob_end_flush();
}
/**
* Generates stylesheet and appends in head tag.
*
* @since x.x.x
*/
public function generate_assets() {
$this_post = array();
global $post;
$this_post = $post;
if ( ! is_object( $this_post ) ) {
return;
}
/**
* Filters the post to build stylesheet for.
*
* @param \WP_Post $this_post The global post.
*/
$this_post = apply_filters( 'cf_post_for_stylesheet', $this_post );
$this->get_generated_stylesheet( $this_post );
}
/**
* Generates stylesheet in loop.
*
* @param object $this_post Current Post Object.
* @since x.x.x
*/
public function get_generated_stylesheet( $this_post ) {
if ( is_object( $this_post ) && isset( $this_post->ID ) && has_blocks( $this_post->ID ) && isset( $this_post->post_content ) ) {
$blocks = $this->parse( $this_post->post_content );
self::$page_blocks = $blocks;
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
return;
}
$assets = $this->get_assets( $blocks );
self::$stylesheet .= $assets['css'];
self::$script .= $assets['js'];
}
}
/**
* Enqueue Gutenberg block assets for both frontend + backend.
*
* @since x.x.x
*/
public function block_assets() {
$block_list_for_assets = self::$current_block_list;
$blocks = Cartflows_Block_Config::get_block_attributes();
foreach ( $block_list_for_assets as $key => $curr_block_name ) {
$js_assets = ( isset( $blocks[ $curr_block_name ]['js_assets'] ) ) ? $blocks[ $curr_block_name ]['js_assets'] : array();
$css_assets = ( isset( $blocks[ $curr_block_name ]['css_assets'] ) ) ? $blocks[ $curr_block_name ]['css_assets'] : array();
foreach ( $js_assets as $asset_handle => $val ) {
// Scripts.
wp_enqueue_script( $val );
}
foreach ( $css_assets as $asset_handle => $val ) {
// Styles.
wp_enqueue_style( $val );
}
}
}
/**
* Parse Guten Block.
*
* @param string $content the content string.
* @since x.x.x
*/
public function parse( $content ) {
global $wp_version;
return ( version_compare( $wp_version, '5', '>=' ) ) ? parse_blocks( $content ) : gutenberg_parse_blocks( $content );
}
/**
* Generates stylesheet for reusable blocks.
*
* @param array $blocks Blocks array.
* @since x.x.x
*/
public function get_assets( $blocks ) {
$desktop = '';
$tablet = '';
$mobile = '';
$tab_styling_css = '';
$mob_styling_css = '';
$js = '';
foreach ( $blocks as $i => $block ) {
if ( is_array( $block ) ) {
if ( '' === $block['blockName'] ) {
continue;
}
if ( 'core/block' === $block['blockName'] ) {
$id = ( isset( $block['attrs']['ref'] ) ) ? $block['attrs']['ref'] : 0;
if ( $id ) {
$content = get_post_field( 'post_content', $id );
$reusable_blocks = $this->parse( $content );
$assets = $this->get_assets( $reusable_blocks );
self::$stylesheet .= $assets['css'];
self::$script .= $assets['js'];
}
} else {
$block_assets = $this->get_block_css_and_js( $block );
// Get CSS for the Block.
$css = $block_assets['css'];
if ( isset( $css['desktop'] ) ) {
$desktop .= $css['desktop'];
$tablet .= $css['tablet'];
$mobile .= $css['mobile'];
}
$js .= $block_assets['js'];
}
}
}
Loading ...