strata-mvc/strata

View on GitHub
src/Model/CustomPostType/CustomPostType.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Strata\Model\CustomPostType;

use Strata\Utility\Hash;
use Strata\Model\WordpressEntity;
use Strata\Model\Taxonomy\Taxonomy;
use Strata\Model\CustomPostType\LabelParser;
use Strata\Model\CustomPostType\Registrar\CustomPostTypeAdminMenuRegistrar;
use Strata\Model\CustomPostType\Registrar\CustomPostTypeRegistrar;
use Strata\Model\CustomPostType\Registrar\TaxonomyRegistrar;
use Strata\Model\CustomPostType\QueriableEntityTrait;

/**
 * A class that wraps around Wordpress' custom post types.
 */
class CustomPostType extends WordpressEntity
{
    use QueriableEntityTrait;

    /**
     * The Wordpress custom post type identifier prefix
     * @var string
     */
    public $wpPrefix = "cpt_";

    /**
     * A list of administration sub-menus associated to the
     * custom post type.
     * @var array
     */
    public $admin_menus = array();

    /**
     * A list of taxonomies associated to the custom post type.
     * @var array
     */
    public $belongs_to = array();

    /**
     * Specifies whether Strata should attempt to automate routing
     * to the model's default controller when the custom post type's
     * slug is matched in the URL.
     * @var boolean|array
     */
    public $routed = false;

    /**
     * Returns a label object that exposes singular and plural labels
     * @return LabelParser
     */
    public function getLabel()
    {
        $labelParser = new LabelParser($this);
        $labelParser->parse();
        return $labelParser;
    }

    /**
     * Registers the custom post type in Wordpress. A Custom post type
     * must trigger this during the 'init' state for it to be recognized
     * automatically by Wordpress.
     */
    public function register()
    {
        $registrars = array(
            new CustomPostTypeRegistrar($this),
            new TaxonomyRegistrar($this)
        );

        foreach ($registrars as $registrar) {
            $registrar->register();
        }
    }

    /**
     * Registers the custom post type's sub menus.
     * @return boolean The result of the registration
     */
    public function registerAdminMenus()
    {
        $registration = new CustomPostTypeAdminMenuRegistrar($this);
        $registration->configure(Hash::normalize($this->admin_menus));
        return $registration->register();
    }

    /**
     * Returns the model's menu icon as specified by the 'menu_icon'
     * configuration key.
     * @return string
     */
    public function getIcon()
    {
        if (array_key_exists('menu_icon', $this->configuration)) {
            return $this->configuration['menu_icon'];
        }

        return 'dashicons-admin-post';
    }

    /**
     * Returns whether or not the current model supports and has taxonomies.
     * @return boolean True if model has taxonomies
     */
    public function hasTaxonomies()
    {
        return count($this->belongs_to) > 0;
    }

    /**
     * Gets the associated taxonomy objects.
     * @return array
     */
    public function getTaxonomies()
    {
        $tax = array();

        foreach (Hash::normalize($this->belongs_to) as $taxonomyName => $taxonomyConfig) {
            if (class_exists($taxonomyName)) {
                $tax[] = new $taxonomyName();
            } else {
                $tax[] = Taxonomy::factory($taxonomyName);
            }
        }

        return $tax;
    }
}