Popularity Values and View Reports. You can also use the included Template Tags to display post popularity and lists of popular posts on your blog. Version: 1.4.2 Author: mod by fher98 - original Alex King Author URI: http://www.guatewireless.org/ */ // Copyright (c) 2005-2007 Alex King. All rights reserved. // // Released under the GPL license // http://www.opensource.org/licenses/gpl-license.php // // This is an add-on for WordPress // http://wordpress.org/ // // ********************************************************************** // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // ********************************************************************** // // 25.11.2008 Thanks to Matt for his fix to auto create DB tables. // 31.08.2008 Mod by fher98 guatewireless.org // I was tired that mr King never did update the plugin to work with wordpress 2.5 // so heres my little hack. // - Edited and updated to support WordPress 2.6.1 // - Added hack akpc_most_popular_in_last_month() // from http://www.evilgeniuschronicles.org/wordpress/2007/03/01/popularity-contest-hacked/ // Known Issues // - When spam comments/pingbacks/trackbacks are deleted or marked as // spam, their value is not always removed from the posts they were // applied to. This is because the hooks in WP do not seem to fire // consistently with sufficient data. Hopefully this will be fixed in // a future release of WP. // Special thanks to Martijn Stegink for help with WordPress 2.3 compatibility. /* -- INSTALLATION --------------------- */ // Change this to "0" below if you don't want to show each post's popularity with the post content @define('AKPC_SHOWPOP', 1); // To hide the popularity score on a per post/page basis, add a custom field to the post/page as follows: // name: hide_popularity // value: 1 // Change this to "0" if you don't want to show the little [?] that links to the explanation of what the popularity means @define('AKPC_SHOWHELP', 1); // If you would like to show lists of popular posts in the sidebar, // take a look at how it is implemented in the included sidebar.php. /* ------------------------------------- */ if (!isset($wpdb)) { require('../wp-blog-header.php'); akpc_init(); } load_plugin_textdomain('guatewireless.org'); if (!function_exists('is_admin_page')) { function is_admin_page() { if (function_exists('is_admin')) { return is_admin(); } if (function_exists('check_admin_referer')) { return true; } else { return false; } } } // -- MAIN FUNCTIONALITY class ak_popularity_contest { var $feed_value; var $home_value; var $archive_value; var $category_value; var $single_value; var $comment_value; var $pingback_value; var $trackback_value; var $logged; var $options; var $top_ranked; var $current_posts; function ak_popularity_contest() { $this->options = array( 'feed_value' ,'home_value' ,'archive_value' ,'category_value' ,'single_value' ,'comment_value' ,'pingback_value' ,'trackback_value' ); $this->feed_value = 1; $this->home_value = 2; $this->archive_value = 4; $this->category_value = 6; $this->single_value = 10; $this->comment_value = 20; $this->pingback_value = 50; $this->trackback_value = 80; $this->logged = 0; $this->top_ranked = array(); $this->current_posts = array(); } function get_settings() { global $wpdb; $result = mysql_query(" SELECT * FROM $wpdb->ak_popularity_options ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } while ($data = mysql_fetch_object($result)) { if (in_array($data->option_name, $this->options)) { $temp = $data->option_name; $this->$temp = $data->option_value; } } return true; } function install() { global $wpdb, $table_prefix; $wpdb->query(" CREATE TABLE IF NOT EXISTS `".$table_prefix."ak_popularity_options` ( `option_name` varchar (50) NOT NULL, `option_value` varchar (50) NOT NULL ) ENGINE = MyISAM;"); $wpdb->query(" CREATE TABLE IF NOT EXISTS `".$table_prefix."ak_popularity` ( `post_id` int (11) NOT NULL, `total` int (11) NOT NULL, `feed_views` int (11) NOT NULL, `home_views` int (11) NOT NULL, `archive_views` int (11) NOT NULL, `category_views` int (11) NOT NULL, `single_views` int (11) NOT NULL, `comments` int (11) NOT NULL, `pingbacks` int (11) NOT NULL, `trackbacks` int (11) NOT NULL, `last_modified` datetime NOT NULL, KEY `post_id` ( `post_id` ) ) ENGINE = MyISAM;"); $this->default_values(); $this->mine_data(); return true; } function default_values() { global $wpdb; foreach ($this->options as $option) { $temp = $this->$option; $result = mysql_query(" INSERT INTO $wpdb->ak_popularity_options VALUES ( '$option' , '$temp' ) ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } return true; } function update_settings() { global $wpdb; foreach ($this->options as $option) { if (isset($_POST[$option])) { $this->$option = intval($_POST[$option]); $temp = $this->$option; $result = mysql_query(" UPDATE $wpdb->ak_popularity_options SET option_value = '$temp' WHERE option_name = '$option' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } } $this->recalculate_popularity(); header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page=popularity-contest.php&updated=true'); die(); } function recalculate_popularity() { global $wpdb; $result = mysql_query(" UPDATE $wpdb->ak_popularity SET total = (home_views * $this->home_value) + (feed_views * $this->feed_value) + (archive_views * $this->archive_value) + (category_views * $this->category_value) + (single_views * $this->single_value) + (comments * $this->comment_value) + (pingbacks * $this->pingback_value) + (trackbacks * $this->trackback_value) ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); } function reset_data() { global $wpdb; $result = mysql_query(" TRUNCATE $wpdb->ak_popularity ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } $result = mysql_query(" TRUNCATE $wpdb->ak_popularity_options ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } $this->default_values(); return true; } function create_post_record($post_ID = -1) { global $wpdb; if ($post_ID == -1) { global $post_ID; } $result = mysql_query(" SELECT post_id FROM $wpdb->ak_popularity WHERE post_id = '$post_ID' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (mysql_num_rows($result) == 0) { $result = mysql_query(" INSERT INTO $wpdb->ak_popularity VALUES ( '$post_ID' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '".date('Y-m-d H:i:s')."' ) ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); } } function delete_post_record($post_ID = -1) { global $wpdb; if ($post_ID == -1) { global $post_ID; } $result = mysql_query(" DELETE FROM $wpdb->ak_popularity WHERE post_id = '$post_ID' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); } function mine_data() { global $wpdb; $posts = mysql_query(" SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if ($posts && mysql_num_rows($posts) > 0) { while ($post = mysql_fetch_object($posts)) { $this->create_post_record($post->ID); $this->populate_post_data($post->ID); } } return true; } function mine_gap_data() { global $wpdb; $posts = mysql_query(" SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id WHERE pop.post_id IS NULL ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if ($posts && mysql_num_rows($posts) > 0) { while ($post = mysql_fetch_object($posts)) { $this->create_post_record($post->ID); $this->populate_post_data($post->ID); } } } function populate_post_data($post_id) { global $wpdb; // grab existing comments $result = mysql_query(" SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_type = '' AND comment_approved = '1' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); $count = mysql_num_rows($result); if ($result && $count > 0) { // increase post popularity $result = mysql_query(" UPDATE $wpdb->ak_popularity SET comments = comments + $count , total = total + ".($this->comment_value * $count)." WHERE post_id = '$post_id' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } // grab existing trackbacks $result = mysql_query(" SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_type = 'trackback' AND comment_approved = '1' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); $count = mysql_num_rows($result); if ($result && $count > 0) { // increase post popularity $result = mysql_query(" UPDATE $wpdb->ak_popularity SET trackbacks = trackbacks + $count , total = total + ".($this->trackback_value * $count)." WHERE post_id = '$post_id' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } // grab existing pingbacks $result = mysql_query(" SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_type = 'pingback' AND comment_approved = '1' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); $count = mysql_num_rows($result); if ($result && $count > 0) { // increase post popularity $result = mysql_query(" UPDATE $wpdb->ak_popularity SET pingbacks = pingbacks + $count , total = total + ".($this->pingback_value * $count)." WHERE post_id = '$post_id' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } } function record_view() { if ($this->logged > 0) { return true; } global $wpdb, $posts; if (!isset($posts) || !is_array($posts) || count($posts) == 0 || is_admin_page()) { return; } $ids = array(); $ak_posts = $posts; foreach ($ak_posts as $post) { $ids[] = $post->ID; } if (is_feed()) { $result = mysql_query(" UPDATE $wpdb->ak_popularity SET feed_views = feed_views + 1 , total = total + $this->feed_value WHERE post_id IN (".implode(',', $ids).") ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } else if (is_archive() && !is_category()) { $result = mysql_query(" UPDATE $wpdb->ak_popularity SET archive_views = archive_views + 1 , total = total + $this->archive_value WHERE post_id IN (".implode(',', $ids).") ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } else if (is_category()) { $result = mysql_query(" UPDATE $wpdb->ak_popularity SET category_views = category_views + 1 , total = total + $this->category_value WHERE post_id IN (".implode(',', $ids).") ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } else if (is_single()) { $result = mysql_query(" UPDATE $wpdb->ak_popularity SET single_views = single_views + 1 , total = total + $this->single_value WHERE post_id = '".$ids[0]."' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } else { $result = mysql_query(" UPDATE $wpdb->ak_popularity SET home_views = home_views + 1 , total = total + $this->home_value WHERE post_id IN (".implode(',', $ids).") ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } } $this->logged++; return true; } function record_feedback($type, $action = '+') { global $wpdb, $comment_post_ID; switch ($type) { case 'trackback': $result = mysql_query(" UPDATE $wpdb->ak_popularity SET trackbacks = trackbacks $action 1 , total = total $action $this->trackback_value WHERE post_id = '$comment_post_ID' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } break; case 'pingback': $result = mysql_query(" UPDATE $wpdb->ak_popularity SET pingback_views = pingback_views $action 1 , total = total $action $this->pingback_value WHERE post_id = '$comment_post_ID' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } break; default: $result = mysql_query(" UPDATE $wpdb->ak_popularity SET comments = comments $action 1 , total = total $action $this->comment_value WHERE post_id = '$comment_post_ID' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } break; } return true; } function edit_feedback($comment_id, $action) { global $wpdb, $comment_post_ID; switch ($action) { case 'delete': if (!isset($comment_post_ID) || empty($comment_post_ID)) { // Often, this data isn't set for us - without it there is no joy return; } else { // Unfortunately, this hook happens after the comment // is already out of the DB, so we don't know what type it was. // Assuming it was a comment (not a trackback or pingback) is // the safest solution. $this->record_feedback('', '-'); } break; case 'status': $result = mysql_query(" SELECT comment_post_ID, comment_type, comment_approved FROM $wpdb->comments WHERE comment_ID = '$comment_id' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if ($result) { while ($data = mysql_fetch_object($result)) { if ($data->comment_approved == 'spam') { $comment_post_ID = $data->comment_post_ID; $this->record_feedback($data->comment_type, '-'); return; } } } } } function recount_feedback() { global $wpdb; $posts = mysql_query(" SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' OR post_status = 'static' ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if ($posts && mysql_num_rows($posts) > 0) { $result = mysql_query(" UPDATE $wpdb->ak_popularity SET comments = 0 , trackbacks = 0 , pingbacks = 0 ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); while ($post = mysql_fetch_object($posts)) { $this->populate_post_data($post->ID); } } $this->recalculate_popularity(); header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page=popularity-contest.php&updated=true'); die(); } function options_form() { $temp = new ak_popularity_contest; print('

'.__('Popularity Contest Options', 'guatewireless.org').'

'.__('Popularity Values', 'guatewireless.org').'

'.__('Adjust the values below as you see fit. When you save the new options the popularity rankings for your posts will be automatically updated to reflect the new values you have chosen.', 'guatewireless.org').'

'.__("(default: $temp->single_value)", 'guatewireless.org').'
'.__("(default: $temp->home_value)", 'guatewireless.org').'
'.__("(default: $temp->archive_value)", 'guatewireless.org').'
'.__("(default: $temp->category_value)", 'guatewireless.org').'
'.__("(default: $temp->feed_value)", 'guatewireless.org').'
'.__("(default: $temp->comment_value)", 'guatewireless.org').'
'.__("(default: $temp->pingback_value)", 'guatewireless.org').'
'.__("(default: $temp->trackback_value)", 'guatewireless.org').'

'.__('Example', 'guatewireless.org').'

  • '.__('Post #1 receives 11 Home Page Views (11 * 2 = 22), 6 Permalink Views (6 * 10 = 60) and 3 Comments (3 * 20 = 60) for a total value of: 142', 'guatewireless.org').'
  • '.__('Post #2 receives 7 Home Page Views (7 * 2 = 14), 10 Permalink Views (10 * 10 = 100), 7 Comments (7 * 20 = 140) and 3 Trackbacks (3 * 80 = 240) for a total value of: 494', 'guatewireless.org').'

'.__('Popularity Contest Template Tags', 'guatewireless.org').'

akpc_the_popularity()

'.__('Put this tag within The Loop to show the popularity of the post being shown. The popularity is shown as a percentage of your most popular post. For example, if the popularity total for Post #1 is 500 and your popular post has a total of 1000, this tag will show a value of 50%.', 'guatewireless.org').'

Example:

  • <?php akpc_the_popularity(); ?>
akpc_most_popular($limit = 10, $before = <li>, $after = </li>)

'.__('Put this tag outside of The Loop (perhaps in your sidebar?) to show a list (like the archives/categories/links list) of your most popular posts. All arguments are optional, the defaults are included in the example above.', 'guatewireless.org').'

Examples:

  • <?php akpc_most_popular(); ?>
  • <li><h2>Most Popular Posts</h2>
       <ul>
       <?php akpc_most_popular(); ?>
       </ul>
    </li>
akpc_most_popular_in_cat($limit = 10, $before = <li>, $after = </li>, $cat_ID = current category)

'.__('Put this tag outside of The Loop (perhaps in your sidebar?) to show a list of the most popular posts in a specific category. You may want to use this on category archive pages. All arguments are', 'guatewireless.org').'

Examples:

  • <?php akpc_most_popular_in_cat(1); ?>
  • <php if (is_category()) { akpc_most_popular_in_cat(); } ?>
  • <?php if (is_category()) { ?>
    <li><h2>Most Popular in \'<?php single_cat_title(); ?>\'</h2>
       <ul>
       <?php akpc_most_popular_in_cat(); ?>
       </ul>
    </li>
    <?php } ?>
akpc_most_popular_in_month($limit, $before, $after, $m = YYYYMM)

'.__('Put this tag outside of The Loop (perhaps in your sidebar?) to show a list of the most popular posts in a specific month. You may want to use this on monthly archive pages.', 'guatewireless.org').'

Examples:

  • <?php akpc_most_popular_in_month(\'200504\'); ?>
  • <php if (is_archive() && is_month()) { akpc_most_popular_in_month(); } ?>
  • <?php if (is_archive() && is_month()) { ?>
    <li><h2>Most Popular in <?php the_time(\'F, Y\'); ?></h2>
       <ul>
       <?php akpc_most_popular_in_month(); ?>
       </ul>
    </li>
    <?php } ?>
'); } function show_report($type = 'popular', $limit = 10, $custom = array()) { global $wpdb; if (count($custom) > 0 && 1 == 0) { } else { $query = ''; $column = ''; $items = array(); switch ($type) { case 'category': $title = $custom['cat_name']; $items = $wpdb->get_results(" SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id LEFT JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id WHERE tt.term_id = ".$custom['cat_ID']." AND p.post_status = 'publish' ORDER BY pop.total DESC LIMIT $limit "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.$this->get_post_rank(-1, $item->total).' '.$item->post_title.'
  • '."\n"; } } break; case 'pop_by_category': $cats = array(); $cats = $wpdb->get_results(" SELECT name, t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id WHERE tt.taxonomy = 'category' ORDER BY name "); $i = 1; if (count($cats) > 0) { foreach ($cats as $cat) { $temp = array( 'cat_ID' => $cat->term_id ,'cat_name' => $cat->name ); $this->show_report('category', 10, $temp); if ($i == 3) { print('
    '); $i = 0; } $i++; } } break; case 'category_popularity': $title = __('Average by Category', 'guatewireless.org'); $items = $wpdb->get_results(" SELECT DISTINCT name, AVG(pop.total) AS avg FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id LEFT JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id LEFT JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN $wpdb->terms t ON tt.term_id = t.term_id GROUP BY name ORDER BY avg DESC "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.ceil($item->avg).' '.$item->name.'
  • '."\n"; } } break; case 'year': global $month; $title = $custom['y'].__(' Average by Month', 'guatewireless.org'); $items = $wpdb->get_results(" SELECT MONTH(p.post_date) AS month, AVG(pop.total) AS avg FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id WHERE YEAR(p.post_date) = '".$custom['y']."' GROUP BY month ORDER BY avg DESC "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.ceil($item->avg).' '.$month[str_pad($item->month, 2, '0', STR_PAD_LEFT)].'
  • '."\n"; } } break; case 'month_popularity': $years = array(); $years = $wpdb->get_results(" SELECT DISTINCT YEAR(post_date) AS year FROM $wpdb->posts ORDER BY year DESC "); $i = 2; if (count($years) > 0) { foreach ($years as $year) { $temp = array( 'y' => $year->year ); $this->show_report('year', 10, $temp); if ($i == 3) { print('
    '); $i = 0; } $i++; } } break; case 'views_wo_feedback': $title = __('Views w/o Feedback', 'guatewireless.org'); $items = $wpdb->get_results(" SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id WHERE pop.comments = 0 AND pop.pingbacks = 0 AND pop.trackbacks = 0 AND p.post_status = 'publish' ORDER BY pop.total DESC LIMIT $limit "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.$this->get_post_rank(-1, $item->total).' '.$item->post_title.'
  • '."\n"; } } break; case 'most_feedback': $query = 'sum'; $column = 'pop.comments + pop.pingbacks + pop.trackbacks AS feedback'; $title = __('Feedback', 'guatewireless.org'); break; case 'last_30': $query = 'date'; $days = 30; $offset = 0; $compare = '>'; $title = __('Last 30 Days', 'guatewireless.org'); break; case 'last_60': $query = 'date'; $days = 60; $offset = 0; $compare = '>'; $title = __('Last 60 Days', 'guatewireless.org'); break; case 'last_90': $query = 'date'; $days = 90; $offset = 0; $compare = '>'; $title = __('Last 90 Days', 'guatewireless.org'); break; case 'last_365': $query = 'date'; $days = 365; $offset = 0; $compare = '>'; $title = __('Last Year', 'guatewireless.org'); break; case '365_plus': $query = 'date'; $days = 0; $offset = -365; $compare = '<'; $title = __('Older Than 1 Year', 'guatewireless.org'); break; case 'most_feed_views': $query = 'most'; $column = 'feed_views'; $title = __('Feed Views', 'guatewireless.org'); break; case 'most_home_views': $query = 'most'; $column = 'home_views'; $title = __('Home Page Views', 'guatewireless.org'); break; case 'most_archive_views': $query = 'most'; $column = 'archive_views'; $title = __('Archive Views', 'guatewireless.org'); break; case 'most_category_views': $query = 'most'; $column = 'category_views'; $title = __('Category Views', 'guatewireless.org'); break; case 'most_single_views': $query = 'most'; $column = 'single_views'; $title = __('Permalink Views', 'guatewireless.org'); break; case 'most_comments': $query = 'most'; $column = 'comments'; $title = __('Comments', 'guatewireless.org'); break; case 'most_pingbacks': $query = 'most'; $column = 'pingbacks'; $title = __('Pingbacks', 'guatewireless.org'); break; case 'most_trackbacks': $query = 'most'; $column = 'trackbacks'; $title = __('Trackbacks', 'guatewireless.org'); break; case 'popular': $query = 'popular'; $column = 'total'; $title = __('Most Popular', 'guatewireless.org'); $list = ''; break; } if (!empty($query)) { switch ($query) { case 'most': $items = $wpdb->get_results(" SELECT p.ID AS ID, p.post_title AS post_title, pop.$column AS $column FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id WHERE p.post_status = 'publish' ORDER BY pop.$column DESC LIMIT $limit "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.$item->$column.' '.$item->post_title.'
  • '."\n"; } } break; case 'date': $items = $wpdb->get_results(" SELECT p.ID AS ID, p.post_title AS post_title, pop.total AS total FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id WHERE DATE_ADD(p.post_date, INTERVAL $days DAY) $compare DATE_ADD(NOW(), INTERVAL $offset DAY) AND p.post_status = 'publish' ORDER BY pop.total DESC LIMIT $limit "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.$this->get_post_rank(-1, $item->total).' '.$item->post_title.'
  • '."\n"; } } break; case 'popular': $items = $wpdb->get_results(" SELECT p.ID AS ID, p.post_title AS post_title, pop.$column AS $column FROM $wpdb->posts p LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id WHERE p.post_status = 'publish' ORDER BY pop.$column DESC LIMIT $limit "); $list = ''; if (count($items) > 0) { foreach ($items as $item) { $list .= '
  • '.$this->get_post_rank(-1, $item->total).' '.$item->post_title.'
  • '."\n"; } } break; } } } if (!empty($list)) { print('

    '.$title.'

      '.$list.'
    '); } } function show_report_extended($type = 'popular', $limit = 50) { global $wpdb, $post; $columns = array( 'popularity' => __('', 'guatewireless.org') ,'title' => __('Title') ,'categories' => __('Categories') ,'single_views' => __('Post', 'guatewireless.org') ,'category_views' => __('Cat', 'guatewireless.org') ,'archive_views' => __('Arch', 'guatewireless.org') ,'home_views' => __('Home', 'guatewireless.org') ,'feed_views' => __('Feed', 'guatewireless.org') ,'comments' => __('Com') ,'pingbacks' => __('Ping') ,'trackbacks' => __('Track') ); ?>

    '.__('Most Popular', 'guatewireless.org').'

    '); $this->show_report_extended('popular', 50); print('

    '.__('Views', 'guatewireless.org').'

    '); $this->show_report('most_single_views'); $this->show_report('most_category_views'); $this->show_report('most_archive_views'); print('
    '); $this->show_report('most_home_views'); $this->show_report('most_feed_views'); print('

    '.__('Feedback', 'guatewireless.org').'

    '); $this->show_report('most_comments'); $this->show_report('most_pingbacks'); $this->show_report('most_trackbacks'); print('
    '); $this->show_report('views_wo_feedback'); print('

    '.__('Averages', 'guatewireless.org').'

    '); $this->show_report('category_popularity'); $this->show_report('month_popularity'); print('

    '.__('Categories', 'guatewireless.org').'

    '); $this->show_report('pop_by_category'); print('

    '.__('Date Range', 'guatewireless.org').'

    '); $this->show_report('last_30'); $this->show_report('last_60'); $this->show_report('last_90'); print('
    '); $this->show_report('last_365'); $this->show_report('365_plus'); print('
    '); } function get_post_total($post_id) { if (!isset($this->current_posts['id_'.$post_id])) { $this->get_current_posts(); } return $this->current_posts['id_'.$post_id]; } function get_post_rank($post_id = -1, $total = -1) { if (count($this->top_ranked) == 0) { $this->get_top_ranked(); } if ($total > -1 && $post_id == -1) { return ceil(($total/$this->top_rank()) * 100).'%'; } if (isset($this->top_ranked['id_'.$post_id])) { $rank = $this->top_ranked['id_'.$post_id]; } else { $rank = $this->get_post_total($post_id); } if (AKPC_SHOWHELP == 1) { $suffix = ' [?]'; } else { $suffix = ''; } if (isset($rank) && $rank != false) { return __('Popularity:', 'guatewireless.org').' '.ceil(($rank/$this->top_rank()) * 100).'%'.$suffix; } else { return __('Popularity:', 'guatewireless.org').' '.__('unranked', 'guatewireless.org').$suffix; } } function show_post_rank($post_id = -1, $total = -1) { print($this->get_post_rank($post_id, $total)); } function top_rank() { if (count($this->top_ranked) == 0) { $this->get_top_ranked(); } foreach ($this->top_ranked as $id => $rank) { return $rank; } } function get_current_posts() { global $wpdb, $posts; if (!isset($posts) || count($posts) == 0) { return true; } $ids = array(); $ak_posts = $posts; foreach ($ak_posts as $post) { $ids[] = $post->ID; } if (count($ids) > 0) { $result = mysql_query(" SELECT post_id, total FROM $wpdb->ak_popularity WHERE post_id IN (".implode(',', $ids).") ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if ($result) { while ($data = mysql_fetch_object($result)) { $this->current_posts['id_'.$data->post_id] = $data->total; } } } return true; } function get_top_ranked() { global $wpdb; $result = mysql_query(" SELECT post_id, total FROM $wpdb->ak_popularity ORDER BY total DESC LIMIT 10 ", $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__); if (!$result) { return false; } while ($data = mysql_fetch_object($result)) { $this->top_ranked['id_'.$data->post_id] = $data->total; } return true; } function show_top_ranked($limit, $before, $after) { global $wpdb; $temp = $wpdb; $join = apply_filters('posts_join', ''); $where = apply_filters('posts_where', ''); $groupby = apply_filters('posts_groupby', ''); if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; } else { $groupby = ' GROUP BY '.$wpdb->posts.'.ID '; } $posts = $wpdb->get_results(" SELECT ID, post_title FROM $wpdb->posts LEFT JOIN $wpdb->ak_popularity pop ON $wpdb->posts.ID = pop.post_id $join WHERE post_status = 'publish' AND post_date < NOW() $where $groupby ORDER BY pop.total DESC LIMIT ".intval($limit) ); if ($posts) { foreach ($posts as $post) { print( $before.'' .$post->post_title.''.$after ); } } else { print($before.'(none)'.$after); } $wpdb = $temp; } function show_top_ranked_in_cat($limit, $before, $after, $cat_ID = '') { if (empty($cat_ID) && is_category()) { global $cat; $cat_ID = $cat; } if (empty($cat_ID)) { return; } global $wpdb; $temp = $wpdb; $join = apply_filters('posts_join', ''); $where = apply_filters('posts_where', ''); $groupby = apply_filters('posts_groupby', ''); if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; } else { $groupby = ' GROUP BY p.ID '; } $posts = $wpdb->get_results(" SELECT ID, post_title FROM $wpdb->posts p LEFT JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id LEFT JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN $wpdb->ak_popularity pop ON p.ID = pop.post_id $join WHERE tt.term_id = '".intval($cat_ID)."' AND tt.taxonomy = 'category' AND post_status = 'publish' AND post_type = 'post' AND post_date < NOW() $where $groupby ORDER BY pop.total DESC LIMIT ".intval($limit) ); if ($posts) { foreach ($posts as $post) { print( $before.'' .$post->post_title.''.$after ); } } else { print($before.'(none)'.$after); } $wpdb = $temp; } function show_top_ranked_in_month($limit, $before, $after, $m = '') { if (empty($m) && is_archive()) { global $m; } if (empty($m)) { global $post; $m = get_the_time('Ym'); } if (empty($m)) { return; } $year = substr($m, 0, 4); $month = substr($m, 4, 2); global $wpdb; $temp = $wpdb; $join = apply_filters('posts_join', ''); $where = apply_filters('posts_where', ''); $groupby = apply_filters('posts_groupby', ''); if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; } else { $groupby = ' GROUP BY '.$wpdb->posts.'.ID '; } $posts = $wpdb->get_results(" SELECT ID, post_title FROM $wpdb->posts LEFT JOIN $wpdb->ak_popularity pop ON $wpdb->posts.ID = pop.post_id $join WHERE YEAR(post_date) = '$year' AND MONTH(post_date) = '$month' AND post_status = 'publish' AND post_date < NOW() $where $groupby ORDER BY pop.total DESC LIMIT ".intval($limit) ); if ($posts) { foreach ($posts as $post) { print( $before.'' .$post->post_title.''.$after ); } } else { print($before.'(none)'.$after); } $wpdb = $temp; } //******************************** AGREGADO ********************************** function show_top_ranked_in_last_month($limit, $before, $after) { global $wpdb; $temp = $wpdb; $join = apply_filters('posts_join', ''); $where = apply_filters('posts_where', ''); $groupby = apply_filters('posts_groupby', ''); if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; } $posts = $wpdb->get_results(" SELECT ID, post_title FROM $wpdb->posts LEFT JOIN $wpdb->ak_popularity pop ON $wpdb->posts.ID = pop.post_id $join WHERE post_date > (SUBDATE(CURDATE(), INTERVAL '30' DAY)) AND post_status = 'publish' AND post_date < NOW() $where $groupby ORDER BY pop.total DESC LIMIT ".intval($limit) ); if ($posts) { foreach ($posts as $post) { print( $before.'' .$post->post_title.''.$after ); } } else { print($before.'(none)'.$after); } $wpdb = $temp; } //******************************** AGREGADO ********************************** } // -- "HOOKABLE" FUNCTIONS function akpc_init() { global $wpdb, $akpc; $wpdb->ak_popularity = $wpdb->prefix.'ak_popularity'; $wpdb->ak_popularity_options = $wpdb->prefix.'ak_popularity_options'; $akpc = new ak_popularity_contest; // CHECK FOR POPULARITY TABLES /* if (isset($_GET['activate']) && $_GET['activate'] == 'true') { $result = mysql_list_tables(DB_NAME); $tables = array(); while ($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } if (!in_array($wpdb->ak_popularity, $tables) && !in_array($wpdb->ak_popularity_options, $tables)) { $akpc->install(); } else { $akpc->get_settings(); $akpc->mine_gap_data(); } }*/ $akpc->install(); $akpc->get_settings(); } function akpc_view($content) { global $akpc; $akpc->record_view(); return $content; } function akpc_feedback_comment() { global $akpc; $akpc->record_feedback('comment'); } function akpc_comment_status($comment_id) { global $akpc; $akpc->edit_feedback($comment_id, 'status'); } function akpc_comment_delete($comment_id) { global $akpc; $akpc->edit_feedback($comment_id, 'delete'); } function akpc_feedback_pingback() { global $akpc; $akpc->record_feedback('pingback'); } function akpc_feedback_trackback() { global $akpc; $akpc->record_feedback('trackback'); } function akpc_publish($post_ID) { global $akpc; $akpc->create_post_record($post_ID); } function akpc_post_delete($post_ID) { global $akpc; $akpc->delete_post_record($post_ID); } function akpc_options_form() { global $akpc; $akpc->options_form(); } function akpc_view_stats() { global $akpc; $akpc->view_stats(); } function akpc_options() { if (function_exists('add_options_page')) { add_options_page( __('Popularity Contest Options', 'guatewireless.org') , __('Popularity', 'guatewireless.org') , 10 , basename(__FILE__) , 'akpc_options_form' ); } if (function_exists('add_submenu_page')) { add_submenu_page( 'index.php' , __('Most Popular Posts', 'guatewireless.org') , __('Most Popular Posts', 'guatewireless.org') , 0 , basename(__FILE__) , 'akpc_view_stats' ); } } function akpc_options_css() { print(''); } // -- TEMPLATE FUNCTIONS function akpc_the_popularity() { global $akpc, $post; $akpc->show_post_rank($post->ID); } function akpc_most_popular($limit = 10, $before = '
  • ', $after = '

  • ') { global $akpc; $akpc->show_top_ranked($limit, $before, $after); } function akpc_most_popular_in_cat($limit = 10, $before = '
  • ', $after = '
  • ', $cat_ID = '') { global $akpc; $akpc->show_top_ranked_in_cat($limit, $before, $after, $cat_ID); } function akpc_most_popular_in_month($limit = 10, $before = '
  • ', $after = '
  • ', $m = '') { global $akpc; $akpc->show_top_ranked_in_month($limit, $before, $after, $m); } function akpc_most_popular_in_last_month($limit = 10, $before = '
  • ', $after = '

  • ') { global $akpc; $akpc->show_top_ranked_in_last_month($limit, $before, $after); } function akpc_content_pop($str) { global $akpc, $post; $show = true; $show = apply_filters('akpc_display_popularity', $show, $post); if (is_feed() || is_admin_page() || get_post_meta($post->ID, 'hide_popularity', true) || !$show) { return $str; } return $str.'

    '.$akpc->get_post_rank($post->ID).'

    '; } // -- HANDLE ACTIONS if (!empty($_POST['ak_action'])) { switch($_POST['ak_action']) { case 'update_popularity_values': akpc_init(); $akpc = new ak_popularity_contest; $akpc->get_settings(); $akpc->update_settings(); break; } } if (!empty($_GET['ak_action'])) { switch($_GET['ak_action']) { case 'recount_feedback': akpc_init(); $akpc = new ak_popularity_contest; $akpc->get_settings(); $akpc->recount_feedback(); break; case 'css': header("Content-type: text/css"); ?> #akpc_most_popular { height: 250px; overflow: auto; margin-bottom: 10px; } #akpc_most_popular td.right, #akpc_options_link { text-align: right; } #akpc_most_popular td a { border: 0; } .akpc_report { float: left; margin: 5px 30px 20px 0; width: 200px; } .akpc_report h3 { border-bottom: 1px solid #999; color #333; margin: 0 0 4px 0; padding: 0 0 2px 0; } .akpc_report ol { margin: 0 0 0 20px; padding: 0; } .akpc_report ol li span { float: right; } .akpc_report ol li a { border: 0; display: block; margin: 0 30px 0 0; } .clear { clear: both; float: none; } #akpc_template_tags dl { margin-left: 10px; } #akpc_template_tags dl dt { font-weight: bold; margin: 0 0 5px 0; } #akpc_template_tags dl dd { margin: 0 0 15px 0; padding: 0 0 0 15px; }