Skip to content

Commit

Permalink
Merge pull request #97 from jjgrainger/feature/add-registrar-classes
Browse files Browse the repository at this point in the history
Add Registrar Classes
  • Loading branch information
jjgrainger authored Dec 4, 2024
2 parents 2f20f62 + 5b812fd commit d4825dd
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 321 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
200 changes: 2 additions & 198 deletions src/PostType.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\PostTypeRegistrar;

/**
* PostType
Expand Down Expand Up @@ -215,67 +216,7 @@ public function columns()
*/
public function register()
{
// register the PostType
if (!post_type_exists($this->name)) {
add_action('init', [$this, 'registerPostType']);
} else {
add_filter('register_post_type_args', [$this, 'modifyPostType'], 10, 2);
}

// register Taxonomies to the PostType
add_action('init', [$this, 'registerTaxonomies']);

// modify filters on the admin edit screen
add_action('restrict_manage_posts', [$this, 'modifyFilters']);

if (isset($this->columns)) {
// modify the admin edit columns.
add_filter("manage_{$this->name}_posts_columns", [$this, 'modifyColumns'], 10, 1);

// populate custom columns
add_filter("manage_{$this->name}_posts_custom_column", [$this, 'populateColumns'], 10, 2);

// run filter to make columns sortable.
add_filter('manage_edit-'.$this->name.'_sortable_columns', [$this, 'setSortableColumns']);

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

/**
* Register the PostType
* @return void
*/
public function registerPostType()
{
// create options for the PostType
$options = $this->createOptions();

// check that the post type doesn't already exist
if (!post_type_exists($this->name)) {
// register the post type
register_post_type($this->name, $options);
}
}

/**
* Modify the existing Post Type.
*
* @return array
*/
public function modifyPostType(array $args, string $posttype)
{
if ($posttype !== $this->name) {
return $args;
}

// create options for the PostType
$options = $this->createOptions();

$args = array_replace_recursive($args, $options);

return $args;
(new PostTypeRegistrar($this))->register();
}

/**
Expand Down Expand Up @@ -380,73 +321,6 @@ public function createLabels()
return array_replace_recursive($labels, $this->labels);
}

/**
* Register Taxonomies to the PostType
* @return void
*/
public function registerTaxonomies()
{
if (!empty($this->taxonomies)) {
foreach ($this->taxonomies as $taxonomy) {
register_taxonomy_for_object_type($taxonomy, $this->name);
}
}
}

/**
* Modify and display filters on the admin edit screen
* @param string $posttype The current screen post type
* @return void
*/
public function modifyFilters($posttype)
{
// first check we are working with the this PostType
if ($posttype === $this->name) {
// calculate what filters to add
$filters = $this->getFilters();

foreach ($filters as $taxonomy) {
// if the taxonomy doesn't exist, ignore it
if (!taxonomy_exists($taxonomy)) {
continue;
}

// If the taxonomy is not registered to the post type, continue.
if (!is_object_in_taxonomy($this->name, $taxonomy)) {
continue;
}

// get the taxonomy object
$tax = get_taxonomy($taxonomy);

// start the html for the filter dropdown
$selected = null;

if (isset($_GET[$taxonomy])) {
$selected = sanitize_title($_GET[$taxonomy]);
}

$dropdown_args = [
'name' => $taxonomy,
'value_field' => 'slug',
'taxonomy' => $tax->name,
'show_option_all' => $tax->labels->all_items,
'hierarchical' => $tax->hierarchical,
'selected' => $selected,
'orderby' => 'name',
'hide_empty' => 0,
'show_count' => 0,
];

// Output screen reader label.
echo '<label class="screen-reader-text" for="cat">' . $tax->labels->filter_by_item . '</label>';

// Output dropdown for taxonomy.
wp_dropdown_categories($dropdown_args);
}
}
}

/**
* Calculate the filters for the PostType
* @return array
Expand All @@ -470,74 +344,4 @@ public function getFilters()

return $filters;
}

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

return $columns;
}

/**
* Populate custom columns for the PostType
* @param string $column The column slug
* @param int $post_id The post ID
*/
public function populateColumns($column, $post_id)
{
if (isset($this->columns->populate[$column])) {
call_user_func_array($this->columns()->populate[$column], [$column, $post_id]);
}
}

/**
* 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_Query $query
*/
public function sortSortableColumns($query)
{
// don't modify the query if we're not in the post type admin
if (!is_admin() || $query->get('post_type') !== $this->name) {
return;
}

$orderby = $query->get('orderby');

// if the sorting a custom column
if ($this->columns()->isSortable($orderby)) {
// get the custom column options
$meta = $this->columns()->sortableMeta($orderby);

// determine type of ordering
if (is_string($meta) or !$meta[1]) {
$meta_key = $meta;
$meta_value = 'meta_value';
} else {
$meta_key = $meta[0];
$meta_value = 'meta_value_num';
}

// set the custom order
$query->set('meta_key', $meta_key);
$query->set('orderby', $meta_value);
}
}
}
Loading

0 comments on commit d4825dd

Please sign in to comment.