Ei kuvausta

custom-css.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Twenty Twenty Custom CSS
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Twenty
  7. * @since Twenty Twenty 1.0
  8. */
  9. if ( ! function_exists( 'twentytwenty_generate_css' ) ) {
  10. /**
  11. * Generate CSS.
  12. *
  13. * @since Twenty Twenty 1.0
  14. *
  15. * @param string $selector The CSS selector.
  16. * @param string $style The CSS style.
  17. * @param string $value The CSS value.
  18. * @param string $prefix The CSS prefix.
  19. * @param string $suffix The CSS suffix.
  20. * @param bool $echo Echo the styles.
  21. */
  22. function twentytwenty_generate_css( $selector, $style, $value, $prefix = '', $suffix = '', $echo = true ) {
  23. $return = '';
  24. /*
  25. * Bail early if we have no $selector elements or properties and $value.
  26. */
  27. if ( ! $value || ! $selector ) {
  28. return;
  29. }
  30. $return = sprintf( '%s { %s: %s; }', $selector, $style, $prefix . $value . $suffix );
  31. if ( $echo ) {
  32. echo $return; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We need to double check this, but for now, we want to pass PHPCS ;)
  33. }
  34. return $return;
  35. }
  36. }
  37. if ( ! function_exists( 'twentytwenty_get_customizer_css' ) ) {
  38. /**
  39. * Get CSS Built from Customizer Options.
  40. * Build CSS reflecting colors, fonts and other options set in the Customizer, and return them for output.
  41. *
  42. * @since Twenty Twenty 1.0
  43. *
  44. * @param string $type Whether to return CSS for the "front-end", "block-editor", or "classic-editor".
  45. */
  46. function twentytwenty_get_customizer_css( $type = 'front-end' ) {
  47. // Get variables.
  48. $body = sanitize_hex_color( twentytwenty_get_color_for_area( 'content', 'text' ) );
  49. $body_default = '#000000';
  50. $secondary = sanitize_hex_color( twentytwenty_get_color_for_area( 'content', 'secondary' ) );
  51. $secondary_default = '#6d6d6d';
  52. $borders = sanitize_hex_color( twentytwenty_get_color_for_area( 'content', 'borders' ) );
  53. $borders_default = '#dcd7ca';
  54. $accent = sanitize_hex_color( twentytwenty_get_color_for_area( 'content', 'accent' ) );
  55. $accent_default = '#cd2653';
  56. // Header.
  57. $header_footer_background = sanitize_hex_color( twentytwenty_get_color_for_area( 'header-footer', 'background' ) );
  58. $header_footer_background_default = '#ffffff';
  59. // Cover.
  60. $cover = sanitize_hex_color( get_theme_mod( 'cover_template_overlay_text_color' ) );
  61. $cover_default = '#ffffff';
  62. // Background.
  63. $background = sanitize_hex_color_no_hash( get_theme_mod( 'background_color' ) );
  64. $background_default = 'f5efe0';
  65. ob_start();
  66. /*
  67. * Note – Styles are applied in this order:
  68. * 1. Element specific
  69. * 2. Helper classes
  70. *
  71. * This enables all helper classes to overwrite base element styles,
  72. * meaning that any color classes applied in the block editor will
  73. * have a higher priority than the base element styles.
  74. */
  75. // Front-End Styles.
  76. if ( 'front-end' === $type ) {
  77. // Auto-calculated colors.
  78. $elements_definitions = twentytwenty_get_elements_array();
  79. foreach ( $elements_definitions as $context => $props ) {
  80. foreach ( $props as $key => $definitions ) {
  81. foreach ( $definitions as $property => $elements ) {
  82. /*
  83. * If we don't have an elements array or it is empty
  84. * then skip this iteration early;
  85. */
  86. if ( ! is_array( $elements ) || empty( $elements ) ) {
  87. continue;
  88. }
  89. $val = twentytwenty_get_color_for_area( $context, $key );
  90. if ( $val ) {
  91. twentytwenty_generate_css( implode( ',', $elements ), $property, $val );
  92. }
  93. }
  94. }
  95. }
  96. if ( $cover && $cover !== $cover_default ) {
  97. twentytwenty_generate_css( '.overlay-header .header-inner', 'color', $cover );
  98. twentytwenty_generate_css( '.cover-header .entry-header *', 'color', $cover );
  99. }
  100. // Block Editor Styles.
  101. } elseif ( 'block-editor' === $type ) {
  102. // Colors.
  103. // Accent color.
  104. if ( $accent && $accent !== $accent_default ) {
  105. twentytwenty_generate_css( ':root .has-accent-color, .editor-styles-wrapper a, .editor-styles-wrapper .has-drop-cap:not(:focus)::first-letter, .editor-styles-wrapper .wp-block-button.is-style-outline .wp-block-button__link, .editor-styles-wrapper .wp-block-pullquote::before, .editor-styles-wrapper .wp-block-file .wp-block-file__textlink', 'color', $accent );
  106. twentytwenty_generate_css( '.editor-styles-wrapper .wp-block-quote', 'border-color', $accent, '' );
  107. twentytwenty_generate_css( '.has-accent-background-color, .editor-styles-wrapper .wp-block-button__link, .editor-styles-wrapper .wp-block-file__button', 'background-color', $accent );
  108. }
  109. // Background color.
  110. if ( $background && $background !== $background_default ) {
  111. twentytwenty_generate_css( '.editor-styles-wrapper', 'background-color', '#' . $background );
  112. twentytwenty_generate_css( '.has-background.has-primary-background-color:not(.has-text-color),.has-background.has-primary-background-color *:not(.has-text-color),.has-background.has-accent-background-color:not(.has-text-color),.has-background.has-accent-background-color *:not(.has-text-color)', 'color', '#' . $background );
  113. }
  114. // Borders color.
  115. if ( $borders && $borders !== $borders_default ) {
  116. twentytwenty_generate_css( '.editor-styles-wrapper .wp-block-code, .editor-styles-wrapper pre, .editor-styles-wrapper .wp-block-preformatted pre, .editor-styles-wrapper .wp-block-verse pre, .editor-styles-wrapper fieldset, .editor-styles-wrapper .wp-block-table, .editor-styles-wrapper .wp-block-table *, .editor-styles-wrapper .wp-block-table.is-style-stripes, .editor-styles-wrapper .wp-block-latest-posts.is-grid li', 'border-color', $borders );
  117. twentytwenty_generate_css( '.editor-styles-wrapper .wp-block-table caption, .editor-styles-wrapper .wp-block-table.is-style-stripes tbody tr:nth-child(odd)', 'background-color', $borders );
  118. }
  119. // Text color.
  120. if ( $body && $body !== $body_default ) {
  121. twentytwenty_generate_css( 'body .editor-styles-wrapper, .editor-post-title__block .editor-post-title__input, .editor-post-title__block .editor-post-title__input:focus', 'color', $body );
  122. }
  123. // Secondary color.
  124. if ( $secondary && $secondary !== $secondary_default ) {
  125. twentytwenty_generate_css( '.editor-styles-wrapper figcaption, .editor-styles-wrapper cite, .editor-styles-wrapper .wp-block-quote__citation, .editor-styles-wrapper .wp-block-quote cite, .editor-styles-wrapper .wp-block-quote footer, .editor-styles-wrapper .wp-block-pullquote__citation, .editor-styles-wrapper .wp-block-pullquote cite, .editor-styles-wrapper .wp-block-pullquote footer, .editor-styles-wrapper ul.wp-block-archives li, .editor-styles-wrapper ul.wp-block-categories li, .editor-styles-wrapper ul.wp-block-latest-posts li, .editor-styles-wrapper ul.wp-block-categories__list li, .editor-styles-wrapper .wp-block-latest-comments time, .editor-styles-wrapper .wp-block-latest-posts time', 'color', $secondary );
  126. }
  127. // Header Footer Background Color.
  128. if ( $header_footer_background && $header_footer_background !== $header_footer_background_default ) {
  129. twentytwenty_generate_css( '.editor-styles-wrapper .wp-block-pullquote::before', 'background-color', $header_footer_background );
  130. }
  131. } elseif ( 'classic-editor' === $type ) {
  132. // Colors.
  133. // Accent color.
  134. if ( $accent && $accent !== $accent_default ) {
  135. twentytwenty_generate_css( 'body#tinymce.wp-editor.content a, body#tinymce.wp-editor.content a:focus, body#tinymce.wp-editor.content a:hover', 'color', $accent );
  136. twentytwenty_generate_css( 'body#tinymce.wp-editor.content blockquote, body#tinymce.wp-editor.content .wp-block-quote', 'border-color', $accent, '', ' !important' );
  137. twentytwenty_generate_css( 'body#tinymce.wp-editor.content button, body#tinymce.wp-editor.content .faux-button, body#tinymce.wp-editor.content .wp-block-button__link, body#tinymce.wp-editor.content .wp-block-file__button, body#tinymce.wp-editor.content input[type=\'button\'], body#tinymce.wp-editor.content input[type=\'reset\'], body#tinymce.wp-editor.content input[type=\'submit\']', 'background-color', $accent );
  138. }
  139. // Background color.
  140. if ( $background && $background !== $background_default ) {
  141. twentytwenty_generate_css( 'body#tinymce.wp-editor.content', 'background-color', '#' . $background );
  142. }
  143. // Text color.
  144. if ( $body && $body !== $body_default ) {
  145. twentytwenty_generate_css( 'body#tinymce.wp-editor.content', 'color', $body );
  146. }
  147. // Secondary color.
  148. if ( $secondary && $secondary !== $secondary_default ) {
  149. twentytwenty_generate_css( 'body#tinymce.wp-editor.content hr:not(.is-style-dots), body#tinymce.wp-editor.content cite, body#tinymce.wp-editor.content figcaption, body#tinymce.wp-editor.content .wp-caption-text, body#tinymce.wp-editor.content .wp-caption-dd, body#tinymce.wp-editor.content .gallery-caption', 'color', $secondary );
  150. }
  151. // Borders color.
  152. if ( $borders && $borders !== $borders_default ) {
  153. twentytwenty_generate_css( 'body#tinymce.wp-editor.content pre, body#tinymce.wp-editor.content hr, body#tinymce.wp-editor.content fieldset,body#tinymce.wp-editor.content input, body#tinymce.wp-editor.content textarea', 'border-color', $borders );
  154. }
  155. }
  156. // Return the results.
  157. return ob_get_clean();
  158. }
  159. }