Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Author: Marcin Gierada
- Author URI: http://www.teastudio.pl/
- Author Email: [email protected]
- License: GPLv2 or later
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly
- }
- /*
- * class to get popular posts from Wordpress Popular Posts plugin
- */
- class WP_Posts_Carousel_Popular_Posts_Query extends WP_Query {
- private $args;
- private $params;
- private $interval;
- public function __construct( $args, $params ) {
- $this->args = $args;
- $this->params = $params;
- add_filter( 'posts_fields', array( $this, 'posts_fields') );
- add_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ) );
- add_filter( 'posts_join_paged', array( $this, 'posts_join_paged') );
- add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) );
- add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) );
- parent::__construct( $this->args );
- remove_filter( 'posts_fields', array( $this, 'posts_fields' ) );
- remove_filter( 'posts_where_paged', array( $this, 'posts_where_paged' ) );
- remove_filter( 'posts_join_paged', array( $this, 'posts_join_paged' ) );
- remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) );
- remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) );
- }
- public function posts_fields( $sql ) {
- return $sql . ', SUM(p.pageviews) AS views';
- }
- public function posts_where_paged( $sql ) {
- $this->interval = apply_filters('wpc_query_popular_posts_interval', '1 DAY', array(
- 'params' => $this->params
- ) );
- if ( $this->interval !== NULL ) {
- return $sql . ' AND post_date > DATE_SUB("'. current_time('mysql') .'", INTERVAL '. $this->interval .')';
- } else {
- return $sql;
- }
- }
- public function posts_join_paged( $sql ) {
- global $wpdb;
- if ( $this->interval !== NULL ) {
- 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 .'))';
- } else {
- return $sql . 'JOIN '. $wpdb->prefix .'popularpostssummary as p ON (p.postid = '. $wpdb->prefix .'posts.ID)';
- }
- }
- public function posts_groupby( $sql ) {
- return $sql . 'ID';
- }
- public function posts_orderby( $sql ) {
- return 'views ' . $this->params['ordering'];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment