| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fonts for typography component |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 9 |
|
| 10 |
/** |
| 11 |
* GhostKit_Fonts |
| 12 |
*/ |
| 13 |
class GhostKit_Fonts { |
| 14 |
/** |
| 15 |
* GhostKit_Fonts constructor. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'init', array( $this, 'enqueue_fonts' ), 20 ); |
| 19 |
add_filter( 'gkt_fonts_list', array( $this, 'add_default_site_fonts' ), 9 ); |
| 20 |
add_filter( 'gkt_fonts_list', array( $this, 'add_google_fonts' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Enqueue fonts. |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function enqueue_fonts() { |
| 29 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_all_fonts_assets' ), 12 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Enqueue frontend & editor assets |
| 34 |
*/ |
| 35 |
public function enqueue_all_fonts_assets() { |
| 36 |
$fonts = $this->get_font_loader_list(); |
| 37 |
|
| 38 |
if ( ( is_admin() || ! empty( $fonts ) ) && isset( $fonts['google-fonts'] ) && ! empty( $fonts['google-fonts'] ) ) { |
| 39 |
$families = array(); |
| 40 |
|
| 41 |
foreach ( $fonts['google-fonts'] as $font => $font_data ) { |
| 42 |
$family = $font; |
| 43 |
|
| 44 |
if ( isset( $font_data['widths'] ) && ! empty( $font_data['widths'] ) ) { |
| 45 |
$weights = array(); |
| 46 |
|
| 47 |
foreach ( $font_data['widths'] as $weight ) { |
| 48 |
$weights[] = $weight; |
| 49 |
} |
| 50 |
|
| 51 |
$family .= ':' . implode( ',', $weights ); |
| 52 |
} |
| 53 |
|
| 54 |
$families[] = $family; |
| 55 |
} |
| 56 |
|
| 57 |
$query_args = array( |
| 58 |
'family' => implode( '|', $families ), |
| 59 |
'display' => 'swap', |
| 60 |
); |
| 61 |
|
| 62 |
wp_enqueue_style( 'ghostkit-fonts-google', add_query_arg( $query_args, 'https://fonts.googleapis.com/css' ), array(), GHOSTKIT_VERSION ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Create Font Loader List for webfont-loader. |
| 68 |
* |
| 69 |
* @return array - Font Loader List. |
| 70 |
*/ |
| 71 |
public function get_font_loader_list() { |
| 72 |
$post_id = null; |
| 73 |
$fonts_list = apply_filters( 'gkt_fonts_list', array() ); |
| 74 |
$default_typography = apply_filters( 'gkt_custom_typography', array() ); |
| 75 |
$fonts = array(); |
| 76 |
$unique_fonts = array(); |
| 77 |
$webfont_list = array(); |
| 78 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; |
| 79 |
$is_admin_editor = is_admin() && $screen && $screen->is_block_editor; |
| 80 |
|
| 81 |
if ( is_singular() ) { |
| 82 |
$post_id = get_the_ID(); |
| 83 |
} elseif ( $is_admin_editor ) { |
| 84 |
global $post; |
| 85 |
$post_id = isset( $post->ID ) ? $post->ID : null; |
| 86 |
} |
| 87 |
|
| 88 |
$is_single = is_singular() && $post_id; |
| 89 |
$is_admin_editor = $is_admin_editor && $post_id; |
| 90 |
$additional_font_weights = array(); |
| 91 |
|
| 92 |
// Default Typography. |
| 93 |
if ( ! empty( $default_typography ) ) { |
| 94 |
// Global Typography. |
| 95 |
$global_typography = get_option( 'ghostkit_typography', array() ); |
| 96 |
if ( isset( $global_typography['ghostkit_typography'] ) && $global_typography['ghostkit_typography'] ) { |
| 97 |
$global_typography = json_decode( $global_typography['ghostkit_typography'], true ); |
| 98 |
} |
| 99 |
if ( ! is_array( $global_typography ) ) { |
| 100 |
$global_typography = array(); |
| 101 |
} |
| 102 |
|
| 103 |
// Meta post Typography. |
| 104 |
$meta_typography = array(); |
| 105 |
if ( $is_single || $is_admin_editor ) { |
| 106 |
$meta_typography = get_post_meta( $post_id, 'ghostkit_typography', true ); |
| 107 |
|
| 108 |
if ( ! empty( $meta_typography ) ) { |
| 109 |
$meta_typography = json_decode( $meta_typography, true ); |
| 110 |
} |
| 111 |
if ( ! is_array( $meta_typography ) ) { |
| 112 |
$meta_typography = array(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// Go through all defaults and prepare full fonts list. |
| 117 |
foreach ( $default_typography as $key => $typography ) { |
| 118 |
$result = array(); |
| 119 |
|
| 120 |
// Default data. |
| 121 |
if ( |
| 122 |
isset( $typography['defaults']['font-family'] ) && |
| 123 |
! empty( $typography['defaults']['font-family'] ) && |
| 124 |
isset( $typography['defaults']['font-family-category'] ) && |
| 125 |
! empty( $typography['defaults']['font-family-category'] ) |
| 126 |
) { |
| 127 |
$weight = ''; |
| 128 |
if ( isset( $typography['defaults']['font-weight'] ) && ! empty( $typography['defaults']['font-weight'] ) ) { |
| 129 |
$weight = $typography['defaults']['font-weight']; |
| 130 |
} |
| 131 |
|
| 132 |
// TODO: add additional_font_weights option support for typography React component. |
| 133 |
if ( isset( $typography['additional_font_weights'] ) && ! empty( $typography['additional_font_weights'] ) ) { |
| 134 |
$additional_font_weights[ $key ] = $typography['additional_font_weights']; |
| 135 |
} |
| 136 |
|
| 137 |
$result = array( |
| 138 |
'family' => $typography['defaults']['font-family-category'], |
| 139 |
'label' => $typography['defaults']['font-family'], |
| 140 |
'weight' => $weight, |
| 141 |
'typography' => $key, |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
// Global data. |
| 146 |
if ( |
| 147 |
isset( $global_typography[ $key ]['fontFamily'] ) && |
| 148 |
! empty( $global_typography[ $key ]['fontFamily'] ) && |
| 149 |
isset( $global_typography[ $key ]['fontFamilyCategory'] ) && |
| 150 |
! empty( $global_typography[ $key ]['fontFamilyCategory'] ) |
| 151 |
) { |
| 152 |
$weight = ''; |
| 153 |
if ( isset( $global_typography[ $key ]['fontWeight'] ) && ! empty( $global_typography[ $key ]['fontWeight'] ) ) { |
| 154 |
$weight = $global_typography[ $key ]['fontWeight']; |
| 155 |
} |
| 156 |
|
| 157 |
$result = array( |
| 158 |
'family' => $global_typography[ $key ]['fontFamilyCategory'], |
| 159 |
'label' => $global_typography[ $key ]['fontFamily'], |
| 160 |
'weight' => $weight, |
| 161 |
'typography' => $key, |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
// Meta data. |
| 166 |
if ( |
| 167 |
isset( $meta_typography[ $key ]['fontFamily'] ) && |
| 168 |
! empty( $meta_typography[ $key ]['fontFamily'] ) && |
| 169 |
isset( $meta_typography[ $key ]['fontFamilyCategory'] ) && |
| 170 |
! empty( $meta_typography[ $key ]['fontFamilyCategory'] ) |
| 171 |
) { |
| 172 |
$weight = ''; |
| 173 |
if ( isset( $meta_typography[ $key ]['fontWeight'] ) && ! empty( $meta_typography[ $key ]['fontWeight'] ) ) { |
| 174 |
$weight = $meta_typography[ $key ]['fontWeight']; |
| 175 |
} |
| 176 |
|
| 177 |
$result = array( |
| 178 |
'family' => $meta_typography[ $key ]['fontFamilyCategory'], |
| 179 |
'label' => $meta_typography[ $key ]['fontFamily'], |
| 180 |
'weight' => $weight, |
| 181 |
'typography' => $key, |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
// Save result. |
| 186 |
if ( isset( $result['family'] ) && $result['family'] && isset( $result['label'] ) && $result['label'] ) { |
| 187 |
$fonts[ $key ] = $result; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// clear array to unique. |
| 193 |
$unique_fonts = array_map( 'unserialize', array_unique( array_map( 'serialize', $fonts ) ) ); |
| 194 |
|
| 195 |
foreach ( $unique_fonts as $font ) { |
| 196 |
if ( isset( $font['family'] ) && ! empty( $font['family'] ) && array_key_exists( $font['family'], $fonts_list ) ) { |
| 197 |
foreach ( $fonts_list[ $font['family'] ]['fonts'] as $find_font ) { |
| 198 |
if ( $font['label'] === $find_font['name'] ) { |
| 199 |
$weights = array(); |
| 200 |
$weight = ( isset( $font['weight'] ) && ! empty( $font['weight'] ) ) ? $font['weight'] : ''; |
| 201 |
$widths = ( isset( $find_font['widths'] ) && ! empty( $find_font['widths'] ) ) ? $find_font['widths'] : array(); |
| 202 |
$category = ( isset( $find_font['category'] ) && ! empty( $find_font['category'] ) ) ? $find_font['category'] : ''; |
| 203 |
$subsets = ( isset( $find_font['subsets'] ) && ! empty( $find_font['subsets'] ) ) ? $find_font['subsets'] : ''; |
| 204 |
|
| 205 |
if ( isset( $additional_font_weights ) && ! empty( $additional_font_weights ) ) { |
| 206 |
foreach ( $additional_font_weights as $key => $additional_font_weight ) { |
| 207 |
if ( $key === $font['typography'] ) { |
| 208 |
$font_weights = $additional_font_weight; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if ( isset( $font_weights ) && ! empty( $font_weights ) && is_array( $font_weights ) ) { |
| 214 |
$insert_weights = array(); |
| 215 |
if ( '' !== $weight ) { |
| 216 |
$weight = str_replace( 'i', '', $weight ); |
| 217 |
$insert_weights[] = $weight; |
| 218 |
$insert_weights[] = $weight . 'i'; |
| 219 |
} |
| 220 |
$insert_weights = array_merge( $insert_weights, $font_weights ); |
| 221 |
foreach ( $insert_weights as $insert_weight ) { |
| 222 |
if ( array_search( $insert_weight, $widths, true ) !== false ) { |
| 223 |
$weights[] = $insert_weight; |
| 224 |
} |
| 225 |
} |
| 226 |
} elseif ( '' !== $weight ) { |
| 227 |
$weight = str_replace( 'i', '', $weight ); |
| 228 |
|
| 229 |
if ( '600' !== $weight && |
| 230 |
'700' !== $weight && |
| 231 |
'800' !== $weight && |
| 232 |
'900' !== $weight ) { |
| 233 |
|
| 234 |
$insert_weights = array( |
| 235 |
$weight, |
| 236 |
$weight . 'i', |
| 237 |
'700', |
| 238 |
'700i', |
| 239 |
); |
| 240 |
|
| 241 |
foreach ( $insert_weights as $insert_weight ) { |
| 242 |
if ( array_search( $insert_weight, $widths, true ) !== false ) { |
| 243 |
$weights[] = $insert_weight; |
| 244 |
} |
| 245 |
} |
| 246 |
} else { |
| 247 |
$insert_weights = array( |
| 248 |
$weight, |
| 249 |
$weight . 'i', |
| 250 |
); |
| 251 |
|
| 252 |
foreach ( $insert_weights as $insert_weight ) { |
| 253 |
if ( array_search( $insert_weight, $widths, true ) !== false ) { |
| 254 |
$weights[] = $insert_weight; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} else { |
| 259 |
$insert_weights = array( |
| 260 |
'400', |
| 261 |
'400i', |
| 262 |
'700', |
| 263 |
'700i', |
| 264 |
); |
| 265 |
|
| 266 |
foreach ( $insert_weights as $insert_weight ) { |
| 267 |
if ( array_search( $insert_weight, $widths, true ) !== false ) { |
| 268 |
$weights[] = $insert_weight; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if ( |
| 274 |
isset( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'] ) && |
| 275 |
! empty( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'] ) && |
| 276 |
is_array( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'] ) |
| 277 |
) { |
| 278 |
$weights = array_values( array_unique( array_merge_recursive( $webfont_list[ $font['family'] ][ $font['label'] ]['widths'], $weights ) ) ); |
| 279 |
} |
| 280 |
|
| 281 |
$webfont_list[ $font['family'] ][ $font['label'] ] = array( |
| 282 |
'widths' => $weights, |
| 283 |
'category' => $category, |
| 284 |
'subsets' => $subsets, |
| 285 |
); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return $webfont_list; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Get Default Site Font. |
| 296 |
* |
| 297 |
* @return array |
| 298 |
*/ |
| 299 |
private function get_default_site_font() { |
| 300 |
return array( |
| 301 |
'name' => 'Default Site Font', |
| 302 |
'widths' => array( |
| 303 |
'', |
| 304 |
'400', |
| 305 |
'700', |
| 306 |
), |
| 307 |
'category' => 'sans-serif', |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Add Default fonts list. |
| 313 |
* |
| 314 |
* @param array $fonts - fonts list. |
| 315 |
* |
| 316 |
* @return array |
| 317 |
*/ |
| 318 |
public function add_default_site_fonts( $fonts ) { |
| 319 |
|
| 320 |
$fonts['default'] = array( |
| 321 |
'name' => __( 'Default Fonts Site', 'ghostkit' ), |
| 322 |
'fonts' => array( |
| 323 |
$this->get_default_site_font(), |
| 324 |
), |
| 325 |
); |
| 326 |
return $fonts; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Add Google fonts list. |
| 331 |
* |
| 332 |
* @link https://developers.google.com/fonts/docs/developer_api?apix_params=%7B%22alt%22%3A%22json%22%2C%22fields%22%3A%22items(family%2Ckind%2Ccategory%2Cvariants%2Csubsets%2Cversion%2ClastModified)%22%2C%22prettyPrint%22%3Afalse%7D - get new fonts. |
| 333 |
* |
| 334 |
* @param array $fonts - fonts list. |
| 335 |
* |
| 336 |
* @return array |
| 337 |
*/ |
| 338 |
public function add_google_fonts( $fonts ) { |
| 339 |
$result = get_transient( 'ghostkit_google_fonts_list' ); |
| 340 |
|
| 341 |
if ( ! $result ) { |
| 342 |
$result = array(); |
| 343 |
$fonts_json = file_get_contents( ghostkit()->plugin_path . 'classes/google-fonts/webfonts.json' ); // phpcs:ignore |
| 344 |
$fonts_object = json_decode( $fonts_json, true ); |
| 345 |
|
| 346 |
foreach ( $fonts_object['items'] as $font ) { |
| 347 |
$weights = array(); |
| 348 |
foreach ( $font['variants'] as $variant ) { |
| 349 |
$variant = str_replace( 'italic', 'i', $variant ); |
| 350 |
|
| 351 |
switch ( $variant ) { |
| 352 |
case 'i': |
| 353 |
$variant = '400i'; |
| 354 |
break; |
| 355 |
case 'regular': |
| 356 |
$variant = '400'; |
| 357 |
break; |
| 358 |
} |
| 359 |
|
| 360 |
$weights[] = $variant; |
| 361 |
} |
| 362 |
$result[] = array( |
| 363 |
'name' => $font['family'], |
| 364 |
'widths' => $weights, |
| 365 |
'category' => $font['category'], |
| 366 |
'subsets' => $font['subsets'], |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
if ( ! empty( $result ) ) { |
| 371 |
set_transient( 'ghostkit_google_fonts_list', $result, DAY_IN_SECONDS ); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
$result[] = $this->get_default_site_font(); |
| 376 |
|
| 377 |
$fonts['google-fonts'] = array( |
| 378 |
'name' => esc_attr__( 'Google Fonts', 'ghostkit' ), |
| 379 |
'fonts' => $result, |
| 380 |
); |
| 381 |
|
| 382 |
return $fonts; |
| 383 |
} |
| 384 |
} |
| 385 |
new GhostKit_Fonts(); |
| 386 |
|