Problem-Solving for Fun and (Meager) Profit

Doing freelance work for a living presents the opportunity to tackle a wide range of problems. One recent gig called for me to figure out a way to protect website content for a limited period of time, each article becoming accessible to the general public at exactly 6:00 a.m. after it’s posted. That way, paying subscribers have a limited window in which to use content themselves while its timeliness still holds premium value, but the site overall contains a wealth of content to attract everyone else.

It’s easy enough to protect content in WordPress, in any number of ways, so it’s only available to users of your choosing. It only took minutes to figure out how to set an expiration duration for that protection — a piece designated as being in the “Subscribers” category becomes openly accessible, say, 24 hours after being posted. But the details of this challenge initially had me stumped. How to set the exact same expiration time for protection on all new “Subscribers” content, regardless of when during the prior 24 hours it was posted? As far as I could tell, no existing plugin performs this function.

My eventual solution was to call the current time into a variable, then set up an obscene array of variations on it, truncating and adding to some, and transforming some of those modifications back and forth between date strings and Unix timestamps, until I could create the right set of conditional statements using mathematically comparable timestamps.

Here’s the result as applied to the site theme’s single.php template:

$post = $wp_query->post;

$todaysdateandtime = current_time('mysql');
$todaysdate = substr($todaysdateandtime,0,10);
$currenttimestamp = current_time('timestamp');
$yesterdaystimestamp = strtotime('-1 day', $currenttimestamp);
$yesterdaysdateandtime = date("c", $yesterdaystimestamp);
$yesterdaysdate = substr($yesterdaysdateandtime,0,10);
$yesterdayscutofftime = $yesterdaysdate . ' 06:00';
$yesterdayscutofftimestamp = strtotime($yesterdayscutofftime);
$tomorrowstimestamp = strtotime('+1 day', $currenttimestamp);
$tomorrowsdateandtime = date("c", $tomorrowstimestamp);
$tomorrowsdate = substr($tomorrowsdateandtime,0,10);
$tomorrowscutofftime = $tomorrowsdate . ' 06:00';
$tomorrowscutofftimestamp = strtotime($tomorrowscutofftime);
$articletimestamp = get_the_time('U');
$todayscutofftime = $todaysdate . ' 06:00';
$todayscutofftimestamp = strtotime($todayscutofftime);
$catmarker = 0;
$timemarker = 0;

foreach((get_the_category()) as $checkcat) { if ($checkcat->cat_name == 'Subscribers') { $catmarker = 1; } }

if ($articletimestamp > $yesterdayscutofftimestamp && $currenttimestamp < $todayscutofftimestamp) { $timemarker = 1; }

if ($articletimestamp > $todayscutofftimestamp && $currenttimestamp < $tomorrowscutofftimestamp) { $timemarker = 1; }

if (current_user_can('read_private_posts')) {

     include ('protected-readable.php');

} else {

     if ($catmarker == 1 && $timemarker == 1) {

          include ('protected-hidden.php');

     } else {

          include ('protected-readable.php');


} ?>

It works like a charm.

Tweaked variants of this code are also applied to content loops on the front page and the RSS template, so that notice of protected content also doesn't reach non-subscribers, except in specific chosen circumstances.

I wouldn't be surprised to find that there's a much more elegant way to do this. It would also be useful to turn this functionality into an actual plugin, so that the template files themselves can remain untouched. But it was a pretty satisfying exercise to figure out, step by step, how to make this work correctly.

[Cross-posted to Shrubbloggers.]