| <?php |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| #[AllowDynamicProperties] |
| class WP_Style_Engine_Processor { |
|
|
| |
| |
| |
| |
| |
| |
| protected $stores = array(); |
|
|
| |
| |
| |
| |
| |
| |
| protected $css_rules = array(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function add_store( $store ) { |
| if ( ! $store instanceof WP_Style_Engine_CSS_Rules_Store ) { |
| _doing_it_wrong( |
| __METHOD__, |
| __( '$store must be an instance of WP_Style_Engine_CSS_Rules_Store' ), |
| '6.1.0' |
| ); |
| return $this; |
| } |
|
|
| $this->stores[ $store->get_name() ] = $store; |
|
|
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function add_rules( $css_rules ) { |
| if ( ! is_array( $css_rules ) ) { |
| $css_rules = array( $css_rules ); |
| } |
|
|
| foreach ( $css_rules as $rule ) { |
| $selector = $rule->get_selector(); |
| $rules_group = $rule->get_rules_group(); |
|
|
| |
| |
| |
| |
| |
| if ( ! empty( $rules_group ) ) { |
| if ( isset( $this->css_rules[ "$rules_group $selector" ] ) ) { |
| $this->css_rules[ "$rules_group $selector" ]->add_declarations( $rule->get_declarations() ); |
| continue; |
| } |
| $this->css_rules[ "$rules_group $selector" ] = $rule; |
| continue; |
| } |
|
|
| |
| if ( isset( $this->css_rules[ $selector ] ) ) { |
| $this->css_rules[ $selector ]->add_declarations( $rule->get_declarations() ); |
| continue; |
| } |
| $this->css_rules[ $rule->get_selector() ] = $rule; |
| } |
|
|
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function get_css( $options = array() ) { |
| $defaults = array( |
| 'optimize' => false, |
| 'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG, |
| ); |
| $options = wp_parse_args( $options, $defaults ); |
|
|
| |
| foreach ( $this->stores as $store ) { |
| $this->add_rules( $store->get_all_rules() ); |
| } |
|
|
| |
| if ( true === $options['optimize'] ) { |
| $this->combine_rules_selectors(); |
| } |
|
|
| |
| $css = ''; |
| foreach ( $this->css_rules as $rule ) { |
| |
| $css .= $rule->get_css( $options['prettify'] ); |
| $css .= $options['prettify'] ? "\n" : ''; |
| } |
| return $css; |
| } |
|
|
| |
| |
| |
| |
| |
| private function combine_rules_selectors() { |
| |
| $selectors_json = array(); |
| foreach ( $this->css_rules as $rule ) { |
| $declarations = $rule->get_declarations()->get_declarations(); |
| ksort( $declarations ); |
| $selectors_json[ $rule->get_selector() ] = wp_json_encode( $declarations ); |
| } |
|
|
| |
| foreach ( $selectors_json as $selector => $json ) { |
| |
| $duplicates = array_keys( $selectors_json, $json, true ); |
| |
| if ( 1 >= count( $duplicates ) ) { |
| continue; |
| } |
|
|
| $declarations = $this->css_rules[ $selector ]->get_declarations(); |
|
|
| foreach ( $duplicates as $key ) { |
| |
| unset( $selectors_json[ $key ] ); |
| |
| unset( $this->css_rules[ $key ] ); |
| } |
| |
| $duplicate_selectors = implode( ',', $duplicates ); |
| $this->css_rules[ $duplicate_selectors ] = new WP_Style_Engine_CSS_Rule( $duplicate_selectors, $declarations ); |
| } |
| } |
| } |
|
|