| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Blocks; |
| 4 |
|
| 5 |
final class StyleHandler { |
| 6 |
private static $instance; |
| 7 |
|
| 8 |
private $prefix = 'bp-style'; |
| 9 |
private $style_dir; |
| 10 |
private $style_url; |
| 11 |
private $frontend_prefix = 'frontend'; |
| 12 |
private $frontend_style_dir; |
| 13 |
private $frontend_style_url; |
| 14 |
private $fse_prefix = 'full-site-editor'; |
| 15 |
private $fse_style_dir; |
| 16 |
private $fse_style_url; |
| 17 |
private $widget_style_filename; |
| 18 |
private $fse_template_ids = array( ); |
| 19 |
private $fse_page_template_id; |
| 20 |
private $block_names = array( ); |
| 21 |
private $fse_block_names = array( ); |
| 22 |
private $widget_block_names = array( ); |
| 23 |
private $templately_template_ids = array( ); |
| 24 |
/** |
| 25 |
* Enqueue the frontend scripts, ensures we only do it once. |
| 26 |
* |
| 27 |
* @var boolean |
| 28 |
*/ |
| 29 |
public $is_main_script_loaded = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Holds block styles array |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
public static $_block_styles = array( ); |
| 37 |
|
| 38 |
/** |
| 39 |
* store generatepress elements id |
| 40 |
*/ |
| 41 |
private $gp_ids = array( ); |
| 42 |
|
| 43 |
/** |
| 44 |
* store astra addon advanced hooks id |
| 45 |
*/ |
| 46 |
private $astra_hook_ids = array( ); |
| 47 |
|
| 48 |
public static function init() { |
| 49 |
if ( null === self::$instance ) { |
| 50 |
self::$instance = new self(); |
| 51 |
} |
| 52 |
|
| 53 |
return self::$instance; |
| 54 |
} |
| 55 |
|
| 56 |
public function __construct() { |
| 57 |
$upload_dir = wp_upload_dir(); |
| 58 |
|
| 59 |
$this->style_dir = $upload_dir[ 'basedir' ] . DIRECTORY_SEPARATOR . $this->prefix . DIRECTORY_SEPARATOR; |
| 60 |
$this->style_url = set_url_scheme( $upload_dir[ 'baseurl' ] ) . '/' . $this->prefix . '/'; |
| 61 |
|
| 62 |
$this->frontend_style_dir = $this->style_dir . $this->frontend_prefix . DIRECTORY_SEPARATOR; |
| 63 |
$this->frontend_style_url = $this->style_url . $this->frontend_prefix . '/'; |
| 64 |
|
| 65 |
$this->fse_style_dir = $this->style_dir . $this->fse_prefix . DIRECTORY_SEPARATOR; |
| 66 |
$this->fse_style_url = $this->style_url . $this->fse_prefix . '/'; |
| 67 |
|
| 68 |
add_filter( 'dynamic_sidebar_params', array( $this, 'eb_widget_dynamic_sidebar_params' ) ); |
| 69 |
|
| 70 |
add_action( 'save_post', array( $this, 'on_save_post' ), 10, 3 ); |
| 71 |
add_action( 'wp', array( $this, 'generate_post_content' ) ); |
| 72 |
|
| 73 |
add_action( 'eb_after_save_responsiveBreakpoints_settings', array( $this, 'remove_frontend_assets' ), 10, 1 ); |
| 74 |
add_action( 'eb_after_reset_responsiveBreakpoints_settings', array( $this, 'remove_frontend_assets' ), 10 ); |
| 75 |
add_filter( 'generate_element_post_id', array( $this, 'get_generatepress_element' ), 99 ); |
| 76 |
add_action( 'wp_footer', array( $this, 'eb_add_widget_css_footer' ) ); |
| 77 |
|
| 78 |
//Enqueue Styles based on Block theme or not |
| 79 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) ); |
| 80 |
if ( ! is_admin() ) { |
| 81 |
add_filter( 'render_block', array( $this, 'load_frontend_scripts_conditionally' ), 10, 2 ); |
| 82 |
// add_action('template_redirect', array($this, 'load_frontend_scripts_conditionally_head')); |
| 83 |
} |
| 84 |
|
| 85 |
//For Templately templates |
| 86 |
add_action( 'templately_printed_location', array( $this, 'templately_templates' ), 10, 3 ); |
| 87 |
|
| 88 |
// Hook into Astra Addon advanced hooks processing |
| 89 |
add_action( 'wp_enqueue_scripts', array( $this, 'astra_advanced_hooks_css_generation' ), 15 ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Enqueue frontend css for post if have one |
| 94 |
* @return void |
| 95 |
* @since 1.0.2 |
| 96 |
*/ |
| 97 |
public function enqueue_frontend_assets() { |
| 98 |
global $post; |
| 99 |
|
| 100 |
$deps = apply_filters( 'eb_generated_css_frontend_deps', array( ) ); |
| 101 |
|
| 102 |
//FSE Template Predefined Style Enqueue |
| 103 |
if ( ! empty( $this->fse_page_template_id && is_int( $this->fse_page_template_id ) ) ) { |
| 104 |
$this->load_frontend_css_file( $this->fse_page_template_id, 'essential-blocks-fse-frontend-style', $this->fse_block_names ); |
| 105 |
} |
| 106 |
|
| 107 |
//Page/Post Predefined Style Enqueue |
| 108 |
if ( ! empty( $post ) && ! empty( $post->ID ) ) { |
| 109 |
$handle = $this->load_frontend_css_file( $post->ID, 'essential-blocks-frontend-style', $this->block_names ); |
| 110 |
if ( ! empty( $handle ) ) { |
| 111 |
$deps[ ] = $handle; |
| 112 |
} |
| 113 |
|
| 114 |
//Page/Post Generated Style Enqueue |
| 115 |
if ( file_exists( $this->style_dir . $this->prefix . '-' . $post->ID . '.min.css' ) ) { |
| 116 |
wp_enqueue_style( 'eb-block-style-' . $post->ID, $this->style_url . $this->prefix . '-' . $post->ID . '.min.css', $deps, substr( md5( microtime( true ) ), 0, 10 ) ); |
| 117 |
} |
| 118 |
|
| 119 |
// Reusable block Style Enqueues |
| 120 |
$reusableIds = get_post_meta( $post->ID, '_eb_reusable_block_ids', true ); |
| 121 |
$reusableIds = ! empty( $reusableIds ) ? $reusableIds : array( ); |
| 122 |
$templateReusableIds = get_option( '_eb_reusable_block_ids', array( ) ); |
| 123 |
$reusableIds = array_unique( array_merge( $reusableIds, $templateReusableIds ) ); |
| 124 |
if ( ! empty( $reusableIds ) ) { |
| 125 |
foreach ( $reusableIds as $reusableId ) { |
| 126 |
if ( file_exists( $this->style_dir . 'reusable-blocks/eb-reusable-' . $reusableId . '.min.css' ) ) { |
| 127 |
wp_enqueue_style( 'eb-reusable-block-style-' . $reusableId, $this->style_url . 'reusable-blocks/eb-reusable-' . $reusableId . '.min.css', $deps, substr( md5( microtime( true ) ), 0, 10 ) ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} else { |
| 132 |
if ( ! empty( $this->block_names ) && ! empty( $this->templately_template_ids ) ) { |
| 133 |
foreach ( $this->templately_template_ids as $template ) { |
| 134 |
$handle = $this->load_frontend_css_file( $template, 'essential-blocks-frontend-style-' . $template, $this->block_names ); |
| 135 |
if ( ! empty( $handle ) ) { |
| 136 |
$deps[ ] = $handle; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// generatepress elements |
| 143 |
if ( in_array( 'gp-premium/gp-premium.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
| 144 |
$gp_elements = get_posts( array( 'post_type' => 'gp_elements' ) ); |
| 145 |
if ( is_array( $gp_elements ) && ! empty( $gp_elements ) ) { |
| 146 |
foreach ( $gp_elements as $element ) { |
| 147 |
if ( file_exists( $this->style_dir . $this->get_eb_filename( $element->ID ) ) ) { |
| 148 |
wp_enqueue_style( 'eb-block-style-' . $element->ID, $this->style_url . $this->get_eb_filename( $element->ID ), $deps, substr( md5( microtime( true ) ), 0, 10 ) ); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
//Blocksy theme support |
| 155 |
if ( in_array( 'blocksy-companion-pro/blocksy-companion.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
| 156 |
$ct_elements = get_posts( array( 'post_type' => 'ct_content_block' ) ); |
| 157 |
if ( is_array( $ct_elements ) && ! empty( $ct_elements ) ) { |
| 158 |
foreach ( $ct_elements as $element ) { |
| 159 |
if ( file_exists( $this->style_dir . $this->get_eb_filename( $element->ID ) ) ) { |
| 160 |
wp_enqueue_style( |
| 161 |
'eb-block-style-' . $element->ID, |
| 162 |
$this->style_url . $this->get_eb_filename( $element->ID ), |
| 163 |
$deps, |
| 164 |
substr( md5( microtime( true ) ), 0, 10 ) |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
//Template Templates |
| 172 |
if ( is_array( $this->templately_template_ids ) && count( $this->templately_template_ids ) > 0 ) { |
| 173 |
foreach ( $this->templately_template_ids as $template ) { |
| 174 |
if ( file_exists( $this->style_dir . $this->get_eb_filename( $template ) ) ) { |
| 175 |
wp_enqueue_style( |
| 176 |
'eb-block-style-' . $template, |
| 177 |
$this->style_url . $this->get_eb_filename( $template ), |
| 178 |
$deps, |
| 179 |
substr( md5( microtime( true ) ), 0, 10 ) |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// NotificationX Post Type Support |
| 186 |
if ( in_array( 'notificationx/notificationx.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
| 187 |
$gutenberg_ids = $this->get_gutenberg_id_from_nx(); |
| 188 |
if ( is_array( $gutenberg_ids ) && ! empty( $gutenberg_ids ) ) { |
| 189 |
foreach ( $gutenberg_ids as $element ) { |
| 190 |
if ( file_exists( $this->frontend_style_dir . $this->get_frontend_filename( $element ) ) ) { |
| 191 |
wp_enqueue_style( |
| 192 |
'frontend' . $element, |
| 193 |
$this->frontend_style_url . $this->get_frontend_filename( $element ), |
| 194 |
$deps, |
| 195 |
substr( md5( microtime( true ) ), 0, 10 ) |
| 196 |
); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// Astra Addon Advanced Hooks CSS files are now handled by astra_advanced_hooks_css_generation() method |
| 203 |
if ( in_array( 'astra-addon/astra-addon.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && defined( 'ASTRA_ADVANCED_HOOKS_POST_TYPE' ) ) { |
| 204 |
foreach ( $this->astra_hook_ids as $element_id ) { |
| 205 |
if ( file_exists( $this->style_dir . $this->get_eb_filename( $element_id ) ) ) { |
| 206 |
wp_enqueue_style( 'eb-block-style-' . $element_id, $this->style_url . $this->get_eb_filename( $element_id ), $deps, substr( md5( microtime( true ) ), 0, 10 ) ); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
//Widget Style Enqueue |
| 212 |
$this->enqueue_widget_styles(); |
| 213 |
|
| 214 |
//FSE Style Enqueue |
| 215 |
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 216 |
$templates = array_unique( $this->fse_template_ids ); |
| 217 |
foreach ( $templates as $template ) { |
| 218 |
if ( is_integer( $template ) && file_exists( $this->fse_style_dir . $this->get_fse_filename( $template ) ) ) { |
| 219 |
wp_enqueue_style( 'eb-fse-style-' . $template, $this->fse_style_url . $this->get_fse_filename( $template ), array( ), substr( md5( microtime( true ) ), 0, 10 ) ); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Hooks assets for enqueue in frontend |
| 226 |
* |
| 227 |
* @param $path string |
| 228 |
* @param $url string |
| 229 |
* |
| 230 |
* @since 3.0.0 |
| 231 |
*/ |
| 232 |
do_action( 'eb_frontend_assets', $this->style_dir, $this->style_url ); |
| 233 |
} |
| 234 |
|
| 235 |
public function load_frontend_scripts_conditionally( $block_content, $block ) { |
| 236 |
if ( null === $block_content ) { |
| 237 |
$block_content = ""; |
| 238 |
} |
| 239 |
|
| 240 |
// Load our main frontend scripts if there's a Essential block |
| 241 |
// loaded in the frontend. |
| 242 |
if ( ! $this->is_main_script_loaded && ! is_admin() ) { |
| 243 |
if ( strpos( $block_content, '<!-- wp:essential-blocks/' ) !== false ) { |
| 244 |
$this->enqueue_frontend_assets(); |
| 245 |
$this->is_main_script_loaded = true; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Only do this for Essential blocks. |
| 250 |
if ( ! isset( $block[ 'blockName' ] ) || strpos( $block[ 'blockName' ], 'essential-blocks/' ) === false ) { |
| 251 |
return $block_content; |
| 252 |
} |
| 253 |
|
| 254 |
return $block_content; |
| 255 |
} |
| 256 |
|
| 257 |
public function load_frontend_css_file( $post_id, $handle_name, $block_names ) { |
| 258 |
$css_file = $this->frontend_style_dir . $this->get_frontend_filename( $post_id ); |
| 259 |
$css_url = $this->frontend_style_url . $this->get_frontend_filename( $post_id ); |
| 260 |
|
| 261 |
if ( file_exists( $css_file ) ) { |
| 262 |
wp_enqueue_style( $handle_name, $css_url, array( ), filemtime( $css_file ) ); |
| 263 |
} else { |
| 264 |
$handle_name = $this->generate_frontend_css_file( $block_names, $handle_name, $css_file, $css_url ); |
| 265 |
} |
| 266 |
return $handle_name; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Generate Frontend CSS file |
| 271 |
* @param array $block_names |
| 272 |
* @return string |
| 273 |
*/ |
| 274 |
public function generate_frontend_css_file( $block_names, $handle, $filename, $fileurl ) { |
| 275 |
$all_blocks = array_unique( $block_names ); |
| 276 |
|
| 277 |
$css = ''; |
| 278 |
if ( count( $all_blocks ) > 0 ) { |
| 279 |
foreach ( $all_blocks as $block ) { |
| 280 |
$blockname = ''; |
| 281 |
$dir = ''; |
| 282 |
if ( defined( 'ESSENTIAL_BLOCKS_PRO_DIR_PATH' ) && 0 === strpos( $block, 'essential-blocks/pro-' ) ) { |
| 283 |
$split_name = explode( '/', $block ); |
| 284 |
$blockname = str_replace( 'pro-', '', $split_name[ 1 ] ); |
| 285 |
$dir = ESSENTIAL_BLOCKS_PRO_DIR_PATH . 'assets' . DIRECTORY_SEPARATOR . 'blocks' . DIRECTORY_SEPARATOR . $blockname . DIRECTORY_SEPARATOR . 'style.css'; |
| 286 |
} elseif ( 0 === strpos( $block, 'essential-blocks/' ) ) { |
| 287 |
$split_name = explode( '/', $block ); |
| 288 |
$blockname = $split_name[ 1 ]; |
| 289 |
$dir = ESSENTIAL_BLOCKS_DIR_PATH . 'assets' . DIRECTORY_SEPARATOR . 'blocks' . DIRECTORY_SEPARATOR . $split_name[ 1 ] . DIRECTORY_SEPARATOR . 'style.css'; |
| 290 |
} else { |
| 291 |
continue; |
| 292 |
} |
| 293 |
if ( file_exists( $dir ) && strlen( $blockname ) > 0 ) { |
| 294 |
$css .= apply_filters( "eb_fixed_frontend_styles/{$blockname}", file_get_contents( $dir ), $blockname, ); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
//Write CSS File and Enqueue |
| 300 |
if ( strlen( trim( $css ) ) > 0 ) { |
| 301 |
if ( ! file_exists( $this->frontend_style_dir ) ) { |
| 302 |
mkdir( $this->frontend_style_dir, 0755, true ); |
| 303 |
} |
| 304 |
|
| 305 |
//Replace Breakpoints |
| 306 |
$breakpoints = array( |
| 307 |
'tablet' => CSSParser::get_responsive_breakpoints( 'tablet' ), |
| 308 |
'mobile' => CSSParser::get_responsive_breakpoints( 'mobile' ) |
| 309 |
); |
| 310 |
|
| 311 |
$all_breakpoints = array( |
| 312 |
'1024' => $breakpoints[ 'tablet' ], |
| 313 |
'1023' => $breakpoints[ 'tablet' ] - 1, |
| 314 |
'1025' => $breakpoints[ 'tablet' ] + 1, |
| 315 |
'767' => $breakpoints[ 'mobile' ], |
| 316 |
'768' => $breakpoints[ 'mobile' ] + 1 |
| 317 |
); |
| 318 |
|
| 319 |
foreach ( $all_breakpoints as $old => $new ) { |
| 320 |
$css = preg_replace( "/(@media[^{]+)width:\s*" . preg_quote( $old ) . "px/", "$1width:" . $new . "px", $css, -1, $count ); |
| 321 |
} |
| 322 |
file_put_contents( $filename, $css ); |
| 323 |
//Enqueue |
| 324 |
wp_enqueue_style( $handle, $fileurl, array( ), filemtime( $filename ) ); |
| 325 |
return $handle; |
| 326 |
} |
| 327 |
return ''; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Generate FSE Assets |
| 332 |
*/ |
| 333 |
public function fse_assets_generation( $template, $type, $templates ) { |
| 334 |
$block_template = resolve_block_template( $type, $templates, $template ); |
| 335 |
if ( ! empty( $block_template ) ) { |
| 336 |
if ( isset( $block_template->content ) ) { |
| 337 |
$parsed_content = parse_blocks( $block_template->content ); |
| 338 |
|
| 339 |
if ( is_array( $parsed_content ) && ! empty( $parsed_content ) ) { |
| 340 |
if ( isset( $block_template->wp_id ) && isset( $block_template->type ) ) { |
| 341 |
$this->fse_page_template_id = $block_template->wp_id; |
| 342 |
$this->fse_template_ids[ ] = $block_template->wp_id; |
| 343 |
$this->write_css_from_content( $parsed_content, $block_template->wp_id, $block_template->type ); |
| 344 |
} |
| 345 |
|
| 346 |
foreach ( $parsed_content as $content ) { |
| 347 |
$this->fse_template_parts_recursive( $content ); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return $template; |
| 354 |
} |
| 355 |
|
| 356 |
public function fse_template_parts_recursive( $content ) { |
| 357 |
if ( isset( $content[ 'blockName' ] ) && isset( $content[ 'attrs' ][ 'theme' ] ) && isset( $content[ 'attrs' ][ 'slug' ] ) ) { |
| 358 |
$id = $content[ 'attrs' ][ 'theme' ] . '//' . $content[ 'attrs' ][ 'slug' ]; |
| 359 |
$template = ''; |
| 360 |
if ( ( 'core/template-part' === $content[ 'blockName' ] ) ) { |
| 361 |
$template = get_block_template( $id, 'wp_template_part' ); |
| 362 |
} elseif ( ( 'core/template' === $content[ 'blockName' ] ) ) { |
| 363 |
$template = get_block_template( $id, 'wp_template' ); |
| 364 |
} |
| 365 |
|
| 366 |
if ( isset( $template->content ) && isset( $template->wp_id ) && isset( $template->type ) ) { |
| 367 |
$this->write_css_from_content( parse_blocks( $template->content ), $template->wp_id, $template->type ); |
| 368 |
|
| 369 |
if ( empty( $this->fse_page_template_id ) ) { |
| 370 |
$this->fse_page_template_id = $template->wp_id; |
| 371 |
} |
| 372 |
} |
| 373 |
} elseif ( isset( $content[ 'innerBlocks' ] ) && count( $content[ 'innerBlocks' ] ) > 0 ) { |
| 374 |
if ( count( $content[ 'innerBlocks' ] ) > 0 ) { |
| 375 |
foreach ( $content[ 'innerBlocks' ] as $block ) { |
| 376 |
self::fse_template_parts_recursive( $block ); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Write CSS for EB Blocks |
| 384 |
* @param array $parsed_content |
| 385 |
* @param integer $post_id |
| 386 |
* @return void |
| 387 |
*/ |
| 388 |
public function write_css_from_content( $parsed_content, $post_id = false, $type = '' ) { |
| 389 |
if ( count( $parsed_content ) === 0 ) { |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
$filename = $this->style_dir . $this->get_eb_filename( $post_id ); |
| 394 |
$eb_blocks = array( ); |
| 395 |
if ( 'wp_template' === $type || 'wp_template_part' === $type ) { |
| 396 |
$recursive_response = CSSParser::eb_block_style_recursive( $parsed_content, $eb_blocks, $this->fse_block_names ); |
| 397 |
$filename = $this->fse_style_dir . $this->get_fse_filename( $post_id ); |
| 398 |
$this->fse_template_ids[ ] = $post_id; |
| 399 |
} else { |
| 400 |
$recursive_response = CSSParser::eb_block_style_recursive( $parsed_content, $eb_blocks, $this->block_names ); |
| 401 |
} |
| 402 |
|
| 403 |
//Check if file exists, return |
| 404 |
if ( file_exists( $filename ) ) { |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
$reusable_Blocks = ! empty( $recursive_response[ 'reusableBlocks' ] ) ? $recursive_response[ 'reusableBlocks' ] : array( ); |
| 409 |
// remove empty reusable blocks |
| 410 |
$reusable_Blocks = array_filter( $reusable_Blocks, function ( $v ) { |
| 411 |
return ! empty( $v ); |
| 412 |
} ); |
| 413 |
unset( $recursive_response[ "reusableBlocks" ] ); |
| 414 |
$style = CSSParser::blocks_to_style_array( $recursive_response ); |
| 415 |
|
| 416 |
if ( false !== $post_id ) { |
| 417 |
$this->write_block_css( $style, $post_id, $type ); //Write CSS file for this page |
| 418 |
} |
| 419 |
|
| 420 |
$reusableIds = $reusable_Blocks ? array_keys( $reusable_Blocks ) : array( ); |
| 421 |
if ( ! empty( $reusableIds ) ) { |
| 422 |
update_option( '_eb_reusable_block_ids', $reusableIds ); |
| 423 |
} |
| 424 |
update_post_meta( $post_id, '_eb_reusable_block_ids', $reusableIds ); |
| 425 |
|
| 426 |
if ( ! empty( $reusable_Blocks ) ) { |
| 427 |
foreach ( $reusable_Blocks as $blockId => $block ) { |
| 428 |
$style = CSSParser::blocks_to_style_array( $block ); |
| 429 |
$this->write_reusable_block_css( $style, $blockId ); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Write block css in upload directory |
| 436 |
* @retun void |
| 437 |
* @since 1.0.2 |
| 438 |
*/ |
| 439 |
private function write_block_css( $block_styles, $post_id, $post_type ) { |
| 440 |
// Write CSS for Page/Posts |
| 441 |
if ( ! empty( $css = CSSParser::build_css( $block_styles ) ) ) { |
| 442 |
if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) { |
| 443 |
if ( ! file_exists( $this->fse_style_dir ) ) { |
| 444 |
mkdir( $this->fse_style_dir, 0755, true ); |
| 445 |
} |
| 446 |
file_put_contents( $this->fse_style_dir . $this->get_fse_filename( $post_id ), $css ); |
| 447 |
} else { |
| 448 |
if ( ! file_exists( $this->style_dir ) ) { |
| 449 |
mkdir( $this->style_dir, 0755, true ); |
| 450 |
} |
| 451 |
file_put_contents( $this->style_dir . $this->get_eb_filename( $post_id ), $css ); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Write css for Reusable block |
| 458 |
* @retun void |
| 459 |
* @since 3.4.0 |
| 460 |
*/ |
| 461 |
private function write_reusable_block_css( $block_styles, $id ) { |
| 462 |
if ( isset( $block_styles ) && is_array( $block_styles ) ) { |
| 463 |
if ( ! empty( $css = CSSParser::build_css( $block_styles ) ) ) { |
| 464 |
$upload_dir = $this->style_dir . 'reusable-blocks/'; |
| 465 |
if ( ! file_exists( $upload_dir ) ) { |
| 466 |
mkdir( $upload_dir, 0755, true ); |
| 467 |
} |
| 468 |
file_put_contents( $upload_dir . DIRECTORY_SEPARATOR . 'eb-reusable-' . abs( $id ) . '.min.css', $css ); |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Single file css generator |
| 475 |
* @retun void |
| 476 |
* @since 3.5.3 |
| 477 |
*/ |
| 478 |
private function single_file_css_generator( $block_styles, $upload_dir, $filename ) { |
| 479 |
$editSiteCssPath = $upload_dir . $filename; |
| 480 |
if ( file_exists( $editSiteCssPath ) ) { |
| 481 |
$existingCss = file_get_contents( $editSiteCssPath ); |
| 482 |
$pattern = "~\/\*(.*?)\*\/~"; |
| 483 |
preg_match_all( $pattern, $existingCss, $result, PREG_PATTERN_ORDER ); |
| 484 |
$allComments = $result[ 0 ]; |
| 485 |
$seperatedIds = array( ); |
| 486 |
foreach ( $allComments as $comment ) { |
| 487 |
$id = preg_replace( '/[^A-Za-z0-9\-]|Ends|Starts/', '', $comment ); |
| 488 |
|
| 489 |
if ( strpos( $comment, "Starts" ) ) { |
| 490 |
$seperatedIds[ $id ][ 'start' ] = $comment; |
| 491 |
} elseif ( strpos( $comment, "Ends" ) ) { |
| 492 |
$seperatedIds[ $id ][ 'end' ] = $comment; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
$seperateStyles = array( ); |
| 497 |
foreach ( $seperatedIds as $key => $ids ) { |
| 498 |
$seperateStyles[ ][ $key ] = isset( $block_styles[ $key ] ) ? $block_styles[ $key ] : array( ); |
| 499 |
} |
| 500 |
|
| 501 |
self::$_block_styles = array_merge( self::$_block_styles, $block_styles ); |
| 502 |
|
| 503 |
if ( ! empty( $css = CSSParser::build_css( self::$_block_styles ) ) ) { |
| 504 |
if ( ! file_exists( $upload_dir ) ) { |
| 505 |
mkdir( $upload_dir ); |
| 506 |
} |
| 507 |
|
| 508 |
file_put_contents( $editSiteCssPath, $css ); |
| 509 |
} |
| 510 |
} else { |
| 511 |
self::$_block_styles = $block_styles; |
| 512 |
if ( ! empty( $css = CSSParser::build_css( $block_styles ) ) ) { |
| 513 |
if ( ! file_exists( $this->style_dir ) ) { |
| 514 |
mkdir( $this->style_dir ); |
| 515 |
} |
| 516 |
file_put_contents( $editSiteCssPath, $css ); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Save Widget CSS when Widget is saved |
| 523 |
* @return void |
| 524 |
* @since 3.5.3 |
| 525 |
*/ |
| 526 |
public function after_save_widget( $id, $sidebar_id, $request, $creating ) { |
| 527 |
$parsed_content = isset( $request[ 'instance' ][ 'raw' ][ 'content' ] ) ? parse_blocks( $request[ 'instance' ][ 'raw' ][ 'content' ] ) : array( ); |
| 528 |
if ( is_array( $parsed_content ) && ! empty( $parsed_content ) ) { |
| 529 |
$eb_blocks = array( ); |
| 530 |
$recursive_response = CSSParser::eb_block_style_recursive( $parsed_content, $eb_blocks, $this->widget_block_names ); |
| 531 |
unset( $recursive_response[ "reusableBlocks" ] ); |
| 532 |
$style = CSSParser::blocks_to_style_array( $recursive_response ); |
| 533 |
//Write CSS file for Widget |
| 534 |
$this->single_file_css_generator( $style, $this->style_dir, $this->prefix . '-widget.min.css' ); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Load Dependencies |
| 540 |
*/ |
| 541 |
private function load_style_handler_dependencies() { |
| 542 |
require_once plugin_dir_path( __FILE__ ) . 'includes/class-parse-css.php'; |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Get post content when page is saved |
| 547 |
*/ |
| 548 |
public function on_save_post( $post_id, $post, $update ) { |
| 549 |
$post_type = get_post_type( $post_id ); |
| 550 |
|
| 551 |
//If This page is draft, return |
| 552 |
if ( isset( $post->post_status ) && 'auto-draft' == $post->post_status ) { |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
// Autosave, do nothing |
| 557 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
// Return if it's a post revision |
| 562 |
if ( false !== wp_is_post_revision( $post_id ) ) { |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
//Remove frontend assets on save post/FSE |
| 567 |
$clean_frontend = false; |
| 568 |
if ( 'templately_library' === $post_type ) { |
| 569 |
$clean_frontend = true; |
| 570 |
} |
| 571 |
$this->remove_frontend_assets( $post_id, $post_type, $clean_frontend ); |
| 572 |
|
| 573 |
// $parsed_content = $this->get_parsed_content( $post_id, $post, $post_type ); |
| 574 |
|
| 575 |
// if ( is_array( $parsed_content ) && ! empty( $parsed_content ) ) { |
| 576 |
// $this->write_css_from_content( $parsed_content, $post_id, $post_type ); |
| 577 |
// } |
| 578 |
} |
| 579 |
|
| 580 |
private function get_parsed_content( $post_id, $post, $post_type ) { |
| 581 |
if ( 'wp_template_part' === $post_type || 'wp_template' === $post_type ) { |
| 582 |
$post = get_post( $post_id ); |
| 583 |
} |
| 584 |
|
| 585 |
$parsed_content = parse_blocks( $post->post_content ); |
| 586 |
|
| 587 |
if ( empty( $parsed_content ) ) { |
| 588 |
delete_post_meta( $post_id, '_eb_reusable_block_ids' ); |
| 589 |
} |
| 590 |
|
| 591 |
return $parsed_content; |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Get post content when page is load in frontend |
| 596 |
*/ |
| 597 |
public function generate_post_content() { |
| 598 |
// FSE assets generation |
| 599 |
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 600 |
add_filter( "404_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 601 |
add_filter( "archive_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 602 |
add_filter( "category_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 603 |
add_filter( "frontpage_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 604 |
add_filter( "home_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 605 |
add_filter( "index_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 606 |
add_filter( "page_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 607 |
add_filter( "search_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 608 |
add_filter( "single_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 609 |
add_filter( "singular_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 610 |
add_filter( "tag_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 611 |
add_filter( "taxonomy_template", array( $this, 'fse_assets_generation' ), 99, 3 ); |
| 612 |
} |
| 613 |
|
| 614 |
//Write CSS from content if css file not exists for specific post by id |
| 615 |
$post_id = get_the_ID(); |
| 616 |
if ( $post_id ) { |
| 617 |
$css_filepath = $this->style_dir . $this->prefix . '-' . $post_id . '.min.css'; |
| 618 |
$frontend_css_filepath = $this->frontend_style_dir . $this->frontend_prefix . '-' . $post_id . '.min.css'; |
| 619 |
if ( ! file_exists( $css_filepath ) || ! file_exists( $frontend_css_filepath ) ) { |
| 620 |
$post_type = get_post_type( $post_id ); |
| 621 |
$post = get_post( $post_id ); |
| 622 |
//If This page is draft, return |
| 623 |
if ( isset( $post->post_status ) && 'auto-draft' == $post->post_status ) { |
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
// Return if it's a post revision |
| 628 |
if ( false !== wp_is_post_revision( $post_id ) ) { |
| 629 |
return null; |
| 630 |
} |
| 631 |
$parsed_content = $this->get_parsed_content( $post_id, $post, $post_type ); |
| 632 |
|
| 633 |
if ( is_array( $parsed_content ) && ! empty( $parsed_content ) ) { |
| 634 |
$this->write_css_from_content( $parsed_content, $post_id, $post_type ); |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Get post id by post_name for FSE template |
| 642 |
*/ |
| 643 |
public static function eb_get_post_content_by_post_name( $post_name ) { |
| 644 |
global $wpdb; |
| 645 |
$sql = $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_name = %s", $post_name ); |
| 646 |
|
| 647 |
return $wpdb->get_results( $sql, ARRAY_A ); |
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
* Remove frontend assets file or folder to regenerate assets |
| 652 |
* If $post_id is false or $post_type is FSE templates, remove directory otherwise remove file by post_id |
| 653 |
* @param integer $post_id |
| 654 |
* @param string $post_type |
| 655 |
* @return void |
| 656 |
*/ |
| 657 |
public function remove_frontend_assets( $post_id = false, $post_type = false, $clean_frontend = false ) { |
| 658 |
if ( false === $post_type || ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) ) { |
| 659 |
AssetGeneration::remove_file( $this->fse_style_dir . $this->get_fse_filename( $post_id ) ); |
| 660 |
} elseif ( $post_id ) { |
| 661 |
AssetGeneration::remove_file( $this->style_dir . $this->get_eb_filename( $post_id ) ); |
| 662 |
} |
| 663 |
|
| 664 |
if ( true === $clean_frontend ) { |
| 665 |
AssetGeneration::remove_directory_files( $this->frontend_style_dir ); |
| 666 |
} else { |
| 667 |
AssetGeneration::remove_file( $this->frontend_style_dir . $this->get_frontend_filename( $post_id ) ); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
/** |
| 672 |
* Write css for widget |
| 673 |
*/ |
| 674 |
public function eb_widget_dynamic_sidebar_params( $params ) { |
| 675 |
global $wp_registered_widgets; |
| 676 |
|
| 677 |
$widget_id = $params[ 0 ][ 'widget_id' ]; |
| 678 |
$widget_obj = $wp_registered_widgets[ $widget_id ]; |
| 679 |
$widget_options = get_option( $widget_obj[ 'callback' ][ 0 ]->option_name ); |
| 680 |
|
| 681 |
// Find the specific instance for this widget ID |
| 682 |
$widget_number = $widget_obj[ 'params' ][ 0 ][ 'number' ]; |
| 683 |
$instance = $widget_options[ $widget_number ]; |
| 684 |
|
| 685 |
$parsed_content = isset( $instance[ 'content' ] ) ? parse_blocks( $instance[ 'content' ] ) : array( ); |
| 686 |
if ( is_array( $parsed_content ) && ! empty( $parsed_content ) ) { |
| 687 |
$eb_blocks = array( ); |
| 688 |
$recursive_response = CSSParser::eb_block_style_recursive( $parsed_content, $eb_blocks, $this->widget_block_names ); |
| 689 |
unset( $recursive_response[ "reusableBlocks" ] ); |
| 690 |
$style = CSSParser::blocks_to_style_array( $recursive_response ); |
| 691 |
|
| 692 |
//Write CSS file for Widget |
| 693 |
$this->single_file_css_generator( $style, $this->style_dir, $this->prefix . '-widget.min.css' ); |
| 694 |
} |
| 695 |
|
| 696 |
return $params; |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* generate_element_post_id |
| 701 |
* |
| 702 |
* @param $post_id int |
| 703 |
* |
| 704 |
* @return int |
| 705 |
*/ |
| 706 |
public function get_generatepress_element( $post_id ) { |
| 707 |
if ( empty( $post_id ) ) { |
| 708 |
return $post_id; |
| 709 |
} |
| 710 |
|
| 711 |
$display_conditions = get_post_meta( $post_id, '_generate_element_display_conditions', true ); |
| 712 |
$display_conditions = $display_conditions ? $display_conditions : array( ); |
| 713 |
$exclude_conditions = get_post_meta( $post_id, '_generate_element_exclude_conditions', true ); |
| 714 |
$exclude_conditions = $exclude_conditions ? $exclude_conditions : array( ); |
| 715 |
$user_conditions = get_post_meta( $post_id, '_generate_element_user_conditions', true ); |
| 716 |
$user_conditions = $user_conditions ? $user_conditions : array( ); |
| 717 |
|
| 718 |
$display = \GeneratePress_Conditions::show_data( |
| 719 |
$display_conditions, |
| 720 |
$exclude_conditions, |
| 721 |
$user_conditions |
| 722 |
); |
| 723 |
|
| 724 |
if ( $display ) { |
| 725 |
$this->gp_ids[ ] = $post_id; |
| 726 |
$post = get_post( $post_id ); |
| 727 |
$parsed_content = parse_blocks( $post->post_content ); |
| 728 |
$this->write_css_from_content( $parsed_content, $post_id, $post->post_type ); |
| 729 |
} |
| 730 |
|
| 731 |
return $post_id; |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Generate CSS for Astra Addon Advanced Hooks |
| 736 |
* |
| 737 |
* @since 5.0.8 |
| 738 |
*/ |
| 739 |
public function astra_advanced_hooks_css_generation() { |
| 740 |
// Check if Astra Addon is active and advanced hooks are available |
| 741 |
if ( |
| 742 |
! in_array( 'astra-addon/astra-addon.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || |
| 743 |
! defined( 'ASTRA_ADVANCED_HOOKS_POST_TYPE' ) || |
| 744 |
! class_exists( 'Astra_Target_Rules_Fields' ) |
| 745 |
) { |
| 746 |
return; |
| 747 |
} |
| 748 |
|
| 749 |
// Use the same logic as Astra Addon to get active advanced hooks |
| 750 |
$option = array( |
| 751 |
'location' => 'ast-advanced-hook-location', |
| 752 |
'exclusion' => 'ast-advanced-hook-exclusion', |
| 753 |
'users' => 'ast-advanced-hook-users' |
| 754 |
); |
| 755 |
|
| 756 |
$result = \Astra_Target_Rules_Fields::get_instance()->get_posts_by_conditions( ASTRA_ADVANCED_HOOKS_POST_TYPE, $option ); |
| 757 |
|
| 758 |
foreach ( $result as $post_id => $post_data ) { |
| 759 |
if ( ! empty( $post_id ) ) { |
| 760 |
$this->astra_hook_ids[ ] = $post_id; |
| 761 |
$post = get_post( $post_id ); |
| 762 |
if ( $post && ! empty( $post->post_content ) ) { |
| 763 |
$parsed_content = parse_blocks( $post->post_content ); |
| 764 |
$this->write_css_from_content( $parsed_content, $post_id, $post->post_type ); |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
* Function for generate Styles for Templately Theme Builder Templates |
| 772 |
* @since 5.0.8 |
| 773 |
* @param integer $template_id |
| 774 |
* @param string $location |
| 775 |
* @param object $template |
| 776 |
* @return void |
| 777 |
*/ |
| 778 |
public function templately_templates( $template_id, $location, $template ) { |
| 779 |
if ( ! is_int( $template_id ) ) { |
| 780 |
return; |
| 781 |
} |
| 782 |
$this->templately_template_ids[ ] = $template_id; |
| 783 |
$css_filepath = $this->style_dir . $this->get_eb_filename( $template_id ); |
| 784 |
// If check file_exists, it creates issue for frontend asset load. We will fix this later |
| 785 |
// if ( ! file_exists( $css_filepath ) ) { |
| 786 |
|
| 787 |
// } |
| 788 |
$post = get_post( $template_id ); |
| 789 |
if ( is_object( $post ) && property_exists( $post, 'post_content' ) ) { |
| 790 |
$content = $post->post_content; |
| 791 |
$parsed_content = parse_blocks( $content ); |
| 792 |
$this->write_css_from_content( $parsed_content, $template_id, $post->post_type ); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
/** |
| 797 |
* Enqueue widget css in footer |
| 798 |
* @return void |
| 799 |
*/ |
| 800 |
public function eb_add_widget_css_footer() { |
| 801 |
$this->enqueue_widget_styles(); |
| 802 |
} |
| 803 |
|
| 804 |
public function enqueue_widget_styles() { |
| 805 |
global $post; |
| 806 |
if ( ! empty( $post ) && ! empty( $post->ID ) ) { |
| 807 |
$css_file = $this->frontend_style_dir . $this->get_widget_frontend_filename( $post->ID ); |
| 808 |
$css_url = $this->frontend_style_url . $this->get_widget_frontend_filename( $post->ID ); |
| 809 |
|
| 810 |
$frontend_handle = 'essential-blocks-widgte-frontend-style'; |
| 811 |
if ( file_exists( $css_file ) ) { |
| 812 |
wp_enqueue_style( $frontend_handle, $css_url, array( ), filemtime( $css_file ) ); |
| 813 |
$deps[ ] = $frontend_handle; |
| 814 |
} else { |
| 815 |
$deps[ ] = $this->generate_frontend_css_file( $this->widget_block_names, $frontend_handle, $css_file, $css_url ); |
| 816 |
} |
| 817 |
|
| 818 |
if ( file_exists( $this->style_dir . $this->prefix . '-widget.min.css' ) ) { |
| 819 |
wp_enqueue_style( 'eb-widget-style', $this->style_url . $this->prefix . '-widget.min.css', $deps, substr( md5( microtime( true ) ), 0, 10 ), 'all' ); |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
public function get_fse_filename( $post_id ) { |
| 825 |
return $this->fse_prefix . '-' . trim( $post_id ) . '.min.css'; |
| 826 |
} |
| 827 |
|
| 828 |
public function get_eb_filename( $post_id ) { |
| 829 |
return $this->prefix . '-' . trim( $post_id ) . '.min.css'; |
| 830 |
} |
| 831 |
|
| 832 |
public function get_frontend_filename( $post_id ) { |
| 833 |
return $this->frontend_prefix . '-' . trim( $post_id ) . '.min.css'; |
| 834 |
} |
| 835 |
|
| 836 |
public function get_widget_frontend_filename( $post_id ) { |
| 837 |
return $this->frontend_prefix . '-widget-' . trim( $post_id ) . '.min.css'; |
| 838 |
} |
| 839 |
|
| 840 |
/** |
| 841 |
* Get gutenberg_id from nx press bar |
| 842 |
*/ |
| 843 |
public function get_gutenberg_id_from_nx() { |
| 844 |
$nx_posts = \NotificationX\Core\Database::get_instance()->query() |
| 845 |
->from( 'nx_posts' ) |
| 846 |
->where( 'enabled', true ) |
| 847 |
->where( 'source', 'press_bar' ) |
| 848 |
->get(); |
| 849 |
$gutenberg_ids = array( ); |
| 850 |
if ( ! empty( $nx_posts ) ) { |
| 851 |
foreach ( $nx_posts as $nx_post ) { |
| 852 |
$data = maybe_unserialize( $nx_post->data ); |
| 853 |
if ( ! empty( $data[ 'gutenberg_id' ] ) ) { |
| 854 |
$gutenberg_ids[ ] = $data[ 'gutenberg_id' ]; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
return $gutenberg_ids; |
| 859 |
} |
| 860 |
} |