PageRenderTime 74ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/mobile/trunk/carrington-core/utility.php

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