Skip to content

Commit

Permalink
create TaxonomyRegistrar class
Browse files Browse the repository at this point in the history
  • Loading branch information
jjgrainger committed Oct 24, 2024
1 parent 7e950e7 commit acac675
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 121 deletions.
145 changes: 145 additions & 0 deletions src/Registrars/TaxonomyRegistrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace PostTypes\Registrars;

use PostTypes\Taxonomy;

class TaxonomyRegistrar
{
protected $taxonomy;

public function __construct(Taxonomy $taxonomy)
{
$this->taxonomy = $taxonomy;
}

public function register()
{
// Get the Taxonomy name.
$name = $this->taxonomy->name;

// Register the taxonomy, set priority to 9 so taxonomies are registered before PostTypes
add_action('init', [$this, 'registerTaxonomy'], 9);

// Assign taxonomy to post type objects
add_action('init', [$this, 'registerTaxonomyToObjects'], 10);

if (isset($this->taxonomy->columns)) {
// Modify the columns for the Taxonomy
add_filter("manage_edit-' . $name . '_columns", [$this, 'modifyColumns']);

// populate the columns for the Taxonomy
add_filter('manage_' . $name . '_custom_column', [$this, 'populateColumns'], 10, 3);

// set custom sortable columns
add_filter('manage_edit-' . $name . '_sortable_columns', [$this, 'setSortableColumns']);

// run action that sorts columns on request
add_action('parse_term_query', [$this, 'sortSortableColumns']);
}
}

/**
* Register the Taxonomy to WordPress
* @return void
*/
public function registerTaxonomy()
{
// Get the existing taxonomy options if it exists.
$options = (taxonomy_exists($this->taxonomy->name)) ? (array) get_taxonomy($this->taxonomy->name) : [];

// create options for the Taxonomy.
$options = array_replace_recursive($options, $this->taxonomy->createOptions());

// register the Taxonomy with WordPress.
register_taxonomy($this->taxonomy->name, null, $options);
}

/**
* Register the Taxonomy to PostTypes
* @return void
*/
public function registerTaxonomyToObjects()
{
// register Taxonomy to each of the PostTypes assigned
if (empty($this->taxonomy->posttypes)) {
return;
}

foreach ($this->taxonomy->posttypes as $posttype) {
register_taxonomy_for_object_type($this->taxonomy->name, $posttype);
}
}

/**
* Modify the columns for the Taxonomy
* @param array $columns The WordPress default columns
* @return array
*/
public function modifyColumns($columns)
{
return $this->taxonomy->columns->modifyColumns($columns);
}

/**
* Populate custom columns for the Taxonomy
* @param string $content
* @param string $column
* @param int $term_id
*/
public function populateColumns($content, $column, $term_id)
{
if (isset($this->taxonomy->columns->populate[$column])) {
$content = call_user_func_array(
$this->taxonomy->columns()->populate[$column],
[$content, $column, $term_id]
);
}

return $content;
}

/**
* Make custom columns sortable
* @param array $columns Default WordPress sortable columns
*/
public function setSortableColumns($columns)
{
if (!empty($this->taxonomy->columns()->sortable)) {
$columns = array_merge($columns, $this->taxonomy->columns()->sortable);
}

return $columns;
}

/**
* Set query to sort custom columns
* @param WP_Term_Query $query
*/
public function sortSortableColumns($query)
{
// don't modify the query if we're not in the post type admin
if (!is_admin() || !in_array($this->taxonomy->name, $query->query_vars['taxonomy'] ?? [])) {
return;
}

// check the orderby is a custom ordering
if (isset($_GET['orderby']) && array_key_exists($_GET['orderby'], $this->taxonomy->columns()->sortable)) {
// get the custom sorting options
$meta = $this->taxonomy->columns()->sortable[$_GET['orderby']];

// check ordering is not numeric
if (is_string($meta)) {
$meta_key = $meta;
$orderby = 'meta_value';
} else {
$meta_key = $meta[0];
$orderby = 'meta_value_num';
}

// set the sort order
$query->query_vars['orderby'] = $orderby;
$query->query_vars['meta_key'] = $meta_key;
}
}
}
123 changes: 2 additions & 121 deletions src/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PostTypes;

use PostTypes\Columns;
use PostTypes\Registrars\TaxonomyRegistrar;

/**
* Taxonomy
Expand Down Expand Up @@ -162,56 +163,7 @@ public function columns()
*/
public function register()
{
// register the taxonomy, set priority to 9
// so taxonomies are registered before PostTypes
add_action('init', [$this, 'registerTaxonomy'], 9);

// assign taxonomy to post type objects
add_action('init', [$this, 'registerTaxonomyToObjects']);

if (isset($this->columns)) {
// modify the columns for the Taxonomy
add_filter("manage_edit-{$this->name}_columns", [$this, 'modifyColumns']);

// populate the columns for the Taxonomy
add_filter("manage_{$this->name}_custom_column", [$this, 'populateColumns'], 10, 3);

// set custom sortable columns
add_filter("manage_edit-{$this->name}_sortable_columns", [$this, 'setSortableColumns']);

// run action that sorts columns on request
add_action('parse_term_query', [$this, 'sortSortableColumns']);
}
}

/**
* Register the Taxonomy to WordPress
* @return void
*/
public function registerTaxonomy()
{
// Get the existing taxonomy options if it exists.
$options = (taxonomy_exists($this->name)) ? (array) get_taxonomy($this->name) : [];

// create options for the Taxonomy.
$options = array_replace_recursive($options, $this->createOptions());

// register the Taxonomy with WordPress.
register_taxonomy($this->name, null, $options);
}

/**
* Register the Taxonomy to PostTypes
* @return void
*/
public function registerTaxonomyToObjects()
{
// register Taxonomy to each of the PostTypes assigned
if (!empty($this->posttypes)) {
foreach ($this->posttypes as $posttype) {
register_taxonomy_for_object_type($this->name, $posttype);
}
}
(new TaxonomyRegistrar($this))->register();
}

/**
Expand Down Expand Up @@ -310,75 +262,4 @@ public function createLabels()

return array_replace($labels, $this->labels);
}

/**
* Modify the columns for the Taxonomy
* @param array $columns The WordPress default columns
* @return array
*/
public function modifyColumns($columns)
{
$columns = $this->columns->modifyColumns($columns);

return $columns;
}

/**
* Populate custom columns for the Taxonomy
* @param string $content
* @param string $column
* @param int $term_id
*/
public function populateColumns($content, $column, $term_id)
{
if (isset($this->columns->populate[$column])) {
$content = call_user_func_array($this->columns()->populate[$column], [$content, $column, $term_id]);
}

return $content;
}

/**
* Make custom columns sortable
* @param array $columns Default WordPress sortable columns
*/
public function setSortableColumns($columns)
{
if (!empty($this->columns()->sortable)) {
$columns = array_merge($columns, $this->columns()->sortable);
}

return $columns;
}

/**
* Set query to sort custom columns
* @param WP_Term_Query $query
*/
public function sortSortableColumns($query)
{
// don't modify the query if we're not in the post type admin
if (!is_admin() || !in_array($this->name, $query->query_vars['taxonomy'] ?? [])) {
return;
}

// check the orderby is a custom ordering
if (isset($_GET['orderby']) && array_key_exists($_GET['orderby'], $this->columns()->sortable)) {
// get the custom sorting options
$meta = $this->columns()->sortable[$_GET['orderby']];

// check ordering is not numeric
if (is_string($meta)) {
$meta_key = $meta;
$orderby = 'meta_value';
} else {
$meta_key = $meta[0];
$orderby = 'meta_value_num';
}

// set the sort order
$query->query_vars['orderby'] = $orderby;
$query->query_vars['meta_key'] = $meta_key;
}
}
}

0 comments on commit acac675

Please sign in to comment.