name)); // get cached fields $db_cache_field = 'googlefont-cache-'.$_theme_name; $db_cache_field_last_updated = 'googlefont-cache-last-'.$_theme_name; $db_cache_field_themename = 'googlefont-'.$_theme_name; $current_fonts = get_option( $db_cache_field ); // get current fonts $last = get_option( $db_cache_field_last_updated ); // get the date for last update $theme = get_option ( $db_cache_field_themename ); // get the theme name $now = time(); // get current timestamp $api_key = $key ? $key : false; $db_font_array = array(); if($api_key){ // check if the api key is set if ( !$last || ( ($now - $last ) > $interval ) || !$theme || $current_fonts == "" || !$current_fonts ) { $fontsSeraliazed = esc_url('//www.googleapis.com/webfonts/v1/webfonts?key='.trim($api_key)); // get the Google Fonts from remote URL $google_response = wp_remote_get ( $fontsSeraliazed, array ( 'sslverify' => false ,'timeout' => 1, 'httpversion' => '1.1') ); // we have no errors, proceed if ( 200 == wp_remote_retrieve_response_code( $google_response ) ) { // parse the result from Google Font API $fontArray = json_decode($google_response['body'], true); } else { global $google_fonts_json; // parse the result from local $fontArray = json_decode($google_fonts_json, true); } $googleFontArray = array(); // generate the array to store the fonts foreach($fontArray['items'] as $index => $value){ $_family = strtolower( str_replace(' ','_',$value['family']) ); $googleFontArray[$_family]['family'] = $value['family']; $googleFontArray[$_family]['variants'] = $value['variants']; $googleFontArray[$_family]['subsets'] = $value['subsets']; } if ( is_array($googleFontArray) ) { // we got good results, so update the existing fields update_option( $db_cache_field, $googleFontArray ); update_option( $db_cache_field_last_updated, time() ); update_option( $db_cache_field_themename, $_theme_name ); } else { // there are no fields, so add them to the database add_option( $db_cache_field, $googleFontArray,'', 'no' ); add_option( $db_cache_field_last_updated, time(),'', 'no' ); add_option( $db_cache_field_themename, $_theme_name,'', 'no' ); } // get the google font array from options DB $db_font_array = get_option( $db_cache_field ); } else { // get the google font array from options DB if( is_array($current_fonts) && count($current_fonts) ) { $db_font_array = $current_fonts; } } return $db_font_array; } // no api key -> return false return false; } add_action( 'wp_enqueue_scripts', 'of_get_google_font', 999 ); endif; /** * Google Fonts Ajax Callback * * Returns a json string with all Google Fonts from DB * * @return string * */ function of_ajax_get_google_font(){ // get the current themes name $_theme = wp_get_theme(); $_theme_name = strtolower(str_replace(' ', '_', $_theme->name)); $fonts = get_option('googlefont-cache-'.$_theme_name); die(json_encode($fonts)); } // creating Ajax call for WordPress add_action( 'wp_ajax_nopriv_of_ajax_get_google_font', 'of_ajax_get_google_font' ); add_action( 'wp_ajax_of_ajax_get_google_font', 'of_ajax_get_google_font' ); /** * Enqueue Styles and Scripts * * Enqueues scripts for the Google Font preview box. * * @param string $hook of the current themes page * * @uses wp_enqueue_style(), wp_enqueue_script() * */ function of_action_enqueue_scripts($hook){ if($hook == 'appearance_page_magee-theme-options'): // get plugin folder //$path = '/'.basename( __DIR__); $path = ""; // enqueue the css file wp_enqueue_style( 'ot-google-font-css', get_template_directory_uri().$path.'/css/google-fonts.css', array(), '', 'all'); // enqueue the js file wp_enqueue_script( 'ot-google-font-js', get_template_directory_uri().$path.'/js/google-fonts.js', array(), '', 'all'); endif; } /* add scripts for metaboxes to post-new.php & post.php */ if(is_admin()){ add_action( 'admin_enqueue_scripts', 'of_action_enqueue_scripts', 11 ); } /** * Get Google Font stylesheets * * Includes the Google Font stylesheets into the head section of the current page * * @param array $default_theme_fonts the default theme fonts set before * * @uses wp_enqueue_style(), wp_enqueue_script() * */ function of_action_get_google_font_link(){ if (!is_admin()) { // get the themes name $_theme = wp_get_theme(); $_theme_name = strtolower(str_replace(' ', '_', $_theme->name)); $_def_fonts = unserialize(OF_FONT_DEFAULTS); // lets get all the font options from the option tree settings $_of_options = get_option( 'alchem_option_settings' ); $_font_array = array(); if(isset($_of_options['settings'])){ foreach($_of_options['settings'] as $index => $_setting){ //print_r($_setting); if($_setting['type'] == 'typography'){ $_font_array[] = alchem_option($_setting['id']); } } } // array to store already used font-families and not load them double $_font_array_backup = array(); // loop through fonts foreach( $_font_array as $index => $font ){ $_output = ""; if( !empty($font) && is_array($font) ) : // get the google font array from db cache $_db_font_cache = get_option( 'googlefont-cache-'.$_theme_name ); if($_def_fonts): if( isset($_db_font_cache[$font['font-family']]['family']) ) { if( !array_key_exists($font['font-family'], $_def_fonts) ) { $_output = $_db_font_cache[$font['font-family']]['family']; } } endif; // check if the font family allready exists if( array_key_exists($font['font-family'], $_db_font_cache) ) { $_font_array_backup[$font['font-family']] = $_db_font_cache[$font['font-family']]; } endif; } // loop through the font array and enqueue the google font stylesheet if needed if( is_array($_font_array_backup) && !empty($_font_array_backup) ){ foreach($_font_array_backup as $index => $_g_font_family){ if(!empty($_g_font_family)){ // build the font weight string $_font_weight_array = array(); foreach($_font_array as $font => $value) { // check if font-weight exists for this font-family if(!empty($value['font-weight']) && $value['font-family'] == $index ) : // check if font-weight already exists, if not, add it to the array if(!in_array($value['font-weight'], $_font_weight_array)): // replace font-weight keywords if($value['font-weight'] == 'normal') $value['font-weight'] = 400; if($value['font-weight'] == 'bold') $value['font-weight'] = 500; $_font_weight_array[] = $value['font-weight']; endif; endif; } $_font_add_string = ""; if(count($_g_font_family['variants'])) $_font_add_string .= ":". implode(',', $_g_font_family['variants']); // build the font style string $_font_style_array = array(); foreach($_font_array as $font => $value) { // check if font-style exists for this font-family if(!empty($value['font-style']) && $value['font-family'] == $index ) : // check if font-style already exists, if not, add it to the array if(!in_array($value['font-style'], $_font_style_array)): $_font_style_array[] = $value['font-style']; endif; endif; } // check, if we already have some font-weights if(count($_font_style_array)) { if(empty($_font_add_string)) { $_font_add_string .= ":". implode(',', $_font_style_array); }else{ $_font_add_string .= ",". implode(',', $_font_style_array); } } wp_enqueue_style( 'ot-google-font-' . $index, esc_url('//fonts.googleapis.com/css?family=' . $_g_font_family['family'] . $_font_add_string), array(), '', 'all'); } } } } } // Action to call the google font include on frontpage add_action('wp_enqueue_scripts', 'of_action_get_google_font_link', 15);