PostTypeBuilder

Maps @annotated PHP classes to custom post types, automatically adds relevant fields to the Admin GUI, and LINQ/ActiveRecord-like queries.

Author:Björn Ali Göransson (profile at wordpress.org)
WordPress version required:3.0
WordPress version tested:3.3.2
Plugin version:0.5
Added to WordPress repository:17-08-2011
Last updated:23-01-2012
Warning! This plugin has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.
Rating, %:0
Rated by:0
Plugin URI:
Total downloads:812
Active installs:10+
plugin download
Click to start download

PostTypeBuilder is an Object Relational Mapper connecting directly into the WordPress engine, and provides handy scaffolding through the WordPress GUI, as well as data querying similar to LINQ and ActiveRecord (Book::find()->where(...)).

class Book extends Entity{
    /** @Property */
    public $number_of_pages;
}
while(have_posts()){
    the_post(); $book = new Book($post);
    
    echo "<dt>" . $book->post_title . "</dt>";
    echo "<dd>" . $book->number_of_pages . "</dd>";
}

Included is the Addendum library to support @annotations.

This plugin saves no information on your system (no database tables, no temporary files). All information is supplied by you in your class files.

See Other notes for more info

Register classes

Any classes in wp-content/classes, where the filename (like class_name.php) corresponds to the classname (like ClassName), will be registered as a WordPress Custom Post Type.

Following example needs to be defined in wp-content/classes/book.php, and results in a registered post type being shown in the admin UI (but not publicly queryable):

namespace MyEntities;
use \PostTypeBuilder\Entity;

class Book extends Entity{
}

See Other notes for more info

Define properties

Any class properties prefixed by the annotational comment /** @Property */ will be registered as WordPress metadata, and form fields will appear in the edit/create screen of the post type corresponding to the property type.

Following example, building on the previous Book example class, will display a text form field in the edit/create post screen:

namespace MyEntities;
use \PostTypeBuilder\Entity;

class Book extends Entity{
    /** @Property */
    public $number_of_pages;
}

To enable the property to carry multiple values, use the = array() assignment:

/** @Property */
public $authors = array();

See Other notes for more info

Find entities

You can load entities in three ways.

(1) By post ID:

$book = new Book(21);

(2) By post object (useful in the loop):

global $post;

$book = new Book($post);

(3) By query:

$books = Book::find()->where("pages > (NUMERIC)", 10);

foreach($books as $book){ ... }

(Note that the query is executed lazily - ie. at the foreach statement)

Following code shows how to manipulate your entities:

$book = new Book();
$book->post_title = "Foo";
$book->save();

(Both post_object members and class properties are accessed in a unified way, but class properties have precedence)

See Other notes for more info

Extend the functionality

The plugin is designed to be extensible, so you can override form field generation for your classes, text representations (by overriding __toString), add your own property types, their form field generation, their text representation, you can hook into save events (and more...).