| 1 |
<?php |
| 2 |
|
| 3 |
class Customify_Font_Selector extends PixCustomifyPlugin { |
| 4 |
|
| 5 |
/** |
| 6 |
* Instance of this class. |
| 7 |
* @since 1.0.0 |
| 8 |
* @var object |
| 9 |
*/ |
| 10 |
protected static $instance = null; |
| 11 |
protected $parent = null; |
| 12 |
protected static $typo_settings = null; |
| 13 |
protected static $options_list = null; |
| 14 |
protected static $theme_fonts = null; |
| 15 |
|
| 16 |
function __construct( $parent ) { |
| 17 |
add_action( 'customize_preview_init', array( $this, 'enqueue_admin_customizer_preview_assets' ), 10 ); |
| 18 |
$load_location = PixCustomifyPlugin::get_plugin_option( 'style_resources_location', 'wp_head' ); |
| 19 |
add_action( $load_location, array( $this, 'output_font_dynamic_style' ), 999999999 ); |
| 20 |
|
| 21 |
$this->parent = $parent; |
| 22 |
self::$theme_fonts = apply_filters( 'customify_theme_fonts', array() ); |
| 23 |
|
| 24 |
add_action( 'customify_font_family_before_options', array( $this, 'add_customify_theme_fonts' ), 11, 2 ); |
| 25 |
} |
| 26 |
|
| 27 |
function add_customify_theme_fonts( $active_font_family, $val ) { |
| 28 |
//first get all the published custom fonts |
| 29 |
if ( empty( self::$theme_fonts ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
echo '<optgroup label="' . esc_html__( 'Theme Fonts', 'customify' ) . '">'; |
| 34 |
foreach ( self::$theme_fonts as $font ) { |
| 35 |
if ( ! empty( $font ) ) { |
| 36 |
//display the select option's HTML |
| 37 |
Pix_Customize_Font_Control::output_font_option( $font['family'], $active_font_family, $font, 'theme_font' ); |
| 38 |
} |
| 39 |
} |
| 40 |
echo "</optgroup>"; |
| 41 |
} |
| 42 |
|
| 43 |
function output_font_dynamic_style() { |
| 44 |
|
| 45 |
self::$options_list = $this->get_options(); |
| 46 |
|
| 47 |
self::get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings ); |
| 48 |
|
| 49 |
if ( empty( self::$typo_settings ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$google_families = $local_families = ''; |
| 54 |
|
| 55 |
$args = array( |
| 56 |
'google_families' => '', |
| 57 |
'local_families' => '', |
| 58 |
'local_srcs' => '', |
| 59 |
); |
| 60 |
|
| 61 |
foreach ( self::$typo_settings as $id => $font ) { |
| 62 |
if ( isset ( $font['value'] ) ) { |
| 63 |
|
| 64 |
$load_all_weights = false; |
| 65 |
if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| 66 |
$load_all_weights = true; |
| 67 |
} |
| 68 |
$value = json_decode( wp_unslash( PixCustomifyPlugin::decodeURIComponent( $font['value'] ) ), true ); |
| 69 |
|
| 70 |
$value = $this->validate_font_values( $value ); |
| 71 |
|
| 72 |
// in case the value is still null, try default value(mostly for google fonts) |
| 73 |
if ( ! is_array( $value ) || $value === null ) { |
| 74 |
$value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); |
| 75 |
} |
| 76 |
|
| 77 |
//bail if by this time we don't have a value of some sort |
| 78 |
if ( empty( $value ) ) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
//Handle special logic for when the $value array is not an associative array |
| 83 |
if ( ! self::is_assoc( $value ) ) { |
| 84 |
$value = $this->process_a_not_associative_font_default( $value ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) { |
| 88 |
$args['google_families'] .= "'" . $value['font_family']; |
| 89 |
|
| 90 |
if ( $load_all_weights && is_array( $value['variants'] ) ) { |
| 91 |
$args['google_families'] .= ":" . implode( ',', $value['variants'] ); |
| 92 |
} elseif ( isset( $value['selected_variants'] ) && ! empty( $value['selected_variants'] ) ) { |
| 93 |
if ( is_array( $value['selected_variants'] ) ) { |
| 94 |
$args['google_families'] .= ":" . implode( ',', $value['selected_variants'] ); |
| 95 |
} elseif ( is_string( $value['selected_variants'] ) || is_numeric( $value['selected_variants'] ) ) { |
| 96 |
$args['google_families'] .= ":" . $value['selected_variants']; |
| 97 |
} |
| 98 |
} elseif ( isset( $value['variants'] ) && ! empty( $value['variants'] ) ) { |
| 99 |
if ( is_array( $value['variants'] ) ) { |
| 100 |
$args['google_families'] .= ":" . implode( ',', $value['variants'] ); |
| 101 |
} else { |
| 102 |
$args['google_families'] .= ":" . $value['variants']; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( isset( $value['selected_subsets'] ) && ! empty( $value['selected_subsets'] ) ) { |
| 107 |
if ( is_array( $value['selected_subsets'] ) ) { |
| 108 |
$args['google_families'] .= ":" . implode( ',', $value['selected_subsets'] ); |
| 109 |
} else { |
| 110 |
$args['google_families'] .= ":" . $value['selected_subsets']; |
| 111 |
} |
| 112 |
} elseif ( isset( $value['subsets'] ) && ! empty( $value['subsets'] ) ) { |
| 113 |
if ( is_array( $value['subsets'] ) ) { |
| 114 |
$args['google_families'] .= ":" . implode( ',', $value['subsets'] ); |
| 115 |
} else { |
| 116 |
$args['google_families'] .= ":" . $value['subsets']; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$args['google_families'] .= '\','; |
| 121 |
} elseif ( isset( self::$theme_fonts[ $value['font_family'] ] ) ) { |
| 122 |
|
| 123 |
// $value['type'] = 'theme_font'; |
| 124 |
// $args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ','; |
| 125 |
// $value['variants'] = self::$theme_fonts[ $value['font_family'] ]['variants']; |
| 126 |
|
| 127 |
if ( false === strpos( $args['local_families'], $value['font_family'] ) ) { |
| 128 |
$args['local_families'] .= $value['font_family']; |
| 129 |
} |
| 130 |
|
| 131 |
if ( false === strpos( $args['local_srcs'], self::$theme_fonts[ $value['font_family'] ]['src'] ) ) { |
| 132 |
$args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ','; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) { |
| 139 |
$this->display_webfont_script( $args ); |
| 140 |
} |
| 141 |
|
| 142 |
foreach ( self::$typo_settings as $key => $font ) { |
| 143 |
if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
|
| 147 |
$value = json_decode( PixCustomifyPlugin::decodeURIComponent( $font['value'] ), true ); |
| 148 |
// in case the value is still null, try default value(mostly for google fonts) |
| 149 |
if ( $value === null ) { |
| 150 |
$value = $this->get_font_defaults_value( $font['value'] ); |
| 151 |
} |
| 152 |
|
| 153 |
// shim the old case when the default was only the font name |
| 154 |
if ( is_string( $value ) && ! empty( $value ) ) { |
| 155 |
$value = array( 'font_family' => $value ); |
| 156 |
} |
| 157 |
|
| 158 |
//Handle special logic for when the $value array is not an associative array |
| 159 |
if ( ! self::is_assoc( $value ) ) { |
| 160 |
$value = $this->process_a_not_associative_font_default( $value ); |
| 161 |
} |
| 162 |
|
| 163 |
$this->output_font_style( $key, $font, $value ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
function display_webfont_script( $args ) { ?> |
| 168 |
<script type="text/javascript"> |
| 169 |
var customify_font_loader = function () { |
| 170 |
var webfontargs = { |
| 171 |
classes: false, |
| 172 |
events: false |
| 173 |
}; |
| 174 |
<?php if ( ! empty( $args['google_families'] ) ) { ?> |
| 175 |
webfontargs.google = { families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>] }; |
| 176 |
<?php } |
| 177 |
if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?> |
| 178 |
webfontargs.custom = { |
| 179 |
families: ['<?php echo( rtrim( $args['local_families'], ',' ) ); ?>'], |
| 180 |
urls: ['<?php echo rtrim( $args['local_srcs'], ',' ) ?>'] |
| 181 |
}; |
| 182 |
<?php } ?> |
| 183 |
WebFont.load(webfontargs); |
| 184 |
} |
| 185 |
|
| 186 |
if ( typeof WebFont !== 'undefined' ) { <?php // if there is a WebFont object, use it ?> |
| 187 |
customify_font_loader(); |
| 188 |
} else { <?php // basically when we don't have the WebFont object we create the google script dynamically ?> |
| 189 |
var tk = document.createElement('script'); |
| 190 |
tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; |
| 191 |
tk.type = 'text/javascript'; |
| 192 |
|
| 193 |
tk.onload = tk.onreadystatechange = function () { |
| 194 |
customify_font_loader(); |
| 195 |
}; |
| 196 |
var s = document.getElementsByTagName('script')[0]; |
| 197 |
s.parentNode.insertBefore(tk, s); |
| 198 |
} |
| 199 |
</script> |
| 200 |
<?php |
| 201 |
} |
| 202 |
|
| 203 |
function output_font_style( $field, $font, $value ) { |
| 204 |
$value = $this->validate_font_values( $value ); |
| 205 |
// some sanitizing |
| 206 |
$load_all_weights = false; |
| 207 |
if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| 208 |
$load_all_weights = true; |
| 209 |
} |
| 210 |
|
| 211 |
$selected_variant = ''; |
| 212 |
if ( ! empty( $value['selected_variants'] ) ) { |
| 213 |
if ( is_array( $value['selected_variants'] ) ) { |
| 214 |
$selected_variant = $value['selected_variants'][0]; |
| 215 |
} else { |
| 216 |
$selected_variant = $value['selected_variants']; |
| 217 |
} |
| 218 |
} ?> |
| 219 |
<style id="customify_font_output_for_<?php echo $field; ?>"> |
| 220 |
<?php |
| 221 |
if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) { |
| 222 |
$output = call_user_func( $font['callback'], $value, $font ); |
| 223 |
echo $output; |
| 224 |
} else { |
| 225 |
echo $font['selector'] . " {"; |
| 226 |
|
| 227 |
// First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin) |
| 228 |
if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) { |
| 229 |
//the variant's font-family |
| 230 |
$this->display_property( 'font-family', $selected_variant['font-family'] ); |
| 231 |
|
| 232 |
if ( ! $load_all_weights ) { |
| 233 |
// if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all |
| 234 |
// we need to "force" the font-weight and font-style |
| 235 |
if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) { |
| 236 |
$selected_variant['font-weight'] = '400 !important'; |
| 237 |
$selected_variant['font-style'] = 'normal !important'; |
| 238 |
} |
| 239 |
|
| 240 |
// output the font weight, if available |
| 241 |
if ( ! empty( $selected_variant['font-weight'] ) ) { |
| 242 |
echo ": " . $selected_variant['font-weight'] . ";\n"; |
| 243 |
$this->display_property( 'font-weight', $selected_variant['font-weight'] ); |
| 244 |
} |
| 245 |
|
| 246 |
// output the font style, if available |
| 247 |
if ( ! empty( $selected_variant['font-style'] ) ) { |
| 248 |
$this->display_property( 'font-style', $selected_variant['font-style'] ); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
} elseif ( isset( $value['font_family'] ) ) { |
| 253 |
// the selected font family |
| 254 |
$this->display_property( 'font-family', $value['font_family'] ); |
| 255 |
|
| 256 |
if ( ! empty( $selected_variant ) && ! $load_all_weights ) { |
| 257 |
$weight_and_style = strtolower( $selected_variant ); |
| 258 |
$italic_font = false; |
| 259 |
|
| 260 |
//determine if this is an italic font (the $weight_and_style is usually like '400' or '400italic' ) |
| 261 |
if ( strpos( $weight_and_style, 'italic' ) !== false ) { |
| 262 |
$weight_and_style = str_replace( 'italic', '', $weight_and_style); |
| 263 |
$italic_font = true; |
| 264 |
} |
| 265 |
|
| 266 |
if ( ! empty( $weight_and_style ) ) { |
| 267 |
//a little bit of sanity check - in case it's not a number |
| 268 |
if( $weight_and_style === 'regular' ) { |
| 269 |
$weight_and_style = 'normal'; |
| 270 |
} |
| 271 |
$this->display_property( 'font-weight', $weight_and_style ); |
| 272 |
} |
| 273 |
|
| 274 |
if ( $italic_font ) { |
| 275 |
$this->display_property( 'font-style', 'italic' ); |
| 276 |
} |
| 277 |
} |
| 278 |
} else if ( isset( $value['font-family'] ) ) { |
| 279 |
$this->display_property( 'font-family', $value['font-family'] ); |
| 280 |
} |
| 281 |
|
| 282 |
if ( ! empty( $value['font_weight'] ) ) { |
| 283 |
$this->display_property( 'font-weight', $value['font_weight'] ); |
| 284 |
} |
| 285 |
|
| 286 |
if ( ! empty( $value['font_size'] ) ) { |
| 287 |
$unit = $this->get_field_unit( $font, 'font-size' ); |
| 288 |
$this->display_property( 'font-size', $value['font_size'], $unit ); |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! empty( $value['line_height'] ) ) { |
| 292 |
$unit = $this->get_field_unit( $font, 'line-height' ); |
| 293 |
$this->display_property( 'line-height', $value['line_height'], $unit ); |
| 294 |
} |
| 295 |
|
| 296 |
if ( ! empty( $value['letter_spacing'] ) ) { |
| 297 |
$unit = $this->get_field_unit( $font, 'letter-spacing' ); |
| 298 |
$this->display_property( 'letter-spacing', $value['letter_spacing'], $unit ); |
| 299 |
} |
| 300 |
|
| 301 |
if ( ! empty( $value['text_align'] ) ) { |
| 302 |
$this->display_property( 'text-align', $value['text_align'] ); |
| 303 |
} |
| 304 |
|
| 305 |
if ( ! empty( $value['text_transform'] ) ) { |
| 306 |
$this->display_property( 'text-transform', $value['text_transform'] ); |
| 307 |
} |
| 308 |
|
| 309 |
if ( ! empty( $value['text_decoration'] ) ) { |
| 310 |
$this->display_property( 'text-decoration', $value['text_decoration'] ); |
| 311 |
} |
| 312 |
echo "}\n"; |
| 313 |
} ?> |
| 314 |
</style> |
| 315 |
<?php |
| 316 |
} |
| 317 |
|
| 318 |
function get_field_unit( $font, $field ) { |
| 319 |
|
| 320 |
if ( empty( $font ) || empty( $font['fields'] ) || empty( $font['fields'][ $field ] ) ) { |
| 321 |
return 'px'; |
| 322 |
} |
| 323 |
|
| 324 |
if ( isset( $font['fields'][ $field ]['unit'] ) ) { |
| 325 |
return $font['fields'][ $field ]['unit']; |
| 326 |
} |
| 327 |
|
| 328 |
if ( isset( $font['fields'][ $field ][3] ) ) { |
| 329 |
return $font['fields'][ $field ][3]; |
| 330 |
} |
| 331 |
|
| 332 |
return 'px'; |
| 333 |
} |
| 334 |
|
| 335 |
function validate_font_values( $values ) { |
| 336 |
|
| 337 |
if ( empty( $values ) ) { |
| 338 |
return array(); |
| 339 |
} |
| 340 |
|
| 341 |
foreach ( $values as $key => $value ) { |
| 342 |
|
| 343 |
if ( strpos( $key, '-' ) !== false ) { |
| 344 |
$new_key = str_replace( '-', '_', $key ); |
| 345 |
|
| 346 |
$values[ $new_key ] = $value; |
| 347 |
|
| 348 |
unset( $values[ $key ] ); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return $values; |
| 353 |
} |
| 354 |
|
| 355 |
function display_selector( $selector ) { |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
function display_property( $property, $value, $unit = '' ) { |
| 360 |
echo "\n" . $property . ": " . $value . $unit . ";\n"; |
| 361 |
} |
| 362 |
|
| 363 |
function enqueue_admin_customizer_preview_assets() { |
| 364 |
$dir = plugin_dir_url( __FILE__ ); |
| 365 |
$dir = rtrim( $dir, 'features/' ); |
| 366 |
wp_enqueue_script( 'font_selector_preview', $dir . '/js/font_selector_preview.js', array( 'jquery' ), false, true ); |
| 367 |
} |
| 368 |
} |
| 369 |
|