[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/style-engine/ -> class-wp-style-engine-css-rule.php (source)

   1  <?php
   2  /**
   3   * Style Engine: WP_Style_Engine_CSS_Rule class
   4   *
   5   * @package WordPress
   6   * @subpackage StyleEngine
   7   * @since 6.1.0
   8   */
   9  
  10  /**
  11   * Core class used for style engine CSS rules.
  12   *
  13   * Holds, sanitizes, processes, and prints CSS declarations for the style engine.
  14   *
  15   * @since 6.1.0
  16   */
  17  #[AllowDynamicProperties]
  18  class WP_Style_Engine_CSS_Rule {
  19  
  20      /**
  21       * The selector.
  22       *
  23       * @since 6.1.0
  24       * @var string
  25       */
  26      protected $selector;
  27  
  28      /**
  29       * The selector declarations.
  30       *
  31       * Contains a WP_Style_Engine_CSS_Declarations object.
  32       *
  33       * @since 6.1.0
  34       * @var WP_Style_Engine_CSS_Declarations
  35       */
  36      protected $declarations;
  37  
  38      /**
  39       * Constructor.
  40       *
  41       * @since 6.1.0
  42       *
  43       * @param string                                    $selector     Optional. The CSS selector. Default empty string.
  44       * @param string[]|WP_Style_Engine_CSS_Declarations $declarations Optional. An associative array of CSS definitions,
  45       *                                                                e.g. `array( "$property" => "$value", "$property" => "$value" )`,
  46       *                                                                or a WP_Style_Engine_CSS_Declarations object.
  47       *                                                                Default empty array.
  48       */
  49  	public function __construct( $selector = '', $declarations = array() ) {
  50          $this->set_selector( $selector );
  51          $this->add_declarations( $declarations );
  52      }
  53  
  54      /**
  55       * Sets the selector.
  56       *
  57       * @since 6.1.0
  58       *
  59       * @param string $selector The CSS selector.
  60       * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
  61       */
  62  	public function set_selector( $selector ) {
  63          $this->selector = $selector;
  64          return $this;
  65      }
  66  
  67      /**
  68       * Sets the declarations.
  69       *
  70       * @since 6.1.0
  71       *
  72       * @param string[]|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
  73       *                                                                or a WP_Style_Engine_CSS_Declarations object.
  74       * @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
  75       */
  76  	public function add_declarations( $declarations ) {
  77          $is_declarations_object = ! is_array( $declarations );
  78          $declarations_array     = $is_declarations_object ? $declarations->get_declarations() : $declarations;
  79  
  80          if ( null === $this->declarations ) {
  81              if ( $is_declarations_object ) {
  82                  $this->declarations = $declarations;
  83                  return $this;
  84              }
  85              $this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
  86          }
  87          $this->declarations->add_declarations( $declarations_array );
  88  
  89          return $this;
  90      }
  91  
  92      /**
  93       * Gets the declarations object.
  94       *
  95       * @since 6.1.0
  96       *
  97       * @return WP_Style_Engine_CSS_Declarations The declarations object.
  98       */
  99  	public function get_declarations() {
 100          return $this->declarations;
 101      }
 102  
 103      /**
 104       * Gets the full selector.
 105       *
 106       * @since 6.1.0
 107       *
 108       * @return string
 109       */
 110  	public function get_selector() {
 111          return $this->selector;
 112      }
 113  
 114      /**
 115       * Gets the CSS.
 116       *
 117       * @since 6.1.0
 118       *
 119       * @param bool $should_prettify Optional. Whether to add spacing, new lines and indents.
 120       *                              Default false.
 121       * @param int  $indent_count    Optional. The number of tab indents to apply to the rule.
 122       *                              Applies if `prettify` is `true`. Default 0.
 123       * @return string
 124       */
 125  	public function get_css( $should_prettify = false, $indent_count = 0 ) {
 126          $rule_indent         = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
 127          $declarations_indent = $should_prettify ? $indent_count + 1 : 0;
 128          $suffix              = $should_prettify ? "\n" : '';
 129          $spacer              = $should_prettify ? ' ' : '';
 130          $selector            = $should_prettify ? str_replace( ',', ",\n", $this->get_selector() ) : $this->get_selector();
 131          $css_declarations    = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );
 132  
 133          if ( empty( $css_declarations ) ) {
 134              return '';
 135          }
 136  
 137          return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
 138      }
 139  }


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref