Async.php
2 months ago
Downloader.php
5 months ago
Embed.php
2 months ago
Fonts.php
5 months ago
Google.php
2 months ago
Google.php
247 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Processes typography-related fields |
| 4 | * and generates the google-font link. |
| 5 | * |
| 6 | * @package kirki-framework/module-webfonts |
| 7 | * @author Themeum |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Kirki\Module\Webfonts; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | use Kirki\Module\Webfonts\Fonts; |
| 20 | use Kirki\GoogleFonts; |
| 21 | |
| 22 | /** |
| 23 | * Manages the way Google Fonts are enqueued. |
| 24 | */ |
| 25 | final class Google { |
| 26 | |
| 27 | /** |
| 28 | * The object instance. |
| 29 | * We use the singleton pattern here to avoid loading the google-font array multiple times. |
| 30 | * This is mostly a performance tweak. |
| 31 | * |
| 32 | * @access private |
| 33 | * @var null|object |
| 34 | */ |
| 35 | private static $instance = null; |
| 36 | |
| 37 | /** |
| 38 | * DUMMY. DOESN'T DO ANYTHING, SIMPLY BACKWARDS-COMPATIBILITY. |
| 39 | * |
| 40 | * @static |
| 41 | * @access public |
| 42 | * @var bool |
| 43 | */ |
| 44 | public static $force_load_all_subsets = false; |
| 45 | |
| 46 | /** |
| 47 | * If set to true, forces loading ALL variants. |
| 48 | * |
| 49 | * @static |
| 50 | * @access public |
| 51 | * @var bool |
| 52 | */ |
| 53 | public static $force_load_all_variants = false; |
| 54 | |
| 55 | /** |
| 56 | * The array of fonts |
| 57 | * |
| 58 | * @access public |
| 59 | * @var array |
| 60 | */ |
| 61 | public $fonts = []; |
| 62 | |
| 63 | /** |
| 64 | * An array of all google fonts. |
| 65 | * |
| 66 | * @access private |
| 67 | * @var array |
| 68 | */ |
| 69 | private $google_fonts = []; |
| 70 | |
| 71 | /** |
| 72 | * An array of fonts that should be hosted locally instead of served via the google-CDN. |
| 73 | * |
| 74 | * @access protected |
| 75 | * @since 1.0.0 |
| 76 | * @var array |
| 77 | */ |
| 78 | protected $hosted_fonts = []; |
| 79 | |
| 80 | /** |
| 81 | * The class constructor. |
| 82 | */ |
| 83 | private function __construct() { |
| 84 | $config = apply_filters( 'kirki_config', [] ); |
| 85 | |
| 86 | // If we have set $config['disable_google_fonts'] to true then do not proceed any further. |
| 87 | if ( isset( $config['disable_google_fonts'] ) && true === $config['disable_google_fonts'] ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | new GoogleFonts(); |
| 92 | add_action( 'wp_ajax_kirki_fonts_standard_all_get', [ $this, 'get_standardfonts_json' ] ); |
| 93 | add_action( 'wp_ajax_nopriv_kirki_fonts_standard_all_get', [ $this, 'get_standardfonts_json' ] ); |
| 94 | |
| 95 | // Populate the array of google fonts. |
| 96 | $this->google_fonts = Fonts::get_google_fonts(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the one, true instance of this class. |
| 101 | * Prevents performance issues since this is only loaded once. |
| 102 | * |
| 103 | * @return object Google |
| 104 | */ |
| 105 | public static function get_instance() { |
| 106 | if ( null === self::$instance ) { |
| 107 | self::$instance = new self(); |
| 108 | } |
| 109 | return self::$instance; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Processes the arguments of a field |
| 114 | * determines if it's a typography field |
| 115 | * and if it is, then takes appropriate actions. |
| 116 | * |
| 117 | * @param array $args The field arguments. |
| 118 | */ |
| 119 | public function generate_google_font( $args ) { |
| 120 | |
| 121 | // Process typography fields. |
| 122 | $process = ( ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) || ( isset( $args['choices'] ) && isset( $args['choices']['parent_type'] ) && 'kirki-typography' === $args['choices']['parent_type'] ) ); |
| 123 | if ( apply_filters( 'kirki_generate_google_font', $process, $args ) ) { |
| 124 | |
| 125 | // Get the value. |
| 126 | $option_type = ( isset( $args['option_type'] ) ) ? $args['option_type'] : 'theme_mod'; |
| 127 | $default = ( isset( $args['default'] ) ) ? $args['default'] : ''; |
| 128 | $value = apply_filters( 'kirki_get_value', get_theme_mod( $args['settings'], $default ), $args['settings'], $default, $option_type ); |
| 129 | |
| 130 | // If we don't have a font-family then we can skip this. |
| 131 | if ( ! isset( $value['font-family'] ) || in_array( $value['font-family'], $this->hosted_fonts, true ) ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // If not a google-font, then we can skip this. |
| 136 | if ( ! isset( $value['font-family'] ) || ! Fonts::is_google_font( $value['font-family'] ) ) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // Set a default value for variants. |
| 141 | if ( ! isset( $value['variant'] ) ) { |
| 142 | $value['variant'] = 'regular'; |
| 143 | } |
| 144 | |
| 145 | // Add the requested google-font. |
| 146 | if ( ! is_array( $value ) || ! isset( $value['font-family'] ) || ! isset( $this->fonts[ $value['font-family'] ] ) ) { |
| 147 | $this->fonts[ $value['font-family'] ] = []; |
| 148 | } |
| 149 | if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) { |
| 150 | $this->fonts[ $value['font-family'] ][] = $value['variant']; |
| 151 | } |
| 152 | |
| 153 | // Are we force-loading all variants? |
| 154 | if ( true === self::$force_load_all_variants ) { |
| 155 | $all_variants = Fonts::get_all_variants(); |
| 156 | $args['choices']['variant'] = array_keys( $all_variants ); |
| 157 | } |
| 158 | |
| 159 | if ( ! empty( $args['choices']['variant'] ) && is_array( $args['choices']['variant'] ) ) { |
| 160 | foreach ( $args['choices']['variant'] as $extra_variant ) { |
| 161 | $this->fonts[ $value['font-family'] ][] = $extra_variant; |
| 162 | } |
| 163 | } |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | // Process non-typography fields. |
| 168 | if ( isset( $args['output'] ) && is_array( $args['output'] ) ) { |
| 169 | foreach ( $args['output'] as $output ) { |
| 170 | |
| 171 | // If we don't have a typography-related output argument we can skip this. |
| 172 | if ( ! isset( $output['property'] ) || ! in_array( $output['property'], [ 'font-family', 'font-weight' ], true ) ) { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | // Get the value. |
| 177 | $option_type = ( isset( $args['option_type'] ) ) ? $args['option_type'] : 'theme_mod'; |
| 178 | $default = ( isset( $args['default'] ) ) ? $args['default'] : ''; |
| 179 | $value = apply_filters( 'kirki_get_value', get_theme_mod( $args['settings'], $default ), $args['settings'], $default, $option_type ); |
| 180 | |
| 181 | if ( is_string( $value ) ) { |
| 182 | if ( 'font-family' === $output['property'] ) { |
| 183 | if ( ! array_key_exists( $value, $this->fonts ) ) { |
| 184 | $this->fonts[ $value ] = []; |
| 185 | } |
| 186 | } elseif ( 'font-weight' === $output['property'] ) { |
| 187 | foreach ( $this->fonts as $font => $variants ) { |
| 188 | if ( ! in_array( $value, $variants, true ) ) { |
| 189 | $this->fonts[ $font ][] = $value; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Determines the vbalidity of the selected font as well as its properties. |
| 200 | * This is vital to make sure that the google-font script that we'll generate later |
| 201 | * does not contain any invalid options. |
| 202 | */ |
| 203 | public function process_fonts() { |
| 204 | |
| 205 | // Early exit if font-family is empty. |
| 206 | if ( empty( $this->fonts ) ) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | foreach ( $this->fonts as $font => $variants ) { |
| 211 | |
| 212 | // Determine if this is indeed a google font or not. |
| 213 | // If it's not, then just remove it from the array. |
| 214 | if ( ! array_key_exists( $font, $this->google_fonts ) ) { |
| 215 | unset( $this->fonts[ $font ] ); |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | // Get all valid font variants for this font. |
| 220 | $font_variants = []; |
| 221 | if ( isset( $this->google_fonts[ $font ]['variants'] ) ) { |
| 222 | $font_variants = $this->google_fonts[ $font ]['variants']; |
| 223 | } |
| 224 | foreach ( $variants as $variant ) { |
| 225 | |
| 226 | // If this is not a valid variant for this font-family |
| 227 | // then unset it and move on to the next one. |
| 228 | if ( ! in_array( strval( $variant ), $font_variants, true ) ) { |
| 229 | $variant_key = array_search( $variant, $this->fonts[ $font ], true ); |
| 230 | unset( $this->fonts[ $font ][ $variant_key ] ); |
| 231 | continue; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the standard fonts JSON. |
| 239 | * |
| 240 | * @since 1.0.0 |
| 241 | * @return void |
| 242 | */ |
| 243 | public function get_standardfonts_json() { |
| 244 | echo wp_json_encode( Fonts::get_standard_fonts() ); |
| 245 | wp_die(); |
| 246 | } |
| 247 | } |