Model Registration
Mantle can help you spend less time writing registration code and more time building websites.
Registering Post Types/Taxonomies
Models can auto-register the object type they represent (a post type for a post
model, a taxonomy for a taxonomy model). They can be generated through a wp-cli command:
bin/mantle make:model Product --model_type=post --registrable
That will generate a model that represents the product post type.
namespace App\Models;
use Mantle\Contracts\Database\Registrable;
use Mantle\Database\Model\Post;
use Mantle\Database\Model\Registration\Register_Post_Type;
use Mantle\Support\Registration\Post_Type_Arguments;
class Product extends Post implements Registrable {
use Register_Post_Type;
/**
* Arguments to register the model with.
*
* @return array<mixed>
*/
public static function get_registration_args(): array {
return Post_Type_Arguments::make()
->public()
->show_in_rest()
->rest_base( static::get_object_name() )
->default_supports()
->taxonomies( [ 'category', 'post_tag' ] )
->label(
singular: __( 'Product', 'app' ),
plural: __( 'Products', 'app' ),
)
->all();
}
}
Registration Arguments
Mantle\Support\Registration\Post_Type_Arguments and
Mantle\Support\Registration\Taxonomy_Arguments are fluent builders for the
arguments passed to register_post_type() and register_taxonomy(). Each
argument has a matching method (public(), show_in_rest(), has_archive(),
rewrite(), and so on), and all() returns the final array. Any argument
without a dedicated method can still be set by calling a method of the same
name, such as ->delete_with_user( true ).
A few helpers go beyond setting a single argument:
label( singular: ..., plural: ... )generates the full set of labels from a singular and plural name. The plural is inferred from the singular when omitted. Uselabels()afterward to override individual labels.default_supports()adds the common post type features (title,editor,author,thumbnail,excerpt,custom-fields,comments, andrevisions). Pass an array to leave some out:->default_supports( except: [ 'comments' ] ).add_support()andremove_support()add or remove a single feature.
Taxonomy models work the same way with Taxonomy_Arguments:
namespace App\Models;
use Mantle\Contracts\Database\Registrable;
use Mantle\Database\Model\Registration\Register_Taxonomy;
use Mantle\Database\Model\Term;
use Mantle\Support\Registration\Taxonomy_Arguments;
class Product_Category extends Term implements Registrable {
use Register_Taxonomy;
/**
* Object types the taxonomy is registered for.
*
* @var string[]
*/
protected static $object_types = [ 'product' ];
/**
* Arguments to register the model with.
*
* @return array<mixed>
*/
public static function get_registration_args(): array {
return Taxonomy_Arguments::make()
->hierarchical()
->public()
->show_in_rest()
->show_admin_column()
->label(
singular: __( 'Product Category', 'app' ),
plural: __( 'Product Categories', 'app' ),
)
->all();
}
}
get_registration_args() must still return an array, so returning a plain
array continues to work if you prefer it.
The model should automatically be registered with Mantle. Models are discovered
from the app/models directory in your application. Mantle will discover your
models after updating composer and generating a new model. They can be manually
discovered by running the model discovery command:
bin/mantle model:discover
Manual Model Registration
In the event you don't wish to use Mantle's built-in model discovery, models
can be listed in your application's config/models.php file instead. Models
listed under register are booted on every request alongside any discovered
models:
return [
'register' => [
App\Models\Product::class,
],
];
To take full control over which models are registered, disable model discovery
with the disable_discovery option. When discovery is disabled, Mantle won't
boot any models for you (including those listed under register), so boot them
from one of your application's service providers:
return [
'disable_discovery' => true,
];
namespace App\Providers;
use App\Models\Product;
use Mantle\Support\Service_Provider;
class App_Service_Provider extends Service_Provider {
public function boot(): void {
Product::boot_if_not_booted();
}
}
Registering REST API Fields
Models can define REST API fields inside of a model easily. Registration should
be defined in the model's boot() method. To ensure the model's fields are
always registered, the model should beadded to the config/models.php file
under the register property.
namespace App\Models;
use Mantle\Contracts\Database\Registrable_Fields;
use Mantle\Database\Model\Post as Base_Post;
use Mantle\Database\Model\Registration\Register_Rest_Fields;
class Post extends Base_Post implements Registrable_Fields {
use Register_Rest_Fields;
protected static function boot() {
static::register_field(
'field-to-register',
function() {
return 'value to return';
}
)
->set_update_callback(
function( $value ) {
// ...
}
);
}
}
Registering Meta Fields
Models can define meta values to associate with the model. Similar to
registering a model's REST API field, registration should be defined in the
model's boot() method. To ensure the fields are always registered, the model
should be added to the config/models.php file under the register property.
By default, Mantle will pass the object_subtype argument for you for the
model, registering meta only for the specific object type and object subtype the
model represents. In the following example, the meta will be added to the post
object type in WordPress and the product object subtype.
namespace App\Models;
use Mantle\Contracts\Database\Registrable_Meta;
use Mantle\Database\Model\Post;
use Mantle\Database\Model\Registration\Register_Meta;
class Product extends Post implements Registrable_Meta {
use Register_Meta;
protected static function boot() {
static::register_meta( 'product_id' );
static::register_meta( 'feedback', [ ... ] );
}
}
Bootable Trait Methods
To allow for simplicity when writing traits that are shared among a set of
models, traits support a boot and initialize method to allow for automatic
registration of the respective trait. The methods are suffixed with the name
of the trait lowercased (for example: boot_{trait_name}). The boot method
must be static since it is called once for the model class, not for an instance.
trait Example_Trait {
public static function boot_example_trait(): void {
// Called once per request.
}
public function initialize_example_trait(): void {
// Called on every model instantiation.
}
}
Model Routing
Models can define post/term singular and archive routes. This will replace the WordPress singular routes for posts and terms with no additional customization needed. Routes inside models can use any model alias or attribute, too.
By default, any model that uses the Register_Post_Type or Register_Taxonomy
will have their routes defined for them. The default route format is
/{object_name}/ and /{object_name}/{slug}/ for the archive and singular
route, respectively. The model can define their own route by replacing the
get_route() or get_archive_route() methods.
For routes that don't use the registration traits the model can still have
their routing handled by including Mantle\Database\Model\Concerns\Custom_Post_Permalink or
Mantle\Database\Model\Concerns\Custom_Term_Link traits.
use Mantle\Database\Model\Post;
use Mantle\Database\Model\Registration\Register_Post_Type;
class Product extends Post {
use Register_Post_Type;
public static function get_route(): ?string {
return '/product/{slug}';
}
}
For more information, see Model Routing