Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrankM1/feec0629794d841f018eb2d07617d193 to your computer and use it in GitHub Desktop.
Save FrankM1/feec0629794d841f018eb2d07617d193 to your computer and use it in GitHub Desktop.
<?php
/**
* Radium Theme Query Class
*
* This is used to create cacheable query to boost performance
*
* @since 1.0.0
*
* @category Radium Framework
* @package NewsFront WP
* @author Franklin M Gitonga
* @link http://radiumthemes.com/
*/
class Radium_Theme_WP_Query {
/**
* Holds a copy of the object for easy reference.
*
* @since 1.0.0
*
* @var object
*/
private static $instance;
/**
* __construct initialize the class and hook into wordpress
*
* @return null
*/
public function __construct() {
}
/**
* Return a cached query
*
* @param string $name_the_transient name of transient key
* @param array $args query arguments
* @param integer $expires transient expiration in seconds
* @return Object the cached object
*/
public static function cache_query( $args = array(), $name_the_transient, $expires = 600 ) {
$expires = apply_filters('radium_cache_query_time', radium_get_option('query_transient_cache_time', false, $expires), $name_the_transient);
$args['suppress_filters'] = false;
if( $expires == 0 || ! radium_get_option('query_transient') ) {
$query = new WP_Query( $args );
} else {
if ( class_exists('SitePress') ) {
global $sitepress;
$current_lang = $sitepress->get_current_language();
$name_the_transient = $current_lang ? $name_the_transient . $current_lang : $name_the_transient;
}
if ( false == get_transient($name_the_transient) ) {
$query = new WP_Query( $args );
set_transient( $name_the_transient, $query, $expires );
} else {
$query = get_transient($name_the_transient);
}
}
return $query;
}
/**
* Wrapper around get_posts that utilizes object caching
*
* @access public
* @param mixed $args (default: NUL)
* @param bool $force_refresh (default: false)
* @return void
*/
public static function get_posts_cached( $args = array(), $name_the_transient, $expires = 600 ) {
$expires = apply_filters('radium_get_posts_cached_time', radium_get_option('query_transient_cache_time', false, $expires), $name_the_transient);
$args['suppress_filters'] = false;
if( $expires == 0 || !radium_get_option('query_transient') ) {
$posts = get_posts( $args );
} else {
if ( false == get_transient($name_the_transient) ) {
$posts = get_posts( $args );
set_transient( $name_the_transient, $posts, $expires );
} else {
$posts = get_transient($name_the_transient);
}
}
return $posts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment