class-seedlet-custom-colors.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Seedlet Theme: Custom Colors Class
  4. *
  5. * This class is in charge of color customization via the Customizer.
  6. *
  7. * Each variable that needs to be updated is defined in the $seedlet_custom_color_variables array below.
  8. * A customizer setting is created for each color, and custom CSS-variables are enqueued in the front and back end.
  9. *
  10. * @package Seedlet
  11. * @since 1.0.0
  12. */
  13. class Seedlet_Custom_Colors {
  14. private $seedlet_custom_color_variables = array();
  15. function __construct() {
  16. /**
  17. * Define color variables
  18. */
  19. $seedlet_colors = array(
  20. array( '--global--color-background', '#FFFFFF', __( 'Background Color', 'seedlet' ) ),
  21. array( '--global--color-foreground', '#333333', __( 'Foreground Color', 'seedlet' ) ),
  22. array( '--global--color-primary', '#000000', __( 'Primary Color', 'seedlet' ) ),
  23. array( '--global--color-secondary', '#3C8067', __( 'Secondary Color', 'seedlet' ) ),
  24. array( '--global--color-tertiary', '#FAFBF6', __( 'Tertiary Color', 'seedlet' ) ),
  25. array( '--global--color-border', '#EFEFEF', __( 'Borders Color', 'seedlet' ) ),
  26. );
  27. $this->seedlet_custom_color_variables = apply_filters( 'seedlet_colors', $seedlet_colors );
  28. /**
  29. * Register Customizer actions
  30. */
  31. add_action( 'customize_register', array( $this, 'seedlet_customize_custom_colors_register' ) );
  32. /**
  33. * Enqueue color variables for customizer & frontend.
  34. */
  35. add_action( 'wp_enqueue_scripts', array( $this, 'seedlet_custom_color_variables' ) );
  36. /**
  37. * Enqueue color variables for editor.
  38. */
  39. add_action( 'enqueue_block_editor_assets', array( $this, 'seedlet_editor_custom_color_variables' ) );
  40. /**
  41. * Enqueue contrast checking.
  42. */
  43. add_action( 'customize_controls_enqueue_scripts', array( $this, 'on_customize_controls_enqueue_scripts' ) );
  44. }
  45. /**
  46. * Find the resulting colour by blending 2 colours
  47. * and setting an opacity level for the foreground colour.
  48. *
  49. * @author J de Silva
  50. * @link http://www.gidnetwork.com/b-135.html
  51. * @param string $foreground Hexadecimal colour value of the foreground colour.
  52. * @param integer $opacity Opacity percentage (of foreground colour). A number between 0 and 100.
  53. * @param string $background Optional. Hexadecimal colour value of the background colour. Default is: <code>FFFFFF</code> aka white.
  54. * @return string Hexadecimal colour value. <code>false</code> on errors.
  55. */
  56. function seedlet_color_blend_by_opacity( $foreground, $opacity, $background = null ) {
  57. static $colors_rgb = array(); // stores colour values already passed through the hexdec() functions below.
  58. if ( ! is_null( $foreground ) ) {
  59. $foreground = '000000'; // default primary.
  60. } else {
  61. $foreground = preg_replace( '/[^0-9a-f]/i', '', $foreground ); //str_replace( '#', '', $foreground );
  62. }
  63. if ( ! is_null( $background ) ) {
  64. $background = 'FFFFFF'; // default background.
  65. } else {
  66. $background = preg_replace( '/[^0-9a-f]/i', '', $background );
  67. }
  68. $pattern = '~^[a-f0-9]{6,6}$~i'; // accept only valid hexadecimal colour values.
  69. if ( ! @preg_match( $pattern, $foreground ) or ! @preg_match( $pattern, $background ) ) {
  70. echo $foreground;
  71. trigger_error( 'Invalid hexadecimal color value(s) found', E_USER_WARNING );
  72. return false;
  73. }
  74. $opacity = intval( $opacity ); // validate opacity data/number.
  75. if ( $opacity > 100 || $opacity < 0 ) {
  76. trigger_error( 'Opacity percentage error, valid numbers are between 0 - 100', E_USER_WARNING );
  77. return false;
  78. }
  79. if ( $opacity == 100 ) { // $transparency == 0
  80. return strtoupper( $foreground );
  81. }
  82. if ( $opacity == 0 ) { // $transparency == 100
  83. return strtoupper( $background );
  84. }
  85. // calculate $transparency value.
  86. $transparency = 100 - $opacity;
  87. if ( ! isset( $colors_rgb[ $foreground ] ) ) { // do this only ONCE per script, for each unique colour.
  88. $f = array(
  89. 'r' => hexdec( $foreground[0] . $foreground[1] ),
  90. 'g' => hexdec( $foreground[2] . $foreground[3] ),
  91. 'b' => hexdec( $foreground[4] . $foreground[5] ),
  92. );
  93. $colors_rgb[ $foreground ] = $f;
  94. } else { // if this function is used 100 times in a script, this block is run 99 times. Efficient.
  95. $f = $colors_rgb[ $foreground ];
  96. }
  97. if ( ! isset( $colors_rgb[ $background ] ) ) { // do this only ONCE per script, for each unique colour.
  98. $b = array(
  99. 'r' => hexdec( $background[0] . $background[1] ),
  100. 'g' => hexdec( $background[2] . $background[3] ),
  101. 'b' => hexdec( $background[4] . $background[5] ),
  102. );
  103. $colors_rgb[ $background ] = $b;
  104. } else { // if this FUNCTION is used 100 times in a SCRIPT, this block will run 99 times. Efficient.
  105. $b = $colors_rgb[ $background ];
  106. }
  107. $add = array(
  108. 'r' => ( $b['r'] - $f['r'] ) / 100,
  109. 'g' => ( $b['g'] - $f['g'] ) / 100,
  110. 'b' => ( $b['b'] - $f['b'] ) / 100,
  111. );
  112. $f['r'] += intval( $add['r'] * $transparency );
  113. $f['g'] += intval( $add['g'] * $transparency );
  114. $f['b'] += intval( $add['b'] * $transparency );
  115. return sprintf( '#%02X%02X%02X', $f['r'], $f['g'], $f['b'] );
  116. }
  117. /**
  118. * Add Theme Options for the Customizer.
  119. *
  120. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  121. */
  122. function seedlet_customize_custom_colors_register( $wp_customize ) {
  123. /**
  124. * Create color options panel.
  125. */
  126. $wp_customize->add_section(
  127. 'seedlet_options',
  128. array(
  129. 'capability' => 'edit_theme_options',
  130. 'title' => esc_html__( 'Colors', 'seedlet' ),
  131. )
  132. );
  133. /**
  134. * Create toggle between default and custom colors.
  135. */
  136. $wp_customize->add_setting(
  137. 'custom_colors_active',
  138. array(
  139. 'capability' => 'edit_theme_options',
  140. 'sanitize_callback' => array( $this, 'sanitize_select' ),
  141. 'transport' => 'refresh',
  142. 'default' => 'default',
  143. )
  144. );
  145. $wp_customize->add_control(
  146. 'custom_colors_active',
  147. array(
  148. 'type' => 'radio',
  149. 'section' => 'seedlet_options',
  150. 'label' => __( 'Colors', 'seedlet' ),
  151. 'choices' => array(
  152. 'default' => __( 'Theme Default', 'seedlet' ),
  153. 'custom' => __( 'Custom', 'seedlet' ),
  154. ),
  155. )
  156. );
  157. /**
  158. * Create customizer color controls.
  159. */
  160. foreach ( $this->seedlet_custom_color_variables as $variable ) {
  161. $wp_customize->add_setting(
  162. "seedlet_$variable[0]",
  163. array(
  164. 'default' => esc_html( $variable[1] ),
  165. 'sanitize_callback' => 'sanitize_hex_color',
  166. )
  167. );
  168. $wp_customize->add_control(
  169. new WP_Customize_Color_Control(
  170. $wp_customize,
  171. "seedlet_$variable[0]",
  172. array(
  173. 'section' => 'seedlet_options',
  174. 'label' => $variable[2],
  175. 'active_callback' => function() use ( $wp_customize ) {
  176. return ( 'custom' === $wp_customize->get_setting( 'custom_colors_active' )->value() );
  177. },
  178. )
  179. )
  180. );
  181. }
  182. }
  183. /**
  184. * Generate color variables.
  185. */
  186. function seedlet_generate_custom_color_variables( $context = null ) {
  187. if ( $context === 'editor' ) {
  188. $theme_css = ':root .editor-styles-wrapper {';
  189. } else {
  190. $theme_css = ':root {';
  191. }
  192. // Check to see if a custom background color has been set that is needed for our color calculation
  193. // If this check isn't present, the color calculation generates a warning that an invalid color has been supplied
  194. $theme_mod_bg_color = empty( get_theme_mod( 'seedlet_--global--color-background' ) ) ? '#FFFFFF' : get_theme_mod( 'seedlet_--global--color-background' );
  195. foreach ( $this->seedlet_custom_color_variables as $variable ) {
  196. if ( ! empty( get_theme_mod( "seedlet_$variable[0]" ) ) ) {
  197. $theme_mod_variable_name = $variable[0];
  198. $theme_mod_default_color = $variable[1];
  199. $theme_mod_custom_color = get_theme_mod( "seedlet_$variable[0]" );
  200. $opacity_integer = 70;
  201. $adjusted_color = $this->seedlet_color_blend_by_opacity( $theme_mod_custom_color, $opacity_integer, $theme_mod_bg_color );
  202. $theme_css .= $theme_mod_variable_name . ':' . $theme_mod_custom_color . ';';
  203. if ( $theme_mod_variable_name === '--global--color-primary' && $theme_mod_default_color !== $theme_mod_custom_color ) {
  204. $theme_css .= '--global--color-primary-hover: ' . $adjusted_color . ';';
  205. }
  206. if ( $theme_mod_variable_name === '--global--color-secondary' && $theme_mod_default_color !== $theme_mod_custom_color ) {
  207. $theme_css .= '--global--color-secondary-hover: ' . $adjusted_color . ';';
  208. }
  209. if ( $theme_mod_variable_name === '--global--color-foreground' && $theme_mod_default_color !== $theme_mod_custom_color ) {
  210. $theme_css .= '--global--color-foreground-low-contrast: ' . $adjusted_color . ';';
  211. }
  212. if ( $theme_mod_variable_name === '--global--color-background' && $theme_mod_default_color !== $theme_mod_custom_color ) {
  213. $theme_css .= '--global--color-background-high-contrast:' . $theme_mod_custom_color . ';';
  214. }
  215. }
  216. }
  217. $theme_css .= '}';
  218. // Text selection colors
  219. $selection_background = $this->seedlet_color_blend_by_opacity( get_theme_mod( 'seedlet_--global--color-primary' ), 5, $theme_mod_bg_color ) . ';';
  220. $theme_css .= '::selection { background-color: ' . $selection_background . '}';
  221. $theme_css .= '::-moz-selection { background-color: ' . $selection_background . '}';
  222. return $theme_css;
  223. }
  224. /**
  225. * Customizer & frontend custom color variables.
  226. */
  227. function seedlet_custom_color_variables() {
  228. wp_enqueue_style( 'seedlet-custom-color-overrides', get_template_directory_uri() . '/assets/css/custom-color-overrides.css', array(), wp_get_theme()->get( 'Version' ) );
  229. if ( 'default' !== get_theme_mod( 'custom_colors_active' ) ) {
  230. wp_add_inline_style( 'seedlet-custom-color-overrides', $this->seedlet_generate_custom_color_variables() );
  231. }
  232. }
  233. /**
  234. * Editor custom color variables.
  235. */
  236. function seedlet_editor_custom_color_variables() {
  237. wp_enqueue_style( 'seedlet-custom-color-overrides', get_template_directory_uri() . '/assets/css/custom-color-overrides.css', array(), wp_get_theme()->get( 'Version' ) );
  238. if ( 'default' !== get_theme_mod( 'custom_colors_active' ) ) {
  239. wp_add_inline_style( 'seedlet-custom-color-overrides', $this->seedlet_generate_custom_color_variables( 'editor' ) );
  240. }
  241. }
  242. /**
  243. * Customizer contrast validation warnings.
  244. *
  245. * @author Per Soderlind
  246. * @link https://github.com/soderlind/2016-customizer-demo
  247. */
  248. function on_customize_controls_enqueue_scripts() {
  249. $handle = 'seedlet-wcag-validate-customizer-color-contrast';
  250. $src = get_template_directory_uri() . '/assets/js/customizer-validate-wcag-color-contrast.js';
  251. $deps = array( 'customize-controls' );
  252. $exports = array(
  253. 'validate_color_contrast' => array(
  254. // key = current color control , values = array with color controls to check color contrast against
  255. 'seedlet_--global--color-primary' => array( 'seedlet_--global--color-background' ),
  256. 'seedlet_--global--color-secondary' => array( 'seedlet_--global--color-background' ),
  257. 'seedlet_--global--color-foreground' => array( 'seedlet_--global--color-background' ),
  258. 'seedlet_--global--color-background' => array( 'seedlet_--global--color-foreground' ),
  259. ),
  260. );
  261. wp_enqueue_script( $handle, $src, $deps );
  262. wp_script_add_data( $handle, 'data', sprintf( 'var seedletValidateWCAGColorContrastExports = %s;', wp_json_encode( $exports ) ) );
  263. // Custom color contrast validation text
  264. wp_localize_script( $handle, 'seedletValidateContrastText', array( esc_html__( 'This color combination may be hard for people to read. Try using a brighter background color and/or a darker foreground color.', 'seedlet' ) ) );
  265. }
  266. /**
  267. * Sanitize select.
  268. *
  269. * @param string $input The input from the setting.
  270. * @param object $setting The selected setting.
  271. *
  272. * @return string $input|$setting->default The input from the setting or the default setting.
  273. */
  274. public static function sanitize_select( $input, $setting ) {
  275. $input = sanitize_key( $input );
  276. $choices = $setting->manager->get_control( $setting->id )->choices;
  277. return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
  278. }
  279. }
  280. new Seedlet_Custom_Colors;