PageRenderTime 25ms CodeModel.GetById 1ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/experimental/text/functions/utility.php

http://carrington.googlecode.com/
PHP | 673 lines | 587 code | 55 blank | 31 comment | 125 complexity | 8fd906dd16cfc070d9a6f00aef5b1019 MD5 | raw file
  1<?php
  2
  3// This file is part of the Carrington Theme for WordPress
  4// http://carringtontheme.com
  5//
  6// Copyright (c) 2008 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		include(CFCT_PATH.'error/exit.php');
 23		die();
 24	}
 25}
 26
 27function cfct_banner($str = '') {
 28	if (!empty($str)) {
 29		include(CFCT_PATH.'misc/banner.php');
 30	}
 31}
 32
 33function cfct_get_option($name) {
 34	$defaults = array(
 35		'cfct_credit' => 'yes'
 36	);
 37	$defaults = apply_filters('cfct_option_defaults', $defaults);
 38	$value = get_option($name);
 39	if ($value == '' && isset($defaults[$name])) {
 40		$value = $defaults[$name];
 41	}
 42	return $value;
 43}
 44
 45function cfct_load_plugins() {
 46	$files = cfct_files(CFCT_PATH.'plugins');
 47	foreach ($files as $file) {
 48		include(CFCT_PATH.'plugins/'.$file);
 49	}
 50}
 51
 52function cfct_default_file($dir) {
 53	$fancy = $dir.'-default.php';
 54	file_exists(CFCT_PATH.$dir.'/'.$fancy) ? $default = $fancy : $default = 'default.php';
 55	return $default;
 56}
 57
 58function cfct_context() {
 59	$context = 'home';
 60	if (is_page()) {
 61		$context = 'page';
 62	}
 63	else if (is_single()) {
 64		$context = 'single';
 65	}
 66	else if (is_category()) {
 67		$context = 'category';
 68	}
 69	else if (is_tag()) {
 70		$context = 'tag';
 71	}
 72	else if (is_author()) {
 73		$context = 'author';
 74	}
 75	else if (is_archive()) {
 76// possible future abstraction for:
 77// 	is_month()
 78// 	is_year()
 79// 	is_day()
 80		$context = 'archive';
 81	}
 82	else if (is_search()) {
 83		$context = 'search';
 84	}
 85	else if (is_home()) {
 86		$context = 'home';
 87		// TODO - check page #
 88	}
 89	else if (is_404()) {
 90		$context = '404';
 91	}
 92	return apply_filters('cfct_context', $context);
 93}
 94
 95/**
 96 * @param $template = folder name of file
 97 * @param $type = file name of file
 98 * @param $keys = keys that could be used for additional filename params
 99 * returns false if file does not exist
100 *
101 */
102function cfct_filename($dir, $type = 'default', $keys = array()) {
103	switch ($type) {
104		case 'author':
105			if (count($keys)) {
106				$file = 'author-'.$keys[0];
107			}
108			else {
109				$file = 'author';
110			}
111			break;
112		case 'category':
113			if (count($keys)) {
114				$file = 'cat-'.$keys[0];
115			}
116			else {
117				$file = 'category';
118			}
119			break;
120		case 'tag':
121			if (count($keys)) {
122				$file = 'tag-'.$keys[0];
123			}
124			else {
125				$file = 'tag';
126			}
127			break;
128		case 'meta':
129			if (count($keys)) {
130				foreach ($keys as $k => $v) {
131					if (!empty($v)) {
132						$file = 'meta-'.$k.'-'.$v;
133					}
134					else {
135						$file = 'meta-'.$k;
136					}
137					break;
138				}
139			}
140			break;
141		case 'user':
142			if (count($keys)) {
143				$file = 'user-'.$keys[0];
144			}
145			break;
146		case 'role':
147			if (count($keys)) {
148				$file = 'role-'.$keys[0];
149			}
150			break;
151		case 'parent':
152			if (count($keys)) {
153				$file = 'parent-'.$keys[0];
154			}
155			break;
156		default:
157		// handles single, etc.
158			$file = $type;
159	}
160	// fallback for category, author, tag, etc.
161	$path = CFCT_PATH.$dir.'/'.$file.'.php';
162	if (!file_exists($path)) {
163		switch ($type) {
164			case 'author':
165			case 'category':
166			case 'tag':
167				$archive_file = CFCT_PATH.$dir.'/archive.php';
168				if (file_exists($archive_file)) {
169					$path = $archive_file;
170				}
171		}
172	}
173	$default = CFCT_PATH.$dir.'/'.cfct_default_file($dir);
174	if (file_exists($path)) {
175		$path = $path;
176	}
177	else if (file_exists($default)) {
178		$path = $default;
179	}
180	else {
181		$path = false;
182	}
183	return apply_filters('cfct_filename', $path);
184}
185
186function cfct_template($dir, $keys = array()) {
187	$context = cfct_context();
188	$file = cfct_filename($dir, $context, $keys);
189	if ($file) {
190		include($file);
191	}
192	else {
193		cfct_die('Error loading '.$dir.' '.__LINE__);
194	}
195}
196
197function cfct_template_file($dir, $file) {
198	$path = '';
199	if (!empty($file)) {
200		$file = basename($file, '.php');
201		$path = CFCT_PATH.$dir.'/'.$file.'.php';
202	}
203	if (file_exists($path)) {
204		include($path);
205	}
206	else {
207		cfct_die('Error loading '.$file.' '.__LINE__);
208	}
209}
210
211function cfct_choose_general_template($dir) {
212	$exec_order = array(
213		'author'
214		, 'role'
215		, 'category'
216		, 'tag'
217		, 'single'
218		, 'default'
219	);
220	$new_exec_order = apply_filters('cfct_general_match_order', $exec_order);
221	$files = cfct_files(CFCT_PATH.$dir);
222	foreach ($new_exec_order as $func) {
223		$func_name = 'cfct_choose_general_template_'.$func;
224		if (function_exists($func_name) && in_array($func, $exec_order)) {
225			$filename = $func_name($dir, $files);
226			if ($filename != false) {
227				break;
228			}
229		}
230	}
231	return apply_filters('cfct_choose_general_template', $filename);
232}
233
234function cfct_choose_general_template_author($dir, $files) {
235	$files = cfct_author_templates($dir, $files);
236	if (count($files)) {
237		$username = get_query_var('author_name');
238		$filename = 'author-'.$username.'.php';
239		if (in_array($filename, $files)) {
240			$keys = array($username);
241			return cfct_filename($dir, 'author', $keys);
242		}
243 	}
244	return false;
245}
246
247function cfct_choose_general_template_category($dir, $files) {
248	$files = cfct_cat_templates($dir, $files);
249	if (count($files)) {
250		global $cat;
251		$slug = cfct_cat_id_to_slug($cat);
252		if (in_array('cat-'.$slug.'.php', $files)) {
253			$keys = array($slug);
254			return cfct_filename($dir, 'category', $keys);
255		}
256 	}
257	return false;
258}
259
260function cfct_choose_general_template_tag($dir, $files) {
261	$files = cfct_tag_templates($dir, $files);
262	if (count($files)) {
263		$tag = get_query_var('tag');
264		if (in_array('tag-'.$tag.'.php', $files)) {
265			$keys = array($tag);
266			return cfct_filename($dir, 'tag', $keys);
267		}
268 	}
269	return false;
270}
271
272function cfct_choose_general_template_role($dir, $files) {
273	$files = cfct_role_templates($dir, $files);
274	if (count($files)) {
275		$username = get_query_var('author_name');
276		$user = new WP_User(cfct_username_to_id($username));
277		if (!empty($user->user_login)) {
278			if (count($user->roles)) {
279				foreach ($user->roles as $role) {
280					$role_file = 'role-'.$role.'.php';
281					if (in_array($role_file, $files)) {
282						return $role_file;
283					}
284				}
285			}
286		}
287 	}
288	return false;
289}
290
291function cfct_choose_general_template_single($dir, $files) {
292	if (cfct_context() == 'single') {
293		$files = cfct_single_templates($dir, $files);
294		if (count($files)) {
295// TODO - check for content matches
296		}
297	}
298	return false;
299}
300
301function cfct_choose_general_template_default($dir, $files) {
302	$context = cfct_context();
303	return cfct_filename($dir, $context);
304}
305
306
307
308function cfct_choose_content_template($type = 'content') {
309	$exec_order = array(
310		'author'
311		, 'meta'
312		, 'category'
313		, 'role'
314		, 'tag'
315		, 'parent'
316		, 'default'
317	);
318	$new_exec_order = apply_filters('cfct_content_match_order', $exec_order);
319	$files = cfct_files(CFCT_PATH.$type);
320	foreach ($new_exec_order as $func) {
321		$func_name = 'cfct_choose_content_template_'.$func;
322		if (function_exists($func_name) && in_array($func, $exec_order)) {
323			$filename = $func_name($type, $files);
324			if ($filename != false) {
325				break;
326			}
327		}
328	}
329	return apply_filters('cfct_choose_content_template', $filename);
330}
331
332function cfct_choose_content_template_author($type = 'content', $files = null) {
333	$files = cfct_author_templates($type, $files);
334	if (count($files)) {
335		$author = get_the_author_login();
336		$file = 'author-'.$author.'.php';
337		if (in_array($file, $files)) {
338			$keys = array($author);
339			return cfct_filename($type, 'author', $keys);
340		}
341	}
342	return false;
343}
344
345function cfct_choose_content_template_meta($type = 'content', $files = null) {
346	global $post;
347	$files = cfct_meta_templates($type, $files);
348	if (count($files)) {
349		$meta = get_post_custom($post->ID);
350		if (count($meta)) {
351// check key, value matches first
352			foreach ($meta as $k => $v) {
353				$val = $v[0];
354				$file = 'meta-'.$k.'-'.$val.'.php';
355				if (in_array($file, $files)) {
356					$keys = array($k => $val);
357					return cfct_filename($type, 'meta', $keys);
358				}
359			}
360// check key matches only
361			foreach ($meta as $k => $v) {
362				$file = 'meta-'.$k.'.php';
363				if (in_array($file, $files)) {
364					$keys = array($k => '');
365					return cfct_filename($type, 'meta', $keys);
366				}
367			}
368		}
369	}
370	return false;
371}
372
373function cfct_choose_content_template_category($type = 'content', $files = null) {
374	$files = cfct_cat_templates($type, $files);
375	if (count($files)) {
376		foreach ($files as $file) {
377			$cat_id = cfct_cat_filename_to_id($file);
378			if (in_category($cat_id)) {
379				$keys = array(cfct_cat_filename_to_slug($file));
380				return cfct_filename($type, 'category', $keys);
381			}
382		}
383	}
384	return false;
385}
386
387function cfct_choose_content_template_role($type = 'content', $files = null) {
388	$files = cfct_role_templates($type, $files);
389	if (count($files)) {
390		$user = new WP_User(get_the_author_ID());
391		if (count($user->roles)) {
392			foreach ($files as $file) {
393				foreach ($user->roles as $role) {
394					if (cfct_role_filename_to_name($file) == $role) {
395						$keys = array($role);
396						return cfct_filename($type, 'role', $keys);
397					}
398				}
399			}
400		}
401	}
402	return false;
403}
404
405function cfct_choose_content_template_tag($type = 'content', $files = null) {
406	global $post;
407	$files = cfct_tag_templates($type, $files);
408	if (count($files)) {
409		$tags = get_the_tags($post->ID);
410		if (count($tags)) {
411			foreach ($files as $file) {
412				foreach ($tags as $tag) {
413					if ($tag->slug == cfct_tag_filename_to_name($file)) {
414						$keys = array($tag->slug);
415						return cfct_filename($type, 'tag', $keys);
416					}
417				}
418			}
419		}
420	}
421	return false;
422}
423
424function cfct_choose_content_template_parent($type = 'content', $files = null) {
425	global $post;
426	$files = cfct_parent_templates($type, $files);
427	if (count($files) && $post->post_parent > 0) {
428		$parent = cfct_post_id_to_slug($post->post_parent);
429		$file = 'parent-'.$parent.'.php';
430		if (in_array($file, $files)) {
431			$keys = array($parent);
432			return cfct_filename($type, 'parent', $keys);
433		}
434	}
435	return false;
436}
437
438function cfct_choose_content_template_default($type = 'content') {
439	$context = cfct_context();
440	return cfct_filename($type, $context);
441}
442
443function cfct_choose_comment_template() {
444	$exec_order = array(
445		'ping'
446		, 'author'
447		, 'user'
448		, 'role'
449		, 'default'
450	);
451	$new_exec_order = apply_filters('cfct_comment_match_order', $exec_order);
452	$files = cfct_files(CFCT_PATH.'comment');
453	foreach ($new_exec_order as $func) {
454		$func_name = 'cfct_choose_comment_template_'.$func;
455		if (function_exists($func_name) && in_array($func, $exec_order)) {
456			$filename = $func_name($files);
457			if ($filename != false) {
458				break;
459			}
460		}
461	}
462	return apply_filters('cfct_choose_comment_template', $filename);
463}
464
465function cfct_choose_comment_template_ping($files) {
466	global $comment;
467	switch ($comment->comment_type) {
468		case 'pingback':
469		case 'trackback':
470			return 'ping';
471			break;
472	}
473	return false;
474}
475
476function cfct_choose_comment_template_author($files) {
477	global $post, $comment;
478	if (!empty($comment->user_id) && $comment->user_id == $post->post_author && in_array('author.php', $files)) {
479		return 'author';
480 	}
481	return false;
482}
483
484function cfct_choose_comment_template_user($files) {
485	global $comment;
486	$files = cfct_comment_templates('user', $files);
487	if (count($files) && !empty($comment->user_id)) {
488		$user = new WP_User($comment->user_id);
489		if (!empty($user->user_login)) {
490			$user_file = 'user-'.$user->user_login.'.php';
491			if (in_array($user_file, $files)) {
492				return $user_file;
493			}
494		}
495 	}
496	return false;
497}
498
499function cfct_choose_comment_template_role($files) {
500	global $comment;
501	$files = cfct_comment_templates('user', $files);
502	if (count($files) && !empty($comment->user_id)) {
503		$user = new WP_User($comment->user_id);
504		if (!empty($user->user_login)) {
505			if (count($user->roles)) {
506				foreach ($user->roles as $role) {
507					$role_file = 'role-'.$role.'.php';
508					if (in_array($role_file, $files)) {
509						return $role_file;
510					}
511				}
512			}
513		}
514 	}
515	return false;
516}
517
518function cfct_choose_comment_template_default($files) {
519	return cfct_default_file('comment');
520}
521
522function cfct_files($path) {
523	$files = wp_cache_get('cfct_files_'.$path, 'cfct');
524	if ($files) {
525		return $files;
526	}
527	$files = array();
528	if ($handle = opendir($path)) {
529		while (false !== ($file = readdir($handle))) {
530			$path = trailingslashit($path);
531			if (is_file($path.$file) && strtolower(substr($file, -4, 4)) == ".php") {
532				$files[] = $file;
533			}
534		}
535	}
536	wp_cache_set('cfct_files_'.$path, $files, 'cfct', 3600);
537	return $files;
538}
539
540function cfct_filter_files($files = array(), $prefix = '') {
541	$matches = array();
542	if (count($files)) {
543		foreach ($files as $file) {
544			if (substr($file, 0, strlen($prefix)) == $prefix) {
545				$matches[] = $file;
546			}
547		}
548	}
549	return $matches;
550}
551
552function cfct_meta_templates($dir, $files = null) {
553	if (is_null($files)) {
554		$files = cfct_files(CFCT_PATH.$dir);
555	}
556	$matches = cfct_filter_files($files, 'meta-');
557	return apply_filters('cfct_meta_templates', $matches);
558}
559
560function cfct_cat_templates($dir, $files = null) {
561	if (is_null($files)) {
562		$files = cfct_files(CFCT_PATH.$dir);
563	}
564	$matches = cfct_filter_files($files, 'cat-');
565	return apply_filters('cfct_cat_templates', $matches);
566}
567
568function cfct_tag_templates($dir, $files = null) {
569	if (is_null($files)) {
570		$files = cfct_files(CFCT_PATH.$dir);
571	}
572	$matches = cfct_filter_files($files, 'tag-');
573	return apply_filters('cfct_tag_templates', $matches);
574}
575
576function cfct_author_templates($dir, $files = null) {
577	if (is_null($files)) {
578		$files = cfct_files(CFCT_PATH.$dir);
579	}
580	$matches = cfct_filter_files($files, 'author-');
581	return apply_filters('cfct_author_templates', $matches);
582}
583
584function cfct_role_templates($dir, $files = null) {
585	if (is_null($files)) {
586		$files = cfct_files(CFCT_PATH.$dir);
587	}
588	$matches = cfct_filter_files($files, 'role-');
589	return apply_filters('cfct_role_templates', $matches);
590}
591
592function cfct_parent_templates($dir, $files = null) {
593	if (is_null($files)) {
594		$files = cfct_files(CFCT_PATH.$dir);
595	}
596	$matches = cfct_filter_files($files, 'parent-');
597	return apply_filters('cfct_parent_templates', $matches);
598}
599
600function cfct_single_templates($dir, $files = null) {
601	if (is_null($files)) {
602		$files = cfct_files(CFCT_PATH.$dir);
603	}
604	$matches = cfct_filter_files($files, 'single');
605	return apply_filters('cfct_single_templates', $matches);
606}
607
608function cfct_comment_templates($type, $files = false) {
609	if (!$files) {
610		$files = cfct_files(CFCT_PATH.'comment');
611	}
612	$matches = array();
613	switch ($type) {
614		case 'user':
615			$matches = cfct_filter_files($files, 'user-');
616			break;
617		case 'role':
618			$matches = cfct_filter_files($files, 'role-');
619			break;
620	}
621	return apply_filters('cfct_comment_templates', $matches);
622}
623
624function cfct_cat_filename_to_id($file) {
625	$cat = str_replace(array('cat-', '.php'), '', $file);
626	$cat = get_category_by_slug($cat);
627	return $cat->cat_ID;
628}
629
630function cfct_cat_filename_to_name($file) {
631	$cat = str_replace(array('cat-', '.php'), '', $file);
632	$cat = get_category_by_slug($cat);
633	return $cat->name;
634}
635
636function cfct_cat_filename_to_slug($file) {
637	return str_replace(array('cat-', '.php'), '', $file);
638}
639
640function cfct_cat_id_to_slug($id) {
641	$cat = &get_category($id);
642	return $cat->slug;
643}
644
645function cfct_username_to_id($username) {
646	return get_profile('ID', $username);
647}
648
649function cfct_tag_filename_to_name($file) {
650	return str_replace(array('tag-', '.php'), '', $file);
651}
652
653function cfct_author_filename_to_name($file) {
654	return str_replace(array('author-', '.php'), '', $file);
655}
656
657function cfct_role_filename_to_name($file) {
658	return str_replace(array('role-', '.php'), '', $file);
659}
660
661function cfct_hcard_comment_author_link($str) {
662	return str_replace('<a href', "<a class='fn url' href", $str);
663}
664function cfct_hcard_ping_author_link($str) {
665	return str_replace('<a href', "<a rel='bookmark' class='fn url' href", $str);
666}
667
668function cfct_post_id_to_slug($id) {
669	$post = get_post($id);
670	return $post->post_name;
671}
672
673?>