[552] | 1 | <?php
|
---|
| 2 | // Block Version: 1.0
|
---|
| 3 | function global_popular_posts_block($options)
|
---|
| 4 | {
|
---|
| 5 | $mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
|
---|
| 6 | $this_template = empty( $options[1] ) ? 'db:'.$mydirname.'_block_popular.html' : trim( $options[1] );
|
---|
| 7 | $disp_count = empty( $options[2] ) ? '10' : $options[2] ;
|
---|
| 8 | $show_month_range = empty( $options[3] ) ? '0' : $options[3] ;
|
---|
| 9 | $date_format = empty( $options[4] ) ? '' : $options[4] ;
|
---|
| 10 | $time_format = empty( $options[5] ) ? '' : $options[5] ;
|
---|
| 11 |
|
---|
| 12 | $selected = explode(',' , $cat_select);
|
---|
| 13 |
|
---|
| 14 | $mydirpath = get_xpress_dir_path();
|
---|
| 15 |
|
---|
| 16 | if (empty($date_format)) $date_format = get_settings('date_format');
|
---|
| 17 | if (empty($time_format)) $time_format = get_settings('time_format');
|
---|
| 18 |
|
---|
| 19 | $cat_select;
|
---|
| 20 | $block = array();
|
---|
| 21 | $item_no = 0;
|
---|
| 22 |
|
---|
| 23 | $selected_author_id = xpress_selected_author_id('echo=0');
|
---|
| 24 |
|
---|
| 25 | $data_array = array();
|
---|
| 26 |
|
---|
| 27 | global $wpdb,$wp_query,$xoops_db,$wp_rewrite;
|
---|
| 28 |
|
---|
| 29 | if (xpress_is_multiblog()){
|
---|
| 30 | $blogs = get_blog_list(0,'all');
|
---|
| 31 | foreach ($blogs AS $blog) {
|
---|
| 32 | switch_to_blog($blog['blog_id']);
|
---|
| 33 | $wp_rewrite->init();
|
---|
| 34 |
|
---|
| 35 | $db_prefix = get_wp_prefix();
|
---|
| 36 |
|
---|
| 37 | $post_tb = $wpdb->posts;
|
---|
| 38 | $view_tb = $db_prefix . 'views';
|
---|
| 39 | $user_tb = $db_prefix . 'users';
|
---|
| 40 |
|
---|
| 41 | $term_relationships_tb = $wpdb->term_relationships; // upper 2.3
|
---|
| 42 | $term_taxonomy = $wpdb->term_taxonomy; // upper 2.3
|
---|
| 43 | $terms_tb = $wpdb->terms; // upper 2.3
|
---|
| 44 |
|
---|
| 45 | $post2cat_tb = $wpdb->post2cat; //under 2.3
|
---|
| 46 | $categories_tb = $wpdb->categories; //under 2.3
|
---|
| 47 |
|
---|
| 48 | include ($mydirpath . '/wp-includes/version.php');
|
---|
| 49 |
|
---|
| 50 | $select = "SELECT $view_tb.post_views, $post_tb.ID, $post_tb.post_title, $post_tb.post_date";
|
---|
| 51 | if ($wp_db_version >= 6124){
|
---|
| 52 | $from = " FROM (((";
|
---|
| 53 | $from .= " $post_tb LEFT JOIN $view_tb ON $post_tb.ID = $view_tb.post_id)";
|
---|
| 54 | $from .= " INNER JOIN $term_relationships_tb ON $post_tb.ID = $term_relationships_tb.object_id)";
|
---|
| 55 | $from .= " INNER JOIN $term_taxonomy ON $term_relationships_tb.term_taxonomy_id = $term_taxonomy.term_taxonomy_id)";
|
---|
| 56 | $from .= " INNER JOIN $terms_tb ON $term_taxonomy.term_id = $terms_tb.term_id ";
|
---|
| 57 |
|
---|
| 58 | $where = " WHERE $post_tb.post_type = 'post' AND $post_tb.post_status = 'publish'";
|
---|
| 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 | }
|
---|
| 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(' • ');
|
---|
| 105 | $category = ob_get_contents();
|
---|
| 106 | ob_end_clean();
|
---|
| 107 |
|
---|
| 108 | if (function_exists('the_tags')){
|
---|
| 109 | ob_start();
|
---|
| 110 | the_tags(__('Tags:', 'xpress') . ' ',' • ','');
|
---|
| 111 | $tags = ob_get_contents();
|
---|
| 112 | ob_end_clean();
|
---|
| 113 | } else {
|
---|
| 114 | $tags = '';
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | ob_start();
|
---|
| 118 | the_modified_date($date_format);
|
---|
| 119 | $post_modified_date = ob_get_contents();
|
---|
| 120 | ob_end_clean();
|
---|
| 121 |
|
---|
| 122 | ob_start();
|
---|
| 123 | the_modified_date($time_format);
|
---|
| 124 | $post_modified_time = ob_get_contents();
|
---|
| 125 | ob_end_clean();
|
---|
| 126 |
|
---|
| 127 | ob_start();
|
---|
| 128 | the_time($date_format);
|
---|
| 129 | $post_date = ob_get_contents();
|
---|
| 130 | ob_end_clean();
|
---|
| 131 |
|
---|
| 132 | ob_start();
|
---|
| 133 | the_time($time_format);
|
---|
| 134 | $post_time = ob_get_contents();
|
---|
| 135 | ob_end_clean();
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | ob_start();
|
---|
| 139 | comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)'));
|
---|
| 140 | $comments_popup_link = ob_get_contents();
|
---|
| 141 | ob_end_clean();
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | $post_title = '<a href="' . $permalink . '">' . $title . '</a>';
|
---|
| 145 | $post_date_time = $post_date . ' ' . $post_time ;
|
---|
| 146 | $post_modified_date_time = $post_modified_date . ' ' . $post_modified_time ;
|
---|
| 147 | $trackback_url = trackback_url(false);
|
---|
| 148 | $post_views = xpress_post_views_count('post_id=' . $post_id . '&format=' . __('Views :%d', 'xpress'). '&echo=0');
|
---|
| 149 | $post_views_num = (int) xpress_post_views_count("post_id={$post_id}&format=%d&echo=0");
|
---|
| 150 | $row_data = array(
|
---|
| 151 | 'post_id' => $post_id ,
|
---|
| 152 | 'post_title' => $post_title ,
|
---|
| 153 | 'post_date' => $post_date ,
|
---|
| 154 | 'post_time' => $post_time ,
|
---|
| 155 | 'post_date_time' => $post_date_time ,
|
---|
| 156 | 'post_modified_date' => $post_modified_date ,
|
---|
| 157 | 'post_modified_time' => $post_modified_time ,
|
---|
| 158 | 'post_modified_date_time' => $post_modified_date_time ,
|
---|
| 159 | 'post_author' => $author ,
|
---|
| 160 | 'post_category' => $category ,
|
---|
| 161 | 'post_tags' => $tags,
|
---|
| 162 | 'post_views' => $post_views,
|
---|
| 163 | 'post_views_num' => $post_views_num,
|
---|
| 164 | 'comment_link' => $comments_popup_link ,
|
---|
| 165 | 'trackback_url' => $trackback_url
|
---|
| 166 | );
|
---|
| 167 | $data_array[] = $row_data;
|
---|
| 168 | }
|
---|
| 169 | } // end of foreach
|
---|
| 170 | restore_current_blog();
|
---|
| 171 | $wp_rewrite->init();
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | usort($data_array, "post_views_cmp");
|
---|
| 176 | if (!empty($disp_count)){
|
---|
| 177 | $data_array = array_slice($data_array,0,$disp_count);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | $item_no = 0;
|
---|
| 181 | foreach ($data_array as $data) {
|
---|
| 182 | $block['contents']['item'.$item_no] = $data;
|
---|
| 183 | $item_no++;
|
---|
| 184 | }
|
---|
| 185 | $block['data_count'] = $item_no;
|
---|
| 186 |
|
---|
| 187 | return $block ;
|
---|
| 188 | }
|
---|
| 189 | function post_views_cmp($a, $b)
|
---|
| 190 | {
|
---|
| 191 | return $b['post_views_num'] - $a['post_views_num'];
|
---|
| 192 | }
|
---|
| 193 | ?> |
---|