-
Notifications
You must be signed in to change notification settings - Fork 3
/
register-taxonomy.php
426 lines (352 loc) · 17.1 KB
/
register-taxonomy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
// includes/register-taxonomy
/**
* Prevent direct access to this file.
*
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Sorry, you are not allowed to access this file directly.' );
}
add_action( 'init', 'ddw_btc_register_templates_taxonomy', 20 );
/**
* Register Taxonomy for categorizing Templates for use in Page Builder contexts.
*
* @since 1.0.0
* @since 1.1.0 Make the default label string "Template" filterable.
* @since 1.3.0 Added logic for optionally adding post types at registering,
* for special Block Editor support.
*
* @see ddw_btc_string_default_content_type()
*
* @uses ddw_btc_get_post_types_for_block_editor()
* @uses register_taxonomy()
*/
function ddw_btc_register_templates_taxonomy() {
/** Set taxonomy labels, filterable */
$tax_labels = apply_filters(
'btc/filter/taxonomy/labels',
array(
'name' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
_x( '%s Categories', 'Taxonomy General Name', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'singular_name' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
_x( '%s Category', 'Taxonomy Singular Name', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'menu_name' => __( 'Taxonomy', 'builder-template-categories' ),
'all_items' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'All %s Categories', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'parent_item' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'Parent %s Category', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'parent_item_colon' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'Parent %s Category:', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'new_item_name' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'New %s Category Name', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'add_new_item' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'Add New %s Category', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'edit_item' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'Edit %s Category', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'update_item' => sprintf(
__( 'Update %s Category', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'view_item' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'View %s Category', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'separate_items_with_commas' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'Separate %s Categories with commas', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'add_or_remove_items' => __( 'Add or remove categories', 'builder-template-categories' ),
'choose_from_most_used' => __( 'Choose from the most used', 'builder-template-categories' ),
'popular_items' => __( 'Popular Categories', 'builder-template-categories' ),
'search_items' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'Search %s Categories', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'not_found' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'No %s Categories found.', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
'no_terms' => sprintf(
/* translators: %s - Label name for default content type, "Template" */
__( 'No %s Categories', 'builder-template-categories' ),
ddw_btc_string_default_content_type()
),
//'items_list' => __( 'Items list', 'builder-template-categories' ),
//'items_list_navigation' => __( 'Items list navigation', 'builder-template-categories' ),
)
);
/** Set taxonomy args, filterable */
$args = apply_filters(
'btc/filter/taxonomy/args',
array(
'labels' => $tax_labels, //ddw_btc_get_taxonomy_labels(),
'hierarchical' => TRUE,
'public' => FALSE,
'show_ui' => TRUE,
'show_admin_column' => TRUE,
'show_in_nav_menus' => FALSE,
'show_tagcloud' => FALSE,
'show_in_rest' => TRUE, //FALSE,
'description' => __( 'Template Categories for templates of Page Builders or similar libraries', 'builder-template-categories' )
)
);
/** Set post types to add the taxonomy to */
$post_types = null;
$block_editor_types = ddw_btc_get_post_types_for_block_editor( 'keys_only' );
if ( ! is_null( $block_editor_types ) ) {
$post_types = $block_editor_types;
}
/** Finally, register the taxonomy */
register_taxonomy(
'builder-template-category',
$post_types, //'btc-custom-post-type',
$args
);
} // end function
add_action( 'admin_menu', 'ddw_btc_inbetween_remove_submenu_for_block_editor_types', 599 );
/**
* Remove the added taxonomy submenu for integrations that explicitely support
* the Block Editor (Gutenberg).
* Note: This is a necessary in-between step, as we run our integrations at a
* later priority. The reasoning behind is, that we need proper label support
* for our template content types, that the WordPress core-added submenus
* don't add.
*
* @since 1.3.0
*
* @uses ddw_btc_get_post_types_for_block_editor()
*
* @global array $GLOBALS[ 'submenu' ]
*/
function ddw_btc_inbetween_remove_submenu_for_block_editor_types() {
$types = ddw_btc_get_post_types_for_block_editor();
if ( ! is_null( $types ) ) {
foreach ( $types as $type => $type_data ) {
$submenu_hook = $type_data[ 'submenu_hook' ];
unset( $GLOBALS[ 'submenu' ][ $submenu_hook ][15] );
} // end foreach
} // end if
} // end function
/**
* Setup predefined taxonomy terms for our taxonomy.
* Default terms: General, Frontpage, Pages, Landing Pages, Sections, Hooks
* and Testing. Plus, more terms depending on various conditions and contexts.
*
* @since 1.0.0
* @since 1.1.0 Added optional terms for products, as well as popups.
* @since 1.2.0 Added optional terms for blocks, fields, boxes, bars and hooks.
* @since 1.4.3 Added optional terms for flows and snippets; moved declaration
* into own function for reusability.
* @since 1.6.0 Added optional terms for forms.
* @since 1.7.0 Added optional terms for URLs and redirects.
*
* @uses ddw_btc_is_woocommerce_active()
* @uses ddw_btc_is_elementor_pro_active()
* @uses ddw_btc_is_elementor_version()
* @uses ddw_btc_is_block_editor_active()
*
* @return array Filterable array of predefined terms, plus arguments.
*/
function ddw_btc_get_predefined_terms() {
/** Set predefined terms, filterable */
$terms = array(
'terms_general' => array(
'name' => esc_attr_x( 'General', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'general', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'General templates', 'Taxonomy term description', 'builder-template-categories' ),
),
'terms_frontpage' => array(
'name' => esc_attr_x( 'Frontpage', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'frontpage', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for the Frontpage/ Homepage', 'Taxonomy term description', 'builder-template-categories' ),
),
'terms_pages' => array(
'name' => esc_attr_x( 'Pages', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'pages', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Pages', 'Taxonomy term description', 'builder-template-categories' ),
),
'terms_landing_pages' => array(
'name' => esc_attr_x( 'Landing Pages', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'landing-pages', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Landing Pages', 'Taxonomy term description', 'builder-template-categories' ),
),
'terms_sections' => array(
'name' => esc_attr_x( 'Sections', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'sections', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Sections/ Content Blocks', 'Taxonomy term description', 'builder-template-categories' ),
),
'terms_hooks' => array(
'name' => esc_attr_x( 'Hooks', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'hooks', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Action Hooks', 'Taxonomy term description', 'builder-template-categories' ),
),
'terms_testing' => array(
'name' => esc_attr_x( 'Testing', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'testing', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates just for testing purposes', 'Taxonomy term description', 'builder-template-categories' ),
),
); // end array
/** Optional: WooCommerce product type templates */
if ( ddw_btc_is_woocommerce_active() && has_filter( 'btc/filter/is_type/woo' ) ) {
$terms[ 'terms_woo' ] = array(
'name' => esc_attr_x( 'Products', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'products', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for WooCommerce products', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Popup type templates */
if ( has_filter( 'btc/filter/is_type/popup' )
|| ( ddw_btc_is_elementor_pro_active() && ddw_btc_is_elementor_version( 'pro', '2.4.0-beta1', '>=' ) )
) {
$terms[ 'terms_popup' ] = array(
'name' => esc_attr_x( 'Popups', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'popups', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Popups/ Modal Windows', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Block type templates */
if ( ddw_btc_is_block_editor_active() || has_filter( 'btc/filter/is_type/block' ) ) {
$terms[ 'terms_block' ] = array(
'name' => esc_attr_x( 'Blocks', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'blocks', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Blocks', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Field type templates */
if ( has_filter( 'btc/filter/is_type/field' ) ) {
$terms[ 'terms_field' ] = array(
'name' => esc_attr_x( 'Fields', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'fields', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Fields', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Box type templates */
if ( has_filter( 'btc/filter/is_type/box' ) ) {
$terms[ 'terms_box' ] = array(
'name' => esc_attr_x( 'Boxes', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'boxes', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Boxes', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Bar type templates */
if ( has_filter( 'btc/filter/is_type/bar' ) ) {
$terms[ 'terms_bar' ] = array(
'name' => esc_attr_x( 'Bars', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'bars', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Bars', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Flow type templates */
if ( has_filter( 'btc/filter/is_type/flow' ) ) {
$terms[ 'terms_flow' ] = array(
'name' => esc_attr_x( 'Flows', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'flows', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Flows', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Snippet type content */
if ( has_filter( 'btc/filter/is_type/snippet' ) ) {
$terms[ 'terms_snippet' ] = array(
'name' => esc_attr_x( 'Snippets', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'snippets', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Snippets', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Form type content */
if ( has_filter( 'btc/filter/is_type/form' ) ) {
$terms[ 'terms_forms' ] = array(
'name' => esc_attr_x( 'Forms', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'forms', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Forms', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: URL type content */
if ( has_filter( 'btc/filter/is_type/url' ) ) {
$terms[ 'terms_urls' ] = array(
'name' => esc_attr_x( 'URLs', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'urls', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for URLs', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Optional: Redirect type content */
if ( has_filter( 'btc/filter/is_type/redirect' ) ) {
$terms[ 'terms_redirects' ] = array(
'name' => esc_attr_x( 'Redirects', 'Taxonomy term title', 'builder-template-categories' ),
'slug' => sanitize_key( _x( 'redirects', 'Taxonomy term slug - only lowercase, a-z, 0-9, hyphens!', 'builder-template-categories' ) ),
'description' => _x( 'Templates for Redirects', 'Taxonomy term description', 'builder-template-categories' ),
);
} // end if
/** Return the terms array, filterable */
return apply_filters(
'btc/filter/taxonomy/predefined_terms',
$terms
);
} // end function
//add_action( 'init', 'ddw_btc_add_predefined_terms', 25 );
/**
* Add predefined taxonomy terms for our taxonomy.
* See ddw_btc_get_predefined_terms() for the (filterable) terms setup.
*
* @since 1.0.0
* @since 1.1.0 Added optional terms for products, as well as popups.
* @since 1.2.0 Added optional terms for blocks, fields, boxes, bars and hooks.
* @since 1.4.3 Added optional terms for flows and snippets.
*
* @see ddw_btc_get_predefined_terms()
*
* @uses ddw_btc_get_predefined_terms() To get all our predefined terms.
* @uses wp_insert_term() To actually insert the terms if they not yet exist.
*
* @param string $taxonomy Registered ID of a given taxonomy.
* @return void
*/
function ddw_btc_add_predefined_terms( $taxonomy = '' ) {
/** Set taxonomy */
$taxonomy = sanitize_key( $taxonomy ); //'builder-template-category';
$terms = ddw_btc_get_predefined_terms();
/** Insert all predefined terms */
if ( ! is_null( $terms ) ) {
foreach ( $terms as $term_key => $term ) {
if ( ! term_exists( $term[ 'slug' ], $taxonomy ) ) {
wp_insert_term(
$term[ 'name' ],
$taxonomy,
array(
'description' => $term[ 'description' ],
'slug' => $term[ 'slug' ],
)
);
unset( $term );
} // end if
} // end foreach
} // end if
} // end function