class-wp-style-engine-css-rule.php 3.7 KB

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