| 1 |
<?php |
| 2 |
/** |
| 3 |
* Generate Dynamic Style |
| 4 |
* @package GutSliderBlocks |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. |
| 8 |
|
| 9 |
if( ! class_exists( 'GutSlider_Dynamic_Style' ) ) { |
| 10 |
|
| 11 |
class GutSlider_Dynamic_Style { |
| 12 |
private $styles = []; |
| 13 |
private $upload_dir; |
| 14 |
private $upload_url; |
| 15 |
|
| 16 |
/** |
| 17 |
* Constructor |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_filter( 'render_block', [ $this, 'collect_block_styles' ], 10, 2 ); |
| 21 |
|
| 22 |
if( wp_is_block_theme( ) ) { |
| 23 |
add_action( 'wp_enqueue_scripts', [ $this, 'generate_and_enqueue_combined_css' ] ); |
| 24 |
} else { |
| 25 |
add_action( 'wp_footer', [ $this, 'generate_and_enqueue_combined_css' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
// Set upload directory and URL |
| 29 |
$upload_dir = wp_upload_dir(); |
| 30 |
$this->upload_dir = $upload_dir['basedir'] . '/gutslider-styles/'; |
| 31 |
$this->upload_url = $upload_dir['baseurl'] . '/gutslider-styles/'; |
| 32 |
|
| 33 |
// Create directory if it doesn't exist |
| 34 |
if ( ! file_exists( $this->upload_dir ) ) { |
| 35 |
wp_mkdir_p( $this->upload_dir ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Collect styles from individual blocks |
| 41 |
*/ |
| 42 |
public function collect_block_styles($block_content, $block) { |
| 43 |
if (isset($block['blockName']) && str_contains($block['blockName'], 'gutsliders/')) { |
| 44 |
do_action( 'gutsliders_render_block', $block ); |
| 45 |
|
| 46 |
if (isset($block['attrs']['blockStyle'])) { |
| 47 |
$style = $block['attrs']['blockStyle']; |
| 48 |
|
| 49 |
if ( is_array( $style ) && !empty( $style ) ) { |
| 50 |
$style = implode(' ', $style); |
| 51 |
} |
| 52 |
|
| 53 |
$this->styles[] = $style; |
| 54 |
} |
| 55 |
} |
| 56 |
return $block_content; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Generate and enqueue combined CSS |
| 61 |
*/ |
| 62 |
public function generate_and_enqueue_combined_css() { |
| 63 |
if (empty($this->styles)) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$combined_css = implode("\n", $this->styles); |
| 68 |
$minified_css = $this->minify_css($combined_css); |
| 69 |
|
| 70 |
$css_file_name = 'gutslider-styles-' . get_the_ID() . '.min.css'; |
| 71 |
$css_file_path = $this->upload_dir . $css_file_name; |
| 72 |
$css_file_url = $this->upload_url . $css_file_name; |
| 73 |
|
| 74 |
// Initialize the WP_Filesystem |
| 75 |
global $wp_filesystem; |
| 76 |
if (empty($wp_filesystem)) { |
| 77 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 78 |
WP_Filesystem(); |
| 79 |
} |
| 80 |
|
| 81 |
// Only create a new file if it doesn't exist or the content has changed |
| 82 |
$existing_content = $wp_filesystem->exists($css_file_path) ? $wp_filesystem->get_contents($css_file_path) : ''; |
| 83 |
if ($existing_content !== $minified_css) { |
| 84 |
$wp_filesystem->put_contents($css_file_path, $minified_css, FS_CHMOD_FILE); |
| 85 |
} |
| 86 |
|
| 87 |
// Check if file exists and is readable before enqueuing |
| 88 |
if (file_exists($css_file_path) && is_readable($css_file_path)) { |
| 89 |
$version = filemtime($css_file_path); |
| 90 |
} else { |
| 91 |
$version = time(); // Fallback version number |
| 92 |
} |
| 93 |
|
| 94 |
// Enqueue the combined CSS file |
| 95 |
if( file_exists( $css_file_path ) ) { |
| 96 |
wp_enqueue_style( |
| 97 |
'gutslider-combined-styles', |
| 98 |
$css_file_url, |
| 99 |
[], |
| 100 |
$version |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Minify CSS |
| 108 |
*/ |
| 109 |
private function minify_css($css) { |
| 110 |
// Remove comments |
| 111 |
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css); |
| 112 |
// Remove space after colons |
| 113 |
$css = str_replace(': ', ':', $css); |
| 114 |
// Remove whitespace |
| 115 |
$css = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $css); |
| 116 |
return $css; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
new GutSlider_Dynamic_Style(); // Initialize the class |