XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/wp-content/themes/xpress_default/blocks/popular_posts_block_theme.php @ 435

Last change on this file since 435 was 435, checked in by toemon, 14 years ago

ブロックオプション、カテゴリのマルチセレクトをカンマ区切りデータとして格納するようにした。 #243

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