cptui_update_taxonomy

Add to or update our CPTUI option with new data.

Description

(bool|string) cptui_update_taxonomy( (array) $data = array() ); 

Returns (bool|string)

False on failure, string on success.

Parameters (1)

0. $data — Optional. (array) => array()
Array of taxonomy data to update. Optional.

Usage

  1. if ( !function_exists( 'cptui_update_taxonomy' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'custom-post-type-ui/inc/taxonomies.php'; 
  3.  
  4. // Array of taxonomy data to update. Optional. 
  5. $data = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = cptui_update_taxonomy($data); 
  9.  

Defined (1)

The function is defined in the following location(s).

/inc/taxonomies.php  
  1. function cptui_update_taxonomy( $data = array() ) { 
  2.  
  3. /** 
  4. * Fires before a taxonomy is updated to our saved options. 
  5. * @since 1.0.0 
  6. * @param array $data Array of taxonomy data we are updating. 
  7. */ 
  8. do_action( 'cptui_before_update_taxonomy', $data ); 
  9.  
  10. // They need to provide a name. 
  11. if ( empty( $data['cpt_custom_tax']['name'] ) ) { 
  12. return cptui_admin_notices( error, '', false, esc_html__( 'Please provide a taxonomy name', 'custom-post-type-ui' ) ); 
  13.  
  14. if ( empty( $data['cpt_post_types'] ) ) { 
  15. return cptui_admin_notices( error, '', false, esc_html__( 'Please provide a post type to attach to.', 'custom-post-type-ui' ) ); 
  16.  
  17. if ( ! empty( $data['tax_original'] ) && $data['tax_original'] != $data['cpt_custom_tax']['name'] ) { 
  18. if ( ! empty( $data['update_taxonomy'] ) ) { 
  19. add_filter( 'cptui_convert_taxonomy_terms', '__return_true' ); 
  20.  
  21. foreach ( $data as $key => $value ) { 
  22. if ( is_string( $value ) ) { 
  23. $data[ $key ] = sanitize_text_field( $value ); 
  24. } else { 
  25. array_map( 'sanitize_text_field', $data[ $key ] ); 
  26.  
  27. if ( false !== strpos( $data['cpt_custom_tax']['name'], '\'' ) || 
  28. false !== strpos( $data['cpt_custom_tax']['name'], '\"' ) || 
  29. false !== strpos( $data['cpt_custom_tax']['rewrite_slug'], '\'' ) || 
  30. false !== strpos( $data['cpt_custom_tax']['rewrite_slug'], '\"' ) ) { 
  31.  
  32. return error; 
  33.  
  34. $taxonomies = cptui_get_taxonomy_data(); 
  35.  
  36. /** 
  37. * Check if we already have a post type of that name. 
  38. * @since 1.3.0 
  39. * @param bool $value Assume we have no conflict by default. 
  40. * @param string $value Post type slug being saved. 
  41. * @param array $post_types Array of existing post types from CPTUI. 
  42. */ 
  43. $slug_exists = apply_filters( 'cptui_taxonomy_slug_exists', false, $data['cpt_custom_tax']['name'], $taxonomies ); 
  44. if ( true === $slug_exists ) { 
  45. return error; 
  46.  
  47. foreach ( $data['cpt_tax_labels'] as $key => $label ) { 
  48. if ( empty( $label ) ) { 
  49. unset( $data['cpt_tax_labels'][ $key ] ); 
  50. $label = str_replace( '"', '', htmlspecialchars_decode( $label ) ); 
  51. $label = htmlspecialchars( $label, ENT_QUOTES ); 
  52. $label = trim( $label ); 
  53. $data['cpt_tax_labels'][ $key ] = stripslashes_deep( $label ); 
  54.  
  55. $label = ucwords( str_replace( '_', ' ', $data['cpt_custom_tax']['name'] ) ); 
  56. if ( ! empty( $data['cpt_custom_tax']['label'] ) ) { 
  57. $label = str_replace( '"', '', htmlspecialchars_decode( $data['cpt_custom_tax']['label'] ) ); 
  58. $label = htmlspecialchars( stripslashes( $label ), ENT_QUOTES ); 
  59.  
  60. $name = trim( $data['cpt_custom_tax']['name'] ); 
  61.  
  62. $singular_label = ucwords( str_replace( '_', ' ', $data['cpt_custom_tax']['name'] ) ); 
  63. if ( ! empty( $data['cpt_custom_tax']['singular_label'] ) ) { 
  64. $singular_label = str_replace( '"', '', htmlspecialchars_decode( $data['cpt_custom_tax']['singular_label'] ) ); 
  65. $singular_label = htmlspecialchars( stripslashes( $singular_label ) ); 
  66. $description = stripslashes_deep( $data['cpt_custom_tax']['description'] ); 
  67. $query_var_slug = trim( $data['cpt_custom_tax']['query_var_slug'] ); 
  68. $rewrite_slug = trim( $data['cpt_custom_tax']['rewrite_slug'] ); 
  69. $rest_base = trim( $data['cpt_custom_tax']['rest_base'] ); 
  70. $show_quickpanel_bulk = ( ! empty( $data['cpt_custom_tax']['show_in_quick_edit'] ) ) ? disp_boolean( $data['cpt_custom_tax']['show_in_quick_edit'] ) : ''; 
  71.  
  72. $taxonomies[ $data['cpt_custom_tax']['name'] ] = array( 
  73. 'name' => $name,  
  74. 'label' => $label,  
  75. 'singular_label' => $singular_label,  
  76. 'description' => $description,  
  77. 'public' => disp_boolean( $data['cpt_custom_tax']['public'] ),  
  78. 'hierarchical' => disp_boolean( $data['cpt_custom_tax']['hierarchical'] ),  
  79. 'show_ui' => disp_boolean( $data['cpt_custom_tax']['show_ui'] ),  
  80. 'show_in_menu' => disp_boolean( $data['cpt_custom_tax']['show_in_menu'] ),  
  81. 'show_in_nav_menus' => disp_boolean( $data['cpt_custom_tax']['show_in_nav_menus'] ),  
  82. 'query_var' => disp_boolean( $data['cpt_custom_tax']['query_var'] ),  
  83. 'query_var_slug' => $query_var_slug,  
  84. 'rewrite' => disp_boolean( $data['cpt_custom_tax']['rewrite'] ),  
  85. 'rewrite_slug' => $rewrite_slug,  
  86. 'rewrite_withfront' => $data['cpt_custom_tax']['rewrite_withfront'],  
  87. 'rewrite_hierarchical' => $data['cpt_custom_tax']['rewrite_hierarchical'],  
  88. 'show_admin_column' => disp_boolean( $data['cpt_custom_tax']['show_admin_column'] ),  
  89. 'show_in_rest' => disp_boolean( $data['cpt_custom_tax']['show_in_rest'] ),  
  90. 'show_in_quick_edit' => $show_quickpanel_bulk,  
  91. 'rest_base' => $rest_base,  
  92. 'labels' => $data['cpt_tax_labels'],  
  93. ); 
  94.  
  95. $taxonomies[ $data['cpt_custom_tax']['name'] ]['object_types'] = $data['cpt_post_types']; 
  96.  
  97. /** 
  98. * Filters whether or not 3rd party options were saved successfully within taxonomy add/update. 
  99. * @since 1.3.0 
  100. * @param bool $value Whether or not someone else saved successfully. Default false. 
  101. * @param array $taxonomies Array of our updated taxonomies data. 
  102. * @param array $data Array of submitted taxonomy to update. 
  103. */ 
  104. if ( false === ( $success = apply_filters( 'cptui_taxonomy_update_save', false, $taxonomies, $data ) ) ) { 
  105. $success = update_option( 'cptui_taxonomies', $taxonomies ); 
  106.  
  107. /** 
  108. * Fires after a taxonomy is updated to our saved options. 
  109. * @since 1.0.0 
  110. * @param array $data Array of taxonomy data that was updated. 
  111. */ 
  112. do_action( 'cptui_after_update_taxonomy', $data ); 
  113.  
  114. // Used to help flush rewrite rules on init. 
  115. set_transient( 'cptui_flush_rewrite_rules', 'true', 5 * 60 ); 
  116.  
  117. if ( isset( $success ) ) { 
  118. if ( 'new' == $data['cpt_tax_status'] ) { 
  119. return 'add_success'; 
  120.  
  121. return 'update_success';