Last active
January 23, 2018 12:45
-
-
Save pixeline/1ab9b46424218b184e64f2c26d164cc4 to your computer and use it in GitHub Desktop.
An interface to the wordpress-popular-posts WordPress plugin that returns the raw data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Popular_Posts_Data | |
* | |
* A class for retrieving an array of popular posts post data. This class is designed for use with the WordPress Popular | |
* Posts plugin (@see https://wordpress.org/plugins/wordpress-popular-posts/) and will not work without it. | |
* | |
* @source https://philsmart.me/wordpress-popular-posts-plugin-raw-data-array/ | |
* @author Phil Smart Alexandre Plennevaux | |
* | |
* Example Usage: | |
* | |
* $popular_blog_posts = new Popular_Posts_Data( | |
* array( | |
* 'range' => 'monthly', | |
* 'freshness' => 1, | |
* 'orderby' => 'views', | |
* 'limit' => 9, | |
* 'post_type' => 'post,page' | |
* ) ); | |
* | |
* print_r($popular_blog_posts); | |
* | |
*/ | |
class Popular_Posts_Data | |
{ | |
/** @var array $items Property for storing the final data array */ | |
protected $items = array(); | |
/** | |
* Constructor. This accepts either an integer or an array of query args suitable for the wpp_get_mostpopular() | |
* function. If an int is passed, it is treated as the number of posts to get. If an array is passed, it is | |
* treated as a plugin-specific query array. | |
* | |
* @param int|array $args | |
*/ | |
function __construct( $args = 10 ) | |
{ | |
// Make sure the function is available | |
if ( ! function_exists( 'wpp_get_mostpopular' )) | |
return; | |
// Catch the output of the function to prevent it from displaying and capture the data via the filter | |
add_filter( 'wpp_query_posts', array( $this, '_get_posts_data' ), 10, 2 ); | |
// Ensure our arguments are of a suitable format | |
$args = $this->normalise_args( $args ); | |
// Invoke the API method using our arguments array, and catch the output in the output buffer | |
ob_start(); | |
wpp_get_mostpopular( $args ); | |
ob_get_clean(); | |
// Remove the filter so we don't affect other instances of the shortcode | |
remove_filter( 'wpp_query_posts', array( $this, '_get_posts_data' ), 10, 2 ); | |
} | |
/** | |
* Normalises args for the wpp_get_mostpopular() function. This ensures we pass the function a correctly formatted | |
* args array, but also allows us to define some default arguments specific to our needs. | |
* | |
* @param int|array $args | |
* | |
* @return array | |
*/ | |
protected function normalise_args( $args ) | |
{ | |
// If an integer is passed, treat it as a limit | |
if (is_int( $args )) | |
$args = array( 'limit' => $args ); | |
// Allows passing of an array of categories by reformatting into a string | |
if (isset( $args['cat'] ) and is_array( $args['cat'] )) | |
$args['cat'] = implode( ',', $args['cat'] ); | |
// Merge parameters with defaults. Set whatever defaults you like here. | |
return wp_parse_args( $args, array( | |
'range' => 'weekly', | |
'freshness' => 1, | |
'orderby' => 'views', | |
'limit' => 10, | |
'post_type' => 'post' | |
) ); | |
} | |
/** | |
* The hooked filter that intercepts the shortcode output and receives a list of posts. | |
* | |
* @param $data | |
* @param $instance | |
*/ | |
public function _get_posts_data( $data, $instance ) | |
{ | |
$this->items = (array) $data; | |
} | |
/** | |
* Public accessor for posts array. | |
* | |
* @return array | |
*/ | |
public function get_posts() | |
{ | |
return $this->items; | |
} | |
public function get_posts_as_array() | |
{ | |
return (array) $this->items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment