PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.0
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / packages / utils / googlefonts / src / GoogleFonts.php
kirki / customizer / packages / utils / googlefonts / src Last commit date
GoogleFonts.php 2 months ago webfont-files.json 2 months ago webfont-names.json 5 months ago webfonts.json 5 months ago
GoogleFonts.php
191 lines
1 <?php
2 /**
3 * Processes typography-related fields
4 * and generates the google-font link.
5 *
6 * @package kirki-framework/googlefonts
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;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Manages the way Google Fonts are enqueued.
21 */
22 final class GoogleFonts {
23
24 /**
25 * An array of our google fonts.
26 *
27 * @static
28 * @access public
29 * @since 1.0.0
30 * @var array
31 */
32 public static $google_fonts;
33
34 /**
35 * An array of our google font names.
36 *
37 * @static
38 * @access public
39 * @since 1.0.2
40 * @var array
41 */
42 public static $google_font_names;
43
44 /**
45 * The class constructor.
46 *
47 * @access public
48 * @since 1.0.0
49 */
50 public function __construct() {
51 add_action( 'wp_ajax_kirki_fonts_google_all_get', [ $this, 'print_googlefonts_json' ] );
52 add_action( 'wp_ajax_nopriv_kirki_fonts_google_all_get', [ $this, 'print_googlefonts_json' ] );
53 }
54
55 /**
56 * Prints the googlefonts JSON file.
57 *
58 * @since 1.0.0
59 * @param bool $die Whether the script should exit or not.
60 * @return void
61 */
62 public function print_googlefonts_json( $die = true ) {
63 include 'webfonts.json'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
64 if ( function_exists( 'wp_die' ) && $die ) {
65 wp_die();
66 }
67 }
68
69 /**
70 * Returns the array of googlefonts from the JSON file.
71 *
72 * @since 1.0.0
73 * @return array
74 */
75 public function get_array() {
76 ob_start();
77 include 'webfonts.json'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
78 return json_decode( ob_get_clean(), true );
79 }
80
81 /**
82 * Returns the array of googlefont names from the JSON file.
83 *
84 * @since 1.0.2
85 * @return array
86 */
87 public function get_names_array() {
88 ob_start();
89 include 'webfont-names.json'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
90 return json_decode( ob_get_clean(), true );
91 }
92
93 /**
94 * Return an array of all available Google Fonts.
95 *
96 * @access public
97 * @since 1.0.0
98 * @return array All Google Fonts.
99 */
100 public function get_google_fonts() {
101
102 // Get fonts from cache.
103 self::$google_fonts = get_site_transient( 'kirki_googlefonts_cache' );
104
105 // If cache is populated, return cached fonts array.
106 if ( self::$google_fonts ) {
107 return self::$google_fonts;
108 }
109
110 // If we got this far, cache was empty so we need to get from JSON.
111 $fonts = $this->get_array();
112
113 self::$google_fonts = [];
114 if ( is_array( $fonts ) ) {
115 foreach ( $fonts['items'] as $font ) {
116 self::$google_fonts[ $font['family'] ] = [
117 'label' => $font['family'],
118 'variants' => $font['variants'],
119 'category' => $font['category'],
120 ];
121 }
122 }
123
124 // Apply the 'kirki_fonts_google_fonts' filter.
125 self::$google_fonts = apply_filters( 'kirki_fonts_google_fonts', self::$google_fonts );
126
127 // Save the array in cache.
128 $cache_time = apply_filters( 'kirki_googlefonts_transient_time', HOUR_IN_SECONDS );
129 set_site_transient( 'kirki_googlefonts_cache', self::$google_fonts, $cache_time );
130
131 return self::$google_fonts;
132 }
133
134 /**
135 * Return an array of all available Google Font names.
136 *
137 * @access public
138 * @since 1.0.2
139 * @return array All Google Font names.
140 */
141 public function get_google_font_names() {
142
143 // Get fonts from cache.
144 self::$google_font_names = get_site_transient( 'kirki_googlefont_names_cache' );
145
146 // If cache is populated, return cached fonts array.
147 if ( self::$google_font_names ) {
148 return self::$google_font_names;
149 }
150
151 // If we got this far, cache was empty so we need to get from JSON.
152 self::$google_font_names = $this->get_names_array();
153
154 // Apply the 'kirki_fonts_google_font_names' filter.
155 self::$google_font_names = apply_filters( 'kirki_fonts_google_font_names', self::$google_font_names );
156
157 // Save the array in cache.
158 $cache_time = apply_filters( 'kirki_googlefont_names_transient_time', HOUR_IN_SECONDS );
159 set_site_transient( 'kirki_googlefont_names_cache', self::$google_font_names, $cache_time );
160
161 return self::$google_font_names;
162 }
163
164 /**
165 * Returns an array of google-fonts matching our arguments.
166 *
167 * @access public
168 * @since 1.0.0
169 * @param array $args The arguments.
170 * @return array
171 */
172 public function get_google_fonts_by_args( $args = [] ) {
173 $cache_name = 'kirki_googlefonts_' . md5( wp_json_encode( $args ) );
174 $cache = get_site_transient( $cache_name );
175 if ( $cache ) {
176 return $cache;
177 }
178
179 $args['sort'] = isset( $args['sort'] ) ? $args['sort'] : 'alpha';
180
181 $fonts = $this->get_array();
182 $ordered_fonts = $fonts['order'][ $args['sort'] ];
183 if ( isset( $args['count'] ) ) {
184 $ordered_fonts = array_slice( $ordered_fonts, 0, $args['count'] );
185 set_site_transient( $cache_name, $ordered_fonts, HOUR_IN_SECONDS );
186 return $ordered_fonts;
187 }
188 set_site_transient( $cache_name, $ordered_fonts, HOUR_IN_SECONDS );
189 return $ordered_fonts;
190 }
191 }