| 1 |
<?php |
| 2 |
/** |
| 3 |
* The public-facing functionality of the module. |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Modules/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* The public-facing functionality of the module. |
| 14 |
*/ |
| 15 |
class Powerkit_Typekit_Fonts_Public extends Powerkit_Module_Public { |
| 16 |
|
| 17 |
/** |
| 18 |
* Webfonts method |
| 19 |
* |
| 20 |
* @var string $load_method Webfonts method. |
| 21 |
*/ |
| 22 |
public $load_method = 'async'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize |
| 26 |
*/ |
| 27 |
public function initialize() { |
| 28 |
add_filter( 'language_attributes', array( $this, 'html_attributes' ), 10, 2 ); |
| 29 |
add_filter( 'powerkit_fonts_list', array( $this, 'typekit_fonts' ), 20 ); |
| 30 |
add_action( 'customize_controls_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) ); |
| 31 |
add_action( 'admin_enqueue_scripts', array( $this, 'editor_enqueue_scripts' ) ); |
| 32 |
add_filter( 'init', array( $this, 'set_load_method' ) ); |
| 33 |
add_filter( 'init', array( $this, 'kirki_support' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Set Webfonts method |
| 38 |
*/ |
| 39 |
public function set_load_method() { |
| 40 |
$this->load_method = apply_filters( 'powerkit_webfonts_load_method', 'async' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add typekit fonts |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @param array $fonts List fonts. |
| 48 |
*/ |
| 49 |
public function typekit_fonts( $fonts ) { |
| 50 |
|
| 51 |
if ( is_customize_preview() ) { |
| 52 |
|
| 53 |
$token = get_option( 'powerkit_typekit_fonts_token' ); |
| 54 |
$kit = get_option( 'powerkit_typekit_fonts_kit' ); |
| 55 |
|
| 56 |
if ( $token && $kit ) { |
| 57 |
|
| 58 |
$data = wp_cache_get( 'powerkit_typekit_fonts_kit_cache' ); |
| 59 |
|
| 60 |
if ( ! $data ) { |
| 61 |
$typekit = new Powerkit_Typekit_Api(); |
| 62 |
|
| 63 |
$data = $typekit->get( $kit, $token ); |
| 64 |
|
| 65 |
wp_cache_set( 'powerkit_typekit_fonts_kit_cache', $data, 'powerkit', 1 ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( $data && isset( $data['kit']['families'] ) && $data['kit']['families'] ) { |
| 69 |
|
| 70 |
$fonts['families']['typekit'] = array( |
| 71 |
'text' => esc_html__( 'Typekit', 'powerkit' ), |
| 72 |
'children' => array(), |
| 73 |
); |
| 74 |
|
| 75 |
foreach ( $data['kit']['families'] as $item ) { |
| 76 |
$id = $item['slug']; |
| 77 |
|
| 78 |
$fonts['families']['typekit']['children'][] = array( |
| 79 |
'id' => $item['slug'], |
| 80 |
'text' => $item['name'], |
| 81 |
); |
| 82 |
|
| 83 |
$fonts['variants'][ $id ] = powerkit_typekit_font_variations_format( $item['variations'] ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $fonts; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Filters the language attributes for display in the html tag. |
| 94 |
* |
| 95 |
* @param string $output A space-separated list of language attributes. |
| 96 |
* @param string $doctype The type of html document (xhtml|html). |
| 97 |
*/ |
| 98 |
public function html_attributes( $output, $doctype ) { |
| 99 |
$token = get_option( 'powerkit_typekit_fonts_token' ); |
| 100 |
$kit = get_option( 'powerkit_typekit_fonts_kit' ); |
| 101 |
|
| 102 |
if ( $token && $kit && 'async' === $this->load_method && ! is_admin() ) { |
| 103 |
$output .= ' class="wf-loading"'; |
| 104 |
} |
| 105 |
|
| 106 |
return $output; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Add support Kirki. |
| 111 |
*/ |
| 112 |
public function kirki_support() { |
| 113 |
if ( class_exists( 'CSCO_Kirki' ) ) { |
| 114 |
add_filter( 'csco_kirki_dynamic_css', array( $this, 'kirki_dynamic_css' ) ); |
| 115 |
} |
| 116 |
if ( class_exists( 'Kirki' ) ) { |
| 117 |
$configs = (array) Kirki::$config; |
| 118 |
|
| 119 |
foreach ( $configs as $config_id => $args ) { |
| 120 |
add_filter( "kirki_{$config_id}_dynamic_css", array( $this, 'kirki_dynamic_css' ) ); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Change font-family stack. |
| 127 |
* |
| 128 |
* @param string $style The dynamic css. |
| 129 |
*/ |
| 130 |
public function kirki_dynamic_css( $style ) { |
| 131 |
$token = get_option( 'powerkit_typekit_fonts_token' ); |
| 132 |
$kit = get_option( 'powerkit_typekit_fonts_kit' ); |
| 133 |
|
| 134 |
if ( $token && $kit ) { |
| 135 |
$typekit = new Powerkit_Typekit_Api(); |
| 136 |
|
| 137 |
$typekit_data = $typekit->get( $kit, $token ); |
| 138 |
if ( isset( $typekit_data['kit']['families'] ) && $typekit_data['kit']['families'] ) { |
| 139 |
foreach ( $typekit_data['kit']['families'] as $family ) { |
| 140 |
$slug = sprintf( 'font-family:%s', $family['slug'] ); |
| 141 |
$stack = sprintf( 'font-family:%s', $family['css_stack'] ); |
| 142 |
// Replace font slug to css stack. |
| 143 |
$style = str_replace( $slug, $stack, $style ); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return $style; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Register fonts in the editor. |
| 153 |
* |
| 154 |
* @param string $page Current page. |
| 155 |
*/ |
| 156 |
public function editor_enqueue_scripts( $page ) { |
| 157 |
|
| 158 |
if ( 'post.php' === $page || 'post-new.php' === $page ) { |
| 159 |
$this->wp_enqueue_scripts(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Register the stylesheets for the public-facing side of the site. |
| 165 |
*/ |
| 166 |
public function wp_enqueue_scripts() { |
| 167 |
$token = get_option( 'powerkit_typekit_fonts_token' ); |
| 168 |
$kit = get_option( 'powerkit_typekit_fonts_kit' ); |
| 169 |
|
| 170 |
if ( $token && $kit ) { |
| 171 |
if ( 'async' === $this->load_method ) { |
| 172 |
wp_enqueue_script( 'powerkit-typekit', plugin_dir_url( __FILE__ ) . 'js/public-powerkit-typekit.js', array( 'jquery' ), powerkit_get_setting( 'version' ), true ); |
| 173 |
|
| 174 |
wp_localize_script( 'powerkit-typekit', 'powerkit_typekit', array( |
| 175 |
'kit' => $kit, |
| 176 |
) ); |
| 177 |
} else { |
| 178 |
wp_enqueue_style( 'powerkit-typekit', sprintf( 'https://use.typekit.net/%s.css', $kit ), array(), powerkit_get_setting( 'version' ), 'all' ); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|