A collection of Abstract Classes for creating common WordPress fixtures which need registering.
For Perique V2.0.*
WordPress has a number of Registerable functions for Post Types, Post Meta and Taxonomies. These tend to require large arrays of arguments to be defined. This library provides Classes which can be registered and used with the Registration process.
$ composer require pinkcrab/registerables
You need to include the module and the Registerable_Middleware will be included automatically. They come with their own dependencies which will need to be added using the module()
method from the App_Factory instance.
$app = ( new PinkCrab\Perique\Application\App_Factory() )
// Normal Perique bootstrapping.
->module( Registerable::class );
->boot();
Once the middleware has been included, you can use Post_Type, Taxonomies, Meta Data and Meta boxes as part of the usual Registration process
Creates a simple post type.
use PinkCrab\Registerables\Post_Type;
class Basic_CPT extends Post_Type {
public string $key = 'basic_cpt';
public string $singular = 'Basic';
public string $plural = 'Basics';
}
Creates a flat taxonomy for the Post Post Type.
use PinkCrab\Registerables\Taxonomy;
class Basic_Tag_Taxonomy extends Taxonomy {
public string $slug = 'basic_tag_tax';
public string $singular = 'Basic Tag Taxonomy';
public string $plural = 'Basic Tag Taxonomies';
public ?string $description = 'The Basic Tag Taxonomy.';
public string $hierarchical = false;
public array $object_type = array( 'post' );
}
Create a simple meta box as part of a post type definition.
class My_CPT extends Post_Type {
public string $key = 'my_cpt';
public string $singular = 'CPT Post';
public string $plural = 'CPT Posts';
public function meta_boxes( array $meta_boxes ): array {
$meta_boxes = MetaBox::side('my_meta_box')
->label('My Meta Box')
->view_template($template_path)
->view_vars($additional_view_data)
->action('save_post', [$this, 'save_method'])
->action('update_post', [$this, 'save_method'])
}
}
If your meta box has any level of complexity, it is recommended to create a separate service which handles this and inject it into the Post_Type class.
/** The Meta Box Service */ class Meta_Box_Service { public function get_meta_boxes(): array { $meta_boxes = array(); $meta_boxes[] = MetaBox::side('my_meta_box') ->label('My Meta Box') ->view_template($template_path) ->view_vars($additional_view_data) ->action('save_post', [$this, 'save_method']) ->action('update_post', [$this, 'save_method']) }
public function save_method( int $post_id ): array { // Handle validating and updating post meta. } }
/* Injected into post type / class My_CPT extends Post_Type { public string $key = 'my_cpt'; public string $singular = 'CPT Post'; public string $plural = 'CPT Posts';
// Pass the service in as a dependency. private Meta_Box_Service $meta_box_service;
public function __construct(Meta_Box_Service $meta_box_service){ $this->meta_box_service = $meta_box_service; }
// Return the populated Meta_Box instances. public function meta_boxes( array $meta_boxes ): array { return $this->meta_box_service->get_meta_boxes(); } }
[See full Meta Box Docs](docs/Meta_Box.md)
### Shared Meta Boxes
In case you would like to render the same meta box on multiple Custom Post Types or to add it to existing ones, you can use the `Shared_Meta_Box_Controller` base class and extend it to register independent meta boxes.
```php
class Acme_Meta_Box extends Shared_Meta_Box_Controller {
/**
* Return the Meta Box instance.
*/
public function meta_box(): Meta_Box {
return Meta_Box::side('acme_box')
->label('Acme Meta Box')
->screen('acme_post_type_a')
->screen('acme_post_type_b')
->view_template($template_path)
->view_vars($additional_view_data)
->action('save_post', [$this, 'save_method'])
->action('update_post', [$this, 'save_method'])
}
/**
* Sets any metadata against the meta box.
* @see Post Type docs for more details
*/
public function meta_data( array $meta_data ): array {
$meta_data[] = ( new Meta_Data( 'acme_meta_1' ) )
->type( 'integer' )
->single......
return $meta_data;
}
/** The save_post and update_post hook callback */
public function save_method( int $post_id ): array {
// Handle validating and updating post meta.
update_post_meta($post_id, 'acme_meta_1', $value);
}
}
The above Meta Box would be shown on both
acme_post_type_a
andacme_post_type_b
You can also inject any dependencies via the constructor too.
You can register post
, term
, user
and comment
meta fields either as a part of Post Types/Taxonomy Registerables or on there own. This fluent object based definition makes it easy to create these inline.
You can add full REST support by supplying a schema for the field and the Registrar will register the field also.
class Additional_Post_Meta extends Additional_Meta_Data_Controller {
/** Define the Meta Data */
public function meta_data(array $meta_data): array {
$meta_data[] = (new Meta_Data('meta_key'))
->post_type('post')
->default('foo')
->description($description)
->single()
->sanitize('sanitize_text_field')
->rest_schema(['type' => 'string']);
return $meta_data;
}
}
You can also define MetaData for Post Types and Taxonomies when creating them.
To register standalone Meta_Data, you can use the Additional_Meta_Data_Controller
which has a single method meta_data(array $meta_data): array
. Like in the example above, you add your Meta_Data instances to the array and return.
The class has an empty constructor, so you can easily inject dependencies in and make use of the App_Config
meta options.
You can see an example project which included a CPT, Taxonomy, Meta Box and Meta Data here
If you would like to contribute to this project, please feel free to fork and submit a PR. If any issue doesn't exist for the problem, please create one.
Please ensure your changes to do not drop coverage lower than they currently are, unless it can not be helped (include a reason why)
Setup the dev environment
$ composer install
$ composer all
- This will run all the tests, static analysis and linter$ composer coverage
- This will produce a HTML coverage report ../coverage-report
$ composer analyse
- This will run PHPStan on lv 8$ composer sniff
- This will run PHPCS with the WP Rule set.Please note the CI Actions runs
composer all
on multiple PHP and WP versions. Running locally will only run with your version of PHP and latest major or WP.
http://www.opensource.org/licenses/mit-license.html
Regiserable
module, but no other changes needed.Post_Type::$templates
to Post_Type::$template