PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mobile/tags/1.0/carrington-core/utility.php

http://carrington.googlecode.com/
PHP | 690 lines | 610 code | 48 blank | 32 comment | 132 complexity | 22752ed7f5cff6e302a0d3d55def3831 MD5 | raw file
  1. <?php
  2. // This file is part of the Carrington Theme Framework for WordPress
  3. // http://carringtontheme.com
  4. //
  5. // Copyright (c) 2008-2009 Crowd Favorite, Ltd. All rights reserved.
  6. // http://crowdfavorite.com
  7. //
  8. // Released under the GPL license
  9. // http://www.opensource.org/licenses/gpl-license.php
  10. //
  11. // **********************************************************************
  12. // This program is distributed in the hope that it will be useful, but
  13. // WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. // **********************************************************************
  16. if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { die(); }
  17. function cfct_die($str = '') {
  18. if (!empty($str)) {
  19. include(CFCT_PATH.'error/exit.php');
  20. die();
  21. }
  22. }
  23. function cfct_banner($str = '') {
  24. if (!empty($str)) {
  25. include(CFCT_PATH.'misc/banner.php');
  26. }
  27. }
  28. function cfct_get_option($name) {
  29. $defaults = array(
  30. 'cfct_credit' => 'yes',
  31. 'cfct_lightbox' => 'yes',
  32. 'cfct_header_image' => 0,
  33. );
  34. $defaults = apply_filters('cfct_option_defaults', $defaults);
  35. $value = get_option($name);
  36. if ($value == '' && isset($defaults[$name])) {
  37. $value = $defaults[$name];
  38. }
  39. return $value;
  40. }
  41. function cfct_load_plugins() {
  42. $files = cfct_files(CFCT_PATH.'plugins');
  43. foreach ($files as $file) {
  44. include(CFCT_PATH.'plugins/'.$file);
  45. }
  46. }
  47. function cfct_default_file($dir) {
  48. $fancy = $dir.'-default.php';
  49. file_exists(CFCT_PATH.$dir.'/'.$fancy) ? $default = $fancy : $default = 'default.php';
  50. return $default;
  51. }
  52. function cfct_context() {
  53. $context = 'home';
  54. if (is_page()) {
  55. $context = 'page';
  56. }
  57. else if (is_single()) {
  58. $context = 'single';
  59. }
  60. else if (is_category()) {
  61. $context = 'category';
  62. }
  63. else if (is_tag()) {
  64. $context = 'tag';
  65. }
  66. else if (is_author()) {
  67. $context = 'author';
  68. }
  69. else if (is_archive()) {
  70. // possible future abstraction for:
  71. // is_month()
  72. // is_year()
  73. // is_day()
  74. $context = 'archive';
  75. }
  76. else if (is_search()) {
  77. $context = 'search';
  78. }
  79. else if (is_home()) {
  80. $context = 'home';
  81. // TODO - check page #
  82. }
  83. else if (is_404()) {
  84. $context = '404';
  85. }
  86. return apply_filters('cfct_context', $context);
  87. }
  88. /**
  89. * @param $template = folder name of file
  90. * @param $type = file name of file
  91. * @param $keys = keys that could be used for additional filename params
  92. * returns false if file does not exist
  93. *
  94. */
  95. function cfct_filename($dir, $type = 'default', $keys = array()) {
  96. switch ($type) {
  97. case 'author':
  98. if (count($keys)) {
  99. $file = 'author-'.$keys[0];
  100. }
  101. else {
  102. $file = 'author';
  103. }
  104. break;
  105. case 'category':
  106. if (count($keys)) {
  107. $file = 'cat-'.$keys[0];
  108. }
  109. else {
  110. $file = 'category';
  111. }
  112. break;
  113. case 'tag':
  114. if (count($keys)) {
  115. $file = 'tag-'.$keys[0];
  116. }
  117. else {
  118. $file = 'tag';
  119. }
  120. break;
  121. case 'meta':
  122. if (count($keys)) {
  123. foreach ($keys as $k => $v) {
  124. if (!empty($v)) {
  125. $file = 'meta-'.$k.'-'.$v;
  126. }
  127. else {
  128. $file = 'meta-'.$k;
  129. }
  130. break;
  131. }
  132. }
  133. break;
  134. case 'user':
  135. if (count($keys)) {
  136. $file = 'user-'.$keys[0];
  137. }
  138. break;
  139. case 'role':
  140. if (count($keys)) {
  141. $file = 'role-'.$keys[0];
  142. }
  143. break;
  144. case 'parent':
  145. if (count($keys)) {
  146. $file = 'parent-'.$keys[0];
  147. }
  148. break;
  149. default:
  150. // handles page, etc.
  151. $file = $type;
  152. }
  153. // fallback for category, author, tag, etc.
  154. $path = CFCT_PATH.$dir.'/'.$file.'.php';
  155. if (!file_exists($path)) {
  156. switch ($type) {
  157. case 'author':
  158. case 'category':
  159. case 'tag':
  160. $archive_file = CFCT_PATH.$dir.'/archive.php';
  161. if (file_exists($archive_file)) {
  162. $path = $archive_file;
  163. }
  164. }
  165. }
  166. $default = CFCT_PATH.$dir.'/'.cfct_default_file($dir);
  167. if (file_exists($path)) {
  168. $path = $path;
  169. }
  170. else if (file_exists($default)) {
  171. $path = $default;
  172. }
  173. else {
  174. $path = false;
  175. }
  176. return apply_filters('cfct_filename', $path);
  177. }
  178. function cfct_template($dir, $keys = array()) {
  179. $context = cfct_context();
  180. $file = cfct_filename($dir, $context, $keys);
  181. if ($file) {
  182. include($file);
  183. }
  184. else {
  185. cfct_die('Error loading '.$dir.' '.__LINE__);
  186. }
  187. }
  188. function cfct_template_file($dir, $file, $data = null) {
  189. $path = '';
  190. if (!empty($file)) {
  191. $file = basename($file, '.php');
  192. $path = CFCT_PATH.$dir.'/'.$file.'.php';
  193. }
  194. if (file_exists($path)) {
  195. include($path);
  196. }
  197. else {
  198. cfct_die('Error loading '.$file.' '.__LINE__);
  199. }
  200. }
  201. function cfct_choose_general_template($dir) {
  202. $exec_order = array(
  203. 'author'
  204. , 'role'
  205. , 'category'
  206. , 'tag'
  207. , 'single'
  208. , 'default'
  209. );
  210. $new_exec_order = apply_filters('cfct_general_match_order', $exec_order);
  211. $files = cfct_files(CFCT_PATH.$dir);
  212. foreach ($new_exec_order as $func) {
  213. $func_name = 'cfct_choose_general_template_'.$func;
  214. if (function_exists($func_name) && in_array($func, $exec_order)) {
  215. $filename = $func_name($dir, $files);
  216. if ($filename != false) {
  217. break;
  218. }
  219. }
  220. }
  221. return apply_filters('cfct_choose_general_template', $filename);
  222. }
  223. function cfct_choose_general_template_author($dir, $files) {
  224. $files = cfct_author_templates($dir, $files);
  225. if (count($files)) {
  226. $username = get_query_var('author_name');
  227. if (empty($username)) {
  228. $user = new WP_User(get_query_var('author'));
  229. $username = $user->user_login;
  230. }
  231. $filename = 'author-'.$username.'.php';
  232. if (in_array($filename, $files)) {
  233. $keys = array($username);
  234. return cfct_filename($dir, 'author', $keys);
  235. }
  236. }
  237. return false;
  238. }
  239. function cfct_choose_general_template_category($dir, $files) {
  240. $files = cfct_cat_templates($dir, $files);
  241. if (count($files)) {
  242. global $cat;
  243. $slug = cfct_cat_id_to_slug($cat);
  244. if (in_array('cat-'.$slug.'.php', $files)) {
  245. $keys = array($slug);
  246. return cfct_filename($dir, 'category', $keys);
  247. }
  248. }
  249. return false;
  250. }
  251. function cfct_choose_general_template_tag($dir, $files) {
  252. $files = cfct_tag_templates($dir, $files);
  253. if (count($files)) {
  254. $tag = get_query_var('tag');
  255. if (in_array('tag-'.$tag.'.php', $files)) {
  256. $keys = array($tag);
  257. return cfct_filename($dir, 'tag', $keys);
  258. }
  259. }
  260. return false;
  261. }
  262. function cfct_choose_general_template_role($dir, $files) {
  263. $files = cfct_role_templates($dir, $files);
  264. if (count($files)) {
  265. $username = get_query_var('author_name');
  266. $user = new WP_User(cfct_username_to_id($username));
  267. if (!empty($user->user_login)) {
  268. if (count($user->roles)) {
  269. foreach ($user->roles as $role) {
  270. $role_file = 'role-'.$role.'.php';
  271. if (in_array($role_file, $files)) {
  272. return $role_file;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return false;
  279. }
  280. function cfct_choose_general_template_single($dir, $files) {
  281. if (cfct_context() == 'single') {
  282. $files = cfct_single_templates($dir, $files);
  283. if (count($files)) {
  284. // check to see if we're in the loop.
  285. global $post;
  286. $orig_post = $post;
  287. while (have_posts()) {
  288. the_post();
  289. $filename = cfct_choose_single_template($files, 'single-*');
  290. if (!$filename) {
  291. if (file_exists(CFCT_PATH.$dir.'/single.php')) {
  292. $filename = 'single.php';
  293. }
  294. }
  295. }
  296. rewind_posts();
  297. $post = $orig_post;
  298. return $filename;
  299. }
  300. }
  301. return false;
  302. }
  303. function cfct_choose_general_template_default($dir, $files) {
  304. $context = cfct_context();
  305. return cfct_filename($dir, $context);
  306. }
  307. function cfct_choose_single_template($files = array(), $filter = '*') {
  308. // must be called within the_loop - cfct_choose_general_template_single() approximates a loop for this reason.
  309. $exec_order = array(
  310. 'author'
  311. , 'meta'
  312. , 'category'
  313. , 'role'
  314. , 'tag'
  315. , 'parent' // for pages
  316. , 'default'
  317. );
  318. $exec_order = apply_filters('cfct_single_match_order', $exec_order);
  319. $filename = false;
  320. global $post;
  321. foreach ($exec_order as $type) {
  322. switch ($type) {
  323. case 'author':
  324. $author_files = cfct_author_templates('', $files);
  325. if (count($author_files)) {
  326. $author = get_the_author_login();
  327. $file = cfct_filename_filter('author-'.$author.'.php', $filter);
  328. if (in_array($file, $author_files)) {
  329. $filename = $file;
  330. }
  331. }
  332. break;
  333. case 'meta':
  334. $meta_files = cfct_meta_templates('', $files);
  335. if (count($meta_files)) {
  336. $meta = get_post_custom($post->ID);
  337. if (count($meta)) {
  338. // check key, value matches first
  339. foreach ($meta as $k => $v) {
  340. $val = $v[0];
  341. $file = cfct_filename_filter('meta-'.$k.'-'.$val.'.php', $filter);
  342. if (in_array($file, $meta_files)) {
  343. $filename = $file;
  344. break;
  345. }
  346. }
  347. // check key matches only
  348. if (!$filename) {
  349. foreach ($meta as $k => $v) {
  350. $file = cfct_filename_filter('meta-'.$k.'.php', $filter);
  351. if (in_array($file, $meta_files)) {
  352. $filename = $file;
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. break;
  360. case 'category':
  361. $cat_files = cfct_cat_templates($type, $files);
  362. if (count($cat_files)) {
  363. foreach ($cat_files as $file) {
  364. $cat_id = cfct_cat_filename_to_id($file);
  365. if (in_category($cat_id)) {
  366. $filename = $file;
  367. break;
  368. }
  369. }
  370. }
  371. break;
  372. case 'role':
  373. $role_files = cfct_role_templates($type, $files);
  374. if (count($role_files)) {
  375. $user = new WP_User(get_the_author_ID());
  376. if (count($user->roles)) {
  377. foreach ($role_files as $file) {
  378. foreach ($user->roles as $role) {
  379. if (cfct_role_filename_to_name($file) == $role) {
  380. $filename = $file;
  381. break;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. break;
  388. case 'tag':
  389. $tag_files = cfct_tag_templates($type, $files);
  390. if (count($tag_files)) {
  391. $tags = get_the_tags($post->ID);
  392. if (is_array($tags) && count($tags)) {
  393. foreach ($tag_files as $file) {
  394. foreach ($tags as $tag) {
  395. if ($tag->slug == cfct_tag_filename_to_name($file)) {
  396. $filename = $file;
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. break;
  404. case 'parent':
  405. $parent_files = cfct_parent_templates($type, $files);
  406. if (count($parent_files) && $post->post_parent > 0) {
  407. $parent = cfct_post_id_to_slug($post->post_parent);
  408. $file = cfct_filename_filter('parent-'.$parent.'.php', $filter);
  409. if (in_array($file, $parent_files)) {
  410. $filename = $file;
  411. }
  412. }
  413. break;
  414. case 'default':
  415. break;
  416. }
  417. if ($filename) {
  418. break;
  419. }
  420. }
  421. return apply_filters('cfct_choose_single_template', $filename);
  422. }
  423. function cfct_choose_content_template($type = 'content') {
  424. $files = cfct_files(CFCT_PATH.$type);
  425. $filename = cfct_choose_single_template($files);
  426. if (cfct_context() == 'page' && file_exists(CFCT_PATH.$type.'/page.php')) {
  427. $filename = 'page.php';
  428. }
  429. if (!$filename) {
  430. $filename = cfct_default_file($type);
  431. }
  432. return apply_filters('cfct_choose_content_template', $filename);
  433. }
  434. function cfct_choose_comment_template() {
  435. $exec_order = array(
  436. 'ping'
  437. , 'author'
  438. , 'user'
  439. , 'role'
  440. , 'default'
  441. );
  442. $new_exec_order = apply_filters('cfct_comment_match_order', $exec_order);
  443. $files = cfct_files(CFCT_PATH.'comment');
  444. foreach ($new_exec_order as $func) {
  445. $func_name = 'cfct_choose_comment_template_'.$func;
  446. if (function_exists($func_name) && in_array($func, $exec_order)) {
  447. $filename = $func_name($files);
  448. if ($filename != false) {
  449. break;
  450. }
  451. }
  452. }
  453. return apply_filters('cfct_choose_comment_template', $filename);
  454. }
  455. function cfct_choose_comment_template_ping($files) {
  456. global $comment;
  457. switch ($comment->comment_type) {
  458. case 'pingback':
  459. case 'trackback':
  460. return 'ping';
  461. break;
  462. }
  463. return false;
  464. }
  465. function cfct_choose_comment_template_author($files) {
  466. global $post, $comment;
  467. if (!empty($comment->user_id) && $comment->user_id == $post->post_author && in_array('author.php', $files)) {
  468. return 'author';
  469. }
  470. return false;
  471. }
  472. function cfct_choose_comment_template_user($files) {
  473. global $comment;
  474. $files = cfct_comment_templates('user', $files);
  475. if (count($files) && !empty($comment->user_id)) {
  476. $user = new WP_User($comment->user_id);
  477. if (!empty($user->user_login)) {
  478. $user_file = 'user-'.$user->user_login.'.php';
  479. if (in_array($user_file, $files)) {
  480. return $user_file;
  481. }
  482. }
  483. }
  484. return false;
  485. }
  486. function cfct_choose_comment_template_role($files) {
  487. global $comment;
  488. $files = cfct_comment_templates('role', $files);
  489. if (count($files) && !empty($comment->user_id)) {
  490. $user = new WP_User($comment->user_id);
  491. if (!empty($user->user_login)) {
  492. if (count($user->roles)) {
  493. foreach ($user->roles as $role) {
  494. $role_file = 'role-'.$role.'.php';
  495. if (in_array($role_file, $files)) {
  496. return $role_file;
  497. }
  498. }
  499. }
  500. }
  501. }
  502. return false;
  503. }
  504. function cfct_choose_comment_template_default($files) {
  505. return cfct_default_file('comment');
  506. }
  507. function cfct_files($path) {
  508. $files = wp_cache_get('cfct_files_'.$path, 'cfct');
  509. if ($files) {
  510. return $files;
  511. }
  512. $files = array();
  513. if ($handle = opendir($path)) {
  514. while (false !== ($file = readdir($handle))) {
  515. $path = trailingslashit($path);
  516. if (is_file($path.$file) && strtolower(substr($file, -4, 4)) == ".php") {
  517. $files[] = $file;
  518. }
  519. }
  520. }
  521. wp_cache_set('cfct_files_'.$path, $files, 'cfct', 3600);
  522. return $files;
  523. }
  524. function cfct_filename_filter($filename, $filter) {
  525. return str_replace('*', $filename, $filter);
  526. }
  527. function cfct_filter_files($files = array(), $prefix = '') {
  528. $matches = array();
  529. if (count($files)) {
  530. foreach ($files as $file) {
  531. if (strpos($file, $prefix) !== false) {
  532. $matches[] = $file;
  533. }
  534. }
  535. }
  536. return $matches;
  537. }
  538. function cfct_meta_templates($dir, $files = null) {
  539. if (is_null($files)) {
  540. $files = cfct_files(CFCT_PATH.$dir);
  541. }
  542. $matches = cfct_filter_files($files, 'meta-');
  543. return apply_filters('cfct_meta_templates', $matches);
  544. }
  545. function cfct_cat_templates($dir, $files = null) {
  546. if (is_null($files)) {
  547. $files = cfct_files(CFCT_PATH.$dir);
  548. }
  549. $matches = cfct_filter_files($files, 'cat-');
  550. return apply_filters('cfct_cat_templates', $matches);
  551. }
  552. function cfct_tag_templates($dir, $files = null) {
  553. if (is_null($files)) {
  554. $files = cfct_files(CFCT_PATH.$dir);
  555. }
  556. $matches = cfct_filter_files($files, 'tag-');
  557. return apply_filters('cfct_tag_templates', $matches);
  558. }
  559. function cfct_author_templates($dir, $files = null) {
  560. if (is_null($files)) {
  561. $files = cfct_files(CFCT_PATH.$dir);
  562. }
  563. $matches = cfct_filter_files($files, 'author-');
  564. return apply_filters('cfct_author_templates', $matches);
  565. }
  566. function cfct_role_templates($dir, $files = null) {
  567. if (is_null($files)) {
  568. $files = cfct_files(CFCT_PATH.$dir);
  569. }
  570. $matches = cfct_filter_files($files, 'role-');
  571. return apply_filters('cfct_role_templates', $matches);
  572. }
  573. function cfct_parent_templates($dir, $files = null) {
  574. if (is_null($files)) {
  575. $files = cfct_files(CFCT_PATH.$dir);
  576. }
  577. $matches = cfct_filter_files($files, 'parent-');
  578. return apply_filters('cfct_parent_templates', $matches);
  579. }
  580. function cfct_single_templates($dir, $files = null) {
  581. if (is_null($files)) {
  582. $files = cfct_files(CFCT_PATH.$dir);
  583. }
  584. $matches = cfct_filter_files($files, 'single');
  585. return apply_filters('cfct_single_templates', $matches);
  586. }
  587. function cfct_comment_templates($type, $files = false) {
  588. if (!$files) {
  589. $files = cfct_files(CFCT_PATH.'comment');
  590. }
  591. $matches = array();
  592. switch ($type) {
  593. case 'user':
  594. $matches = cfct_filter_files($files, 'user-');
  595. break;
  596. case 'role':
  597. $matches = cfct_filter_files($files, 'role-');
  598. break;
  599. }
  600. return apply_filters('cfct_comment_templates', $matches);
  601. }
  602. function cfct_cat_filename_to_id($file) {
  603. $cat = str_replace(array('single-cat-', 'cat-', '.php'), '', $file);
  604. $cat = get_category_by_slug($cat);
  605. return $cat->cat_ID;
  606. }
  607. function cfct_cat_filename_to_name($file) {
  608. $cat = str_replace(array('single-cat-', 'cat-', '.php'), '', $file);
  609. $cat = get_category_by_slug($cat);
  610. return $cat->name;
  611. }
  612. function cfct_cat_filename_to_slug($file) {
  613. return str_replace(array('single-cat-', 'cat-', '.php'), '', $file);
  614. }
  615. function cfct_cat_id_to_slug($id) {
  616. $cat = &get_category($id);
  617. return $cat->slug;
  618. }
  619. function cfct_username_to_id($username) {
  620. return get_profile('ID', $username);
  621. }
  622. function cfct_tag_filename_to_name($file) {
  623. return str_replace(array('single-tag-', 'tag-', '.php'), '', $file);
  624. }
  625. function cfct_author_filename_to_name($file) {
  626. return str_replace(array('single-author-', 'author-', '.php'), '', $file);
  627. }
  628. function cfct_role_filename_to_name($file) {
  629. return str_replace(array('single-role-', 'role-', '.php'), '', $file);
  630. }
  631. function cfct_post_id_to_slug($id) {
  632. $post = get_post($id);
  633. return $post->post_name;
  634. }
  635. function cfct_basic_content_formatting($str) {
  636. $str = wptexturize($str);
  637. $str = convert_smilies($str);
  638. $str = convert_chars($str);
  639. $str = wpautop($str);
  640. return $str;
  641. }
  642. ?>