XPressME Integration Kit

Trac

source: trunk/wp-content/themes/xpress_mu_home/blocks/popular_posts_block_theme.php @ 213

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

WordPressMU時、メイン homeテーマ追加

File size: 6.4 KB
Line 
1<?php
2function popular_posts_block($options)
3{
4        $mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
5        $this_template = empty( $options[1] ) ? 'db:'.$mydirname.'_block_popular.html' : trim( $options[1] );
6        $disp_count = empty( $options[2] ) ? '10' : $options[2] ;
7        $show_month_range = empty( $options[3] ) ? '0' : $options[3] ;
8        $date_format = empty( $options[4] ) ? '' : $options[4] ;
9        $time_format = empty( $options[5] ) ? '' : $options[5] ;
10        $tag_select = $options[6] ;
11        $selected = array_slice($options, 7); // get allowed cats
12
13        $mydirpath = get_xpress_dir_path();
14       
15        if (empty($date_format)) $date_format = get_settings('date_format');
16        if (empty($time_format)) $time_format = get_settings('time_format');
17        if (array_search(0,$selected)===0) {
18                $cat_select = false;
19        }else {
20                $cat_select = true;                     
21        }
22        $cat_id = implode(',',$selected);
23        $block = array();
24        $item_no = 0;   
25       
26        $selected_author_id = xpress_selected_author_id(false);
27
28        global $wpdb,$wp_query,$xoops_db;
29       
30        $db_prefix = get_wp_prefix();
31       
32        $post_tb = $db_prefix . 'posts';
33        $view_tb = $db_prefix . 'views';
34        $user_tb = $db_prefix . 'users';
35       
36        $term_relationships_tb = $db_prefix . 'term_relationships';     // upper 2.3
37        $term_taxonomy = $db_prefix . 'term_taxonomy';                          // upper 2.3
38        $terms_tb = $db_prefix . 'terms';                                                       // upper 2.3
39       
40        $post2cat_tb = $db_prefix . 'post2cat';                                         //under 2.3
41        $categories_tb = $db_prefix . 'categories';                             //under 2.3
42       
43        include ($mydirpath . '/wp-includes/version.php');
44               
45        $select = "SELECT $view_tb.post_views, $post_tb.ID, $post_tb.post_title, $post_tb.post_date";                           
46
47                $from  = " FROM (((";
48                $from .= " $post_tb LEFT JOIN $view_tb ON $post_tb.ID = $view_tb.post_id)";
49                $from .= " INNER JOIN $term_relationships_tb ON $post_tb.ID = $term_relationships_tb.object_id)";
50                $from .= " INNER JOIN $term_taxonomy ON $term_relationships_tb.term_taxonomy_id = $term_taxonomy.term_taxonomy_id)";
51                $from .= " INNER JOIN $terms_tb ON $term_taxonomy.term_id = $terms_tb.term_id ";
52               
53                $where = " WHERE $post_tb.post_type = 'post' AND $post_tb.post_status = 'publish'";
54//              if (!empty($selected_author_id)){
55//                      $where  .= " AND ($post_tb.post_author = $selected_author_id) ";
56//                      $where  .= " AND ($post_tb.post_author = 2) ";
57//              }
58
59                if ($cat_select) {
60                        $where .= " AND ($term_taxonomy.term_id IN ($cat_id))";
61                }
62               
63                if (!empty($tag_select)) {
64                        $tag_id_list= get_tag_id($tag_select);
65                        if (!empty($tag_id_list))
66                                $where .= " AND ($term_taxonomy.term_id IN ($tag_id_list))";
67                }
68               
69
70        if ($show_month_range > 0) {
71                        $where .= " AND (UNIX_TIMESTAMP($post_tb.post_date) >= UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE, INTERVAL -$show_month_range month)))";
72        }
73        $order_limmit = " GROUP BY $post_tb.ID ORDER BY $view_tb.post_views DESC LIMIT 0, $disp_count";
74        $sql = $select . $from . $where . $order_limmit;
75
76        $populars = $wpdb->get_results($sql);
77       
78        foreach ($populars as $popular){
79                $wp_query->in_the_loop = true;          //for use the_tags() in multi lopp
80                $r = new WP_Query("p=$popular->ID");
81                if($r->have_posts()){
82                        $r->the_post();
83                        ob_start();
84                                the_ID();
85                                $post_id = ob_get_contents();
86                        ob_end_clean();
87                       
88                        ob_start();
89                                the_title();
90                                $title = ob_get_contents();
91                        ob_end_clean();
92                       
93                        ob_start();
94                                the_permalink();
95                                $permalink = ob_get_contents();
96                        ob_end_clean();                                 
97                       
98                        ob_start();
99                                the_author_posts_link();
100                                $author = ob_get_contents();
101                        ob_end_clean();
102                       
103                        ob_start();
104                                the_category(' &bull; ');
105                                $category = ob_get_contents();
106                        ob_end_clean();
107                       
108                        ob_start();
109                                the_tags(__('Tags:', 'kubrick') . ' ',' &bull; ','');
110                                $tags = ob_get_contents();
111                        ob_end_clean();
112                       
113                        ob_start();
114                                the_modified_date($date_format);
115                                $post_modified_date = ob_get_contents();
116                        ob_end_clean();
117                       
118                        ob_start();
119                                the_modified_date($time_format);
120                                $post_modified_time = ob_get_contents();
121                        ob_end_clean();
122                       
123                        ob_start();
124                                the_time($date_format);
125                                $post_date = ob_get_contents();
126                        ob_end_clean();
127                       
128                        ob_start();
129                                the_time($time_format);
130                                $post_time = ob_get_contents();
131                        ob_end_clean();
132                       
133                       
134                        ob_start();
135                                comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)'));
136                                $comments_popup_link = ob_get_contents();
137                        ob_end_clean();
138                       
139
140                        $post_title = '<a href="' . $permalink . '">' . $title . '</a>';
141                        $post_date_time = $post_date . ' ' . $post_time ;
142                        $post_modified_date_time = $post_modified_date . ' ' . $post_modified_time ;
143                        $trackback_url = trackback_url(false);
144                        $post_viwes = xpress_post_views_count($post_id,__('views: %d','xpress') ,false);
145//                      if (empty($tags)) $tags = __('Not Tag');
146
147                        $row_data = array(
148                                'post_id'               => $post_id ,
149                                'post_title'    => $post_title ,
150                                'post_date' => $post_date ,
151                                'post_time' => $post_time ,
152                                'post_date_time' => $post_date_time ,
153                                'post_modified_date' => $post_modified_date ,
154                                'post_modified_time' => $post_modified_time ,
155                                'post_modified_date_time' => $post_modified_date_time ,
156                                'post_author'   => $author ,
157                                'post_category'         => $category , 
158                                'post_tags'             => $tags,
159                                'post_views'            => $post_viwes,
160                                'comment_link'  => $comments_popup_link ,
161                                'trackback_url' => $trackback_url
162                        );
163                        $block['contents']['item'.$item_no] = $row_data;
164                        $item_no++;
165                }
166        }  // end of foreach
167        $block['data_count'] = $item_no;  //xml unserialise error
168        return $block ;
169}
170
171function get_tag_id($tag_list = ''){
172        global $wpdb,$wp_query;
173       
174        if (empty($tag_list)) return '';
175       
176        $tag_arrys = explode(',',$tag_list);
177        $tag_str = '';
178        foreach ($tag_arrys as $tag_s){
179                if (!empty($tag_str)) $tag_str .= ',';
180                $tag_str .= "'" . $tag_s . "'";
181        }
182       
183       
184       
185        $db_prefix = get_wp_prefix();
186        $db_xpress_terms = $db_prefix . 'terms';                                        // upper 2.3
187        $db_xpress_term_taxonomy = $db_prefix . 'term_taxonomy';                                // upper 2.3
188        $query = "
189                SELECT $db_xpress_terms.term_id as tag_ID 
190                FROM $db_xpress_terms LEFT JOIN $db_xpress_term_taxonomy ON $db_xpress_terms.term_id = $db_xpress_term_taxonomy.term_id
191                WHERE $db_xpress_term_taxonomy.taxonomy = 'post_tag' AND $db_xpress_terms.name IN ($tag_str)
192    ";
193
194        $tags = $wpdb->get_results($query);
195        $no =0;
196        foreach ($tags as $tag){
197                $tags_id[$no] = $tag->tag_ID;
198                $no++; 
199        }
200        $tags_id_list = implode(',' , $tags_id);
201        return $tags_id_list;
202}
203?>
Note: See TracBrowser for help on using the repository browser.