|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Registration Registrar for all custom Taxonomies. |
|
7
|
|
|
* |
|
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Glynn Quelch <[email protected]> |
|
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
22
|
|
|
* @package PinkCrab\Registerables |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace PinkCrab\Registerables\Registrar; |
|
26
|
|
|
|
|
27
|
|
|
use Exception; |
|
28
|
|
|
use PinkCrab\Registerables\Taxonomy; |
|
29
|
|
|
use PinkCrab\Registerables\Registerable_Hooks; |
|
30
|
|
|
use PinkCrab\Registerables\Registrar\Registrar; |
|
31
|
|
|
use PinkCrab\Registerables\Validator\Taxonomy_Validator; |
|
32
|
|
|
use PinkCrab\Registerables\Registrar\Meta_Data_Registrar; |
|
33
|
|
|
use PinkCrab\Registerables\Module\Middleware\Registerable; |
|
34
|
|
|
|
|
35
|
|
|
class Taxonomy_Registrar implements Registrar { |
|
36
|
|
|
|
|
37
|
|
|
protected Taxonomy_Validator $validator; |
|
38
|
|
|
protected Meta_Data_Registrar $meta_data_registrar; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
Taxonomy_Validator $validator, |
|
42
|
|
|
Meta_Data_Registrar $meta_data_registrar |
|
43
|
|
|
) { |
|
44
|
|
|
$this->validator = $validator; |
|
45
|
|
|
$this->meta_data_registrar = $meta_data_registrar; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Register a taxonomy |
|
50
|
|
|
* |
|
51
|
|
|
* @param \PinkCrab\Registerables\Module\Middleware\Registerable $registerable |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function register( Registerable $registerable ): void { |
|
55
|
|
|
/** |
|
56
|
|
|
* @var Taxonomy $registerable, Validation call below catches no Post_Type Registerables |
|
57
|
|
|
*/ |
|
58
|
|
|
|
|
59
|
|
|
if ( ! $this->validator->validate( $registerable ) ) { |
|
60
|
|
|
throw new Exception( |
|
61
|
|
|
sprintf( |
|
62
|
|
|
'Failed validating taxonomy model(%s) with errors: %s', |
|
63
|
|
|
esc_html( get_class( $registerable ) ), |
|
64
|
|
|
esc_html( join( ', ', $this->validator->get_errors() ) ) |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Attempt to register the taxonomy. |
|
70
|
|
|
try { |
|
71
|
|
|
$result = \register_taxonomy( |
|
72
|
|
|
$registerable->slug, |
|
|
|
|
|
|
73
|
|
|
$registerable->object_type, |
|
|
|
|
|
|
74
|
|
|
/* @phpstan-ignore-next-line */ |
|
75
|
|
|
$this->compile_args( $registerable ) |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
if ( is_a( $result, \WP_Error::class ) ) { |
|
79
|
|
|
throw new Exception( join( $result->get_error_messages() ) ); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} catch ( \Throwable $th ) { |
|
82
|
|
|
throw new Exception( esc_html( "Failed to register {$registerable->slug} taxonomy ({$th->getMessage()})" ) ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Register any associated meta data. |
|
86
|
|
|
$this->register_meta_data( $registerable ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Registers all meta data for taxonomy. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \PinkCrab\Registerables\Taxonomy $taxonomy |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function register_meta_data( Taxonomy $taxonomy ): void { |
|
96
|
|
|
|
|
97
|
|
|
// Get all meta fields for taxonomy. |
|
98
|
|
|
$meta_fields = $taxonomy->meta_data( array() ); |
|
99
|
|
|
|
|
100
|
|
|
// Attempt to register all Meta for taxonomy. |
|
101
|
|
|
try { |
|
102
|
|
|
foreach ( $meta_fields as $meta_field ) { |
|
103
|
|
|
$this->meta_data_registrar->register_for_term( $meta_field, $taxonomy->slug ); |
|
104
|
|
|
} |
|
105
|
|
|
} catch ( \Throwable $th ) { |
|
106
|
|
|
throw new Exception( esc_html( $th->getMessage() ) ); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Compiles the args used to register the taxonomy. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \PinkCrab\Registerables\Taxonomy $taxonomy |
|
114
|
|
|
* @return array<string, string|int|array<string, string>> |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function compile_args( Taxonomy $taxonomy ): array { |
|
117
|
|
|
// Create the labels. |
|
118
|
|
|
$base_labels = array( |
|
119
|
|
|
'name' => $taxonomy->plural, |
|
120
|
|
|
'singular_name' => $taxonomy->singular, |
|
121
|
|
|
'menu_name' => \ucfirst( \esc_attr( $taxonomy->plural ?? '' ) ), |
|
122
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
123
|
|
|
'search_items' => wp_sprintf( _x( 'Search %s', 'Label for searching plural items. Default is ‘Search {taxonomy plural name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->plural ?? '' ) ), |
|
124
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
125
|
|
|
'popular_items' => wp_sprintf( _x( 'Popular %s', 'Label for the popular terms', 'pinkcrab' ), \esc_attr( $taxonomy->plural ?? '' ) ), |
|
126
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
127
|
|
|
'edit_item' => wp_sprintf( _x( 'Edit %s', 'Label for editing a singular item. Default is ‘Edit {taxonomy singular name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
128
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
129
|
|
|
'view_item' => wp_sprintf( _x( 'View %s', 'Label for viewing a singular item. Default is ‘View {taxonomy singular name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
130
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
131
|
|
|
'update_item' => wp_sprintf( _x( 'Update %s', 'Label for editing a singular item. Default is ‘Edit {taxonomy singular name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
132
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
133
|
|
|
'add_new_item' => wp_sprintf( _x( 'Add New %s', 'Label for adding a new singular item. Default is ‘Add New {taxonomy singular name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
134
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
135
|
|
|
'new_item_name' => wp_sprintf( _x( 'New %s', 'Label for the new item page title. Default is ‘New {taxonomy singular name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
136
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
137
|
|
|
'not_found' => wp_sprintf( _x( 'No %s found', 'Label used when no items are found. Default is ‘No {taxonomy plural name} found’.', 'pinkcrab' ), \esc_attr( $taxonomy->plural ?? '' ) ), |
|
138
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
139
|
|
|
'items_list' => wp_sprintf( _x( '%s list', 'Label for the table hidden heading. Default is ‘{taxonomy plural name} list’.', 'pinkcrab' ), \ucfirst( \esc_attr( $taxonomy->plural ?? '' ) ) ), |
|
140
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
141
|
|
|
'items_list_navigation' => wp_sprintf( _x( '%s list navigation', 'Label for the pagination hidden heading. Default is ‘{taxonomy plural name} list’.', 'pinkcrab' ), \ucfirst( \esc_attr( $taxonomy->plural ?? '' ) ) ), |
|
142
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
143
|
|
|
'all_items' => wp_sprintf( _x( 'All %s', 'Label for the pagination hidden heading. Default is ‘{taxonomy plural name} list’.', 'pinkcrab' ), \ucfirst( \esc_attr( $taxonomy->plural ?? '' ) ) ), |
|
144
|
|
|
'most_used' => _x( 'Most Used', 'Title for the Most Used tab. Default \'Most Used\'.', 'pinkcrab' ), |
|
145
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
146
|
|
|
'back_to_items' => wp_sprintf( _x( '← Back to %s', 'Label for the pagination hidden heading. Default is ‘{taxonomy plural name} list’.', 'pinkcrab' ), \ucfirst( \esc_attr( $taxonomy->plural ?? '' ) ) ), |
|
147
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
148
|
|
|
'item_link' => wp_sprintf( _x( '%s Link', 'Title for a navigation link block variation. Default is ‘{taxonomy singular name} Link’.', 'pinkcrab' ), \ucfirst( \esc_attr( $taxonomy->singular ?? '' ) ) ), |
|
149
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
150
|
|
|
'item_link_description' => wp_sprintf( _x( 'A link to a %s', 'Description for a navigation link block variation. Default is ‘A link to a {taxonomy singular name}’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
$tag_labels = array( |
|
154
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
155
|
|
|
'separate_items_with_commas' => wp_sprintf( _x( 'Separate %s with commas', 'This label is only used for non-hierarchical taxonomies. Default \'Separate {taxonomy plural name} with commas\', used in the meta box.’.', 'pinkcrab' ), \esc_attr( $taxonomy->plural ?? '' ) ), |
|
156
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
157
|
|
|
'add_or_remove_items' => wp_sprintf( _x( 'Add or remove %s', 'This label is only used for non-hierarchical taxonomies. Default \'Add or remove {taxonomy plural name}\', used in the meta box when JavaScript is disabled.', 'pinkcrab' ), \esc_attr( $taxonomy->plural ?? '' ) ), |
|
158
|
|
|
/* translators: %s: Taxonomy plural name */ |
|
159
|
|
|
'choose_from_most_used' => wp_sprintf( _x( 'Add or remove %s', 'This label is only used on non-hierarchical taxonomies. Default\'Choose from the most used {taxonomy plural name}\', used in the meta box.', 'pinkcrab' ), \esc_attr( $taxonomy->plural ?? '' ) ), |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
$hierarchical_labels = array( |
|
163
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
164
|
|
|
'parent_item_colon' => wp_sprintf( _x( 'Parent %s:', 'Label used to prefix parents of hierarchical items. Not used on non-hierarchical taxonomys. Default is ‘Parent {taxonomy plural name}:’.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
165
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
166
|
|
|
'parent_item' => wp_sprintf( _x( 'Parent %s', 'Label for the parent term', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
167
|
|
|
/* translators: %s: Taxonomy singular name */ |
|
168
|
|
|
'filter_by_item' => wp_sprintf( _x( 'Filter by %s', 'This label is only used for hierarchical taxonomies. Default \'Filter by {taxonomy singular name}\', used in the posts list table.', 'pinkcrab' ), \esc_attr( $taxonomy->singular ?? '' ) ), |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
$labels = array_merge( |
|
172
|
|
|
$base_labels, |
|
173
|
|
|
$taxonomy->hierarchical ? $hierarchical_labels : $tag_labels |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Allow 3rd party plugins to filter the labels also. |
|
178
|
|
|
* |
|
179
|
|
|
* @filter_handle PinkCrab/Registerable/taxonomy_labels |
|
180
|
|
|
* @param array<string, string> $labels |
|
181
|
|
|
* @param Taxonomy $taxonomy |
|
182
|
|
|
* @return array<string, string> |
|
183
|
|
|
*/ |
|
184
|
|
|
$labels = apply_filters( Registerable_Hooks::TAXONOMY_LABELS, $taxonomy->filter_labels( $labels ), $taxonomy ); |
|
185
|
|
|
|
|
186
|
|
|
// Compose args. |
|
187
|
|
|
$args = array( |
|
188
|
|
|
'labels' => $labels, |
|
189
|
|
|
'publicly_queryable' => $taxonomy->publicly_queryable, |
|
190
|
|
|
'show_ui' => $taxonomy->show_ui, |
|
191
|
|
|
'show_in_menu' => $taxonomy->show_in_menu, |
|
192
|
|
|
'show_in_nav_menus' => $taxonomy->public, |
|
193
|
|
|
'show_in_rest' => $taxonomy->show_in_rest, |
|
194
|
|
|
'rest_base' => $taxonomy->rest_base ?? $taxonomy->slug, |
|
195
|
|
|
'rest_controller_class' => $taxonomy->rest_controller_class, |
|
196
|
|
|
'show_tagcloud' => $taxonomy->show_tagcloud, |
|
197
|
|
|
'show_in_quick_edit' => $taxonomy->show_in_quick_edit, |
|
198
|
|
|
'show_admin_column' => $taxonomy->show_admin_column, |
|
199
|
|
|
'sort' => $taxonomy->sort, |
|
200
|
|
|
'description' => $taxonomy->description, |
|
201
|
|
|
'rewrite' => $taxonomy->slug, |
|
202
|
|
|
'label' => $taxonomy->label ?? $taxonomy->plural, |
|
203
|
|
|
'query_var' => $taxonomy->query_var, |
|
204
|
|
|
'hierarchical' => $taxonomy->hierarchical, |
|
205
|
|
|
'update_count_callback' => $taxonomy->update_count_callback ?? '_update_post_term_count', |
|
206
|
|
|
'meta_box_cb' => $taxonomy->meta_box_cb ?? |
|
207
|
|
|
$taxonomy->hierarchical ? 'post_categories_meta_box' : 'post_tags_meta_box', |
|
208
|
|
|
'default_term' => $taxonomy->default_term, |
|
209
|
|
|
); |
|
210
|
|
|
|
|
211
|
|
|
// Merge existing capabilities with the new ones. |
|
212
|
|
|
$args['capabilities'] = array_merge( |
|
213
|
|
|
array( |
|
214
|
|
|
'manage_terms' => 'manage_categories', |
|
215
|
|
|
'edit_terms' => 'manage_categories', |
|
216
|
|
|
'delete_terms' => 'manage_categories', |
|
217
|
|
|
'assign_terms' => 'edit_posts', |
|
218
|
|
|
), |
|
219
|
|
|
$taxonomy->capabilities ?? array() |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Allow 3rd party plugins to filter this also. |
|
224
|
|
|
* |
|
225
|
|
|
* @filter_handle PinkCrab/Registerable/taxonomy_args |
|
226
|
|
|
* @param array<string, string|bool|int|null|array<string, string>> $args |
|
227
|
|
|
* @param Taxonomy $taxonomy |
|
228
|
|
|
* @return array<string, string|bool|int|null|array<string, string>> |
|
229
|
|
|
*/ |
|
230
|
|
|
/* @phpstan-ignore-next-line, this is due to apply_filters type hints being wrong. */ |
|
231
|
|
|
return apply_filters( Registerable_Hooks::TAXONOMY_ARGS, $taxonomy->filter_args( $args ), $taxonomy ); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|