PageRenderTime 73ms CodeModel.GetById 13ms app.highlight 53ms RepoModel.GetById 2ms app.codeStats 0ms

/mobile/tags/1.2/carrington-core/utility.php

http://carrington.googlecode.com/
PHP | 821 lines | 721 code | 58 blank | 42 comment | 161 complexity | 6585b60c054571e4dd7e98dcb6e63101 MD5 | raw file
  1<?php
  2
  3// This file is part of the Carrington Theme Framework for WordPress
  4// http://carringtontheme.com
  5//
  6// Copyright (c) 2008-2010 Crowd Favorite, Ltd. All rights reserved.
  7// http://crowdfavorite.com
  8//
  9// Released under the GPL license
 10// http://www.opensource.org/licenses/gpl-license.php
 11//
 12// **********************************************************************
 13// This program is distributed in the hope that it will be useful, but
 14// WITHOUT ANY WARRANTY; without even the implied warranty of
 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 16// **********************************************************************
 17
 18if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { die(); }
 19
 20function cfct_die($str = '') {
 21	if (!empty($str)) {
 22		if (file_exists(CFCT_PATH.'error/exit.php')) {
 23			include(CFCT_PATH.'error/exit.php');
 24			die();
 25		}
 26		else {
 27			wp_die($str);
 28		}
 29	}
 30}
 31
 32function cfct_banner($str = '') {
 33	if (!empty($str)) {
 34		if (file_exists(CFCT_PATH.'misc/banner.php')) {
 35			include(CFCT_PATH.'misc/banner.php');
 36		}
 37		else {
 38			echo '<p>'.$str.'</p>';
 39		}
 40	}
 41}
 42
 43function cfct_get_option($name) {
 44	$defaults = array(
 45		'cfct_credit' => 'yes',
 46		'cfct_lightbox' => 'yes',
 47		'cfct_header_image' => 0,
 48	);
 49	$defaults = apply_filters('cfct_option_defaults', $defaults);
 50	$value = get_option($name);
 51	if ($value == '' && isset($defaults[$name])) {
 52		$value = $defaults[$name];
 53	}
 54	return $value;
 55}
 56
 57function cfct_load_plugins() {
 58	$files = cfct_files(CFCT_PATH.'plugins');
 59	if (count($files)) {
 60		foreach ($files as $file) {
 61			if (file_exists(CFCT_PATH.'plugins/'.$file)) {
 62				include_once(CFCT_PATH.'plugins/'.$file);
 63			}
 64// child theme support
 65			if (file_exists(STYLESHEETPATH.'/plugins/'.$file)) {
 66				include_once(STYLESHEETPATH.'/plugins/'.$file);
 67			}
 68		}
 69	}
 70}
 71
 72function cfct_default_file($dir) {
 73	$fancy = $dir.'-default.php';
 74	file_exists(CFCT_PATH.$dir.'/'.$fancy) ? $default = $fancy : $default = 'default.php';
 75	return $default;
 76}
 77
 78function cfct_context() {
 79	$context = 'home';
 80	if (is_home()) {
 81		$context = 'home';
 82	}
 83	else if (is_page()) {
 84		$context = 'page';
 85	}
 86	else if (is_single()) {
 87		$context = 'single';
 88	}
 89	else if (is_category()) {
 90		$context = 'category';
 91	}
 92	else if (is_tag()) {
 93		$context = 'tag';
 94	}
 95	else if (is_author()) {
 96		$context = 'author';
 97	}
 98	else if (is_archive()) {
 99// possible future abstraction for:
100// 	is_month()
101// 	is_year()
102// 	is_day()
103		$context = 'archive';
104	}
105	else if (is_search()) {
106		$context = 'search';
107	}
108	else if (is_404()) {
109		$context = '404';
110	}
111	return apply_filters('cfct_context', $context);
112}
113
114/**
115 * @param $template = folder name of file
116 * @param $type = file name of file
117 * @param $keys = keys that could be used for additional filename params
118 * returns false if file does not exist
119 *
120 */
121function cfct_filename($dir, $type = 'default', $keys = array()) {
122	switch ($type) {
123		case 'author':
124			if (count($keys)) {
125				$file = 'author-'.$keys[0];
126			}
127			else {
128				$file = 'author';
129			}
130			break;
131		case 'category':
132			if (count($keys)) {
133				$file = 'cat-'.$keys[0];
134			}
135			else {
136				$file = 'category';
137			}
138			break;
139		case 'tag':
140			if (count($keys)) {
141				$file = 'tag-'.$keys[0];
142			}
143			else {
144				$file = 'tag';
145			}
146			break;
147		case 'meta':
148			if (count($keys)) {
149				foreach ($keys as $k => $v) {
150					if (!empty($v)) {
151						$file = 'meta-'.$k.'-'.$v;
152					}
153					else {
154						$file = 'meta-'.$k;
155					}
156					break;
157				}
158			}
159			break;
160		case 'user':
161			if (count($keys)) {
162				$file = 'user-'.$keys[0];
163			}
164			break;
165		case 'role':
166			if (count($keys)) {
167				$file = 'role-'.$keys[0];
168			}
169			break;
170		case 'parent':
171			if (count($keys)) {
172				$file = 'parent-'.$keys[0];
173			}
174			break;
175		default:
176		// handles page, etc.
177			$file = $type;
178	}
179	// fallback for category, author, tag, etc.
180	// child theme path
181	$path = STYLESHEETPATH.'/'.$dir.'/'.$file.'.php';
182	// check for child theme first
183	if (!file_exists($path)) {
184		// use parent theme if no child theme file found
185		$path = CFCT_PATH.$dir.'/'.$file.'.php';
186	}
187	if (!file_exists($path)) {
188		switch ($type) {
189			case 'author':
190			case 'category':
191			case 'tag':
192				// child theme path
193				$path = STYLESHEETPATH.'/'.$dir.'/archive.php';
194				if (!file_exists($path)) {
195					// use parent theme if no child theme file found
196					$path = CFCT_PATH.$dir.'/archive.php';
197				}
198		}
199	}
200	$default = CFCT_PATH.$dir.'/'.cfct_default_file($dir);
201	if (file_exists($path)) {
202		$path = $path;
203	}
204	else if (file_exists($default)) {
205		$path = $default;
206	}
207	else {
208		$path = false;
209	}
210	return apply_filters('cfct_filename', $path);
211}
212
213function cfct_template($dir, $keys = array()) {
214	$context = cfct_context();
215	$file = cfct_filename($dir, $context, $keys);
216	if ($file) {
217		include($file);
218	}
219	else {
220		cfct_die('Error loading '.$dir.' '.__LINE__);
221	}
222}
223
224function cfct_template_file($dir, $file, $data = null) {
225	$path = '';
226	if (!empty($file)) {
227		$file = basename($file, '.php');
228		// child theme support
229		$path = STYLESHEETPATH.'/'.$dir.'/'.$file.'.php';
230		if (!file_exists($path)) {
231			$path = CFCT_PATH.$dir.'/'.$file.'.php';
232		}
233	}
234	if (file_exists($path)) {
235		include($path);
236	}
237	else {
238		cfct_die('Error loading '.$file.' '.__LINE__);
239	}
240}
241
242function cfct_choose_general_template($dir) {
243	$exec_order = array(
244		'author',
245		'role',
246		'category',
247		'tag',
248		'single',
249		'default'
250	);
251	$exec_order = apply_filters('cfct_general_match_order', $exec_order);
252	$files = cfct_files(CFCT_PATH.$dir);
253	foreach ($exec_order as $func_name) {
254		if (!function_exists($func_name)) {
255			$func_name = 'cfct_choose_general_template_'.$func_name;
256		}
257		if (function_exists($func_name)) {
258			$filename = $func_name($dir, $files);
259			if ($filename != false) {
260				break;
261			}
262		}
263	}
264	return apply_filters('cfct_choose_general_template', $filename, $dir);
265}
266
267function cfct_choose_general_template_author($dir, $files) {
268	$files = cfct_author_templates($dir, $files);
269	if (count($files)) {
270		$username = get_query_var('author_name');
271		if (empty($username)) {
272			$user = new WP_User(get_query_var('author'));
273			$username = $user->user_login;
274		}
275		$filename = 'author-'.$username.'.php';
276		if (in_array($filename, $files)) {
277			$keys = array($username);
278			return cfct_filename($dir, 'author', $keys);
279		}
280 	}
281	return false;
282}
283
284function cfct_choose_general_template_category($dir, $files) {
285	$files = cfct_cat_templates($dir, $files);
286	if (count($files)) {
287		global $cat;
288		$slug = cfct_cat_id_to_slug($cat);
289		if (in_array('cat-'.$slug.'.php', $files)) {
290			$keys = array($slug);
291			return cfct_filename($dir, 'category', $keys);
292		}
293 	}
294	return false;
295}
296
297function cfct_choose_general_template_tag($dir, $files) {
298	$files = cfct_tag_templates($dir, $files);
299	if (count($files)) {
300		$tag = get_query_var('tag');
301		if (in_array('tag-'.$tag.'.php', $files)) {
302			$keys = array($tag);
303			return cfct_filename($dir, 'tag', $keys);
304		}
305 	}
306	return false;
307}
308
309function cfct_choose_general_template_role($dir, $files) {
310	$files = cfct_role_templates($dir, $files);
311	if (count($files)) {
312		$username = get_query_var('author_name');
313		$user = new WP_User(cfct_username_to_id($username));
314		if (!empty($user->user_login)) {
315			if (count($user->roles)) {
316				foreach ($user->roles as $role) {
317					$role_file = 'role-'.$role.'.php';
318					if (in_array($role_file, $files)) {
319						return $role_file;
320					}
321				}
322			}
323		}
324 	}
325	return false;
326}
327
328function cfct_choose_general_template_single($dir, $files) {
329	if (cfct_context() == 'single') {
330		$files = cfct_single_templates($dir, $files);
331		if (count($files)) {
332// check to see if we're in the loop.
333			global $post;
334			$orig_post = $post;
335			while (have_posts()) {
336				the_post();
337				$filename = cfct_choose_single_template($files, 'single-*');
338				if (!$filename) {
339					if (file_exists(CFCT_PATH.$dir.'/single.php')) {
340						$filename = 'single.php';
341					}
342				}
343			}
344			rewind_posts();
345			$post = $orig_post;
346			return $filename;
347		}
348	}
349	return false;
350}
351
352function cfct_choose_general_template_default($dir, $files) {
353	$context = cfct_context();
354	return cfct_filename($dir, $context);
355}
356
357function cfct_choose_single_template($files = array(), $filter = '*') {
358// must be called within the_loop - cfct_choose_general_template_single() approximates a loop for this reason.
359	$exec_order = array(
360		'author',
361		'meta',
362		'category',
363		'type',
364		'role',
365		'tag',
366		'parent', // for pages
367	);
368	$exec_order = apply_filters('cfct_single_match_order', $exec_order);
369	$filename = false;
370	foreach ($exec_order as $func_name) {
371		if (!function_exists($func_name)) {
372			$func_name = 'cfct_choose_single_template_'.$func_name;
373		}
374		if (function_exists($func_name)) {
375			$filename = $func_name($dir, $files, $filter);
376			if ($filename != false) {
377				break;
378			}
379		}
380	}
381	return apply_filters('cfct_choose_single_template', $filename);
382}
383
384function cfct_choose_single_template_type($dir, $files, $filter) {
385	$type_files = cfct_type_templates('', $files);
386	if (count($type_files)) {
387		global $post;
388		$file = cfct_filename_filter('type-'.$post->post_type.'.php', $filter);
389		if (in_array($file, $type_files)) {
390			return $file;
391		}
392	}
393	return false;
394}
395
396function cfct_choose_single_template_author($dir, $files, $filter) {
397	$author_files = cfct_author_templates('', $files);
398	if (count($author_files)) {
399		$author = get_the_author_login();
400		$file = cfct_filename_filter('author-'.$author.'.php', $filter);
401		if (in_array($file, $author_files)) {
402			return $file;
403		}
404	}
405	return false;
406}
407
408function cfct_choose_single_template_meta($dir, $files, $filter) {
409	global $post;
410	$meta_files = cfct_meta_templates('', $files);
411	if (count($meta_files)) {
412		$meta = get_post_custom($post->ID);
413		if (count($meta)) {
414// check key, value matches first
415			foreach ($meta as $k => $v) {
416				$val = $v[0];
417				$file = cfct_filename_filter('meta-'.$k.'-'.$val.'.php', $filter);
418				if (in_array($file, $meta_files)) {
419					return $file;
420				}
421			}
422// check key matches only
423			if (!$filename) {
424				foreach ($meta as $k => $v) {
425					$file = cfct_filename_filter('meta-'.$k.'.php', $filter);
426					if (in_array($file, $meta_files)) {
427						return $file;
428					}
429				}
430			}
431		}
432	}
433	return false;
434}
435
436function cfct_choose_single_template_category($dir, $files, $filter) {
437	$cat_files = cfct_cat_templates($type, $files);
438	if (count($cat_files)) {
439		foreach ($cat_files as $file) {
440			$file = cfct_filename_filter($file, $filter);
441			$cat_id = cfct_cat_filename_to_id($file);
442			if (in_category($cat_id)) {
443				return $file;
444			}
445		}
446	}
447	return false;
448}
449
450function cfct_choose_single_template_role($dir, $files, $filter) {
451	$role_files = cfct_role_templates($type, $files);
452	if (count($role_files)) {
453		$user = new WP_User(get_the_author_meta('ID'));
454		if (count($user->roles)) {
455			foreach ($role_files as $file) {
456				$file = cfct_filename_filter($file, $filter);
457				foreach ($user->roles as $role) {
458					if (cfct_role_filename_to_name($file) == $role) {
459						return $file;
460					}
461				}
462			}
463		}
464	}
465	return false;
466}
467
468function cfct_choose_single_template_tag($dir, $files, $filter) {
469	global $post;
470	$tag_files = cfct_tag_templates($type, $files);
471	if (count($tag_files)) {
472		$tags = get_the_tags($post->ID);
473		if (is_array($tags) && count($tags)) {
474			foreach ($tag_files as $file) {
475				$file = cfct_filename_filter($file, $filter);
476				foreach ($tags as $tag) {
477					if ($tag->slug == cfct_tag_filename_to_name($file)) {
478						return $file;
479					}
480				}
481			}
482		}
483	}
484	return false;
485}
486
487function cfct_choose_single_template_parent($dir, $files, $filter) {
488	global $post;
489	$parent_files = cfct_parent_templates($type, $files);
490	if (count($parent_files) && $post->post_parent > 0) {
491		$parent = cfct_post_id_to_slug($post->post_parent);
492		$file = cfct_filename_filter('parent-'.$parent.'.php', $filter);
493		if (in_array($file, $parent_files)) {
494			return $file;
495		}
496	}
497	return false;
498}
499
500function cfct_choose_content_template($type = 'content') {
501	$files = cfct_files(CFCT_PATH.$type);
502	$filename = cfct_choose_single_template($files);
503	if (!$filename && cfct_context() == 'page' && file_exists(CFCT_PATH.$type.'/page.php')) {
504		$filename = 'page.php';
505	}
506	if (!$filename) {
507		$filename = cfct_default_file($type);
508	}
509	return apply_filters('cfct_choose_content_template', $filename, $type);
510}
511
512function cfct_choose_comment_template() {
513	$exec_order = array(
514		'ping',
515		'author',
516		'user',
517		'meta',
518		'role',
519		'default',
520	);
521	$exec_order = apply_filters('cfct_comment_match_order', $exec_order);
522	$files = cfct_files(CFCT_PATH.'comment');
523	foreach ($exec_order as $func_name) {
524		if (!function_exists($func_name)) {
525			$func_name = 'cfct_choose_comment_template_'.$func_name;
526		}
527		if (function_exists($func_name)) {
528			$filename = $func_name($files);
529			if ($filename != false) {
530				break;
531			}
532		}
533	}
534	return apply_filters('cfct_choose_comment_template', $filename);
535}
536
537function cfct_choose_comment_template_ping($files) {
538	global $comment;
539	if (in_array('ping.php', $files)) {
540		switch ($comment->comment_type) {
541			case 'pingback':
542			case 'trackback':
543				return 'ping';
544				break;
545		}
546	}
547	return false;
548}
549
550function cfct_choose_comment_template_meta($files) {
551	global $comment;
552	$meta_files = cfct_meta_templates('', $files);
553	if (count($meta_files)) {
554		$meta = get_metadata('comment', $comment->comment_ID);
555		if (count($meta)) {
556// check key, value matches first
557			foreach ($meta as $k => $v) {
558				$val = $v[0];
559				$file = 'meta-'.$k.'-'.$val.'.php';
560				if (in_array($file, $meta_files)) {
561					return $file;
562				}
563			}
564// check key matches only
565			if (!$filename) {
566				foreach ($meta as $k => $v) {
567					$file = 'meta-'.$k.'.php';
568					if (in_array($file, $meta_files)) {
569						return $file;
570					}
571				}
572			}
573		}
574	}
575	return false;
576}
577
578function cfct_choose_comment_template_author($files) {
579	global $post, $comment;
580	if (!empty($comment->user_id) && $comment->user_id == $post->post_author && in_array('author.php', $files)) {
581		return 'author';
582 	}
583	return false;
584}
585
586function cfct_choose_comment_template_user($files) {
587	global $comment;
588	$files = cfct_comment_templates('user', $files);
589	if (count($files) && !empty($comment->user_id)) {
590		$user = new WP_User($comment->user_id);
591		if (!empty($user->user_login)) {
592			$user_file = 'user-'.$user->user_login.'.php';
593			if (in_array($user_file, $files)) {
594				return $user_file;
595			}
596		}
597 	}
598	return false;
599}
600
601function cfct_choose_comment_template_role($files) {
602	global $comment;
603	$files = cfct_comment_templates('role', $files);
604	if (count($files) && !empty($comment->user_id)) {
605		$user = new WP_User($comment->user_id);
606		if (!empty($user->user_login)) {
607			if (count($user->roles)) {
608				foreach ($user->roles as $role) {
609					$role_file = 'role-'.$role.'.php';
610					if (in_array($role_file, $files)) {
611						return $role_file;
612					}
613				}
614			}
615		}
616 	}
617	return false;
618}
619
620function cfct_choose_comment_template_default($files) {
621	return cfct_default_file('comment');
622}
623
624function cfct_filename_filter($filename, $filter) {
625	// check for filter already appended
626	if (substr($filename, 0, strlen($filter) - 1) == str_replace('*', '', $filter)) {
627		return $filename;
628	}
629	return str_replace('*', $filename, $filter);
630}
631
632function cfct_files($path) {
633	$files = apply_filters('cfct_files_'.$path, false);
634	if ($files) {
635		return $files;
636	}
637	$files = wp_cache_get('cfct_files_'.$path, 'cfct');
638	if ($files) {
639		return $files;
640	}
641	$files = array();
642	$paths = array($path);
643	if (STYLESHEETPATH.'/' != CFCT_PATH) {
644		// load child theme files
645		$paths[] = STYLESHEETPATH.'/'.str_replace(CFCT_PATH, '', $path);
646	}
647	foreach ($paths as $path) {
648		if (is_dir($path) && $handle = opendir($path)) {
649			while (false !== ($file = readdir($handle))) {
650				$path = trailingslashit($path);
651				if (is_file($path.$file) && strtolower(substr($file, -4, 4)) == ".php") {
652					$files[] = $file;
653				}
654			}
655			closedir($handle);
656		}
657	}
658	$files = array_unique($files);
659	wp_cache_set('cfct_files_'.$path, $files, 'cfct', 3600);
660	return $files;
661}
662
663function cfct_filter_files($files = array(), $prefix = '') {
664	$matches = array();
665	if (count($files)) {
666		foreach ($files as $file) {
667			if (strpos($file, $prefix) !== false) {
668				$matches[] = $file;
669			}
670		}
671	}
672	return $matches;
673}
674
675function cfct_meta_templates($dir, $files = null) {
676	if (is_null($files)) {
677		$files = cfct_files(CFCT_PATH.$dir);
678	}
679	$matches = cfct_filter_files($files, 'meta-');
680	return apply_filters('cfct_meta_templates', $matches);
681}
682
683function cfct_cat_templates($dir, $files = null) {
684	if (is_null($files)) {
685		$files = cfct_files(CFCT_PATH.$dir);
686	}
687	$matches = cfct_filter_files($files, 'cat-');
688	return apply_filters('cfct_cat_templates', $matches);
689}
690
691function cfct_tag_templates($dir, $files = null) {
692	if (is_null($files)) {
693		$files = cfct_files(CFCT_PATH.$dir);
694	}
695	$matches = cfct_filter_files($files, 'tag-');
696	return apply_filters('cfct_tag_templates', $matches);
697}
698
699function cfct_author_templates($dir, $files = null) {
700	if (is_null($files)) {
701		$files = cfct_files(CFCT_PATH.$dir);
702	}
703	$matches = cfct_filter_files($files, 'author-');
704	return apply_filters('cfct_author_templates', $matches);
705}
706
707function cfct_type_templates($dir, $files = null) {
708	if (is_null($files)) {
709		$files = cfct_files(CFCT_PATH.$dir);
710	}
711	$matches = cfct_filter_files($files, 'type-');
712	return apply_filters('cfct_type_templates', $matches);
713}
714
715function cfct_role_templates($dir, $files = null) {
716	if (is_null($files)) {
717		$files = cfct_files(CFCT_PATH.$dir);
718	}
719	$matches = cfct_filter_files($files, 'role-');
720	return apply_filters('cfct_role_templates', $matches);
721}
722
723function cfct_parent_templates($dir, $files = null) {
724	if (is_null($files)) {
725		$files = cfct_files(CFCT_PATH.$dir);
726	}
727	$matches = cfct_filter_files($files, 'parent-');
728	return apply_filters('cfct_parent_templates', $matches);
729}
730
731function cfct_single_templates($dir, $files = null) {
732	if (is_null($files)) {
733		$files = cfct_files(CFCT_PATH.$dir);
734	}
735	$matches = cfct_filter_files($files, 'single');
736	return apply_filters('cfct_single_templates', $matches);
737}
738
739function cfct_comment_templates($type, $files = false) {
740	if (!$files) {
741		$files = cfct_files(CFCT_PATH.'comment');
742	}
743	$matches = array();
744	switch ($type) {
745		case 'user':
746			$matches = cfct_filter_files($files, 'user-');
747			break;
748		case 'role':
749			$matches = cfct_filter_files($files, 'role-');
750			break;
751	}
752	return apply_filters('cfct_comment_templates', $matches);
753}
754
755function cfct_cat_filename_to_id($file) {
756	$cat = cfct_cat_filename_to_slug($file);
757	$cat = get_category_by_slug($cat);
758	return $cat->cat_ID;
759}
760
761function cfct_cat_filename_to_name($file) {
762	$cat = cfct_cat_filename_to_slug($file);
763	$cat = get_category_by_slug($cat);
764	return $cat->name;
765}
766
767function cfct_cat_filename_to_slug($file) {
768	return str_replace(array('single-cat-', 'cat-', '.php'), '', $file);
769}
770
771function cfct_cat_id_to_slug($id) {
772	$cat = &get_category($id);
773	return $cat->slug;
774}
775
776function cfct_username_to_id($username) {
777	return get_profile('ID', $username);
778}
779
780function cfct_tag_filename_to_name($file) {
781	return str_replace(array('single-tag-', 'tag-', '.php'), '', $file);
782}
783
784function cfct_author_filename_to_name($file) {
785	return str_replace(array('single-author-', 'author-', '.php'), '', $file);
786}
787
788function cfct_role_filename_to_name($file) {
789	return str_replace(array('single-role-', 'role-', '.php'), '', $file);
790}
791
792function cfct_post_id_to_slug($id) {
793	$post = get_post($id);
794	return $post->post_name;
795}
796
797function cfct_basic_content_formatting($str) {
798	$str = wptexturize($str);
799	$str = convert_smilies($str);
800	$str = convert_chars($str);
801	$str = wpautop($str);
802	return $str;
803}
804
805function cfct_leading_dir($path) {
806	$val = array(
807		'dir' => '',
808		'file' => ''
809	);
810	if (strpos($path, '/') !== false) {
811		$parts = explode('/', $path);
812		$val['file'] = $parts[count($parts) - 1];
813		$val['dir'] = implode('/', array_slice($parts, 0, count($parts) - 1));
814	}
815	else {
816		$val['file'] = $path;
817	}
818	return $val;
819}
820
821?>