=== PostTypeBuilder ===
Contributors: bornemix
Tags: orm, posttypebuilder, bornemix, database, class
Requires at least: 3.0
Tested up to: 3.3.1
Stable tag: 0.5
License: GPL2
Maps @annotated PHP classes to custom post types, automatically adds relevant fields to the Admin GUI, and LINQ/ActiveRecord-like queries.
== Description ==
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 "
" . $book->post_title . "";
echo "" . $book->number_of_pages . "";
}
`
Included is the [Addendum](http://code.google.com/p/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](http://wordpress.org/extend/plugins/posttypebuilder/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](http://wordpress.org/extend/plugins/posttypebuilder/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](http://wordpress.org/extend/plugins/posttypebuilder/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](http://wordpress.org/extend/plugins/posttypebuilder/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...).
== Screenshots ==
1. Automatic form field generation of first and second example (in the description tab)
== Register classes ==
At Wordpress initialization stage, when an HTTP request is made, PostTypeBuilder searches `wp-content/classes` for PHP files.
Each file is then included, and PostTypeBuilder determines if class `\MyEntities\α` was defined as a result of that inclusion. `MyEntities` is the required namespace where you need to define your entities, and `α` is the filename converted to CamelCase.
For example, if file `wp-content/classes/newsletter_issue.php` exists, PostTypeBuilder will include that and then check if the class `\MyEntities\NewsletterIssue` exists.
*To change the default namespace, define POSTTYPEBUILDER_ENTITIES_NAMESPACE before PostTypeBuilder gets ahold of it.*
= Options =
There are a number of options available (defined in `annotations.php`):
* `@CanonicalName("")`
* `@Name("")`
* `@PluralName("")`
* `@Labels({})`
* `@Options({})`
* `@Panels({})`
To use these, include those you want in a single `/** */` annotational "DocBlock" comment, with each annotation separated by some whitespace (preferrably, each on a separate line).
*`@CanonicalName("")`* defines that the wordpress-internal name should equal the string that is supplied between the parenthesis. By default, this will be equal to the PHP filename, excluding the `.php` extension. (ie. "book")
The following example registers a Post class, with the internal, canonical name "content_post":
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/** @CanonicalName("content_post") */
class Post extends Entity{
}
`
*`@Name("")`* defines the singular name (used in labels, menus) to be the string supplied between the parenthesis. By default, this will be equal to the CamelCase version of the canonical name, each Camel segment being separated by space. (ie. "Book")
*`@PluralName("")`* defines the plural name (used in labels, menus) to be the string supplied between the parenthesis. By default, this will be equal to the implicit (or explicit) singular name + "s". (ie. "Books")
The following example defines the irregular plural name "Virii" for the class "Virus":
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/** @PluralName("Virii") */
class Virus extends Entity{
}
`
*`@Labels({})`* is a way to override the PostTypeBuilder label determination mechanism, which is partly described above (see `generate_class_meta` in `posttypebuilder.php`). Supplied values will override the values provided by PostTypeBuilder. Possible keys correspond to the keys in the labels object documented in [register_post_type()](http://codex.wordpress.org/Function_Reference/register_post_type).
The following example overrides the menu name (subsequent label assignations need to separated by comma):
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/** @Labels({menu_name = "Foo"}) */
class Book extends Entity{
}
`
*`@Supports({})`* is a way to specify the post type supports array.
Since post types do not work without a title, it is automatically added to whatever you supply here. Editor is not enabled by default.
Possible values are:
* 'title'
* 'editor' (content)
* 'author'
* 'thumbnail' (featured image, current theme must also support post-thumbnails)
* 'excerpt'
* 'trackbacks'
* 'custom-fields'
* 'comments' (also will see comment count balloon on edit screen)
* 'revisions' (will store revisions)
* 'page-attributes' (menu order, hierarchical must be true to show Parent option)
* 'post-formats' add post formats, see [Post Formats](http://wordpress.org/Post_Formats).
The following example enables support for text editor and comments:
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/** @Supports({"editor","comments"}) */
class Book extends Entity{
}
`
*`@IsPublic({})`* makes the post type publicly queryable.
By default, entity types are not enabled to be visible to outside users. As such, you will get a 404 error when clicking "View post".
The following example enables "publicly queryable" for the entity type:
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/** @IsPublic */
class Book extends Entity{
}
`
*`@Options({})`* is a way to override the option defaults that are passed to WP's [register_post_type()](http://codex.wordpress.org/Function_Reference/register_post_type).
The following example enables post archives:
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/**
@IsPublic
@Options({
has_archive = "books"
})
*/
class Book extends Entity{
}
`
*`@Panels`* specifies the form field groups ("panels") that are to be shown on the edit/create post screen. By default, only one panel is available ("properties"). Supplying @Panels will clear the default panel, so you need to redeclare it if you use this annotation.
The following code specifies that two panels, "Properties" (in main column) and "Options" (in side column) should be used:
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
/**
@Panels({
{
id="properties",
label="Properties",
position="normal"
},
{
id="options",
label="Options",
position="side"
}
})
*/
class Book extends Entity{
/** @Property(panel="properties") */
public $author_name;
/** @Property(type="Boolean",panel="options") */
public $show_on_startpage;
}
`
== Define properties ==
Each property that PostTypeBuilder is to be aware of, needs to be prefixed with the annotational comment `/** @Property */`. Otherwise it will be a plain old PHP class property (not managed by PostTypeBuilder).
By default, the property type is "Text", which can be declared explicitly by including `/** @Property(type="Text") */`.
The following example defines a property called `number_of_pages` in the entity Book:
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
class Book extends Entity{
/** @Property */
public $number_of_pages;
}
`
*Note: The specific visibility `public` is not required - but without any modifier, [the variable name will not be registered as a class property](http://php.net/manual/en/language.oop5.visibility.php). Either of `public`, `private` or `protected` will do (`var` evaluates to `public`), but note that only public properties will show up in the edit/create post screen.*
By prefixing the property declaration with the `@Property` annotation, PostTypeBuilder will:
1. Sync it when relevant instances are loaded/saved from the database.
2. Generate a form field for it when showing the edit/create post screen.
3. Make it available for searching/filtering/ordering when using the PostTypeBuilder query mechanism.
= Multiple values =
A property can hold multiple values if declared with `= array()` in the class definition. Form field will then be preceded by a checkbox-list of current values that can be rearranged by drag and drop, and unchecked to be removed. Adding input to form field will add a new value.
Since 0.4, PostTypeBuilder uses WP's post_meta functions which takes care of array serialization when saving.
The following example enables the "author_names" property to hold multiple values:
`
namespace MyEntities;
use \PostTypeBuilder\Entity;
class Book extends Entity{
/** @Property */
public $author_names = array();
}
`
= Options =
To supply options to this annotation, include them in a comma-separated list of equals-separated name-value pairs, the list surrounded by braces between the optional parenthesis after the annotation keyword: `/** Property({option1="Foo", option2="Bar"}) */`
There are a few options you can supply to this annotation:
* `type` (default `"Text"`)
* `label` (default `null`)
* `required` (default `false`)
* `panel` (default `"properties"`)
* `visible` (default `true`)
*`type`* specifies the class property type, either a built-in PostTypeBuilder type, an entity declared in MyEntities, or a custom type supplied by you (look in `types.php` for inspiration).
PostTypeBuilder supplies a number of built-in types:
* `Text` (default) - Long string.
* `LongText` - Long string, but the form field will be a multiline `