/inc/tools.php

  1. <?php 
  2. /** 
  3. * Custom Post Type UI Tools. 
  4. * 
  5. * @package CPTUI 
  6. * @subpackage Tools 
  7. * @author WebDevStudios 
  8. * @since 1.0.0 
  9. */ 
  10.  
  11. // Exit if accessed directly. 
  12. if ( ! defined( 'ABSPATH' ) ) { 
  13. exit; 
  14.  
  15. /** 
  16. * Register our tabs for the Tools screen. 
  17. * 
  18. * @since 1.3.0 
  19. * @since 1.5.0 Renamed to "Tools" 
  20. * 
  21. * @internal 
  22. * 
  23. * @param array $tabs Array of tabs to display. Optional. 
  24. * @param string $current_page Current page being shown. Optional. Default empty string. 
  25. * @return array Amended array of tabs to show. 
  26. */ 
  27. function cptui_tools_tabs( $tabs = array(), $current_page = '' ) { 
  28.  
  29. if ( 'tools' === $current_page ) { 
  30. $classes = array( 'nav-tab' ); 
  31.  
  32. $tabs['page_title'] = get_admin_page_title(); 
  33. $tabs['tabs'] = array(); 
  34. $tabs['tabs']['post_types'] = array( 
  35. 'text' => __( 'Post Types', 'custom-post-type-ui' ),  
  36. 'classes' => $classes,  
  37. 'url' => cptui_admin_url( 'admin.php?page=cptui_' . $current_page ),  
  38. 'aria-selected' => 'false',  
  39. ); 
  40.  
  41. $tabs['tabs']['taxonomies'] = array( 
  42. 'text' => __( 'Taxonomies', 'custom-post-type-ui' ),  
  43. 'classes' => $classes,  
  44. 'url' => esc_url( add_query_arg( array( 'action' => 'taxonomies' ), cptui_admin_url( 'admin.php?page=cptui_' . $current_page ) ) ),  
  45. 'aria-selected' => 'false',  
  46. ); 
  47.  
  48. $tabs['tabs']['get_code'] = array( 
  49. 'text' => __( 'Get Code', 'custom-post-type-ui' ),  
  50. 'classes' => $classes,  
  51. 'url' => esc_url( add_query_arg( array( 'action' => 'get_code' ), cptui_admin_url( 'admin.php?page=cptui_' . $current_page ) ) ),  
  52. 'aria-selected' => 'false',  
  53. ); 
  54.  
  55. $tabs['tabs']['debuginfo'] = array( 
  56. 'text' => __( 'Debug Info', 'custom-post-type-ui' ),  
  57. 'classes' => $classes,  
  58. 'url' => esc_url( add_query_arg( array( 'action' => 'debuginfo' ), cptui_admin_url( 'admin.php?page=cptui_' . $current_page ) ) ),  
  59. 'aria-selected' => 'false',  
  60. ); 
  61.  
  62. $active_class = 'nav-tab-active'; 
  63. $action = cptui_get_current_action(); 
  64. if ( ! empty( $action ) ) { 
  65. if ( 'taxonomies' === $action ) { 
  66. $tabs['tabs']['taxonomies']['classes'][] = $active_class; 
  67. $tabs['tabs']['taxonomies']['aria-selected'] = 'true'; 
  68. } elseif ( 'get_code' === $action ) { 
  69. $tabs['tabs']['get_code']['classes'][] = $active_class; 
  70. $tabs['tabs']['get_code']['aria-selected'] = 'true'; 
  71. } elseif ( 'debuginfo' === $action ) { 
  72. $tabs['tabs']['debuginfo']['classes'][] = $active_class; 
  73. $tabs['tabs']['debuginfo']['aria-selected'] = 'true'; 
  74. } else { 
  75. $tabs['tabs']['post_types']['classes'][] = $active_class; 
  76. $tabs['tabs']['post_types']['aria-selected'] = 'true'; 
  77.  
  78. /** 
  79. * Filters the tabs being added for the tools area. 
  80. * 
  81. * @since 1.5.0 
  82. * 
  83. * @param array $tabs Array of tabs to show. 
  84. * @param string $action Current tab being shown. 
  85. * @param string $active_class Class to use to mark the tab active. 
  86. */ 
  87. $tabs = apply_filters( 'cptui_tools_tabs', $tabs, $action, $active_class ); 
  88.  
  89. return $tabs; 
  90. add_filter( 'cptui_get_tabs', 'cptui_tools_tabs', 10, 2 ); 
  91.  
  92. /** 
  93. * Create our settings page output. 
  94. * 
  95. * @since 1.0.0 
  96. * 
  97. * @internal 
  98. */ 
  99. function cptui_tools() { 
  100.  
  101. $tab = ''; 
  102. if ( ! empty( $_GET ) ) { 
  103. if ( ! empty( $_GET['action'] ) && 'taxonomies' === $_GET['action'] ) { 
  104. $tab = 'taxonomies'; 
  105. } elseif ( ! empty( $_GET['action'] ) && 'get_code' === $_GET['action'] ) { 
  106. $tab = 'get_code'; 
  107. } elseif ( ! empty( $_GET['action'] ) && 'debuginfo' === $_GET['action'] ) { 
  108. $tab = 'debuginfo'; 
  109. } else { 
  110. $tab = 'post_types'; 
  111.  
  112. echo '<div class="wrap">'; 
  113.  
  114. /** 
  115. * Fires right inside the wrap div for the import/export pages. 
  116. * 
  117. * @since 1.3.0 
  118. * 
  119. * @deprecated 1.5.0 
  120. */ 
  121. do_action_deprecated( 'cptui_inside_importexport_wrap', array(), '1.5.0', 'cptui_inside_tools_wrap' ); 
  122.  
  123. /** 
  124. * Fires right inside the wrap div for the tools pages. 
  125. * 
  126. * @since 1.5.0 
  127. */ 
  128. do_action( 'cptui_inside_tools_wrap' ); 
  129.  
  130. // Create our tabs. 
  131. cptui_settings_tab_menu( $page = 'tools' ); 
  132.  
  133. /** 
  134. * Fires inside the markup for the import/export section. 
  135. * 
  136. * Allows for more modular control and adding more sections more easily. 
  137. * 
  138. * @since 1.2.0 
  139. * 
  140. * @deprecated 1.5.0 
  141. * 
  142. * @param string $tab Current tab being displayed. 
  143. */ 
  144. do_action_deprecated( 'cptui_import_export_sections', array( $tab ), '1.5.0', 'cptui_tools_sections' ); 
  145.  
  146. /** 
  147. * Fires inside the markup for the tools section. 
  148. * 
  149. * Allows for more modular control and adding more sections more easily. 
  150. * 
  151. * @since 1.5.0 
  152. * 
  153. * @param string $tab Current tab being displayed. 
  154. */ 
  155. do_action( 'cptui_tools_sections', $tab ); 
  156.  
  157.  
  158. echo '</div><!-- End .wrap -->'; 
  159.  
  160. /** 
  161. * Display our copy-able code for registered taxonomies. 
  162. * 
  163. * @since 1.0.0 
  164. * @since 1.2.0 Added $cptui_taxonomies parameter. 
  165. * @since 1.2.0 Added $single parameter. 
  166. * 
  167. * @param array $cptui_taxonomies Array of taxonomies to render. Optional. 
  168. * @param bool $single Whether or not we are rendering a single taxonomy. Optional. Default false. 
  169. */ 
  170. function cptui_get_taxonomy_code( $cptui_taxonomies = array(), $single = false ) { 
  171. if ( ! empty( $cptui_taxonomies ) ) { 
  172. $callback = 'cptui_register_my_taxes'; 
  173. if ( $single ) { 
  174. $key = key( $cptui_taxonomies ); 
  175. $callback = 'cptui_register_my_taxes_' . str_replace( '-', '_', $cptui_taxonomies[ $key ]['name'] ); 
  176. ?> 
  177. function <?php echo $callback; ?>() { 
  178. <?php 
  179. foreach ( $cptui_taxonomies as $tax ) { 
  180. echo cptui_get_single_taxonomy_registery( $tax ); 
  181. } ?> 
  182.  
  183. add_action( 'init', '<?php echo $callback; ?>' ); 
  184. <?php 
  185. } else { 
  186. _e( 'No taxonomies to display at this time', 'custom-post-type-ui' ); 
  187.  
  188. /** 
  189. * Create output for single taxonomy to be ready for copy/paste from Get Code. 
  190. * 
  191. * @since 1.0.0 
  192. * 
  193. * @param array $taxonomy Taxonomy data to output. Optional. 
  194. */ 
  195. function cptui_get_single_taxonomy_registery( $taxonomy = array() ) { 
  196.  
  197. $post_types = "''"; 
  198. if ( is_array( $taxonomy['object_types'] ) ) { 
  199. $post_types = 'array( "' . implode( '", "', $taxonomy['object_types'] ) . '" )'; 
  200.  
  201. if ( false !== get_disp_boolean( $taxonomy['rewrite'] ) ) { 
  202. $rewrite = disp_boolean( $taxonomy['rewrite'] ); 
  203.  
  204. $rewrite_slug = ' \'slug\' => \'' . $taxonomy['name'] . '\', '; 
  205. if ( ! empty( $taxonomy['rewrite_slug'] ) ) { 
  206. $rewrite_slug = ' \'slug\' => \'' . $taxonomy['rewrite_slug'] . '\', '; 
  207.  
  208. $rewrite_withfront = ''; 
  209. $withfront = disp_boolean( $taxonomy['rewrite_withfront'] ); 
  210. if ( ! empty( $withfront ) ) { 
  211. $rewrite_withfront = ' \'with_front\' => ' . $withfront . ', '; 
  212.  
  213. $hierarchical = ( ! empty( $taxonomy['rewrite_hierarchical'] ) ) ? disp_boolean( $taxonomy['rewrite_hierarchical'] ) : ''; 
  214. $rewrite_hierarchcial = ''; 
  215. if ( ! empty( $hierarchical ) ) { 
  216. $rewrite_hierarchcial = ' \'hierarchical\' => ' . $hierarchical . ', '; 
  217.  
  218. if ( ! empty( $taxonomy['rewrite_slug'] ) || false !== disp_boolean( $taxonomy['rewrite_withfront'] ) ) { 
  219. $rewrite_start = 'array('; 
  220. $rewrite_end = ')'; 
  221.  
  222. $rewrite = $rewrite_start . $rewrite_slug . $rewrite_withfront . $rewrite_hierarchcial . $rewrite_end; 
  223. } else { 
  224. $rewrite = disp_boolean( $taxonomy['rewrite'] ); 
  225. $public = ( isset( $taxonomy['public'] ) ) ? disp_boolean( $taxonomy['public'] ) : 'true'; 
  226. $show_in_quick_edit = ( isset( $taxonomy['show_in_quick_edit'] ) ) ? disp_boolean( $taxonomy['show_in_quick_edit'] ) : disp_boolean( $taxonomy['show_ui'] ); 
  227.  
  228. $show_in_menu = ( ! empty( $taxonomy['show_in_menu'] ) && false !== get_disp_boolean( $taxonomy['show_in_menu'] ) ) ? 'true' : 'false'; 
  229. if ( empty( $taxonomy['show_in_menu'] ) ) { 
  230. $show_in_menu = disp_boolean( $taxonomy['show_ui'] ); 
  231.  
  232. $show_in_nav_menus = ( ! empty( $taxonomy['show_in_nav_menus'] ) && false !== get_disp_boolean( $taxonomy['show_in_nav_menus'] ) ) ? 'true' : 'false'; 
  233. if ( empty( $taxonomy['show_in_nav_menus'] ) ) { 
  234. $show_in_nav_menus = $public; 
  235.  
  236. $my_theme = wp_get_theme(); 
  237. $textdomain = $my_theme->get( 'TextDomain' ); 
  238. ?> 
  239.  
  240. /** 
  241. * Taxonomy: <?php echo $taxonomy['label']; ?>. 
  242. */ 
  243.  
  244. $labels = array( 
  245. "name" => __( '<?php echo $taxonomy['label']; ?>', '<?php echo $textdomain; ?>' ),  
  246. "singular_name" => __( '<?php echo $taxonomy['singular_label']; ?>', '<?php echo $textdomain; ?>' ),  
  247. <?php 
  248. foreach ( $taxonomy['labels'] as $key => $label ) { 
  249. if ( ! empty( $label ) ) { 
  250. echo "\t\t" . '"' . $key . '" => __( \'' . $label . '\', \'' . $textdomain . '\' ), ' . "\n"; 
  251. ?> 
  252. ); 
  253.  
  254. $args = array( 
  255. "label" => __( '<?php echo $taxonomy['label']; ?>', '<?php echo $textdomain; ?>' ),  
  256. "labels" => $labels,  
  257. "public" => <?php echo $public; ?>,  
  258. "hierarchical" => <?php echo $taxonomy['hierarchical']; ?>,  
  259. "label" => "<?php echo $taxonomy['label']; ?>",  
  260. "show_ui" => <?php echo disp_boolean( $taxonomy['show_ui'] ); ?>,  
  261. "show_in_menu" => <?php echo $show_in_menu; ?>,  
  262. "show_in_nav_menus" => <?php echo $show_in_nav_menus; ?>,  
  263. "query_var" => <?php echo disp_boolean( $taxonomy['query_var'] );?>,  
  264. "rewrite" => <?php echo $rewrite; ?>,  
  265. "show_admin_column" => <?php echo $taxonomy['show_admin_column']; ?>,  
  266. "show_in_rest" => <?php echo disp_boolean( $taxonomy['show_in_rest'] ); ?>,  
  267. "rest_base" => "<?php echo $taxonomy['rest_base']; ?>",  
  268. "show_in_quick_edit" => <?php echo $show_in_quick_edit; ?>,  
  269. ); 
  270. register_taxonomy( "<?php echo $taxonomy['name']; ?>", <?php echo $post_types; ?>, $args ); 
  271. <?php 
  272.  
  273. /** 
  274. * Display our copy-able code for registered post types. 
  275. * 
  276. * @since 1.0.0 
  277. * @since 1.2.0 Added $cptui_post_types parameter. 
  278. * @since 1.2.0 Added $single parameter. 
  279. * 
  280. * @param array $cptui_post_types Array of post types to render. Optional. 
  281. * @param bool $single Whether or not we are rendering a single post type. Optional. Default false. 
  282. */ 
  283. function cptui_get_post_type_code( $cptui_post_types = array(), $single = false ) { 
  284. // Whitespace very much matters here, thus why it's all flush against the left side. 
  285. if ( ! empty( $cptui_post_types ) ) { 
  286. $callback = 'cptui_register_my_cpts'; 
  287. if ( $single ) { 
  288. $key = key( $cptui_post_types ); 
  289. $callback = 'cptui_register_my_cpts_' . str_replace( '-', '_', $cptui_post_types[ $key ]['name'] ); 
  290. ?> 
  291.  
  292. function <?php echo $callback; ?>() { 
  293. <?php // Space before this line reflects in textarea. 
  294. foreach ( $cptui_post_types as $type ) { 
  295. echo cptui_get_single_post_type_registery( $type ); 
  296. ?> 
  297.  
  298. add_action( 'init', '<?php echo $callback; ?>' ); 
  299. <?php 
  300. } else { 
  301. _e( 'No post types to display at this time', 'custom-post-type-ui' ); 
  302.  
  303. /** 
  304. * Create output for single post type to be ready for copy/paste from Get Code. 
  305. * 
  306. * @since 1.0.0 
  307. * 
  308. * @param array $post_type Post type data to output. Optional. 
  309. */ 
  310. function cptui_get_single_post_type_registery( $post_type = array() ) { 
  311.  
  312. /** This filter is documented in custom-post-type-ui/custom-post-type-ui.php */ 
  313. $post_type['map_meta_cap'] = apply_filters( 'cptui_map_meta_cap', 'true', $post_type['name'], $post_type ); 
  314.  
  315. /** This filter is documented in custom-post-type-ui/custom-post-type-ui.php */ 
  316. $user_supports_params = apply_filters( 'cptui_user_supports_params', array(), $post_type['name'], $post_type ); 
  317. if ( is_array( $user_supports_params ) ) { 
  318. $post_type['supports'] = array_merge( $post_type['supports'], $user_supports_params ); 
  319.  
  320. $yarpp = false; // Prevent notices. 
  321. if ( ! empty( $post_type['custom_supports'] ) ) { 
  322. $custom = explode( ', ', $post_type['custom_supports'] ); 
  323. foreach ( $custom as $part ) { 
  324. // We'll handle YARPP separately. 
  325. if ( in_array( $part, array( 'YARPP', 'yarpp' ) ) ) { 
  326. $yarpp = true; 
  327. continue; 
  328. $post_type['supports'][] = $part; 
  329.  
  330. $rewrite_withfront = ''; 
  331. $rewrite = get_disp_boolean( $post_type['rewrite'] ); 
  332. if ( false !== $rewrite ) { 
  333. $rewrite = disp_boolean( $post_type['rewrite'] ); 
  334.  
  335. $rewrite_slug = ' "slug" => "' . $post_type['name'] . '", '; 
  336. if ( ! empty( $post_type['rewrite_slug'] ) ) { 
  337. $rewrite_slug = ' "slug" => "' . $post_type['rewrite_slug'] . '", '; 
  338.  
  339. $withfront = disp_boolean( $post_type['rewrite_withfront'] ); 
  340. if ( ! empty( $withfront ) ) { 
  341. $rewrite_withfront = ' "with_front" => ' . $withfront . ' '; 
  342.  
  343. if ( ! empty( $post_type['rewrite_slug'] ) || ! empty( $post_type['rewrite_withfront'] ) ) { 
  344. $rewrite_start = 'array('; 
  345. $rewrite_end = ')'; 
  346.  
  347. $rewrite = $rewrite_start . $rewrite_slug . $rewrite_withfront . $rewrite_end; 
  348. } else { 
  349. $rewrite = disp_boolean( $post_type['rewrite'] ); 
  350. $has_archive = get_disp_boolean( $post_type['has_archive'] ); 
  351. if ( false !== $has_archive ) { 
  352. $has_archive = disp_boolean( $post_type['has_archive'] ); 
  353. if ( ! empty( $post_type['has_archive_string'] ) ) { 
  354. $has_archive = '"' . $post_type['has_archive_string'] . '"'; 
  355. } else { 
  356. $has_archive = disp_boolean( $post_type['has_archive'] ); 
  357.  
  358. $supports = ''; 
  359. // Do a little bit of php work to get these into strings. 
  360. if ( ! empty( $post_type['supports'] ) && is_array( $post_type['supports'] ) ) { 
  361. $supports = 'array( "' . implode( '", "', $post_type['supports'] ) . '" )'; 
  362.  
  363. if ( in_array( 'none', $post_type['supports'] ) ) { 
  364. $supports = 'false'; 
  365.  
  366. $taxonomies = ''; 
  367. if ( ! empty( $post_type['taxonomies'] ) && is_array( $post_type['taxonomies'] ) ) { 
  368. $taxonomies = 'array( "' . implode( '", "', $post_type['taxonomies'] ) . '" )'; 
  369.  
  370. if ( in_array( $post_type['query_var'], array( 'true', 'false', '0', '1' ) ) ) { 
  371. $post_type['query_var'] = disp_boolean( $post_type['query_var'] ); 
  372. if ( ! empty( $post_type['query_var_slug'] ) ) { 
  373. $post_type['query_var'] = '"' . $post_type['query_var_slug'] . '"'; 
  374.  
  375. if ( empty( $post_type['show_in_rest'] ) ) { 
  376. $post_type['show_in_rest'] = 'false'; 
  377.  
  378. $post_type['description'] = addslashes( $post_type['description'] ); 
  379.  
  380. $my_theme = wp_get_theme(); 
  381. $textdomain = $my_theme->get( 'TextDomain' ); 
  382. ?> 
  383.  
  384. /** 
  385. * Post Type: <?php echo $post_type['label']; ?>. 
  386. */ 
  387.  
  388. $labels = array( 
  389. "name" => __( '<?php echo $post_type['label']; ?>', '<?php echo $textdomain; ?>' ),  
  390. "singular_name" => __( '<?php echo $post_type['singular_label']; ?>', '<?php echo $textdomain; ?>' ),  
  391. <?php 
  392. foreach ( $post_type['labels'] as $key => $label ) { 
  393. if ( ! empty( $label ) ) { 
  394. if ( 'parent' === $key ) { 
  395. // Fix for incorrect label key. See #439. 
  396. echo "\t\t" . '"' . 'parent_item_colon' . '" => __( \'' . $label . '\', \'' . $textdomain . '\' ), ' . "\n"; 
  397. } else { 
  398. echo "\t\t" . '"' . $key . '" => __( \'' . $label . '\', \'' . $textdomain . '\' ), ' . "\n"; 
  399. ?> 
  400. ); 
  401.  
  402. $args = array( 
  403. "label" => __( '<?php echo $post_type['label']; ?>', '<?php echo $textdomain; ?>' ),  
  404. "labels" => $labels,  
  405. "description" => "<?php echo $post_type['description']; ?>",  
  406. "public" => <?php echo disp_boolean( $post_type['public'] ); ?>,  
  407. "publicly_queryable" => <?php echo disp_boolean( $post_type['publicly_queryable'] ); ?>,  
  408. "show_ui" => <?php echo disp_boolean( $post_type['show_ui'] ); ?>,  
  409. "show_in_rest" => <?php echo disp_boolean( $post_type['show_in_rest'] ); ?>,  
  410. "rest_base" => "<?php echo $post_type['rest_base']; ?>",  
  411. "has_archive" => <?php echo $has_archive; ?>,  
  412. "show_in_menu" => <?php echo disp_boolean( $post_type['show_in_menu'] ); ?>,  
  413. <?php if ( ! empty( $post_type['show_in_menu_string'] ) ) { ?> 
  414. "show_in_menu_string" => "<?php echo $post_type['show_in_menu_string']; ?>",  
  415. <?php } ?> 
  416. "exclude_from_search" => <?php echo disp_boolean( $post_type['exclude_from_search'] ); ?>,  
  417. "capability_type" => "<?php echo $post_type['capability_type']; ?>",  
  418. "map_meta_cap" => <?php echo disp_boolean( $post_type['map_meta_cap'] ); ?>,  
  419. "hierarchical" => <?php echo disp_boolean( $post_type['hierarchical'] ); ?>,  
  420. "rewrite" => <?php echo $rewrite; ?>,  
  421. "query_var" => <?php echo $post_type['query_var']; ?>,  
  422. <?php if ( ! empty( $post_type['menu_position'] ) ) { ?> 
  423. "menu_position" => <?php echo $post_type['menu_position']; ?>,  
  424. <?php } ?> 
  425. <?php if ( ! empty( $post_type['menu_icon'] ) ) { ?> 
  426. "menu_icon" => "<?php echo $post_type['menu_icon']; ?>",  
  427. <?php } ?> 
  428. <?php if ( ! empty( $supports ) ) { ?> 
  429. "supports" => <?php echo $supports; ?>,  
  430. <?php } ?> 
  431. <?php if ( ! empty( $taxonomies ) ) { ?> 
  432. "taxonomies" => <?php echo $taxonomies; ?>,  
  433. <?php } ?> 
  434. <?php if ( true === $yarpp ) { ?> 
  435. "yarpp_support" => <?php echo disp_boolean( $yarpp ); ?>,  
  436. <?php } ?> 
  437. ); 
  438.  
  439. register_post_type( "<?php echo $post_type['name']; ?>", $args ); 
  440. <?php 
  441.  
  442. /** 
  443. * Import the posted JSON data from a separate export. 
  444. * 
  445. * @since 1.0.0 
  446. * 
  447. * @internal 
  448. * 
  449. * @param array $postdata $_POST data as json. Optional. 
  450. * @return mixed false on nothing to do, otherwise void. 
  451. */ 
  452. function cptui_import_types_taxes_settings( $postdata = array() ) { 
  453. if ( ! isset( $postdata['cptui_post_import'] ) && ! isset( $postdata['cptui_tax_import'] ) ) { 
  454. return false; 
  455.  
  456. $status = 'import_fail'; 
  457. $success = false; 
  458.  
  459. /** 
  460. * Filters the post type data to import. 
  461. * 
  462. * Allows third parties to provide their own data dump and import instead of going through our UI. 
  463. * 
  464. * @since 1.2.0 
  465. * 
  466. * @param bool $value Default to no data. 
  467. */ 
  468. $third_party_post_type_data = apply_filters( 'cptui_third_party_post_type_import', false ); 
  469.  
  470. /** 
  471. * Filters the taxonomy data to import. 
  472. * 
  473. * Allows third parties to provide their own data dump and import instead of going through our UI. 
  474. * 
  475. * @since 1.2.0 
  476. * 
  477. * @param bool $value Default to no data. 
  478. */ 
  479. $third_party_taxonomy_data = apply_filters( 'cptui_third_party_taxonomy_import', false ); 
  480.  
  481. if ( false !== $third_party_post_type_data ) { 
  482. $postdata['cptui_post_import'] = $third_party_post_type_data; 
  483.  
  484. if ( false !== $third_party_taxonomy_data ) { 
  485. $postdata['cptui_tax_import'] = $third_party_taxonomy_data; 
  486.  
  487. if ( ! empty( $postdata['cptui_post_import'] ) ) { 
  488. $cpt_data = stripslashes_deep( trim( $postdata['cptui_post_import'] ) ); 
  489. $settings = json_decode( $cpt_data, true ); 
  490.  
  491. // Add support to delete settings outright, without accessing database. 
  492. // Doing double check to protect. 
  493. if ( is_null( $settings ) && '{""}' === $cpt_data ) { 
  494.  
  495. /** 
  496. * Filters whether or not 3rd party options were deleted successfully within post type import. 
  497. * 
  498. * @since 1.3.0 
  499. * 
  500. * @param bool $value Whether or not someone else deleted successfully. Default false. 
  501. * @param array $postdata Post type data. 
  502. */ 
  503. if ( false === ( $success = apply_filters( 'cptui_post_type_import_delete_save', false, $postdata ) ) ) { 
  504. $success = delete_option( 'cptui_post_types' ); 
  505.  
  506. if ( $settings ) { 
  507. if ( false !== cptui_get_post_type_data() ) { 
  508. /** This filter is documented in /inc/import-export.php */ 
  509. if ( false === ( $success = apply_filters( 'cptui_post_type_import_delete_save', false, $postdata ) ) ) { 
  510. delete_option( 'cptui_post_types' ); 
  511.  
  512. /** 
  513. * Filters whether or not 3rd party options were updated successfully within the post type import. 
  514. * 
  515. * @since 1.3.0 
  516. * 
  517. * @param bool $value Whether or not someone else updated successfully. Default false. 
  518. * @param array $postdata Post type data. 
  519. */ 
  520. if ( false === ( $success = apply_filters( 'cptui_post_type_import_update_save', false, $postdata ) ) ) { 
  521. $success = update_option( 'cptui_post_types', $settings ); 
  522. // Used to help flush rewrite rules on init. 
  523. set_transient( 'cptui_flush_rewrite_rules', 'true', 5 * 60 ); 
  524.  
  525. if ( $success ) { 
  526. $status = 'import_success'; 
  527. } elseif ( ! empty( $postdata['cptui_tax_import'] ) ) { 
  528. $tax_data = stripslashes_deep( trim( $postdata['cptui_tax_import'] ) ); 
  529. $settings = json_decode( $tax_data, true ); 
  530.  
  531. // Add support to delete settings outright, without accessing database. 
  532. // Doing double check to protect. 
  533. if ( is_null( $settings ) && '{""}' === $tax_data ) { 
  534.  
  535. /** 
  536. * Filters whether or not 3rd party options were deleted successfully within taxonomy import. 
  537. * 
  538. * @since 1.3.0 
  539. * 
  540. * @param bool $value Whether or not someone else deleted successfully. Default false. 
  541. * @param array $postdata Taxonomy data 
  542. */ 
  543. if ( false === ( $success = apply_filters( 'cptui_taxonomy_import_delete_save', false, $postdata ) ) ) { 
  544. $success = delete_option( 'cptui_taxonomies' ); 
  545.  
  546. if ( $settings ) { 
  547. if ( false !== cptui_get_taxonomy_data() ) { 
  548. /** This filter is documented in /inc/import-export.php */ 
  549. if ( false === ( $success = apply_filters( 'cptui_taxonomy_import_delete_save', false, $postdata ) ) ) { 
  550. delete_option( 'cptui_taxonomies' ); 
  551. /** 
  552. * Filters whether or not 3rd party options were updated successfully within the taxonomy import. 
  553. * 
  554. * @since 1.3.0 
  555. * 
  556. * @param bool $value Whether or not someone else updated successfully. Default false. 
  557. * @param array $postdata Taxonomy data. 
  558. */ 
  559. if ( false === ( $success = apply_filters( 'cptui_taxonomy_import_update_save', false, $postdata ) ) ) { 
  560. $success = update_option( 'cptui_taxonomies', $settings ); 
  561. // Used to help flush rewrite rules on init. 
  562. set_transient( 'cptui_flush_rewrite_rules', 'true', 5 * 60 ); 
  563. if ( $success ) { 
  564. $status = 'import_success'; 
  565.  
  566. return $status; 
  567.  
  568. /** 
  569. * Content for the Post Types/Taxonomies Tools tab. 
  570. * 
  571. * @since 1.2.0 
  572. * 
  573. * @internal 
  574. */ 
  575. function cptui_render_posttypes_taxonomies_section() { 
  576. ?> 
  577.  
  578. <p><?php _e( 'If you are wanting to migrate registered post types or taxonomies from this site to another, that will also use Custom Post Type UI, use the import and export functionality. If you are moving away from Custom Post Type UI, use the information in the "Get Code" tab.', 'custom-post-type-ui' ); ?></p> 
  579.  
  580. <p> 
  581. <?php 
  582. printf( 
  583. '<strong>%s</strong>: %s',  
  584. __( 'NOTE', 'custom-post-type-ui' ),  
  585. __( 'This will not export the associated posts or taxonomy terms, just the settings.', 'custom-post-type-ui' ) 
  586. ); 
  587. ?> 
  588. </p> 
  589. <table class="form-table cptui-table"> 
  590. <?php if ( ! empty( $_GET ) && empty( $_GET['action'] ) ) { ?> 
  591. <tr> 
  592. <td class="outter"> 
  593. <h2><label for="cptui_post_import"><?php _e( 'Import Post Types', 'custom-post-type-ui' ); ?></label></h2> 
  594.  
  595. <form method="post"> 
  596. <textarea class="cptui_post_import" placeholder="<?php esc_attr_e( 'Paste content here.', 'custom-post-type-ui' ); ?>" id="cptui_post_import" name="cptui_post_import"></textarea> 
  597.  
  598. <p class="wp-ui-highlight"> 
  599. <strong><?php _e( 'Note:', 'custom-post-type-ui' ); ?></strong> <?php _e( 'Importing will overwrite previous registered settings.', 'custom-post-type-ui' ); ?> 
  600. </p> 
  601.  
  602. <p> 
  603. <strong><?php _e( 'To import post types from a different WordPress site, paste the exported content from that site and click the "Import" button.', 'custom-post-type-ui' ); ?></strong> 
  604. </p> 
  605.  
  606. <p> 
  607. <input class="button button-primary" type="submit" value="<?php esc_attr_e( 'Import', 'custom-post-type-ui' ); ?>" /> 
  608. </p> 
  609. </form> 
  610. </td> 
  611. <td class="outter"> 
  612. <h2><label for="cptui_post_export"><?php _e( 'Export Post Types', 'custom-post-type-ui' ); ?></label></h2> 
  613. <?php 
  614. $cptui_post_types = cptui_get_post_type_data(); 
  615. if ( ! empty( $cptui_post_types ) ) { 
  616. $content = esc_html( json_encode( $cptui_post_types ) ); 
  617. } else { 
  618. $content = __( 'No post types registered yet.', 'custom-post-type-ui' ); 
  619. ?> 
  620. <textarea title="<?php esc_attr_e( 'To copy the system info, click below then press Ctrl + C (PC) or Cmd + C (Mac).', 'custom-post-type-ui' ); ?>" onclick="this.focus();this.select();" onfocus="this.focus();this.select();" readonly="readonly" aria-readonly="true" class="cptui_post_import" id="cptui_post_export" name="cptui_post_export"><?php echo $content; ?></textarea> 
  621.  
  622. <p> 
  623. <strong><?php _e( 'Use the content above to import current post types into a different WordPress site. You can also use this to simply back up your post type settings.', 'custom-post-type-ui' ); ?></strong> 
  624. </p> 
  625. </td> 
  626. </tr> 
  627. <?php } elseif ( ! empty( $_GET ) && 'taxonomies' == $_GET['action'] ) { ?> 
  628. <tr> 
  629. <td class="outter"> 
  630. <h2><label for="cptui_tax_import"><?php _e( 'Import Taxonomies', 'custom-post-type-ui' ); ?></label></h2> 
  631.  
  632. <form method="post"> 
  633. <textarea class="cptui_tax_import" placeholder="<?php esc_attr_e( 'Paste content here.', 'custom-post-type-ui' ); ?>" id="cptui_tax_import" name="cptui_tax_import"></textarea> 
  634.  
  635. <p class="wp-ui-highlight"> 
  636. <strong><?php _e( 'Note:', 'custom-post-type-ui' ); ?></strong> <?php _e( 'Importing will overwrite previous registered settings.', 'custom-post-type-ui' ); ?> 
  637. </p> 
  638.  
  639. <p> 
  640. <strong><?php _e( 'To import taxonomies from a different WordPress site, paste the exported content from that site and click the "Import" button.', 'custom-post-type-ui' ); ?></strong> 
  641. </p> 
  642.  
  643. <p> 
  644. <input class="button button-primary" type="submit" value="<?php esc_attr_e( 'Import', 'custom-post-type-ui' ); ?>" /> 
  645. </p> 
  646. </form> 
  647. </td> 
  648. <td class="outter"> 
  649. <h2><label for="cptui_tax_export"><?php _e( 'Export Taxonomies', 'custom-post-type-ui' ); ?></label></h2> 
  650. <?php 
  651. $cptui_taxonomies = cptui_get_taxonomy_data(); 
  652. if ( ! empty( $cptui_taxonomies ) ) { 
  653. $content = esc_html( json_encode( $cptui_taxonomies ) ); 
  654. } else { 
  655. $content = __( 'No taxonomies registered yet.', 'custom-post-type-ui' ); 
  656. ?> 
  657. <textarea title="<?php esc_attr_e( 'To copy the system info, click below then press Ctrl + C (PC) or Cmd + C (Mac).', 'custom-post-type-ui' ); ?>" onclick="this.focus();this.select()" onfocus="this.focus();this.select();" readonly="readonly" aria-readonly="true" class="cptui_tax_import" id="cptui_tax_export" name="cptui_tax_export"><?php echo $content; ?></textarea> 
  658.  
  659. <p> 
  660. <strong><?php _e( 'Use the content above to import current taxonomies into a different WordPress site. You can also use this to simply back up your taxonomy settings.', 'custom-post-type-ui' ); ?></strong> 
  661. </p> 
  662. </td> 
  663. </tr> 
  664. <?php } ?> 
  665. </table> 
  666. <?php 
  667.  
  668. /** 
  669. * Content for the Get Code tab. 
  670. * 
  671. * @since 1.2.0 
  672. * 
  673. * @internal 
  674. */ 
  675. function cptui_render_getcode_section() { 
  676. ?> 
  677. <h1><?php _e( 'Get Post Type and Taxonomy Code', 'custom-post-type-ui' ); ?></h1> 
  678.  
  679. <h2><?php _e( 'All CPT UI Post Types', 'custom-post-type-ui' ); ?></h2> 
  680.  
  681. <p><?php esc_html_e( 'All of the selectable code snippets below are useful if you wish to migrate away from CPTUI and retain your existing registered post types or taxonomies.', 'custom-post-type-ui' ); ?></p> 
  682.  
  683. <?php $cptui_post_types = cptui_get_post_type_data(); ?> 
  684. <label for="cptui_post_type_get_code"><?php _e( 'Copy/paste the code below into your functions.php file.', 'custom-post-type-ui' ); ?></label> 
  685. <textarea name="cptui_post_type_get_code" id="cptui_post_type_get_code" class="cptui_post_type_get_code" onclick="this.focus();this.select()" onfocus="this.focus();this.select();" readonly="readonly" aria-readonly="true"><?php cptui_get_post_type_code( $cptui_post_types ); ?></textarea> 
  686.  
  687. <?php 
  688. if ( ! empty( $cptui_post_types ) ) { 
  689. foreach ( $cptui_post_types as $post_type ) { ?> 
  690. <h2 id="<?php echo esc_attr( $post_type['name'] ); ?>"><?php 
  691. $type = ( ! empty( $post_type['label'] ) ) ? $post_type['label'] : $post_type['name']; 
  692. printf( __( '%s Post Type', 'custom-post-type-ui' ), $type ); ?></h2> 
  693. <label for="cptui_post_type_get_code_<?php echo $post_type['name']; ?>"><?php _e( 'Copy/paste the code below into your functions.php file.', 'custom-post-type-ui' ); ?></label> 
  694. <textarea name="cptui_post_type_get_code_<?php echo $post_type['name']; ?>" id="cptui_post_type_get_code_<?php echo $post_type['name']; ?>" class="cptui_post_type_get_code" onclick="this.focus();this.select()" onfocus="this.focus();this.select();" readonly="readonly" aria-readonly="true"><?php cptui_get_post_type_code( array( $post_type ), true ); ?></textarea> 
  695. <?php } 
  696. } ?> 
  697.  
  698. <h2><?php _e( 'All CPT UI Taxonomies', 'custom-post-type-ui' ); ?></h2> 
  699.  
  700. <?php $cptui_taxonomies = cptui_get_taxonomy_data(); ?> 
  701. <label for="cptui_tax_get_code"><?php _e( 'Copy/paste the code below into your functions.php file.', 'custom-post-type-ui' ); ?></label> 
  702. <textarea name="cptui_tax_get_code" id="cptui_tax_get_code" class="cptui_tax_get_code" onclick="this.focus();this.select()" onfocus="this.focus();this.select();" readonly="readonly" aria-readonly="true"><?php cptui_get_taxonomy_code( $cptui_taxonomies ); ?></textarea> 
  703.  
  704. <?php 
  705. if ( ! empty( $cptui_taxonomies ) ) { 
  706. foreach ( $cptui_taxonomies as $taxonomy ) { ?> 
  707. <h2 id="<?php echo esc_attr( $taxonomy['name'] ); ?>"><?php 
  708. $tax = ( ! empty( $taxonomy['label'] ) ) ? $taxonomy['label'] : $taxonomy['name']; 
  709. printf( __( '%s Taxonomy', 'custom-post-type-ui' ), $tax ); ?></h2> 
  710. <label for="cptui_tax_get_code_<?php echo $taxonomy['name']; ?>"><?php _e( 'Copy/paste the code below into your functions.php file.', 'custom-post-type-ui' ); ?></label> 
  711. <textarea name="cptui_tax_get_code_<?php echo $taxonomy['name']; ?>" id="cptui_tax_get_code_<?php echo $taxonomy['name']; ?>" class="cptui_tax_get_code" onclick="this.focus();this.select()" onfocus="this.focus();this.select();" readonly="readonly" aria-readonly="true"><?php cptui_get_taxonomy_code( array( $taxonomy ), true ); ?></textarea> 
  712. <?php } 
  713. } ?> 
  714. <?php 
  715.  
  716. /** 
  717. * Content for the Debug Info tab. 
  718. * 
  719. * @since 1.2.0 
  720. * 
  721. * @internal 
  722. */ 
  723. function cptui_render_debuginfo_section() { 
  724. $debuginfo = new CPTUI_Debug_Info(); 
  725.  
  726. echo '<form id="cptui_debug_info" method="post">'; 
  727. $debuginfo->tab_site_info(); 
  728.  
  729. if ( ! empty( $_POST ) && isset( $_POST['cptui_debug_info_email'] ) ) { 
  730. $email_args = array(); 
  731. $email_args['email'] = sanitize_text_field( $_POST['cptui_debug_info_email'] ); 
  732. $debuginfo->send_email( $email_args ); 
  733.  
  734. echo '<p><label for="cptui_debug_info_email">' . __( 'Please provide an email address to send debug information to: ', 'custom-post-type-ui' ) . '</label><input type="email" id="cptui_debug_info_email" name="cptui_debug_info_email" value="" /></p>'; 
  735.  
  736. /** 
  737. * Filters the text value to use on the button when sending debug information. 
  738. * 
  739. * @since 1.2.0 
  740. * 
  741. * @param string $value Text to use for the button. 
  742. */ 
  743. echo '<p><input type="submit" class="button-primary" name="cptui_send_debug_email" value="' . esc_attr( apply_filters( 'cptui_debug_email_submit_button', __( 'Send debug info', 'custom-post-type-ui' ) ) ) . '" /></p>'; 
  744. echo '</form>'; 
  745.  
  746. /** 
  747. * Fires after the display of the site information. 
  748. * 
  749. * @since 1.3.0 
  750. */ 
  751. do_action( 'cptui_after_site_info' ); 
  752.  
  753. /** 
  754. * Renders various tab sections for the Tools page, based on current tab. 
  755. * 
  756. * @since 1.2.0 
  757. * 
  758. * @internal 
  759. * 
  760. * @param string $tab Current tab to display. 
  761. */ 
  762. function cptui_render_tools( $tab ) { 
  763. if ( isset( $tab ) ) { 
  764. if ( 'post_types' == $tab || 'taxonomies' == $tab ) { 
  765. cptui_render_posttypes_taxonomies_section(); 
  766.  
  767. if ( 'get_code' == $tab ) { 
  768. cptui_render_getcode_section(); 
  769.  
  770. if ( 'debuginfo' == $tab ) { 
  771. cptui_render_debuginfo_section(); 
  772. add_action( 'cptui_tools_sections', 'cptui_render_tools' ); 
  773.  
  774. /** 
  775. * Handle the import of transferred post types and taxonomies. 
  776. * 
  777. * @since 1.5.0 
  778. */ 
  779. function cptui_do_import_types_taxes() { 
  780.  
  781. if ( ! empty( $_POST ) && 
  782. ( ! empty( $_POST['cptui_post_import'] ) && isset( $_POST['cptui_post_import'] ) ) || 
  783. ( ! empty( $_POST['cptui_tax_import'] ) && isset( $_POST['cptui_tax_import'] ) ) 
  784. ) { 
  785. $success = cptui_import_types_taxes_settings( $_POST ); 
  786. add_action( 'admin_notices', "cptui_{$success}_admin_notice" ); 
  787. add_action( 'init', 'cptui_do_import_types_taxes', 8 ); 
.