XPressME Integration Kit

Trac

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

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

ブロックオプションで日付スタイルをカスタマイズできるオプションを追加 bump Ver0.19

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