teastudio

WP_Posts_Carousel_Popular_Posts_Query

May 11th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2. /*
  3. Author: Marcin Gierada
  4. Author URI: http://www.teastudio.pl/
  5. Author Email: [email protected]
  6. License: GPLv2 or later
  7. License URI: http://www.gnu.org/licenses/gpl-2.0.html
  8. */
  9.  
  10. if ( ! defined( 'ABSPATH' ) ) {
  11.     exit; // Exit if accessed directly
  12. }
  13.  
  14. /*
  15.  * class to get popular posts from Wordpress Popular Posts plugin
  16.  */
  17. class WP_Posts_Carousel_Popular_Posts_Query extends WP_Query {
  18.     private $args;
  19.     private $params;
  20.     private $interval;
  21.  
  22.     public function __construct( $args, $params ) {
  23.         $this->args = $args;
  24.         $this->params = $params;
  25.  
  26.         add_filter( 'posts_fields', array( $this, 'posts_fields') );
  27.         add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ) );
  28.         add_filter( 'posts_join_paged', array( $this, 'posts_join_paged') );
  29.         add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) );
  30.         add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) );
  31.  
  32.         parent::__construct( $this->args );
  33.  
  34.         remove_filter( 'posts_fields', array( $this, 'posts_fields' ) );
  35.         remove_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ) );
  36.         remove_filter( 'posts_join_paged', array( $this, 'posts_join_paged' ) );
  37.         remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) );
  38.         remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) );
  39.     }
  40.  
  41.     public function posts_fields( $sql ) {
  42.         return $sql . ', SUM(p.pageviews) AS views';
  43.     }
  44.  
  45.     public function posts_where_paged( $sql ) {
  46.         $this->interval = apply_filters('wpc_query_popular_posts_interval', '1 DAY', array(
  47.             'params' => $this->params
  48.         ) );
  49.  
  50.         if ( $this->interval !== NULL ) {
  51.             return $sql . ' AND post_date > DATE_SUB("'. current_time('mysql') .'", INTERVAL '. $this->interval .')';
  52.         } else {
  53.             return $sql;
  54.         }
  55.  
  56.     }
  57.  
  58.     public function posts_join_paged( $sql ) {
  59.         global $wpdb;
  60.  
  61.         if ( $this->interval !== NULL ) {
  62.             return $sql . 'JOIN '. $wpdb->prefix .'popularpostssummary as p ON (p.postid = '. $wpdb->prefix .'posts.ID AND p.last_viewed > DATE_SUB("'. current_time('mysql') .'", INTERVAL '. $this->interval .'))';
  63.         } else {
  64.             return $sql . 'JOIN '. $wpdb->prefix .'popularpostssummary as p ON (p.postid = '. $wpdb->prefix .'posts.ID)';
  65.         }
  66.     }
  67.  
  68.     public function posts_groupby( $sql ) {
  69.         return $sql . 'ID';
  70.     }
  71.  
  72.     public function posts_orderby( $sql ) {
  73.         return 'views ' . $this->params['ordering'];
  74.     }
  75. }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment