<?php

/** 
 *	Last Post by Author
 *
 *	Must be used in conjunction with the User Access Expiration Plugin
 *	http://wordpress.org/extend/plugins/user-access-expiration/
 *	
 *	Get each users's most recent post as determined by post date. 
 *	Disable the user's access if his/her last post is more than a certain
 *	number of days older than the current date. As long as the user adds 
 *	a new post before the max days required his/her access will not expire.
 *
 *	For example, if Bob last posted on October 01 and the days required
 *	to keep posting is 5 then on Otober 06 Bob's access will be disabled 
 *	or expired. If however, Bob adds a new post on October 05 he will not 
 *	be disabled until October 10.
 *
 *	@author		Nate Jacobs
 *	@since		0.1
 *	@link		http://gist.github.com/1258547
 */
// not the best action. Really should be a cron job that runs once a day.
add_action( 'admin_init', 'uae_last_post' );
function uae_last_post()
{
	/* Start User Configurable Values */
	// how many days does an author have to create a new post until they are disabled
	$days = 10;
	/* End User Configurable Values */
	
	// declare global $wpdb
	global $wpdb;
	
	// get the results of the query and put the object returned into the variable $posts
	// in plain english: get the most recent post for each author
	$posts = $wpdb->get_results(
		"SELECT post_author, ID, post_date FROM (	SELECT * FROM $wpdb->posts ORDER BY post_date DESC ) 
		X WHERE post_status='publish' AND post_type='post' GROUP BY post_author ORDER BY ID DESC"
	);
	
	// loop through each returned post
	foreach ( $posts as $post )
	{
		// get today's date in unix timestamp format
		$today = strtotime( date( 'Y-m-d' ) );
		// get the expiration date in unix timestamp format
		// the date is X number of days from post date where X is set in the $days variable above
		$expire = strtotime( '+'. $days .'days', strtotime( $post->post_date ) );
		// is the expiration date older than today? 
		// if so, disable the user's access
		if ( $expire < $today )
		{
			update_user_meta( $post->post_author, 'uae_user_access_expired', 'true' );	
		}
	}
}
?>