class-kirki-field-typography.php (7799B)
1 <?php 2 /** 3 * Override field methods 4 * 5 * @package Kirki 6 * @subpackage Controls 7 * @copyright Copyright (c) 2016, Aristeides Stathopoulos 8 * @license http://opensource.org/licenses/https://opensource.org/licenses/MIT 9 * @since 2.2.7 10 */ 11 12 if ( ! class_exists('Kirki_Field_Typography')) { 13 14 /** 15 * Field overrides. 16 */ 17 class Kirki_Field_Typography extends Kirki_Field 18 { 19 20 /** 21 * Sets the control type. 22 * 23 * @access protected 24 */ 25 protected function set_type() 26 { 27 28 $this->type = 'kirki-typography'; 29 30 } 31 32 /** 33 * Sets the $sanitize_callback 34 * 35 * @access protected 36 */ 37 protected function set_sanitize_callback() 38 { 39 40 // If a custom sanitize_callback has been defined, 41 // then we don't need to proceed any further. 42 if ( ! empty($this->sanitize_callback)) { 43 return; 44 } 45 $this->sanitize_callback = array($this, 'sanitize'); 46 47 } 48 49 /** 50 * Sets the $js_vars 51 * 52 * @access protected 53 */ 54 protected function set_js_vars() 55 { 56 57 if ( ! is_array($this->js_vars)) { 58 $this->js_vars = array(); 59 } 60 61 // Check if transport is set to auto. 62 // If not, then skip the auto-calculations and exit early. 63 if ('auto' !== $this->transport) { 64 return; 65 } 66 67 // Set transport to refresh initially. 68 // Serves as a fallback in case we failt to auto-calculate js_vars. 69 $this->transport = 'refresh'; 70 71 $js_vars = array(); 72 73 // Try to auto-generate js_vars. 74 // First we need to check if js_vars are empty, and that output is not empty. 75 if ( ! empty($this->output)) { 76 77 // Start going through each item in the $output array. 78 foreach ($this->output as $output) { 79 $output['function'] = 'css'; 80 81 // If 'element' or 'property' are not defined, skip this. 82 if ( ! isset($output['element'])) { 83 continue; 84 } 85 if (is_array($output['element'])) { 86 $output['element'] = implode(',', $output['element']); 87 } 88 if (false !== strpos($output['element'], ':')) { 89 $output['function'] = 'style'; 90 } 91 92 // If we got this far, it's safe to add this. 93 $js_vars[] = $output; 94 } 95 96 // Did we manage to get all the items from 'output'? 97 // If not, then we're missing something so don't add this. 98 if (count($js_vars) !== count($this->output)) { 99 return; 100 } 101 $this->js_vars = $js_vars; 102 $this->transport = 'postMessage'; 103 104 } 105 106 } 107 108 /** 109 * Sanitizes typography controls 110 * 111 * @since 2.2.0 112 * 113 * @param array $value The value. 114 * 115 * @return array 116 */ 117 public static function sanitize($value) 118 { 119 120 if ( ! is_array($value)) { 121 return array(); 122 } 123 124 // Escape the font-family. 125 if (isset($value['font-family'])) { 126 $value['font-family'] = esc_attr($value['font-family']); 127 } 128 129 // Make sure we're using a valid variant. 130 // We're adding checks for font-weight as well for backwards-compatibility 131 // Versions 2.0 - 2.2 were using an integer font-weight. 132 if (isset($value['variant']) || isset($value['font-weight'])) { 133 if (isset($value['font-weight']) && ! empty($value['font-weight'])) { 134 if ( ! isset($value['variant']) || empty($value['variant'])) { 135 $value['variant'] = $value['font-weight']; 136 } 137 unset($value['font-weight']); 138 } 139 $valid_variants = Kirki_Fonts::get_all_variants(); 140 if ( ! array_key_exists($value['variant'], $valid_variants)) { 141 $value['variant'] = 'regular'; 142 } 143 } 144 145 // Make sure the saved value is "subsets" (plural) and not "subset". 146 // This is for compatibility with older versions. 147 if (isset($value['subset'])) { 148 if ( ! empty($value['subset'])) { 149 if ( ! isset($value['subsets']) || empty($value['subset'])) { 150 $value['subsets'] = $value['subset']; 151 } 152 } 153 unset($value['subset']); 154 } 155 156 // Make sure we're using a valid subset. 157 if (isset($value['subsets'])) { 158 $valid_subsets = Kirki_Fonts::get_google_font_subsets(); 159 $subsets_ok = array(); 160 if (is_array($value['subsets'])) { 161 foreach ($value['subsets'] as $subset) { 162 if (array_key_exists($subset, $valid_subsets)) { 163 $subsets_ok[] = $subset; 164 } 165 } 166 $value['subsets'] = $subsets_ok; 167 } 168 } 169 170 // Sanitize the font-size. 171 if (isset($value['font-size']) && ! empty($value['font-size'])) { 172 $value['font-size'] = Kirki_Sanitize_Values::css_dimension($value['font-size']); 173 if (is_numeric($value['font-size'])) { 174 $value['font-size'] .= 'px'; 175 } 176 } 177 178 if (isset($value['mobile-font-size']) && ! empty($value['mobile-font-size'])) { 179 $value['mobile-font-size'] = Kirki_Sanitize_Values::css_dimension($value['mobile-font-size']); 180 if (is_numeric($value['mobile-font-size'])) { 181 $value['mobile-font-size'] .= 'px'; 182 } 183 } 184 185 // Sanitize the line-height. 186 if (isset($value['line-height']) && ! empty($value['line-height'])) { 187 $value['line-height'] = Kirki_Sanitize_Values::css_dimension($value['line-height']); 188 } 189 190 // Sanitize the letter-spacing. 191 if (isset($value['letter-spacing']) && ! empty($value['letter-spacing'])) { 192 $value['letter-spacing'] = Kirki_Sanitize_Values::css_dimension($value['letter-spacing']); 193 if (is_numeric($value['letter-spacing'])) { 194 $value['letter-spacing'] .= 'px'; 195 } 196 } 197 198 // Sanitize the text-align. 199 if (isset($value['text-align']) && ! empty($value['text-align'])) { 200 if ( ! in_array($value['text-align'], array('inherit', 'left', 'center', 'right', 'justify'))) { 201 $value['text-align'] = 'inherit'; 202 } 203 } 204 205 // Sanitize the text-transform. 206 if (isset($value['text-transform']) && ! empty($value['text-transform'])) { 207 if ( ! in_array($value['text-transform'], array('none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit'))) { 208 $value['text-transform'] = 'none'; 209 } 210 } 211 212 // Sanitize the color. 213 if (isset($value['color']) && ! empty($value['color'])) { 214 // $color = ariColor::newColor( $value['color'] ); 215 $value['color'] = Kirki_Sanitize_Values::color($value['color']); 216 } 217 218 return $value; 219 220 } 221 } 222 }