1. Go to this page and download the library: Download idetik/coretik library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
useCoretik\Core\Builders\Taxonomy;
useCoretik\Core\Builders\PostType;
// Declare post type
PostType::make('my_custom_post_type')
->setSingularName('Title')
->setPluralName('Titles')
->addToSchema();
// Declare taxonomy
Taxonomy::make('my_taxonomy')
->setSingularName('Title')
->setPluralName('Titles')
->for('my_custom_post_type')
->addToSchema();
useCoretik\Core\Builders\Taxonomy;
useApp\Model\MyModel;
useApp\Query\MyQuery;
useApp\Handler\MyHandlerA;
useApp\Handler\MyHandlerB;
// Accept same arguments as register_post_type() (https://developer.wordpress.org/reference/functions/register_post_type/) and register_extended_post_type() (https://github.com/johnbillion/extended-cpts/wiki/Registering-Post-Types)
$register_extended_post_type_args = [];
PostType::make('my_custom_post_type')
->setSingularName('Title')
->setPluralName('Titles')
->setArgs($register_extended_post_type_args) // Optional,
->factory(MyModel::class) // Optional, you can add a custom model factory or use the default factory built in Coretik
->querier(MyQuery::class) // Optional, you can add a custom query class or use the default querier built in Coretik
->handler(MyHandlerA::class) // Optional, you can use many handlers on the same builder
->handler(MyHandlerB::class)
->attach('myMacroA', 'my_callable') // Optional, you can attach all callables you want
->attach('myMacroB', 'my_callable')
->addToSchema();
useCoretik\Core\Builders\Taxonomy;
useApp\Model\MyTermModel;
useApp\Query\MyTermQuery;
useApp\Handler\MyTermHandlerA;
useApp\Handler\MyTermHandlerB;
// Accept same arguments as register_taxonomy() (https://developer.wordpress.org/reference/functions/register_taxonomy/) and register_extended_taxonomy() (https://github.com/johnbillion/extended-cpts/wiki/Registering-taxonomies)
$register_extended_taxonomy_args = [];
Taxonomy::make('my_custom_taxonomy')
->setSingularName('Title')
->setPluralName('Titles')
->setArgs($register_extended_taxonomy_args) // Optional,
->factory(MyTermModel::class) // Optional, you can add a custom model factory or use the default factory built in Coretik
->querier(MyTermQuery::class) // Optional, you can add a custom query class or use the default querier built in Coretik
->handler(MyTermHandlerA::class) // Optional, you can use many handlers on the same builder
->handler(MyTermHandlerB::class)
->attach('myMacroA', 'my_callable') // Optional, you can attach all callables you want
->attach('myMacroB', 'my_callable')
->addToSchema();
useCoretik\Core\Models\Wp\PostModel;
useCoretik\Core\Models\Interfaces\ModelInterface;
useCoretik\Core\Collection;
classMyPostModelextendsPostModel{
protectedfunctionintializeModel(): void{
$this->declareMetas([
'ma_meta_a' => 'bdd_field_name',
'ma_meta_b' => 'other_bdd_field_name',
]);
// Each meta// @see Core/Models/MetaDefinition.php$this->metaDefinition('ma_meta_a')->castTo('array');
$this->metaDefinition('ma_meta_b')->protectWith(fn ($model) => (bool)$model->canIUpdateThisValue());
}
publicfunctioncanIUpdateThisValue(): bool{
// @todo create a guardreturntrue;
}
publicfunctionfoo(): string{
if (in_array('bar', $this->get('ma_meta_a'))) {
return'bar';
}
return'foo';
}
/**
* Accessor
*
* To define an accessor, create a getFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access.
* In this example, we'll define an accessor for the first_name attribute. The accessor will automatically be called when attempting to retrieve the value of the first_name attribute:
*/publicfunctiongetFirstNameAttribute(): string{
if (empty($this->get('ma_meta_b'))) {
return'toto';
}
return$this->get('ma_meta_b');
}
/**
* Mutator
*
* To define a mutator, define a setFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access.
* So, again, let's define a mutator for the first_name attribute. This mutator will be automatically called when we attempt to set the value of the first_name attribute on the model:
*/publicfunctionsetFirstNameAttribute($value){
$this->ma_meta_b = strtolower($value);
}
/**
* Relationships
*
* For now, only post <-> taxonomy relationships are ready to use with wp-admin.
* Posts to posts relationships (1, n)
$model = app()->schema('my_custom_post_type')->model();
$model->post_title = 'Mr Bar Foo';
$model->post_status = 'publish';
$model->ma_meta_a = 'Foo';
$model->first_name = 'Bar';
$model->setAttributes(['smart', 'tall']);
$model->addToGroup(100); // a post with title 'Groupe 1' from 'my_post_type_group' with ID 100,
$model->save();
$modelId = $model->id();