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

/experimental/text/functions/utility.php

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