initial upload
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/* Default Font Family */
|
||||
function zeitfresser_get_default_site_identity_font_family() {
|
||||
return esc_html__( "Oswald", 'zeitfresser' );
|
||||
}
|
||||
|
||||
function zeitfresser_get_default_main_font_family() {
|
||||
return esc_html__( "Oswald", 'zeitfresser' );
|
||||
}
|
||||
|
||||
function zeitfresser_get_default_secondary_font_family() {
|
||||
return esc_html__( "Roboto", 'zeitfresser' );
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/* Add Google Fonts */
|
||||
require dirname( __FILE__ ) . '/google-fonts.php';
|
||||
|
||||
/* Add Default Font Family for Customizer Settings */
|
||||
require dirname( __FILE__ ) . '/default-font-family.php';
|
||||
|
||||
include_once wp_normalize_path( dirname( __FILE__ ) . '/inc/helper-functions.php' );
|
||||
include_once wp_normalize_path( dirname( __FILE__ ) . '/inc/class-webfonts-local.php' );
|
||||
include_once wp_normalize_path( dirname( __FILE__ ) . '/inc/class-fonts-google-local.php' );
|
||||
|
||||
|
||||
|
||||
require dirname( __FILE__ ) . '/site-identity/site-identity-font-family.php';
|
||||
require dirname( __FILE__ ) . '/main/main-font-family.php';
|
||||
require dirname( __FILE__ ) . '/secondary/secondary-font-family.php';
|
||||
|
||||
|
||||
add_action( 'wp_enqueue_scripts', 'zeitfresser_google_fonts_scripts', 5 );
|
||||
function zeitfresser_google_fonts_scripts() {
|
||||
$local_css = zeitfresser_get_local_webfonts_css();
|
||||
|
||||
wp_register_style( 'zeitfresser-webfonts', false, array(), ZEITFRESSER_VERSION );
|
||||
wp_enqueue_style( 'zeitfresser-webfonts' );
|
||||
|
||||
if ( ! empty( $local_css ) ) {
|
||||
wp_add_inline_style( 'zeitfresser-webfonts', $local_css );
|
||||
return;
|
||||
}
|
||||
|
||||
$args = zeitfresser_used_google_fonts();
|
||||
$fonts_url = zeitfresser_fonts_url( $args );
|
||||
|
||||
if ( empty( $fonts_url ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_enqueue_style( 'google-fonts', $fonts_url, array(), null );
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,457 @@
|
||||
<?php
|
||||
/**
|
||||
* Handles downloading a font from the google-fonts API locally.
|
||||
* Solves privacy concerns with Google's CDN
|
||||
* and their sometimes less-than-transparent policies.
|
||||
*
|
||||
* @package zeitfresser
|
||||
*/
|
||||
|
||||
// Do not allow directly accessing this file.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit( 'Direct script access denied.' );
|
||||
}
|
||||
|
||||
final class Daisy_Blog_Google_Local {
|
||||
|
||||
/**
|
||||
* The name of the font-family
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $family;
|
||||
|
||||
/**
|
||||
* The system path where font-files are stored.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $folder_path;
|
||||
|
||||
/**
|
||||
* The URL where files for this font can be found.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $folder_url;
|
||||
|
||||
/**
|
||||
* An array of instances for this object.
|
||||
*
|
||||
* @static
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
private static $instances = array();
|
||||
|
||||
/**
|
||||
* Create an instance of this object for a specific font-family.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param string $family The font-family name.
|
||||
* @return Daisy_Blog_Google_Local
|
||||
*/
|
||||
public static function init( $family ) {
|
||||
$key = sanitize_key( $family );
|
||||
if ( ! isset( self::$instances[ $key ] ) ) {
|
||||
self::$instances[ $key ] = new self( $family );
|
||||
}
|
||||
return self::$instances[ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @access private
|
||||
* @param string $family The font-family name.
|
||||
*/
|
||||
private function __construct( $family ) {
|
||||
$this->family = $family;
|
||||
$key = sanitize_key( $this->family );
|
||||
$this->folder_path = $this->get_root_path() . "/$key";
|
||||
$this->folder_url = $this->get_root_url() . "/$key";
|
||||
$this->files = $this->get_font_family();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the @font-face CSS.
|
||||
*
|
||||
* @access public
|
||||
* @param array $variants The variants we want to get.
|
||||
* @return string
|
||||
*/
|
||||
public function get_css( $variants = array() ) {
|
||||
if ( ! $this->files ) {
|
||||
return;
|
||||
}
|
||||
$key = md5( wp_json_encode( $this->files ) );
|
||||
$cached = get_transient( $key );
|
||||
if ( $cached ) {
|
||||
return $cached;
|
||||
}
|
||||
$css = '';
|
||||
|
||||
// If $variants is empty then use all variants available.
|
||||
if ( empty( $variants ) ) {
|
||||
$variants = array_keys( $this->files );
|
||||
}
|
||||
|
||||
// Download files.
|
||||
$this->download_font_family( $variants );
|
||||
|
||||
// Create the @font-face CSS.
|
||||
foreach ( $variants as $variant ) {
|
||||
$css .= $this->get_variant_fontface_css( $variant );
|
||||
}
|
||||
set_transient( $key, $css, WEEK_IN_SECONDS );
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download font-family files.
|
||||
*
|
||||
* @access public
|
||||
* @param array $variants An array of variants to download. Leave empty to download all.
|
||||
* @return void
|
||||
*/
|
||||
public function download_font_family( $variants = array() ) {
|
||||
if ( empty( $variants ) ) {
|
||||
$variants = array_keys( $this->files );
|
||||
}
|
||||
foreach ( $this->files as $variant => $file ) {
|
||||
if ( in_array( $variant, $variants ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
|
||||
$this->download_font_file( $file );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a font-file and saves it locally.
|
||||
*
|
||||
* @access private
|
||||
* @param string $url The URL of the file we want to get.
|
||||
* @return bool
|
||||
*/
|
||||
private function download_font_file( $url ) {
|
||||
$path = $this->folder_path . '/' . $this->get_filename_from_url( $url );
|
||||
|
||||
// If the folder doesn't exist, create it.
|
||||
if ( ! file_exists( $this->folder_path ) ) {
|
||||
wp_mkdir_p( $this->folder_path );
|
||||
}
|
||||
// If the file exists no reason to do anything.
|
||||
if ( file_exists( $path ) ) {
|
||||
return true;
|
||||
}else{
|
||||
$contents = $this->get_remote_url_contents( $url );
|
||||
// Write file.
|
||||
$filesystem = $this->get_filesystem();
|
||||
return $filesystem->put_contents( $path, $contents, FS_CHMOD_FILE );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the remote URL contents.
|
||||
*
|
||||
* @access private
|
||||
* @param string $url The URL we want to get.
|
||||
* @return string The contents of the remote URL.
|
||||
*/
|
||||
public function get_remote_url_contents( $url ) {
|
||||
$response = wp_remote_get( $url );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return array();
|
||||
}
|
||||
$html = wp_remote_retrieve_body( $response );
|
||||
if ( is_wp_error( $html ) ) {
|
||||
return;
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the filename by breaking-down the URL parts.
|
||||
*
|
||||
* @access private
|
||||
* @param string $url The URL.
|
||||
* @return string The filename.
|
||||
*/
|
||||
private function get_filename_from_url( $url ) {
|
||||
$url_parts = explode( '/', $url );
|
||||
$parts_count = count( $url_parts );
|
||||
if ( 1 < $parts_count ) {
|
||||
return $url_parts[ count( $url_parts ) - 1 ];
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the @font-face CSS for a specific variant.
|
||||
*
|
||||
* @access public
|
||||
* @param string $variant The variant.
|
||||
* @return string
|
||||
*/
|
||||
public function get_variant_fontface_css( $variant ) {
|
||||
$font_face = "@font-face{font-family:'{$this->family}';";
|
||||
|
||||
// Get the font-style.
|
||||
$font_style = ( false !== strpos( $variant, 'italic' ) ) ? 'italic' : 'normal';
|
||||
$font_face .= "font-style:{$font_style};";
|
||||
|
||||
// Get the font-weight.
|
||||
$font_weight = '400';
|
||||
$font_weight = str_replace( 'italic', '', $variant );
|
||||
$font_weight = ( ! $font_weight || 'regular' === $font_weight ) ? '400' : $font_weight;
|
||||
$font_face .= "font-weight:{$font_weight};";
|
||||
|
||||
// Get the font-names.
|
||||
$font_name_0 = $this->get_local_font_name( $variant, false );
|
||||
$font_name_1 = $this->get_local_font_name( $variant, true );
|
||||
$font_face .= "src:local('{$font_name_0}'),";
|
||||
if ( $font_name_0 !== $font_name_1 ) {
|
||||
$font_face .= "local('{$font_name_1}'),";
|
||||
}
|
||||
|
||||
// Get the font-url.
|
||||
$font_url = $this->get_variant_local_url( $variant );
|
||||
$paths = $this->get_font_files_paths();
|
||||
if ( ! file_exists( $paths[ $variant ] ) ) {
|
||||
$font_url = $this->files[ $variant ];
|
||||
}
|
||||
|
||||
// Get the font-format.
|
||||
$font_format = ( strpos( $font_url, '.woff2' ) ) ? 'woff2' : 'truetype';
|
||||
$font_format = ( strpos( $font_url, '.woff' ) && ! strpos( $font_url, '.woff2' ) ) ? 'woff' : $font_format;
|
||||
$font_face .= "url({$font_url}) format('{$font_format}');}";
|
||||
|
||||
return $font_face;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the font-family.
|
||||
* This is used by @font-face in case the user already has the font downloaded locally.
|
||||
*
|
||||
* @access public
|
||||
* @param string $variant The variant.
|
||||
* @param bool $compact Whether we want the compact formatting or not.
|
||||
* @return string
|
||||
*/
|
||||
public function get_local_font_name( $variant, $compact = false ) {
|
||||
$variant_names = array(
|
||||
'100' => 'Thin',
|
||||
'100i' => 'Thin Italic',
|
||||
'100italic' => 'Thin Italic',
|
||||
'200' => 'Extra-Light',
|
||||
'200i' => 'Extra-Light Italic',
|
||||
'200italic' => 'Extra-Light Italic',
|
||||
'300' => 'Light',
|
||||
'300i' => 'Light Italic',
|
||||
'300italic' => 'Light Italic',
|
||||
'400' => 'Regular',
|
||||
'regular' => 'Regular',
|
||||
'400i' => 'Regular Italic',
|
||||
'italic' => 'Italic',
|
||||
'400italic' => 'Regular Italic',
|
||||
'500' => 'Medium',
|
||||
'500i' => 'Medium Italic',
|
||||
'500italic' => 'Medium Italic',
|
||||
'600' => 'Semi-Bold',
|
||||
'600i' => 'Semi-Bold Italic',
|
||||
'600italic' => 'Semi-Bold Italic',
|
||||
'700' => 'Bold',
|
||||
'700i' => 'Bold Italic',
|
||||
'700italic' => 'Bold Italic',
|
||||
'800' => 'Extra-Bold',
|
||||
'800i' => 'Extra-Bold Italic',
|
||||
'800italic' => 'Extra-Bold Italic',
|
||||
'900' => 'Black',
|
||||
'900i' => 'Black Italic',
|
||||
'900italic' => 'Black Italic',
|
||||
);
|
||||
|
||||
$variant = (string) $variant;
|
||||
if ( $compact ) {
|
||||
if ( isset( $variant_names[ $variant ] ) ) {
|
||||
return str_replace( array( ' ', '-' ), '', $this->family ) . '-' . str_replace( array( ' ', '-' ), '', $variant_names[ $variant ] );
|
||||
}
|
||||
return str_replace( array( ' ', '-' ), '', $this->family );
|
||||
}
|
||||
|
||||
if ( isset( $variant_names[ $variant ] ) ) {
|
||||
return $this->family . ' ' . $variant_names[ $variant ];
|
||||
}
|
||||
return $this->family;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the local URL for a variant.
|
||||
*
|
||||
* @access public
|
||||
* @param string $variant The variant.
|
||||
* @return string The URL.
|
||||
*/
|
||||
public function get_variant_local_url( $variant ) {
|
||||
$local_urls = $this->get_font_files_urls_local();
|
||||
|
||||
if ( empty( $local_urls ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Return the specific variant if we can find it.
|
||||
if ( isset( $local_urls[ $variant ] ) ) {
|
||||
return $local_urls[ $variant ];
|
||||
}
|
||||
|
||||
// Return regular if the one we want could not be found.
|
||||
if ( isset( $local_urls['regular'] ) ) {
|
||||
return $local_urls['regular'];
|
||||
}
|
||||
|
||||
// Return the first available if all else failed.
|
||||
$vals = array_values( $local_urls );
|
||||
return $vals[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of local file URLs.
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_font_files_urls_local() {
|
||||
$urls = array();
|
||||
$files = $this->get_font_files();
|
||||
foreach ( $files as $key => $file ) {
|
||||
$urls[ $key ] = $this->folder_url . '/' . $file;
|
||||
}
|
||||
return $urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of local file paths.
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_font_files_paths() {
|
||||
$paths = array();
|
||||
$files = $this->get_font_files();
|
||||
foreach ( $files as $key => $file ) {
|
||||
$paths[ $key ] = $this->folder_path . '/' . $file;
|
||||
}
|
||||
return $paths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of font-files.
|
||||
* Only contains the filenames.
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_font_files() {
|
||||
$files = array();
|
||||
foreach ( $this->files as $key => $url ) {
|
||||
$files[ $key ] = $this->get_filename_from_url( $url );
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the root fonts folder path.
|
||||
* Other paths are built based on this.
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function get_root_path() {
|
||||
|
||||
// Get the upload directory for this site.
|
||||
$upload_dir = wp_upload_dir();
|
||||
$path = untrailingslashit( wp_normalize_path( $upload_dir['basedir'] ) ) . '/webfonts';
|
||||
|
||||
// If the folder doesn't exist, create it.
|
||||
if ( ! file_exists( $path ) ) {
|
||||
wp_mkdir_p( $path );
|
||||
}
|
||||
|
||||
// Return the path.
|
||||
return apply_filters( 'daisy_blog_googlefonts_root_path', $path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the root folder url.
|
||||
* Other urls are built based on this.
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function get_root_url() {
|
||||
|
||||
// Get the upload directory for this site.
|
||||
$upload_dir = wp_upload_dir();
|
||||
|
||||
// The URL.
|
||||
$url = trailingslashit( $upload_dir['baseurl'] );
|
||||
|
||||
// Take care of domain mapping.
|
||||
// When using domain mapping we have to make sure that the URL to the file
|
||||
// does not include the original domain but instead the mapped domain.
|
||||
if ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ) {
|
||||
if ( function_exists( 'domain_mapping_siteurl' ) && function_exists( 'get_original_url' ) ) {
|
||||
$mapped_domain = domain_mapping_siteurl( false );
|
||||
$original_domain = get_original_url( 'siteurl' );
|
||||
$url = str_replace( $original_domain, $mapped_domain, $url );
|
||||
}
|
||||
}
|
||||
$url = str_replace( array( 'https://', 'http://' ), '//', $url );
|
||||
return apply_filters( 'daisy_blog_googlefonts_root_url', untrailingslashit( esc_url_raw( $url ) ) . '/webfonts' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a font-family from the array of google-fonts.
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_font_family() {
|
||||
// Get the fonts array.
|
||||
$fonts = $this->get_fonts();
|
||||
if ( isset( $fonts[ $this->family ] ) ) {
|
||||
return $fonts[ $this->family ];
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the font defined in the google-fonts API.
|
||||
*
|
||||
* @access private
|
||||
* @return array
|
||||
*/
|
||||
private function get_fonts() {
|
||||
zeitfresser_get_google_fonts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the WP_Filesystem object.
|
||||
*
|
||||
* @access protected
|
||||
* @return object
|
||||
*/
|
||||
protected function get_filesystem(){
|
||||
// The WordPress filesystem.
|
||||
global $wp_filesystem;
|
||||
|
||||
if ( empty( $wp_filesystem ) ) {
|
||||
require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' );
|
||||
WP_Filesystem();
|
||||
}
|
||||
return $wp_filesystem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Handles adding to the footer the @font-face CSS for locally-hosted google-fonts.
|
||||
* Solves privacy concerns with Google's CDN and their sometimes less-than-transparent policies.
|
||||
*
|
||||
* @package zeitfresser
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manages the way Google Fonts are enqueued.
|
||||
*/
|
||||
final class Daisy_Blog_Webfonts_Local{
|
||||
|
||||
/**
|
||||
* @access protected
|
||||
*/
|
||||
protected $googlefonts;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function __construct( $googlefonts ) {
|
||||
$this->googlefonts = $googlefonts;
|
||||
|
||||
add_action( 'wp_footer', array( $this, 'add_styles' ) );
|
||||
add_action( 'admin_footer', array( $this, 'add_styles' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Webfont Loader for Google Fonts.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function add_styles() {
|
||||
|
||||
$hosted_fonts = $this->googlefonts;
|
||||
|
||||
// Early exit if we don't need to add any fonts.
|
||||
if ( empty( $hosted_fonts ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we only do this once per font-family.
|
||||
$hosted_fonts = array_unique( $hosted_fonts );
|
||||
|
||||
// Start CSS.
|
||||
$css = '';
|
||||
foreach( $hosted_fonts as $family ){
|
||||
// Add the @font-face CSS for this font-family.
|
||||
$css .= Daisy_Blog_Google_Local::init( $family )->get_css();
|
||||
}
|
||||
|
||||
// If we've got CSS, add to the footer.
|
||||
if ( $css ) {
|
||||
echo '<style id="daisy-blog-local-webfonts">' . $css . '</style>'; // WPCS: XSS ok.
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,334 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* Function to check if it's a google font
|
||||
*/
|
||||
function zeitfresser_is_google_font( $font ){
|
||||
$return = false;
|
||||
$websafe_fonts = zeitfresser_get_websafe_font();
|
||||
if( $font ){
|
||||
if( array_key_exists( $font, $websafe_fonts ) ){
|
||||
//Web Safe Font
|
||||
$return = false;
|
||||
}else{
|
||||
//Google Font
|
||||
$return = true;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if( ! function_exists( 'zeitfresser_get_websafe_font' ) ) {
|
||||
|
||||
/**
|
||||
* Function listing WebSafe Fonts and its attributes
|
||||
*/
|
||||
function zeitfresser_get_websafe_font(){
|
||||
$standard_fonts = array(
|
||||
'georgia-serif' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => 'Georgia, serif',
|
||||
),
|
||||
'palatino-serif' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Palatino Linotype", "Book Antiqua", Palatino, serif',
|
||||
),
|
||||
'times-serif' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Times New Roman", Times, serif',
|
||||
),
|
||||
'arial-helvetica' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => 'Arial, Helvetica, sans-serif',
|
||||
),
|
||||
'arial-gadget' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Arial Black", Gadget, sans-serif',
|
||||
),
|
||||
'comic-cursive' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Comic Sans MS", cursive, sans-serif',
|
||||
),
|
||||
'impact-charcoal' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => 'Impact, Charcoal, sans-serif',
|
||||
),
|
||||
'lucida' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Lucida Sans Unicode", "Lucida Grande", sans-serif',
|
||||
),
|
||||
'tahoma-geneva' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => 'Tahoma, Geneva, sans-serif',
|
||||
),
|
||||
'trebuchet-helvetica' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Trebuchet MS", Helvetica, sans-serif',
|
||||
),
|
||||
'verdana-geneva' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => 'Verdana, Geneva, sans-serif',
|
||||
),
|
||||
'courier' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Courier New", Courier, monospace',
|
||||
),
|
||||
'lucida-monaco' => array(
|
||||
'variants' => array( 'regular', 'italic', '700', '700italic' ),
|
||||
'fonts' => '"Lucida Console", Monaco, monospace',
|
||||
)
|
||||
);
|
||||
|
||||
return apply_filters( 'daisy_blog_standard_fonts', $standard_fonts );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function zeitfresser_used_google_fonts() {
|
||||
$main_font_family = zeitfresser_get_mod( 'main_font_family', zeitfresser_get_default_main_font_family() );
|
||||
$secondary_font_family = zeitfresser_get_mod( 'secondary_font_family', zeitfresser_get_default_secondary_font_family() );
|
||||
$site_identity_font_family = esc_attr( zeitfresser_get_mod( 'site_identity_font_family', zeitfresser_get_default_site_identity_font_family() ) );
|
||||
|
||||
$args['main_font_family'] = $main_font_family;
|
||||
$args['secondary_font_family'] = $secondary_font_family;
|
||||
$args['site_identity_font_family'] = $site_identity_font_family;
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
||||
|
||||
add_action( 'wp_loaded', 'zeitfresser_google_font_local' );
|
||||
if( ! function_exists( 'zeitfresser_google_font_local' ) ) {
|
||||
/**
|
||||
* Function that load Google Fonts used in our theme from customer locally.
|
||||
* Solves privacy concerns with Google's CDN and their sometimes less-than-transparent policies.
|
||||
*/
|
||||
function zeitfresser_google_font_local() {
|
||||
|
||||
$args = array();
|
||||
$fonts = zeitfresser_used_google_fonts();
|
||||
|
||||
foreach( $fonts as $font ) {
|
||||
|
||||
$is_google_font = zeitfresser_is_google_font( $font );
|
||||
|
||||
if( $is_google_font ) {
|
||||
array_push( $args, $font );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new Daisy_Blog_Webfonts_Local( $args );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if ( ! function_exists( 'zeitfresser_font_weight_variants' ) ) {
|
||||
/**
|
||||
* Return the font variants used by the theme as an array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function zeitfresser_font_weight_variants() {
|
||||
$weights = explode( ';', zeitfresser_font_weight_query() );
|
||||
$weights = array_map( 'trim', $weights );
|
||||
$weights = array_filter( $weights );
|
||||
|
||||
if ( ! in_array( '400', $weights, true ) ) {
|
||||
$weights[] = '400';
|
||||
}
|
||||
|
||||
$weights = array_values( array_unique( $weights ) );
|
||||
sort( $weights );
|
||||
|
||||
return $weights;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'zeitfresser_get_local_webfonts_css' ) ) {
|
||||
/**
|
||||
* Build local @font-face CSS for currently selected Google fonts.
|
||||
*
|
||||
* Falls back to remote font file URLs per variant if a local file is not
|
||||
* available yet, so typography does not break during warmup.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function zeitfresser_get_local_webfonts_css() {
|
||||
$fonts = array_values( array_unique( array_filter( zeitfresser_used_google_fonts() ) ) );
|
||||
$variants = zeitfresser_font_weight_variants();
|
||||
$css = '';
|
||||
|
||||
foreach ( $fonts as $font ) {
|
||||
if ( ! zeitfresser_is_google_font( $font ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$css .= Daisy_Blog_Google_Local::init( $font )->get_css( $variants );
|
||||
}
|
||||
|
||||
return $css;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'zeitfresser_get_local_webfont_urls' ) ) {
|
||||
/**
|
||||
* Extract local font asset URLs from a generated @font-face stylesheet.
|
||||
*
|
||||
* @param string $css Local webfont CSS.
|
||||
* @return array
|
||||
*/
|
||||
function zeitfresser_get_local_webfont_urls( $css ) {
|
||||
if ( empty( $css ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
preg_match_all( '#url\(([^)]+)\)#', $css, $matches );
|
||||
|
||||
if ( empty( $matches[1] ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$urls = array();
|
||||
|
||||
foreach ( $matches[1] as $url ) {
|
||||
$url = trim( $url, "\"'" );
|
||||
|
||||
if ( false !== strpos( $url, content_url() ) ) {
|
||||
$urls[] = esc_url_raw( $url );
|
||||
}
|
||||
}
|
||||
|
||||
return array_values( array_unique( array_filter( $urls ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( ! function_exists( 'zeitfresser_font_weight_query' ) ) {
|
||||
/**
|
||||
* Return the compact weight list used by the Zeitfresser theme.
|
||||
*/
|
||||
function zeitfresser_font_weight_query() {
|
||||
$weights = array( '400', '500', '700' );
|
||||
$body_weight = (string) zeitfresser_get_mod( 'font_weight', zeitfresser_get_default_font_weight() );
|
||||
|
||||
if ( preg_match( '/^\d{3}$/', $body_weight ) ) {
|
||||
$weights[] = $body_weight;
|
||||
}
|
||||
|
||||
$weights = array_values( array_unique( array_filter( $weights ) ) );
|
||||
sort( $weights );
|
||||
|
||||
return implode( ';', $weights );
|
||||
}
|
||||
}
|
||||
|
||||
if( ! function_exists( 'zeitfresser_fonts_url' ) ) {
|
||||
/**
|
||||
* Returns a Google Fonts CSS2 URL for the selected theme fonts.
|
||||
*/
|
||||
function zeitfresser_fonts_url( $fonts = array() ) {
|
||||
$font_families = array();
|
||||
$weights = zeitfresser_font_weight_query();
|
||||
|
||||
foreach ( $fonts as $font ) {
|
||||
if ( ! zeitfresser_is_google_font( $font ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$family_name = trim( (string) $font );
|
||||
|
||||
if ( '' === $family_name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$font_families[] = 'family=' . str_replace( ' ', '+', $family_name ) . ':wght@' . $weights;
|
||||
}
|
||||
|
||||
$font_families = array_values( array_unique( $font_families ) );
|
||||
|
||||
if ( empty( $font_families ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return esc_url( 'https://fonts.googleapis.com/css2?' . implode( '&', $font_families ) . '&display=swap' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if( ! function_exists( 'zeitfresser_check_varient' ) ) {
|
||||
/**
|
||||
* Checks for matched varients in google fonts for typography fields
|
||||
*/
|
||||
function zeitfresser_check_varient( $font_family = 'serif', $font_variants = 'regular', $body = false ){
|
||||
$variant = '';
|
||||
$var = array();
|
||||
$google_fonts = zeitfresser_get_google_fonts(); //Google Fonts
|
||||
$websafe_fonts = zeitfresser_get_websafe_font(); //Standard Web Safe Fonts
|
||||
|
||||
if( array_key_exists( $font_family, $google_fonts ) ){
|
||||
$variants = $google_fonts[ $font_family ][ 'variants' ];
|
||||
if( in_array( $font_variants, $variants ) ){
|
||||
if( $body ){ //LOAD ALL VARIANTS FOR BODY FONT
|
||||
foreach( $variants as $v ){
|
||||
$var[] = $v;
|
||||
}
|
||||
$variant = implode( ',', $var );
|
||||
}else{
|
||||
$variant = $font_variants;
|
||||
}
|
||||
}else{
|
||||
$variant = 'regular';
|
||||
}
|
||||
}else{ //Standard Web Safe Fonts
|
||||
if( array_key_exists( $font_family, $websafe_fonts ) ){
|
||||
$variants = $websafe_fonts[ $font_family ][ 'variants' ];
|
||||
if( in_array( $font_variants, $variants ) ){
|
||||
if( $body ){ //LOAD ALL VARIANTS FOR BODY FONT
|
||||
foreach( $variants as $v ){
|
||||
$var[] = $v;
|
||||
}
|
||||
$variant = implode( ',', $var );
|
||||
}else{
|
||||
$variant = $font_variants;
|
||||
}
|
||||
}else{
|
||||
$variant = 'regular';
|
||||
}
|
||||
}
|
||||
}
|
||||
return $variant;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if( ! function_exists( 'zeitfresser_get_google_fonts' ) ) {
|
||||
/**
|
||||
* Get Google Fonts
|
||||
*/
|
||||
function zeitfresser_get_google_fonts(){
|
||||
$webfonts_json = @file_get_contents( get_template_directory_uri() . '/inc/blocks/font-family/inc/google-webfonts.json', true );
|
||||
$fonts = json_decode( $webfonts_json, true );
|
||||
|
||||
$google_fonts = array();
|
||||
|
||||
if ( is_array( $fonts ) ) {
|
||||
foreach ( $fonts['items'] as $font ) {
|
||||
$google_fonts[ $font['family'] ] = array(
|
||||
'variants' => $font['variants'],
|
||||
);
|
||||
}
|
||||
}
|
||||
return $google_fonts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
jQuery( function( $ ) {
|
||||
|
||||
wp.customize('main_font_family',function ( value ) {
|
||||
value.bind(function ( to ) {
|
||||
$("head").append("<link href='https://fonts.googleapis.com/css?family=" + to + ":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>");
|
||||
document.body.style.setProperty('--primary-font', to);
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
||||
} );
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
add_action( 'customize_register', 'zeitfresser_main_font_family' );
|
||||
function zeitfresser_main_font_family( $wp_customize ) {
|
||||
|
||||
$wp_customize->add_setting( 'main_font_family', array(
|
||||
'default' => zeitfresser_get_default_main_font_family(),
|
||||
'transport' => 'postMessage',
|
||||
'sanitize_callback' => 'zeitfresser_sanitize_google_fonts'
|
||||
) );
|
||||
|
||||
$wp_customize->add_control( 'main_font_family', array(
|
||||
'settings' => 'main_font_family',
|
||||
'label' => esc_html__( 'Primary Font', 'zeitfresser' ),
|
||||
'section' => 'daisy_blog_font_customization_section',
|
||||
'type' => 'select',
|
||||
'choices' => zeitfresser_google_fonts( zeitfresser_free_pro() ),
|
||||
) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
add_action( 'customize_preview_init', 'zeitfresser_main_font_family_enqueue_scripts' );
|
||||
function zeitfresser_main_font_family_enqueue_scripts() {
|
||||
|
||||
$main_font_family = esc_attr( get_theme_mod( 'main_font_family', zeitfresser_get_default_main_font_family() ) );
|
||||
|
||||
wp_enqueue_script( 'graphthemes-main-font-family-customizer', get_template_directory_uri() . '/inc/blocks/font-family/main/customizer-main-font-family.js', array('jquery'), '', true );
|
||||
}
|
||||
|
||||
|
||||
add_action( 'wp_enqueue_scripts', 'zeitfresser_main_font_family_dynamic_css' );
|
||||
function zeitfresser_main_font_family_dynamic_css() {
|
||||
|
||||
$main_font_family = esc_attr( get_theme_mod( 'main_font_family', zeitfresser_get_default_main_font_family() ) );
|
||||
|
||||
$dynamic_css = ":root { --primary-font: $main_font_family; }";
|
||||
|
||||
wp_add_inline_style( 'zeitfresser', $dynamic_css );
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
jQuery( function( $ ) {
|
||||
|
||||
wp.customize('secondary_font_family',function ( value ) {
|
||||
value.bind(function ( to ) {
|
||||
$("head").append("<link href='https://fonts.googleapis.com/css?family=" + to + ":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>");
|
||||
document.body.style.setProperty('--secondary-font', to);
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
||||
} );
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
add_action( 'customize_register', 'zeitfresser_secondary_font_family' );
|
||||
function zeitfresser_secondary_font_family( $wp_customize ) {
|
||||
|
||||
$wp_customize->add_setting( 'secondary_font_family', array(
|
||||
'default' => zeitfresser_get_default_secondary_font_family(),
|
||||
'transport' => 'postMessage',
|
||||
'sanitize_callback' => 'zeitfresser_sanitize_google_fonts'
|
||||
) );
|
||||
|
||||
$wp_customize->add_control( 'secondary_font_family', array(
|
||||
'settings' => 'secondary_font_family',
|
||||
'label' => esc_html__( 'Secondary Font', 'zeitfresser' ),
|
||||
'section' => 'daisy_blog_font_customization_section',
|
||||
'type' => 'select',
|
||||
'choices' => zeitfresser_google_fonts( zeitfresser_free_pro() ),
|
||||
) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
add_action( 'customize_preview_init', 'zeitfresser_secondary_font_family_enqueue_scripts' );
|
||||
function zeitfresser_secondary_font_family_enqueue_scripts() {
|
||||
|
||||
$secondary_font_family = esc_attr( get_theme_mod( 'secondary_font_family', zeitfresser_get_default_secondary_font_family() ) );
|
||||
|
||||
|
||||
wp_enqueue_script( 'graphthemes-secondary-font-family-customizer', get_template_directory_uri() . '/inc/blocks/font-family/secondary/customizer-secondary-font-family.js', array('jquery'), '', true );
|
||||
}
|
||||
|
||||
|
||||
add_action( 'wp_enqueue_scripts', 'zeitfresser_secondary_font_family_dynamic_css' );
|
||||
function zeitfresser_secondary_font_family_dynamic_css() {
|
||||
|
||||
$secondary_font_family = esc_attr( get_theme_mod( 'secondary_font_family', zeitfresser_get_default_secondary_font_family() ) );
|
||||
|
||||
|
||||
$dynamic_css = ":root { --secondary-font: $secondary_font_family; }";
|
||||
|
||||
wp_add_inline_style( 'zeitfresser', $dynamic_css );
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
jQuery( function( $ ) {
|
||||
|
||||
wp.customize('site_identity_font_family',function ( value ) {
|
||||
value.bind(function ( to ) {
|
||||
$("head").append("<link rel='stylesheet' href='https://fonts.googleapis.com/css?family="+to+":200,300,400,500,600,700,800,900|' rel='stylesheet' type='text/css'>");
|
||||
document.body.style.setProperty('--site-identity-font-family', to);
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
||||
} );
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
add_action( 'customize_register', 'zeitfresser_site_identity_font_family' );
|
||||
function zeitfresser_site_identity_font_family( $wp_customize ) {
|
||||
|
||||
$wp_customize->add_setting( 'site_identity_font_family', array(
|
||||
'default' => zeitfresser_get_default_site_identity_font_family(),
|
||||
'transport' => 'postMessage',
|
||||
'sanitize_callback' => 'zeitfresser_sanitize_google_fonts'
|
||||
) );
|
||||
|
||||
$wp_customize->add_control( 'site_identity_font_family', array(
|
||||
'settings' => 'site_identity_font_family',
|
||||
'label' => esc_html__( 'Site Title/Tagline Font', 'zeitfresser' ),
|
||||
'section' => 'title_tagline',
|
||||
'type' => 'select',
|
||||
'choices' => zeitfresser_google_fonts( zeitfresser_free_pro() ),
|
||||
) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
add_action( 'customize_preview_init', 'zeitfresser_site_identity_font_family_enqueue_scripts' );
|
||||
function zeitfresser_site_identity_font_family_enqueue_scripts() {
|
||||
|
||||
$site_identity_font_family = esc_attr( get_theme_mod( 'site_identity_font_family', zeitfresser_get_default_site_identity_font_family() ) );
|
||||
|
||||
|
||||
wp_enqueue_script( 'graphthemes-site-identity-font-family-customizer', get_template_directory_uri() . '/inc/blocks/font-family/site-identity/customizer-site-identity-font-family.js', array('jquery'), '', true );
|
||||
}
|
||||
|
||||
|
||||
add_action( 'wp_enqueue_scripts', 'zeitfresser_site_identity_font_family_dynamic_css' );
|
||||
function zeitfresser_site_identity_font_family_dynamic_css() {
|
||||
|
||||
$site_identity_font_family = esc_attr( get_theme_mod( 'site_identity_font_family', zeitfresser_get_default_site_identity_font_family() ) );
|
||||
|
||||
$dynamic_css = ":root { --site-identity-font-family: $site_identity_font_family; }";
|
||||
|
||||
wp_add_inline_style( 'zeitfresser', $dynamic_css );
|
||||
}
|
||||
Reference in New Issue
Block a user