PageRenderTime 137ms CodeModel.GetById 80ms app.highlight 35ms RepoModel.GetById 16ms app.codeStats 0ms

/branches/0.804-vivace/fp-includes/core/core.theme.php

https://bitbucket.org/alexandrul/flatpress
PHP | 581 lines | 357 code | 183 blank | 41 comment | 46 complexity | db46010abc744b16ccdbf48a47a39f11 MD5 | raw file
  1<?php
  2	
  3	function theme_loadsettings() {
  4		
  5		global $fp_config, $theme, $FLATPRESS;
  6		
  7		$theme = array(
  8			// name of the theme
  9			'name' 			=> 'theme',
 10			// author of the theme
 11			'author' 		=> 'anonymous',
 12			// theme website
 13			'www' 			=> 'http://flatpress.nowhereland.it',
 14			// fp version
 15			'version'		=> -1,
 16			// default style (must be in res/ dir
 17			
 18			'style' => array(
 19			
 20				'style_def' 		=> 'style.css',
 21				// default style for admin panel (usually it's the same of the theme)
 22				'style_admin' 		=> 'style.css',
 23			), 
 24			
 25			// if false a default css is used to style some elements of the panel
 26			// if true, we'll suppose these elements are already styled in your own css's
 27			'admin_custom_interf'	=> false
 28		);
 29	
 30		
 31		if (!defined('THE_THEME'))
 32			define('THE_THEME', $fp_config['general']['theme']);
 33			
 34		
 35		// backward compatibility:
 36		$conf1 = THEMES_DIR . THE_THEME . '/theme_conf.php';
 37		
 38		// new naming convention. Yeah, I know, just an underscore
 39		// instead of the dot, so? It is more "consistent" :D
 40		$conf2 = THEMES_DIR . THE_THEME . '/theme.conf.php';
 41
 42		ob_start();
 43
 44		if (file_exists($conf2)) {
 45			include($conf2);
 46		} elseif (file_exists($conf1)) {
 47			include($conf1);
 48		}
 49		
 50		
 51		if (!defined('THEME_LEGACY_MODE')) {
 52			if ($theme['version'] < 0.702) {
 53				define('THEME_LEGACY_MODE', true);
 54				theme_register_default_widgetsets();
 55			} else {
 56				define('THEME_LEGACY_MODE', false);
 57 
 58				if (isset($theme['default_style'])) {
 59				
 60					if (!isset($fp_config['general']['style']))
 61						$fp_config['general']['style'] = $theme['default_style'];
 62
 63
 64					include(THEMES_DIR . THE_THEME . "/{$fp_config['general']['style']}/style.conf.php");
 65
 66					$theme['style'] = $style; 
 67
 68				} else {
 69					
 70					
 71					$theme['style'] = array(
 72						
 73						'style_def' 	=> $theme['style_def']? $theme['style_def'] : 'style.css',
 74						'style_admin'	=> $theme['style_admin']? $theme['style_admin'] : 'style.css',
 75
 76					);
 77
 78
 79				}
 80				
 81			}
 82
 83			// no widgets registered, load default set	
 84			if (!get_registered_widgets())
 85				theme_register_default_widgetsets();
 86
 87		}
 88
 89		ob_end_clean();
 90				
 91		return $theme;
 92		
 93	}
 94	
 95	function theme_register_default_widgetsets() {
 96		register_widgetset('left');
 97		register_widgetset('right');
 98		register_widgetset('top');
 99		register_widgetset('bottom');
100	}
101
102	function theme_getdir($id = THE_THEME) {
103		return theme_exists($id);
104	}
105	
106	function theme_exists($id) {
107		// quick fix for win
108		$f = THEMES_DIR . ($id);
109		if (file_exists($f))
110			return $f .'/';
111		
112		return '';
113	}
114	
115	function theme_style_exists($id, $themeid=THE_THEME) {
116		if ($f = theme_exists($themeid)) {
117			if (file_exists($f))
118				return $f . '/';
119		}
120		return '';
121			
122	}
123	
124	function theme_geturl($id = THE_THEME) {
125		return BLOG_BASEURL . THEMES_DIR . $id . '/';
126	}
127	
128	function theme_style_geturl($style, $id=THE_THEME) {
129		return theme_geturl($id) . $style . '/';
130	}
131
132 
133	function theme_list() {
134		$dir = THEMES_DIR;
135		$dh  = opendir($dir);
136		$i = 0;
137		while (false !== ($filename = readdir($dh))) {
138			if ( ($filename != '.') && ($filename != '..') ) {
139				$files[$i++] = $filename;
140			}
141		}
142		sort($files);
143		return $files;
144	}
145	
146     
147	function theme_wp_head() {
148		global $fp_config;
149		
150		echo "\n<!-- FP STD HEADER -->\n";
151		
152		echo "\n<meta name=\"generator\" content=\"FlatPress ". system_ver() ."\" />\n";
153		echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Get RSS 2.0 Feed\" href=\"".
154			theme_feed_link('rss2') 
155			."\" />\n";
156
157		echo "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"Get Atom 1.0 Feed\" href=\"".
158			theme_feed_link('atom') 
159			."\" />\n";
160	
161		echo "<!-- EOF FP STD HEADER -->\n";
162	}
163	
164	function theme_head_stylesheet() {
165		
166		global $fp_config, $theme;
167		
168		echo "\n<!-- FP STD STYLESHEET -->\n";
169		
170		echo '<link media="screen,projection,handheld" href="';
171		echo BLOG_BASEURL . THEMES_DIR . THE_THEME;
172		
173		
174		$css = defined('MOD_ADMIN_PANEL')?
175			 $theme['style']['style_admin'] : $theme['style']['style_def'];
176		
177		$substyle = '/'. (isset($fp_config['general']['style'])? $fp_config['general']['style'].'/' : '');
178		
179		echo $substyle . 'res/'. $css .'" type="text/css" rel="stylesheet" />';
180
181		if (@$theme['style']['style_print']) {
182			echo '<link media="print" href="';
183			echo BLOG_BASEURL . THEMES_DIR . THE_THEME;
184			echo $substyle . 'res/'. $theme['style']['style_print'] .'" type="text/css" rel="stylesheet" />';
185		}
186		
187		echo "\n<!-- FP STD STYLESHEET -->\n";
188		
189
190	}
191
192	function admin_head_action() {
193		global $theme;
194		if (!$theme['admin_custom_interface'])
195			echo '<link media="screen" href="'.BLOG_BASEURL.'admin/res/admin.css" type="text/css" rel="stylesheet" />';
196	}
197		
198	add_filter('admin_head', 'admin_head_action');
199
200
201
202	
203	add_action('wp_head', 'theme_wp_head');
204	add_action('wp_head', 'theme_head_stylesheet');
205
206
207	
208	
209		function get_wp_head() {
210			do_action('wp_head');
211			if (class_exists('AdminPanel'))
212				do_action('admin_head');
213		}
214		
215		$smarty->register_function('header', 'get_wp_head');
216		
217	
218	function theme_wp_footer() {
219		global $fp_config;
220		echo $fp_config['general']['footer'];
221	}
222	
223	add_action('wp_footer', 'theme_wp_footer');	
224	
225		function get_wp_footer() {
226			do_action('wp_footer');
227		}
228		
229		$smarty->register_function('footer', 'get_wp_footer');
230	
231	
232	function theme_charset() {
233		global $fp_config;
234		header('Content-Type: text/html; charset='. $fp_config['general']['charset']);
235		
236	}
237			
238	add_action('init', 'theme_charset');
239	
240	function theme_init(&$smarty) { /* &$mode */
241		
242		global $fp_config, $lang, $theme, $fp_params;
243		
244		// avoid compiled tpl collision (i.e. change theme without this and cry)
245		$smarty->compile_id = md5($fp_config['general']['theme']);
246		$smarty->template_dir = ABS_PATH . THEMES_DIR . $fp_config['general']['theme'] . '/';
247		
248		$loggedin = user_loggedin();
249		
250		$flatpress = $fp_config['general'];
251		// retained for compatibility 
252		// todo: ugly, clean this up
253		// smarty has constant facilities included ^_^
254		//$flatpress['FP_INTERFACE']	= FP_INTERFACE;
255		//$flatpress['BLOGURL']	= BLOG_BASEURL;
256		
257		
258		$flatpress['loggedin']	= $loggedin;
259		
260		if ($loggedin)
261			$flatpress['user'] = user_get();
262		
263		// useful shorthand for themes
264		// e.g. {$flatpress.themeurl}imgs/myimage.png
265		
266		if (isset($fp_config['general']['style'])) {
267			$themeurl = theme_style_geturl($fp_config['general']['style']);
268		} else {
269			$themeurl = theme_geturl();
270		}
271		
272		$flatpress['themeurl'] = $themeurl;
273
274		$flatpress['params'] = $fp_params;
275		
276		$flatpress_upper = array_change_key_case($flatpress, CASE_UPPER);
277		
278		$flatpress = array_merge($flatpress, $flatpress_upper);
279					
280		$smarty->assign('flatpress', $flatpress);
281		
282		$smarty->assign('lang', $lang);
283		
284		$smarty->assign('blogtitle', $fp_config['general']['title']);
285
286		$smarty->assign('pagetitle', 
287				apply_filters('wp_title', "", '&raquo;'));
288				
289		$smarty->assign_by_ref('fp_config', $fp_config);
290				
291		
292		$smarty->register_modifier('tag', 'theme_apply_filters_wrapper');
293		$smarty->register_modifier('link', 'theme_apply_filters_link_wrapper');
294		$smarty->register_modifier('filed', 'theme_entry_categories');
295		
296		
297		if (!isset($fp_params['feed']) || empty($fp_params['feed'])) {
298			$smarty->register_modifier('date_format_daily', 'theme_smarty_modifier_date_format_daily');
299			$smarty->register_modifier('date_format', 'theme_date_format');
300		}
301		
302		$smarty->register_modifier('date_rfc3339', 'theme_smarty_modifier_date_rfc3339');
303		
304		$smarty->register_function('action', 'theme_smarty_function_action');
305		
306		
307	}
308	
309	
310		function smarty_block_page($params, $content) {
311			return $content;
312		}
313		
314		$smarty->register_block('page', 'smarty_block_page');
315	
316	function theme_apply_filters_wrapper($var, $hook) {
317		$args = func_get_args();
318		$tmp = $args[0];
319		$args[0] = $args[1];
320		$args[1] = $tmp;
321		return call_user_func_array('apply_filters', $args);
322	}
323	
324	
325	function theme_apply_filters_link_wrapper($var, $hook) {
326		return apply_filters($hook, '', $var);
327	}
328	
329	
330	
331	function theme_smarty_function_action($params, &$smarty) {
332		if (isset($params['hook']))
333			do_action($params['hook']);
334			
335	}
336	
337	function theme_date_format($string, $format = null, $default_date = '') {
338
339		$timestamp = 0;
340
341		if ($string) {
342			$timestamp = $string; // smarty_make_timestamp($string);
343		} elseif ($default_date != '') {
344			$timestamp = $default_date; // smarty_make_timestamp($default_date);
345		} else {
346			return;
347		}
348
349		if (is_null($format)) {
350			global $fp_config;
351			$format = $fp_config['locale']['timeformat'];
352		}
353		
354	
355
356		return date_strformat($format, $timestamp);
357		
358	} 
359	
360	
361	function theme_smarty_modifier_date_format_daily(
362			$string, $format = null, $default_date = '' ) {
363	
364		global $THEME_CURRENT_DAY, $lang, $fp_config;
365	
366		if (is_null($format))
367			$format = $fp_config['locale']['dateformat'];
368
369		$current_day = theme_date_format($string, $format, $default_date);
370    
371	if (!isset($THEME_CURRENT_DAY) || $THEME_CURRENT_DAY != $current_day) {
372		$THEME_CURRENT_DAY = $current_day;
373		
374		return $current_day;
375		
376	}
377	
378	return '';
379    
380	}
381	
382	
383	/**
384	* Get date in RFC3339
385	* For example used in XML/Atom
386	*
387	* @param integer $timestamp
388	* @return string date in RFC3339
389	* @author Boris Korobkov
390	* @see http://tools.ietf.org/html/rfc3339
391	*
392	* http://it.php.net/manual/en/function.date.php#75757
393	*
394	*/
395		
396	function theme_smarty_modifier_date_rfc3339($timestamp='') {
397		
398		if (!$timestamp) {
399        	$timestamp = time();
400    	}
401    	
402    	$date = date('Y-m-d\TH:i:s', $timestamp);
403
404	    $matches = array();
405	    if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
406	        $date .= $matches[1].$matches[2].':'.$matches[3];
407	    } else {
408	        $date .= 'Z';
409	    }
410	    return $date;
411
412	}
413
414
415	
416	
417	
418	// {{{ permalink, commentlink, staticlink:  filters
419	
420	add_filter('feed_link', 'theme_def_feed_link', 0, 2);
421	function theme_def_feed_link($str, $type) {
422		return BLOG_BASEURL . "?x=feed:{$type}";
423	}
424	function theme_feed_link ($feed='rss2') {
425		return apply_filters('feed_link', '', $feed);
426	}
427	
428	add_filter('post_comments_feed_link', 'theme_def_feed_comments_link', 0, 3);
429	function theme_def_feed_comments_link($str, $feed, $id) {
430		return BLOG_BASEURL . "?x=entry:$id;comments:1;feed:{$feed}";
431	}
432	function theme_comments_feed_link ($feed='rss2', $id) {
433		return apply_filters('post_comments_feed_link', '', $feed, $id);
434	}
435	
436	 
437	add_filter('post_link', 'theme_def_permalink', 0, 2);
438	function theme_def_permalink($str, $id) {
439		return BLOG_BASEURL . "?x=entry:$id";
440	}
441	function get_permalink ($id) {
442		return apply_filters('post_link', '', $id);
443	}
444	
445	add_filter('comments_link', 'theme_def_commentlink', 0, 2);
446	function theme_def_commentlink($str, $id) {
447		return BLOG_BASEURL . "?x=entry:$id;comments:1";
448	}
449	function get_comments_link ($id) {
450		return apply_filters('comments_link', '', $id);
451	}
452	
453
454	add_filter('page_link', 'theme_def_staticlink', 0, 2);
455	function theme_def_staticlink($str, $id) {
456		return BLOG_BASEURL . "?page=$id";
457	}
458	function theme_staticlink ($id) {
459		return apply_filters('page_link', '', $id);
460	}
461	
462	add_filter('category_link', 'theme_def_catlink', 0, 2);
463	function theme_def_catlink($str, $catid) {
464		return BLOG_BASEURL . "?x=cat:$catid";
465	}
466	function get_category_link($catid) {
467		return apply_filters('category_link', '', $catid);
468	}
469	
470	
471	function get_year_link($year) {
472		return wp_specialchars(
473			apply_filters(
474				'year_link', 
475				BLOG_BASEURL . '?x=y:'. str_pad($year, 2, '0', STR_PAD_LEFT), 
476				$year)
477		);
478	}
479	
480	function get_month_link($year, $month) {
481		return wp_specialchars(
482			apply_filters(
483				'month_link',  
484				BLOG_BASEURL . '?x=y:'. str_pad($year, 2, '0', STR_PAD_LEFT) . 
485				';m:' . str_pad($month, 2, '0', STR_PAD_LEFT),
486				$year, 
487				$month)
488			);
489	}
490	
491	function get_day_link($year, $month, $day) {
492		return wp_specialchars(
493			apply_filters(
494				'day_link',  
495				BLOG_BASEURL	. '?x=y:'. str_pad($year, 2, '0', STR_PAD_LEFT) 
496								. ';m:' . str_pad($month, 2, '0', STR_PAD_LEFT) 
497								. ';d:' . str_pad($day, 2, '0', STR_PAD_LEFT),
498				$year, 
499				$month,
500				$day)
501		);
502	}
503
504
505	
506	// }}}
507	
508	
509	function theme_entry_commentcount($count) {
510		global $lang;
511		switch ($count) {  
512			case 0: 	return $comments = $lang['main']['nocomments'];
513			case 1:		return $comments = $lang['main']['comment'];
514			default:	return $comments = $count . ' ' . $lang['main']['comments'];
515		}
516		
517	}
518	add_filter('comments_number', 'theme_entry_commentcount');
519	
520	
521	function theme_entry_categories($cats, $link = true, $separator=', ') {
522		if (!$cats) {
523			return;
524		} else {
525			$filed=array();
526			if ($tmp1 = entry_categories_get('defs')) {
527				
528				foreach ($tmp1 as $k=>$c) {
529					if(array_intersect(array($k),$cats)) {  
530						$filed[] = $link? "<a href=\"" . get_category_link($k) ."\">$c</a>" : $c;
531					}
532				}
533			}
534			if ($filed) {
535				return implode($separator, $filed);
536			}
537		}
538	}
539	
540	/*
541		this is called only in legacy mode
542		
543	*/
544	
545	
546	// {{{ ENTRY
547	function &theme_entry_filters(&$contentarr, $id=null) {
548		
549		$contentarr['subject']=apply_filters('the_title', $contentarr['subject']);
550	
551		$contentarr['content'] = apply_filters('the_content', $contentarr['content']);
552		
553		if (isset($contentarr['comments'])) {
554			$contentarr['commentcount'] = $contentarr['comments'];
555			$contentarr['comments'] = apply_filters('comments_number', $contentarr['commentcount']);
556		}
557		
558		$contentarr['permalink']=get_permalink($id);
559		
560		$contentarr['commentlink']=get_comments_link($id);
561		return $contentarr;
562	}
563	
564	//{{{ COMMENTS	
565	function &theme_comments_filters(&$contentarr, $key) {
566		
567		$contentarr['name']=apply_filters('comment_author_name', $contentarr['name']);
568		if (isset($contentarr['email'])) {
569			$contentarr['email']=apply_filters('comment_author_email', $contentarr['email']);
570			$contentarr['mailto'] = 'mailto:' . $contentarr['email'];
571		}
572		if (!isset($contentarr['url'])) $contentarr['url'] = '#'; 
573		$contentarr['timestamp']=$contentarr['date'];
574		$contentarr['content']=apply_filters('comment_text', $contentarr['content']);
575		
576		return $contentarr;
577
578	}
579		
580	
581?>