| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file defines Custom Fonts |
| 4 |
* |
| 5 |
* Themify_Custom_Fonts class register post type for Custom Fonts and load them |
| 6 |
* |
| 7 |
* |
| 8 |
* @package Themify_Builder |
| 9 |
* @subpackage Themify_Builder/classes |
| 10 |
*/ |
| 11 |
if (!class_exists('Themify_Custom_Fonts',false)) : |
| 12 |
|
| 13 |
/** |
| 14 |
* The Custom Fonts class. |
| 15 |
* |
| 16 |
* This class register post type for Custom Fonts and load them. |
| 17 |
* |
| 18 |
* |
| 19 |
* @package Themify_Builder |
| 20 |
* @subpackage Themify_Builder/classes |
| 21 |
* @author Themify |
| 22 |
*/ |
| 23 |
class Themify_Custom_Fonts { |
| 24 |
|
| 25 |
/** |
| 26 |
* slug Custom Fonts Object. |
| 27 |
* |
| 28 |
* @access private const |
| 29 |
* @var string $slug . |
| 30 |
*/ |
| 31 |
const SLUG = 'tb_cf'; |
| 32 |
|
| 33 |
/** |
| 34 |
* API Url |
| 35 |
* |
| 36 |
* @access static |
| 37 |
* @var string $api_url . |
| 38 |
*/ |
| 39 |
public static $api_url; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 43 |
* |
| 44 |
* @access public |
| 45 |
*/ |
| 46 |
public static function init() { |
| 47 |
self::$api_url = site_url('/?tb_load_cf='); |
| 48 |
self::load_fonts_api(); |
| 49 |
self::register_cpt(); |
| 50 |
if (is_admin()) { |
| 51 |
add_filter('themify_metaboxes', array(__CLASS__, 'meta_box')); |
| 52 |
add_filter('themify_metabox/fields/tm-cf', array(__CLASS__, 'meta_box_fields'), 10, 2); |
| 53 |
add_filter('upload_mimes', array(__CLASS__, 'upload_mimes'), 99, 1); |
| 54 |
add_action('admin_head', array(__CLASS__, 'clean_admin_listing_page')); |
| 55 |
add_action('admin_footer', array(__CLASS__, 'admin_footer')); |
| 56 |
add_action('admin_footer', array(__CLASS__, 'edit_screen_footer')); |
| 57 |
} else { |
| 58 |
add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_custom_fonts'), 30); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register Custom Font Custom Post Type |
| 64 |
* |
| 65 |
* @access static |
| 66 |
*/ |
| 67 |
private static function register_cpt() { |
| 68 |
if (!class_exists('CPT',false)) { |
| 69 |
include THEMIFY_DIR . '/class-cpt.php'; |
| 70 |
} |
| 71 |
|
| 72 |
// create a template custom post type |
| 73 |
$cpt = new CPT(array( |
| 74 |
'post_type_name' => self::SLUG, |
| 75 |
'singular' => __('Custom Font', 'themify'), |
| 76 |
'plural' => __('Custom Fonts', 'themify') |
| 77 |
), array( |
| 78 |
'supports' => array('title'), |
| 79 |
'exclude_from_search' => true, |
| 80 |
'show_in_nav_menus' => false, |
| 81 |
'show_in_menu' => false, |
| 82 |
'show_ui' => true, |
| 83 |
'public' => false, |
| 84 |
'has_archive' => false |
| 85 |
)); |
| 86 |
|
| 87 |
// define the columns to appear on the admin edit screen |
| 88 |
$cpt->columns(array( |
| 89 |
'cb' => '<input type="checkbox" />', |
| 90 |
'title' => __('Title', 'themify'), |
| 91 |
'font_preview' => __('Preview', 'themify') |
| 92 |
)); |
| 93 |
} |
| 94 |
|
| 95 |
public static function meta_box(array $meta_boxes):array { |
| 96 |
$meta_boxes['tm-cf'] = array( |
| 97 |
'id' => 'tm-cf', |
| 98 |
'title' => __('Manage Font Files', 'themify'), |
| 99 |
'context' => 'normal', |
| 100 |
'priority' => 'high', |
| 101 |
'screen' => array(self::SLUG) |
| 102 |
); |
| 103 |
|
| 104 |
return $meta_boxes; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Setup the custom fields |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public static function meta_box_fields(array $fields):array { |
| 113 |
$weights = array( |
| 114 |
array('value' => '100', 'name' => __('100', 'themify')), |
| 115 |
array('value' => '200', 'name' => __('200', 'themify')), |
| 116 |
array('value' => '300', 'name' => __('300', 'themify')), |
| 117 |
array('value' => '400', 'name' => __('400', 'themify')), |
| 118 |
array('value' => '500', 'name' => __('500', 'themify')), |
| 119 |
array('value' => '600', 'name' => __('600', 'themify')), |
| 120 |
array('value' => '700', 'name' => __('700', 'themify')), |
| 121 |
array('value' => '800', 'name' => __('800', 'themify')), |
| 122 |
array('value' => '900', 'name' => __('900', 'themify')), |
| 123 |
); |
| 124 |
$weights_max = $weights; |
| 125 |
foreach ($weights_max as $i => $w) { |
| 126 |
if ($w['value'] === '900') { |
| 127 |
$weights_max[$i]['selected'] = true; |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$fields['tm-cf'] = array( |
| 133 |
'name' => __('Font Variations', 'themify'), |
| 134 |
'id' => 'tm-cf', |
| 135 |
'options' => array( |
| 136 |
array( |
| 137 |
'name' => 'custom_fonts_notice', |
| 138 |
'title' => '', |
| 139 |
'description' => '', |
| 140 |
'type' => 'separator', |
| 141 |
'meta' => array( |
| 142 |
'html' => '<div class="themify-info-link">' . __('To add custom fonts: select the font weight/style and upload the font files accordingly. You don\'t need to upload all formats. The common support format is .ttf or .woff/.woff2. For variable fonts, enable "Variable Font" and set the min/max weight range (usually 100–900), then upload a single .woff2 or .ttf variable font file. The custom fonts will appear on Builder and Customizer font select.', 'themify') . '</div>' |
| 143 |
), |
| 144 |
), |
| 145 |
array( |
| 146 |
'name' => 'variations', |
| 147 |
'type' => 'repeater', |
| 148 |
'show_first' => true, |
| 149 |
'add_new_label' => __('Add New Variation', 'themify'), |
| 150 |
'fields' => array( |
| 151 |
array( |
| 152 |
'name' => 'variable', |
| 153 |
'title' => __('Variable Font', 'themify'), |
| 154 |
'label' => __('This is a variable font (single file with a weight range)', 'themify'), |
| 155 |
'type' => 'checkbox', |
| 156 |
), |
| 157 |
array( |
| 158 |
'name' => 'weight', |
| 159 |
'title' => __('Weight', 'themify'), |
| 160 |
'type' => 'fontweight_dropdown', |
| 161 |
'meta' => $weights, |
| 162 |
'description' => __('For variable fonts, this is the minimum weight.', 'themify'), |
| 163 |
), |
| 164 |
array( |
| 165 |
'name' => 'weight_max', |
| 166 |
'title' => __('Max Weight', 'themify'), |
| 167 |
'type' => 'fontweight_dropdown', |
| 168 |
'meta' => $weights_max, |
| 169 |
'description' => __('For variable fonts only. Usually 900.', 'themify'), |
| 170 |
'class' => 'tf-cf-weight-max', |
| 171 |
), |
| 172 |
array( |
| 173 |
'name' => 'style', |
| 174 |
'title' => __('Style', 'themify'), |
| 175 |
'type' => 'dropdown', |
| 176 |
'meta' => array( |
| 177 |
array('value' => 'normal', 'name' => __('Normal', 'themify')), |
| 178 |
array('value' => 'italic', 'name' => __('Italic', 'themify')), |
| 179 |
array('value' => 'oblique', 'name' => __('Oblique', 'themify')) |
| 180 |
) |
| 181 |
), |
| 182 |
array( |
| 183 |
'name' => 'woff', |
| 184 |
'ext' => 'woff', |
| 185 |
'title' => __('WOFF File', 'themify'), |
| 186 |
'type' => 'font', |
| 187 |
'mime' => 'application/x-font-woff', |
| 188 |
), |
| 189 |
array( |
| 190 |
'name' => 'woff2', |
| 191 |
'ext' => 'woff2', |
| 192 |
'title' => __('WOFF2 File', 'themify'), |
| 193 |
'type' => 'font', |
| 194 |
'mime' => 'application/x-font-woff2', |
| 195 |
), |
| 196 |
array( |
| 197 |
'name' => 'ttf', |
| 198 |
'ext' => 'ttf,otf', |
| 199 |
'title' => __('TTF / OTF File', 'themify'), |
| 200 |
'type' => 'font', |
| 201 |
'mime' => 'application/x-font-ttf', |
| 202 |
), |
| 203 |
array( |
| 204 |
'name' => 'svg', |
| 205 |
'ext' => 'svg', |
| 206 |
'title' => __('SVG File', 'themify'), |
| 207 |
'type' => 'font', |
| 208 |
'mime' => 'image/svg+xml', |
| 209 |
), |
| 210 |
array( |
| 211 |
'name' => 'eot', |
| 212 |
'ext' => 'eot', |
| 213 |
'title' => __('EOT File', 'themify'), |
| 214 |
'type' => 'font', |
| 215 |
'mime' => 'application/x-font-eot', |
| 216 |
), |
| 217 |
array( |
| 218 |
'name' => 'separator', |
| 219 |
'type' => 'separator', |
| 220 |
), |
| 221 |
), |
| 222 |
) |
| 223 |
), |
| 224 |
); |
| 225 |
|
| 226 |
return $fields; |
| 227 |
} |
| 228 |
|
| 229 |
public static function upload_mimes(array $mime_types):array { |
| 230 |
$ext = array( |
| 231 |
'woff' => 'application/x-font-woff', |
| 232 |
'woff2' => 'application/x-font-woff2', |
| 233 |
'ttf' => 'font/sfnt', |
| 234 |
'svg' => 'image/svg+xml', |
| 235 |
'eot' => 'application/x-font-eot', |
| 236 |
'otf' => 'font/otf', |
| 237 |
); |
| 238 |
foreach ($ext as $type => $mime) { |
| 239 |
if (!isset($mime_types[$type])) { |
| 240 |
$mime_types[$type] = $mime; |
| 241 |
} |
| 242 |
} |
| 243 |
return $mime_types; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Clean up admin Font manager admin listing |
| 248 |
*/ |
| 249 |
public static function clean_admin_listing_page() { |
| 250 |
global $typenow; |
| 251 |
if (self::SLUG === $typenow) { |
| 252 |
add_filter('months_dropdown_results', '__return_empty_array'); |
| 253 |
add_action('manage_' . self::SLUG . '_posts_custom_column', array(__CLASS__, 'render_columns'), 10, 2); |
| 254 |
add_filter('screen_options_show_screen', '__return_false'); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Render preview column in font manager admin listing |
| 260 |
* |
| 261 |
* @param $column |
| 262 |
* @param $post_id |
| 263 |
*/ |
| 264 |
public static function render_columns($column, $post_id) { |
| 265 |
if ('font_preview' === $column) { |
| 266 |
$variations = get_post_meta($post_id, 'variations', true); |
| 267 |
if (empty($variations)) { |
| 268 |
return; |
| 269 |
} |
| 270 |
$fonts = array( |
| 271 |
'woff' => 'woff', |
| 272 |
'woff2' => 'woff2', |
| 273 |
'ttf' => 'truetype', |
| 274 |
'svg' => 'svg', |
| 275 |
'eot' => 'embedded-opentype', |
| 276 |
); |
| 277 |
$font_family = get_the_title($post_id); |
| 278 |
ob_start(); |
| 279 |
foreach ($variations as $var): |
| 280 |
$src = ''; |
| 281 |
foreach ($fonts as $type => $format) { |
| 282 |
$src .= !empty($var[$type]) ? 'url(\'' . $var[$type] . '\') format(\'' . $format . '\'),' : ''; |
| 283 |
} |
| 284 |
list($weight_min, $weight_max) = self::get_weight_range($var); |
| 285 |
$font_weight = self::is_variable($var) ? $weight_min . ' ' . $weight_max : $weight_min; |
| 286 |
?> |
| 287 |
@font-face{font-family:'<?php echo $font_family; ?>';font-weight:<?php echo $font_weight; ?>;font-style:<?php echo $var['style']; ?>;src:<?php echo rtrim($src, ','); ?>; } |
| 288 |
<?php |
| 289 |
endforeach; |
| 290 |
$font_face = ob_get_clean(); |
| 291 |
printf('<style>%s</style><span style="font-family: \'%s\';">%s</span>', trim($font_face), $font_family, __('Themify makes your dreams come true.', 'themify')); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Returns a list of Custom Fonts |
| 297 |
* @return array |
| 298 |
*/ |
| 299 |
public static function get_list($env = 'builder') { |
| 300 |
$list = 'builder' !== $env ? array() : array( |
| 301 |
array('value' => '', 'name' => ''), |
| 302 |
array( |
| 303 |
'value' => '', |
| 304 |
'name' => '--- ' . __('Custom Fonts', 'themify') . ' ---' |
| 305 |
) |
| 306 |
); |
| 307 |
$fonts = self::get_posts(array('limit' => -1)); |
| 308 |
foreach ($fonts as $slug => $font) { |
| 309 |
if (!empty($font['family']) && !empty($font['variants'])) { |
| 310 |
$list[] = array( |
| 311 |
'value' => $slug, |
| 312 |
'name' => $font['family'], |
| 313 |
'variant' => $font['variants'] |
| 314 |
); |
| 315 |
} |
| 316 |
} |
| 317 |
return $list; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Whether a variation is a variable font |
| 322 |
*/ |
| 323 |
private static function is_variable($data): bool { |
| 324 |
return !empty($data['variable']); |
| 325 |
} |
| 326 |
|
| 327 |
private static function normalize_weight($weight): string { |
| 328 |
$weight = str_replace(array('italic', 'oblique', 'regular', 'normal', 'bold'), array('', '', '400', '400', '700'), (string) $weight); |
| 329 |
$weight = preg_replace('/[^0-9]/', '', $weight); |
| 330 |
return $weight === '' ? '400' : $weight; |
| 331 |
} |
| 332 |
|
| 333 |
private static function variant_key($weight, $style = 'normal'): string { |
| 334 |
$weight = (string) $weight; |
| 335 |
if ($style === 'italic') { |
| 336 |
return $weight . 'i'; |
| 337 |
} |
| 338 |
if (!empty($style) && $style !== 'normal') { |
| 339 |
return $weight . $style; |
| 340 |
} |
| 341 |
return $weight; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Get min/max weight for a variation (same value twice for static fonts) |
| 346 |
* |
| 347 |
* @return int[] |
| 348 |
*/ |
| 349 |
private static function get_weight_range($data): array { |
| 350 |
$min = (int) self::normalize_weight(!empty($data['weight']) ? $data['weight'] : '400'); |
| 351 |
if (!self::is_variable($data)) { |
| 352 |
return array($min, $min); |
| 353 |
} |
| 354 |
$max = !empty($data['weight_max']) ? (int) $data['weight_max'] : 900; |
| 355 |
if ($max < $min) { |
| 356 |
return array($max, $min); |
| 357 |
} |
| 358 |
return array($min, $max); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Whether a requested weight matches a stored variation |
| 363 |
*/ |
| 364 |
private static function matches_weight($requested, $data): bool { |
| 365 |
$requested = (int) self::normalize_weight($requested); |
| 366 |
list($min, $max) = self::get_weight_range($data); |
| 367 |
return $requested >= $min && $requested <= $max; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Returns a list of variants |
| 372 |
* @return array |
| 373 |
*/ |
| 374 |
private static function get_variants($variants) { |
| 375 |
$vars = array(); |
| 376 |
if (!empty($variants) && is_array($variants)) { |
| 377 |
foreach ($variants as $var) { |
| 378 |
$style = !empty($var['style']) ? $var['style'] : 'normal'; |
| 379 |
list($min, $max) = self::get_weight_range($var); |
| 380 |
if (self::is_variable($var)) { |
| 381 |
for ($w = $min; $w <= $max; $w += 100) { |
| 382 |
$key = self::variant_key($w, $style); |
| 383 |
if ((string) $w === '400' && $style === 'normal') { |
| 384 |
array_unshift($vars, $key); |
| 385 |
} else { |
| 386 |
$vars[] = $key; |
| 387 |
} |
| 388 |
} |
| 389 |
} else { |
| 390 |
$key = self::variant_key($min, $style); |
| 391 |
if ((string) $min === '400' && $style === 'normal') { |
| 392 |
array_unshift($vars, $key); |
| 393 |
} else { |
| 394 |
$vars[] = $key; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
$vars = array_keys(array_flip($vars)); |
| 399 |
} |
| 400 |
return $vars; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Get a list of Custom Fonts CPT |
| 405 |
* |
| 406 |
* @param array $args arguments of get posts |
| 407 |
* @return array |
| 408 |
*/ |
| 409 |
private static function get_posts(array $args = array()):array { |
| 410 |
$limit = empty($args['limit']) ? 10 : $args['limit']; |
| 411 |
$post_names = empty($args['post_names']) ? array() : $args['post_names']; |
| 412 |
$cf_posts = array(); |
| 413 |
$posts_args = array( |
| 414 |
'post_type' => self::SLUG, |
| 415 |
'posts_per_page' => $limit, |
| 416 |
'no_found_rows' => true, |
| 417 |
'ignore_sticky_posts' => true, |
| 418 |
'orderby' => 'ID', |
| 419 |
'order' => 'DESC', |
| 420 |
'post_name__in' => $post_names |
| 421 |
); |
| 422 |
$posts = get_posts($posts_args); |
| 423 |
unset($posts_args); |
| 424 |
if ($posts) { |
| 425 |
foreach ($posts as $post) { |
| 426 |
$post_id = $post->ID; |
| 427 |
$data = get_post_meta($post_id, 'variations', true); |
| 428 |
$cf_posts[$post->post_name] = array( |
| 429 |
'family' => $post->post_title, |
| 430 |
'variants' => self::get_variants($data), |
| 431 |
'data' => $data |
| 432 |
); |
| 433 |
} |
| 434 |
} |
| 435 |
return $cf_posts; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Generate @font-face CSS for list of fonts in $_GET['tb_load_cf'] |
| 440 |
* |
| 441 |
* This is used only in Builder live preview. On frontend self::load_fonts() |
| 442 |
* adds the necessary code as inline CSS to the page. |
| 443 |
* |
| 444 |
* @return void |
| 445 |
*/ |
| 446 |
private static function load_fonts_api() { |
| 447 |
if (!empty($_GET['tb_load_cf'])) { |
| 448 |
header('Content-Type: text/css'); |
| 449 |
header('Expires: 0'); |
| 450 |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 451 |
header('Cache-Control: private', false); |
| 452 |
header('Content-Transfer-Encoding: binary'); |
| 453 |
$fonts = $_GET['tb_load_cf']; |
| 454 |
$fonts = preg_replace('/\.css$/', '', $fonts); |
| 455 |
$fonts = explode('|', $fonts); |
| 456 |
echo self::get_font_face_css($fonts); |
| 457 |
exit; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Returns @font-face CSS for all the custom fonts used in a given $post_id |
| 463 |
* |
| 464 |
* @param int $post_id |
| 465 |
* @return string Generated CSS code to load the fonts |
| 466 |
*/ |
| 467 |
public static function load_fonts($post_id):string { |
| 468 |
$fonts = explode('|', self::get_fonts($post_id)); |
| 469 |
return self::get_font_face_css($fonts, true); |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Generate @font-family CSS code from an array of fonts |
| 474 |
* |
| 475 |
* @param array $fonts |
| 476 |
* @return string |
| 477 |
*/ |
| 478 |
private static function get_font_face_css($fonts, $builder = false):string { |
| 479 |
$font_css = ''; |
| 480 |
$api_fonts = array(); |
| 481 |
if (true === $builder) { |
| 482 |
$customizer_fonts = apply_filters('themify_custom_fonts', array()); |
| 483 |
$cf = array(); |
| 484 |
foreach ($customizer_fonts as $font) { |
| 485 |
$f = explode(':', $font); |
| 486 |
$v = isset($f[1]) ? explode(',', $f[1]) : array('normal'); |
| 487 |
$cf[$f[0]] = !isset($cf[$f[0]]) ? $v : array_keys(array_flip(array_merge($cf[$f[0]], $v))); |
| 488 |
} |
| 489 |
$customizer_fonts = $cf; |
| 490 |
$cf = null; |
| 491 |
} |
| 492 |
foreach ($fonts as $font) { |
| 493 |
if (!empty($font)) { |
| 494 |
$font = explode(':', $font); |
| 495 |
$font_family = $font[0]; |
| 496 |
$variations = empty($font[1]) ? array() : explode(',', $font[1]); |
| 497 |
// Skip loading duplicate fonts |
| 498 |
if (true === $builder && isset($customizer_fonts[$font_family])) { |
| 499 |
$variations = array_diff($variations, $customizer_fonts[$font_family]); |
| 500 |
} |
| 501 |
$api_fonts[$font_family] = !isset($api_fonts[$font_family]) ? $variations : array_keys(array_flip(array_merge($api_fonts[$font_family], $variations))); |
| 502 |
} |
| 503 |
} |
| 504 |
unset($fonts); |
| 505 |
if (!empty($api_fonts)) { |
| 506 |
$cf_fonts = self::get_posts(array('post_names' => array_keys($api_fonts), 'limit' => -1)); |
| 507 |
if (!empty($cf_fonts)) { |
| 508 |
foreach ($api_fonts as $font_family => $variations) { |
| 509 |
if ( !empty($cf_fonts[$font_family]['data'])) { |
| 510 |
if (empty($variations) && isset($cf_fonts[$font_family]['variants'])) { |
| 511 |
$variations = $cf_fonts[$font_family]['variants']; |
| 512 |
} |
| 513 |
if (!empty($variations)) { |
| 514 |
foreach ($variations as $var) { |
| 515 |
foreach ($cf_fonts[$font_family]['data'] as $k => $v) { |
| 516 |
if (self::matches_weight($var, $v)) { |
| 517 |
$font_css .= self::get_font_face_from_data($font_family, $cf_fonts[$font_family]['data'][$k]) . PHP_EOL; |
| 518 |
unset($cf_fonts[$font_family]['data'][$k]); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
return $font_css; |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Generate font-face CSS |
| 533 |
* |
| 534 |
* @param string $font_family |
| 535 |
* @param array $data variations data |
| 536 |
* @return string |
| 537 |
*/ |
| 538 |
private static function get_font_face_from_data($font_family, $data) { |
| 539 |
$font_face = ''; |
| 540 |
$src = array(); |
| 541 |
$types = array('woff2', 'woff', 'eot', 'ttf', 'svg'); |
| 542 |
foreach ($types as $type) { |
| 543 |
if (empty($data[$type])) { |
| 544 |
continue; |
| 545 |
} |
| 546 |
if ('svg' === $type) { |
| 547 |
$data[$type] .= '#' . str_replace(' ', '', $font_family); |
| 548 |
} |
| 549 |
|
| 550 |
$src[] = self::get_font_src_per_type($type, $data[$type]); |
| 551 |
} |
| 552 |
if (empty($src)) { |
| 553 |
return $font_face; |
| 554 |
} |
| 555 |
list($weight_min, $weight_max) = self::get_weight_range($data); |
| 556 |
$font_weight = self::is_variable($data) ? $weight_min . ' ' . $weight_max : $weight_min; |
| 557 |
$font_face = '@font-face{' . PHP_EOL; |
| 558 |
$font_face .= "\tfont-family:'" . $font_family . "';" . PHP_EOL; |
| 559 |
$font_face .= "\tfont-style:" . $data['style'] . ';' . PHP_EOL; |
| 560 |
$font_face .= "\tfont-weight:" . $font_weight . ';' . PHP_EOL; |
| 561 |
$font_face .= "\tfont-display:swap;" . PHP_EOL; |
| 562 |
|
| 563 |
if (!empty($data['eot'])) { |
| 564 |
$font_face .= "\tsrc:url('" . esc_attr($data['eot']) . "');" . PHP_EOL; |
| 565 |
} |
| 566 |
|
| 567 |
$font_face .= "\tsrc:" . implode(',' . PHP_EOL . "\t\t", $src) . PHP_EOL . '}'; |
| 568 |
|
| 569 |
return $font_face; |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Generate font file src base on file type |
| 574 |
* |
| 575 |
* @param string $type font file type |
| 576 |
* @param string $url font file url |
| 577 |
* @return string |
| 578 |
*/ |
| 579 |
private static function get_font_src_per_type($type, $url) { |
| 580 |
$src = 'url(\'' . esc_attr($url) . '\') '; |
| 581 |
switch ($type) { |
| 582 |
case 'woff': |
| 583 |
case 'woff2': |
| 584 |
case 'svg': |
| 585 |
$src .= 'format(\'' . $type . '\')'; |
| 586 |
break; |
| 587 |
case 'ttf': |
| 588 |
$src .= 'format(\'truetype\')'; |
| 589 |
break; |
| 590 |
case 'eot': |
| 591 |
$src = 'url(\'' . esc_attr($url) . '?#iefix\') format(\'embedded-opentype\')'; |
| 592 |
break; |
| 593 |
} |
| 594 |
return $src; |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Enqueues Custom Fonts (Builder) |
| 599 |
*/ |
| 600 |
public static function get_fonts($post_id = null) { |
| 601 |
$entry_cf_fonts = get_option('themify_builder_cf_fonts'); |
| 602 |
if (!empty($entry_cf_fonts) && is_array($entry_cf_fonts)) { |
| 603 |
$entry_id = $post_id ? $post_id : Themify_Builder_Model::get_ID(); |
| 604 |
if (isset($entry_cf_fonts[$entry_id])) { |
| 605 |
return $entry_cf_fonts[$entry_id]; |
| 606 |
} |
| 607 |
} |
| 608 |
return false; |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* Enqueue Custom fonts (if any) on the page |
| 613 |
* |
| 614 |
* @uses themify_custom_fonts filter |
| 615 |
*/ |
| 616 |
public static function enqueue_custom_fonts() { |
| 617 |
$fonts = apply_filters('themify_custom_fonts', array()); |
| 618 |
if (!empty($fonts)) { |
| 619 |
$css = self::get_font_face_css($fonts); |
| 620 |
if (!empty($css)) { |
| 621 |
echo '<style id="themify-custom-fonts">' . $css . '</style>'; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
public static function get_cf_fonts_url(array $fonts = array()) { |
| 627 |
$res = array(); |
| 628 |
static $isLoaded = array(); |
| 629 |
foreach ($fonts as $key => $font) { |
| 630 |
if (!empty($font) && preg_match('/^\w/', $font)) { |
| 631 |
/* fix the delimiter with multiple weight variants, it should use `,` and not `:` |
| 632 |
reset the delimiter between font name and first variant */ |
| 633 |
$font = preg_replace('/,/', ':', str_replace(':', ',', $font), 1); |
| 634 |
if (!in_array($font, $isLoaded, true)) { |
| 635 |
$isLoaded[] = $font; |
| 636 |
$res[$key] = $font; |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
if (!empty($res)) { |
| 641 |
return self::$api_url . implode('|', $res); |
| 642 |
} |
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
public static function admin_footer() { |
| 647 |
$screen = get_current_screen(); |
| 648 |
if ($screen->base !== 'edit' || $screen->post_type !== self::SLUG) { |
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
wp_enqueue_script('jquery-ui-slider'); |
| 653 |
wp_enqueue_script('themify-scripts'); |
| 654 |
wp_enqueue_style('themify-ui'); |
| 655 |
wp_enqueue_style('themify-icons'); |
| 656 |
?> |
| 657 |
|
| 658 |
<div class="notice notice-success" id="tf_font_import_notice"> |
| 659 |
<p><?php themify_is_themify_theme() ? printf(__('FYI: You can download all in-use Google Fonts to your own server. Go to <a href="%s">Themify Settings > General</a> > Google Fonts to enable this option.', 'themify'), admin_url('admin.php?page=themify#setting-general')) : printf(__('FYI: You can download all in-use Google Fonts to your own server. Go to <a href="%s">Themify Builder > Settings</a> Download Google Fonts section to enable this option.', 'themify'), admin_url('admin.php?page=themify-builder')); ?></p> |
| 660 |
</div> |
| 661 |
|
| 662 |
<style id="tf_font_preview_style"></style> |
| 663 |
|
| 664 |
<div id="tf_font_size_slider_wrap"> |
| 665 |
<div id="tf_font_size_slider"> |
| 666 |
</div> |
| 667 |
</div> |
| 668 |
<?php |
| 669 |
} |
| 670 |
|
| 671 |
/** |
| 672 |
* Toggle Max Weight field when Variable Font is enabled on the edit screen |
| 673 |
*/ |
| 674 |
public static function edit_screen_footer() { |
| 675 |
$screen = get_current_screen(); |
| 676 |
if (!$screen || $screen->post_type !== self::SLUG || !in_array($screen->base, array('post', 'post-new'), true)) { |
| 677 |
return; |
| 678 |
} |
| 679 |
?> |
| 680 |
<script> |
| 681 |
jQuery(function($){ |
| 682 |
function toggleRow($row){ |
| 683 |
var isVar = $row.find('input[type="checkbox"][name*="[variable]"]').is(':checked'); |
| 684 |
$row.find('select[name$="[weight_max]"]').closest('.themify_field_row').toggle(isVar); |
| 685 |
var $weightTitle = $row.find('select[name$="[weight]"]').closest('.themify_field_row').find('.themify_field_title'); |
| 686 |
if ($weightTitle.length) { |
| 687 |
$weightTitle.text(isVar ? '<?php echo esc_js(__('Min Weight', 'themify')); ?>' : '<?php echo esc_js(__('Weight', 'themify')); ?>'); |
| 688 |
} |
| 689 |
} |
| 690 |
function refreshAll(){ |
| 691 |
$('.themify-repeater-row').each(function(){ toggleRow($(this)); }); |
| 692 |
} |
| 693 |
refreshAll(); |
| 694 |
$(document).on('change', 'input[type="checkbox"][name*="[variable]"]', function(){ |
| 695 |
var $row = $(this).closest('.themify-repeater-row'); |
| 696 |
if (this.checked) { |
| 697 |
$row.find('select[name$="[weight]"]').val('100'); |
| 698 |
$row.find('select[name$="[weight_max]"]').val('900'); |
| 699 |
} |
| 700 |
toggleRow($row); |
| 701 |
}); |
| 702 |
$(document).on('click', '.themify-repeater-add', function(){ |
| 703 |
setTimeout(refreshAll, 50); |
| 704 |
}); |
| 705 |
}); |
| 706 |
</script> |
| 707 |
<?php |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
add_action( 'init', [ 'Themify_Custom_Fonts', 'init' ] ); |
| 712 |
endif; |
| 713 |
|
| 714 |
/** |
| 715 |
* Displays the Font Weight field, converts deprecated option values for backward compat |
| 716 |
*/ |
| 717 |
function themify_meta_field_fontweight_dropdown($args) { |
| 718 |
if ('normal' === $args['meta_value']) { |
| 719 |
$args['meta_value'] = 400; |
| 720 |
} else if ('bold' === $args['meta_value']) { |
| 721 |
$args['meta_value'] = 700; |
| 722 |
} |
| 723 |
|
| 724 |
themify_meta_field_dropdown($args); |
| 725 |
} |
| 726 |
|