Singularities

Introduction

Singularities, this is how are named WordPress types of content in the Assely framework. All of this types are grouped and unified to common API. This way creating and managing your post types, taxonomies, metaboxes etc. is simple and intuitive. In this chapter, you will learn how to create, register and use it inside your application.

Creating Singularities

By default, each application singularity is stored inside the own type-named directory. For example, post types can be found in app\Posttypes folder, taxonomies inside app\Taxonomies, and so on.

Via console command

Assely's WP-CLI commands can scaffold singularities for you. Just type inside console wp help assely:make to see all available commands. List of all available you will find in WP-CLI documentation.

wp assely:make posttype Movies

wp assely:make taxonomy Genres --belongsto="App\Posttypes\Movies"

Manually created class file

When you do not have access to the WP-CLI, you need to create singularity manually. Make a class that is extending base singularity and you are ready to go.

namespace App\Posttypes;

use Assely\Posttype\Posttype;

class Movie extends Posttype {
    //
}

Registering Singularities

In order to register new singularities, you need to add them inside config/singularities.php file. Each one has separated entry inside this file. Add singularity classname to the correct array. On start arrays already contains default singularities which are shipped with WordPress.

For example, you created new post type named App\Posttypes\Movies. To bootstrap this singularity, simply add his classname to the posttypes array:

'posttypes' => [
    // ...other posttypes

    App\Posttypes\Movies::class,
],

Dependency injection

Type-hinted constructor parameters will be automatically resolved from the container. This is the best place to inject other application classes that may be used by the singularity. For example, common usage is to inject post type for Fielder selective field.

<?php

use Field;
use App\Posttypes\Movies;
use Assely\Profile\Profile;

class Reviewer extends Profile
{
    /**
     * Profile slug.
     *
     * @var string
     */
    public $slug = 'reviewer';

    /**
     * Construct profile.
     *
     * @param \App\Posttypes\Movies $movies
     */
    public function __construct(Movies $movies) {
        $this->movies = $movies;
    }

    /**
     * Profile custom fields.
     *
     * @return \Assely\Field\Field[]
     */
    public function fields() {
        return [
            Field::selective('favourite_movies', [
                'items' => $movies->all()
            ])
        ];
    }
}

Configuring how metadata is stored

Singularities can store it's metadata inside the database in a various way. This configuration determines how you will be accessing these values across your application.

Preserve as single record

All metadata are stored in the single record and are accessible under one, same key.

public function arguments() {
    return [
        'preserve' => 'single'
    ];
}

For example, you have App\Metaboxes\MovieDetails with movie_details slug and Field::datepicker('release_date') field.

use Field;

public function fields() {
    return [
        Field::datepicker('release_date'),
        Field::text('director')
    ];
}

The metadata for this singularity will be preserved something like this:

meta_key meta_value
movie_details ['release_date' => '2016-06-06T00:00:00+00:00', 'director' => 'Gore Verbinski']

Preserve as multiple records

Switching to multiple causes that metadata are stored in multiple records, where the key will be a slug of the top level custom field.

public function arguments() {
    return [
        'preserve' => 'multiple'
    ];
}

In this case example from above will be stored differently.

meta_key meta_value
release_date 2016-06-06T00:00:00+00:00
director Gore Verbinski