ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-kirki-fonts.php (6488B)


      1 <?php
      2 /**
      3  * A simple object containing properties for fonts.
      4  *
      5  * @package     Kirki
      6  * @category    Core
      7  * @author      Aristeides Stathopoulos
      8  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
      9  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     10  * @since       1.0
     11  */
     12 
     13 if ( ! class_exists( 'Kirki_Fonts' ) ) {
     14 
     15 	/**
     16 	 * The Kirki_Fonts object.
     17 	 */
     18 	final class Kirki_Fonts {
     19 
     20 		/**
     21 		 * The mode we'll be using to add google fonts.
     22 		 * This is a todo item, not yet functional.
     23 		 *
     24 		 * @static
     25 		 * @todo
     26 		 * @access public
     27 		 * @var string
     28 		 */
     29 		public static $mode = 'link';
     30 
     31 		/**
     32 		 * Holds a single instance of this object.
     33 		 *
     34 		 * @static
     35 		 * @access private
     36 		 * @var null|object
     37 		 */
     38 		private static $instance = null;
     39 
     40 		/**
     41 		 * An array of our google fonts.
     42 		 *
     43 		 * @static
     44 		 * @access public
     45 		 * @var null|object
     46 		 */
     47 		public static $google_fonts = null;
     48 
     49 		/**
     50 		 * The class constructor.
     51 		 */
     52 		private function __construct() {}
     53 
     54 		/**
     55 		 * Get the one, true instance of this class.
     56 		 * Prevents performance issues since this is only loaded once.
     57 		 *
     58 		 * @return object Kirki_Fonts
     59 		 */
     60 		public static function get_instance() {
     61 			if ( null === self::$instance ) {
     62 				self::$instance = new self();
     63 			}
     64 			return self::$instance;
     65 		}
     66 
     67 		/**
     68 		 * Compile font options from different sources.
     69 		 *
     70 		 * @return array    All available fonts.
     71 		 */
     72 		public static function get_all_fonts() {
     73 			$standard_fonts = self::get_standard_fonts();
     74 			$google_fonts   = self::get_google_fonts();
     75 
     76 			return apply_filters( 'kirki/fonts/all', array_merge( $standard_fonts, $google_fonts ) );
     77 		}
     78 
     79 		/**
     80 		 * Return an array of standard websafe fonts.
     81 		 *
     82 		 * @return array    Standard websafe fonts.
     83 		 */
     84 		 public static function get_standard_fonts() {
     85 			$i18n = Kirki_l10n::get_strings();
     86 			$standard_fonts = array(
     87 				'serif' => array(
     88 					'label' => $i18n['serif'],
     89 					'stack' => 'Georgia,Times, Times New Roman ,serif',
     90 				),
     91 				'sans-serif' => array(
     92 					'label'  => $i18n['sans-serif'],
     93 					'stack'  => 'Helvetica,Arial,sans-serif',
     94 				),
     95 				'monospace' => array(
     96 					'label' => $i18n['monospace'],
     97 					'stack' => 'Monaco,"Lucida Sans Typewriter","Lucida Typewriter","Courier New",Courier,monospace',
     98 				),
     99 			);
    100 			return apply_filters( 'kirki/fonts/standard_fonts', $standard_fonts );
    101 		}
    102 
    103 		/**
    104 		 * Return an array of backup fonts based on the font-category
    105 		 *
    106 		 * @return array
    107 		 */
    108 		public static function get_backup_fonts() {
    109 			$backup_fonts = array(
    110 				'sans-serif'  => 'Helvetica, Arial, sans-serif',
    111 				'serif'       => 'Georgia, serif',
    112 				'display'     => '"Comic Sans MS", cursive, sans-serif',
    113 				'handwriting' => '"Comic Sans MS", cursive, sans-serif',
    114 				'monospace'   => '"Lucida Console", Monaco, monospace',
    115 			);
    116 			return apply_filters( 'kirki/fonts/backup_fonts', $backup_fonts );
    117 		}
    118 
    119 		/**
    120 		 * Return an array of all available Google Fonts.
    121 		 *
    122 		 * @return array    All Google Fonts.
    123 		 */
    124 		public static function get_google_fonts() {
    125 
    126 			if ( null === self::$google_fonts || empty( self::$google_fonts ) ) {
    127 
    128 				$fonts = include wp_normalize_path( Kirki::$path . '/includes/webfonts.php' );
    129 
    130 				$google_fonts = array();
    131 				if ( is_array( $fonts ) ) {
    132 					foreach ( $fonts['items'] as $font ) {
    133 						$google_fonts[ $font['family'] ] = array(
    134 							'label'    => $font['family'],
    135 							'variants' => $font['variants'],
    136 							'subsets'  => $font['subsets'],
    137 							'category' => $font['category'],
    138 						);
    139 					}
    140 				}
    141 
    142 				self::$google_fonts = apply_filters( 'kirki/fonts/google_fonts', $google_fonts );
    143 
    144 			}
    145 
    146 			return self::$google_fonts;
    147 
    148 		}
    149 
    150 		/**
    151 		 * Dummy function to avoid issues with backwards-compatibility.
    152 		 * This is not functional, but it will prevent PHP Fatal errors.
    153 		 *
    154 		 * @static
    155 		 * @access public
    156 		 */
    157 		public static function get_google_font_uri() {}
    158 
    159 		/**
    160 		 * Returns an array of all available subsets.
    161 		 *
    162 		 * @static
    163 		 * @access public
    164 		 * @return array
    165 		 */
    166 		public static function get_google_font_subsets() {
    167 			$i18n = Kirki_l10n::get_strings();
    168 			return array(
    169 				// 'all'          => $i18n['all'],
    170 				'cyrillic'     => $i18n['cyrillic'],
    171 				'cyrillic-ext' => $i18n['cyrillic-ext'],
    172 				'devanagari'   => $i18n['devanagari'],
    173 				'greek'        => $i18n['greek'],
    174 				'greek-ext'    => $i18n['greek-ext'],
    175 				'khmer'        => $i18n['khmer'],
    176 				// 'latin'        => $i18n['latin'],
    177 				'latin-ext'    => $i18n['latin-ext'],
    178 				'vietnamese'   => $i18n['vietnamese'],
    179 				'hebrew'       => $i18n['hebrew'],
    180 				'arabic'       => $i18n['arabic'],
    181 				'bengali'      => $i18n['bengali'],
    182 				'gujarati'     => $i18n['gujarati'],
    183 				'tamil'        => $i18n['tamil'],
    184 				'telugu'       => $i18n['telugu'],
    185 				'thai'         => $i18n['thai'],
    186 			);
    187 		}
    188 
    189 		/**
    190 		 * Returns an array of all available variants.
    191 		 *
    192 		 * @static
    193 		 * @access public
    194 		 * @return array
    195 		 */
    196 		public static function get_all_variants() {
    197 			$i18n = Kirki_l10n::get_strings();
    198 			return array(
    199 				'100'       => $i18n['ultra-light'],
    200 				'100italic' => $i18n['ultra-light-italic'],
    201 				'200'       => $i18n['light'],
    202 				'200italic' => $i18n['light-italic'],
    203 				'300'       => $i18n['book'],
    204 				'300italic' => $i18n['book-italic'],
    205 				'regular'   => $i18n['regular'],
    206 				'400'   => $i18n['regular'],
    207 				'italic'    => $i18n['italic'],
    208 				'500'       => $i18n['medium'],
    209 				'500italic' => $i18n['medium-italic'],
    210 				'600'       => $i18n['semi-bold'],
    211 				'600italic' => $i18n['semi-bold-italic'],
    212 				'700'       => $i18n['bold'],
    213 				'700italic' => $i18n['bold-italic'],
    214 				'800'       => $i18n['extra-bold'],
    215 				'800italic' => $i18n['extra-bold-italic'],
    216 				'900'       => $i18n['ultra-bold'],
    217 				'900italic' => $i18n['ultra-bold-italic'],
    218 			);
    219 		}
    220 
    221 		/**
    222 		 * Determine if a font-name is a valid google font or not.
    223 		 *
    224 		 * @static
    225 		 * @access public
    226 		 * @param string $fontname The name of the font we want to check.
    227 		 * @return bool
    228 		 */
    229 		public static function is_google_font( $fontname ) {
    230 			return ( array_key_exists( $fontname, self::$google_fonts ) );
    231 		}
    232 
    233 		/**
    234 		 * Gets available options for a font.
    235 		 *
    236 		 * @static
    237 		 * @access public
    238 		 * @return array
    239 		 */
    240 		public static function get_font_choices() {
    241 			$fonts = self::get_all_fonts();
    242 			$fonts_array = array();
    243 			foreach ( $fonts as $key => $args ) {
    244 				$fonts_array[ $key ] = $key;
    245 			}
    246 			return $fonts_array;
    247 		}
    248 	}
    249 }