PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.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 7.4.0 All 402 releases
gutenberg / build / scripts / style-engine / class-wp-style-engine-processor-gutenberg.php

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

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