function get_totalviews_term($term_id = 1, $display = true, $with_bot = true, $type = '') { global $wpdb; $where = ''; $inner_join = ''; if ($term_id != 0) { if (is_array($term_id)) { $term_id = array_map('intval', $term_id); $where = 'tt.term_id IN (' . implode(',', $term_id) . ') AND '; } else { $where = 'tt.term_id=' . intval($term_id) . ' AND '; } $inner_join = 'INNER JOIN ' . $wpdb->term_relationships . ' AS tr ON pm.post_id = tr.object_id' . ' INNER JOIN ' . $wpdb->term_taxonomy . ' AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = "' . $type . '"'; } $add_word = get_totalviews_stats_word($term_id, $with_bot, $type); if ($with_bot) { $total_views = $wpdb->get_var('SELECT SUM(IFNULL(CAST(pm.meta_value AS UNSIGNED), 0)) FROM ' . $wpdb->postmeta . ' AS pm ' . $inner_join . ' WHERE ' . $where . ' (pm.meta_key = "' . WP_PVP::$post_meta_views . '" OR pm.meta_key = "' . WP_PVP::$post_meta_botviews . '")'); $template = str_replace('%VIEW_COUNT%', '<span id="wppvp_gt_' . $add_word . '">%VIEW_COUNT%</span>', WP_PVP::$options['template']); } else { $total_views = $wpdb->get_var('SELECT SUM(IFNULL(CAST(pm.meta_value AS UNSIGNED), 0)) FROM ' . $wpdb->postmeta . ' AS pm ' . $inner_join . ' WHERE ' . $where . ' pm.meta_key = "' . WP_PVP::$post_meta_views . '"'); $template = str_replace('%VIEW_COUNT%', '<span id="wppvp_gt_' . $add_word . '">%VIEW_COUNT%</span>', WP_PVP::$options['user_template']); } $total_views = intval($total_views); if ($display) { if (defined('WP_CACHE') && WP_CACHE) { WP_PVP::add_cache_stats('gt', $term_id, $with_bot, $type); $template = str_replace('%VIEW_COUNT%', '', $template); } else { $template = str_replace('%VIEW_COUNT%', number_format_i18n($total_views), $template); } echo $template; } else { return $total_views; } }
public static function add_cache_stats($addin, $id, $with_bot = true, $type = '') { global $wpdb; static $first_run = true; $count_id = md5($_SERVER['REQUEST_URI']); if ($first_run) { $wpdb->query('DELETE FROM ' . $wpdb->postviews_plus . ' WHERE count_id = "' . $count_id . '"'); $wpdb->query('DELETE FROM ' . $wpdb->postviews_plus . ' WHERE add_time < ' . (time() - 86400 * 7)); $first_run = false; } $data = $wpdb->get_row('SELECT * FROM ' . $wpdb->postviews_plus . ' WHERE count_id = "' . $count_id . '"'); if ($data) { switch ($addin) { case 'tv': if ($data->tv == '') { $update = array('tv' => $id); } else { if (!in_array($id, explode(',', $data->tv))) { $update = array('tv' => $data->tv . ',' . $id); } } if (isset($update)) { $wpdb->update($wpdb->postviews_plus, $update, array('count_id' => $count_id)); } break; case 'gt': $add_word = get_totalviews_stats_word($id, $with_bot, $type); if ($data->gt == '') { $update = array('gt' => $add_word); } else { if (!in_array($add_word, explode(',', $data->gt))) { $update = array('gt' => $data->gt . ',' . $add_word); } } if (isset($update)) { $wpdb->update($wpdb->postviews_plus, $update, array('count_id' => $count_id)); } break; } } else { switch ($addin) { case 'tv': $wpdb->insert($wpdb->postviews_plus, array('tv' => $id, 'count_id' => $count_id, 'add_time' => time())); break; case 'gt': $add_word = get_totalviews_stats_word($id, $with_bot, $type); $wpdb->insert($wpdb->postviews_plus, array('gt' => $add_word, 'count_id' => $count_id, 'add_time' => time())); break; } } }