PluginProbe
Gutenberg / 17.2.4
Gutenberg v17.2.4
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / style-engine / class-wp-style-engine-processor-gutenberg.php

class-wp-style-engine-processor-gutenberg.php in Gutenberg 17.2.4, at build/style-engine/class-wp-style-engine-processor-gutenberg.php

158 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP_Style_Engine_Processor_Gutenberg
4 *
5 * Compiles styles from stores or collection of CSS rules.
6 *
7 * @package Gutenberg
8 */
9
10 if ( class_exists( 'WP_Style_Engine_Processor_Gutenberg' ) ) {
11 return;
12 }
13
14 /**
15 * Compiles styles from stores or collection of CSS rules.
16 *
17 * @access private
18 */
19 class WP_Style_Engine_Processor_Gutenberg {
20
21 /**
22 * A collection of Style Engine Store objects.
23 *
24 * @var WP_Style_Engine_CSS_Rules_Store_Gutenberg[]
25 */
26 protected $stores = array();
27
28 /**
29 * The set of CSS rules that this processor will work on.
30 *
31 * @var WP_Style_Engine_CSS_Rule_Gutenberg[]
32 */
33 protected $css_rules = array();
34
35 /**
36 * Add a store to the processor.
37 *
38 * @param WP_Style_Engine_CSS_Rules_Store_Gutenberg $store The store to add.
39 *
40 * @return WP_Style_Engine_Processor_Gutenberg Returns the object to allow chaining methods.
41 */
42 public function add_store( $store ) {
43 if ( ! $store instanceof WP_Style_Engine_CSS_Rules_Store_Gutenberg ) {
44 _doing_it_wrong(
45 __METHOD__,
46 __( '$store must be an instance of WP_Style_Engine_CSS_Rules_Store_Gutenberg', 'default' ),
47 '6.1.0'
48 );
49 return $this;
50 }
51
52 $this->stores[ $store->get_name() ] = $store;
53
54 return $this;
55 }
56
57 /**
58 * Adds rules to be processed.
59 *
60 * @param WP_Style_Engine_CSS_Rule_Gutenberg|WP_Style_Engine_CSS_Rule_Gutenberg[] $css_rules A single, or an array of, WP_Style_Engine_CSS_Rule_Gutenberg objects from a store or otherwise.
61 *
62 * @return WP_Style_Engine_Processor_Gutenberg Returns the object to allow chaining methods.
63 */
64 public function add_rules( $css_rules ) {
65 if ( ! is_array( $css_rules ) ) {
66 $css_rules = array( $css_rules );
67 }
68
69 foreach ( $css_rules as $rule ) {
70 $selector = $rule->get_selector();
71 if ( isset( $this->css_rules[ $selector ] ) ) {
72 $this->css_rules[ $selector ]->add_declarations( $rule->get_declarations() );
73 continue;
74 }
75 $this->css_rules[ $rule->get_selector() ] = $rule;
76 }
77
78 return $this;
79 }
80
81 /**
82 * Get the CSS rules as a string.
83 *
84 * Since 6.4.0 Optimization is no longer the default.
85 *
86 * @param array $options {
87 * Optional. An array of options. Default empty array.
88 *
89 * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
90 * @type bool $prettify Whether to add new lines and indents to output. Default is to inherit the value of the global constant `SCRIPT_DEBUG`, if it is defined.
91 * }
92 *
93 * @return string The computed CSS.
94 */
95 public function get_css( $options = array() ) {
96 $defaults = array(
97 'optimize' => false,
98 'prettify' => SCRIPT_DEBUG,
99 );
100 $options = wp_parse_args( $options, $defaults );
101
102 // If we have stores, get the rules from them.
103 foreach ( $this->stores as $store ) {
104 $this->add_rules( $store->get_all_rules() );
105 }
106
107 // Combine CSS selectors that have identical declarations.
108 if ( true === $options['optimize'] ) {
109 $this->combine_rules_selectors();
110 }
111
112 // Build the CSS.
113 $css = '';
114 foreach ( $this->css_rules as $rule ) {
115 $css .= $rule->get_css( $options['prettify'] );
116 $css .= $options['prettify'] ? "\n" : '';
117 }
118 return $css;
119 }
120
121 /**
122 * Combines selectors from the rules store when they have the same styles.
123 *
124 * @return void
125 */
126 private function combine_rules_selectors() {
127 // Build an array of selectors along with the JSON-ified styles to make comparisons easier.
128 $selectors_json = array();
129 foreach ( $this->css_rules as $rule ) {
130 $declarations = $rule->get_declarations()->get_declarations();
131 ksort( $declarations );
132 $selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations );
133 }
134
135 // Combine selectors that have the same styles.
136 foreach ( $selectors_json as $selector => $json ) {
137 // Get selectors that use the same styles.
138 $duplicates = array_keys( $selectors_json, $json, true );
139 // Skip if there are no duplicates.
140 if ( 1 >= count( $duplicates ) ) {
141 continue;
142 }
143
144 $declarations = $this->css_rules[ $selector ]->get_declarations();
145
146 foreach ( $duplicates as $key ) {
147 // Unset the duplicates from the $selectors_json array to avoid looping through them as well.
148 unset( $selectors_json[ $key ] );
149 // Remove the rules from the rules collection.
150 unset( $this->css_rules[ $key ] );
151 }
152 // Create a new rule with the combined selectors.
153 $duplicate_selectors = implode( ',', $duplicates );
154 $this->css_rules[ $duplicate_selectors ] = new WP_Style_Engine_CSS_Rule_Gutenberg( $duplicate_selectors, $declarations );
155 }
156 }
157 }
158