PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/code/cake/app/webroot/research/wp-content/themes/annotum-base/carrington-core/utility.php

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