The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. October 14, 2019, 06:52:46 PM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: PHP WordPress Class idea... comments please  (Read 6760 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: August 06, 2009, 09:25:46 AM »

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?
« Last Edit: August 06, 2009, 09:38:28 AM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: August 06, 2009, 09:38:03 AM »

Oh, sorry - here's a little example of how the code might be used:

Code:
<?php
$wp 
= new wpConnection('127.0.0.1''username''password''mydatabase''wp_');

$content[] = 'Here are the 5 most recent posts and comments:';

$h $wp->iteratePostsByDate(true5);
while (
$wp->loadPost($h))
{
$wp->openAuthor($wp->post->author_id);
$content[] = <<<HTML
Post Author: {$wp->author->name}<br>
Post Title: 
{$wp->post->title}<br>
Post: 
{$wp->post->content}<br>
Most Recent Comments:
HTML;
$c $wp->iterateCommentsByPost($wp->post->id3);
while ($wp->loadComment($c))
$content[] = "{$wp->comment->comment_author} said: {$wp->shorten($wp->comment->comment_content50)}";
}

echo 
implode(chr(10), $content);
?>


Obviously I've hinted at a couple other things there - I'd like to have some formatting functions in the class as well to make things easier, like "shorten" or "dateFormat" so that you could still use the object right in a HEREDOC or interpolated string.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: August 06, 2009, 11:03:37 AM »

You know that a certain other member of this forum and I have been working on a fully-featured wordpress emulation layer?
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: August 06, 2009, 12:55:02 PM »

No I had no idea... would that bastard be hallucinating a horse?

In any case, what do you think of the class?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #4 on: August 06, 2009, 01:02:55 PM »

I think it's sexy, as always.

No, it's the guy who makes fun of the horse-hallucinator with me.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: August 06, 2009, 02:58:17 PM »

ah, delightful. Spoke with the gentleman last night AAMAF. Perhaps we should converse on this issue.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #6 on: August 06, 2009, 05:46:02 PM »

hehe

I think that should be AAMOF.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: August 06, 2009, 05:54:48 PM »

Shurrup.  ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #8 on: August 11, 2009, 02:21:23 PM »

That's tits mcgee there perks - apparently great twisted minds think alike.   Applause
Logged

We can't stop here, this is bat country.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: August 11, 2009, 03:10:36 PM »

Ah-so, the prodigal coder announces himself. Indeed, ol' chum...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #10 on: August 11, 2009, 03:22:47 PM »

I'm like the Harry Potter of code cause I sell mad copies and children think I'm Magical.
Logged

We can't stop here, this is bat country.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: August 11, 2009, 03:36:18 PM »

Oh FFS. Here comes discussion about his "magic wand" again.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
herbacious
Journeyman
***
Offline Offline

Posts: 51


View Profile
« Reply #12 on: August 26, 2009, 02:54:42 PM »

so back on topic, this is a class for pulling posts etc out of wordpress to be displayed in another CMS or whatever - a wordpress front end replacement effectively?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #13 on: August 26, 2009, 04:24:25 PM »

Actually bigger than that, because I'd like to escape the WordPress functions for putting together a blog fascia.

The challenge for the developer, is that there's a certain component of "reinventing the wheel" if they use a class like this: all of a sudden, you need to manage comment posting, who can see what comment etc. But on the plus side, particularly if you're developing a push-only blog then it's not that bad at all.

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
patch
Rookie
**
Offline Offline

Posts: 32


View Profile
« Reply #14 on: August 27, 2009, 11:07:41 AM »

I couldn't see any reference to 'pages' there, only 'posts' ... you thinking about doing anything with pages with the class?
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!