PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.10.0
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.10.0
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
slider-blocks / includes / classes / class-generate-style.php

class-generate-style.php in GutSlider – All in One Slider and Carousel Blocks for Gutenberg 2.10.0, at includes/classes/class-generate-style.php

121 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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