This commit is contained in:
Alexander Agnarson 2020-02-25 18:04:54 +01:00
parent 62def2a89b
commit 7ad333b360
167 changed files with 2955 additions and 2493 deletions

View file

@ -7,7 +7,7 @@
* @category Core
* @author Ari Stathopoulos (@aristath)
* @copyright Copyright (c) 2019, Ari Stathopoulos (@aristath)
* @license https://opensource.org/licenses/MIT
* @license https://opensource.org/licenses/MIT
* @since 1.0
*/
@ -213,7 +213,7 @@ final class Kirki_Fonts_Google {
// If this is not a valid variant for this font-family
// then unset it and move on to the next one.
if ( ! in_array( $variant, $font_variants, true ) ) {
if ( ! in_array( strval( $variant ), $font_variants, true ) ) {
$variant_key = array_search( $variant, $this->fonts[ $font ], true );
unset( $this->fonts[ $font ][ $variant_key ] );
continue;

View file

@ -96,6 +96,11 @@ final class Kirki_Fonts_Helper {
*/
public static function download_font_file( $url ) {
$saved_fonts = get_option( 'kirki_font_local_filenames', array() );
if ( isset( $saved_fonts[ $url ] ) && file_exists( $saved_fonts[ $url ]['file'] ) ) {
return $saved_fonts[ $url ]['url'];
}
// Gives us access to the download_url() and wp_handle_sideload() functions.
require_once ABSPATH . 'wp-admin/includes/file.php';
@ -118,6 +123,7 @@ final class Kirki_Fonts_Helper {
);
$overrides = array(
'test_type' => false,
'test_form' => false,
'test_size' => true,
);
@ -126,6 +132,8 @@ final class Kirki_Fonts_Helper {
$results = wp_handle_sideload( $file, $overrides );
if ( empty( $results['error'] ) ) {
$saved_fonts[ $url ] = $results;
update_option( 'kirki_font_local_filenames', $saved_fonts );
return $results['url'];
}
return false;

View file

@ -6,7 +6,7 @@
* @category Core
* @author Ari Stathopoulos (@aristath)
* @copyright Copyright (c) 2019, Ari Stathopoulos (@aristath)
* @license https://opensource.org/licenses/MIT
* @license https://opensource.org/licenses/MIT
* @since 1.0
*/
@ -96,22 +96,6 @@ final class Kirki_Fonts {
return apply_filters( 'kirki_fonts_standard_fonts', $standard_fonts );
}
/**
* Return an array of backup fonts based on the font-category
*
* @return array
*/
public static function get_backup_fonts() {
$backup_fonts = array(
'sans-serif' => 'Helvetica, Arial, sans-serif',
'serif' => 'Georgia, serif',
'display' => '"Comic Sans MS", cursive, sans-serif',
'handwriting' => '"Comic Sans MS", cursive, sans-serif',
'monospace' => '"Lucida Console", Monaco, monospace',
);
return apply_filters( 'kirki_fonts_backup_fonts', $backup_fonts );
}
/**
* Return an array of all available Google Fonts.
*
@ -122,8 +106,13 @@ final class Kirki_Fonts {
// Get fonts from cache.
self::$google_fonts = get_site_transient( 'kirki_googlefonts_cache' );
// If we're debugging, don't use cached.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
/**
* Reset the cache if we're using action=kirki-reset-cache in the URL.
*
* Note to code reviewers:
* There's no need to check nonces or anything else, this is a simple true/false evaluation.
*/
if ( ! empty( $_GET['action'] ) && 'kirki-reset-cache' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
self::$google_fonts = false;
}
@ -154,7 +143,7 @@ final class Kirki_Fonts {
self::$google_fonts = apply_filters( 'kirki_fonts_google_fonts', $google_fonts );
// Save the array in cache.
$cache_time = apply_filters( 'kirki_googlefonts_transient_time', HOUR_IN_SECONDS );
$cache_time = apply_filters( 'kirki_googlefonts_transient_time', DAY_IN_SECONDS );
set_site_transient( 'kirki_googlefonts_cache', self::$google_fonts, $cache_time );
return self::$google_fonts;

View file

@ -6,7 +6,7 @@
* @category Core
* @author Ari Stathopoulos (@aristath)
* @copyright Copyright (c) 2019, Ari Stathopoulos (@aristath)
* @license https://opensource.org/licenses/MIT
* @license https://opensource.org/licenses/MIT
* @since 3.0
*/

View file

@ -6,7 +6,7 @@
* @category Core
* @author Ari Stathopoulos (@aristath)
* @copyright Copyright (c) 2019, Ari Stathopoulos (@aristath)
* @license https://opensource.org/licenses/MIT
* @license https://opensource.org/licenses/MIT
* @since 3.0
*/
@ -146,8 +146,11 @@ final class Kirki_Modules_Webfonts_Embed {
$url = "https://fonts.googleapis.com/css?family={$family}:{$weights}&subset=cyrillic,cyrillic-ext,devanagari,greek,greek-ext,khmer,latin,latin-ext,vietnamese,hebrew,arabic,bengali,gujarati,tamil,telugu,thai";
$transient_id = 'kirki_gfonts_' . md5( $url );
$contents = get_site_transient( $transient_id );
$contents = get_transient( $transient_id );
/**
* Reset the cache if we're using action=kirki-reset-cache in the URL.
*
* Note to code reviewers:
* There's no need to check nonces or anything else, this is a simple true/false evaluation.
*/
@ -193,8 +196,15 @@ final class Kirki_Modules_Webfonts_Embed {
$contents = $this->use_local_files( $contents );
}
// Set the transient for a week.
set_site_transient( $transient_id, $contents, WEEK_IN_SECONDS );
// Remove protocol to fix http/https issues.
$contents = str_replace(
array( 'http://', 'https://' ),
array( '//', '//' ),
$contents
);
// Set the transient for a day.
set_transient( $transient_id, $contents, DAY_IN_SECONDS );
}
}
if ( $contents ) {
@ -222,7 +232,10 @@ final class Kirki_Modules_Webfonts_Embed {
* @return string The CSS with local URLs.
*/
private function use_local_files( $css ) {
preg_match( '/https\:.*?\.woff/', $css, $matches );
preg_match_all( '/https\:.*?\.woff/', $css, $matches );
$matches = array_shift( $matches );
foreach ( $matches as $match ) {
if ( 0 === strpos( $match, 'https://fonts.gstatic.com' ) ) {
$new_url = Kirki_Fonts_Helper::download_font_file( $match );

View file

@ -6,7 +6,7 @@
* @category Modules
* @author Ari Stathopoulos (@aristath)
* @copyright Copyright (c) 2019, Ari Stathopoulos (@aristath)
* @license https://opensource.org/licenses/MIT
* @license https://opensource.org/licenses/MIT
* @since 3.0.0
*/

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long