Advertisement
xpeed

class font load in front

Jun 11th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. <?php
  2. if ( !defined( 'FW' ) ) {
  3. die( 'Forbidden' );
  4. }
  5.  
  6. class Unyson_Google_Fonts {
  7.  
  8. static private $data = array(
  9. 'subset' => array(),
  10. 'family' => array(),
  11. );
  12.  
  13. public static function add_typography_v2( $value ) {
  14. if ( in_array( isset( $value[ 'google_font' ] ), array( true, 'true' ), true ) ) {
  15. self::$data[ 'family' ][ $value[ 'family' ] ][ 'variants' ][ (string) $value[ 'variation' ] ] = true;
  16. self::$data[ 'subset' ][ $value[ 'subset' ] ] = true;
  17. }
  18. }
  19.  
  20. public static function clear() {
  21. self::$data = array();
  22. }
  23.  
  24. public static function generate_url() {
  25. /**
  26. * Example:
  27. *
  28. * <link href="
  29. * https://fonts.googleapis.com/css?
  30. * family={Family}|{Family}:{variant},{variant}
  31. * &amp;
  32. * subset=cyrillic-ext,greek,vietnamese
  33. * " rel="stylesheet">
  34. */
  35. if ( empty( self::$data[ 'family' ] ) ) {
  36. return false;
  37. }
  38.  
  39. $query = array(
  40. 'family' => array(),
  41. 'subset' => implode( ',', array_keys( self::$data[ 'subset' ] ) ),
  42. );
  43.  
  44. foreach ( self::$data[ 'family' ] as $family => $family_data ) {
  45. if ( !empty( $family ) ) {
  46. $query[ 'family' ][] = $family . ':' . implode( ',', array_keys( $family_data[ 'variants' ] ) );
  47. }
  48. }
  49. $query[ 'family' ] = implode( '|', $query[ 'family' ] );
  50. return add_query_arg( 'family', urlencode( $query[ 'family' ] ), "https://fonts.googleapis.com/css" );
  51.  
  52. // return '//fonts.googleapis.com/css?' . build_query( $query );
  53. }
  54.  
  55. public static function output_url() {
  56. if ( $url = self::generate_url() ):
  57. ?><link href="<?php echo esc_attr( $url ) ?>" rel="stylesheet"><?php
  58. endif;
  59. }
  60.  
  61. }
  62.  
  63. add_action( 'wp_enqueue_scripts', array( 'Unyson_Google_Fonts', 'output_url' ), 9999 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement