| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is the class that handles the logic for Cloud Fonts. |
| 4 |
* |
| 5 |
* @see https://pixelgrade.com |
| 6 |
* @author Pixelgrade |
| 7 |
* @since 2.7.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
if ( ! class_exists( 'Customify_Cloud_Fonts' ) ) : |
| 15 |
|
| 16 |
class Customify_Cloud_Fonts { |
| 17 |
|
| 18 |
/** |
| 19 |
* Holds the only instance of this class. |
| 20 |
* @var null|Customify_Cloud_Fonts |
| 21 |
* @access protected |
| 22 |
* @since 2.7.0 |
| 23 |
*/ |
| 24 |
protected static $_instance = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @since 2.7.0 |
| 30 |
*/ |
| 31 |
protected function __construct() { |
| 32 |
$this->init(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Initialize this module. |
| 37 |
* |
| 38 |
* @since 2.7.0 |
| 39 |
*/ |
| 40 |
public function init() { |
| 41 |
// Hook up. |
| 42 |
$this->add_hooks(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Initiate our hooks |
| 47 |
* |
| 48 |
* @since 2.7.0 |
| 49 |
*/ |
| 50 |
public function add_hooks() { |
| 51 |
/* |
| 52 |
* Handle the cloud fonts preprocessing. |
| 53 |
*/ |
| 54 |
add_filter( 'customify_get_cloud_fonts', array( $this, 'preprocess_config' ), 5, 1 ); |
| 55 |
|
| 56 |
/* |
| 57 |
* Add the cloud fonts to the Font Selector |
| 58 |
*/ |
| 59 |
add_filter( 'customify_cloud_fonts', array( $this, 'add_cloud_fonts_to_font_selector' ), 10, 1 ); |
| 60 |
|
| 61 |
/* |
| 62 |
* Handle the cloud fonts preprocessing. |
| 63 |
*/ |
| 64 |
add_filter( 'customify_get_cloud_system_fonts', array( $this, 'preprocess_config' ), 5, 1 ); |
| 65 |
|
| 66 |
/* |
| 67 |
* Add the cloud system fonts to the Font Selector |
| 68 |
*/ |
| 69 |
add_filter( 'customify_system_fonts', array( $this, 'add_cloud_system_fonts_to_font_selector' ), 10, 1 ); |
| 70 |
|
| 71 |
/* |
| 72 |
* Add the cloud font categories to the list. |
| 73 |
*/ |
| 74 |
add_filter( 'customify_font_categories', array( $this, 'add_cloud_font_categories' ), 10, 1 ); |
| 75 |
} |
| 76 |
|
| 77 |
public function add_cloud_fonts_to_font_selector( $fonts ) { |
| 78 |
if ( empty( $fonts ) ) { |
| 79 |
$fonts = []; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $this->is_supported() ) { |
| 83 |
return $fonts; |
| 84 |
} |
| 85 |
|
| 86 |
$fonts = array_merge( $fonts, $this->get_cloud_fonts() ); |
| 87 |
|
| 88 |
return $fonts; |
| 89 |
} |
| 90 |
|
| 91 |
public function add_cloud_system_fonts_to_font_selector( $fonts ) { |
| 92 |
if ( empty( $fonts ) ) { |
| 93 |
$fonts = []; |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! $this->is_supported() ) { |
| 97 |
return array_merge( $fonts, $this->get_default_system_fonts() ); |
| 98 |
} |
| 99 |
|
| 100 |
$fonts = array_merge( $fonts, $this->get_system_fonts() ); |
| 101 |
|
| 102 |
return $fonts; |
| 103 |
} |
| 104 |
|
| 105 |
public function add_cloud_font_categories( $categories ) { |
| 106 |
if ( empty( $categories ) ) { |
| 107 |
$categories = []; |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! $this->is_supported() ) { |
| 111 |
return $categories; |
| 112 |
} |
| 113 |
|
| 114 |
$categories = array_merge( $categories, $this->get_categories() ); |
| 115 |
|
| 116 |
return $categories; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Preprocess the cloud fonts configuration. |
| 121 |
* |
| 122 |
* Convert the cloud font list to a format suitable for Customify. |
| 123 |
* |
| 124 |
* @since 2.7.0 |
| 125 |
* |
| 126 |
* @param array $config |
| 127 |
* |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function preprocess_config( $config ) { |
| 131 |
if ( empty( $config ) ) { |
| 132 |
return $config; |
| 133 |
} |
| 134 |
|
| 135 |
$new_config = array(); |
| 136 |
foreach ( $config as $font_id => $font_config ) { |
| 137 |
if ( empty( $font_config['font_family'] ) ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
$new_config[ $font_config['font_family'] ] = $this->preprocess_font_config( $font_config ); |
| 143 |
} |
| 144 |
|
| 145 |
return $new_config; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Preprocess a cloud font config before using it. |
| 150 |
* |
| 151 |
* @since 2.7.0 |
| 152 |
* |
| 153 |
* @param array $font_config |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
private function preprocess_font_config( $font_config ) { |
| 158 |
if ( empty( $font_config ) ) { |
| 159 |
return $font_config; |
| 160 |
} |
| 161 |
|
| 162 |
// We need to convert the received data structure to the one expected by Customify. |
| 163 |
return array( |
| 164 |
'family' => $font_config['font_family'], |
| 165 |
'family_display' => empty( $font_config['font_family_display'] ) ? '' : $font_config['font_family_display'], |
| 166 |
'src' => empty( $font_config['stylesheet'] ) ? false : $font_config['stylesheet'], |
| 167 |
'variants' => empty( $font_config['variants'] ) ? [] : $font_config['variants'], |
| 168 |
'category' => empty( $font_config['category'] ) ? '' : $font_config['category'], |
| 169 |
'fallback_stack' => empty( $font_config['fallback_stack'] ) ? '' : $font_config['fallback_stack'], |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Get the cloud fonts configuration. |
| 175 |
* |
| 176 |
* @since 2.7.0 |
| 177 |
* |
| 178 |
* @param bool $skip_cache Optional. Whether to use the cached config or fetch a new one. |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public function get_cloud_fonts( $skip_cache = false ) { |
| 183 |
// Make sure that the Design Assets class is loaded. |
| 184 |
require_once 'lib/class-customify-design-assets.php'; |
| 185 |
|
| 186 |
// Get the design assets data. |
| 187 |
$design_assets = Customify_Design_Assets::instance()->get( $skip_cache ); |
| 188 |
if ( false === $design_assets || empty( $design_assets['cloud_fonts'] ) ) { |
| 189 |
$config = $this->get_default_cloud_fonts(); |
| 190 |
} else { |
| 191 |
$config = $design_assets['cloud_fonts']; |
| 192 |
} |
| 193 |
|
| 194 |
return apply_filters( 'customify_get_cloud_fonts', $config ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get the cloud standard fonts configuration. |
| 199 |
* |
| 200 |
* @since 2.8.0 |
| 201 |
* |
| 202 |
* @param bool $skip_cache Optional. Whether to use the cached config or fetch a new one. |
| 203 |
* |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function get_system_fonts( $skip_cache = false ) { |
| 207 |
// Make sure that the Design Assets class is loaded. |
| 208 |
require_once 'lib/class-customify-design-assets.php'; |
| 209 |
|
| 210 |
// Get the design assets data. |
| 211 |
$design_assets = Customify_Design_Assets::instance()->get( $skip_cache ); |
| 212 |
if ( false === $design_assets || empty( $design_assets['system_fonts'] ) ) { |
| 213 |
$config = $this->get_default_system_fonts(); |
| 214 |
} else { |
| 215 |
$config = $design_assets['system_fonts']; |
| 216 |
} |
| 217 |
|
| 218 |
return apply_filters( 'customify_get_cloud_system_fonts', $config ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get the font categories configuration. |
| 223 |
* |
| 224 |
* @since 2.8.0 |
| 225 |
* |
| 226 |
* @param bool $skip_cache Optional. Whether to use the cached config or fetch a new one. |
| 227 |
* |
| 228 |
* @return array |
| 229 |
*/ |
| 230 |
public function get_categories( $skip_cache = false ) { |
| 231 |
// Make sure that the Design Assets class is loaded. |
| 232 |
require_once 'lib/class-customify-design-assets.php'; |
| 233 |
|
| 234 |
// Get the design assets data. |
| 235 |
$design_assets = Customify_Design_Assets::instance()->get( $skip_cache ); |
| 236 |
if ( false === $design_assets || empty( $design_assets['font_categories'] ) ) { |
| 237 |
$categories = $this->get_default_categories(); |
| 238 |
} else { |
| 239 |
$categories = $design_assets['font_categories']; |
| 240 |
} |
| 241 |
|
| 242 |
return apply_filters( 'customify_get_cloud_font_categories', $categories ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Get the default (hard-coded) standard fonts configuration. |
| 247 |
* |
| 248 |
* This is only a fallback config in case we can't communicate with the cloud, the first time. |
| 249 |
* |
| 250 |
* @since 2.8.0 |
| 251 |
* |
| 252 |
* @return array |
| 253 |
*/ |
| 254 |
protected function get_default_system_fonts() { |
| 255 |
$default_system_fonts = array( |
| 256 |
"Arial, Helvetica, sans-serif" => "Arial, Helvetica, sans-serif", |
| 257 |
"'Arial Black', Gadget, sans-serif" => "'Arial Black', Gadget, sans-serif", |
| 258 |
"'Bookman Old Style', serif" => "'Bookman Old Style', serif", |
| 259 |
"'Comic Sans MS', cursive" => "'Comic Sans MS', cursive", |
| 260 |
"Courier, monospace" => "Courier, monospace", |
| 261 |
"Garamond, serif" => "Garamond, serif", |
| 262 |
"Georgia, serif" => "Georgia, serif", |
| 263 |
"Impact, Charcoal, sans-serif" => "Impact, Charcoal, sans-serif", |
| 264 |
"'Lucida Console', Monaco, monospace" => "'Lucida Console', Monaco, monospace", |
| 265 |
"'Lucida Sans Unicode', 'Lucida Grande', sans-serif" => "'Lucida Sans Unicode', 'Lucida Grande', sans-serif", |
| 266 |
"'MS Sans Serif', Geneva, sans-serif" => "'MS Sans Serif', Geneva, sans-serif", |
| 267 |
"'MS Serif', 'New York', sans-serif" => "'MS Serif', 'New York', sans-serif", |
| 268 |
"'Palatino Linotype', 'Book Antiqua', Palatino, serif" => "'Palatino Linotype', 'Book Antiqua', Palatino, serif", |
| 269 |
"Tahoma, Geneva, sans-serif" => "Tahoma, Geneva, sans-serif", |
| 270 |
"'Times New Roman', Times,serif" => "'Times New Roman', Times, serif", |
| 271 |
"'Trebuchet MS', Helvetica, sans-serif" => "'Trebuchet MS', Helvetica, sans-serif", |
| 272 |
"Verdana, Geneva, sans-serif" => "Verdana, Geneva, sans-serif", |
| 273 |
); |
| 274 |
|
| 275 |
return apply_filters( 'customify_style_manager_default_system_fonts', $default_system_fonts ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Get the default (hard-coded) cloud fonts configuration. |
| 280 |
* |
| 281 |
* This is only a fallback config in case we can't communicate with the cloud, the first time. |
| 282 |
* |
| 283 |
* @since 2.7.0 |
| 284 |
* |
| 285 |
* @return array |
| 286 |
*/ |
| 287 |
protected function get_default_cloud_fonts() { |
| 288 |
$default_config = []; |
| 289 |
|
| 290 |
return apply_filters( 'customify_style_manager_default_cloud_fonts', $default_config ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Get the default (hard-coded) font categories configuration. |
| 295 |
* |
| 296 |
* This is only a fallback config in case we can't communicate with the cloud, the first time. |
| 297 |
* |
| 298 |
* @since 2.8.0 |
| 299 |
* |
| 300 |
* @return array |
| 301 |
*/ |
| 302 |
protected function get_default_categories() { |
| 303 |
$default_categories = []; |
| 304 |
|
| 305 |
return apply_filters( 'customify_style_manager_default_cloud_font_categories', $default_categories ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Determine if Cloud Fonts are supported. |
| 310 |
* |
| 311 |
* @since 2.7.0 |
| 312 |
* |
| 313 |
* @return bool |
| 314 |
*/ |
| 315 |
public function is_supported() { |
| 316 |
// For now we will only use the fact that Style Manager is supported. |
| 317 |
return apply_filters( 'style_manager_cloud_fonts_are_supported', Customify_Style_Manager::instance()->is_supported() ); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Main Customify_Cloud_Fonts Instance |
| 322 |
* |
| 323 |
* Ensures only one instance of Customify_Cloud_Fonts is loaded or can be loaded. |
| 324 |
* |
| 325 |
* @since 2.7.0 |
| 326 |
* @static |
| 327 |
* |
| 328 |
* @return Customify_Cloud_Fonts Main Customify_Cloud_Fonts instance |
| 329 |
*/ |
| 330 |
public static function instance() { |
| 331 |
|
| 332 |
if ( is_null( self::$_instance ) ) { |
| 333 |
self::$_instance = new self(); |
| 334 |
} |
| 335 |
return self::$_instance; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Cloning is forbidden. |
| 340 |
* |
| 341 |
* @since 2.7.0 |
| 342 |
*/ |
| 343 |
public function __clone() { |
| 344 |
|
| 345 |
_doing_it_wrong( __FUNCTION__,esc_html__( 'You should not do that!', 'customify' ), null ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Unserializing instances of this class is forbidden. |
| 350 |
* |
| 351 |
* @since 2.7.0 |
| 352 |
*/ |
| 353 |
public function __wakeup() { |
| 354 |
|
| 355 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null ); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
endif; |
| 360 |
|