PageRenderTime 225ms CodeModel.GetById 210ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/session-fix/flatpress/fp-includes/core/core.theme.php

https://bitbucket.org/alexandrul/flatpress
PHP | 577 lines | 353 code | 182 blank | 42 comment | 45 complexity | 6c4657e382c74e6705f97d4f53a293c2 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 THEMES_DIR . ($id) . '/';
104	}
105	
106	function theme_exists($id) {
107		$f = theme_getdir($id); 
108		if (file_exists($f))
109			return $f;
110		
111		return '';
112	}
113	
114	function theme_style_exists($id, $themeid=THE_THEME) {
115		if ($f = theme_exists($themeid)) {
116			$fs = $f . ($id) . '/';
117			if (file_exists($fs))
118				return $fs;
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<link media=\"screen\" href=\"interface/templates/form.css\" type=\"text/css\" rel=\"stylesheet\" />";
153		
154		echo "\n<meta name=\"generator\" content=\"FlatPress ". system_ver() ."\" />\n";
155		//echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Get RSS 2.0 Feed\" href=\"{$fp_config['general']['www']}rss.php\" />\n";
156		echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Get RSS 2.0 Feed\" href=\"".
157			theme_feed_link('rss2') 
158		."\" />\n";
159		echo "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"Get Atom 1.0 Feed\" href=\"".
160			theme_feed_link('atom') 
161		."\" />\n";
162	
163	
164		echo "<!-- EOF FP STD HEADER -->\n";
165	}
166	
167	function theme_head_stylesheet() {
168		
169		global $fp_config, $theme;
170		
171		echo "\n<!-- FP STD STYLESHEET -->\n";
172		
173		echo '<link media="screen" href="';
174		echo BLOG_BASEURL . THEMES_DIR . THE_THEME;
175		
176		
177		$css = defined('MOD_ADMIN_PANEL')?
178			 $theme['style']['style_admin'] : $theme['style']['style_def'];
179		
180		$substyle = '/'. (isset($fp_config['general']['style'])? $fp_config['general']['style'].'/' : '');
181		
182		echo $substyle . 'res/'. $css .'" type="text/css" rel="stylesheet" />';
183		
184		echo "\n<!-- FP STD STYLESHEET -->\n";
185		
186
187	}
188
189	function admin_head_action() {
190		global $theme;
191		if (!$theme['admin_custom_interface'])
192			echo '<link media="screen" href="'.BLOG_BASEURL.'admin/res/admin.css" type="text/css" rel="stylesheet" />';
193	}
194		
195	add_filter('admin_head', 'admin_head_action');
196
197
198
199	
200	add_action('wp_head', 'theme_wp_head');
201	add_action('wp_head', 'theme_head_stylesheet');
202
203
204	
205	
206		function get_wp_head() {
207			do_action('wp_head');
208			if (class_exists('AdminPanel'))
209				do_action('admin_head');
210		}
211		
212		$smarty->register_function('header', 'get_wp_head');
213		
214	
215	function theme_wp_footer() {
216		global $fp_config;
217		echo $fp_config['general']['footer'];
218	}
219	
220	add_action('wp_footer', 'theme_wp_footer');	
221	
222		function get_wp_footer() {
223			do_action('wp_footer');
224		}
225		
226		$smarty->register_function('footer', 'get_wp_footer');
227	
228	
229	function theme_charset() {
230		global $fp_config;
231		header('Content-Type: text/html; charset='. $fp_config['general']['charset']);
232		
233	}
234			
235	add_action('init', 'theme_charset');
236	
237	function theme_init(&$smarty) { /* &$mode */
238		
239		global $fp_config;
240		global $lang;
241		global $theme;
242		
243		// avoid compiled tpl collision (i.e. change theme without this and cry)
244		$smarty->compile_id = md5($fp_config['general']['theme']);
245		$smarty->template_dir = THEMES_DIR . $fp_config['general']['theme'] . '/';
246		
247		$loggedin = user_loggedin();
248		
249		$flatpress = $fp_config['general'];
250		// retained for compatibility 
251		// todo: ugly, clean this up
252		// smarty has constant facilities included ^_^
253		//$flatpress['FP_INTERFACE']	= FP_INTERFACE;
254		//$flatpress['BLOGURL']	= BLOG_BASEURL;
255		
256		
257		$flatpress['loggedin']	= $loggedin;
258		
259		if ($loggedin)
260			$flatpress['user'] = user_get();
261		
262		// useful shorthand for themes
263		// e.g. {$flatpress.themeurl}imgs/myimage.png
264		
265		if (isset($fp_config['general']['style'])) {
266			$themeurl = theme_style_geturl($fp_config['general']['style']);
267		} else {
268			$themeurl = theme_geturl();
269		}
270		
271		$flatpress['themeurl'] = $themeurl;
272		
273		$flatpress_upper = array_change_key_case($flatpress, CASE_UPPER);
274		
275		$flatpress = array_merge($flatpress, $flatpress_upper);
276					
277		$smarty->assign('flatpress', $flatpress);
278		
279		$smarty->assign('lang', $lang);
280		
281		$smarty->assign('blogtitle', $fp_config['general']['title']);
282
283		$smarty->assign('pagetitle', 
284				apply_filters('wp_title', "", '&raquo;'));
285				
286		$smarty->assign_by_ref('fp_config', $fp_config);
287				
288		
289		$smarty->register_modifier('tag', 'theme_apply_filters_wrapper');
290		$smarty->register_modifier('link', 'theme_apply_filters_link_wrapper');
291		$smarty->register_modifier('filed', 'theme_entry_categories');
292		
293		
294		if (!isset($_GET['feed']) || empty($_GET['feed'])) {
295			$smarty->register_modifier('date_format_daily', 'theme_smarty_modifier_date_format_daily');
296			$smarty->register_modifier('date_format', 'theme_date_format');
297		}
298		
299		$smarty->register_modifier('date_rfc3339', 'theme_smarty_modifier_date_rfc3339');
300		
301		$smarty->register_function('action', 'theme_smarty_function_action');
302		
303		
304	}
305	
306	
307		function smarty_block_page($params, $content) {
308			return $content;
309		}
310		
311		$smarty->register_block('page', 'smarty_block_page');
312	
313	function theme_apply_filters_wrapper($var, $hook) {
314		$args = func_get_args();
315		$tmp = $args[0];
316		$args[0] = $args[1];
317		$args[1] = $tmp;
318		return call_user_func_array('apply_filters', $args);
319	}
320	
321	
322	function theme_apply_filters_link_wrapper($var, $hook) {
323		return apply_filters($hook, '', $var);
324	}
325	
326	
327	
328	function theme_smarty_function_action($params, &$smarty) {
329		if (isset($params['hook']))
330			do_action($params['hook']);
331			
332	}
333	
334	function theme_date_format($string, $format = null, $default_date = '') {
335
336		$timestamp = 0;
337
338		if ($string) {
339			$timestamp = $string; // smarty_make_timestamp($string);
340		} elseif ($default_date != '') {
341			$timestamp = $default_date; // smarty_make_timestamp($default_date);
342		} else {
343			return;
344		}
345
346		if (is_null($format)) {
347			global $fp_config;
348			$format = $fp_config['locale']['timeformat'];
349		}
350		
351	
352
353		return date_strformat($format, $timestamp);
354		
355	} 
356	
357	
358	function theme_smarty_modifier_date_format_daily(
359			$string, $format = null, $default_date = '' ) {
360	
361		global $THEME_CURRENT_DAY, $lang, $fp_config;
362	
363		if (is_null($format))
364			$format = $fp_config['locale']['dateformat'];
365
366		$current_day = theme_date_format($string, $format, $default_date);
367    
368	if (!isset($THEME_CURRENT_DAY) || $THEME_CURRENT_DAY != $current_day) {
369		$THEME_CURRENT_DAY = $current_day;
370		
371		return $current_day;
372		
373	}
374	
375	return '';
376    
377	}
378	
379	
380	/**
381	* Get date in RFC3339
382	* For example used in XML/Atom
383	*
384	* @param integer $timestamp
385	* @return string date in RFC3339
386	* @author Boris Korobkov
387	* @see http://tools.ietf.org/html/rfc3339
388	*
389	* http://it.php.net/manual/en/function.date.php#75757
390	*
391	*/
392		
393	function theme_smarty_modifier_date_rfc3339($timestamp='') {
394		
395		if (!$timestamp) {
396        	$timestamp = time();
397    	}
398    	
399    	$date = date('Y-m-d\TH:i:s', $timestamp);
400
401	    $matches = array();
402	    if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
403	        $date .= $matches[1].$matches[2].':'.$matches[3];
404	    } else {
405	        $date .= 'Z';
406	    }
407	    return $date;
408
409	}
410
411
412	
413	
414	
415	// {{{ permalink, commentlink, staticlink:  filters
416	
417	add_filter('feed_link', 'theme_def_feed_link', 0, 2);
418	function theme_def_feed_link($str, $type) {
419		return BLOG_BASEURL . "?feed={$type}";
420	}
421	function theme_feed_link ($feed='rss2') {
422		return apply_filters('feed_link', '', $feed);
423	}
424	
425	add_filter('post_comments_feed_link', 'theme_def_feed_comments_link', 0, 3);
426	function theme_def_feed_comments_link($str, $feed, $id) {
427		return BLOG_BASEURL . "?entry=$id&amp;comments&amp;feed={$feed}";
428	}
429	function theme_comments_feed_link ($feed='rss2', $id) {
430		return apply_filters('post_comments_feed_link', '', $feed, $id);
431	}
432	
433	 
434	add_filter('post_link', 'theme_def_permalink', 0, 2);
435	function theme_def_permalink($str, $id) {
436		return BLOG_BASEURL . "?entry=$id";
437	}
438	function get_permalink ($id) {
439		return apply_filters('post_link', '', $id);
440	}
441	
442	add_filter('comments_link', 'theme_def_commentlink', 0, 2);
443	function theme_def_commentlink($str, $id) {
444		return BLOG_BASEURL . "?entry=$id&amp;comments";
445	}
446	function get_comments_link ($id) {
447		return apply_filters('comments_link', '', $id);
448	}
449	
450
451	add_filter('page_link', 'theme_def_staticlink', 0, 2);
452	function theme_def_staticlink($str, $id) {
453		return BLOG_BASEURL . "?page=$id";
454	}
455	function theme_staticlink ($id) {
456		return apply_filters('page_link', '', $id);
457	}
458	
459	add_filter('category_link', 'theme_def_catlink', 0, 2);
460	function theme_def_catlink($str, $catid) {
461		return BLOG_BASEURL . "?cat=$catid";
462	}
463	function get_category_link($catid) {
464		return apply_filters('category_link', '', $catid);
465	}
466	
467	
468	function get_year_link($year) {
469		return wp_specialchars(
470			apply_filters(
471				'year_link', 
472				BLOG_BASEURL . '?y='. str_pad($year, 2, '0', STR_PAD_LEFT), 
473				$year)
474		);
475	}
476	
477	function get_month_link($year, $month) {
478		return wp_specialchars(
479			apply_filters(
480				'month_link',  
481				BLOG_BASEURL . '?y='. str_pad($year, 2, '0', STR_PAD_LEFT) . 
482				'&m=' . str_pad($month, 2, '0', STR_PAD_LEFT),
483				$year, 
484				$month)
485			);
486	}
487	
488	function get_day_link($year, $month, $day) {
489		return wp_specialchars(
490			apply_filters(
491				'month_link',  
492				BLOG_BASEURL	. '?y='. str_pad($year, 2, '0', STR_PAD_LEFT) 
493								. '&m=' . str_pad($month, 2, '0', STR_PAD_LEFT) 
494								. '&d=' . str_pad($day, 2, '0', STR_PAD_LEFT),
495				$year, 
496				$month)
497		);
498	}
499
500
501	
502	// }}}
503	
504	
505	function theme_entry_commentcount($count) {
506		global $lang;
507		switch ($count) {  
508			case 0: 	return $comments = $lang['main']['nocomments'];
509			case 1:		return $comments = $lang['main']['comment'];
510			default:	return $comments = $count . ' ' . $lang['main']['comments'];
511		}
512		
513	}
514	add_filter('comments_number', 'theme_entry_commentcount');
515	
516	
517	function theme_entry_categories($cats, $link = true, $separator=', ') {
518		if (!$cats) {
519			return;
520		} else {
521			$filed=array();
522			if ($tmp1 = entry_categories_get('defs')) {
523				
524				foreach ($tmp1 as $k=>$c) {
525					if(array_intersect(array($k),$cats)) {  
526						$filed[] = $link? "<a href=\"" . get_category_link($k) ."\">$c</a>" : $c;
527					}
528				}
529			}
530			if ($filed) {
531				return implode($separator, $filed);
532			}
533		}
534	}
535	
536	/*
537		this is called only in legacy mode
538		
539	*/
540	
541	
542	// {{{ ENTRY
543	function &theme_entry_filters(&$contentarr, $id=null) {
544		
545		$contentarr['subject']=apply_filters('the_title', $contentarr['subject']);
546	
547		$contentarr['content'] = apply_filters('the_content', $contentarr['content']);
548		
549		if (isset($contentarr['comments'])) {
550			$contentarr['commentcount'] = $contentarr['comments'];
551			$contentarr['comments'] = apply_filters('comments_number', $contentarr['commentcount']);
552		}
553		
554		$contentarr['permalink']=get_permalink($id);
555		
556		$contentarr['commentlink']=get_comments_link($id);
557		return $contentarr;
558	}
559	
560	//{{{ COMMENTS	
561	function &theme_comments_filters(&$contentarr, $key) {
562		
563		$contentarr['name']=apply_filters('comment_author_name', $contentarr['name']);
564		if (isset($contentarr['email'])) {
565			$contentarr['email']=apply_filters('comment_author_email', $contentarr['email']);
566			$contentarr['mailto'] = 'mailto:' . $contentarr['email'];
567		}
568		if (!isset($contentarr['url'])) $contentarr['url'] = '#'; 
569		$contentarr['timestamp']=$contentarr['date'];
570		$contentarr['content']=apply_filters('comment_text', $contentarr['content']);
571		
572		return $contentarr;
573
574	}
575		
576	
577?>