wpcom-colors-css-variables.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /*
  4. * Color palette related utilities
  5. */
  6. require_once get_template_directory() . '/inc/color-utils.php';
  7. // Custom Colors:
  8. function varia_define_color_annotations( $colors ) {
  9. // Background Color
  10. if ( isset( $colors['background'] ) ) {
  11. // --wp--preset--color--background
  12. add_color_rule(
  13. 'bg',
  14. $colors['background'],
  15. array(
  16. // This placeholder is needed to make the color annotations work
  17. array( '.wp--preset--color--background', 'background-color' ),
  18. ),
  19. __( 'Background Color' )
  20. );
  21. }
  22. // Foreground Color
  23. if ( isset( $colors['foreground'] ) ) {
  24. // --wp--preset--color--foreground
  25. add_color_rule(
  26. 'txt',
  27. $colors['foreground'],
  28. array(
  29. // This placeholder is needed to make the color annotations work
  30. array( '.wp--preset--color--foreground', 'color' ),
  31. ),
  32. __( 'Foreground Color' )
  33. );
  34. }
  35. // Primary Color
  36. if ( isset( $colors['primary'] ) ) {
  37. // --wp--preset--color--primary
  38. add_color_rule(
  39. 'link',
  40. $colors['primary'],
  41. array(
  42. // This placeholder is needed to make the color annotations work
  43. array( '.wp--preset--color--primary', 'color' ),
  44. ),
  45. __( 'Primary Color' )
  46. );
  47. }
  48. // Secondary Color
  49. if ( isset( $colors['secondary'] ) ) {
  50. // --wp--preset--color--secondary
  51. add_color_rule(
  52. 'fg1',
  53. $colors['secondary'],
  54. array(
  55. // Text-color
  56. array( '.wp--preset--color--secondary', 'color' ),
  57. ),
  58. __( 'Secondary Color' )
  59. );
  60. }
  61. // Tertiary Color
  62. if ( isset( $colors['tertiary'] ) ) {
  63. // --wp--preset--color--tertiary
  64. add_color_rule(
  65. 'fg2',
  66. $colors['tertiary'],
  67. array(
  68. // Text-color
  69. array( '.wp--preset--color--tertiary', 'color' ),
  70. ),
  71. __( 'Tertiary Color' )
  72. );
  73. }
  74. }
  75. /**
  76. * Custom CSS.
  77. * The plugin takes the body of this function and applies it in a style tag in the document head.
  78. */
  79. function varia_custom_colors_extra_css() {
  80. $colors_array = get_theme_mod( 'colors_manager' );
  81. if ( isset( $colors_array['colors']['bg'] ) ) {
  82. $background = $colors_array['colors']['bg'];
  83. $background_low_contrast = change_color_luminescence( $background, -10 );
  84. $background_high_contrast = change_color_luminescence( $background, 10 );
  85. }
  86. if ( isset( $colors_array['colors']['txt'] ) ) {
  87. $foreground = $colors_array['colors']['txt'];
  88. $foreground_low_contrast = change_color_luminescence( $foreground, 10 );
  89. $foreground_high_contrast = change_color_luminescence( $foreground, -10 );
  90. }
  91. if ( isset( $colors_array['colors']['link'] ) ) {
  92. $primary = $colors_array['colors']['link'];
  93. $primary_hover = change_color_luminescence( $primary, 10 );
  94. $primary_dark = change_color_luminescence( $primary, -10 );
  95. }
  96. if ( isset( $colors_array['colors']['fg1'] ) ) {
  97. $secondary = $colors_array['colors']['fg1'];
  98. $secondary_hover = change_color_luminescence( $secondary, 10 );
  99. }
  100. if ( isset( $colors_array['colors']['fg2'] ) ) {
  101. $border = $colors_array['colors']['fg2'];
  102. $border_low_contrast = change_color_luminescence( $border, 10 );
  103. $border_high_contrast = change_color_luminescence( $border, -10 );
  104. }
  105. ?>
  106. :root,
  107. #editor .editor-styles-wrapper {
  108. <?php if ( isset( $colors_array['colors']['bg'] ) ) { ?>
  109. --wp--preset--color--background: <?php echo $background; ?>;
  110. --wp--preset--color--background-low-contrast: <?php echo $background_low_contrast; ?>;
  111. --wp--preset--color--background-high-contrast: <?php echo $background_high_contrast; ?>;
  112. <?php
  113. }
  114. if ( isset( $colors_array['colors']['txt'] ) ) {
  115. ?>
  116. --wp--preset--color--foreground: <?php echo $foreground; ?>;
  117. --wp--preset--color--foreground-low-contrast: <?php echo $foreground_low_contrast; ?>;
  118. --wp--preset--color--foreground-high-contrast: <?php echo $foreground_high_contrast; ?>;
  119. <?php
  120. }
  121. if ( isset( $colors_array['colors']['link'] ) ) {
  122. ?>
  123. --wp--preset--color--primary: <?php echo $primary; ?>;
  124. --wp--preset--color--primary-hover: <?php echo $primary_hover; ?>;
  125. --wp--preset--color--primary-dark: <?php echo $primary_dark; ?>;
  126. <?php
  127. }
  128. if ( isset( $colors_array['colors']['fg1'] ) ) {
  129. ?>
  130. --wp--preset--color--secondary: <?php echo $secondary; ?>;
  131. --wp--preset--color--secondary-hover: <?php echo $secondary_hover; ?>;
  132. <?php
  133. }
  134. if ( isset( $colors_array['colors']['fg2'] ) ) {
  135. ?>
  136. --wp--preset--color--border: <?php echo $border; ?>;
  137. --wp--preset--color--border-low-contrast: <?php echo $border_low_contrast; ?>;
  138. --wp--preset--color--border-high-contrast: <?php echo $border_high_contrast; ?>;
  139. <?php } ?>
  140. }
  141. <?php
  142. }
  143. add_theme_support( 'custom_colors_extra_css', 'varia_custom_colors_extra_css' );
  144. /**
  145. * Featured Palettes
  146. */
  147. $has_tertiary_color = false;
  148. if ( function_exists( 'varia_default_colors' ) ) {
  149. $default_colors = varia_default_colors();
  150. varia_define_color_annotations( $default_colors );
  151. if ( $default_colors['tertiary'] ) {
  152. $has_tertiary_color = true;
  153. }
  154. }
  155. $light = array(
  156. '#FFFFFF',
  157. '#1D1E1E',
  158. '#C8133E',
  159. '#4E2F4B',
  160. );
  161. $medium = array(
  162. '#EEF4F7',
  163. '#242527',
  164. '#35845D',
  165. '#233252',
  166. );
  167. $dark = array(
  168. '#1F2527',
  169. '#FFFFFF',
  170. '#9FD3E8',
  171. '#FBE6AA',
  172. );
  173. if ( $has_tertiary_color ) {
  174. $light[] = '#F9F9F9';
  175. $medium[] = '#F9F9F9';
  176. $dark[] = '#364043';
  177. }
  178. add_color_palette(
  179. $light,
  180. /* translators: This is the name for a color scheme */
  181. 'Light'
  182. );
  183. add_color_palette(
  184. $medium,
  185. /* translators: This is the name for a color scheme */
  186. 'Medium'
  187. );
  188. add_color_palette(
  189. $dark,
  190. /* translators: This is the name for a color scheme */
  191. 'Dark'
  192. );