XPressME Integration Kit

Trac

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

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

custom_functions コード整理(xpress_is_関数をまとめて配置&コメント追加)

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