Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit fbe2513

Browse files
committed
Merge branch 'refs/heads/v1.2.2-rc1'
2 parents 13f4781 + 7d8a493 commit fbe2513

File tree

3 files changed

+56
-43
lines changed

3 files changed

+56
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
##### v1.2.2
4+
* fix issues when registering taxonomy across multiple post type
5+
* remove wrapper function `options_merge`
6+
37
##### v1.2.1
48
* reduce the defaults within the class
59
* replace contents of `options_merge` function with `array_replace_recursive`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WP Custom Post Type Class v1.2.1
1+
# WP Custom Post Type Class v1.2.2
22

33
A single class to help you build more advanced custom post types quickly.
44

src/CPT.php

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
99
@author jjgrainger
1010
@url http://jjgrainger.co.uk
11+
@version 1.2.1
12+
@license http://www.opensource.org/licenses/mit-license.html MIT License
1113
1214
*/
1315

@@ -75,6 +77,16 @@ class CPT {
7577
public $taxonomies;
7678

7779

80+
/*
81+
@var array $taxonomy_settings
82+
public variable that holds an array of the taxonomies associated with the post type and their options
83+
used when registering the taxonomies
84+
assigined on register_taxonomy()
85+
*/
86+
87+
public $taxonomy_settings;
88+
89+
7890

7991
/*
8092
@var array $filters
@@ -117,7 +129,7 @@ class CPT {
117129
/*
118130
@function __contructor(@post_type_name, @options)
119131
120-
@param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singlualr)
132+
@param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singluar)
121133
@param array $options User submitted options
122134
*/
123135

@@ -181,11 +193,18 @@ function __construct($post_type_names, $options = array()) {
181193
// register the post type
182194
$this->add_action('init', array(&$this, 'register_post_type'));
183195

196+
// register taxonomies
197+
$this->add_action('init', array(&$this, 'register_taxonomies'));
198+
184199
// add taxonomy to admin edit columns
185200
$this->add_filter('manage_edit-' . $this->post_type_name . '_columns', array(&$this, 'add_admin_columns'));
186201

187202
// populate the taxonomy columns with the posts terms
188203
$this->add_action('manage_' . $this->post_type_name . '_posts_custom_column', array(&$this, 'populate_admin_columns'), 10, 2);
204+
205+
// add filter select option to admin edit
206+
$this->add_action('restrict_manage_posts', array(&$this, 'add_taxonomy_filters'));
207+
189208
}
190209

191210

@@ -252,23 +271,6 @@ function set($var, $value) {
252271
}
253272

254273

255-
/*
256-
helper function options_merge
257-
merges user submitted options array with default settings
258-
preserving default data if not included in user options
259-
260-
@param array $defaults the default option array
261-
@param array $options user submitted options to add/override defaults
262-
263-
*/
264-
265-
function options_merge($defaults, $options) {
266-
267-
return array_replace_recursive($defaults, $options);
268-
269-
}
270-
271-
272274

273275
/*
274276
helper function add_action
@@ -472,7 +474,7 @@ function register_post_type() {
472474
);
473475

474476
// merge user submitted options with defaults
475-
$options = $this->options_merge($defaults, $this->options);
477+
$options = array_replace_recursive($defaults, $this->options);
476478

477479
// set the object options as full options passed
478480
$this->options = $options;
@@ -582,38 +584,45 @@ function register_taxonomy($taxonomy_names, $options = array()) {
582584
);
583585

584586
// merge default options with user submitted options
585-
$options = $this->options_merge($defaults, $options);
587+
$options = array_replace_recursive($defaults, $options);
586588

587-
// register the taxonomy if it doesn't exist
588-
if(!taxonomy_exists($taxonomy_name)) {
589+
// add the taxonomy to the object array
590+
// this is used to add columns and filters to admin pannel
591+
$this->taxonomies[] = $taxonomy_name;
589592

590-
// register the taxonomy with Wordpress
591-
$this->add_action('init', function() use($taxonomy_name, $post_type, $options) {
592-
register_taxonomy($taxonomy_name, $post_type, $options);
593-
});
593+
// create array used when registering taxonomies
594+
$this->taxonomy_settings[$taxonomy_name] = $options;
594595

596+
}
595597

596-
} else {
597598

598-
// if taxonomy exists, attach exisiting taxonomy to post type
599-
$this->add_action('init', function() use($taxonomy_name, $post_type) {
600-
register_taxonomy_for_object_type($taxonomy_name, $post_type);
601-
});
602599

603-
}
600+
/*
601+
function register_taxonomies
602+
cycles through taxonomies added with the class and registers them
604603
605-
// add the taxonomy to the object array
606-
// this is used to add columns and filters to admin pannel
607-
$this->taxonomies[] = $taxonomy_name;
604+
function is used with add_action
605+
*/
606+
function register_taxonomies() {
608607

609-
// add taxonomy to admin edit columns
610-
$this->add_filter('manage_edit-' . $post_type . '_columns', array(&$this, 'add_admin_columns'));
608+
// foreach taxonomy registered with the post type
609+
foreach($this->taxonomy_settings as $taxonomy_name => $options) {
611610

612-
// populate the taxonomy columns with the posts terms
613-
$this->add_action('manage_' . $post_type . '_posts_custom_column', array(&$this, 'populate_admin_columns'), 10, 2);
611+
// register the taxonomy if it doesn't exist
612+
if(!taxonomy_exists($taxonomy_name)) {
613+
614+
// register the taxonomy with Wordpress
615+
register_taxonomy($taxonomy_name, $this->post_type_name, $options);
616+
617+
618+
} else {
619+
620+
// if taxonomy exists, attach exisiting taxonomy to post type
621+
register_taxonomy_for_object_type($taxonomy_name, $this->post_type_name);
622+
623+
}
624+
}
614625

615-
// add filter select option to admin edit
616-
$this->add_action('restrict_manage_posts', array(&$this, 'add_taxonomy_filters'));
617626

618627
}
619628

@@ -1099,4 +1108,4 @@ function menu_icon($icon = "dashicons-admin-page") {
10991108

11001109
}
11011110

1102-
}
1111+
}

0 commit comments

Comments
 (0)