| @@ -3,51 +3,119 @@ | ||
| 3 | 3 | * Generate Dynamic Style |
| 4 | 4 | * @package GutSliderBlocks |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | - if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. | |
| 7 | +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. | |
| 8 | 8 | |
| 9 | - if( ! class_exists( 'GutSlider_Dynamic_Style' ) ) { | |
| 9 | +if( ! class_exists( 'GutSlider_Dynamic_Style' ) ) { | |
| 10 | 10 | |
| 11 | 11 | class GutSlider_Dynamic_Style { |
| 12 | + private $styles = []; | |
| 13 | + private $upload_dir; | |
| 14 | + private $upload_url; | |
| 12 | 15 | |
| 13 | 16 | /** |
| 14 | 17 | * Constructor |
| 15 | - * return void | |
| 16 | 18 | */ |
| 17 | 19 | public function __construct() { |
| 18 | - add_filter( 'render_block', [ $this, 'generate_dynamic_style' ], 10, 2 ); | |
| 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 | + } | |
| 19 | 37 | } |
| 20 | 38 | |
| 21 | - function generate_dynamic_style($block_content, $block) { | |
| 39 | + /** | |
| 40 | + * Collect styles from individual blocks | |
| 41 | + */ | |
| 42 | + public function collect_block_styles($block_content, $block) { | |
| 22 | 43 | if (isset($block['blockName']) && str_contains($block['blockName'], 'gutsliders/')) { |
| 23 | - | |
| 24 | 44 | do_action( 'gutsliders_render_block', $block ); |
| 25 | 45 | |
| 26 | 46 | if (isset($block['attrs']['blockStyle'])) { |
| 27 | - | |
| 28 | 47 | $style = $block['attrs']['blockStyle']; |
| 29 | - | |
| 30 | - $handle = isset( $block['attrs']['uniqueId'] ) ? $block['attrs']['uniqueId'] : 'slider-blocks'; | |
| 31 | - | |
| 48 | + | |
| 32 | 49 | if ( is_array( $style ) && !empty( $style ) ) { |
| 33 | 50 | $style = implode(' ', $style); |
| 34 | 51 | } |
| 35 | - // minify style to remove extra space | |
| 36 | - $style = preg_replace( '/\s+/', ' ', $style ); | |
| 52 | + | |
| 53 | + $this->styles[] = $style; | |
| 54 | + } | |
| 55 | + } | |
| 56 | + return $block_content; | |
| 57 | + } | |
| 37 | 58 | |
| 38 | - // register style | |
| 39 | - wp_register_style( $handle, false, [], GUTSLIDER_VERSION, 'all' ); // wp_register_style( $handle, $src, $deps, $ver, $media ); | |
| 40 | - wp_enqueue_style( $handle ); | |
| 41 | - wp_add_inline_style( $handle, $style ); | |
| 59 | + /** | |
| 60 | + * Generate and enqueue combined CSS | |
| 61 | + */ | |
| 62 | + public function generate_and_enqueue_combined_css() { | |
| 63 | + if (empty($this->styles)) { | |
| 64 | + return; | |
| 65 | + } | |
| 42 | 66 | |
| 43 | - } | |
| 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; | |
| 44 | 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(); | |
| 45 | 79 | } |
| 46 | - return $block_content; | |
| 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 | + | |
| 47 | 104 | } |
| 48 | 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 | + } | |
| 49 | 118 | } |
| 119 | +} | |
| 50 | 120 | |
| 51 | - } | |
| 52 | - | |
| 53 | - new GutSlider_Dynamic_Style(); // Initialize the class | |
| 121 | +new GutSlider_Dynamic_Style(); // Initialize the class | |