PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/utility.php

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