Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom taxonomy support #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Exceptions/TaxonomyRegistrationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rareloop\Lumberjack\Exceptions;

class TaxonomyRegistrationException extends \Exception
{

}
17 changes: 17 additions & 0 deletions src/Providers/CustomTaxonomyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rareloop\Lumberjack\Providers;

use Rareloop\Lumberjack\Config;

class CustomTaxonomyServiceProvider extends ServiceProvider
{
public function boot(Config $config)
{
$taxonomiesToRegister = $config->get('taxonomies.register');

foreach ($taxonomiesToRegister as $taxonomy) {
$taxonomy::register();
}
}
}
156 changes: 156 additions & 0 deletions src/Term.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Rareloop\Lumberjack;

use Rareloop\Lumberjack\Exceptions\TaxonomyRegistrationException;
use Spatie\Macroable\Macroable;
use Timber\Term as TimberTerm;
use Timber\Timber;

class Term extends TimberTerm
{
use Macroable {
Macroable::__call as __macroableCall;
Macroable::__callStatic as __macroableCallStatic;
}

public function __construct($tid = null, $tax = '', $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) {
parent::__construct($tid, $tax);
}
}

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);
}

trigger_error('Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR);
}

/**
* Return the key used to register the taxonomy with WordPress
* First parameter of the `register_taxonomy` function:
* https://developer.wordpress.org/reference/functions/register_taxonomy/
*
* @return string
*/
public static function getTaxonomyType()
{
return null;
}

/**
* Return the object type which use this taxonomy.
* Second parameter of the `register_taxonomy` function:
* https://developer.wordpress.org/reference/functions/register_taxonomy/
*
* @return array|null
*/
public static function getTaxonomyObjectTypes()
{
return ['post'];
}

/**
* Return the config to use to register the taxonomy with WordPress
* Third parameter of the `register_taxonomy` function:
* https://developer.wordpress.org/reference/functions/register_taxonomy/
*
* @return array|null
*/
protected static function getTaxonomyConfig()
{
return null;
}

/**
* Register this PostType with WordPress
*
* @return void
*/
public static function register()
{
$taxonomyType = static::getTaxonomyType();
$taxonomyObjectTypes = static::getTaxonomyObjectTypes();
$config = static::getTaxonomyConfig();

if (empty($taxonomyType)) {
throw new TaxonomyRegistrationException('Taxonomy type not set');
}

if (empty($taxonomyObjectTypes)) {
throw new TaxonomyRegistrationException('Taxonomy object type not set');
}

if (empty($config)) {
throw new TaxonomyRegistrationException('Config not set');
}

register_taxonomy($taxonomyType, $taxonomyObjectTypes, $config);
}

/**
* Get all terms of this taxonomy
*
* @param string $orderby Field(s) to order terms by (defaults to term_order)
* @param string $order Whether to order terms in ascending or descending order (defaults to ASC)
* @return Illuminate\Support\Collection
*/
public static function all($orderby = 'term_order', $order = 'ASC')
{
$order = strtoupper($order);

$args = [
'orderby' => $orderby,
'order' => $order,
];

return static::query($args);
}


/**
* Convenience function that takes a standard set of WP_Term_Query arguments but mixes it with
* arguments that mean we're selecting the right taxonomy type
*
* @param array $args standard WP_Term_Query array
* @return Illuminate\Support\Collection
*/
public static function query($args = null)
{
$args = is_array($args) ? $args : [];

// Set the correct post type
$args = array_merge($args, ['taxonomy' => static::getTaxonomyType()]);

return static::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 Illuminate\Support\Collection
*/
private static function terms($args = null)
{
return collect(Timber::get_terms($args, [], get_called_class()));
}
}
76 changes: 76 additions & 0 deletions tests/Unit/Providers/CustomTaxonomyServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Rareloop\Lumberjack\Test;

use Brain\Monkey\Functions;
use Mockery;
use PHPUnit\Framework\TestCase;
use Rareloop\Lumberjack\Application;
use Rareloop\Lumberjack\Config;
use Rareloop\Lumberjack\Term;
use Rareloop\Lumberjack\Providers\CustomTaxonomyServiceProvider;
use Rareloop\Lumberjack\Test\Unit\BrainMonkeyPHPUnitIntegration;

class CustomTaxonomyServiceProviderTest extends TestCase
{
use BrainMonkeyPHPUnitIntegration;

/** @test */
public function should_call_register_taxonomy_for_each_configured_taxonomy()
{
$app = new Application(__DIR__ . '/..');

$config = new Config;

$config->set('taxonomies.register', [
CustomTaxonomy1::class,
CustomTaxonomy2::class,
]);

Functions\expect('register_taxonomy')
->times(2);

$provider = new CustomTaxonomyServiceProvider($app);
$provider->boot($config);
}
}

class CustomTaxonomy1 extends Term
{
public static function getTaxonomyType()
{
return 'custom_taxonomy_1';
}

public static function getTaxonomyObjectTypes()
{
return ['post'];
}

protected static function getTaxonomyConfig()
{
return [
'not' => 'empty',
];
}
}

class CustomTaxonomy2 extends Term
{
public static function getTaxonomyType()
{
return 'custom_taxonomy_1';
}

public static function getTaxonomyObjectTypes()
{
return ['post'];
}

protected static function getTaxonomyConfig()
{
return [
'not' => 'empty',
];
}
}
Loading