Skip to content

Commit

Permalink
feat(post-type): introduce PostTypeInterface/Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
whizark committed Jan 3, 2016
1 parent d074d06 commit bce122f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Empty file removed src/Component/PostType/.gitkeep
Empty file.
54 changes: 54 additions & 0 deletions src/Component/PostType/PostTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* PostType Interface
*
* @author Whizark <devaloka@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2014 Whizark.
* @license MIT
*/

namespace Devaloka\Component\PostType;

use Devaloka\Component\Taxonomy\TaxonomyInterface;

/**
* Interface PostTypeInterface
*
* @package Devaloka\Component\PostType
*
* @codeCoverageIgnore
*/
interface PostTypeInterface
{
/**
* @return string
*/
public function getName();

/**
* @return mixed[]
*/
public function getOptions();

/**
* @param TaxonomyInterface $taxonomy
*/
public function addTaxonomy(TaxonomyInterface $taxonomy);

/**
* @return \Devaloka\Component\Taxonomy\TaxonomyInterface[] An array of taxonomy ID(s) that will be registered for
* the Post Type.
*/
public function getTaxonomies();

/**
* @return object|\WP_Error The registered post type object, or an error object.
*/
public function register();

/**
* @return bool|\WP_Error True on success, WP_Error on failure.
*/
public function unregister();
}
59 changes: 59 additions & 0 deletions src/Component/PostType/PostTypeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* PostType Trait
*
* @author Whizark <devaloka@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2015 Whizark.
* @license MIT
* @license GPL-2.0
* @license GPL-3.0
*/

namespace Devaloka\Component\PostType;

use Devaloka\Component\Taxonomy\TaxonomyInterface;
use LogicException;

/**
* Trait PostTypeTrait
*
* @package Devaloka\Component\PostType
*
* @codeCoverageIgnore
*/
trait PostTypeTrait
{
protected $taxonomies = [];

public function addTaxonomy(TaxonomyInterface $taxonomy)
{
$this->taxonomies[$taxonomy->getName()] = $taxonomy;
}

public function register()
{
/** @var PostTypeInterface $this */

$options = $this->getOptions();
$taxonomies = [];

foreach ($this->getTaxonomies() as $taxonomy) {
$taxonomies[] = $taxonomy->getName();
}

$options['taxonomies'] = $taxonomies;

return register_post_type($this->getName(), $options);
}

/**
* @throws LogicException
*
* @see https://core.trac.wordpress.org/ticket/14761 #14761 (unregister_post_type()) – WordPress Trac
*/
public function unregister()
{
throw new LogicException('unregister() is not implemented yet.');
}
}

0 comments on commit bce122f

Please sign in to comment.