assets
2 months ago
field-handlers
2 months ago
migrator
2 months ago
block.php
2 months ago
editor.php
2 months ago
path-url-trait.php
2 months ago
proxy.php
2 months ago
registry.php
2 months ago
style-cache.php
2 months ago
style-engine.php
2 months ago
style-inserter.php
2 months ago
style-manager.php
2 months ago
webpack.config.js
2 months ago
style-cache.php
100 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CSS caching class |
| 4 | */ |
| 5 | namespace Crocoblock\Blocks_Style; |
| 6 | |
| 7 | class Style_Cache { |
| 8 | |
| 9 | /** |
| 10 | * Class instance |
| 11 | * |
| 12 | * @var Style_Cache |
| 13 | */ |
| 14 | private static $instance = null; |
| 15 | |
| 16 | /** |
| 17 | * Cached styles by classes |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $generated_css = array(); |
| 22 | |
| 23 | /** |
| 24 | * List of already printed CSS classes |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $printed_css = array(); |
| 29 | |
| 30 | /** |
| 31 | * Check if the class is already printed |
| 32 | * |
| 33 | * @param string $class_name |
| 34 | * @return boolean |
| 35 | */ |
| 36 | public function is_printed( $class_name ) { |
| 37 | |
| 38 | if ( ! is_string( $class_name ) || empty( $class_name ) ) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | return in_array( $class_name, $this->printed_css, true ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get cached or generated styles. |
| 47 | * |
| 48 | * @param Style_Engine $style_engine |
| 49 | * @return void |
| 50 | */ |
| 51 | public function get_cached( $style_engine ) { |
| 52 | |
| 53 | if ( ! $style_engine instanceof Style_Engine ) { |
| 54 | return array(); |
| 55 | } |
| 56 | |
| 57 | $class_name = $style_engine->get_class_name(); |
| 58 | |
| 59 | if ( isset( $this->generated_css[ $class_name ] ) ) { |
| 60 | return $this->generated_css[ $class_name ]; |
| 61 | } |
| 62 | |
| 63 | $styles_data = $style_engine->get_styles(); |
| 64 | |
| 65 | if ( ! empty( $styles_data ) ) { |
| 66 | $this->generated_css[ $class_name ] = $styles_data; |
| 67 | } |
| 68 | |
| 69 | return $styles_data; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add a class to the printed CSS classes list |
| 74 | * |
| 75 | * @param string $class_name |
| 76 | */ |
| 77 | public function add_printed( $class_name ) { |
| 78 | |
| 79 | if ( ! is_string( $class_name ) || empty( $class_name ) ) { |
| 80 | return; |
| 81 | } |
| 82 | if ( ! in_array( $class_name, $this->printed_css, true ) ) { |
| 83 | $this->printed_css[] = $class_name; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the instance of the Style_Cache class |
| 89 | * |
| 90 | * @return Style_Cache |
| 91 | */ |
| 92 | public static function get_instance() { |
| 93 | |
| 94 | if ( null === self::$instance ) { |
| 95 | self::$instance = new self(); |
| 96 | } |
| 97 | |
| 98 | return self::$instance; |
| 99 | } |
| 100 | } |