Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
T5 Random Posts Endpoint
<?php # -*- coding: utf-8 -*-
/*
* Plugin Name: T5 Random Posts
* Description: Display a list of random posts.
* Version: 2012.07.02
* Author: Thomas Scholz <info@toscho.de>
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*/
// refresh permalink settings
register_activation_hook(
__FILE__ ,
array ( 'T5_Random_Posts_Endpoint', 'flush_rewrite_rules' )
);
register_deactivation_hook(
__FILE__ ,
array ( 'T5_Random_Posts_Endpoint', 'flush_rewrite_rules' )
);
if ( ! class_exists( 'T5_Random_Posts_Endpoint' ) )
{
add_action( 'init', array ( 'T5_Random_Posts_Endpoint', 'init' ) );
/**
* Build a root endpoint.
*/
class T5_Random_Posts_Endpoint
{
/**
* Register endpoint and actions.
*
* @wp-hook 'init'
* @return void
*/
public static function init()
{
add_rewrite_endpoint( 'randomposts', EP_ROOT );
add_filter( 'request', array ( __CLASS__, 'filter_request' ) );
add_action( 'template_redirect', array ( __CLASS__, 'render' ) );
}
/**
* Prepare the endpoint variable so it has always a value.
*
* @wp-hook 'request'
* @param array $request
* @return array
*/
public static function filter_request( $request )
{
if ( isset ( $request['randomposts'] )
and empty ( $request['randomposts'] )
)
{
$request['randomposts'] = 'html';
}
return $request;
}
/**
* Create output.
*
* @wp-hook 'template_redirect'
* @return void
*/
public static function render()
{
// This is not our endpoint.
if ( '' === $type = get_query_var( 'randomposts' ) )
{
return;
}
// Someone is poking around.
// You can extend this and build different output variants.
if ( ! in_array( $type, array ( 'html', 'json' ) ) )
{
status_header( 414 );
print 'Unsupported Media Type. Use "html" or "json" only.';
exit;
}
// Empty blog?
if ( ! $posts = get_posts( array ( 'numberposts' => 10, 'orderby' => 'rand' ) ) )
{
status_header( 404 );
print 'No posts found.';
exit;
}
self::$type( self::prepare_post_data( $posts ) );
exit;
}
/**
* Build a simple array with just titles and permalinks.
*
* @wp-hook 'template_redirect'
* @param array $posts
* @return void
*/
protected static function prepare_post_data( $posts )
{
$data = array ();
foreach ( $posts as $post )
{
if ( empty ( $post->post_title) )
{
continue;
}
$data[ $post->ID ] = array (
'title' => strip_tags( $post->post_title ),
'url' => get_permalink( $post->ID )
);
}
return $data;
}
/**
* Render JSON output
*
* @wp-hook 'template_redirect'
* @param array $data
* @return void
*/
protected static function json( array $data )
{
header( 'Content-type: application/json' );
print json_encode( $data );
}
/**
* Render HTML output
*
* @wp-hook 'template_redirect'
* @param array $data
* @return void
*/
protected static function html( array $data )
{
?>
<!doctype html>
<title><?php bloginfo( 'name' ); ?></title>
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<?php
// Just a sample style. Be creative! :)
?>
<style>
body, body *
{
display: block;
font: 1em/1.4 Calibri, sans-serif;
list-style: none;
margin: 0;
padding: 0;
text-decoration: none;
}
a
{
background: #f5f5f5;
border-bottom: 1px solid #ddd;
color: #333;
padding: 5px 10px;
}
a:hover, a:focus
{
background: #333;
color: #f5f5f5;
}
h1 a
{
font-size: 1.2em;
font-weight: bold;
}
</style>
<h1><a href="<?php bloginfo( 'url' ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<ul>
<?php
foreach ( $data as $post )
{
print '<li><a href="' . $post['url'] . '">' . $post['title'] . '</a></li>';
}
?>
</ul>
<?php
}
/**
* Reset permalinks.
*
* @wp-hook 'activate_' . __FILE__
* @wp-hook 'deactivate_' . __FILE__
* @return void
*/
public static function flush_rewrite_rules()
{
remove_action( 'init', array ( __CLASS__, 'init' ) );
add_action( 'init', 'flush_rewrite_rules', 11 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.