PluginProbe ʕ •ᴥ•ʔ
Custom Post Type UI / 1.5.7
Custom Post Type UI v1.5.7
1.19.2 1.19.1 1.19.0 trunk 0.7.0.0 0.7.1.0 0.7.2.0 0.8.0.0 0.8.1 0.8.2 0.8.3 0.8.4 0.8.5 0.9.0 0.9.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.2 1.11.0 1.11.1 1.11.2 1.12.0 1.12.1 1.13.0 1.13.1 1.13.2 1.13.3 1.13.4 1.13.5 1.13.6 1.13.7 1.14.0 1.15.0 1.15.1 1.16.0 1.17.0 1.17.1 1.17.2 1.17.3 1.18.0 1.18.1 1.18.2 1.18.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.8.0 1.8.1 1.8.2 1.9.0 1.9.1 1.9.2
custom-post-type-ui / custom-post-type-ui.php
custom-post-type-ui Last commit date
classes 8 years ago css 8 years ago images 8 years ago inc 8 years ago js 8 years ago changelog.txt 8 years ago custom-post-type-ui.php 8 years ago readme.txt 8 years ago
custom-post-type-ui.php
860 lines
1 <?php
2 /**
3 * Custom Post Type UI.
4 *
5 * For all your post type and taxonomy needs.
6 *
7 * @package CPTUI
8 * @subpackage Loader
9 * @author WebDevStudios
10 * @since 0.1.0.0
11 */
12
13 /*
14 Plugin Name: Custom Post Type UI
15 Plugin URI: https://github.com/WebDevStudios/custom-post-type-ui/
16 Description: Admin panel for creating custom post types and custom taxonomies in WordPress
17 Author: WebDevStudios
18 Version: 1.5.7
19 Author URI: https://webdevstudios.com/
20 Text Domain: custom-post-type-ui
21 Domain Path: /languages
22 License: GPLv2
23 */
24
25 // Exit if accessed directly.
26 if ( ! defined( 'ABSPATH' ) ) {
27 exit;
28 }
29
30 define( 'CPT_VERSION', '1.5.7' ); // Left for legacy purposes.
31 define( 'CPTUI_VERSION', '1.5.7' );
32 define( 'CPTUI_WP_VERSION', get_bloginfo( 'version' ) );
33
34 /**
35 * Load our Admin UI class that powers our form inputs.
36 *
37 * @since 1.0.0
38 *
39 * @internal
40 */
41 function cptui_load_ui_class() {
42 require_once( plugin_dir_path( __FILE__ ) . 'classes/class.cptui_admin_ui.php' );
43 require_once( plugin_dir_path( __FILE__ ) . 'classes/class.cptui_debug_info.php' );
44 }
45 add_action( 'init', 'cptui_load_ui_class' );
46
47 /**
48 * Set a transient used for redirection upon activation.
49 *
50 * @since 1.4.0
51 */
52 function cptui_activation_redirect() {
53 // Bail if activating from network, or bulk.
54 if ( is_network_admin() ) {
55 return;
56 }
57
58 // Add the transient to redirect.
59 set_transient( 'cptui_activation_redirect', true, 30 );
60 }
61 add_action( 'activate_' . plugin_basename( __FILE__ ), 'cptui_activation_redirect' );
62
63 /**
64 * Redirect user to CPTUI about page upon plugin activation.
65 *
66 * @since 1.4.0
67 */
68 function cptui_make_activation_redirect() {
69
70 if ( ! get_transient( 'cptui_activation_redirect' ) ) {
71 return;
72 }
73
74 delete_transient( 'cptui_activation_redirect' );
75
76 // Bail if activating from network, or bulk.
77 if ( is_network_admin() ) {
78 return;
79 }
80
81 if ( ! cptui_is_new_install() ) {
82 return;
83 }
84
85 // Redirect to CPTUI about page.
86 wp_safe_redirect(
87 add_query_arg(
88 array( 'page' => 'cptui_main_menu' ),
89 cptui_admin_url( 'admin.php?page=cptui_main_menu' )
90 )
91 );
92 }
93 add_action( 'admin_init', 'cptui_make_activation_redirect', 1 );
94
95 /**
96 * Flush our rewrite rules on deactivation.
97 *
98 * @since 0.8.0
99 *
100 * @internal
101 */
102 function cptui_deactivation() {
103 flush_rewrite_rules();
104 }
105 register_deactivation_hook( __FILE__, 'cptui_deactivation' );
106
107 /**
108 * Register our text domain.
109 *
110 * @since 0.8.0
111 *
112 * @internal
113 */
114 function cptui_load_textdomain() {
115 load_plugin_textdomain( 'custom-post-type-ui' );
116 }
117 add_action( 'plugins_loaded', 'cptui_load_textdomain' );
118
119 /**
120 * Load our main menu.
121 *
122 * Submenu items added in version 1.1.0
123 *
124 * @since 0.1.0
125 *
126 * @internal
127 */
128 function cptui_plugin_menu() {
129
130 /**
131 * Filters the required capability to manage CPTUI settings.
132 *
133 * @since 1.3.0
134 *
135 * @param string $value Capability required.
136 */
137 $capability = apply_filters( 'cptui_required_capabilities', 'manage_options' );
138 $parent_slug = 'cptui_main_menu';
139
140 add_menu_page( __( 'Custom Post Types', 'custom-post-type-ui' ), __( 'CPT UI', 'custom-post-type-ui' ), $capability, $parent_slug, 'cptui_settings', cptui_menu_icon() );
141 add_submenu_page( $parent_slug, __( 'Add/Edit Post Types', 'custom-post-type-ui' ), __( 'Add/Edit Post Types', 'custom-post-type-ui' ), $capability, 'cptui_manage_post_types', 'cptui_manage_post_types' );
142 add_submenu_page( $parent_slug, __( 'Add/Edit Taxonomies', 'custom-post-type-ui' ), __( 'Add/Edit Taxonomies', 'custom-post-type-ui' ), $capability, 'cptui_manage_taxonomies', 'cptui_manage_taxonomies' );
143 add_submenu_page( $parent_slug, __( 'Registered Types and Taxes', 'custom-post-type-ui' ), __( 'Registered Types/Taxes', 'custom-post-type-ui' ), $capability, 'cptui_listings', 'cptui_listings' );
144 add_submenu_page( $parent_slug, __( 'Custom Post Type UI Tools', 'custom-post-type-ui' ), __( 'Tools', 'custom-post-type-ui' ), $capability, 'cptui_tools', 'cptui_tools' );
145 add_submenu_page( $parent_slug, __( 'Help/Support', 'custom-post-type-ui' ), __( 'Help/Support', 'custom-post-type-ui' ), $capability, 'cptui_support', 'cptui_support' );
146
147 /**
148 * Fires after the default submenu pages.
149 *
150 * @since 1.3.0
151 *
152 * @param string $value Parent slug for Custom Post Type UI menu.
153 * @param string $capability Capability required to manage CPTUI settings.
154 */
155 do_action( 'cptui_extra_menu_items', $parent_slug, $capability );
156
157 // Remove the default one so we can add our customized version.
158 remove_submenu_page( $parent_slug, 'cptui_main_menu' );
159 add_submenu_page( $parent_slug, __( 'About CPT UI', 'custom-post-type-ui' ), __( 'About CPT UI', 'custom-post-type-ui' ), $capability, 'cptui_main_menu', 'cptui_settings' );
160 }
161 add_action( 'admin_menu', 'cptui_plugin_menu' );
162
163 /**
164 * Fire our CPTUI Loaded hook.
165 *
166 * @since 1.3.0
167 *
168 * @internal Use `cptui_loaded` hook.
169 */
170 function cptui_loaded() {
171
172 /**
173 * Fires upon plugins_loaded WordPress hook.
174 *
175 * CPTUI loads its required files on this hook.
176 *
177 * @since 1.3.0
178 */
179 do_action( 'cptui_loaded' );
180 }
181 add_action( 'plugins_loaded', 'cptui_loaded' );
182
183 /**
184 * Load our submenus.
185 *
186 * @since 1.0.0
187 *
188 * @internal
189 */
190 function cptui_create_submenus() {
191 require_once( plugin_dir_path( __FILE__ ) . 'inc/about.php' );
192 require_once( plugin_dir_path( __FILE__ ) . 'inc/utility.php' );
193 require_once( plugin_dir_path( __FILE__ ) . 'inc/post-types.php' );
194 require_once( plugin_dir_path( __FILE__ ) . 'inc/taxonomies.php' );
195 require_once( plugin_dir_path( __FILE__ ) . 'inc/listings.php' );
196 require_once( plugin_dir_path( __FILE__ ) . 'inc/tools.php' );
197 require_once( plugin_dir_path( __FILE__ ) . 'inc/support.php' );
198 }
199 add_action( 'cptui_loaded', 'cptui_create_submenus' );
200
201 /**
202 * Fire our CPTUI init hook.
203 *
204 * @since 1.3.0
205 *
206 * @internal Use `cptui_init` hook.
207 */
208 function cptui_init() {
209
210 /**
211 * Fires upon init WordPress hook.
212 *
213 * @since 1.3.0
214 */
215 do_action( 'cptui_init' );
216 }
217 add_action( 'init', 'cptui_init' );
218
219 /**
220 * Enqueue CPTUI admin styles.
221 *
222 * @since 1.0.0
223 *
224 * @internal
225 */
226 function cptui_add_styles() {
227 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
228 return;
229 }
230
231 $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
232 wp_register_script( 'cptui', plugins_url( "js/cptui{$min}.js", __FILE__ ), array( 'jquery', 'postbox' ), CPTUI_VERSION, true );
233 wp_enqueue_style( 'cptui-css', plugins_url( "css/cptui{$min}.css", __FILE__ ), array(), CPTUI_VERSION );
234 }
235 add_action( 'admin_enqueue_scripts', 'cptui_add_styles' );
236
237 /**
238 * Register our users' custom post types.
239 *
240 * @since 0.5.0
241 *
242 * @internal
243 */
244 function cptui_create_custom_post_types() {
245 $cpts = get_option( 'cptui_post_types' );
246
247 if ( empty( $cpts ) ) {
248 return;
249 }
250
251 /**
252 * Fires before the start of the post type registrations.
253 *
254 * @since 1.3.0
255 *
256 * @param array $cpts Array of post types to register.
257 */
258 do_action( 'cptui_pre_register_post_types', $cpts );
259
260 if ( is_array( $cpts ) ) {
261 foreach ( $cpts as $post_type ) {
262 cptui_register_single_post_type( $post_type );
263 }
264 }
265
266 /**
267 * Fires after the completion of the post type registrations.
268 *
269 * @since 1.3.0
270 *
271 * @param array $cpts Array of post types registered.
272 */
273 do_action( 'cptui_post_register_post_types', $cpts );
274 }
275 add_action( 'init', 'cptui_create_custom_post_types', 10 ); // Leave on standard init for legacy purposes.
276
277 /**
278 * Helper function to register the actual post_type.
279 *
280 * @since 1.0.0
281 *
282 * @internal
283 *
284 * @param array $post_type Post type array to register. Optional.
285 * @return null Result of register_post_type.
286 */
287 function cptui_register_single_post_type( $post_type = array() ) {
288
289 /**
290 * Filters the map_meta_cap value.
291 *
292 * @since 1.0.0
293 *
294 * @param bool $value True.
295 * @param string $name Post type name being registered.
296 * @param array $post_type All parameters for post type registration.
297 */
298 $post_type['map_meta_cap'] = apply_filters( 'cptui_map_meta_cap', true, $post_type['name'], $post_type );
299
300 /**
301 * Filters custom supports parameters for 3rd party plugins.
302 *
303 * @since 1.0.0
304 *
305 * @param array $value Empty array to add supports keys to.
306 * @param string $name Post type slug being registered.
307 * @param array $post_type Array of post type arguments to be registered.
308 */
309 $user_supports_params = apply_filters( 'cptui_user_supports_params', array(), $post_type['name'], $post_type );
310
311 if ( is_array( $user_supports_params ) && ! empty( $user_supports_params ) ) {
312 if ( is_array( $post_type['supports'] ) ) {
313 $post_type['supports'] = array_merge( $post_type['supports'], $user_supports_params );
314 } else {
315 $post_type['supports'] = array( $user_supports_params );
316 }
317 }
318
319 $yarpp = false; // Prevent notices.
320 if ( ! empty( $post_type['custom_supports'] ) ) {
321 $custom = explode( ',', $post_type['custom_supports'] );
322 foreach ( $custom as $part ) {
323 // We'll handle YARPP separately.
324 if ( in_array( $part, array( 'YARPP', 'yarpp' ) ) ) {
325 $yarpp = true;
326 continue;
327 }
328 $post_type['supports'][] = trim( $part );
329 }
330 }
331
332 if ( in_array( 'none', $post_type['supports'] ) ) {
333 $post_type['supports'] = false;
334 }
335
336 $labels = array(
337 'name' => $post_type['label'],
338 'singular_name' => $post_type['singular_label'],
339 );
340
341 $preserved = cptui_get_preserved_keys( 'post_types' );
342 foreach ( $post_type['labels'] as $key => $label ) {
343
344 if ( ! empty( $label ) ) {
345 if ( 'parent' === $key ) {
346 $labels['parent_item_colon'] = $label;
347 } else {
348 $labels[ $key ] = $label;
349 }
350 } elseif ( empty( $label ) && in_array( $key, $preserved ) ) {
351 $labels[ $key ] = cptui_get_preserved_label( 'post_types', $key, $post_type['label'], $post_type['singular_label'] );
352 }
353 }
354
355 $has_archive = get_disp_boolean( $post_type['has_archive'] );
356 if ( ! empty( $post_type['has_archive_string'] ) ) {
357 $has_archive = $post_type['has_archive_string'];
358 }
359
360 $show_in_menu = get_disp_boolean( $post_type['show_in_menu'] );
361 if ( ! empty( $post_type['show_in_menu_string'] ) ) {
362 $show_in_menu = $post_type['show_in_menu_string'];
363 }
364
365 $rewrite = get_disp_boolean( $post_type['rewrite'] );
366 if ( false !== $rewrite ) {
367 // Core converts to an empty array anyway, so safe to leave this instead of passing in boolean true.
368 $rewrite = array();
369 $rewrite['slug'] = ( ! empty( $post_type['rewrite_slug'] ) ) ? $post_type['rewrite_slug'] : $post_type['name'];
370
371 $rewrite['with_front'] = true; // Default value.
372 if ( isset( $post_type['rewrite_withfront'] ) ) {
373 $rewrite['with_front'] = ( 'false' === disp_boolean( $post_type['rewrite_withfront'] ) ) ? false : true;
374 }
375 }
376
377 $menu_icon = ( ! empty( $post_type['menu_icon'] ) ) ? $post_type['menu_icon'] : null;
378
379 if ( in_array( $post_type['query_var'], array( 'true', 'false', '0', '1' ) ) ) {
380 $post_type['query_var'] = get_disp_boolean( $post_type['query_var'] );
381 }
382 if ( ! empty( $post_type['query_var_slug'] ) ) {
383 $post_type['query_var'] = $post_type['query_var_slug'];
384 }
385
386 $menu_position = null;
387 if ( ! empty( $post_type['menu_position'] ) ) {
388 $menu_position = (int) $post_type['menu_position'];
389 }
390
391 $public = get_disp_boolean( $post_type['public'] );
392 if ( ! empty( $post_type['exclude_from_search'] ) ) {
393 $exclude_from_search = get_disp_boolean( $post_type['exclude_from_search'] );
394 } else {
395 $exclude_from_search = ( false === $public ) ? true : false;
396 }
397
398 $queryable = ( ! empty( $post_type['publicly_queryable'] ) && isset( $post_type['publicly_queryable'] ) ) ? get_disp_boolean( $post_type['publicly_queryable'] ) : $public;
399
400 if ( empty( $post_type['show_in_nav_menus'] ) ) {
401 // Defaults to value of public.
402 $post_type['show_in_nav_menus'] = $public;
403 }
404
405 if ( empty( $post_type['show_in_rest'] ) ) {
406 $post_type['show_in_rest'] = false;
407 }
408
409 $rest_base = null;
410 if ( ! empty( $post_type['rest_base'] ) ) {
411 $rest_base = $post_type['rest_base'];
412 }
413
414 $args = array(
415 'labels' => $labels,
416 'description' => $post_type['description'],
417 'public' => get_disp_boolean( $post_type['public'] ),
418 'publicly_queryable' => $queryable,
419 'show_ui' => get_disp_boolean( $post_type['show_ui'] ),
420 'show_in_nav_menus' => get_disp_boolean( $post_type['show_in_nav_menus'] ),
421 'has_archive' => $has_archive,
422 'show_in_menu' => $show_in_menu,
423 'show_in_rest' => get_disp_boolean( $post_type['show_in_rest'] ),
424 'rest_base' => $rest_base,
425 'exclude_from_search' => $exclude_from_search,
426 'capability_type' => $post_type['capability_type'],
427 'map_meta_cap' => $post_type['map_meta_cap'],
428 'hierarchical' => get_disp_boolean( $post_type['hierarchical'] ),
429 'rewrite' => $rewrite,
430 'menu_position' => $menu_position,
431 'menu_icon' => $menu_icon,
432 'query_var' => $post_type['query_var'],
433 'supports' => $post_type['supports'],
434 'taxonomies' => $post_type['taxonomies'],
435 );
436
437 if ( true === $yarpp ) {
438 $args['yarpp_support'] = $yarpp;
439 }
440
441 /**
442 * Filters the arguments used for a post type right before registering.
443 *
444 * @since 1.0.0
445 * @since 1.3.0 Added original passed in values array
446 *
447 * @param array $args Array of arguments to use for registering post type.
448 * @param string $value Post type slug to be registered.
449 * @param array $post_type Original passed in values for post type.
450 */
451 $args = apply_filters( 'cptui_pre_register_post_type', $args, $post_type['name'], $post_type );
452
453 return register_post_type( $post_type['name'], $args );
454 }
455
456 /**
457 * Register our users' custom taxonomies.
458 *
459 * @since 0.5.0
460 *
461 * @internal
462 */
463 function cptui_create_custom_taxonomies() {
464 $taxes = get_option( 'cptui_taxonomies' );
465
466 if ( empty( $taxes ) ) {
467 return;
468 }
469
470 /**
471 * Fires before the start of the taxonomy registrations.
472 *
473 * @since 1.3.0
474 *
475 * @param array $taxes Array of taxonomies to register.
476 */
477 do_action( 'cptui_pre_register_taxonomies', $taxes );
478
479 if ( is_array( $taxes ) ) {
480 foreach ( $taxes as $tax ) {
481 cptui_register_single_taxonomy( $tax );
482 }
483 }
484
485 /**
486 * Fires after the completion of the taxonomy registrations.
487 *
488 * @since 1.3.0
489 *
490 * @param array $taxes Array of taxonomies registered.
491 */
492 do_action( 'cptui_post_register_taxonomies', $taxes );
493 }
494 add_action( 'init', 'cptui_create_custom_taxonomies', 9 ); // Leave on standard init for legacy purposes.
495
496 /**
497 * Helper function to register the actual taxonomy.
498 *
499 * @since 1.0.0
500 *
501 * @internal
502 *
503 * @param array $taxonomy Taxonomy array to register. Optional.
504 * @return null Result of register_taxonomy.
505 */
506 function cptui_register_single_taxonomy( $taxonomy = array() ) {
507
508 $labels = array(
509 'name' => $taxonomy['label'],
510 'singular_name' => $taxonomy['singular_label'],
511 );
512
513 $description = '';
514 if ( ! empty( $taxonomy['description'] ) ) {
515 $description = $taxonomy['description'];
516 }
517
518 $preserved = cptui_get_preserved_keys( 'taxonomies' );
519 foreach ( $taxonomy['labels'] as $key => $label ) {
520
521 if ( ! empty( $label ) ) {
522 $labels[ $key ] = $label;
523 } elseif ( empty( $label ) && in_array( $key, $preserved ) ) {
524 $labels[ $key ] = cptui_get_preserved_label( 'taxonomies', $key, $taxonomy['label'], $taxonomy['singular_label'] );
525 }
526 }
527
528 $rewrite = get_disp_boolean( $taxonomy['rewrite'] );
529 if ( false !== get_disp_boolean( $taxonomy['rewrite'] ) ) {
530 $rewrite = array();
531 $rewrite['slug'] = ( ! empty( $taxonomy['rewrite_slug'] ) ) ? $taxonomy['rewrite_slug'] : $taxonomy['name'];
532 $rewrite['with_front'] = true;
533 if ( isset( $taxonomy['rewrite_withfront'] ) ) {
534 $rewrite['with_front'] = ( 'false' === disp_boolean( $taxonomy['rewrite_withfront'] ) ) ? false : true;
535 }
536 $rewrite['hierarchical'] = false;
537 if ( isset( $taxonomy['rewrite_hierarchical'] ) ) {
538 $rewrite['hierarchical'] = ( 'true' === disp_boolean( $taxonomy['rewrite_hierarchical'] ) ) ? true : false;
539 }
540 }
541
542 if ( in_array( $taxonomy['query_var'], array( 'true', 'false', '0', '1' ) ) ) {
543 $taxonomy['query_var'] = get_disp_boolean( $taxonomy['query_var'] );
544 }
545 if ( true === $taxonomy['query_var'] && ! empty( $taxonomy['query_var_slug'] ) ) {
546 $taxonomy['query_var'] = $taxonomy['query_var_slug'];
547 }
548
549 $public = ( ! empty( $taxonomy['public'] ) && false === get_disp_boolean( $taxonomy['public'] ) ) ? false : true;
550
551 $show_admin_column = ( ! empty( $taxonomy['show_admin_column'] ) && false !== get_disp_boolean( $taxonomy['show_admin_column'] ) ) ? true : false;
552
553 $show_in_menu = ( ! empty( $taxonomy['show_in_menu'] ) && false !== get_disp_boolean( $taxonomy['show_in_menu'] ) ) ? true : false;
554
555 if ( empty( $taxonomy['show_in_menu'] ) ) {
556 $show_in_menu = get_disp_boolean( $taxonomy['show_ui'] );
557 }
558
559 $show_in_nav_menus = ( ! empty( $taxonomy['show_in_nav_menus'] ) && false !== get_disp_boolean( $taxonomy['show_in_nav_menus'] ) ) ? true : false;
560 if ( empty( $taxonomy['show_in_nav_menus'] ) ) {
561 $show_in_nav_menus = $public;
562 }
563
564 $show_in_rest = ( ! empty( $taxonomy['show_in_rest'] ) && false !== get_disp_boolean( $taxonomy['show_in_rest'] ) ) ? true : false;
565
566 $show_in_quick_edit = ( ! empty( $taxonomy['show_in_quick_edit'] ) && false !== get_disp_boolean( $taxonomy['show_in_quick_edit'] ) ) ? true : false;
567
568 $rest_base = null;
569 if ( ! empty( $taxonomy['rest_base'] ) ) {
570 $rest_base = $taxonomy['rest_base'];
571 }
572
573 $args = array(
574 'labels' => $labels,
575 'label' => $taxonomy['label'],
576 'description' => $description,
577 'public' => $public,
578 'hierarchical' => get_disp_boolean( $taxonomy['hierarchical'] ),
579 'show_ui' => get_disp_boolean( $taxonomy['show_ui'] ),
580 'show_in_menu' => $show_in_menu,
581 'show_in_nav_menus' => $show_in_nav_menus,
582 'query_var' => $taxonomy['query_var'],
583 'rewrite' => $rewrite,
584 'show_admin_column' => $show_admin_column,
585 'show_in_rest' => $show_in_rest,
586 'rest_base' => $rest_base,
587 'show_in_quick_edit' => $show_in_quick_edit,
588 );
589
590 $object_type = ( ! empty( $taxonomy['object_types'] ) ) ? $taxonomy['object_types'] : '';
591
592 /**
593 * Filters the arguments used for a taxonomy right before registering.
594 *
595 * @since 1.0.0
596 * @since 1.3.0 Added original passed in values array
597 *
598 * @param array $args Array of arguments to use for registering taxonomy.
599 * @param string $value Taxonomy slug to be registered.
600 * @param array $taxonomy Original passed in values for taxonomy.
601 */
602 $args = apply_filters( 'cptui_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy );
603
604 return register_taxonomy( $taxonomy['name'], $object_type, $args );
605 }
606
607 /**
608 * Construct and output tab navigation.
609 *
610 * @since 1.0.0
611 *
612 * @param string $page Whether it's the CPT or Taxonomy page. Optional. Default "post_types".
613 */
614 function cptui_settings_tab_menu( $page = 'post_types' ) {
615
616 /**
617 * Filters the tabs to render on a given page.
618 *
619 * @since 1.3.0
620 *
621 * @param array $value Array of tabs to render.
622 * @param string $page Current page being displayed.
623 */
624 $tabs = (array) apply_filters( 'cptui_get_tabs', array(), $page );
625
626 if ( ! empty( $tabs['page_title'] ) ) {
627 printf(
628 '<h1>%s</h1><h2 class="nav-tab-wrapper">',
629 $tabs['page_title']
630 );
631 }
632
633 foreach ( $tabs['tabs'] as $tab ) {
634 printf(
635 '<a class="%s" href="%s" aria-selected="%s">%s</a>',
636 implode( ' ', $tab['classes'] ),
637 $tab['url'],
638 $tab['aria-selected'],
639 $tab['text']
640 );
641 }
642
643 echo '</h2>';
644 }
645
646 /**
647 * Convert our old settings to the new options keys.
648 *
649 * These are left with standard get_option/update_option function calls for legacy and pending update purposes.
650 *
651 * @since 1.0.0
652 *
653 * @internal
654 *
655 * @return bool Whether or not options were successfully updated.
656 */
657 function cptui_convert_settings() {
658
659 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
660 return;
661 }
662
663 $retval = '';
664
665 if ( false === get_option( 'cptui_post_types' ) && ( $post_types = get_option( 'cpt_custom_post_types' ) ) ) {
666
667 $new_post_types = array();
668 foreach ( $post_types as $type ) {
669 $new_post_types[ $type['name'] ] = $type; // This one assigns the # indexes. Named arrays are our friend.
670 $new_post_types[ $type['name'] ]['supports'] = ( ! empty( $type[0] ) ) ? $type[0] : array(); // Especially for multidimensional arrays.
671 $new_post_types[ $type['name'] ]['taxonomies'] = ( ! empty( $type[1] ) ) ? $type[1] : array();
672 $new_post_types[ $type['name'] ]['labels'] = ( ! empty( $type[2] ) ) ? $type[2] : array();
673 unset(
674 $new_post_types[ $type['name'] ][0],
675 $new_post_types[ $type['name'] ][1],
676 $new_post_types[ $type['name'] ][2]
677 ); // Remove our previous indexed versions.
678 }
679
680 $retval = update_option( 'cptui_post_types', $new_post_types );
681 }
682
683 if ( false === get_option( 'cptui_taxonomies' ) && ( $taxonomies = get_option( 'cpt_custom_tax_types' ) ) ) {
684
685 $new_taxonomies = array();
686 foreach ( $taxonomies as $tax ) {
687 $new_taxonomies[ $tax['name'] ] = $tax; // Yep, still our friend.
688 $new_taxonomies[ $tax['name'] ]['labels'] = $tax[0]; // Taxonomies are the only thing with
689 $new_taxonomies[ $tax['name'] ]['object_types'] = $tax[1]; // "tax" in the name that I like.
690 unset(
691 $new_taxonomies[ $tax['name'] ][0],
692 $new_taxonomies[ $tax['name'] ][1]
693 );
694 }
695
696 $retval = update_option( 'cptui_taxonomies', $new_taxonomies );
697 }
698
699 if ( ! empty( $retval ) ) {
700 flush_rewrite_rules();
701 }
702
703 return $retval;
704 }
705 add_action( 'admin_init', 'cptui_convert_settings' );
706
707 /**
708 * Return a notice based on conditions.
709 *
710 * @since 1.0.0
711 *
712 * @param string $action The type of action that occurred. Optional. Default empty string.
713 * @param string $object_type Whether it's from a post type or taxonomy. Optional. Default empty string.
714 * @param bool $success Whether the action succeeded or not. Optional. Default true.
715 * @param string $custom Custom message if necessary. Optional. Default empty string.
716 * @return bool|string false on no message, else HTML div with our notice message.
717 */
718 function cptui_admin_notices( $action = '', $object_type = '', $success = true, $custom = '' ) {
719
720 $class = array();
721 $class[] = ( $success ) ? 'updated' : 'error';
722 $class[] = 'notice is-dismissible';
723 $object_type = esc_attr( $object_type );
724
725 $messagewrapstart = '<div id="message" class="' . implode( ' ', $class ) . '"><p>';
726 $message = '';
727
728 $messagewrapend = '</p></div>';
729
730 if ( 'add' == $action ) {
731 if ( $success ) {
732 $message .= sprintf( __( '%s has been successfully added', 'custom-post-type-ui' ), $object_type );
733 } else {
734 $message .= sprintf( __( '%s has failed to be added', 'custom-post-type-ui' ), $object_type );
735 }
736 } elseif ( 'update' == $action ) {
737 if ( $success ) {
738 $message .= sprintf( __( '%s has been successfully updated', 'custom-post-type-ui' ), $object_type );
739 } else {
740 $message .= sprintf( __( '%s has failed to be updated', 'custom-post-type-ui' ), $object_type );
741 }
742 } elseif ( 'delete' == $action ) {
743 if ( $success ) {
744 $message .= sprintf( __( '%s has been successfully deleted', 'custom-post-type-ui' ), $object_type );
745 } else {
746 $message .= sprintf( __( '%s has failed to be deleted', 'custom-post-type-ui' ), $object_type );
747 }
748 } elseif ( 'import' == $action ) {
749 if ( $success ) {
750 $message .= sprintf( __( '%s has been successfully imported', 'custom-post-type-ui' ), $object_type );
751 } else {
752 $message .= sprintf( __( '%s has failed to be imported', 'custom-post-type-ui' ), $object_type );
753 }
754 } elseif ( 'error' == $action ) {
755 if ( ! empty( $custom ) ) {
756 $message = $custom;
757 }
758 }
759
760 if ( $message ) {
761
762 /**
763 * Filters the custom admin notice for CPTUI.
764 *
765 * @since 1.0.0
766 *
767 * @param string $value Complete HTML output for notice.
768 * @param string $action Action whose message is being generated.
769 * @param string $message The message to be displayed.
770 * @param string $messagewrapstart Beginning wrap HTML.
771 * @param string $messagewrapend Ending wrap HTML.
772 */
773 return apply_filters( 'cptui_admin_notice', $messagewrapstart . $message . $messagewrapend, $action, $message, $messagewrapstart, $messagewrapend );
774 }
775
776 return false;
777 }
778
779 /**
780 * Return array of keys needing preserved.
781 *
782 * @since 1.0.5
783 *
784 * @param string $type Type to return. Either 'post_types' or 'taxonomies'. Optional. Default empty string.
785 * @return array Array of keys needing preservered for the requested type.
786 */
787 function cptui_get_preserved_keys( $type = '' ) {
788
789 $preserved_labels = array(
790 'post_types' => array(
791 'add_new_item',
792 'edit_item',
793 'new_item',
794 'view_item',
795 'all_items',
796 'search_items',
797 'not_found',
798 'not_found_in_trash',
799 ),
800 'taxonomies' => array(
801 'search_items',
802 'popular_items',
803 'all_items',
804 'parent_item',
805 'parent_item_colon',
806 'edit_item',
807 'update_item',
808 'add_new_item',
809 'new_item_name',
810 'separate_items_with_commas',
811 'add_or_remove_items',
812 'choose_from_most_used',
813 ),
814 );
815 return ( ! empty( $type ) ) ? $preserved_labels[ $type ] : array();
816 }
817
818 /**
819 * Return label for the requested type and label key.
820 *
821 * @since 1.0.5
822 *
823 * @param string $type Type to return. Either 'post_types' or 'taxonomies'. Optional. Default empty string.
824 * @param string $key Requested label key. Optional. Default empty string.
825 * @param string $plural Plural verbiage for the requested label and type. Optional. Default empty string.
826 * @param string $singular Singular verbiage for the requested label and type. Optional. Default empty string.
827 * @return string Internationalized default label.
828 */
829 function cptui_get_preserved_label( $type = '', $key = '', $plural = '', $singular = '' ) {
830
831 $preserved_labels = array(
832 'post_types' => array(
833 'add_new_item' => sprintf( __( 'Add new %s', 'custom-post-type-ui' ), $singular ),
834 'edit_item' => sprintf( __( 'Edit %s', 'custom-post-type-ui' ), $singular ),
835 'new_item' => sprintf( __( 'New %s', 'custom-post-type-ui' ), $singular ),
836 'view_item' => sprintf( __( 'View %s', 'custom-post-type-ui' ), $singular ),
837 'all_items' => sprintf( __( 'All %s', 'custom-post-type-ui' ), $plural ),
838 'search_items' => sprintf( __( 'Search %s', 'custom-post-type-ui' ), $plural ),
839 'not_found' => sprintf( __( 'No %s found.', 'custom-post-type-ui' ), $plural ),
840 'not_found_in_trash' => sprintf( __( 'No %s found in trash.', 'custom-post-type-ui' ), $plural ),
841 ),
842 'taxonomies' => array(
843 'search_items' => sprintf( __( 'Search %s', 'custom-post-type-ui' ), $plural ),
844 'popular_items' => sprintf( __( 'Popular %s', 'custom-post-type-ui' ), $plural ),
845 'all_items' => sprintf( __( 'All %s', 'custom-post-type-ui' ), $plural ),
846 'parent_item' => sprintf( __( 'Parent %s', 'custom-post-type-ui' ), $singular ),
847 'parent_item_colon' => sprintf( __( 'Parent %s:', 'custom-post-type-ui' ), $singular ),
848 'edit_item' => sprintf( __( 'Edit %s', 'custom-post-type-ui' ), $singular ),
849 'update_item' => sprintf( __( 'Update %s', 'custom-post-type-ui' ), $singular ),
850 'add_new_item' => sprintf( __( 'Add new %s', 'custom-post-type-ui' ), $singular ),
851 'new_item_name' => sprintf( __( 'New %s name', 'custom-post-type-ui' ), $singular ),
852 'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'custom-post-type-ui' ), $plural ),
853 'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'custom-post-type-ui' ), $plural ),
854 'choose_from_most_used' => sprintf( __( 'Choose from the most used %s', 'custom-post-type-ui' ), $plural ),
855 ),
856 );
857
858 return $preserved_labels[ $type ][ $key ];
859 }
860