1<?php 2/** 3 * DokuWiki Action Plugin GoogleFonts 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Tamara Phillips <tamara.phillips@gmail.com> 7 */ 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); 12if(!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13 14require_once(DOKU_PLUGIN.'action.php'); 15 16class action_plugin_googlefonts extends DokuWiki_Action_Plugin { 17 18 // register hook 19 function register(&$controller) { 20 $controller->register_hook('TPL_METAHEADER_OUTPUT','BEFORE', $this, '_addFontCode'); 21 } 22 23 /** 24 * @param unknown_type $event 25 * @param unknown_type $param 26 */ 27 function _addFontCode(&$event, $param) { 28 29 $CSSfiles = array(); 30 $CSSembed = array(); 31 $fontNames = array(); 32 33 for ($i = 1; $i <= 6; $i++) { 34 ${fontName.$i} = $this->getConf('fontName'.$i); 35 ${headings.$i} = $this->getConf('headings'.$i); 36 ${genFamily.$i} = $this->getConf('genFamily'.$i); 37 ${addStyle.$i} = $this->getConf('addStyle'.$i); 38 $fontNames[] = ${fontName.$i}; 39 // add styles 40 // if not set, set them through CSS as usual 41 if ( ${addStyle.$i} && !empty(${fontName.$i}) ) { 42 $CSSembed[] = ${headings.$i}." { font-family: '".preg_replace('/:.*/','',${fontName.$i})."', ".${genFamily.$i}."; }"; 43 } 44 } 45 46 $CSSfiles = array( 47 '//fonts.googleapis.com/css?family='.trim(implode("|",str_replace(' ', '+', $fontNames)),"|") 48 ); 49 50 // include all relevant CSS files 51 if (!empty($CSSfiles)) { 52 foreach($CSSfiles as $CSSfile) { 53 $event->data['link'][] = array( 54 'type' => 'text/css', 55 'rel' => 'stylesheet', 56 'media' => 'screen', 57 'href' => $CSSfile 58 ); 59 } 60 } 61 // embed all relevant CSS code 62 if (!empty($CSSembed)){ 63 foreach($CSSembed as $CSSembeded) { 64 $event->data['style'][] = array( 65 'type' => 'text/css', 66 'media' => 'screen', 67 '_data' => $CSSembeded 68 ); 69 } 70 } 71 } 72} 73