source: trunk/www.guidonia.net/wp/wp-content/plugins/wordpress-mobile-edition/carrington-mobile-1.0.2/carrington-core/utility.php@ 44

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