admin-bar
1 year ago
client-migration
6 months ago
compatibility
1 year ago
customizer
1 year ago
freemius
8 months ago
menu-icons
1 year ago
metabox
1 year ago
onboarding
3 months ago
panel
9 months ago
post-settings
6 months ago
preloader
1 year ago
shortcodes
1 year ago
themepanel
1 year ago
widgets
8 months ago
wizard
3 years ago
adobe-font.php
1 year ago
custom-code.php
8 months ago
dashboard.php
1 year ago
image-resizer.php
1 year ago
jshrink.php
3 years ago
ocean-extra-strings.php
3 years ago
plugins-tab.php
1 year ago
update-message.php
1 year ago
utils.php
1 year ago
walker.php
4 years ago
adobe-font.php
304 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Adobe Fonts Integration |
| 4 | * |
| 5 | * @package Ocean_Extra |
| 6 | * @author OceanWP |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 10 | |
| 11 | if ( ! class_exists( 'OceanWP_Adobe_Font' ) ) { |
| 12 | |
| 13 | /** |
| 14 | * Register Adobe Fonts Class |
| 15 | */ |
| 16 | class OceanWP_Adobe_Font { |
| 17 | |
| 18 | /** |
| 19 | * Active status. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | public $active_status; |
| 24 | |
| 25 | /** |
| 26 | * Single instance of the class. |
| 27 | * |
| 28 | * @var OceanWP_Adobe_Font |
| 29 | */ |
| 30 | private static $_instance = null; |
| 31 | |
| 32 | /** |
| 33 | * Adobe Fonts base instance. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | private static $font_base = 'oe-adobe-fonts'; |
| 38 | const REMOTE_URL_BASE = 'https://use.typekit.net/%s.css'; |
| 39 | const OE_ADOBE_FONTS_VER = '1.0.0'; |
| 40 | |
| 41 | /** |
| 42 | * Main OceanWP_Adobe_Font Instance |
| 43 | * |
| 44 | * @static |
| 45 | * @see OceanWP_Adobe_Font() |
| 46 | * @return Main OceanWP_Adobe_Font instance |
| 47 | */ |
| 48 | public static function instance() { |
| 49 | if ( is_null( self::$_instance ) ) { |
| 50 | self::$_instance = new self(); |
| 51 | } |
| 52 | return self::$_instance; |
| 53 | } // End instance(). |
| 54 | |
| 55 | /** |
| 56 | * Constructor |
| 57 | */ |
| 58 | public function __construct() { |
| 59 | $enable_module = $this->get_active_status(); |
| 60 | if ( empty( $enable_module ) ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $enable_elementor_mode = get_option( 'owp_adobe_fonts_integration_enable_elementor', 0 ); |
| 65 | $enable_customizer_mode = get_option( 'owp_adobe_fonts_integration_enable_customizer', 0 ); |
| 66 | |
| 67 | if ( ! empty( $enable_elementor_mode ) || ! empty( $enable_customizer_mode ) ) { |
| 68 | add_action( 'wp_enqueue_scripts', array( $this, 'adobe_font_css' ) ); |
| 69 | } |
| 70 | |
| 71 | if ( ! empty( $enable_elementor_mode ) ) { |
| 72 | // Elementor page builder. |
| 73 | add_filter( 'elementor/fonts/groups', array( $this, 'elementor_group' ) ); |
| 74 | add_filter( 'elementor/fonts/additional_fonts', array( $this, 'add_elementor_fonts' ) ); |
| 75 | } |
| 76 | |
| 77 | if ( ! empty( $enable_customizer_mode ) ) { |
| 78 | add_action( 'ocean_customizer_fonts', array( $this, 'render_customizer_group' ) ); |
| 79 | add_action( 'enqueue_block_editor_assets', array( $this, 'adobe_font_css' ) ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Cloning is forbidden. |
| 85 | */ |
| 86 | public function __clone() { |
| 87 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?' ), '1.0.0' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Deserializing instances of this class is forbidden. |
| 92 | */ |
| 93 | public function __wakeup() { |
| 94 | _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?' ), '1.0.0' ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return Adobe Fonts integration project ID. |
| 99 | */ |
| 100 | public function get_project_id() { |
| 101 | return get_option( 'owp_adobe_fonts_integration_project_id' ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Return the status of the Adobe Fonts integration option. |
| 106 | */ |
| 107 | public function get_active_status() { |
| 108 | return get_option( 'owp_adobe_fonts_integration' ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Adobe Fonts project ID validation |
| 113 | * |
| 114 | * @throws Exception If data missing or invalid. |
| 115 | */ |
| 116 | public function check_project_id() { |
| 117 | try { |
| 118 | $project_id = get_option( 'owp_adobe_fonts_integration_project_id' ); |
| 119 | |
| 120 | if ( empty( $project_id ) ) { |
| 121 | throw new Exception( 'Project ID can not be empty.' ); |
| 122 | } |
| 123 | |
| 124 | $adobe_uri = 'https://typekit.com/api/v1/json/kits/' . $project_id . '/published'; |
| 125 | $response = wp_remote_get( |
| 126 | esc_url_raw( $adobe_uri ), |
| 127 | array( |
| 128 | 'timeout' => '30', |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 133 | throw new Exception( 'Project ID is invalid.' ); |
| 134 | } |
| 135 | |
| 136 | $this->save( $project_id, $response ); |
| 137 | |
| 138 | return array( |
| 139 | 'status' => 'success', |
| 140 | 'message' => 'Project ID is valid', |
| 141 | ); |
| 142 | } catch ( Exception $e ) { |
| 143 | delete_option( 'oe-adobe-fonts' ); |
| 144 | |
| 145 | return array( |
| 146 | 'status' => 'failed', |
| 147 | 'message' => $e->getMessage(), |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Save settings for Adobe Fonts |
| 154 | * |
| 155 | * @param string $project_id Returns Adobe fonts project ID. |
| 156 | * @param string $response_data Check if the project ID exists. |
| 157 | */ |
| 158 | public function save( $project_id, $response_data ) { |
| 159 | $project_info = array(); |
| 160 | |
| 161 | $data = json_decode( wp_remote_retrieve_body( $response_data ), true ); |
| 162 | $families = $data['kit']['families']; |
| 163 | |
| 164 | foreach ( $families as $family ) { |
| 165 | |
| 166 | $family_name = str_replace( ' ', '-', $family['name'] ); |
| 167 | |
| 168 | $project_info[ $family_name ] = array( |
| 169 | 'family' => $family_name, |
| 170 | 'fallback' => str_replace( '"', '', $family['css_stack'] ), |
| 171 | 'weights' => array(), |
| 172 | ); |
| 173 | |
| 174 | foreach ( $family['variations'] as $variation ) { |
| 175 | |
| 176 | $variations = str_split( $variation ); |
| 177 | |
| 178 | switch ( $variations[0] ) { |
| 179 | case 'n': |
| 180 | $style = 'normal'; |
| 181 | break; |
| 182 | default: |
| 183 | $style = 'normal'; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | $weight = $variations[1] . '00'; |
| 188 | |
| 189 | if ( ! in_array( $weight, $project_info[ $family_name ]['weights'], true ) ) { |
| 190 | $project_info[ $family_name ]['weights'][] = $weight; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | $project_info[ $family_name ]['slug'] = $family['slug']; |
| 195 | $project_info[ $family_name ]['css_names'] = $family['css_names']; |
| 196 | } |
| 197 | |
| 198 | $options = array(); |
| 199 | $options['oe-adobe-font-id'] = ! empty( $project_id ) ? sanitize_text_field( $project_id ) : ''; |
| 200 | $options['oe-adobe-fonts-list'] = isset( $project_id ) ? $project_info : ''; |
| 201 | |
| 202 | update_option( 'oe-adobe-fonts', $options ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add Custom Font group to elementor font list. |
| 207 | * |
| 208 | * @param array $font_groups Add new fonts to the existing group of fonts. |
| 209 | */ |
| 210 | public function elementor_group( $font_groups ) { |
| 211 | $new_group[ self::$font_base ] = __( 'OE Adobe Fonts', 'ocean-extra' ); |
| 212 | $font_groups = $new_group + $font_groups; |
| 213 | return $font_groups; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Render Adobe fonts in the Customizer. |
| 218 | */ |
| 219 | public function render_customizer_group() { |
| 220 | $fonts_list = get_option( 'oe-adobe-fonts' ); |
| 221 | $all_fonts = isset( $fonts_list['oe-adobe-fonts-list'] ) ? $fonts_list['oe-adobe-fonts-list'] : array(); |
| 222 | $fonts_array = array(); |
| 223 | |
| 224 | if ( ! empty($all_fonts) ) { |
| 225 | $adobeFonts = []; |
| 226 | foreach ($all_fonts as $font_family_name => $fonts_url) { |
| 227 | $font_slug = isset($fonts_url['slug']) ? $fonts_url['slug'] : ''; |
| 228 | $font_css = isset($fonts_url['css_names'][0]) ? $fonts_url['css_names'][0] : $font_slug; |
| 229 | $font_weights = isset($fonts_url['weights']) ? $fonts_url['weights'] : []; |
| 230 | $adobeFonts[] = [ |
| 231 | 'value' => esc_attr($font_css), |
| 232 | 'label' => esc_html($font_slug), |
| 233 | 'variants' => $font_weights |
| 234 | ]; |
| 235 | } |
| 236 | $fonts_array[] = [ |
| 237 | 'label' => esc_attr__('OE Adobe Fonts', 'ocean-extra'), |
| 238 | 'options' => $adobeFonts, |
| 239 | ]; |
| 240 | } |
| 241 | |
| 242 | $html = wp_json_encode($fonts_array); |
| 243 | |
| 244 | echo $html; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Add Custom Fonts to the Elementor. |
| 249 | * |
| 250 | * @param array $fonts Add Adobe Fonts to the list of available fonts. |
| 251 | */ |
| 252 | |
| 253 | public function add_elementor_fonts( $fonts ) { |
| 254 | $fonts_list = get_option( 'oe-adobe-fonts' ); |
| 255 | $custom_fonts = array(); |
| 256 | |
| 257 | // Check if $fonts_list is an array and if 'oe-adobe-fonts-list' key exists. |
| 258 | if ( is_array( $fonts_list ) && ! empty( $fonts_list['oe-adobe-fonts-list'] ) ) { |
| 259 | $all_fonts = $fonts_list['oe-adobe-fonts-list']; |
| 260 | foreach ( $all_fonts as $font_family_name => $fonts_url ) { |
| 261 | $font_slug = isset( $fonts_url['slug'] ) ? $fonts_url['slug'] : ''; |
| 262 | $font_css = isset( $fonts_url['css_names'][0] ) ? $fonts_url['css_names'][0] : $font_slug; |
| 263 | $custom_fonts[ $font_css ] = self::$font_base; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return array_merge( $fonts, $custom_fonts ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Adobe Fonts CSS |
| 272 | */ |
| 273 | public function adobe_font_css() { |
| 274 | $adobe_remote_url = $this->get_adobe_url(); |
| 275 | if ( false !== $adobe_remote_url ) { |
| 276 | wp_enqueue_style( 'oe-adobe-css', $adobe_remote_url, array(), self::OE_ADOBE_FONTS_VER ); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Get URL of the Adobe Fonts project ID. |
| 282 | */ |
| 283 | private function get_adobe_url() { |
| 284 | $adode_fonts_info = get_option( 'oe-adobe-fonts' ); |
| 285 | if ( empty( $adode_fonts_info['oe-adobe-fonts-list'] ) ) { |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | return sprintf( self::REMOTE_URL_BASE, $adode_fonts_info['oe-adobe-font-id'] ); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Returns the main instance of OceanWP_Adobe_Font to prevent the need to use globals. |
| 296 | * |
| 297 | * @return object OceanWP_Adobe_Font |
| 298 | */ |
| 299 | function OceanWP_Adobe_Font() { |
| 300 | return OceanWP_Adobe_Font::instance(); |
| 301 | } // End OceanWP_Adobe_Font function. |
| 302 | |
| 303 | OceanWP_Adobe_Font(); |
| 304 |