Skip to content

Commit

Permalink
feat(posttype): Allow unregistering post types by passing false
Browse files Browse the repository at this point in the history
feat(taxonomy): Allow unregistering taxonomies by passing `false`
  • Loading branch information
Log1x committed Apr 1, 2020
1 parent 737c5f6 commit 71f4d71
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ To modify an existing post type, simply treat it as if you are creating a new po
],
```

It is also possible to unregister an existing post type by simply passing `false`:

```php
'post' => [
'book' => false,
],
```

Please note that some built-in post types (e.g. Post) can not be conventionally unregistered.

For additional configuration options for post types, please see:

- [`register_post_type()`](https://developer.wordpress.org/reference/functions/register_post_type/)
Expand Down Expand Up @@ -116,6 +126,15 @@ As with post types, to modify an existing taxonomy, simply pass only the configu
],
```

Also like post types, you can easily unregister an existing taxonomy by simply passing `false`:

```php
'taxonomy' => [
'post_tag' => false,
'category' => false,
],
```

For additional configuration options for taxonomies, please see:

- [`register_taxonomy()`](https://codex.wordpress.org/Function_Reference/register_taxonomy)
Expand Down
64 changes: 63 additions & 1 deletion src/Poet.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function __construct($config = [])
* If a post type already exists, the object will be modified instead.
* ↪ https://codex.wordpress.org/Function_Reference/get_post_type_object
*
* If a post type already exists and is set to `false`, the post type
* will be unregistered.
* ↪ https://developer.wordpress.org/reference/functions/unregister_post_type/
*
* @return void
*/
protected function registerPosts()
Expand All @@ -51,6 +55,10 @@ protected function registerPosts()
}

if ($this->exists($key)) {
if (is_bool($value) && $value === false) {
return $this->remove($key);
}

return $this->modify($key, $value);
}

Expand All @@ -70,6 +78,11 @@ protected function registerPosts()
* If a taxonomy already exists, the object will be modified instead.
* ↪ https://developer.wordpress.org/reference/functions/get_taxonomy/
*
* If a taxonomy already exists and is set to `false`, the taxonomy
* will be unregistered.
* ↪ https://developer.wordpress.org/reference/functions/unregister_taxonomy_for_object_type/
* https://developer.wordpress.org/reference/functions/unregister_taxonomy/
*
* @return void
*/
protected function registerTaxonomies()
Expand All @@ -81,6 +94,10 @@ protected function registerTaxonomies()
}

if ($this->exists($key)) {
if (is_bool($value) && $value === false) {
return $this->remove($key);
}

return $this->modify($key, $value);
}

Expand Down Expand Up @@ -173,6 +190,29 @@ protected function modify($name, $config)
});
}

/**
* Removes an existing post type or taxonomy object.
*
* @param string $name
* @return void
*/
protected function remove($name)
{
$object = get_post_type_object($name) ?: get_taxonomy($name);

if (! $this->verify($object)) {
return;
}

if ($this->isTaxonomy($object)) {
return collect($object->object_type)->each(function ($key) use ($object) {
return unregister_taxonomy_for_object_type($object->name, $key) ?? unregister_taxonomy($object);
});
}

return unregister_post_type($object);
}

/**
* Checks if the passed object is a valid WP_Post_Type or WP_Taxonomy instance.
*
Expand All @@ -181,7 +221,7 @@ protected function modify($name, $config)
*/
protected function verify($object)
{
return $object instanceof WP_Post_Type || $object instanceof WP_Taxonomy;
return $this->isPostType($object) || $this->isTaxonomy($object);
}

/**
Expand All @@ -197,6 +237,28 @@ protected function namespace($delimiter = '/')
) ?? 'sage') . $delimiter;
}

/**
* Check if an object is an instance of WP_Post_Type.
*
* @param mixed $object
* @return bool
*/
protected function isPostType($object)
{
return $object instanceof WP_Post_Type;
}

/**
* Check if an object is an instance of WP_Taxonomy.
*
* @param mixed $object
* @return bool
*/
protected function isTaxonomy($object)
{
return $object instanceof WP_Taxonomy;
}

/**
* Check if a string is empty after stripping tags and whitespace.
*
Expand Down

0 comments on commit 71f4d71

Please sign in to comment.