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;
}
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 update_google_font_api_key() {
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
check_admin_referer( 'save_settings_' . $this->plugin_name );
if ( ! $this->is_load_google_fonts ) {
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( wp_unslash( $_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( wp_unslash( $_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' );
}
}
}
public function validate_google_api_key( $g_key = '' ) {
$g_key = trim( $g_key );
$response_fonts = array();
if ( ! empty( $g_key ) ) {
$respone_api = wp_remote_get( "https://www.googleapis.com/webfonts/v1/webfonts?sort=alpha&key=" . $g_key,
array(
'sslverify' => false,
'timeout' => 45
)
);
// 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']; // @codingStandardsIgnoreLine // phpcs:ignore
$response_fonts = json_decode( $json_string, true );
}
}
return $response_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 ) {
$font_list = array();
$response_fonts = $this->validate_google_api_key( $this->google_api_key );
// Check it is a valid request
if ( ! empty( $response_fonts ) ) {
// 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; // @codingStandardsIgnoreLine // phpcs:ignore
$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; // @codingStandardsIgnoreLine // phpcs:ignore
$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 );
$default_fonts = array_merge( array( '' => __( 'Default' ) ), $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";
}
$font_size = $option['size'] ?? '';
$font_line_height = $option['line_height'] ?? '';
$font_face = $option['face'] ?? '';
$font_style = $option['style'] ?? '';
$font_color = $option['color'] ?? '';
$font_css = '';
if ( ! empty( $font_size ) && ! empty( $font_line_height ) && ! empty( $font_face ) && ! empty( $font_style ) ) {
$font_css .= 'font:'.$font_style.' '.$font_size.'/' . $font_line_height . ' ' .stripslashes( str_replace( array( '"', "'" ), '', $font_face ) ).' !important;';
} else {
if ( ! empty( $font_size ) ) $font_css .= 'font-size:'.$font_size.' !important;';
if ( ! empty( $font_line_height ) ) $font_css .= 'line-height:'.$font_line_height.' !important;';
if ( ! empty( $font_face ) ) $font_css .= 'font-family:'.stripslashes( str_replace( array( '"', "'" ), '', $font_face ) ).' !important;';
if ( ! empty( $font_style ) ) {
// font style
if ( strstr( $font_style, 'italic' ) !== false ) $font_css .= 'font-style:italic !important;';
if ( strstr( $font_style, 'normal' ) !== false ) $font_css .= 'font-style:normal !important;';
// font weight
if ( strstr( $font_style, 'italic' ) !== false ) {
$font_style_ex = explode( ' ', $font_style );
if ( is_array( $font_style_ex ) && count( $font_style_ex ) > 1 ) {
$font_css .= 'font-weight:'.$font_style_ex[0].' !important;';
}
} else {
$font_css .= 'font-weight:'.$font_style.' !important;';
}
}
}
if ( ! empty( $font_color ) ) $font_css .= 'color:'.$font_color.' !important;';
return apply_filters( $this->plugin_name . '_generate_font_css', $font_css, $option, $em );
}
/*-----------------------------------------------------------------------------------*/
/* 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);
if ( $echo ) {
echo "\n\n";
echo ''."\n";
} else {
$output .= "\n\n";
$output .= ''."\n";
$output = str_replace( '|"','"',$output);
}
}
}
if ( $echo )
echo '';
else
return $output;
} // End generate_google_webfonts()
}
}