1 | <?php
|
---|
2 | function popular_posts_block($options)
|
---|
3 | {
|
---|
4 | $mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
|
---|
5 | $disp_count = empty( $options[1] ) ? '10' : $options[1] ;
|
---|
6 | $show_month_range = empty( $options[2] ) ? '0' : $options[2] ;
|
---|
7 | $tag_select = $options[3] ;
|
---|
8 | $selected = array_slice($options,4); // get allowed cats
|
---|
9 | $this_template = empty( $options[5] ) ? 'db:'.$mydirname.'_block_popular.html' : trim( $options[5] );
|
---|
10 | $mydirpath = get_xpress_dir_path();
|
---|
11 |
|
---|
12 | if (array_search(0,$selected)===0) {
|
---|
13 | $cat_select = false;
|
---|
14 | }else {
|
---|
15 | $cat_select = true;
|
---|
16 | }
|
---|
17 | $cat_id = implode(',',$selected);
|
---|
18 | $block = array();
|
---|
19 | $item_no = 0;
|
---|
20 |
|
---|
21 | global $wpdb,$wp_query;
|
---|
22 |
|
---|
23 | $db_prefix = get_wp_prefix();
|
---|
24 |
|
---|
25 | $post_tb = $db_prefix . 'posts';
|
---|
26 | $view_tb = $db_prefix . 'views';
|
---|
27 | $user_tb = $db_prefix . 'users';
|
---|
28 |
|
---|
29 | $term_relationships_tb = $db_prefix . 'term_relationships'; // upper 2.3
|
---|
30 | $term_taxonomy = $db_prefix . 'term_taxonomy'; // upper 2.3
|
---|
31 | $terms_tb = $db_prefix . 'terms'; // upper 2.3
|
---|
32 |
|
---|
33 | $post2cat_tb = $db_prefix . 'post2cat'; //under 2.3
|
---|
34 | $categories_tb = $db_prefix . 'categories'; //under 2.3
|
---|
35 |
|
---|
36 | include ($mydirpath . '/wp-includes/version.php');
|
---|
37 |
|
---|
38 | $select = "SELECT $view_tb.post_views, $post_tb.ID, $post_tb.post_title, $post_tb.post_date, $user_tb.display_name";
|
---|
39 | if ($wp_db_version >= 6124){
|
---|
40 | $from = " FROM ((((";
|
---|
41 | $from .= " $post_tb LEFT JOIN $view_tb ON $post_tb.ID = $view_tb.post_id)";
|
---|
42 | $from .= " INNER JOIN $term_relationships_tb ON $post_tb.ID = $term_relationships_tb.object_id)";
|
---|
43 | $from .= " INNER JOIN $term_taxonomy ON $term_relationships_tb.term_taxonomy_id = $term_taxonomy.term_taxonomy_id)";
|
---|
44 | $from .= " INNER JOIN $terms_tb ON $term_taxonomy.term_id = $terms_tb.term_id)";
|
---|
45 | $from .= " INNER JOIN $user_tb ON $post_tb.post_author = $user_tb.ID";
|
---|
46 |
|
---|
47 | $where = " WHERE ($post_tb.post_type = 'post') AND ($post_tb.post_status = 'publish')";
|
---|
48 |
|
---|
49 | if ($cat_select) {
|
---|
50 | $where .= " AND ($term_taxonomy.term_id IN ($cat_id))";
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (!empty($tag_select)) {
|
---|
54 | $tag_id_list= get_tag_id($tag_select);
|
---|
55 | if (!empty($tag_id_list))
|
---|
56 | $where .= " AND ($term_taxonomy.term_id IN ($tag_id_list))";
|
---|
57 | }
|
---|
58 |
|
---|
59 | } else {
|
---|
60 | $from = " FROM ((";
|
---|
61 | $from .= " $post_tb LEFT JOIN $view_tb ON $post_tb.ID = $view_tb.post_id)";
|
---|
62 | $from .= " LEFT JOIN $post2cat_tb ON $post_tb.ID = $post2cat_tb.post_id)";
|
---|
63 | $from .= " INNER JOIN $user_tb ON $post_tb.post_author = $user_tb.ID";
|
---|
64 |
|
---|
65 | $where = " WHERE ($post_tb.post_status = 'publish') AND (UNIX_TIMESTAMP($post_tb.post_date) <= UNIX_TIMESTAMP())" ;
|
---|
66 |
|
---|
67 | if ($cat_select) {
|
---|
68 | $where .= " AND ($post2cat_tb.category_id IN ($cat_id))";
|
---|
69 | }
|
---|
70 | }
|
---|
71 | if ($show_month_range > 0) {
|
---|
72 | $where .= " AND (UNIX_TIMESTAMP($post_tb.post_date) >= UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE, INTERVAL -$show_month_range month)))";
|
---|
73 | }
|
---|
74 | $order_limmit = " GROUP BY $post_tb.ID ORDER BY $view_tb.post_views DESC LIMIT 0, $disp_count";
|
---|
75 |
|
---|
76 | $populars = $wpdb->get_results($select . $from . $where . $order_limmit);
|
---|
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(' • ');
|
---|
105 | $category = ob_get_contents();
|
---|
106 | ob_end_clean();
|
---|
107 |
|
---|
108 | ob_start();
|
---|
109 | the_tags(__('Tags:', 'kubrick') . ' ',' • ','');
|
---|
110 | $tags = ob_get_contents();
|
---|
111 | ob_end_clean();
|
---|
112 |
|
---|
113 | ob_start();
|
---|
114 | the_modified_date(get_settings('date_format'));
|
---|
115 | $post_modified_date = ob_get_contents();
|
---|
116 | ob_end_clean();
|
---|
117 |
|
---|
118 | ob_start();
|
---|
119 | the_modified_date(get_settings('time_format'));
|
---|
120 | $post_modified_time = ob_get_contents();
|
---|
121 | ob_end_clean();
|
---|
122 |
|
---|
123 | ob_start();
|
---|
124 | the_time(get_settings('date_format'));
|
---|
125 | $post_date = ob_get_contents();
|
---|
126 | ob_end_clean();
|
---|
127 |
|
---|
128 | ob_start();
|
---|
129 | the_time(get_settings('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' ,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 |
|
---|
171 | function 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 | ?> |
---|