PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/0.909/fp-includes/core/core.theme.php

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