Webfonts.php
161 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles webfonts. |
| 4 | * |
| 5 | * @package kirki-framework/module-webfonts |
| 6 | * @author Themeum |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Module; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | use Kirki\Compatibility\Values; |
| 19 | use Kirki\Compatibility\Kirki; |
| 20 | use Kirki\Module\Webfonts\Google; |
| 21 | use Kirki\Module\Webfonts\Embed; |
| 22 | use Kirki\Module\Webfonts\Async; |
| 23 | |
| 24 | /** |
| 25 | * The Webfonts object. |
| 26 | */ |
| 27 | class Webfonts { |
| 28 | |
| 29 | /** |
| 30 | * The class instance. |
| 31 | * |
| 32 | * @static |
| 33 | * @access private |
| 34 | * @since 1.0.0 |
| 35 | * @var object |
| 36 | */ |
| 37 | private static $instance; |
| 38 | |
| 39 | /** |
| 40 | * The Google object. |
| 41 | * |
| 42 | * @access protected |
| 43 | * @since 1.0.0 |
| 44 | * @var \Kirki\Module\Webfonts\Google |
| 45 | */ |
| 46 | protected $fonts_google; |
| 47 | |
| 48 | /** |
| 49 | * An array of fields to be processed. |
| 50 | * |
| 51 | * @static |
| 52 | * @access public |
| 53 | * @since 1.0.0 |
| 54 | * @var array |
| 55 | */ |
| 56 | public static $fields = []; |
| 57 | |
| 58 | /** |
| 59 | * Get the one, true instance of this class. |
| 60 | * |
| 61 | * @static |
| 62 | * @access public |
| 63 | * @since 1.0.0 |
| 64 | * @return object |
| 65 | */ |
| 66 | public static function get_instance() { |
| 67 | if ( null === self::$instance ) { |
| 68 | self::$instance = new self(); |
| 69 | } |
| 70 | return self::$instance; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * The class constructor |
| 75 | * |
| 76 | * @access public |
| 77 | * @since 1.0.0 |
| 78 | */ |
| 79 | public function __construct() { |
| 80 | add_action( 'kirki_field_init', [ $this, 'field_init' ], 10, 2 ); |
| 81 | add_action( 'wp_loaded', [ $this, 'run' ] ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Run on after_setup_theme. |
| 86 | * |
| 87 | * @access public |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | public function run() { |
| 91 | $this->fonts_google = Google::get_instance(); |
| 92 | $this->init(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Init other objects depending on the method we'll be using. |
| 97 | * |
| 98 | * @access protected |
| 99 | * @since 1.0.0 |
| 100 | */ |
| 101 | protected function init() { |
| 102 | foreach ( array_keys( Kirki::$config ) as $config_id ) { |
| 103 | if ( 'async' === $this->get_method() ) { |
| 104 | new Async( $config_id, $this, $this->fonts_google ); |
| 105 | } |
| 106 | new Embed( $config_id, $this, $this->fonts_google ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the method we're going to use. |
| 112 | * |
| 113 | * @access public |
| 114 | * @since 1.0.0 |
| 115 | * @deprecated in 3.0.36. |
| 116 | * @return string |
| 117 | */ |
| 118 | public function get_method() { |
| 119 | return ( is_customize_preview() || is_admin() ) ? 'async' : 'embed'; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Runs when a field gets added. |
| 124 | * Adds fields to this object so we can loop through them. |
| 125 | * |
| 126 | * @access public |
| 127 | * @since 1.0.0 |
| 128 | * @param array $args The field args. |
| 129 | * @param Object $object The field object. |
| 130 | * @return void |
| 131 | */ |
| 132 | public function field_init( $args, $object ) { |
| 133 | if ( ! isset( $args['type'] ) && isset( $object->type ) ) { |
| 134 | $args['type'] = $object->type; |
| 135 | } |
| 136 | |
| 137 | if ( ! isset( $args['type'] ) || $args['type'] !== 'kirki-typography' ) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // Use the settings ID as key: |
| 142 | self::$fields[ $args['settings'] ] = $args; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Goes through all our fields and then populates the $this->fonts property. |
| 148 | * |
| 149 | * @access public |
| 150 | * @param string $config_id The config-ID. |
| 151 | */ |
| 152 | public function loop_fields( $config_id ) { |
| 153 | foreach ( self::$fields as $field ) { |
| 154 | if ( isset( $field['kirki_config'] ) && $config_id !== $field['kirki_config'] ) { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | $this->fonts_google->generate_google_font( $field ); |
| 159 | } |
| 160 | } |
| 161 | } |