@vikramjb
Thank you. I think I've figured out how to slay this beast, and it hit me because of your reply.
First of all, regarding Cron:
Cron is amazing. I use it daily on my own system (Ubuntu Linux 9.04). The problem with Cron however, is it is only available on *nix systems (Linux, Solaris, BSD, Mac OS X, etc...). Windows does have a similar tool (called Scheduled Tasks or something), but it isn't exactly Cron.
In my perfect world, I'd wake up tomorrow and everyone would be running Linux or Mac and Windows would cease to exist. Sadly, this is not reality.
Now what Wordpress does is attempts to use Cron. First, it looks to see if Cron is available. Next it tries executing the Cron job and then waits for a response. If it worked, great. If not, then it starts from the top and tries a new strategy.
The thing is, even if Cron is installed on a server, and if the server's PHP settings allow execution of command-line scripts, it's still very possible that the hosting provider has disabled user-level command execution (often the case on shared hosting accounts).
As I mentioned in my previous post, this is my problem with other CMS's. They allow for too many 'ifs.' When time could have been spent just doing the job directly, it was wasted trying to do it a certain obscure (albeit very efficient) way.
Now on to your second method...
Replying to you in my last post I said:
The only 'practical' method I've devised (for my purposes at least) is to do a quick check for posts that need to be published when a user performs some arbitrary action (i.e. logging in). This wouldn't be elegant or precise, but it would work.
I probably wasn't very clear about what I meant, but it's EXACTLY what you suggested. I'm just happy you could make what I was thinking clearer in my own head. Kudos.
WARNING: The rest of what I'm about to write is probably really, really boring, but I can see the solution clearly right now and I think typing it out would solidify it for me.
Your example query is pretty much exactly what I'll be doing. The issue I was having was that since HTML is a 'stateless/static' medium, something needs to happen in order for the query to be executed.
When I was reading your suggestion, that 'something' hit me.
Here it goes:
In my system, there are 4 distinct kinds of pages - mainPages, subPages, entries, and modules. Each of them serves a different purpose (or more accurately, purposes), but they are all displayed to the screen in the same way. Template designers simply echo out a method(function) called 'displayContent()' from an object called '$content' ($content->displayContent()).
What that jargon means is that EVERY time a viewable page is displayed, this function is executed. What I'm going to do is sneak a 'pseudo-Cron' command inside this function. Take this code snippet:
PHP Code:
function oddsAre($odds) {
$num = intval($odds);
if ((!is_int($num)) || ($num <= 0) || ($num > 100)) {
trigger_error("Function 'oddsAre()' only accepts whole numbers between 1 and 100.", E_USER_ERROR);
return false;
}
$spin = mt_rand(1,100);
if ($spin <= $odds) {
return true;
} else {
return false;
}
}
What this does is takes the number(odds/probability) you give and returns either true or false based on your number. The closer your number to 100, the better the odds of returning true. What does this have to do with what I was talking about?
Well, since $content->displayContent() is called on every page, I can simply stick this inside the displayContent() function:
PHP Code:
if (oddsAre(33)) {
$this->doScheduledTasks() // <-- another function that 'does things'
}
What this means is that roughly 1/3 of the time any 'content' is displayed, the system will do things like check for posts that need published, ones that have expired, and user sessions that have expired and handle them accordingly.
It won't happen every time, so performance won't be hurt too bad, but it will happen enough to be efficient. Likewise, webmasters will be able to tweak this number to their own liking.
Alright... I'm done now. If you read that all, well you have a greater attention span than I.
I'm not exactly sure how you did it, but your post got my brain firing!
Cheers.