XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/wp-content/plugins/xpressme/include/custom_functions.php @ 272

Last change on this file since 272 was 272, checked in by toemon, 15 years ago

#130 カスタムテンプレートタグでwp_parse_argsを利用
get_xpress_excerpt_contents()
xpress_the_content()
を除き修正

File size: 27.4 KB
Line 
1<?php
2function xpress_the_title($args = '')
3{
4        $defaults = array(
5                'echo' => 1
6        );
7        $r = wp_parse_args( $args, $defaults );
8
9        extract( $r );
10
11        $output = '<div class ="xpress-post-header">' . "\n";
12       
13        if (function_exists('hotDates')) {
14                ob_start();
15                        hotDates();
16                        $output .= ob_get_contents();
17                ob_end_clean();
18        }
19        $output .= '<div class ="xpress-post-title">' . "\n";
20        $output .= '<h2><a href="';
21        ob_start();
22                the_permalink();
23                $output .= ob_get_contents();
24        ob_end_clean();
25       
26        if(function_exists('the_title_attribute')){
27                $title = the_title_attribute('echo=0');
28        } else {
29                ob_start();
30                        the_title();
31                        $title = ob_get_contents();
32                ob_end_clean();
33        }
34                                               
35        $output .= '" rel="bookmark" title="';
36        $output .= sprintf(__('Permanent Link to %s', 'xpress'), $title);
37        $output .= '">';
38        $output .= $title;
39        $output .= '</a></h2>' . "\n";
40        $output .= '</div>' . "\n";
41        $output .= '</div>' . "\n";
42       
43        if ($echo)
44                echo $output;
45        else
46                return $output;
47
48}
49
50function xpress_is_wpmu() {
51        global $xoops_config;
52       
53        return $xoops_config->is_wpmu;
54}
55
56function xpress_is_wp20() {
57        global $xoops_config;
58       
59        return $xoops_config->is_wp20;
60}
61
62function xpress_selected_author($args ='' ) {
63        $defaults = array(
64                'echo' => 1
65        );
66        $r = wp_parse_args( $args, $defaults );
67
68        extract( $r );
69
70        $output = '';
71        $author_cookie = get_xpress_dir_name() . "_select_author" ;
72        if (!empty($_COOKIE[$author_cookie])){
73                $uid = intval($_COOKIE[$author_cookie]);
74                $user_info = get_userdata($uid);
75                $output = $user_info->display_name;
76        }
77        if ($echo)
78                echo $output;
79        else
80                return $output;
81               
82}
83function xpress_selected_author_id($args ='' ) {
84        $defaults = array(
85                'echo' => 1
86        );
87        $r = wp_parse_args( $args, $defaults );
88
89        extract( $r );
90        $output = '';
91        $author_cookie = get_xpress_dir_name() . "_select_author" ;
92        if (!empty($_COOKIE[$author_cookie])){
93                $output = intval($_COOKIE[$author_cookie]);
94        } else {
95                $output = '';
96        }
97        if ($echo)
98                echo $output;
99        else
100                return $output;         
101}
102
103function xpress_now_user_level($args ='' ) {
104        global $current_user;
105        $defaults = array(
106                'echo' => 1
107        );
108        $r = wp_parse_args( $args, $defaults );
109
110        extract( $r );
111       
112        $output = @$current_user->user_level;
113        if ($echo)
114                echo $output;
115        else
116                return $output;
117}
118
119
120       
121function xpress_credit($args ='')
122{
123        global $wp_version , $xoops_config;
124        if ($xoops_config->is_wpmu) {
125                global $wpmu_version;
126        }
127       
128        $defaults = array(
129                'echo' => 1
130        );
131        $r = wp_parse_args( $args, $defaults );
132
133        extract( $r );
134       
135        $xpress_version = $xoops_config->module_version;
136        $xpress_codename = $xoops_config->module_codename;
137        $output = '<a href="http://ja.xpressme.info"'. " target='_blank'" . '>XPressME Ver.' . sprintf('%.2f %s',$xpress_version,$xpress_codename) .'</a>';
138        if ($xoops_config->is_wpmu) {
139                $output .= '(included <a href="http://mu.wordpress.org/" title="Powered by WordPress"'." target='_blank'". '>WordPress MU ' . $wpmu_version . '</a>)';
140        } else {
141                if (strstr($wp_version,'ME')){
142                        $output .= '(included <a href="http://wpme.sourceforge.jp/" title="Powered by WordPress"'." target='_blank'". '>WordPress ' . $wp_version . '</a>)';
143                } else {
144                        $output .= '(included <a href="http://wordpress.org/" title="Powered by WordPress"'." target='_blank'". '>WordPress ' . $wp_version . '</a>)';
145                }
146        }
147        if ($echo)
148                echo $output;
149        else
150                return $output;
151}
152
153function xpress_convert_time($args ='')
154{
155        $defaults = array(
156                'echo' => 1,
157                'format' => '(%.3f sec.)'               
158        );
159        $r = wp_parse_args( $args, $defaults );
160
161        extract( $r );
162       
163        $output =  sprintf($format,timer_stop(0));
164        if ($echo)
165                echo $output;
166        else
167                return $output;
168}
169
170function xpress_is_theme_sidebar_disp(){
171        global $xpress_config;
172        if (is_wordpress_style()) return true;
173        return $xpress_config->is_theme_sidebar_disp;
174}       
175
176function xpress_left_arrow_post_link($args ='')
177{
178        global $xpress_config;
179        $defaults = array(
180                'echo' => 1
181        );
182        $r = wp_parse_args( $args, $defaults );
183
184        extract( $r );
185       
186        $ret = '';
187               
188        if($xpress_config->is_left_postnavi_old){
189                $link_title = $xpress_config->old_post_link_text;
190                ob_start();
191                if ($xpress_config->is_postnavi_title_disp)
192                        previous_post_link('&laquo; %link');
193                else
194                        previous_post_link('&laquo; %link',$link_title);
195                $ret = ob_get_contents();
196                ob_end_clean();
197                ob_start();
198                        previous_post_link('%link',$link_title);
199                        $GLOBALS['left_arrow_post_link'] = ob_get_contents();
200                ob_end_clean();
201
202        } else {
203                $link_title = $xpress_config->newer_post_link_text;
204                ob_start();
205                if ($xpress_config->is_postnavi_title_disp)
206                        next_post_link('&laquo; %link');
207                else
208                        next_post_link('&laquo; %link',$link_title);
209                $ret = ob_get_contents();
210                ob_end_clean();
211                ob_start();
212                        next_post_link('%link',$link_title);
213                        $GLOBALS['left_arrow_post_link'] = ob_get_contents();
214                ob_end_clean();
215
216        }
217       
218        if ($xpress_config->is_postnavi_title_disp){
219                $on_mouse_show = $link_title;
220        } else  {
221                if($xpress_config->is_left_postnavi_old){
222                        ob_start();
223                                previous_post_link('%link');
224                                $on_mouse_show = ob_get_contents();
225                        ob_end_clean();
226                } else {
227                        ob_start();
228                                next_post_link('%link');
229                                $on_mouse_show = ob_get_contents();
230                        ob_end_clean();
231                }
232                $pattern = "<a[^>]*?>(.*)<\/a>";
233                preg_match("/".$pattern."/s",  $on_mouse_show, $body_matches);
234                $on_mouse_show = $body_matches[1];
235        }
236        $output = str_replace('">','" title="'.$on_mouse_show . '">' , $ret);
237
238        if ($echo)
239                echo $output;
240        else
241                return $output;
242}
243
244function xpress_right_arrow_post_link($args ='')
245{
246        global $xpress_config;
247        $defaults = array(
248                'echo' => 1
249        );
250        $r = wp_parse_args( $args, $defaults );
251
252        extract( $r );
253
254        $ret = '';             
255       
256        if($xpress_config->is_left_postnavi_old){
257                $link_title = $xpress_config->newer_post_link_text;
258                ob_start();
259                if ($xpress_config->is_postnavi_title_disp)
260                        next_post_link('%link &raquo;');
261                else
262                        next_post_link('%link &raquo;',$link_title);
263                $ret = ob_get_contents();
264                ob_end_clean();
265                ob_start();
266                        next_post_link('%link',$link_title);
267                        $GLOBALS['right_arrow_post_link'] = ob_get_contents();
268                ob_end_clean();
269
270        } else {
271                $link_title = $xpress_config->old_post_link_text;
272                ob_start();
273                if ($xpress_config->is_postnavi_title_disp)
274                        previous_post_link('%link &raquo;');
275                else
276                        previous_post_link('%link &raquo;',$link_title);
277                $ret = ob_get_contents();
278                ob_end_clean();
279                ob_start();
280                        previous_post_link('%link',$link_title);
281                        $GLOBALS['right_arrow_post_link'] = ob_get_contents();
282                ob_end_clean();
283
284        }
285       
286        if ($xpress_config->is_postnavi_title_disp){
287                $on_mouse_show = $link_title;
288        } else  {
289                if($xpress_config->is_left_postnavi_old){
290                        ob_start();
291                                next_post_link('%link');
292                                $on_mouse_show = ob_get_contents();
293                        ob_end_clean();
294                } else {
295                        ob_start();
296                                previous_post_link('%link');
297                                $on_mouse_show = ob_get_contents();
298                        ob_end_clean();
299                }
300                $pattern = "<a[^>]*?>(.*)<\/a>";
301                preg_match("/".$pattern."/s",  $on_mouse_show, $body_matches);
302                $on_mouse_show = $body_matches[1];
303        }
304        $output = str_replace('">','" title="'.$on_mouse_show . '">' , $ret);
305
306        if ($echo)
307                echo $output;
308        else
309                return $output;
310}
311// page link
312function xpress_left_arrow_posts_link($args ='')
313{
314        global $xpress_config;
315        $defaults = array(
316                'echo' => 1
317        );
318        $r = wp_parse_args( $args, $defaults );
319
320        extract( $r );
321
322        $output = '';
323               
324        if($xpress_config->is_left_page_navi_old){
325                $link_title = $xpress_config->old_page_link_text;
326                ob_start();
327                next_posts_link("&laquo; $link_title");
328                $output = ob_get_contents();
329                ob_end_clean();
330        } else {
331                $link_title = $xpress_config->newer_page_link_text;
332                ob_start();
333                previous_posts_link("&laquo; $link_title");
334                $output = ob_get_contents();
335                ob_end_clean();
336        }
337
338        if ($echo)
339                echo $output;
340        else
341                return $output;
342}
343
344function xpress_right_arrow_posts_link($args ='')
345{
346        global $xpress_config;
347        $defaults = array(
348                'echo' => 1
349        );
350        $r = wp_parse_args( $args, $defaults );
351
352        extract( $r );
353
354        $output = '';           
355       
356        if($xpress_config->is_left_page_navi_old){
357                $link_title = $xpress_config->newer_page_link_text;
358                ob_start();
359                previous_posts_link("$link_title &raquo;");
360                $output = ob_get_contents();
361                ob_end_clean();
362        } else {
363                $link_title = $xpress_config->old_page_link_text;
364                ob_start();
365                next_posts_link("$link_title &raquo;");
366                $output = ob_get_contents();
367                ob_end_clean();
368        }
369
370        if ($echo)
371                echo $output;
372        else
373                return $output;
374}
375
376
377function xpress_is_author_view_count(){
378        global $xpress_config;
379        return $xpress_config->is_author_view_count;
380}
381
382function xpress_is_multi_user(){
383        global $xpress_config;
384        return $xpress_config->is_multi_user;
385}
386
387
388function xpress_substr($str, $start, $length, $trimmarker = '...')
389{
390    if (function_exists('mb_substr')){
391        $str2 = mb_substr( $str , $start , $length);
392        return $str2 . ( mb_strlen($str)!=mb_strlen($str2) ? $trimmarker : '' );
393    } else {
394        return ( strlen($str) - $start <= $length ) ? substr( $str, $start, $length ) : substr( $str, $start, $length - strlen($trimmarker) ) . $trimmarker;
395    }
396}
397
398
399// views count
400// Set and retrieves post views given a post ID or post object.
401// Retrieves post views given a post ID or post object.
402function xpress_post_views_count($args ='') {
403        global $xoops_db,$wpdb;
404
405        static $post_cache_views;
406
407        $defaults = array(
408                'post_id' => 0,
409                'format'=> __('views :%d','xpressme'),
410                'echo' => 1
411        );
412        $r = wp_parse_args( $args, $defaults );
413
414        extract( $r );
415
416        if ( empty($post_id) ) {
417                if ( isset($GLOBALS['post']) )
418                        $post_id = $GLOBALS['post']->ID;
419        }
420
421        $post_id = intval($post_id);
422        if($post_id==0) return null;
423        if(!isset($post_cache_views[$post_id])){
424        $sql = "SELECT post_views FROM " . get_wp_prefix() . "views" . " WHERE post_id=$post_id";
425        $post_views = $xoops_db->get_var($sql);
426        if (!$post_views) {
427                $post_cache_views[$post_id] = 0;
428        }else{
429                $post_cache_views[$post_id] = $post_views;
430        }
431        }
432        $v_count = intval($post_cache_views[$post_id]);
433       
434        if (empty($format)) $format = __('views :%d','xpressme');
435       
436        $output = sprintf($format,$v_count);
437
438        if ($echo)
439                echo $output;
440        else
441                return $output;
442}
443
444function set_post_views_count(&$content) {
445        if ( empty($_GET["feed"]) &&  empty($GLOBALS["feed"]) && empty($GLOBALS["doing_trackback"]) && empty($GLOBALS["doing_rss"]) && empty($_POST) && is_single() ){
446                post_views_counting();
447        }
448        return $content;
449}
450
451// Set post views given a post ID or post object.
452function post_views_counting($post_id = 0) {
453        global $xoops_db,$wpdb;
454        global $table_prefix;
455        static $views;
456       
457        $post_id = intval($post_id);
458        if ( empty($post_id) && isset($GLOBALS['post']) ){
459                $post_id = $GLOBALS['post']->ID;
460        }
461
462
463        $views_db = get_wp_prefix() . 'views';
464
465        if($post_id==0 || !empty($views[$post_id])) return null;
466       
467        if(!xpress_is_author_view_count()){
468                $current_user_id = $GLOBALS['current_user']->ID;
469                $post_author_id = $GLOBALS['post']->post_author;
470                if ($current_user_id ==$post_author_id) return null;
471        }
472
473    $sql = "SELECT post_views FROM " . $views_db . " WHERE post_id=$post_id";
474        $post_views_found = $xoops_db->get_var($sql);
475        if($post_views_found){
476        $sql = "UPDATE " . $views_db . " SET post_views=post_views+1 WHERE post_id=$post_id";
477    }else{
478        $sql = "INSERT INTO " . $views_db . " (post_id, post_views) VALUES ($post_id, 1)";
479    }
480    $xoops_db->query($sql);
481        return true;
482}
483
484function get_xpress_excerpt_contents($excerpt_length_word,$excerpt_length_character,$more_link_text = '') {
485        global $post,$xpress_config;
486       
487        $blog_encoding = get_option('blog_charset');
488        $text = get_the_content('');
489        if (function_exists('strip_shortcodes')){ //@since WP2.5
490                $text = strip_shortcodes( $text );
491        }
492        $text = apply_filters('the_content', $text);
493        $text = str_replace(']]>', ']]&gt;', $text);
494        $text = strip_tags($text);
495        $is_almost_ascii = ($xpress_config->ascii_judged_rate < round(@(mb_strlen($text, $blog_encoding) / strlen($text)) * 100)) ? true : false;
496        if($is_almost_ascii) {
497                $words = explode(' ', $text, $excerpt_length_word + 1);
498
499                if(count($words) > $excerpt_length_word) {
500                        array_pop($words);
501                        array_push($words, ' ... ');
502                        $text = implode(' ', $words);
503                        if (!empty($more_link_text)) $text .= '<p align="center"><a href="'. get_permalink() . "\">".$more_link_text .'</a></p>';
504
505                }
506        }
507        elseif(mb_strlen($text, $blog_encoding) > $excerpt_length_character) {
508                $text = mb_substr($text, 0, $xpress_config->excerpt_length_character, $blog_encoding) . ' ... ';
509                if (!empty($more_link_text)) $text .= '<p align="center"><a href="'. get_permalink() . "\">".$more_link_text .'</a></p>';
510        }
511
512        return $text;
513}
514
515function xpress_the_content($more_link_text = null, $stripteaser = 0, $more_file = '',$show = true)
516{
517        global $post,$xpress_config;
518       
519       
520        if ($xpress_config->is_content_excerpt){
521                $excerpt_length_word = $xpress_config->excerpt_length_word;
522                $excerpt_length_character = $xpress_config->excerpt_length_character;
523                $more_link_text = $xpress_config->more_link_text;
524                $content = get_xpress_excerpt_contents($excerpt_length_word,$excerpt_length_character,$more_link_text);
525        } else {
526                $content = get_the_content($more_link_text,$stripteaser,$more_file);
527                $content = apply_filters('the_content', $content);
528                $content = str_replace(']]>', ']]&gt;', $content);
529        }       
530        if(empty($show)) return $content;
531        echo $content;
532}
533
534function xpress_is_contributor()
535{
536        global $current_user;
537        get_currentuserinfo();
538        if ($current_user->user_level > 3)
539                return true;
540        else
541                return false;
542}
543
544function xpress_post_new_link($args ='')
545{
546        global $xoops_config;
547       
548        $defaults = array(
549                'link_title'=> 'Post New',
550                'echo' => 1
551        );
552        $r = wp_parse_args( $args, $defaults );
553
554        extract( $r );
555
556       
557        if ($xoops_config->wp_db_version  > 5000){
558                $output = '<a href="'. get_xpress_url() . '/wp-admin/post-new.php' . '">' . $link_title . '</a>';
559        } else {
560                $output = '<a href="'. get_xpress_url() . '/wp-admin/post.php' . '">' . $link_title . '</a>';
561        }       
562        if ($echo)
563                echo $output;
564        else
565                return $output;
566}
567
568function xpress_conditional_title($args ='')
569{
570        $defaults = array(
571                'echo' => 1
572        );
573        $r = wp_parse_args( $args, $defaults );
574
575        extract( $r );
576       
577        $selected_author = xpress_selected_author('echo=0');
578       
579        $output = __('Main', 'xpressme');
580        $output = '';
581        if (is_category())
582                $output = sprintf(__('Archive for the &#8216;%s&#8217; Category', 'xpressme'), single_cat_title('', false));
583        if (function_exists( 'is_tag' )){
584                if (is_tag())
585                        $output = sprintf(__('Posts Tagged &#8216;%s&#8217;', 'xpressme'), single_tag_title('', false) );
586        }
587        if (is_day())
588                $output = sprintf(__('Archive for %s|Daily archive page', 'xpressme'), get_the_time(__('F jS, Y', 'xpressme')));
589        if (is_month())
590                $output = sprintf(__('Archive for %s|Monthly archive page', 'xpressme'), get_the_time(__('F, Y', 'xpressme')));
591        if (is_year())
592                $output = sprintf(__('Archive for %s|Yearly archive page', 'xpressme'), get_the_time(__('Y', 'xpressme')));
593        if (is_author()){
594                if (empty($selected_author))
595                        $output = sprintf(__('Archive for the &#8216;%s&#8217; Author', 'xpressme'), get_author_name( get_query_var('author')));
596        }
597        if (is_search())
598                $output = sprintf(__('Search Results of word &#8216;%s&#8217;', 'xpressme'), get_search_query());
599       
600        if (!empty($selected_author)){
601                $selected_id = xpress_selected_author_id('echo=0');
602//              $output = get_avatar($selected_id,$size = '32') . sprintf(__('Article of %s', 'xpressme'), $selected_author) . ' - ' . $output;
603                if (empty($output))
604                        $output = sprintf(__('Article of %s', 'xpressme'), $selected_author) ;
605                else
606                        $output = sprintf(__('Article of %s', 'xpressme'), $selected_author) . ' - ' . $output;
607        }       
608        if ($echo)
609                echo $output;
610        else
611                return $output;
612}
613
614function xpress_pings_list($args =''){
615        $defaults = array(
616                'echo' => 1
617        );
618        $r = wp_parse_args( $args, $defaults );
619
620        extract( $r );
621       
622        $trackbacks = xpress_get_pings();
623        if (! empty($trackbacks)) {
624                $output = '<ol id="xpress_pingslist"> ';
625
626                foreach ($trackbacks as $trackback){
627                        $list = date(get_settings('date_format'),$trackback['date']) . ' <a target="_blank" href="' . $trackback['site_url'] . '" rel="external nofollow">' . sprintf(__('From %1$s on site %2$s','xpressme'),$trackback['title'],$trackback['site_name']) . "</a>\n" ;
628
629                        $output .=  '<li>';
630                        $output .=  $list ;
631                        $output .=  '</li>';
632
633                }
634                $output .= '</ol>' ;
635        } else {
636                $output = '';
637        }
638       
639        if ($echo)
640                echo $output;
641        else
642                return $output;
643}
644
645function xpress_pings_number( $args ='' ) {
646        $defaults = array(
647                'zero' => __('No Trackback/Pingback', 'xpressme'),
648                'one' => __('One Trackback/Pingback', 'xpressme'),
649                'more' => __('% TrackBack/Pingback', 'xpressme'),
650                'deprecated' => '',
651                'echo' => 1
652        );
653        $r = wp_parse_args( $args, $defaults );
654
655        extract( $r );
656
657        $pings = xpress_get_pings();
658        if (empty($pings)){
659                $number = 0;
660        }else {
661                $number = count($pings);
662        }
663        if ( $number > 1 )
664                $output = str_replace('%', number_format_i18n($number), $more);
665        elseif ( $number == 0 )
666                $output = $zero;
667        else // must be one
668                $output = $one;
669
670        if ($echo)
671                echo $output;
672        else
673                return $output;
674}
675
676
677function xpress_get_pings()
678{
679        global $withcomments, $post, $wpdb, $id, $trackback, $user_login, $user_ID, $user_identity;
680
681        if ( ! (is_single() || is_page() || $withcomments) )
682                return;
683
684        /** @todo Use API instead of SELECTs. */
685        if ( $user_ID) {
686                $trackbacks = $wpdb->get_results(sprintf("SELECT * , UNIX_TIMESTAMP(comment_date) AS comment_timestamp ,UNIX_TIMESTAMP(comment_date_gmt) AS comment_timestamp_gmt FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) ) AND ( comment_type = 'trackback' OR comment_type = 'pingback' ) ORDER BY comment_date", $post->ID, $user_ID));
687        } else if ( empty($trackback_author) ) {
688                $trackbacks = $wpdb->get_results(sprintf("SELECT * , UNIX_TIMESTAMP(comment_date) AS comment_timestamp ,UNIX_TIMESTAMP(comment_date_gmt) AS comment_timestamp_gmt FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND ( comment_type = 'trackback' OR comment_type = 'pingback' ) ORDER BY comment_date", $post->ID));
689        } else {
690                $trackbacks = $wpdb->get_results(sprintf("SELECT * , UNIX_TIMESTAMP(comment_date) AS comment_timestamp ,UNIX_TIMESTAMP(comment_date_gmt) AS comment_timestamp_gmt FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) AND ( comment_type = 'trackback' OR comment_type = 'pingback' ) ORDER BY comment_date", $post->ID, $trackback_author, $trackback_author_email));
691        }
692
693        if ($trackbacks){
694                $ret = array();
695                foreach ($trackbacks as $trackback){
696
697                        $pattern = '<strong>(.*)<\/strong>(.*)';
698                        if ( preg_match ( "/".$pattern."/i", $trackback->comment_content , $match ) ){
699                                $title = $match[1];
700                                $content = $match[2];
701                        }
702                        if (empty($title)) $title = $trackback->comment_author;
703
704
705                        $row_data = array(
706                                'ID'            => $trackback->comment_ID ,
707                                'post_ID'       => $trackback->comment_post_ID ,
708                                'site_name' => $trackback->comment_author ,
709                                'site_url' => $trackback->comment_author_url ,
710                                'title' => $title ,
711                                'content' => $content ,
712                                'date'          => $trackback->comment_timestamp ,
713                                'date_gmt'              => $trackback->comment_timestamp_gmt ,
714                                'agent'         => $trackback->comment_agent ,
715                                'type'          => $trackback->comment_type ,
716                                'IP'            => $trackback->comment_author_IP ,
717                        );
718                        array_push($ret,$row_data);
719                }
720                return $ret;
721        }
722                        return false;
723}
724
725function xpress_get_calendar($args = '') {
726        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts , $xoops_config;
727
728        $defaults = array(
729                sun_color => '#DB0000',
730                sat_color => '#004D99',
731                initial => true
732        );
733        $r = wp_parse_args( $args, $defaults );
734
735        extract( $r );
736        if ($xoops_config->is_wp20){
737                ob_start();
738                        get_calendar(true);
739                        $output = ob_get_contents();
740                ob_end_clean();
741                $output = preg_replace('/<th abbr=/', '<th align="center"  abbr=', $output); //week name align center
742                $output = preg_replace('/<td>/', '<td align="center">', $output); //days align center
743                $output = preg_replace('/<td id="today">/', '<td id="today" align="center">', $output); //today align center
744
745                return $output;
746        }
747
748        ob_start();
749        // Quick check. If we have no posts at all, abort!
750        if ( !$posts ) {
751                $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
752                if ( !$gotsome )
753                        return;
754        }
755
756        if ( isset($_GET['w']) )
757                $w = ''.intval($_GET['w']);
758
759        // week_begins = 0 stands for Sunday
760        $week_begins = intval(get_option('start_of_week'));
761
762        // Let's figure out when we are
763        if ( !empty($monthnum) && !empty($year) ) {
764                $thismonth = ''.zeroise(intval($monthnum), 2);
765                $thisyear = ''.intval($year);
766        } elseif ( !empty($w) ) {
767                // We need to get the month from MySQL
768                $thisyear = ''.intval(substr($m, 0, 4));
769                $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
770                $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')");
771        } elseif ( !empty($m) ) {
772                $thisyear = ''.intval(substr($m, 0, 4));
773                if ( strlen($m) < 6 )
774                                $thismonth = '01';
775                else
776                                $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
777        } else {
778                $thisyear = gmdate('Y', current_time('timestamp'));
779                $thismonth = gmdate('m', current_time('timestamp'));
780        }
781
782        $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
783
784        // Get the next and previous month and year with at least one post
785        $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
786                FROM $wpdb->posts
787                WHERE post_date < '$thisyear-$thismonth-01'
788                AND post_type = 'post' AND post_status = 'publish'
789                        ORDER BY post_date DESC
790                        LIMIT 1");
791        $next = $wpdb->get_row("SELECT  DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
792                FROM $wpdb->posts
793                WHERE post_date >       '$thisyear-$thismonth-01'
794                AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
795                AND post_type = 'post' AND post_status = 'publish'
796                        ORDER   BY post_date ASC
797                        LIMIT 1");
798
799        echo '<table  summary="' . __('Calendar') . '">
800        <caption>' . sprintf(_c('%1$s %2$s|Used as a calendar caption'), $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
801        <thead>
802        <tr>';
803
804        $myweek = array();
805
806        for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
807                $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
808        }
809
810        foreach ( $myweek as $wd ) {
811
812                for($week_num=0;$week_num<=6;$week_num++){
813                        $week_name = $wp_locale->get_weekday($week_num);
814                        if ($week_name === $wd) break;
815                }
816               
817                $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
818                if ($week_num ==0) $day_name = '<span style="color: ' . $sun_color . '">' . $day_name . '</span>';
819                if ($week_num ==6) $day_name = '<span style="color: ' . $sat_color . '">' . $day_name . '</span>';
820                echo "\n\t\t<th align=\"center\" abbr=\"$wd\" scope=\"col\" title=\"$wd\">$day_name</th>";
821        }
822
823        echo '
824        </tr>
825        </thead>
826
827        <tfoot>
828        <tr>';
829
830        if ( $previous ) {
831                echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($previous->month) . '" colspan="3" id="prev"><a href="' .
832                get_month_link($previous->year, $previous->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month),
833                        date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
834        } else {
835                echo "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
836        }
837
838        echo "\n\t\t".'<td class="pad">&nbsp;</td>';
839
840        if ( $next ) {
841                echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($next->month) . '" colspan="3" id="next"><a href="' .
842                get_month_link($next->year, $next->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month),
843                        date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
844        } else {
845                echo "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
846        }
847
848        echo '
849        </tr>
850        </tfoot>
851
852        <tbody>
853        <tr>';
854
855        // Get days with posts
856        $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
857                FROM $wpdb->posts WHERE MONTH(post_date) = '$thismonth'
858                AND YEAR(post_date) = '$thisyear'
859                AND post_type = 'post' AND post_status = 'publish'
860                AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);
861        if ( $dayswithposts ) {
862                foreach ( (array) $dayswithposts as $daywith ) {
863                        $daywithpost[] = $daywith[0];
864                }
865        } else {
866                $daywithpost = array();
867        }
868
869        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') !== false)
870                $ak_title_separator = "\n";
871        else
872                $ak_title_separator = ', ';
873
874        $ak_titles_for_day = array();
875        $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
876                ."FROM $wpdb->posts "
877                ."WHERE YEAR(post_date) = '$thisyear' "
878                ."AND MONTH(post_date) = '$thismonth' "
879                ."AND post_date < '".current_time('mysql')."' "
880                ."AND post_type = 'post' AND post_status = 'publish'"
881        );
882        if ( $ak_post_titles ) {
883                foreach ( (array) $ak_post_titles as $ak_post_title ) {
884
885                                $post_title = apply_filters( "the_title", $ak_post_title->post_title );
886                                $post_title = str_replace('"', '&quot;', wptexturize( $post_title ));
887
888                                if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
889                                        $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
890                                if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
891                                        $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
892                                else
893                                        $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
894                }
895        }
896
897
898        // See how much we should pad in the beginning
899        $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
900        if ( 0 != $pad )
901                echo "\n\t\t".'<td colspan="'.$pad.'" class="pad">&nbsp;</td>';
902
903        $daysinmonth = intval(date('t', $unixmonth));
904        for ( $day = 1; $day <= $daysinmonth; ++$day ) {
905                if ( isset($newrow) && $newrow )
906                        echo "\n\t</tr>\n\t<tr>\n\t\t";
907                $newrow = false;
908
909                if ( $day == gmdate('j', (time() + (get_option('gmt_offset') * 3600))) && $thismonth == gmdate('m', time()+(get_option('gmt_offset') * 3600)) && $thisyear == gmdate('Y', time()+(get_option('gmt_offset') * 3600)) )
910                        echo '<td id="today" align="center">';
911                else
912                        echo '<td align="center">';
913
914                if ( in_array($day, $daywithpost) ) // any posts today?
915                                echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>";
916                else
917                        echo $day;
918                echo '</td>';
919
920                if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
921                        $newrow = true;
922        }
923
924        $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
925        if ( $pad != 0 && $pad != 7 )
926                echo "\n\t\t".'<td class="pad" colspan="'.$pad.'">&nbsp;</td>';
927
928        echo "\n\t</tr>\n\t</tbody>\n\t</table>";
929
930        $output = ob_get_contents();
931        ob_end_clean();
932//      echo $output;
933//      $cache[ $key ] = $output;
934//      wp_cache_set( 'get_calendar', $cache, 'calendar' );
935        return $output;
936}
937?>
Note: See TracBrowser for help on using the repository browser.