XPressME Integration Kit

Trac

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

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

#90 マルチループを使っているブロックがマルチユーザに対応していない に対する修正

File size: 7.0 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;
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, $user_tb.display_name";                           
46        if ($wp_db_version >= 6124){
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                $from .= " INNER JOIN $user_tb ON $post_tb.post_author = $user_tb.ID";
53               
54                $where = " WHERE ($post_tb.post_type = 'post') AND ($post_tb.post_status = 'publish')";
55                if (!empty($selected_author_id)){
56                        $where  .= " AND $post_tb.post_author = $selected_author_id ";
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        } else {
70                $from  = " FROM ((";
71                $from .= " $post_tb LEFT JOIN $view_tb ON $post_tb.ID = $view_tb.post_id)";
72                $from .= " LEFT JOIN $post2cat_tb ON $post_tb.ID = $post2cat_tb.post_id)";
73                $from .= " INNER JOIN $user_tb ON $post_tb.post_author = $user_tb.ID";
74               
75                $where = " WHERE ($post_tb.post_status = 'publish') AND  (UNIX_TIMESTAMP($post_tb.post_date) <= UNIX_TIMESTAMP())" ;
76               
77                if ($cat_select) {
78                        $where .= " AND ($post2cat_tb.category_id IN ($cat_id))";
79                }
80                if (!empty($selected_author_id)){
81                        $where  .= " AND $post_tb.post_author = $selected_author_id ";
82                }
83
84        }
85        if ($show_month_range > 0) {
86                        $where .= " AND (UNIX_TIMESTAMP($post_tb.post_date) >= UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE, INTERVAL -$show_month_range month)))";
87        }
88        $order_limmit = " GROUP BY $post_tb.ID ORDER BY $view_tb.post_views DESC LIMIT 0, $disp_count";
89       
90        $populars = $wpdb->get_results($select . $from . $where . $order_limmit);
91       
92        foreach ($populars as $popular){
93                $wp_query->in_the_loop = true;          //for use the_tags() in multi lopp
94                $r = new WP_Query("p=$popular->ID");
95                if($r->have_posts()){
96                        $r->the_post();
97                        ob_start();
98                                the_ID();
99                                $post_id = ob_get_contents();
100                        ob_end_clean();
101                       
102                        ob_start();
103                                the_title();
104                                $title = ob_get_contents();
105                        ob_end_clean();
106                       
107                        ob_start();
108                                the_permalink();
109                                $permalink = ob_get_contents();
110                        ob_end_clean();                                 
111                       
112                        ob_start();
113                                the_author_posts_link();
114                                $author = ob_get_contents();
115                        ob_end_clean();
116                       
117                        ob_start();
118                                the_category(' &bull; ');
119                                $category = ob_get_contents();
120                        ob_end_clean();
121                       
122                        ob_start();
123                                the_tags(__('Tags:', 'kubrick') . ' ',' &bull; ','');
124                                $tags = ob_get_contents();
125                        ob_end_clean();
126                       
127                        ob_start();
128                                the_modified_date($date_format);
129                                $post_modified_date = ob_get_contents();
130                        ob_end_clean();
131                       
132                        ob_start();
133                                the_modified_date($time_format);
134                                $post_modified_time = ob_get_contents();
135                        ob_end_clean();
136                       
137                        ob_start();
138                                the_time($date_format);
139                                $post_date = ob_get_contents();
140                        ob_end_clean();
141                       
142                        ob_start();
143                                the_time($time_format);
144                                $post_time = ob_get_contents();
145                        ob_end_clean();
146                       
147                       
148                        ob_start();
149                                comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)'));
150                                $comments_popup_link = ob_get_contents();
151                        ob_end_clean();
152                       
153
154                        $post_title = '<a href="' . $permalink . '">' . $title . '</a>';
155                        $post_date_time = $post_date . ' ' . $post_time ;
156                        $post_modified_date_time = $post_modified_date . ' ' . $post_modified_time ;
157                        $trackback_url = trackback_url(false);
158                        $post_viwes = xpress_post_views_count($post_id,'views: %d' ,false);
159//                      if (empty($tags)) $tags = __('Not Tag');
160
161                        $row_data = array(
162                                'post_id'               => $post_id ,
163                                'post_title'    => $post_title ,
164                                'post_date' => $post_date ,
165                                'post_time' => $post_time ,
166                                'post_date_time' => $post_date_time ,
167                                'post_modified_date' => $post_modified_date ,
168                                'post_modified_time' => $post_modified_time ,
169                                'post_modified_date_time' => $post_modified_date_time ,
170                                'post_author'   => $author ,
171                                'post_category'         => $category , 
172                                'post_tags'             => $tags,
173                                'post_views'            => $post_viwes,
174                                'comment_link'  => $comments_popup_link ,
175                                'trackback_url' => $trackback_url
176                        );
177                        $block['contents']['item'.$item_no] = $row_data;
178                        $item_no++;
179                }
180        }  // end of foreach
181        $block['data_count'] = $item_no;  //xml unserialise error
182        return $block ;
183}
184
185function get_tag_id($tag_list = ''){
186        global $wpdb,$wp_query;
187       
188        if (empty($tag_list)) return '';
189       
190        $tag_arrys = explode(',',$tag_list);
191        $tag_str = '';
192        foreach ($tag_arrys as $tag_s){
193                if (!empty($tag_str)) $tag_str .= ',';
194                $tag_str .= "'" . $tag_s . "'";
195        }
196       
197       
198       
199        $db_prefix = get_wp_prefix();
200        $db_xpress_terms = $db_prefix . 'terms';                                        // upper 2.3
201        $db_xpress_term_taxonomy = $db_prefix . 'term_taxonomy';                                // upper 2.3
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.