Last active
October 23, 2019 19:53
-
-
Save trovster/de5ba2a8ac087f659e0bbda3b3819d32 to your computer and use it in GitHub Desktop.
WordPress taxonomy integrated with Lumberjack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint,SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint | |
namespace App\Models\Taxonomy; | |
use App\Exceptions\TaxonomyRegistrationException; | |
use Rareloop\Lumberjack\ScopedQueryBuilder; | |
use Spatie\Macroable\Macroable; | |
use Tightenco\Collect\Support\Collection; | |
use Timber\Term; | |
use Timber\Timber; | |
abstract class Base extends Term | |
{ | |
use Macroable { | |
Macroable::__call as __macroableCall; | |
Macroable::__callStatic as __macroableCallStatic; | |
} | |
public function __construct($id = false, $taxonomy = '', $preventTimberInit = false) | |
{ | |
/** | |
* There are occasions where we do not want the bootstrap the data. At the moment this is | |
* designed to make Query Scopes possible | |
*/ | |
if (!$preventTimberInit) { | |
$taxonomy = static::getTaxonomy(); | |
parent::__construct($id, $taxonomy); | |
} | |
} | |
public function __call($name, $arguments) | |
{ | |
if (static::hasMacro($name)) { | |
return $this->__macroableCall($name, $arguments); | |
} | |
return parent::__call($name, $arguments); | |
} | |
public static function __callStatic($name, $arguments) | |
{ | |
if (static::hasMacro($name)) { | |
return static::__macroableCallStatic($name, $arguments); | |
} | |
if (in_array($name, ['whereStatus', 'whereIdIn', 'whereIdNotIn'])) { | |
$builder = static::builder(); | |
return call_user_func_array([$builder, $name], $arguments); | |
} | |
trigger_error('Call to undefined method ' . self::class . '::' . $name . '()', E_USER_ERROR); | |
} | |
/** | |
* Create a QueryBuilder scoped to this taxonomy. | |
* | |
* @return \App\Models\Taxonomy\QueryBuilder | |
*/ | |
public static function builder(): ScopedQueryBuilder | |
{ | |
return new ScopedQueryBuilder(static::class); | |
} | |
/** | |
* Return the key used to register the taxonomy with WordPress | |
* First parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return string | |
*/ | |
public static function getTaxonomy(): string | |
{ | |
return ''; | |
} | |
/** | |
* Return the object type to use to register the taxonomy with WordPress | |
* Second parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return array|string | |
*/ | |
protected static function getTaxonomyObjectType() | |
{ | |
return []; | |
} | |
/** | |
* Return the config to use to register the taxonomy with WordPress | |
* Second parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return array | |
*/ | |
protected static function getTaxonomyConfig(): array | |
{ | |
return []; | |
} | |
/** | |
* Register this taxonomy with WordPress, | |
* | |
* @return void | |
*/ | |
public static function register(): void | |
{ | |
$taxonomy = static::getTaxonomy(); | |
$objectType = static::getTaxonomyObjectType(); | |
$config = static::getTaxonomyConfig(); | |
if (empty($taxonomy)) { | |
throw new TaxonomyRegistrationException('Taxonomy not set'); | |
} | |
if (empty($objectType)) { | |
throw new TaxonomyRegistrationException('Object type not set'); | |
} | |
if (empty($config)) { | |
throw new TaxonomyRegistrationException('Config not set'); | |
} | |
register_taxonomy($taxonomy, $objectType, $config); | |
} | |
/** | |
* Get all posts of this taxonomy | |
* | |
* @param int $perPage The number of items to return (defaults to all) | |
* @param string $orderby | |
* @param string $order | |
* @return \Tightenco\Collect\Support\Collection | |
*/ | |
public static function all(int $perPage = -1, string $orderby = 'menu_order', string $order = 'ASC'): Collection | |
{ | |
$order = strtoupper($order); | |
$args = [ | |
'posts_per_page' => $perPage, | |
'orderby' => $orderby, | |
'order' => $order, | |
]; | |
return static::query($args); | |
} | |
/** | |
* Convenience function that takes a standard set of WP_Query arguments but mixes it with | |
* arguments that mean we're selecting the right taxonomy. | |
* | |
* @param array $args standard WP_Query array | |
* @return \Tightenco\Collect\Support\Collection | |
*/ | |
public static function query(?array $args = null): Collection | |
{ | |
$args = is_array($args) ? $args : []; | |
// Set the correct taxonomy | |
$args = array_merge($args, ['taxonomy' => static::getTaxonomy()]); | |
if (!isset($args['hide_empty'])) { | |
$args['hide_empty'] = true; | |
} | |
return static::get_terms($args); | |
} | |
/** | |
* Raw query function that uses the arguments provided to make a call to Timber::get_terms | |
* and casts the returning data in instances of ourself. | |
* | |
* @param array $args standard WP_Query array | |
* @return \Tightenco\Collect\Support\Collection | |
*/ | |
private static function get_terms(?array $args = null): Collection // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps | |
{ | |
return collect(Timber::get_terms($args, static::class)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\Taxonomy; | |
use App\Models\Traits\ACF; | |
class Category extends Base | |
{ | |
use ACF; | |
/** | |
* Return the key used to register the taxonomy with WordPress | |
* First parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return string | |
*/ | |
public static function getTaxonomy(): string | |
{ | |
return 'category'; | |
} | |
/** | |
* Return the config to use to register the taxonomy with WordPress | |
* Second parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return array | |
*/ | |
protected static function getTaxonomyConfig(): array | |
{ | |
return []; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\Taxonomy; | |
use App\Models\Traits\ACF; | |
class Custom extends Base | |
{ | |
use ACF; | |
/** | |
* Return the key used to register the taxonomy with WordPress | |
* First parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return string | |
*/ | |
public static function getTaxonomy(): string | |
{ | |
return 'custom'; | |
} | |
/** | |
* Return the object type to use to register the taxonomy with WordPress | |
* Second parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return array|string | |
*/ | |
protected static function getTaxonomyObjectType() | |
{ | |
return [ | |
'post', | |
'page', | |
]; | |
} | |
/** | |
* Return the config to use to register the taxonomy with WordPress | |
* Second parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return array | |
*/ | |
protected static function getTaxonomyConfig(): array | |
{ | |
return [ | |
'label' => 'Custom Taxonomy', | |
'labels' => [ | |
'name' => 'Custom Taxonomies', | |
'singular_name' => 'Custom Taxonomy', | |
], | |
'description' => '', | |
'hierarchical' => true, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_rest' => true, | |
'rewrite' => false, | |
'sort' => true, | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\Taxonomy; | |
use App\Models\Traits\ACF; | |
class Tag extends Base | |
{ | |
use ACF; | |
/** | |
* Return the key used to register the taxonomy with WordPress | |
* First parameter of the `register_taxonomy` function: | |
* https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* | |
* @return string | |
*/ | |
public static function getTaxonomy(): string | |
{ | |
return 'post_tag'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment