PageRenderTime 127ms CodeModel.GetById 60ms app.highlight 26ms RepoModel.GetById 34ms app.codeStats 1ms

/branches/coding-style/fp-includes/core/core.theme.php

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