array( 'name' => "Sansita One", 'variant' => ''), array( 'name' => "Chivo", 'variant' => ''), array( 'name' => "Spinnaker", 'variant' => ''), array( 'name' => "Poller One", 'variant' => ''), array( 'name' => "Alike Angular", 'variant' => ''), array( 'name' => "Gochi Hand", 'variant' => ''), array( 'name' => "Poly", 'variant' => ''), array( 'name' => "Andada", 'variant' => ''), array( 'name' => "Federant", 'variant' => ''), array( 'name' => "Ubuntu Condensed", 'variant' => ''), array( 'name' => "Ubuntu Mono", 'variant' => ''), array( 'name' => "Sancreek", 'variant' => ''), array( 'name' => "Coda", 'variant' => ''), array( 'name' => "Rancho", 'variant' => ''), array( 'name' => "Satisfy", 'variant' => ''), array( 'name' => "Pinyon Script", 'variant' => ''), array( 'name' => "Vast Shadow", 'variant' => ''), array( 'name' => "Marck Script", 'variant' => ''), array( 'name' => "Salsa", 'variant' => ''), array( 'name' => "Amatic SC", 'variant' => ''), array( 'name' => "Quicksand", 'variant' => ''), array( 'name' => "Linden Hill", 'variant' => ''), array( 'name' => "Corben", 'variant' => ''), array( 'name' => "Creepster Caps", 'variant' => ''), array( 'name' => "Butcherman Caps", 'variant' => ''), array( 'name' => "Eater Caps", 'variant' => ''), array( 'name' => "Nosifer Caps", 'variant' => ''), array( 'name' => "Atomic Age", 'variant' => ''), array( 'name' => "Contrail One", 'variant' => ''), array( 'name' => "Jockey One", 'variant' => ''), array( 'name' => "Cabin Sketch", 'variant' => ':r,b'), array( 'name' => "Cabin Condensed", 'variant' => ':r,b'), array( 'name' => "Fjord One", 'variant' => ''), array( 'name' => "Rametto One", 'variant' => ''), array( 'name' => "Mate", 'variant' => ':r,i'), array( 'name' => "Mate SC", 'variant' => ''), array( 'name' => "Arapey", 'variant' => ':r,i'), array( 'name' => "Supermercado One", 'variant' => ''), array( 'name' => "Petrona", 'variant' => ''), array( 'name' => "Lancelot", 'variant' => ''), array( 'name' => "Convergence", 'variant' => ''), array( 'name' => "Cutive", 'variant' => ''), array( 'name' => "Karla", 'variant' => ':400,400italic,700,700italic'), array( 'name' => "Bitter", 'variant' => ':r,i,b'), array( 'name' => "Asap", 'variant' => ':400,700,400italic,700italic'), array( 'name' => "Bree Serif", 'variant' => '') ); /*-----------------------------------------------------------------------------------*/ /* Fonts Face Constructor */ /*-----------------------------------------------------------------------------------*/ public function __construct() { parent::__construct(); if ( ! $this->is_load_google_fonts ) { $this->google_fonts = array(); return; } // Enable Google Font API Key if ( isset( $_POST[ $this->google_api_key_option . '_enable' ] ) ) { $old_google_api_key_enable = get_option( $this->google_api_key_option . '_enable', 0 ); update_option( $this->google_api_key_option . '_enable', 1 ); $option_value = trim( sanitize_text_field( $_POST[ $this->google_api_key_option ] ) ); $old_google_api_key_option = get_option( $this->google_api_key_option ); if ( 1 != $old_google_api_key_enable || $option_value != $old_google_api_key_option ) { update_option( $this->google_api_key_option, $option_value ); // Clear cached of google api key status delete_transient( $this->google_api_key_option . '_status' ); } // Disable Google Font API Key } elseif ( isset( $_POST[ $this->google_api_key_option ] ) ) { $old_google_api_key_enable = get_option( $this->google_api_key_option . '_enable', 0 ); update_option( $this->google_api_key_option . '_enable', 0 ); $option_value = trim( sanitize_text_field( $_POST[ $this->google_api_key_option ] ) ); update_option( $this->google_api_key_option, $option_value ); if ( 0 != $old_google_api_key_enable ) { // Clear cached of google api key status delete_transient( $this->google_api_key_option . '_status' ); } } if ( apply_filters( $this->plugin_name . '_new_google_fonts_enable', true ) ) { $this->is_valid_google_api_key(); $google_fonts = get_option( $this->plugin_name . '_google_font_list', array() ); } else { $google_fonts = array(); } if ( ! is_array( $google_fonts ) || count( $google_fonts ) < 1 ) { $google_fonts = apply_filters( $this->plugin_name . '_google_fonts', $this->google_fonts ); } sort( $google_fonts ); $new_google_fonts = array(); foreach ( $google_fonts as $row ) { $new_google_fonts[$row['name']] = $row; } $this->google_fonts = $new_google_fonts; } public function is_valid_google_api_key( $cache=true ) { $is_valid = false; if ( ! $this->is_load_google_fonts ) { return false; } $this->google_api_key = get_option( $this->google_api_key_option, '' ); $google_api_key_enable = get_option( $this->google_api_key_option . '_enable', 0 ); if ( '' != trim( $this->google_api_key ) && 1 == $google_api_key_enable ) { $google_api_key_status = get_transient( $this->google_api_key_option . '_status' ); if ( ! $cache ) { $google_api_key_status = null; } if ( ! $google_api_key_status ) { $respone_api = wp_remote_get( "https://www.googleapis.com/webfonts/v1/webfonts?sort=alpha&key=" . trim( $this->google_api_key ), array( 'sslverify' => false, 'timeout' => 45 ) ); $font_list = array(); $response_fonts = array(); // Check it is a valid request if ( ! is_wp_error( $respone_api ) ) { $json_string = version_compare( PHP_VERSION, '7.4', '>=' ) || get_magic_quotes_gpc() ? stripslashes( $respone_api['body'] ) : $respone_api['body']; $response_fonts = json_decode( $json_string, true ); // Make sure that the valid response from google is not an error message if ( ! isset( $response_fonts['error'] ) ) { $google_api_key_status = 'valid'; } else { $google_api_key_status = 'invalid'; } } else { $google_api_key_status = 'invalid'; } // Get font list from default webfonts.json file of plugin if ( 'invalid' == $google_api_key_status && file_exists( $this->admin_plugin_dir() . '/assets/webfonts/webfonts.json' ) ) { $response = wp_remote_get( $this->admin_plugin_url() . '/assets/webfonts/webfonts.json', array( 'timeout' => 120 ) ); $webfonts = wp_remote_retrieve_body( $response ); if ( ! empty( $webfonts ) ) { $json_string = version_compare( PHP_VERSION, '7.4', '>=' ) || get_magic_quotes_gpc() ? stripslashes( $webfonts ) : $webfonts; $response_fonts = json_decode( $json_string, true ); } } // Saving the font list to database for get later if ( is_array( $response_fonts ) && isset( $response_fonts['items'] ) && is_array( $response_fonts['items'] ) && count( $response_fonts['items'] ) > 0 ) { foreach ( $response_fonts['items'] as $font_item ) { $variants = ''; $comma = ''; foreach ( $font_item['variants'] as $variant ) { $variants .= $comma . trim( $variant ); $comma = ','; } if ( '' != trim( $variants ) ) { $variants = ':' . $variants; } $font_list[] = array( 'name' => trim( $font_item['family'] ), 'variant' => $variants ); } } update_option( $this->plugin_name . '_google_font_list', $font_list ); //caching google api status for 24 hours set_transient( $this->google_api_key_option . '_status', $google_api_key_status, 86400 ); } if ( 'valid' == $google_api_key_status ) { $is_valid = true; } } else { $google_api_key_status = get_transient( $this->google_api_key_option . '_status' ); if ( ! $cache ) { $google_api_key_status = null; } if ( ! $google_api_key_status ) { $font_list = array(); $response_fonts = array(); // Get font list from default webfonts.json file of plugin if ( file_exists( $this->admin_plugin_dir() . '/assets/webfonts/webfonts.json' ) ) { $response = wp_remote_get( $this->admin_plugin_url() . '/assets/webfonts/webfonts.json', array( 'timeout' => 120 ) ); $webfonts = wp_remote_retrieve_body( $response ); if ( ! empty( $webfonts ) ) { $json_string = version_compare( PHP_VERSION, '7.4', '>=' ) || get_magic_quotes_gpc() ? stripslashes( $webfonts ) : $webfonts; $response_fonts = json_decode( $json_string, true ); } } // Saving the font list to database for get later if ( is_array( $response_fonts ) && isset( $response_fonts['items'] ) && is_array( $response_fonts['items'] ) && count( $response_fonts['items'] ) > 0 ) { foreach ( $response_fonts['items'] as $font_item ) { $variants = ''; $comma = ''; foreach ( $font_item['variants'] as $variant ) { $variants .= $comma . trim( $variant ); $comma = ','; } if ( '' != trim( $variants ) ) { $variants = ':' . $variants; } $font_list[] = array( 'name' => trim( $font_item['family'] ), 'variant' => $variants ); } } update_option( $this->plugin_name . '_google_font_list', $font_list ); //caching google api status for 24 hours set_transient( $this->google_api_key_option . '_status', 'invalid', 86400 ); } } return $is_valid; } /*-----------------------------------------------------------------------------------*/ /* Get Window Default Fonts */ /*-----------------------------------------------------------------------------------*/ public function get_default_fonts() { $default_fonts = apply_filters( $this->plugin_name . '_default_fonts', $this->default_fonts ); asort( $default_fonts ); return $default_fonts; } /*-----------------------------------------------------------------------------------*/ /* Get Google Fonts */ /*-----------------------------------------------------------------------------------*/ public function get_google_fonts() { return $this->google_fonts; } /*-----------------------------------------------------------------------------------*/ /* generate_font_css() */ /* Generate font CSS for frontend */ /*-----------------------------------------------------------------------------------*/ public function generate_font_css( $option, $em = '1.2' ) { $google_fonts = $this->get_google_fonts(); if ( array_key_exists( $option['face'], $google_fonts ) ) { $option['face'] = "'" . $option['face'] . "', arial, sans-serif"; } $line_height = '1.4em'; if( isset( $option['line_height'] ) ){ $line_height = $option['line_height']; } if ( !@$option['style'] && !@$option['size'] && !@$option['color'] ) return 'font-family: '.stripslashes($option["face"]).' !important;'; else return 'font:'.$option['style'].' '.$option['size'].'/' . $line_height . ' ' .stripslashes($option['face']).' !important; color:'.$option['color'].' !important;'; } /*-----------------------------------------------------------------------------------*/ /* Google Webfonts Stylesheet Generator */ /*-----------------------------------------------------------------------------------*/ /* INSTRUCTIONS: Needs to be loaded for the Google Fonts options to work for font options. add_action( 'wp_head', array( $this, 'generate_google_webfonts' ) ); */ public function generate_google_webfonts( $my_google_fonts = array(), $echo = true ) { $google_fonts = $this->get_google_fonts(); $fonts = ''; $output = ''; // Go through the options if ( is_array( $my_google_fonts ) ) { foreach ( $my_google_fonts as $font_face ) { // Check if the google font name exists in the current "face" option if ( array_key_exists( $font_face, $google_fonts ) && !strstr( $fonts, $font_face ) ) { $fonts .= $google_fonts[$font_face]['name'].$google_fonts[$font_face]['variant']."|"; } } // End Foreach Loop // Output google font css in header if ( trim( $fonts ) != '' ) { $fonts = str_replace( " ","+",$fonts); $output .= "\n\n"; $output .= ''."\n"; $output = str_replace( '|"','"',$output); } } if ( $echo ) echo $output; else return $output; } // End generate_google_webfonts() } }