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

Language PHP Lines 743
MD5 Hash 180068b27b0a92a54e2493f8e873f501 Estimated Cost $10,025 (why?)
Repository https://bitbucket.org/alexandrul/flatpress View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<?php

/**
 * Enter description here...
 *
 * @global $fp_config, $theme, $FLATPRESS
 * @return array
 */
function theme_loadsettings() {
	global $fp_config, $theme, $FLATPRESS;
	$theme = array(
		'name' => 'theme', // name of the theme
		'author' => 'anonymous',// author of the theme
		'www' => 'http://flatpress.nowhereland.it',// theme website
		'version' => -1,// fp version
		'style' => array(
			'style_def' => 'style.css',// default style (must be in res/ dir
			'style_admin' => 'style.css',// default style for admin panel (usually it's the same of the theme)
		),
		// if false a default css is used to style some elements of the panel
		// if true, we'll suppose these elements are already styled in your own css's
		'admin_custom_interf' => false
	);
	if (!defined('THE_THEME')) {
		define('THE_THEME', $fp_config['general']['theme']);
	}
	// backward compatibility:
	$conf1 = THEMES_DIR . THE_THEME . '/theme_conf.php';
	/*
		new naming convention. Yeah, I know, just an underscore
		instead of the dot, so? It is more "consistent" :D
	*/
	$conf2 = THEMES_DIR . THE_THEME . '/theme.conf.php';
	ob_start();
	if (file_exists($conf2)) {
		include($conf2);
	} elseif (file_exists($conf1)) {
		include($conf1);
	}
	if (!defined('THEME_LEGACY_MODE')) {
		if ($theme['version'] < 0.702) {
			define('THEME_LEGACY_MODE', true);
			theme_register_default_widgetsets();
		} else {
			define('THEME_LEGACY_MODE', false);
			if (isset($theme['default_style'])) {
				if (!isset($fp_config['general']['style'])) {
					$fp_config['general']['style'] = $theme['default_style'];
				}
				include(THEMES_DIR . THE_THEME .'/'. $fp_config['general']['style'] .'/style.conf.php');
				$theme['style'] = $style;
			} else {
				$theme['style'] = array(
					'style_def' => $theme['style_def']
						? $theme['style_def']
						: 'style.css',
					'style_admin' => $theme['style_admin']
						? $theme['style_admin']
						: 'style.css',
				);
			}
		}
		// no widgets registered, load default set	
		if (!get_registered_widgets()) {
			theme_register_default_widgetsets();
		}
	}
	ob_end_clean();
	return $theme;
}

/**
 * Registers the four default widget sets.
 *
 */
function theme_register_default_widgetsets() {
	register_widgetset('left');
	register_widgetset('right');
	register_widgetset('top');
	register_widgetset('bottom');
}

/**
 * Enter description here...
 *
 * @param string $id
 * @return string
 */
function theme_getdir($id = THE_THEME) {
	return theme_exists($id);
}

/**
 * Enter description here...
 *
 * @param string $id
 * @return string
 */
function theme_exists($id) {
	// quick fix for win
	$f = THEMES_DIR . $id;
	if (file_exists($f)) {
		return $f .'/';
	}
	return '';
}

/**
 * Enter description here...
 *
 * @param string $id Not used.
 * @param string $themeid
 * @return string
 */
function theme_style_exists($id, $themeid = THE_THEME) {
	if ($f = theme_exists($themeid)) {
		if (file_exists($f)) {
			// DMKE: $f is either '' or $themeid.'/'
			// And now we're adding another slash...?
			return $f .'/';
		}
	}
	return '';
}

/**
 * Enter description here...
 *
 * @param string $id
 * @return string
 */
function theme_geturl($id = THE_THEME) {
	return BLOG_BASEURL . THEMES_DIR . $id .'/';
}

/**
 * Enter description here...
 *
 * @param string $style
 * @param string $id
 * @return string
 */
function theme_style_geturl($style, $id = THE_THEME) {
	return theme_geturl($id) . $style .'/';
}

/**
 * Gets an array with all available theme directories.
 * Currently it won't be checked if a theme directory contains
 * really a theme...
 *
 * @return array
 */
function theme_list() {
	$dir = THEMES_DIR;
	$dh  = opendir($dir);
	$i = 0;
	// DMKE: This construct is alway again quite ugly... Anyway it works ;-)
	while (false !== ($filename = readdir($dh))) {
		if (($filename != '.') && ($filename != '..')) {
			$files[$i++] = $filename;
		}
	}
	sort($files);
	return $files;
}

/**
 * Prints additional meta data and the links to the feeds.
 *
 * @global $fp_config
 */
function theme_wp_head() {
	global $fp_config;
	// DMKE: I *REALLY* don't like these comments...
	// Maybe they could be eneabled with a VERBOSE_OUTPUT constant?
	echo "\n<!-- FP STD HEADER -->\n";
	echo "\n<meta name=\"generator\" content=\"FlatPress ".
		system_ver() ."\" />\n";
	echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"Get RSS 2.0 Feed\" href=\"".
		theme_feed_link('rss2') ."\" />\n";
	echo "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"Get Atom 1.0 Feed\" href=\"".
		theme_feed_link('atom') ."\" />\n";
	echo "<!-- EOF FP STD HEADER -->\n";
}

/**
 * Prints the link(s) to the stylesheet(s)
 *
 * @global $fp_config, $theme
 */
function theme_head_stylesheet() {
	global $fp_config, $theme;
	// DMKE: Same as in line 175
	echo "\n<!-- FP STD STYLESHEET -->\n";
	echo '<link media="screen,projection,handheld" href="';
	echo BLOG_BASEURL . THEMES_DIR . THE_THEME;
	$css = defined('MOD_ADMIN_PANEL')
		? $theme['style']['style_admin']
		: $theme['style']['style_def'];
	$substyle = isset($fp_config['general']['style'])
		? '/'. $fp_config['general']['style'] .'/'
		: '/';
	echo $substyle . 'res/'. $css .'" type="text/css" rel="stylesheet" />';
	if (@$theme['style']['style_print']) {
		echo '<link media="print" href="';
		echo BLOG_BASEURL . THEMES_DIR . THE_THEME;
		echo $substyle .'res/'. $theme['style']['style_print'] .'" type="text/css" rel="stylesheet" />';
	}
	echo "\n<!-- FP STD STYLESHEET -->\n";
}

/**
 * Enter description here...
 *
 * @global $theme
 */
function admin_head_action() {
	global $theme;
	if (!$theme['admin_custom_interface']) {
		echo '<link media="screen" href="'.
			BLOG_BASEURL .'admin/res/admin.css" type="text/css" rel="stylesheet" />';
	}
}

add_filter('admin_head', 'admin_head_action');
add_action('wp_head',    'theme_wp_head');
add_action('wp_head',    'theme_head_stylesheet');

/**
 * Enter description here...
 *
 */
function get_wp_head() {
	do_action('wp_head');
	if (class_exists('AdminPanel')) {
		do_action('admin_head');
	}
}
$smarty->register_function('header', 'get_wp_head');
	
/**
 * Prints the footer defined in the configuration panel.
 *
 * @global $fp_config
 */
function theme_wp_footer() {
	global $fp_config;
	echo $fp_config['general']['footer'];
}
add_action('wp_footer', 'theme_wp_footer');	

/**
 * Enter description here...
 *
 */
function get_wp_footer() {
	do_action('wp_footer');
}
$smarty->register_function('footer', 'get_wp_footer');

/**
 * Sends charset header. 
 *
 */
function theme_charset() {
	global $fp_config;
	header('Content-Type: text/html; charset='. $fp_config['general']['charset']);
}
add_action('init', 'theme_charset');

/**
 * Enter description here...
 *
 * @global $fp_config, $lang, $theme, $fp_params
 * @param object $smarty
 */
function theme_init(&$smarty) { /* &$mode */
	global $fp_config, $lang, $theme, $fp_params;
	// avoid compiled tpl collision (i.e. change theme without this and cry)
	$smarty->compile_id   = md5($fp_config['general']['theme']);
	$smarty->template_dir = ABS_PATH . THEMES_DIR . $fp_config['general']['theme'] .'/';
	$loggedin = user_loggedin();
	$flatpress = $fp_config['general'];
	$flatpress['loggedin']	= $loggedin;
	/*
		retained for compatibility 
		todo: ugly, clean this up
		smarty has constant facilities included ^_^
		$flatpress['FP_INTERFACE'] = FP_INTERFACE;
		$flatpress['BLOGURL'] = BLOG_BASEURL;
	*/
	if ($loggedin) {
		$flatpress['user'] = user_get();
	}
	/*
		useful shorthand for themes 
		e.g. {$flatpress.themeurl}imgs/myimage.png
	*/
	if (isset($fp_config['general']['style'])) {
		$flatpress['themeurl'] = theme_style_geturl($fp_config['general']['style']);
	} else {
		$flatpress['themeurl'] = theme_geturl();
	}
	$flatpress['params'] = $fp_params;
	$flatpress_upper = array_change_key_case($flatpress, CASE_UPPER);
	$flatpress = array_merge($flatpress, $flatpress_upper);
	$smarty->assign('flatpress',        $flatpress);
	$smarty->assign('lang',             $lang);
	$smarty->assign('blogtitle',        $fp_config['general']['title']);
	$smarty->assign('pagetitle',        apply_filters('wp_title', "", '&raquo;'));
	$smarty->assign_by_ref('fp_config', $fp_config);
	$smarty->register_modifier('tag',   'theme_apply_filters_wrapper');
	$smarty->register_modifier('link',  'theme_apply_filters_link_wrapper');
	$smarty->register_modifier('filed', 'theme_entry_categories');
	if (!isset($fp_params['feed']) || empty($fp_params['feed'])) {
		$smarty->register_modifier('date_format_daily', 'theme_smarty_modifier_date_format_daily');
		$smarty->register_modifier('date_format',       'theme_date_format');
	}
	$smarty->register_modifier('date_rfc3339', 'theme_smarty_modifier_date_rfc3339');
	$smarty->register_function('action',       'theme_smarty_function_action');
}

/**
 * Enter description here...
 *
 * @param mixed $params Not used
 * @param mixed $content
 * @return mixed
 */
function smarty_block_page($params, $content) {
	return $content;
}
$smarty->register_block('page', 'smarty_block_page');

/**
 * Enter description here...
 *
 * @param var $var
 * @param string $hook
 * @return mixed
 */
function theme_apply_filters_wrapper($var, $hook) {
	$args = func_get_args();
	$tmp = $args[0];
	$args[0] = $args[1];
	$args[1] = $tmp;
	return call_user_func_array('apply_filters', $args);
}

/**
 * Enter description here...
 *
 * @param unknown_type $var
 * @param unknown_type $hook
 * @return unknown
 */
function theme_apply_filters_link_wrapper($var, $hook) {
	return apply_filters($hook, '', $var);
}

/**
 * Enter description here...
 *
 * @param array $params
 * @param object $smarty
 */
function theme_smarty_function_action($params, &$smarty) {
	if (isset($params['hook'])) {
		do_action($params['hook']);
	}
}

/**
 * Enter description here...
 *
 * @global $fp_config
 * @param string $string
 * @param string $format
 * @param int $default_date
 * @return string
 */
function theme_date_format($string, $format = null, $default_date = '') {
	$timestamp = 0;
	if ($string) {
		$timestamp = $string; // smarty_make_timestamp($string);
	} elseif ($default_date != '') {
		$timestamp = $default_date; // smarty_make_timestamp($default_date);
	} else {
		return;
	}
	if (is_null($format)) {
		global $fp_config;
		$format = $fp_config['locale']['timeformat'];
	}
	return date_strformat($format, $timestamp);
} 

/**
 * Enter description here...
 *
 * @global $THEME_CURRENT_DAY, $lang, $fp_config
 * @param string $string
 * @param string $format
 * @param int $default_date
 * @return string
 */
function theme_smarty_modifier_date_format_daily(
	$string, $format = null, $default_date = ''
) {
	global $THEME_CURRENT_DAY, $lang, $fp_config;
	if (is_null($format)) {
		$format = $fp_config['locale']['dateformat'];
	}
	$current_day = theme_date_format($string, $format, $default_date);
	if (
		!isset($THEME_CURRENT_DAY) ||
		$THEME_CURRENT_DAY != $current_day
	) {
		$THEME_CURRENT_DAY = $current_day;
		return $current_day;
	}
	return '';
}


/**
 * Get date in RFC3339. Used e.g. in XML/Atom
 *
 * @param int $timestamp
 * @return string Date in RFC3339
 * @author Boris Korobkov
 * @see http://tools.ietf.org/html/rfc3339
 * @see http://it.php.net/manual/en/function.date.php#75757
 */
function theme_smarty_modifier_date_rfc3339($timestamp = '') {
	if (!$timestamp) {
		$timestamp = time();
	}
	$date = date('Y-m-d\TH:i:s', $timestamp);
	$matches = array();
	if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
		$date .= $matches[1] . $matches[2] .':'. $matches[3];
	} else {
		$date .= 'Z';
	}
	return $date;
}


// {{{ permalink, commentlink, staticlink:  filters

/**
 * Generates the url of the newsfeed.
 *
 * @param mixed $str Not usec
 * @param string $type Either 'rss2' or 'atom'
 * @return string
 */
function theme_def_feed_link($str, $type) {
	return BLOG_BASEURL . "?x=feed:$type";
}
add_filter('feed_link', 'theme_def_feed_link', 0, 2);

/**
 * Gets the url of the newsfeed.
 *
 * @param string $feed Either 'rss2' (default) or 'atom'
 * @return string
 */
function theme_feed_link($feed = 'rss2') {
	return apply_filters('feed_link', '', $feed);
}

/**
 * Generates the url to the newsfeed of an entry's comments.
 *
 * @param mixed $str Not used
 * @param string $feed Either 'rss2' or 'atom'
 * @param string $id The entry ID
 * @return string
 */
function theme_def_feed_comments_link($str, $feed, $id) {
	return BLOG_BASEURL . "?x=entry:$id;comments:1;feed:$feed";
}
add_filter('post_comments_feed_link', 'theme_def_feed_comments_link', 0, 3);

/**
 * Gets the url to the newsfeed of an entry's comments.
 *
 * @param string $feed Either 'rss2' (default) or 'atom'
 * @param string $id The entry ID
 * @return string
 */
function theme_comments_feed_link ($feed = 'rss2', $id) {
	return apply_filters('post_comments_feed_link', '', $feed, $id);
}

/**
 * Generates the url to an entry.
 *
 * @param mixed $str Not used
 * @param strig $id The entry ID
 * @return string
 */
function theme_def_permalink($str, $id) {
	return BLOG_BASEURL . "?x=entry:$id";
}
add_filter('post_link', 'theme_def_permalink', 0, 2);

/**
 * Gets the url to an entry
 *
 * @param string $id
 * @return string
 */
function get_permalink ($id) {
	return apply_filters('post_link', '', $id);
}

/**
 * Generates the url to the comments of a given entry.
 *
 * @param mixed $str Not used
 * @param string $id The entry ID
 * @return string
 */
function theme_def_commentlink($str, $id) {
	return BLOG_BASEURL . "?x=entry:$id;comments:1";
}
add_filter('comments_link', 'theme_def_commentlink', 0, 2);

/**
 * Gets the url to the comments of a given entry.
 *
 * @param string $id The entry ID
 * @return string
 */
function get_comments_link ($id) {
	return apply_filters('comments_link', '', $id);
}

/**
 * Generates the url to a static page.
 *
 * @param mixed $str Not used
 * @param string $id The page ID
 * @return string
 */
function theme_def_staticlink($str, $id) {
	return BLOG_BASEURL . "?page=$id";
}
add_filter('page_link', 'theme_def_staticlink', 0, 2);

/**
 * Gets the url to a static page
 *
 * @param string $id The page ID
 * @return string
 */
function theme_staticlink ($id) {
	return apply_filters('page_link', '', $id);
}

/**
 * Generates the url to a category.
 *
 * @param mixed $str Not uses
 * @param string $catid The category
 * @return string
 */
function theme_def_catlink($str, $catid) {
	return BLOG_BASEURL . "?x=cat:$catid";
}
add_filter('category_link', 'theme_def_catlink', 0, 2);

/**
 * Gets the url to a category.
 *
 * @param string $catid The category
 * @return string
 */
function get_category_link($catid) {
	return apply_filters('category_link', '', $catid);
}

/**
 * Gets the url to a yearly archive.
 *
 * @param string|int $year
 * @return string
 */
function get_year_link($year) {
	return wp_specialchars(
		apply_filters(
			'year_link', 
			BLOG_BASEURL .'?x=y:'. str_pad($year, 2, '0', STR_PAD_LEFT),
			$year
		)
	);
}

/**
 * Gets the url to a monthly archive.
 *
 * @param string|int $year
 * @param string|int $month
 * @return string
 */
function get_month_link($year, $month) {
	return wp_specialchars(
		apply_filters(
			'month_link',  
			BLOG_BASEURL
				.'?x=y:'. str_pad($year, 2, '0', STR_PAD_LEFT)
				.';m:'. str_pad($month, 2, '0', STR_PAD_LEFT),
			$year,
			$month
		)
	);
}

/**
 * Gets the url to a daily archive.
 *
 * @param string|int $year
 * @param string|int $month
 * @param string|int $day
 * @return string
 */
function get_day_link($year, $month, $day) {
	return wp_specialchars(
		apply_filters(
			'day_link',  
			BLOG_BASEURL
				.'?x=y:'. str_pad($year, 2, '0', STR_PAD_LEFT) 
				.';m:'. str_pad($month, 2, '0', STR_PAD_LEFT) 
				.';d:'. str_pad($day, 2, '0', STR_PAD_LEFT),
			$year,
			$month,
			$day
		)
	);
}

// }}}

/**
 * Enter description here...
 *
 * @global $lang
 * @param int $count
 * @return string
 */
function theme_entry_commentcount($count) {
	global $lang;
	switch ($count) {  
		case 0:
			return $lang['main']['nocomments'];
			break;
		case 1:
			return $lang['main']['comment'];
			break;
		default:
			return $count .' '. $lang['main']['comments'];
			break;
	}
}
add_filter('comments_number', 'theme_entry_commentcount');

/**
 * Enter description here...
 *
 * @param unknown_type $cats
 * @param boolean $link
 * @param string $separator
 * @return unknown
 */
function theme_entry_categories($cats, $link = true, $separator = ', ') {
	if (!$cats) {
		return;
	} else {
		$filed = array();
		if ($tmp1 = entry_categories_get('defs')) {
			foreach ($tmp1 as $k => $c) {
				if (array_intersect(array($k), $cats)) {
					$filed[] = $link
						? "<a href=\"" . get_category_link($k) ."\">$c</a>"
						: $c;
				}
			}
		}
		if ($filed) {
			return implode($separator, $filed);
		}
	}
}

/* this is called only in legacy mode */
// {{{ ENTRY

/**
 * Enter description here...
 *
 * @param array $contarr
 * @param string $id
 * @return array
 */
function &theme_entry_filters(&$contarr, $id = null) {
	$contarr['subject'] = apply_filters('the_title', $contarr['subject']);
	$contarr['content'] = apply_filters('the_content', $contarr['content']);
	if (isset($contarr['comments'])) {
		$contarr['commentcount'] = $contarr['comments'];
		$contarr['comments'] = apply_filters('comments_number', $contarr['commentcount']);
	}
	$contarr['permalink']   = get_permalink($id);
	$contarr['commentlink'] = get_comments_link($id);
	return $contarr;
}

//{{{ COMMENTS
/**
 * Enter description here...
 *
 * @param array $contarr
 * @param mixed $key Not used
 * @return array
 */
function &theme_comments_filters(&$contarr, $key) {
	$contarr['name'] = apply_filters('comment_author_name', $contarr['name']);
	if (isset($contarr['email'])) {
		$contarr['email']  = apply_filters('comment_author_email', $contarr['email']);
		$contarr['mailto'] = 'mailto:' . $contarr['email'];
	}
	if (!isset($contarr['url'])) {
		$contarr['url'] = '#'; 
	}
	$contarr['timestamp'] = $contarr['date'];
	$contarr['content']   = apply_filters('comment_text', $contarr['content']);
	return $contarr;
}

?>
Back to Top