cptui_update_post_type

Add to or update our CPTUI option with new data.

Description

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

Returns (bool|string)

False on failure, string on success.

Parameters (1)

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

Usage

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

Defined (1)

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

/inc/post-types.php  
  1. function cptui_update_post_type( $data = array() ) { 
  2.  
  3. /** 
  4. * Fires before a post_type is updated to our saved options. 
  5. * @since 1.0.0 
  6. * @param array $data Array of post_type data we are updating. 
  7. */ 
  8. do_action( 'cptui_before_update_post_type', $data ); 
  9.  
  10. // They need to provide a name. 
  11. if ( empty( $data['cpt_custom_post_type']['name'] ) ) { 
  12. return cptui_admin_notices( error, '', false, __( 'Please provide a post type name', 'custom-post-type-ui' ) ); 
  13.  
  14. if ( ! empty( $data['cpt_original'] ) && $data['cpt_original'] != $data['cpt_custom_post_type']['name'] ) { 
  15. if ( ! empty( $data['update_post_types'] ) ) { 
  16. add_filter( 'cptui_convert_post_type_posts', '__return_true' ); 
  17.  
  18. // Clean up $_POST data. 
  19. foreach ( $data as $key => $value ) { 
  20. if ( is_string( $value ) ) { 
  21. $data[ $key ] = sanitize_text_field( $value ); 
  22. } else { 
  23. array_map( 'sanitize_text_field', $data[ $key ] ); 
  24.  
  25. // Check if they didn't put quotes in the name or rewrite slug. 
  26. if ( false !== strpos( $data['cpt_custom_post_type']['name'], '\'' ) || 
  27. false !== strpos( $data['cpt_custom_post_type']['name'], '\"' ) || 
  28. false !== strpos( $data['cpt_custom_post_type']['rewrite_slug'], '\'' ) || 
  29. false !== strpos( $data['cpt_custom_post_type']['rewrite_slug'], '\"' ) ) { 
  30.  
  31. return error; 
  32.  
  33. $post_types = cptui_get_post_type_data(); 
  34.  
  35. /** 
  36. * Check if we already have a post type of that name. 
  37. * @since 1.3.0 
  38. * @param bool $value Assume we have no conflict by default. 
  39. * @param string $value Post type slug being saved. 
  40. * @param array $post_types Array of existing post types from CPTUI. 
  41. */ 
  42. $slug_exists = apply_filters( 'cptui_post_type_slug_exists', false, $data['cpt_custom_post_type']['name'], $post_types ); 
  43. $slug_as_page = cptui_check_page_slugs( $data['cpt_custom_post_type']['name'] ); 
  44. if ( true === $slug_exists ) { 
  45. return error; 
  46. if ( true === $slug_as_page ) { 
  47. return error; 
  48.  
  49. if ( empty( $data['cpt_addon_taxes'] ) || ! is_array( $data['cpt_addon_taxes'] ) ) { 
  50. $data['cpt_addon_taxes'] = array(); 
  51.  
  52. if ( empty( $data['cpt_supports'] ) || ! is_array( $data['cpt_supports'] ) ) { 
  53. $data['cpt_supports'] = array(); 
  54.  
  55. foreach ( $data['cpt_labels'] as $key => $label ) { 
  56. if ( empty( $label ) ) { 
  57. unset( $data['cpt_labels'][ $key ] ); 
  58.  
  59. $label = str_replace( '"', '', htmlspecialchars_decode( $label ) ); 
  60. $label = htmlspecialchars( $label, ENT_QUOTES ); 
  61. $label = trim( $label ); 
  62. if ( 'parent' === $key ) { 
  63. $data['cpt_labels']['parent_item_colon'] = stripslashes_deep( $label ); 
  64. } else { 
  65. $data['cpt_labels'][ $key ] = stripslashes_deep( $label ); 
  66.  
  67. if ( empty( $data['cpt_custom_post_type']['menu_icon'] ) ) { 
  68. $data['cpt_custom_post_type']['menu_icon'] = null; 
  69.  
  70. $label = ucwords( str_replace( '_', ' ', $data['cpt_custom_post_type']['name'] ) ); 
  71. if ( ! empty( $data['cpt_custom_post_type']['label'] ) ) { 
  72. $label = str_replace( '"', '', htmlspecialchars_decode( $data['cpt_custom_post_type']['label'] ) ); 
  73. $label = htmlspecialchars( stripslashes( $label ), ENT_QUOTES ); 
  74.  
  75. $singular_label = ucwords( str_replace( '_', ' ', $data['cpt_custom_post_type']['name'] ) ); 
  76. if ( ! empty( $data['cpt_custom_post_type']['singular_label'] ) ) { 
  77. $singular_label = str_replace( '"', '', htmlspecialchars_decode( $data['cpt_custom_post_type']['singular_label'] ) ); 
  78. $singular_label = htmlspecialchars( stripslashes( $singular_label ), ENT_QUOTES ); 
  79.  
  80. $name = trim( $data['cpt_custom_post_type']['name'] ); 
  81. $description = stripslashes_deep( $data['cpt_custom_post_type']['description'] ); 
  82. $rest_base = trim( $data['cpt_custom_post_type']['rest_base'] ); 
  83. $has_archive_string = trim( $data['cpt_custom_post_type']['has_archive_string'] ); 
  84. $capability_type = trim( $data['cpt_custom_post_type']['capability_type'] ); 
  85. $rewrite_slug = trim( $data['cpt_custom_post_type']['rewrite_slug'] ); 
  86. $query_var_slug = trim( $data['cpt_custom_post_type']['query_var_slug'] ); 
  87. $menu_position = trim( $data['cpt_custom_post_type']['menu_position'] ); 
  88. $show_in_menu_string = trim( $data['cpt_custom_post_type']['show_in_menu_string'] ); 
  89. $menu_icon = trim( $data['cpt_custom_post_type']['menu_icon'] ); 
  90. $custom_supports = trim( $data['cpt_custom_post_type']['custom_supports'] ); 
  91.  
  92. $post_types[ $data['cpt_custom_post_type']['name'] ] = array( 
  93. 'name' => $name,  
  94. 'label' => $label,  
  95. 'singular_label' => $singular_label,  
  96. 'description' => $description,  
  97. 'public' => disp_boolean( $data['cpt_custom_post_type']['public'] ),  
  98. 'publicly_queryable' => disp_boolean( $data['cpt_custom_post_type']['publicly_queryable'] ),  
  99. 'show_ui' => disp_boolean( $data['cpt_custom_post_type']['show_ui'] ),  
  100. 'show_in_nav_menus' => disp_boolean( $data['cpt_custom_post_type']['show_in_nav_menus'] ),  
  101. 'show_in_rest' => disp_boolean( $data['cpt_custom_post_type']['show_in_rest'] ),  
  102. 'rest_base' => $rest_base,  
  103. 'has_archive' => disp_boolean( $data['cpt_custom_post_type']['has_archive'] ),  
  104. 'has_archive_string' => $has_archive_string,  
  105. 'exclude_from_search' => disp_boolean( $data['cpt_custom_post_type']['exclude_from_search'] ),  
  106. 'capability_type' => $capability_type,  
  107. 'hierarchical' => disp_boolean( $data['cpt_custom_post_type']['hierarchical'] ),  
  108. 'rewrite' => disp_boolean( $data['cpt_custom_post_type']['rewrite'] ),  
  109. 'rewrite_slug' => $rewrite_slug,  
  110. 'rewrite_withfront' => disp_boolean( $data['cpt_custom_post_type']['rewrite_withfront'] ),  
  111. 'query_var' => disp_boolean( $data['cpt_custom_post_type']['query_var'] ),  
  112. 'query_var_slug' => $query_var_slug,  
  113. 'menu_position' => $menu_position,  
  114. 'show_in_menu' => disp_boolean( $data['cpt_custom_post_type']['show_in_menu'] ),  
  115. 'show_in_menu_string' => $show_in_menu_string,  
  116. 'menu_icon' => $menu_icon,  
  117. 'supports' => $data['cpt_supports'],  
  118. 'taxonomies' => $data['cpt_addon_taxes'],  
  119. 'labels' => $data['cpt_labels'],  
  120. 'custom_supports' => $custom_supports,  
  121. ); 
  122.  
  123. /** 
  124. * Filters whether or not 3rd party options were saved successfully within post type add/update. 
  125. * @since 1.3.0 
  126. * @param bool $value Whether or not someone else saved successfully. Default false. 
  127. * @param array $post_types Array of our updated post types data. 
  128. * @param array $data Array of submitted post type to update. 
  129. */ 
  130. if ( false === ( $success = apply_filters( 'cptui_post_type_update_save', false, $post_types, $data ) ) ) { 
  131. $success = update_option( 'cptui_post_types', $post_types ); 
  132.  
  133. /** 
  134. * Fires after a post type is updated to our saved options. 
  135. * @since 1.0.0 
  136. * @param array $data Array of post type data that was updated. 
  137. */ 
  138. do_action( 'cptui_after_update_post_type', $data ); 
  139.  
  140. // Used to help flush rewrite rules on init. 
  141. set_transient( 'cptui_flush_rewrite_rules', 'true', 5 * 60 ); 
  142.  
  143. if ( isset( $success ) ) { 
  144. if ( 'new' == $data['cpt_type_status'] ) { 
  145. return 'add_success'; 
  146. return 'update_success';