PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/tags/3.1/utility.php

http://carrington.googlecode.com/
PHP | 1432 lines | 837 code | 69 blank | 526 comment | 184 complexity | 8ee979922194c2246a2f63f81ec0c1e3 MD5 | raw file
  1. <?php
  2. // This file is part of the Carrington Core Framework for WordPress
  3. // http://carringtontheme.com
  4. //
  5. // Copyright (c) 2008-2010 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. /**
  18. * Die with custom error page if it exists
  19. *
  20. * @param string $str String to die with if no file found
  21. *
  22. **/
  23. function cfct_die($str = '') {
  24. if (!empty($str)) {
  25. if (file_exists(CFCT_PATH.'error/exit.php')) {
  26. include(CFCT_PATH.'error/exit.php');
  27. die();
  28. }
  29. else {
  30. wp_die($str);
  31. }
  32. }
  33. }
  34. /**
  35. * Display custom banners for alerts
  36. *
  37. * @param string $str String to display if no banner file found
  38. *
  39. **/
  40. function cfct_banner($str = '') {
  41. if (!empty($str)) {
  42. if (file_exists(CFCT_PATH.'misc/banner.php')) {
  43. include(CFCT_PATH.'misc/banner.php');
  44. }
  45. else {
  46. echo '<p>'.$str.'</p>';
  47. }
  48. }
  49. }
  50. /**
  51. * Get a Carrington Framework option, load the default otherwise
  52. *
  53. * @param string $name Name of the option to retrieve
  54. * @return mixed Value of the option
  55. *
  56. **/
  57. function cfct_get_option($name) {
  58. $defaults = array(
  59. 'cfct_credit' => 'yes',
  60. 'cfct_lightbox' => 'yes',
  61. 'cfct_header_image' => 0,
  62. );
  63. $defaults = apply_filters('cfct_option_defaults', $defaults);
  64. $value = get_option($name);
  65. if ($value == '' && isset($defaults[$name])) {
  66. $value = $defaults[$name];
  67. }
  68. return $value;
  69. }
  70. /**
  71. * Load theme plugins
  72. *
  73. **/
  74. function cfct_load_plugins() {
  75. $files = cfct_files(CFCT_PATH.'plugins');
  76. if (count($files)) {
  77. foreach ($files as $file) {
  78. if (file_exists(CFCT_PATH.'plugins/'.$file)) {
  79. include_once(CFCT_PATH.'plugins/'.$file);
  80. }
  81. // child theme support
  82. if (file_exists(STYLESHEETPATH.'/plugins/'.$file)) {
  83. include_once(STYLESHEETPATH.'/plugins/'.$file);
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * Return the default file to use for a given directory
  90. *
  91. * @param string $dir Directory to get the default file for
  92. * @return string Filename pertaining to the default file
  93. *
  94. **/
  95. function cfct_default_file($dir) {
  96. $fancy = $dir.'-default.php';
  97. file_exists(CFCT_PATH.$dir.'/'.$fancy) ? $default = $fancy : $default = 'default.php';
  98. return $default;
  99. }
  100. /**
  101. * Return the context of the current page
  102. *
  103. * @return string The context of the current page
  104. *
  105. **/
  106. function cfct_context() {
  107. $context = 'home';
  108. if (is_home()) {
  109. $context = 'home';
  110. }
  111. else if (is_page()) {
  112. $context = 'page';
  113. }
  114. else if (is_single()) {
  115. $context = 'single';
  116. }
  117. else if (is_category()) {
  118. $context = 'category';
  119. }
  120. else if (is_tag()) {
  121. $context = 'tag';
  122. }
  123. else if (is_tax()) {
  124. $context = 'taxonomy';
  125. }
  126. else if (is_author()) {
  127. $context = 'author';
  128. }
  129. else if (is_archive()) {
  130. // possible future abstraction for:
  131. // is_month()
  132. // is_year()
  133. // is_day()
  134. $context = 'archive';
  135. }
  136. else if (is_search()) {
  137. $context = 'search';
  138. }
  139. else if (is_404()) {
  140. $context = '404';
  141. }
  142. return apply_filters('cfct_context', $context);
  143. }
  144. /**
  145. * Get the filename for a given directory, type and keys
  146. *
  147. * @param string $dir Folder name of file
  148. * @param string $type File name of file
  149. * @param array $keys Keys that could be used for additional filename params
  150. * @return mixed Path to the file, false if file does not exist
  151. *
  152. */
  153. function cfct_filename($dir, $type = 'default', $keys = array()) {
  154. switch ($type) {
  155. case 'author':
  156. if (count($keys)) {
  157. $file = 'author-'.$keys[0];
  158. }
  159. else {
  160. $file = 'author';
  161. }
  162. break;
  163. case 'category':
  164. if (count($keys)) {
  165. $file = 'cat-'.$keys[0];
  166. }
  167. else {
  168. $file = 'category';
  169. }
  170. break;
  171. case 'tag':
  172. if (count($keys)) {
  173. $file = 'tag-'.$keys[0];
  174. }
  175. else {
  176. $file = 'tag';
  177. }
  178. break;
  179. case 'meta':
  180. if (count($keys)) {
  181. foreach ($keys as $k => $v) {
  182. if (!empty($v)) {
  183. $file = 'meta-'.$k.'-'.$v;
  184. }
  185. else {
  186. $file = 'meta-'.$k;
  187. }
  188. break;
  189. }
  190. }
  191. break;
  192. case 'user':
  193. if (count($keys)) {
  194. $file = 'user-'.$keys[0];
  195. }
  196. break;
  197. case 'role':
  198. if (count($keys)) {
  199. $file = 'role-'.$keys[0];
  200. }
  201. break;
  202. case 'parent':
  203. if (count($keys)) {
  204. $file = 'parent-'.$keys[0];
  205. }
  206. break;
  207. case 'taxonomy':
  208. switch (count($keys)) {
  209. case 1:
  210. $file = 'tax-'.$keys[0];
  211. break;
  212. case 2:
  213. $file = 'tax-'.$keys[0].'-'.$keys[1];
  214. break;
  215. default:
  216. break;
  217. }
  218. break;
  219. default:
  220. // handles page, etc.
  221. $file = $type;
  222. }
  223. // fallback for category, author, tag, etc.
  224. // child theme path
  225. $path = STYLESHEETPATH.'/'.$dir.'/'.$file.'.php';
  226. // check for child theme first
  227. if (!file_exists($path)) {
  228. // use parent theme if no child theme file found
  229. $path = CFCT_PATH.$dir.'/'.$file.'.php';
  230. }
  231. if (!file_exists($path)) {
  232. switch ($type) {
  233. case 'author':
  234. case 'category':
  235. case 'tag':
  236. case 'taxonomy':
  237. // child theme path
  238. $path = STYLESHEETPATH.'/'.$dir.'/archive.php';
  239. if (!file_exists($path)) {
  240. // use parent theme if no child theme file found
  241. $path = CFCT_PATH.$dir.'/archive.php';
  242. }
  243. }
  244. }
  245. $default = CFCT_PATH.$dir.'/'.cfct_default_file($dir);
  246. if (file_exists($path)) {
  247. $path = $path;
  248. }
  249. else if (file_exists($default)) {
  250. $path = $default;
  251. }
  252. else {
  253. $path = false;
  254. }
  255. return apply_filters('cfct_filename', $path);
  256. }
  257. /**
  258. * Include a specific file based on context, directory and keys
  259. *
  260. * @param string $dir
  261. * @param array $keys Keys used to help build the filename
  262. *
  263. **/
  264. function cfct_template($dir, $keys = array()) {
  265. $context = cfct_context();
  266. $file = cfct_filename($dir, $context, $keys);
  267. if ($file) {
  268. include($file);
  269. }
  270. else {
  271. cfct_die('Error loading '.$dir.' '.__LINE__);
  272. }
  273. }
  274. /**
  275. * Include a specific file based on directory and filename
  276. *
  277. * @param string $dir Directory the file will be in
  278. * @param string $file Filename
  279. * @param $data not used
  280. *
  281. **/
  282. function cfct_template_file($dir, $file, $data = null) {
  283. $path = '';
  284. if (!empty($file)) {
  285. $file = basename($file, '.php');
  286. // child theme support
  287. $path = STYLESHEETPATH.'/'.$dir.'/'.$file.'.php';
  288. if (!file_exists($path)) {
  289. $path = CFCT_PATH.$dir.'/'.$file.'.php';
  290. }
  291. }
  292. if (file_exists($path)) {
  293. include($path);
  294. }
  295. else {
  296. cfct_die('Error loading '.$file.' '.__LINE__);
  297. }
  298. }
  299. /**
  300. * Gets the proper filename (path) to use in displaying a template
  301. *
  302. * @param string $dir Directory to use/search in
  303. * @return string Path to the file
  304. *
  305. **/
  306. function cfct_choose_general_template($dir) {
  307. $exec_order = array(
  308. 'author',
  309. 'role',
  310. 'category',
  311. 'taxonomy',
  312. 'tag',
  313. 'type',
  314. 'single',
  315. 'default'
  316. );
  317. $exec_order = apply_filters('cfct_general_match_order', $exec_order);
  318. $files = cfct_files(CFCT_PATH.$dir);
  319. foreach ($exec_order as $func_name) {
  320. if (!function_exists($func_name)) {
  321. $func_name = 'cfct_choose_general_template_'.$func_name;
  322. }
  323. if (function_exists($func_name)) {
  324. $filename = $func_name($dir, $files);
  325. if ($filename != false) {
  326. break;
  327. }
  328. }
  329. }
  330. return apply_filters('cfct_choose_general_template', $filename, $dir);
  331. }
  332. /**
  333. * Gets the proper filename (path) to use for displaying a page based on an author's name
  334. *
  335. * @param string $dir Directory to use for selecting the template file
  336. * @param array $files A list of files to loop through
  337. * @return mixed Path to the file, false if no file exists
  338. *
  339. **/
  340. function cfct_choose_general_template_author($dir, $files) {
  341. $files = cfct_author_templates($dir, $files);
  342. if (count($files)) {
  343. $username = get_query_var('author_name');
  344. if (empty($username)) {
  345. $user = new WP_User(get_query_var('author'));
  346. $username = $user->user_login;
  347. }
  348. $filename = 'author-'.$username.'.php';
  349. if (in_array($filename, $files)) {
  350. $keys = array($username);
  351. return cfct_filename($dir, 'author', $keys);
  352. }
  353. }
  354. return false;
  355. }
  356. /**
  357. * Gets the proper filename (path) to use for displaying a page based on a category's slug
  358. *
  359. * @param string $dir Directory to use for selecting the template file
  360. * @param array $files A list of files to loop through
  361. * @return mixed Path to the file, false if no file exists
  362. *
  363. **/
  364. function cfct_choose_general_template_category($dir, $files) {
  365. $files = cfct_cat_templates($dir, $files);
  366. if (count($files)) {
  367. global $cat;
  368. $slug = cfct_cat_id_to_slug($cat);
  369. if (in_array('cat-'.$slug.'.php', $files)) {
  370. $keys = array($slug);
  371. return cfct_filename($dir, 'category', $keys);
  372. }
  373. }
  374. return false;
  375. }
  376. /**
  377. * Gets the proper filename (path) to use for displaying a page based on a custom taxonomy and a slug within that taxonomy
  378. *
  379. * @param string $dir Directory to use for selecting the template file
  380. * @param array $files A list of files to loop through
  381. * @return mixed Path to the file, false if no file exists
  382. *
  383. **/
  384. function cfct_choose_general_template_taxonomy($dir, $files) {
  385. $files = cfct_tax_templates($dir, $files);
  386. if (count($files)) {
  387. $tax = get_query_var('taxonomy');
  388. $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
  389. if (!empty($term) && in_array('tax-'.$tax.'-'.$term->slug.'.php', $files)) {
  390. $keys = array($tax, $term->slug);
  391. return cfct_filename($dir, 'taxonomy', $keys);
  392. }
  393. }
  394. return false;
  395. }
  396. /**
  397. * Gets the proper filename (path) to use for displaying a page based on a tag slug
  398. *
  399. * @param string $dir Directory to use for selecting the template file
  400. * @param array $files A list of files to loop through
  401. * @return mixed Path to the file, false if no file exists
  402. *
  403. **/
  404. function cfct_choose_general_template_tag($dir, $files) {
  405. $files = cfct_tag_templates($dir, $files);
  406. if (count($files)) {
  407. $tag = get_query_var('tag');
  408. if (in_array('tag-'.$tag.'.php', $files)) {
  409. $keys = array($tag);
  410. return cfct_filename($dir, 'tag', $keys);
  411. }
  412. }
  413. return false;
  414. }
  415. /**
  416. * Gets the proper filename (path) to use for displaying a page based on custom post type
  417. *
  418. * @param string $dir Directory to use for selecting the template file
  419. * @param array $files A list of files to loop through
  420. * @return mixed Path to the file, false if no file exists
  421. *
  422. **/
  423. function cfct_choose_general_template_type($dir, $files) {
  424. $files = cfct_type_templates($dir, $files);
  425. if (count($files)) {
  426. $type = get_query_var('post_type');
  427. $file = 'type-'.$type.'.php';
  428. if (in_array($file, $files)) {
  429. return $file;
  430. }
  431. }
  432. return false;
  433. }
  434. /**
  435. * Gets the proper filename (path) to use for displaying a page based on a user's role
  436. *
  437. * @param string $dir Directory to use for selecting the template file
  438. * @param array $files A list of files to loop through
  439. * @return mixed Path to the file, false if no file exists
  440. *
  441. **/
  442. function cfct_choose_general_template_role($dir, $files) {
  443. $files = cfct_role_templates($dir, $files);
  444. if (count($files)) {
  445. $username = get_query_var('author_name');
  446. $user = new WP_User(cfct_username_to_id($username));
  447. if (!empty($user->user_login)) {
  448. if (count($user->roles)) {
  449. foreach ($user->roles as $role) {
  450. $role_file = 'role-'.$role.'.php';
  451. if (in_array($role_file, $files)) {
  452. return $role_file;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. return false;
  459. }
  460. /**
  461. * Gets the proper filename (path) to use for displaying a page based on whether or not it is a single page and its general context
  462. *
  463. * @param string $dir Directory to use for selecting the template file
  464. * @param array $files A list of files to loop through
  465. * @return mixed Path to the file, false if no file exists
  466. *
  467. **/
  468. function cfct_choose_general_template_single($dir, $files) {
  469. if (cfct_context() == 'single') {
  470. $files = cfct_single_templates($dir, $files);
  471. if (count($files)) {
  472. // check to see if we're in the loop.
  473. global $post;
  474. $orig_post = $post;
  475. while (have_posts()) {
  476. the_post();
  477. $filename = cfct_choose_single_template($files, 'single-*');
  478. if (!$filename) {
  479. if (file_exists(CFCT_PATH.$dir.'/single.php')) {
  480. $filename = 'single.php';
  481. }
  482. }
  483. }
  484. rewind_posts();
  485. $post = $orig_post;
  486. return $filename;
  487. }
  488. }
  489. return false;
  490. }
  491. /**
  492. * Gets the proper filename (path) to use for displaying a default page based on context
  493. *
  494. * @param string $dir Directory to use for selecting the template file
  495. * @param array $files A list of files to loop through
  496. * @return mixed path to the file, false if no file exists
  497. *
  498. **/
  499. function cfct_choose_general_template_default($dir, $files) {
  500. $context = cfct_context();
  501. $keys = array();
  502. if ($context == 'taxonomy') {
  503. $keys = array(get_query_var('taxonomy'));
  504. }
  505. return cfct_filename($dir, $context, $keys);
  506. }
  507. /**
  508. * Chooses which template to display for the single context
  509. *
  510. * @param array $files A list of files to search through to find the correct template
  511. * @param string $filter Used in filtering the filename
  512. * @param string $dir The directory to search for matching files in
  513. * @return mixed path to the file, false if no file exists
  514. *
  515. **/
  516. function cfct_choose_single_template($files = array(), $filter = '*', $dir = '') {
  517. // must be called within the_loop - cfct_choose_general_template_single() approximates a loop for this reason.
  518. $exec_order = array(
  519. 'author',
  520. 'meta',
  521. 'format',
  522. 'category',
  523. 'taxonomy',
  524. 'type',
  525. 'role',
  526. 'tag',
  527. 'parent', // for pages
  528. 'default',
  529. );
  530. $exec_order = apply_filters('cfct_single_match_order', $exec_order);
  531. $filename = false;
  532. foreach ($exec_order as $func_name) {
  533. if (!function_exists($func_name)) {
  534. $func_name = 'cfct_choose_single_template_'.$func_name;
  535. }
  536. if (function_exists($func_name)) {
  537. $filename = $func_name($dir, $files, $filter);
  538. if ($filename !== false) {
  539. break;
  540. }
  541. }
  542. }
  543. return apply_filters('cfct_choose_single_template', $filename);
  544. }
  545. /**
  546. * Chooses which template to display for the single context based on custom post type
  547. *
  548. * @param string $dir Directory to search through for files
  549. * @param array $files A list of files to search through to find the correct template
  550. * @param string $filter Used in filtering the filename
  551. * @return mixed path to the file, false if no file exists
  552. *
  553. **/
  554. function cfct_choose_single_template_type($dir, $files, $filter) {
  555. $type_files = cfct_type_templates($dir, $files);
  556. if (count($type_files)) {
  557. global $post;
  558. $file = cfct_filename_filter('type-'.$post->post_type.'.php', $filter);
  559. if (in_array($file, $type_files)) {
  560. return $file;
  561. }
  562. }
  563. return false;
  564. }
  565. /**
  566. * Chooses which template to display for the single context based on author login
  567. *
  568. * @param string $dir Directory to use for selecting the template file
  569. * @param array $files A list of files to search through to find the correct template
  570. * @param string $filter Used in filtering the filename
  571. * @return mixed Path to the file, false if no file exists
  572. *
  573. **/
  574. function cfct_choose_single_template_author($dir, $files, $filter) {
  575. $author_files = cfct_author_templates($dir, $files);
  576. if (count($author_files)) {
  577. $author = get_the_author_login();
  578. $file = cfct_filename_filter('author-'.$author.'.php', $filter);
  579. if (in_array($file, $author_files)) {
  580. return $file;
  581. }
  582. }
  583. return false;
  584. }
  585. /**
  586. * Chooses which template to display for the single context based on meta information
  587. *
  588. * @param string $dir Directory to use for selecting the template file
  589. * @param array $files A list of files to search through to find the correct template
  590. * @param string $filter Used in filtering the filename
  591. * @return mixed Path to the file, false if no file exists
  592. *
  593. **/
  594. function cfct_choose_single_template_meta($dir, $files, $filter) {
  595. global $post;
  596. $meta_files = cfct_meta_templates('', $files);
  597. if (count($meta_files)) {
  598. $meta = get_post_custom($post->ID);
  599. if (count($meta)) {
  600. // check key, value matches first
  601. foreach ($meta as $k => $v) {
  602. $val = $v[0];
  603. $file = cfct_filename_filter('meta-'.$k.'-'.$val.'.php', $filter);
  604. if (in_array($file, $meta_files)) {
  605. return $file;
  606. }
  607. }
  608. // check key matches only
  609. if (!$filename) {
  610. foreach ($meta as $k => $v) {
  611. $file = cfct_filename_filter('meta-'.$k.'.php', $filter);
  612. if (in_array($file, $meta_files)) {
  613. return $file;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. return false;
  620. }
  621. /**
  622. * Chooses which template to display for the single context based on post format
  623. *
  624. * @param string $dir Directory to use for selecting the template file
  625. * @param array $files A list of files to search through to find the correct template
  626. * @param string $filter Used in filtering the filename
  627. * @return mixed Path to the file, false if no file exists
  628. *
  629. **/
  630. function cfct_choose_single_template_format($dir, $files, $filter) {
  631. global $post;
  632. $format_files = cfct_format_templates($dir, $files);
  633. if (count($format_files)) {
  634. $post_format = get_post_format($post->ID);
  635. foreach ($format_files as $file) {
  636. if (cfct_format_filename_to_format($file) == $post_format) {
  637. return cfct_filename_filter($file, $filter);
  638. }
  639. }
  640. }
  641. return false;
  642. }
  643. /**
  644. * Chooses which template to display for the single context based on category slug
  645. *
  646. * @param string $dir Directory to use for selecting the template file
  647. * @param array $files A list of files to search through to find the correct template
  648. * @param string $filter Used in filtering the filename
  649. * @return mixed Path to the file, false if no file exists
  650. *
  651. **/
  652. function cfct_choose_single_template_category($dir, $files, $filter) {
  653. $cat_files = cfct_cat_templates($dir, $files);
  654. if (count($cat_files)) {
  655. foreach ($cat_files as $file) {
  656. $file = cfct_filename_filter($file, $filter);
  657. $cat_id = cfct_cat_filename_to_id($file);
  658. if (in_category($cat_id)) {
  659. return $file;
  660. }
  661. }
  662. }
  663. return false;
  664. }
  665. /**
  666. * Chooses which template to display for the single context based on user role
  667. *
  668. * @param string $dir Directory to use for selecting the template file
  669. * @param array $files A list of files to search through to find the correct template
  670. * @param string $filter Used in filtering the filename
  671. * @return mixed Path to the file, false if no file exists
  672. *
  673. **/
  674. function cfct_choose_single_template_role($dir, $files, $filter) {
  675. $role_files = cfct_role_templates($dir, $files);
  676. if (count($role_files)) {
  677. $user = new WP_User(get_the_author_meta('ID'));
  678. if (count($user->roles)) {
  679. foreach ($role_files as $file) {
  680. $file = cfct_filename_filter($file, $filter);
  681. foreach ($user->roles as $role) {
  682. if (cfct_role_filename_to_name($file) == $role) {
  683. return $file;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. return false;
  690. }
  691. /**
  692. * Chooses which template to display for the single context based on taxonomy name and slug within that taxonomy
  693. *
  694. * @param string $dir Directory to use for selecting the template file
  695. * @param array $files A list of files to search through to find the correct template
  696. * @param string $filter used in filtering the filename
  697. * @return mixed path to the file, false if no file exists
  698. *
  699. **/
  700. function cfct_choose_single_template_taxonomy($dir, $files, $filter) {
  701. global $post;
  702. $tax_files = cfct_tax_templates($dir, $files);
  703. if (count($tax_files)) {
  704. foreach ($tax_files as $file) {
  705. $file = cfct_filename_filter($file, $filter);
  706. $tax = cfct_tax_filename_to_tax_name($file);
  707. $file_slug = cfct_tax_filename_to_slug($file);
  708. $terms = wp_get_post_terms($post->ID, $tax);
  709. if (is_array($terms) && count($terms)) {
  710. foreach ($terms as $term) {
  711. if ($term->taxonomy == $tax && $term->slug == $file_slug) {
  712. return $file;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. return false;
  719. }
  720. /**
  721. * Chooses which template to display for the single context based
  722. * on post_tag
  723. *
  724. * @param string $dir Directory to use for selecting the template file
  725. * @param array $files A list of files to search through to find the correct template
  726. * @param string $filter Used in filtering the filename
  727. * @return mixed Path to the file, false if no file exists
  728. *
  729. **/
  730. function cfct_choose_single_template_tag($dir, $files, $filter) {
  731. global $post;
  732. $tag_files = cfct_tag_templates($dir, $files);
  733. if (count($tag_files)) {
  734. $tags = get_the_tags($post->ID);
  735. if (is_array($tags) && count($tags)) {
  736. foreach ($tag_files as $file) {
  737. $file = cfct_filename_filter($file, $filter);
  738. foreach ($tags as $tag) {
  739. if ($tag->slug == cfct_tag_filename_to_name($file)) {
  740. return $file;
  741. }
  742. }
  743. }
  744. }
  745. }
  746. return false;
  747. }
  748. /**
  749. * Chooses which template to display for the single context based on a post's parent
  750. *
  751. * @param string $dir Directory to use for selecting the template file
  752. * @param array $files A list of files to search through to find the correct template
  753. * @param string $filter Used in filtering the filename
  754. * @return mixed Path to the file, false if no file exists
  755. *
  756. **/
  757. function cfct_choose_single_template_parent($dir, $files, $filter) {
  758. global $post;
  759. $parent_files = cfct_parent_templates($dir, $files);
  760. if (count($parent_files) && $post->post_parent > 0) {
  761. $parent = cfct_post_id_to_slug($post->post_parent);
  762. $file = cfct_filename_filter('parent-'.$parent.'.php', $filter);
  763. if (in_array($file, $parent_files)) {
  764. return $file;
  765. }
  766. }
  767. return false;
  768. }
  769. /**
  770. * Chooses which template to display for the content context
  771. *
  772. * @param string $content Used in filtering and default if no template file can be found
  773. * @return mixed Path to the file, false if no file exists
  774. *
  775. **/
  776. function cfct_choose_content_template($type = 'content') {
  777. $files = cfct_files(CFCT_PATH.$type);
  778. $filename = cfct_choose_single_template($files);
  779. if (!$filename && cfct_context() == 'page' && file_exists(CFCT_PATH.$type.'/page.php')) {
  780. $filename = 'page.php';
  781. }
  782. if (!$filename) {
  783. $filename = cfct_default_file($type);
  784. }
  785. return apply_filters('cfct_choose_content_template', $filename, $type);
  786. }
  787. /**
  788. * Chooses which template to display for the comment context
  789. *
  790. * @return mixed Path to the file, false if no file exists
  791. *
  792. **/
  793. function cfct_choose_comment_template() {
  794. $exec_order = array(
  795. 'ping',
  796. 'author',
  797. 'user',
  798. 'meta',
  799. 'role',
  800. 'default',
  801. );
  802. $exec_order = apply_filters('cfct_comment_match_order', $exec_order);
  803. $files = cfct_files(CFCT_PATH.'comment');
  804. foreach ($exec_order as $func_name) {
  805. if (!function_exists($func_name)) {
  806. $func_name = 'cfct_choose_comment_template_'.$func_name;
  807. }
  808. if (function_exists($func_name)) {
  809. $filename = $func_name($files);
  810. if ($filename != false) {
  811. break;
  812. }
  813. }
  814. }
  815. return apply_filters('cfct_choose_comment_template', $filename);
  816. }
  817. /**
  818. * Chooses which template to display for the comment context based on whether or not the comment is a ping or trackback
  819. *
  820. * @param array $files A list of files to search through to find the correct template
  821. * @return mixed Path to the file, false if no file exists
  822. *
  823. **/
  824. function cfct_choose_comment_template_ping($files) {
  825. global $comment;
  826. if (in_array('ping.php', $files)) {
  827. switch ($comment->comment_type) {
  828. case 'pingback':
  829. case 'trackback':
  830. return 'ping';
  831. break;
  832. }
  833. }
  834. return false;
  835. }
  836. /**
  837. * Chooses which template to display for the comment context based on meta data
  838. *
  839. * @param array $files A list of files to search through to find the correct template
  840. * @return mixed Path to the file, false if no file exists
  841. *
  842. **/
  843. function cfct_choose_comment_template_meta($files) {
  844. global $comment;
  845. $meta_files = cfct_meta_templates('', $files);
  846. if (count($meta_files)) {
  847. $meta = get_metadata('comment', $comment->comment_ID);
  848. if (count($meta)) {
  849. // check key, value matches first
  850. foreach ($meta as $k => $v) {
  851. $val = $v[0];
  852. $file = 'meta-'.$k.'-'.$val.'.php';
  853. if (in_array($file, $meta_files)) {
  854. return $file;
  855. }
  856. }
  857. // check key matches only
  858. if (!$filename) {
  859. foreach ($meta as $k => $v) {
  860. $file = 'meta-'.$k.'.php';
  861. if (in_array($file, $meta_files)) {
  862. return $file;
  863. }
  864. }
  865. }
  866. }
  867. }
  868. return false;
  869. }
  870. /**
  871. * Chooses which template to display for the comment context based on the post author
  872. *
  873. * @param array $files A list of files to search through to find the correct template
  874. * @return mixed Path to the file, false if no file exists
  875. *
  876. **/
  877. function cfct_choose_comment_template_author($files) {
  878. global $post, $comment;
  879. if (!empty($comment->user_id) && $comment->user_id == $post->post_author && in_array('author.php', $files)) {
  880. return 'author';
  881. }
  882. return false;
  883. }
  884. /**
  885. * Chooses which template to display for the comment context based on the comment author
  886. *
  887. * @param array $files A list of files to search through to find the correct template
  888. * @return mixed Path to the file, false if no file exists
  889. *
  890. **/
  891. function cfct_choose_comment_template_user($files) {
  892. global $comment;
  893. $files = cfct_comment_templates('user', $files);
  894. if (count($files) && !empty($comment->user_id)) {
  895. $user = new WP_User($comment->user_id);
  896. if (!empty($user->user_login)) {
  897. $user_file = 'user-'.$user->user_login.'.php';
  898. if (in_array($user_file, $files)) {
  899. return $user_file;
  900. }
  901. }
  902. }
  903. return false;
  904. }
  905. /**
  906. * Chooses which template to display for the comment context based on comment author's role
  907. *
  908. * @param array $files A list of files to search through to find the correct template
  909. * @return mixed Path to the file, false if no file exists
  910. *
  911. **/
  912. function cfct_choose_comment_template_role($files) {
  913. global $comment;
  914. $files = cfct_comment_templates('role', $files);
  915. if (count($files) && !empty($comment->user_id)) {
  916. $user = new WP_User($comment->user_id);
  917. if (!empty($user->user_login)) {
  918. if (count($user->roles)) {
  919. foreach ($user->roles as $role) {
  920. $role_file = 'role-'.$role.'.php';
  921. if (in_array($role_file, $files)) {
  922. return $role_file;
  923. }
  924. }
  925. }
  926. }
  927. }
  928. return false;
  929. }
  930. /**
  931. * Chooses the default template to be used in the comment context
  932. *
  933. * @param array $files A list of files to search through to find the correct template
  934. * @return mixed Path to the file, false if no file exists
  935. *
  936. **/
  937. function cfct_choose_comment_template_default($files) {
  938. return cfct_default_file('comment');
  939. }
  940. /**
  941. * Filters a filename based on an inputted string
  942. *
  943. * @param string $filename Filename to filter
  944. * @param string $filter What to filter with
  945. * @return string The filtered filename
  946. *
  947. **/
  948. function cfct_filename_filter($filename, $filter) {
  949. // check for filter already appended
  950. if (substr($filename, 0, strlen($filter) - 1) == str_replace('*', '', $filter)) {
  951. return $filename;
  952. }
  953. return str_replace('*', $filename, $filter);
  954. }
  955. /**
  956. * Get a list of php files within a given path as well as files in corresponding child themes
  957. *
  958. * @param sting $path Path to the directory to search
  959. * @return array Files within the path directory
  960. *
  961. **/
  962. function cfct_files($path) {
  963. $files = apply_filters('cfct_files_'.$path, false);
  964. if ($files) {
  965. return $files;
  966. }
  967. $files = wp_cache_get('cfct_files_'.$path, 'cfct');
  968. if ($files) {
  969. return $files;
  970. }
  971. $files = array();
  972. $paths = array($path);
  973. if (STYLESHEETPATH.'/' != CFCT_PATH) {
  974. // load child theme files
  975. $paths[] = STYLESHEETPATH.'/'.str_replace(CFCT_PATH, '', $path);
  976. }
  977. foreach ($paths as $path) {
  978. if (is_dir($path) && $handle = opendir($path)) {
  979. while (false !== ($file = readdir($handle))) {
  980. $path = trailingslashit($path);
  981. if (is_file($path.$file) && strtolower(substr($file, -4, 4)) == ".php") {
  982. $files[] = $file;
  983. }
  984. }
  985. closedir($handle);
  986. }
  987. }
  988. $files = array_unique($files);
  989. wp_cache_set('cfct_files_'.$path, $files, 'cfct', 3600);
  990. return $files;
  991. }
  992. /**
  993. * Filters a list of files based on a prefix
  994. *
  995. * @param array $files A list of files to be filtered
  996. * @param string $prefix A string to search for and filter with in the filenames
  997. * @return array A list of files that contain the prefix
  998. *
  999. **/
  1000. function cfct_filter_files($files = array(), $prefix = '') {
  1001. $matches = array();
  1002. if (count($files)) {
  1003. foreach ($files as $file) {
  1004. if (strpos($file, $prefix) !== false) {
  1005. $matches[] = $file;
  1006. }
  1007. }
  1008. }
  1009. return $matches;
  1010. }
  1011. /**
  1012. * Get a list of files that match the meta template structure
  1013. *
  1014. * @param string $dir Directory to search through for files if none are given
  1015. * @param array $files A list of files to search through
  1016. * @return array List of files that match the meta template structure
  1017. *
  1018. **/
  1019. function cfct_meta_templates($dir, $files = null) {
  1020. if (is_null($files)) {
  1021. $files = cfct_files(CFCT_PATH.$dir);
  1022. }
  1023. $matches = cfct_filter_files($files, 'meta-');
  1024. return apply_filters('cfct_meta_templates', $matches);
  1025. }
  1026. /**
  1027. * Get a list of files that match the category template structure
  1028. *
  1029. * @param string $dir Directory to search through for files if none are given
  1030. * @param array $files A list of files to search through
  1031. * @return array List of files that match the category template structure
  1032. *
  1033. **/
  1034. function cfct_cat_templates($dir, $files = null) {
  1035. if (is_null($files)) {
  1036. $files = cfct_files(CFCT_PATH.$dir);
  1037. }
  1038. $matches = cfct_filter_files($files, 'cat-');
  1039. return apply_filters('cfct_cat_templates', $matches);
  1040. }
  1041. /**
  1042. * Get a list of files that match the tag template structure
  1043. *
  1044. * @param string $dir Directory to search through for files if none are given
  1045. * @param array $files A list of files to search through
  1046. * @return array List of files that match the tag template structure
  1047. *
  1048. **/
  1049. function cfct_tag_templates($dir, $files = null) {
  1050. if (is_null($files)) {
  1051. $files = cfct_files(CFCT_PATH.$dir);
  1052. }
  1053. $matches = cfct_filter_files($files, 'tag-');
  1054. return apply_filters('cfct_tag_templates', $matches);
  1055. }
  1056. /**
  1057. * Get a list of files that match the custom taxonomy template structure
  1058. *
  1059. * @param string $dir Directory to search through for files if none are given
  1060. * @param array $files A list of files to search through
  1061. * @return array List of files that match the custom taxonomy template structure
  1062. *
  1063. **/
  1064. function cfct_tax_templates($dir, $files = null) {
  1065. if (is_null($files)) {
  1066. $files = cfct_files(CFCT_PATH.$dir);
  1067. }
  1068. $matches = cfct_filter_files($files, 'tax-');
  1069. return apply_filters('cfct_tax_templates', $matches);
  1070. }
  1071. /**
  1072. * Get a list of files that match the post format structure
  1073. *
  1074. * @param string $dir Directory to search through for files if none are given
  1075. * @param array $files A list of files to search through
  1076. * @return array List of files that match the post format template structure
  1077. *
  1078. **/
  1079. function cfct_format_templates($dir, $files = null) {
  1080. if (is_null($files)) {
  1081. $files = cfct_files(CFCT_PATH.$dir);
  1082. }
  1083. $matches = cfct_filter_files($files, 'format-');
  1084. return apply_filters('cfct_format_templates', $matches);
  1085. }
  1086. /**
  1087. * Get a list of files that match the author template structure
  1088. *
  1089. * @param string $dir Directory to search through for files if none are given
  1090. * @param array $files A list of files to search through
  1091. * @return array list of files that match the author template structure
  1092. *
  1093. **/
  1094. function cfct_author_templates($dir, $files = null) {
  1095. if (is_null($files)) {
  1096. $files = cfct_files(CFCT_PATH.$dir);
  1097. }
  1098. $matches = cfct_filter_files($files, 'author-');
  1099. return apply_filters('cfct_author_templates', $matches);
  1100. }
  1101. /**
  1102. * Get a list of files that match the custom post type template structure
  1103. *
  1104. * @param string $dir Directory to search through for files if none are given
  1105. * @param array $files A list of files to search through
  1106. * @return array List of files that match the custom post type template structure
  1107. *
  1108. **/
  1109. function cfct_type_templates($dir, $files = null) {
  1110. if (is_null($files)) {
  1111. $files = cfct_files(CFCT_PATH.$dir);
  1112. }
  1113. $matches = cfct_filter_files($files, 'type-');
  1114. return apply_filters('cfct_type_templates', $matches);
  1115. }
  1116. /**
  1117. * Get a list of files that match the user role template structure
  1118. *
  1119. * @param string $dir Directory to search through for files if none are given
  1120. * @param array $files A list of files to search through
  1121. * @return array List of files that match the user role template structure
  1122. *
  1123. **/
  1124. function cfct_role_templates($dir, $files = null) {
  1125. if (is_null($files)) {
  1126. $files = cfct_files(CFCT_PATH.$dir);
  1127. }
  1128. $matches = cfct_filter_files($files, 'role-');
  1129. return apply_filters('cfct_role_templates', $matches);
  1130. }
  1131. /**
  1132. * Get a list of files that match the post parent template structure
  1133. *
  1134. * @param string $dir Directory to search through for files if none are given
  1135. * @param array $files A list of files to search through
  1136. * @return array List of files that match the post parent template structure
  1137. *
  1138. **/
  1139. function cfct_parent_templates($dir, $files = null) {
  1140. if (is_null($files)) {
  1141. $files = cfct_files(CFCT_PATH.$dir);
  1142. }
  1143. $matches = cfct_filter_files($files, 'parent-');
  1144. return apply_filters('cfct_parent_templates', $matches);
  1145. }
  1146. /**
  1147. * Get a list of files that match the single template structure
  1148. *
  1149. * @param string $dir Directory to search through for files if none are given
  1150. * @param array $files A list of files to search through
  1151. * @return array List of files that match the single template structure
  1152. *
  1153. **/
  1154. function cfct_single_templates($dir, $files = null) {
  1155. if (is_null($files)) {
  1156. $files = cfct_files(CFCT_PATH.$dir);
  1157. }
  1158. $matches = cfct_filter_files($files, 'single');
  1159. return apply_filters('cfct_single_templates', $matches);
  1160. }
  1161. /**
  1162. * Get a list of files that match the comment template structure for a given type
  1163. *
  1164. * @param string $type The type of template to search for
  1165. * @param array $files A list of files to search through
  1166. * @return array List of files that match the comment template structure for a given type
  1167. *
  1168. **/
  1169. function cfct_comment_templates($type, $files = false) {
  1170. if (!$files) {
  1171. $files = cfct_files(CFCT_PATH.'comment');
  1172. }
  1173. $matches = array();
  1174. switch ($type) {
  1175. case 'user':
  1176. $matches = cfct_filter_files($files, 'user-');
  1177. break;
  1178. case 'role':
  1179. $matches = cfct_filter_files($files, 'role-');
  1180. break;
  1181. }
  1182. return apply_filters('cfct_comment_templates', $matches);
  1183. }
  1184. /**
  1185. * Get the id of a category from the category slug of a filename
  1186. *
  1187. * @param string $file Filename
  1188. * @return int Category id matching the category slug of the filename
  1189. *
  1190. **/
  1191. function cfct_cat_filename_to_id($file) {
  1192. $cat = cfct_cat_filename_to_slug($file);
  1193. $cat = get_category_by_slug($cat);
  1194. return $cat->cat_ID;
  1195. }
  1196. /**
  1197. * Get the name of a category from the category slug of a filename
  1198. *
  1199. * @param string $file Filename
  1200. * @return string Category name matching the category slug of the filename
  1201. *
  1202. **/
  1203. function cfct_cat_filename_to_name($file) {
  1204. $cat = cfct_cat_filename_to_slug($file);
  1205. $cat = get_category_by_slug($cat);
  1206. return $cat->name;
  1207. }
  1208. /**
  1209. * Get the slug of a category from a filename
  1210. *
  1211. * @param string $file Filename
  1212. * @return string Category slug
  1213. *
  1214. **/
  1215. function cfct_cat_filename_to_slug($file) {
  1216. return str_replace(array('single-cat-', 'cat-', '.php'), '', $file);
  1217. }
  1218. /**
  1219. * Get the slug of a category from its id
  1220. *
  1221. * @param int $id Category id
  1222. * @return string Category slug
  1223. *
  1224. **/
  1225. function cfct_cat_id_to_slug($id) {
  1226. $cat = &get_category($id);
  1227. return $cat->slug;
  1228. }
  1229. /**
  1230. * Get the id of a user from their username
  1231. *
  1232. * @param string $username A user's username
  1233. * @return int The id of the user
  1234. *
  1235. **/
  1236. function cfct_username_to_id($username) {
  1237. return get_profile('ID', $username);
  1238. }
  1239. /**
  1240. * Get the slug of a tag from a filename
  1241. *
  1242. * @param string $file Filename
  1243. * @return string Tag slug
  1244. *
  1245. **/
  1246. function cfct_tag_filename_to_name($file) {
  1247. return str_replace(array('single-tag-', 'tag-', '.php'), '', $file);
  1248. }
  1249. /**
  1250. * Get the author from a filename
  1251. *
  1252. * @param string $file Filename
  1253. * @return string Author
  1254. *
  1255. **/
  1256. function cfct_author_filename_to_name($file) {
  1257. return str_replace(array('single-author-', 'author-', '.php'), '', $file);
  1258. }
  1259. /**
  1260. * Get the role from a filename
  1261. *
  1262. * @param string $file Filename
  1263. * @return string Role
  1264. *
  1265. **/
  1266. function cfct_role_filename_to_name($file) {
  1267. return str_replace(array('single-role-', 'role-', '.php'), '', $file);
  1268. }
  1269. /**
  1270. * Get the post format from a filename
  1271. *
  1272. * @param string $file Filename
  1273. * @return string Post format
  1274. *
  1275. **/
  1276. function cfct_format_filename_to_format($file) {
  1277. return str_replace(array('single-format-', 'format-', '.php'), '', $file);
  1278. }
  1279. /**
  1280. * Get the taxonomy name from a filename
  1281. *
  1282. * @param string $file Filename
  1283. * @return string Taxonomy name
  1284. *
  1285. **/
  1286. function cfct_tax_filename_to_tax_name($file) {
  1287. $tax = str_replace(array('single-tax-', 'tax-', '.php'), '', $file);
  1288. $tax = explode('-', $tax);
  1289. return $tax[0];
  1290. }
  1291. /**
  1292. * Get the slug of a taxonomy from a filename
  1293. *
  1294. * @param string $file Filename
  1295. * @return string Taxonomy slug
  1296. *
  1297. **/
  1298. function cfct_tax_filename_to_slug($file) {
  1299. $slug = str_replace(array('single-tax-', 'tax-', '.php'), '', $file);
  1300. $slug = explode('-', $slug);
  1301. if (!empty($slug[1])) {
  1302. return $slug[1];
  1303. }
  1304. return '';
  1305. }
  1306. /**
  1307. * Get the post name from its id
  1308. *
  1309. * @param int $id A post id
  1310. * @return string Post name
  1311. *
  1312. **/
  1313. function cfct_post_id_to_slug($id) {
  1314. $post = get_post($id);
  1315. return $post->post_name;
  1316. }
  1317. /**
  1318. * Custom formatting for strings
  1319. *
  1320. * @param string $str A string to be formatted
  1321. * @return string Formatted string
  1322. *
  1323. **/
  1324. function cfct_basic_content_formatting($str) {
  1325. $str = wptexturize($str);
  1326. $str = convert_smilies($str);
  1327. $str = convert_chars($str);
  1328. $str = wpautop($str);
  1329. return $str;
  1330. }
  1331. /**
  1332. * Get an array with the path to the director of the file as well as the filename
  1333. *
  1334. * @param string $path A path to a file
  1335. * @return array Contains the directory the file is in as well as the filename
  1336. *
  1337. **/
  1338. function cfct_leading_dir($path) {
  1339. $val = array(
  1340. 'dir' => '',
  1341. 'file' => ''
  1342. );
  1343. if (strpos($path, '/') !== false) {
  1344. $parts = explode('/', $path);
  1345. $val['file'] = $parts[count($parts) - 1];
  1346. $val['dir'] = implode('/', array_slice($parts, 0, count($parts) - 1));
  1347. }
  1348. else {
  1349. $val['file'] = $path;
  1350. }
  1351. return $val;
  1352. }
  1353. /**
  1354. * Prevent code from breaking in WP versions < 3.1
  1355. *
  1356. *
  1357. **/
  1358. if (!function_exists('get_post_format')) {
  1359. function get_post_format($post_id) {
  1360. return false;
  1361. }
  1362. }
  1363. ?>