If you've ever built a WP theme, then you may be familiar with a really bad design decision they made. Their functions to retrieve content are inconsistent in approach, but worse - they don't return a value, they echo (whatever they are supposed to be retrieving) directly out towards the surfer. Now in most cases, this is probably not a horrible thing. But for me, where all sites are flowing into a single main.php file and nothing gets back out to the user until all objects in the processing chain have had
their say, this is a show stopper.
This has not been a huge issue, but has recently reared it's ugly head in that several of my clients want to integrate a blog (or at least an article publishing blog with no comments) right into their existing website. Now, either this means that the website itself needs to be completely changed as to how its contructed (I think not: I'll not give up all the capabilities I've built in by going this direction for a damn article system) or WP needs to be redone.
The part I really like about WP is the editing console or back office. Great stuff. And the fact that it's all DB driven is equally good. So what I've decided to do is create a wpConnection class that will give me access to the WP maintained database in a programmatic way that handles what I want to do. My preliminary thoughts have satisfied the requirements of what I need to get done, but on reflection, I thought about a couple other uses. For example: what if my retail shops' inventory were listed as "posts" in the WP database - then I could use the WP comment system for user feedback on particular items as an almost plug-and-play surfer content generation system. And of course, all of the "Approve before posting" WP stuff still applies, so I could very easily either let the website control people approve/disaprove comments via the WP backend, or add a little mini-control center in the backoffice that I supply to clients.
So, what I am presenting here is the outline of the class as I currently see it and would like feedback. I will post the entire thing publicly for use by everyone, so I'd like to get comments and ideas on how the class can be made stronger and more robust. I am trying to satisfy several different programmatic methods with it, yet keep the function count to a reasonable level. So without further ado, here are my thoughts.
Object Creation $wp = new wpConnection($dbHost, $dbUserName, $dbPassword, $dbDatabase, $wpTablePrefix)
// returns either an object handle or false
Syntax & Structure $wp->open[Name] - load a single (name) by id into the member object
eg., $wp->openPost($id), $wp->openComment($id)
$wp->get[Name]By[Filter]($data...) Return an array pointer to the set that is identical to an iterators values
eg., $wp->openPostsByDate([TRUE/false, decending] [, $count])
$wp->iterate[Name] - start a simple iterator (category, users)
eg., $wp->iterateCategories()
$wp->iterate[Name]By[Filter] - start a specialized iterator (posts by category)
eg., $wp->iteratePostsByDate([TRUE/false decending] [, $count])
$wp->load[Name] - load the next member record in the iteration set with the object from the database
eg., $wp->loadPost() - load a post from the current iterator into the member record
Data Members Data members will be listed as members of a member on the object, appropriate to their kind.
Data members will be a direct map to the actual fields in the database.
eg., $wp->post->id, $wp->comment->comment_author_ip, $wp->post->content
How iterators will work Similar to database constructs. Start an iterator handle then load until false.
$handle = $wp->iteratePostsByDate(true, 5); // get 10 most recent posts
while ($wp->loadPost($handle))
{
// At this point, $wp->content property is filled from the actual database record
// and could be used like this:
$buff = <<<HTML
<div class="articleTitle">{$wp->post->title}</div>
<div class="articleContent">{$wp->post->content}</div>
HTML;
}
Obviously, when the iterator is out of data it will return false at the loadPost function.
Most of what I'm currently thinking
$wp = new wpConnection($dbHost, $dbUserName, $dbPassword, $dbDatabase, $wpTablePrefix)
// Load the data members directly from an id
$wp->openPost($id)
$wp->openComment($id)
$wp->openCategory($id)
$wp->openLink($id)
$wp->openAuthor($id)
$wp->openUser($id)
// simple lists
$array = $wp->getAuthors()
$array = $wp->getLinks()
$array = $wp->getCategories()
$array = $wp->getUsers()
// complicated lists
$array = $wp->getPostsByDate([TRUE/false decending] [, $count])
$array = $wp->getPostsByRange($youngestDate, $oldestDate)
$array = $wp->getPostsByAuthor($authorID [,$count])
$array = $wp->getPostsByCategory($categoryID [, $count])
$array = $wp->getUnpublished()
$array = $wp->getCommentsByDate([TRUE/false decending] [, $count])
$array = $wp->getCommentsByPost($postID [,$count])
$array = $wp->getCommentsByUser($userID [,$count])
$array = $wp->getUnapproved()
// create an interator instance
$handle = $wp->iteratePostsByDate([TRUE/false decending] [, $count])
$handle = $wp->iteratePostsByRange($youngestDate, $oldestDate)
$handle = $wp->iteratePostsByAuthor($authorID [,$count])
$handle = $wp->iteratePostsByCategory($categoryID [, $count])
$handle = $wp->iterateUnpublished()
$handle = $wp->iterateCommentsByDate([TRUE/false decending] [, $count])
$handle = $wp->iterateCommentsByPost($postID [,$count])
$handle = $wp->iterateCommentsByUser($userID [,$count])
$handle = $wp->iterateUnapproved()
// load data members from an interator
$wp->loadAuthor($handle)
$wp->loadLink($handle)
$wp->loadCategory($handle)
$wp->loadUser($handle)
$wp->loadPost($handle)
$wp->loadComment($handle)
// CRUD functions
$wp->newPost($authorID, $title, $content [,$postDate]) // this will probably have more params
$wp->updatePost($postID, $title, $content)
$wp->deletePost($postID)
$wp->publishPost($postID)
$wp->newComment($userID, $postID, $content)
$wp->updateComment($commentID, $content)
$wp->deleteComment($commentID)
$wp->approveComment($commentID)
$wp->spamComment($commentID)
// Data Members
$wp->post
$wp->comment
$wp->link
$wp->author
$wp->user
$wp->category
Righty then - what am I missing, what else would be strong to add?