Skip to content

Commit d4825dd

Browse files
authored
Merge pull request #97 from jjgrainger/feature/add-registrar-classes
Add Registrar Classes
2 parents 2f20f62 + 5b812fd commit d4825dd

9 files changed

+509
-321
lines changed

.editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
indent_style = space

src/PostType.php

+2-198
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PostTypes;
44

55
use PostTypes\Columns;
6+
use PostTypes\Registrars\PostTypeRegistrar;
67

78
/**
89
* PostType
@@ -215,67 +216,7 @@ public function columns()
215216
*/
216217
public function register()
217218
{
218-
// register the PostType
219-
if (!post_type_exists($this->name)) {
220-
add_action('init', [$this, 'registerPostType']);
221-
} else {
222-
add_filter('register_post_type_args', [$this, 'modifyPostType'], 10, 2);
223-
}
224-
225-
// register Taxonomies to the PostType
226-
add_action('init', [$this, 'registerTaxonomies']);
227-
228-
// modify filters on the admin edit screen
229-
add_action('restrict_manage_posts', [$this, 'modifyFilters']);
230-
231-
if (isset($this->columns)) {
232-
// modify the admin edit columns.
233-
add_filter("manage_{$this->name}_posts_columns", [$this, 'modifyColumns'], 10, 1);
234-
235-
// populate custom columns
236-
add_filter("manage_{$this->name}_posts_custom_column", [$this, 'populateColumns'], 10, 2);
237-
238-
// run filter to make columns sortable.
239-
add_filter('manage_edit-'.$this->name.'_sortable_columns', [$this, 'setSortableColumns']);
240-
241-
// run action that sorts columns on request.
242-
add_action('pre_get_posts', [$this, 'sortSortableColumns']);
243-
}
244-
}
245-
246-
/**
247-
* Register the PostType
248-
* @return void
249-
*/
250-
public function registerPostType()
251-
{
252-
// create options for the PostType
253-
$options = $this->createOptions();
254-
255-
// check that the post type doesn't already exist
256-
if (!post_type_exists($this->name)) {
257-
// register the post type
258-
register_post_type($this->name, $options);
259-
}
260-
}
261-
262-
/**
263-
* Modify the existing Post Type.
264-
*
265-
* @return array
266-
*/
267-
public function modifyPostType(array $args, string $posttype)
268-
{
269-
if ($posttype !== $this->name) {
270-
return $args;
271-
}
272-
273-
// create options for the PostType
274-
$options = $this->createOptions();
275-
276-
$args = array_replace_recursive($args, $options);
277-
278-
return $args;
219+
(new PostTypeRegistrar($this))->register();
279220
}
280221

281222
/**
@@ -380,73 +321,6 @@ public function createLabels()
380321
return array_replace_recursive($labels, $this->labels);
381322
}
382323

383-
/**
384-
* Register Taxonomies to the PostType
385-
* @return void
386-
*/
387-
public function registerTaxonomies()
388-
{
389-
if (!empty($this->taxonomies)) {
390-
foreach ($this->taxonomies as $taxonomy) {
391-
register_taxonomy_for_object_type($taxonomy, $this->name);
392-
}
393-
}
394-
}
395-
396-
/**
397-
* Modify and display filters on the admin edit screen
398-
* @param string $posttype The current screen post type
399-
* @return void
400-
*/
401-
public function modifyFilters($posttype)
402-
{
403-
// first check we are working with the this PostType
404-
if ($posttype === $this->name) {
405-
// calculate what filters to add
406-
$filters = $this->getFilters();
407-
408-
foreach ($filters as $taxonomy) {
409-
// if the taxonomy doesn't exist, ignore it
410-
if (!taxonomy_exists($taxonomy)) {
411-
continue;
412-
}
413-
414-
// If the taxonomy is not registered to the post type, continue.
415-
if (!is_object_in_taxonomy($this->name, $taxonomy)) {
416-
continue;
417-
}
418-
419-
// get the taxonomy object
420-
$tax = get_taxonomy($taxonomy);
421-
422-
// start the html for the filter dropdown
423-
$selected = null;
424-
425-
if (isset($_GET[$taxonomy])) {
426-
$selected = sanitize_title($_GET[$taxonomy]);
427-
}
428-
429-
$dropdown_args = [
430-
'name' => $taxonomy,
431-
'value_field' => 'slug',
432-
'taxonomy' => $tax->name,
433-
'show_option_all' => $tax->labels->all_items,
434-
'hierarchical' => $tax->hierarchical,
435-
'selected' => $selected,
436-
'orderby' => 'name',
437-
'hide_empty' => 0,
438-
'show_count' => 0,
439-
];
440-
441-
// Output screen reader label.
442-
echo '<label class="screen-reader-text" for="cat">' . $tax->labels->filter_by_item . '</label>';
443-
444-
// Output dropdown for taxonomy.
445-
wp_dropdown_categories($dropdown_args);
446-
}
447-
}
448-
}
449-
450324
/**
451325
* Calculate the filters for the PostType
452326
* @return array
@@ -470,74 +344,4 @@ public function getFilters()
470344

471345
return $filters;
472346
}
473-
474-
/**
475-
* Modify the columns for the PostType
476-
* @param array $columns Default WordPress columns
477-
* @return array The modified columns
478-
*/
479-
public function modifyColumns($columns)
480-
{
481-
$columns = $this->columns->modifyColumns($columns);
482-
483-
return $columns;
484-
}
485-
486-
/**
487-
* Populate custom columns for the PostType
488-
* @param string $column The column slug
489-
* @param int $post_id The post ID
490-
*/
491-
public function populateColumns($column, $post_id)
492-
{
493-
if (isset($this->columns->populate[$column])) {
494-
call_user_func_array($this->columns()->populate[$column], [$column, $post_id]);
495-
}
496-
}
497-
498-
/**
499-
* Make custom columns sortable
500-
* @param array $columns Default WordPress sortable columns
501-
*/
502-
public function setSortableColumns($columns)
503-
{
504-
if (!empty($this->columns()->sortable)) {
505-
$columns = array_merge($columns, $this->columns()->sortable);
506-
}
507-
508-
return $columns;
509-
}
510-
511-
/**
512-
* Set query to sort custom columns
513-
* @param WP_Query $query
514-
*/
515-
public function sortSortableColumns($query)
516-
{
517-
// don't modify the query if we're not in the post type admin
518-
if (!is_admin() || $query->get('post_type') !== $this->name) {
519-
return;
520-
}
521-
522-
$orderby = $query->get('orderby');
523-
524-
// if the sorting a custom column
525-
if ($this->columns()->isSortable($orderby)) {
526-
// get the custom column options
527-
$meta = $this->columns()->sortableMeta($orderby);
528-
529-
// determine type of ordering
530-
if (is_string($meta) or !$meta[1]) {
531-
$meta_key = $meta;
532-
$meta_value = 'meta_value';
533-
} else {
534-
$meta_key = $meta[0];
535-
$meta_value = 'meta_value_num';
536-
}
537-
538-
// set the custom order
539-
$query->set('meta_key', $meta_key);
540-
$query->set('orderby', $meta_value);
541-
}
542-
}
543347
}

0 commit comments

Comments
 (0)