| 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_fonts_to_font_selector' ), 10, 1 ); |
| 60 |
} |
| 61 |
|
| 62 |
public function add_fonts_to_font_selector( $fonts ) { |
| 63 |
if ( empty( $fonts ) ) { |
| 64 |
$fonts = array(); |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! $this->is_supported() ) { |
| 68 |
return $fonts; |
| 69 |
} |
| 70 |
|
| 71 |
$fonts = array_merge( $fonts, $this->get_fonts() ); |
| 72 |
|
| 73 |
return $fonts; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Preprocess the cloud fonts configuration. |
| 78 |
* |
| 79 |
* Convert the cloud font list to a format suitable for Customify. |
| 80 |
* |
| 81 |
* @since 2.7.0 |
| 82 |
* |
| 83 |
* @param array $config |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function preprocess_config( $config ) { |
| 88 |
if ( empty( $config ) ) { |
| 89 |
return $config; |
| 90 |
} |
| 91 |
|
| 92 |
$new_config = array(); |
| 93 |
foreach ( $config as $font_id => $font_config ) { |
| 94 |
if ( empty( $font_config['font_family'] ) || empty( $font_config['stylesheet'] ) ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
$new_config[ $font_config['font_family'] ] = $this->preprocess_font_config( $font_config ); |
| 100 |
} |
| 101 |
|
| 102 |
return $new_config; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Preprocess a cloud font config before using it. |
| 107 |
* |
| 108 |
* @since 2.7.0 |
| 109 |
* |
| 110 |
* @param array $font_config |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
private function preprocess_font_config( $font_config ) { |
| 115 |
if ( empty( $font_config ) ) { |
| 116 |
return $font_config; |
| 117 |
} |
| 118 |
|
| 119 |
// We need to convert the received data structure to the one expected by Customify. |
| 120 |
return array( |
| 121 |
'family' => $font_config['font_family'], |
| 122 |
'src' => $font_config['stylesheet'], |
| 123 |
'variants' => empty( $font_config['variants'] ) ? array() : $font_config['variants'], |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the cloud fonts configuration. |
| 129 |
* |
| 130 |
* @since 2.7.0 |
| 131 |
* |
| 132 |
* @param bool $skip_cache Optional. Whether to use the cached config or fetch a new one. |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function get_fonts( $skip_cache = false ) { |
| 137 |
// Make sure that the Design Assets class is loaded. |
| 138 |
require_once 'lib/class-customify-design-assets.php'; |
| 139 |
|
| 140 |
// Get the design assets data. |
| 141 |
$design_assets = Customify_Design_Assets::instance()->get( $skip_cache ); |
| 142 |
if ( false === $design_assets || empty( $design_assets['cloud_fonts'] ) ) { |
| 143 |
$config = $this->get_default_config(); |
| 144 |
} else { |
| 145 |
$config = $design_assets['cloud_fonts']; |
| 146 |
} |
| 147 |
|
| 148 |
return apply_filters( 'customify_get_cloud_fonts', $config ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the default (hard-coded) cloud fonts configuration. |
| 153 |
* |
| 154 |
* This is only a fallback config in case we can't communicate with the cloud, the first time. |
| 155 |
* |
| 156 |
* @since 2.7.0 |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
protected function get_default_config() { |
| 161 |
$default_config = array( |
| 162 |
); |
| 163 |
|
| 164 |
return apply_filters( 'customify_style_manager_default_cloud_fonts', $default_config ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Determine if Cloud Fonts are supported. |
| 169 |
* |
| 170 |
* @since 2.7.0 |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
public function is_supported() { |
| 175 |
// For now we will only use the fact that Style Manager is supported. |
| 176 |
return apply_filters( 'style_manager_cloud_fonts_are_supported', Customify_Style_Manager::instance()->is_supported() ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Main Customify_Cloud_Fonts Instance |
| 181 |
* |
| 182 |
* Ensures only one instance of Customify_Cloud_Fonts is loaded or can be loaded. |
| 183 |
* |
| 184 |
* @since 2.7.0 |
| 185 |
* @static |
| 186 |
* |
| 187 |
* @return Customify_Cloud_Fonts Main Customify_Cloud_Fonts instance |
| 188 |
*/ |
| 189 |
public static function instance() { |
| 190 |
|
| 191 |
if ( is_null( self::$_instance ) ) { |
| 192 |
self::$_instance = new self(); |
| 193 |
} |
| 194 |
return self::$_instance; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Cloning is forbidden. |
| 199 |
* |
| 200 |
* @since 2.7.0 |
| 201 |
*/ |
| 202 |
public function __clone() { |
| 203 |
|
| 204 |
_doing_it_wrong( __FUNCTION__,esc_html__( 'You should not do that!', 'customify' ), null ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Unserializing instances of this class is forbidden. |
| 209 |
* |
| 210 |
* @since 2.7.0 |
| 211 |
*/ |
| 212 |
public function __wakeup() { |
| 213 |
|
| 214 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
endif; |
| 219 |
|