PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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