first commit without licensingg
This commit is contained in:
218
core/customizer/modules/class-fonts-google.php
Normal file
218
core/customizer/modules/class-fonts-google.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
/**
|
||||
* Customizer Fonts Google
|
||||
*
|
||||
* @package Revision
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'CSCO_Customizer_Fonts_Google' ) ) {
|
||||
/**
|
||||
* Customizer Fonts Google Class.
|
||||
*/
|
||||
final class CSCO_Customizer_Fonts_Google {
|
||||
|
||||
/**
|
||||
* The array of fonts
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $fonts_output = array();
|
||||
|
||||
/**
|
||||
* An array of all google fonts.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $google_fonts = array();
|
||||
|
||||
/**
|
||||
* Fonts to load.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fonts_to_load = array();
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->google_fonts = CSCO_Customizer_Fonts::get_google_fonts();
|
||||
|
||||
/** Initialize actions */
|
||||
add_action( 'wp_loaded', array( $this, 'populate_fonts' ) );
|
||||
add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
|
||||
|
||||
if ( ! is_admin() || is_customize_preview() ) {
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_google_fonts' ), 999 );
|
||||
} else {
|
||||
add_action( 'enqueue_block_assets', array( $this, 'enqueue_google_fonts' ), 999 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loader for Google Fonts.
|
||||
*/
|
||||
public function populate_fonts() {
|
||||
// Go through our fields and populate $this->fonts_output.
|
||||
$this->loop_fields();
|
||||
|
||||
// Goes through $this->fonts_output and adds or removes things as needed.
|
||||
$this->process_fonts();
|
||||
|
||||
foreach ( $this->fonts_output as $font => $weights ) {
|
||||
foreach ( $weights as $key => $value ) {
|
||||
if ( 'italic' === $value ) {
|
||||
$weights[ $key ] = '400i';
|
||||
} else {
|
||||
$weights[ $key ] = str_replace( array( 'regular', 'bold', 'italic' ), array( '400', '', 'i' ), $value );
|
||||
}
|
||||
}
|
||||
|
||||
$this->fonts_to_load[] = array(
|
||||
'family' => $font,
|
||||
'weights' => $weights,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Goes through all our fields and then populates the $this->fonts_output property.
|
||||
*/
|
||||
public function loop_fields() {
|
||||
$fields = CSCO_Customizer::$fields;
|
||||
|
||||
if ( is_array( $fields ) && $fields ) {
|
||||
foreach ( $fields as $field ) {
|
||||
if ( ! isset( $field['type'] ) || false === strpos( $field['type'], 'typography' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check active callback.
|
||||
if ( ! CSCO_Customizer_Helper::active_callback( $field ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the value.
|
||||
$value = CSCO_Customizer_Helper::get_value( $field['settings'] );
|
||||
|
||||
// If we don't have a font-family then we can skip this.
|
||||
if ( ! isset( $value['font-family'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If not a google-font, then we can skip this.
|
||||
if ( ! isset( $value['font-family'] ) || ! CSCO_Customizer_Fonts::is_google_font( $value['font-family'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set a default value for variants.
|
||||
if ( ! isset( $value['variant'] ) ) {
|
||||
$value['variant'] = 'regular';
|
||||
}
|
||||
|
||||
// Add the requested google-font.
|
||||
if ( ! isset( $this->fonts_output[ $value['font-family'] ] ) ) {
|
||||
$this->fonts_output[ $value['font-family'] ] = array();
|
||||
}
|
||||
|
||||
if ( ! in_array( $value['variant'], $this->fonts_output[ $value['font-family'] ], true ) ) {
|
||||
$this->fonts_output[ $value['font-family'] ][] = $value['variant'];
|
||||
}
|
||||
|
||||
if ( isset( $field['choices']['variant'] ) && is_array( $field['choices']['variant'] ) ) {
|
||||
foreach ( $field['choices']['variant'] as $extra_variant ) {
|
||||
if ( ! in_array( $extra_variant, $this->fonts_output[ $value['font-family'] ], true ) ) {
|
||||
$this->fonts_output[ $value['font-family'] ][] = $extra_variant;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the vbalidity of the selected font as well as its properties.
|
||||
* This is vital to make sure that the google-font script that we'll generate later
|
||||
* does not contain any invalid options.
|
||||
*/
|
||||
public function process_fonts() {
|
||||
|
||||
// Early exit if font-family is empty.
|
||||
if ( empty( $this->fonts_output ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->fonts_output as $font => $variants ) {
|
||||
|
||||
// Determine if this is indeed a google font or not.
|
||||
// If it's not, then just remove it from the array.
|
||||
if ( ! array_key_exists( $font, $this->google_fonts ) ) {
|
||||
unset( $this->fonts_output[ $font ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get all valid font variants for this font.
|
||||
$font_variants = array();
|
||||
|
||||
if ( isset( $this->google_fonts[ $font ]['variants'] ) ) {
|
||||
$font_variants = $this->google_fonts[ $font ]['variants'];
|
||||
}
|
||||
|
||||
foreach ( $variants as $variant ) {
|
||||
// If this is not a valid variant for this font-family
|
||||
// then unset it and move on to the next one.
|
||||
if ( ! in_array( strval( $variant ), $font_variants, true ) ) {
|
||||
$variant_key = array_search( $variant, $this->fonts_output[ $font ], true );
|
||||
unset( $this->fonts_output[ $font ][ $variant_key ] );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add preconnect for Google Fonts.
|
||||
*
|
||||
* @param array $urls URLs to print for resource hints.
|
||||
* @param string $relation_type The relation type the URLs are printed.
|
||||
*/
|
||||
public function resource_hints( $urls, $relation_type ) {
|
||||
$fonts_to_load = $this->fonts_output;
|
||||
|
||||
if ( ! empty( $fonts_to_load ) && 'preconnect' === $relation_type ) {
|
||||
$urls[] = array(
|
||||
'href' => 'https://fonts.gstatic.com',
|
||||
'crossorigin',
|
||||
);
|
||||
}
|
||||
return $urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue Google fonts.
|
||||
*/
|
||||
public function enqueue_google_fonts() {
|
||||
|
||||
foreach ( $this->fonts_to_load as $font ) {
|
||||
// Set family.
|
||||
$family = str_replace( ' ', '+', trim( $font['family'] ) );
|
||||
|
||||
// Set weights.
|
||||
$weights = join( ',', $font['weights'] );
|
||||
|
||||
/**
|
||||
* The csco_customizer_google_fonts_subset hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
$subset = apply_filters( 'csco_customizer_google_fonts_subset', 'latin,latin-ext,cyrillic,cyrillic-ext,vietnamese' );
|
||||
|
||||
$url = "https://fonts.googleapis.com/css?family={$family}:{$weights}&subset={$subset}&display=swap";
|
||||
|
||||
wp_enqueue_style( md5( $url ), $url, array(), csco_get_theme_data( 'Version' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new CSCO_Customizer_Fonts_Google();
|
||||
}
|
||||
250
core/customizer/modules/class-fonts-theme.php
Normal file
250
core/customizer/modules/class-fonts-theme.php
Normal file
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
/**
|
||||
* Customizer Theme Fonts Google
|
||||
*
|
||||
* @package Revision
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'CSCO_Customizer_Fonts_Theme' ) ) {
|
||||
/**
|
||||
* Customizer Fonts Theme Class.
|
||||
*/
|
||||
final class CSCO_Customizer_Fonts_Theme {
|
||||
/**
|
||||
* The theme fonts.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $fonts_output = array();
|
||||
|
||||
/**
|
||||
* The theme all variants.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $variants_output = array();
|
||||
|
||||
/**
|
||||
* The class constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
/**
|
||||
* The csco_theme_fonts hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
$this->fonts_output = apply_filters( 'csco_theme_fonts', array() );
|
||||
|
||||
/** Initialize actions */
|
||||
add_action( 'wp', array( $this, 'process_fonts' ) );
|
||||
add_filter( 'admin_init', array( $this, 'set_variants_output' ) );
|
||||
add_filter( 'template_redirect', array( $this, 'set_variants_output' ) );
|
||||
add_filter( 'csco_customizer_dynamic_css', array( $this, 'dynamic_css_fonts_stack' ) );
|
||||
add_filter( 'csco_customizer_fonts_choices', array( $this, 'customizer_fonts_choices' ), 999 );
|
||||
|
||||
if ( ! is_admin() || is_customize_preview() ) {
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_theme_fonts' ), 999 );
|
||||
} else {
|
||||
add_action( 'enqueue_block_assets', array( $this, 'enqueue_theme_fonts' ), 999 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stack the variants that are used in the theme.
|
||||
*/
|
||||
public function set_variants_output() {
|
||||
$fields = CSCO_Customizer::$fields;
|
||||
|
||||
$extra_variants = array();
|
||||
|
||||
if ( is_array( $fields ) && $fields ) {
|
||||
foreach ( $fields as $field ) {
|
||||
if ( ! isset( $field['type'] ) || false === strpos( $field['type'], 'typography' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check active callback.
|
||||
if ( ! CSCO_Customizer_Helper::active_callback( $field ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field_value = CSCO_Customizer_Helper::get_value( $field['settings'] );
|
||||
|
||||
if ( ! isset( $field_value['font-family'] ) || ! $field_value['font-family'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set font-family.
|
||||
$font_family = $field_value['font-family'];
|
||||
|
||||
// Set font-weight.
|
||||
$font_weight = ( isset( $field_value['font-weight'] ) && $field_value['font-weight'] ) ? $field_value['font-weight'] : 400;
|
||||
|
||||
$font_weight = ( 'regular' === $font_weight ) ? 400 : absint( $font_weight );
|
||||
|
||||
// Set font-style.
|
||||
$font_style = ( isset( $field_value['font-style'] ) && $field_value['font-style'] ) ? $field_value['font-style'] : 'normal';
|
||||
|
||||
// Add hash.
|
||||
array_push( $extra_variants, $font_family . $font_weight . $font_style );
|
||||
|
||||
if ( ! isset( $field['choices']['variant'] ) || ! $field['choices']['variant'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add all possible variations from choices.
|
||||
foreach ( $field['choices']['variant'] as $variant ) {
|
||||
// Get font-weight from variant.
|
||||
$font_weight = $this->get_font_weight( $variant );
|
||||
|
||||
// Get font-style from variant.
|
||||
$font_style = $this->get_font_style( $variant );
|
||||
|
||||
// Add hash of choices.
|
||||
array_push( $extra_variants, $font_family . $font_weight . $font_style );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->variants_output = $extra_variants;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process fonts
|
||||
*/
|
||||
public function process_fonts() {
|
||||
foreach ( $this->fonts_output as $key => $font ) {
|
||||
if ( ! isset( $font['name'] ) || ! isset( $font['variants'] ) ) {
|
||||
unset( $this->fonts_output[ $key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new fonts for choices
|
||||
*
|
||||
* @param array $fonts List fonts.
|
||||
*/
|
||||
public function customizer_fonts_choices( $fonts ) {
|
||||
|
||||
if ( is_customize_preview() ) {
|
||||
|
||||
// Add new section.
|
||||
if ( $this->fonts_output ) {
|
||||
$fonts['fonts']['families']['theme'] = array(
|
||||
'text' => esc_html__( 'Theme Fonts', 'revision' ),
|
||||
'children' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
// Add new font.
|
||||
foreach ( $this->fonts_output as $slug => $font ) {
|
||||
$fonts['fonts']['families']['theme']['children'][] = array(
|
||||
'text' => $font['name'],
|
||||
'id' => $slug,
|
||||
);
|
||||
|
||||
$fonts['fonts']['variants'][ $slug ] = $font['variants'];
|
||||
}
|
||||
}
|
||||
|
||||
return $fonts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend font stack for dynamic styles
|
||||
*
|
||||
* @param string $style The dynamic css.
|
||||
*/
|
||||
public function dynamic_css_fonts_stack( $style ) {
|
||||
|
||||
foreach ( $this->fonts_output as $slug => $font ) {
|
||||
$style = str_replace( sprintf( 'font-family:%s;', $slug ), sprintf( 'font-family:%s,-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";', $slug ), $style );
|
||||
}
|
||||
|
||||
return $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font-weight from variant.
|
||||
*
|
||||
* @param string $variant The variant of font.
|
||||
*/
|
||||
public function get_font_weight( $variant = 'regular' ) {
|
||||
$font_weight = filter_var( $variant, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
|
||||
|
||||
return ( 'regular' === $variant || 'italic' === $variant ) ? 400 : absint( $font_weight );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font-style from variant.
|
||||
*
|
||||
* @param string $variant The variant of font.
|
||||
*/
|
||||
public function get_font_style( $variant = 'regular' ) {
|
||||
return ( false === strpos( $variant, 'italic' ) ) ? 'normal' : 'italic';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all our styles and returns them as a string.
|
||||
*
|
||||
* @param string $method Webfonts Load Method.
|
||||
*/
|
||||
public function get_styles_theme_fonts( $method = null ) {
|
||||
ob_start();
|
||||
foreach ( $this->fonts_output as $slug => $font ) {
|
||||
|
||||
foreach ( $font['variants'] as $variant ) {
|
||||
$font_family = $slug;
|
||||
|
||||
// Get font-weight from variant.
|
||||
$font_weight = $this->get_font_weight( $variant );
|
||||
|
||||
// Get font-style from variant.
|
||||
$font_style = $this->get_font_style( $variant );
|
||||
|
||||
// Get font path.
|
||||
$font_path = get_theme_file_uri( sprintf( 'assets/static/fonts/%s-%s', $slug, $variant ) );
|
||||
|
||||
// Get hash from font.
|
||||
$hash = $font_family . $font_weight . $font_style;
|
||||
|
||||
// Check whether the font is used in the theme.
|
||||
if ( ! in_array( $hash, $this->variants_output, true ) ) {
|
||||
continue;
|
||||
}
|
||||
?>
|
||||
@font-face {
|
||||
font-family: <?php echo esc_html( $slug ); ?>;
|
||||
src: url('<?php echo esc_html( $font_path ); ?>.woff2') format('woff2'),
|
||||
url('<?php echo esc_html( $font_path ); ?>.woff') format('woff');
|
||||
font-weight: <?php echo esc_html( $font_weight ); ?>;
|
||||
font-style: <?php echo esc_html( $font_style ); ?>;
|
||||
font-display: swap;
|
||||
}
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
$styles = ob_get_clean();
|
||||
|
||||
// Remove extra characters.
|
||||
$styles = str_replace( array( "\t", "\r", "\n" ), '', $styles );
|
||||
|
||||
return $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue theme fonts.
|
||||
*/
|
||||
public function enqueue_theme_fonts() {
|
||||
wp_register_style( 'cs-theme-fonts', false, array(), csco_get_theme_data( 'Version' ) );
|
||||
|
||||
wp_enqueue_style( 'cs-theme-fonts' );
|
||||
|
||||
wp_add_inline_style( 'cs-theme-fonts', $this->get_styles_theme_fonts() );
|
||||
}
|
||||
}
|
||||
|
||||
new CSCO_Customizer_Fonts_Theme();
|
||||
}
|
||||
194
core/customizer/modules/class-fonts.php
Normal file
194
core/customizer/modules/class-fonts.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
/**
|
||||
* Customizer Fonts
|
||||
*
|
||||
* @package Revision
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'CSCO_Customizer_Fonts' ) ) {
|
||||
/**
|
||||
* Customizer Fonts Class.
|
||||
*/
|
||||
final class CSCO_Customizer_Fonts {
|
||||
|
||||
/**
|
||||
* An array of our google fonts.
|
||||
*
|
||||
* @static
|
||||
* @var null|object
|
||||
*/
|
||||
public static $google_fonts = null;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
/** Initialize actions */
|
||||
add_action( 'wp_ajax_csco_typography_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) );
|
||||
add_action( 'wp_ajax_nopriv_csco_typography_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) );
|
||||
add_action( 'wp_ajax_csco_typography_fonts_standard_all_get', array( $this, 'get_standardfonts_json' ) );
|
||||
add_action( 'wp_ajax_nopriv_csco_typography_fonts_standard_all_get', array( $this, 'get_standardfonts_json' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the googlefonts JSON file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function get_googlefonts_json() {
|
||||
require get_theme_file_path( '/core/customizer/assets/webfonts.json' ); // phpcs:ignore.
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the standard fonts JSON.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function get_standardfonts_json() {
|
||||
echo wp_json_encode( self::get_standard_fonts() );
|
||||
wp_die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all available variants.
|
||||
*
|
||||
* @static
|
||||
* @return array
|
||||
*/
|
||||
public static function get_all_variants() {
|
||||
return array(
|
||||
'100' => esc_html__( 'Ultra-Light 100', 'revision' ),
|
||||
'100light' => esc_html__( 'Ultra-Light 100', 'revision' ),
|
||||
'100italic' => esc_html__( 'Ultra-Light 100 Italic', 'revision' ),
|
||||
'200' => esc_html__( 'Light 200', 'revision' ),
|
||||
'200italic' => esc_html__( 'Light 200 Italic', 'revision' ),
|
||||
'300' => esc_html__( 'Book 300', 'revision' ),
|
||||
'300italic' => esc_html__( 'Book 300 Italic', 'revision' ),
|
||||
'400' => esc_html__( 'Normal 400', 'revision' ),
|
||||
'regular' => esc_html__( 'Normal 400', 'revision' ),
|
||||
'italic' => esc_html__( 'Normal 400 Italic', 'revision' ),
|
||||
'500' => esc_html__( 'Medium 500', 'revision' ),
|
||||
'500italic' => esc_html__( 'Medium 500 Italic', 'revision' ),
|
||||
'600' => esc_html__( 'Semi-Bold 600', 'revision' ),
|
||||
'600bold' => esc_html__( 'Semi-Bold 600', 'revision' ),
|
||||
'600italic' => esc_html__( 'Semi-Bold 600 Italic', 'revision' ),
|
||||
'700' => esc_html__( 'Bold 700', 'revision' ),
|
||||
'700italic' => esc_html__( 'Bold 700 Italic', 'revision' ),
|
||||
'800' => esc_html__( 'Extra-Bold 800', 'revision' ),
|
||||
'800bold' => esc_html__( 'Extra-Bold 800', 'revision' ),
|
||||
'800italic' => esc_html__( 'Extra-Bold 800 Italic', 'revision' ),
|
||||
'900' => esc_html__( 'Ultra-Bold 900', 'revision' ),
|
||||
'900bold' => esc_html__( 'Ultra-Bold 900', 'revision' ),
|
||||
'900italic' => esc_html__( 'Ultra-Bold 900 Italic', 'revision' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of standard websafe fonts.
|
||||
*
|
||||
* @return array Standard websafe fonts.
|
||||
*/
|
||||
public static function get_standard_fonts() {
|
||||
$standard_fonts = array(
|
||||
'serif' => array(
|
||||
'label' => 'Serif',
|
||||
'stack' => 'Georgia,Times,"Times New Roman",serif',
|
||||
),
|
||||
'sans-serif' => array(
|
||||
'label' => 'Sans Serif',
|
||||
'stack' => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif',
|
||||
),
|
||||
'monospace' => array(
|
||||
'label' => 'Monospace',
|
||||
'stack' => 'Monaco,"Lucida Sans Typewriter","Lucida Typewriter","Courier New",Courier,monospace',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* The csco_customizer_fonts_standard_fonts hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
return apply_filters( 'csco_customizer_fonts_standard_fonts', $standard_fonts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of all available Google Fonts.
|
||||
*
|
||||
* @return array All Google Fonts.
|
||||
*/
|
||||
public static function get_google_fonts() {
|
||||
|
||||
// Get fonts from cache.
|
||||
self::$google_fonts = get_site_transient( 'csco_customizer_googlefonts_cache' );
|
||||
|
||||
/**
|
||||
* Reset the cache if we're using action=cs-googlefonts-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'] ) && 'cs-googlefonts-reset-cache' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
|
||||
self::$google_fonts = false;
|
||||
}
|
||||
|
||||
// If cache is populated, return cached fonts array.
|
||||
if ( self::$google_fonts ) {
|
||||
return self::$google_fonts;
|
||||
}
|
||||
|
||||
// If we got this far, cache was empty so we need to get from JSON.
|
||||
ob_start();
|
||||
|
||||
require get_theme_file_path( '/core/customizer/assets/webfonts.json' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
|
||||
|
||||
$fonts_json = ob_get_clean();
|
||||
$fonts = json_decode( $fonts_json, true );
|
||||
|
||||
$google_fonts = array();
|
||||
if ( is_array( $fonts ) ) {
|
||||
foreach ( $fonts['items'] as $font ) {
|
||||
$google_fonts[ $font['family'] ] = array(
|
||||
'label' => $font['family'],
|
||||
'variants' => $font['variants'],
|
||||
'category' => $font['category'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the 'csco_customizer_fonts_google_fonts' filter
|
||||
*
|
||||
* The csco_customizer_fonts_google_fonts hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
self::$google_fonts = apply_filters( 'csco_customizer_fonts_google_fonts', $google_fonts );
|
||||
|
||||
/**
|
||||
* Save the array in cache.
|
||||
*
|
||||
* The csco_customizer_googlefonts_transient_time hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
$cache_time = apply_filters( 'csco_customizer_googlefonts_transient_time', DAY_IN_SECONDS );
|
||||
set_site_transient( 'csco_customizer_googlefonts_cache', self::$google_fonts, $cache_time );
|
||||
|
||||
return self::$google_fonts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a font-name is a valid google font or not.
|
||||
*
|
||||
* @param string $fontname The name of the font we want to check.
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_google_font( $fontname ) {
|
||||
return ( array_key_exists( $fontname, self::$google_fonts ) );
|
||||
}
|
||||
}
|
||||
|
||||
new CSCO_Customizer_Fonts();
|
||||
}
|
||||
240
core/customizer/modules/class-output-styles.php
Normal file
240
core/customizer/modules/class-output-styles.php
Normal file
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
/**
|
||||
* Theme Customizer Output Styles
|
||||
*
|
||||
* @package Revision
|
||||
*/
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'CSCO_Customizer_Output_Styles' ) ) {
|
||||
/**
|
||||
* Class Theme Customizer Output
|
||||
*/
|
||||
class CSCO_Customizer_Output_Styles {
|
||||
|
||||
/**
|
||||
* The class constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
if ( ! is_admin() || is_customize_preview() ) {
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_output_styles' ), 999 );
|
||||
} else {
|
||||
add_action( 'enqueue_block_assets', array( $this, 'enqueue_output_styles' ), 999 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all our styles and returns them as a string.
|
||||
*/
|
||||
public function get_output_styles() {
|
||||
|
||||
$output_styles = __return_empty_string();
|
||||
|
||||
// Get an array of all our fields.
|
||||
$fields = CSCO_Customizer::$fields;
|
||||
|
||||
// Check if we need to exit early.
|
||||
if ( empty( $fields ) || ! is_array( $fields ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Initially we're going to format our styles as an array.
|
||||
// This is going to make processing them a lot easier
|
||||
// and make sure there are no duplicate styles etc.
|
||||
$css = array();
|
||||
|
||||
// Start parsing our fields.
|
||||
foreach ( $fields as $field ) {
|
||||
// No need to process fields without an output, or an improperly-formatted output.
|
||||
if ( ! isset( $field['output'] ) || empty( $field['output'] ) || ! is_array( $field['output'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the value of this field.
|
||||
$value = CSCO_Customizer_Helper::get_value( $field['settings'] );
|
||||
|
||||
// Check active callback.
|
||||
if ( ! CSCO_Customizer_Helper::active_callback( $field ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start parsing the output arguments of the field.
|
||||
foreach ( $field['output'] as $output ) {
|
||||
|
||||
if ( is_admin() && ! is_customize_preview() ) {
|
||||
// Check if this is an admin style.
|
||||
if ( ! isset( $output['context'] ) || ! in_array( 'editor', $output['context'], true ) ) {
|
||||
continue;
|
||||
}
|
||||
} elseif ( isset( $output['context'] ) && ! in_array( 'front', $output['context'], true ) ) {
|
||||
// Check if this is a frontend style.
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = wp_parse_args(
|
||||
$output, array(
|
||||
'element' => '',
|
||||
'property' => '',
|
||||
'media_query' => 'global',
|
||||
'prefix' => '',
|
||||
'units' => '',
|
||||
'suffix' => '',
|
||||
'value_pattern' => '$',
|
||||
'choice' => '',
|
||||
'convert' => '',
|
||||
)
|
||||
);
|
||||
// If element is an array, convert it to a string.
|
||||
if ( is_array( $output['element'] ) ) {
|
||||
$output['element'] = implode( ',', $output['element'] );
|
||||
}
|
||||
// Simple fields.
|
||||
if ( ! is_array( $value ) ) {
|
||||
$value_pattern = str_replace( '$', ( $value ? $value : '' ), $output['value_pattern'] );
|
||||
|
||||
if ( 'rgb' === $output['convert'] ) {
|
||||
$value_pattern = csco_hex2rgba( $value_pattern, false );
|
||||
}
|
||||
|
||||
if ( ! empty( $output['element'] ) && ! empty( $output['property'] ) && $value_pattern ) {
|
||||
$css[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value_pattern . $output['units'] . $output['suffix'];
|
||||
}
|
||||
} else {
|
||||
if ( 'typography' === $field['type'] ) {
|
||||
|
||||
$value = CSCO_Customizer_Helper::typography_sanitize( $value );
|
||||
|
||||
$properties = array(
|
||||
'font-family',
|
||||
'font-size',
|
||||
'variant',
|
||||
'font-weight',
|
||||
'font-style',
|
||||
'letter-spacing',
|
||||
'word-spacing',
|
||||
'line-height',
|
||||
'text-align',
|
||||
'text-transform',
|
||||
'text-decoration',
|
||||
'color',
|
||||
);
|
||||
|
||||
foreach ( $properties as $property ) {
|
||||
// Early exit if the value is not in the defaults.
|
||||
if ( ! isset( $field['default'][ $property ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Early exit if the value is not saved in the values.
|
||||
if ( ! isset( $value[ $property ] ) || ! $value[ $property ] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Take care of variants.
|
||||
if ( 'variant' === $property && isset( $value['variant'] ) && ! empty( $value['variant'] ) ) {
|
||||
|
||||
// Get the font_weight.
|
||||
$font_weight = str_replace( 'italic', '', $value['variant'] );
|
||||
$font_weight = in_array( $font_weight, array( '', 'regular' ), true ) ? '400' : $font_weight;
|
||||
|
||||
$css[ $output['media_query'] ][ $output['element'] ]['font-weight'] = $font_weight;
|
||||
|
||||
// Is this italic?
|
||||
$is_italic = ( false !== strpos( $value['variant'], 'italic' ) );
|
||||
if ( $is_italic ) {
|
||||
$css[ $output['media_query'] ][ $output['element'] ]['font-style'] = 'italic';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$css[ $output['media_query'] ][ $output['element'] ][ $property ] = $output['prefix'] . $value[ $property ] . $output['suffix'];
|
||||
}
|
||||
} elseif ( 'multicolor' === $field['type'] ) {
|
||||
|
||||
if ( ! empty( $output['element'] ) && ! empty( $output['property'] ) && ! empty( $output['choice'] ) ) {
|
||||
$css[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $value[ $output['choice'] ] . $output['units'] . $output['suffix'];
|
||||
}
|
||||
} else {
|
||||
foreach ( $value as $key => $subvalue ) {
|
||||
$property = $key;
|
||||
|
||||
if ( false !== strpos( $output['property'], '%%' ) ) {
|
||||
|
||||
$property = str_replace( '%%', $key, $output['property'] );
|
||||
|
||||
} elseif ( ! empty( $output['property'] ) ) {
|
||||
|
||||
$output['property'] = $output['property'] . '-' . $key;
|
||||
}
|
||||
|
||||
if ( 'background-image' === $output['property'] && false === strpos( $subvalue, 'url(' ) ) {
|
||||
$subvalue = sprintf( 'url("%s")', set_url_scheme( $subvalue ) );
|
||||
}
|
||||
if ( $subvalue ) {
|
||||
$css[ $output['media_query'] ][ $output['element'] ][ $property ] = $subvalue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the array of CSS properties and produce the final CSS.
|
||||
if ( ! is_array( $css ) || empty( $css ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ( $css as $media_query => $styles ) {
|
||||
$output_styles .= ( 'global' !== $media_query ) ? $media_query . '{' : '';
|
||||
|
||||
foreach ( $styles as $style => $style_array ) {
|
||||
$css_for_style = '';
|
||||
|
||||
foreach ( $style_array as $property => $value ) {
|
||||
if ( is_string( $value ) && '' !== $value ) {
|
||||
$css_for_style .= sprintf( '%s:%s;', $property, $value );
|
||||
} elseif ( is_array( $value ) ) {
|
||||
foreach ( $value as $subvalue ) {
|
||||
if ( is_string( $subvalue ) && '' !== $subvalue ) {
|
||||
$css_for_style .= sprintf( '%s:%s;', $property, $subvalue );
|
||||
}
|
||||
}
|
||||
}
|
||||
$value = ( is_string( $value ) ) ? $value : '';
|
||||
}
|
||||
if ( '' !== $css_for_style ) {
|
||||
$output_styles .= $style . sprintf( '{%s}', $css_for_style );
|
||||
}
|
||||
}
|
||||
|
||||
$output_styles .= ( 'global' !== $media_query ) ? '}' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* The csco_customizer_output_styles hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
$output_styles = apply_filters( 'csco_customizer_output_styles', $output_styles );
|
||||
|
||||
return $output_styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue output styles.
|
||||
*/
|
||||
public function enqueue_output_styles() {
|
||||
wp_register_style( 'cs-customizer-output-styles', false, array(), csco_get_theme_data( 'Version' ) );
|
||||
|
||||
wp_enqueue_style( 'cs-customizer-output-styles' );
|
||||
|
||||
wp_add_inline_style( 'cs-customizer-output-styles', $this->get_output_styles() );
|
||||
}
|
||||
}
|
||||
|
||||
new CSCO_Customizer_Output_Styles();
|
||||
}
|
||||
Reference in New Issue
Block a user