class-seedlet-custom-colors.php 12 KB

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