__( 'Import from Custom Post Type UI', 'acf' ),
'type' => 'checkbox',
'name' => 'acf_import_cptui',
'choices' => $choices,
'toggle' => true,
)
);
?>
' . esc_html__( 'Importing a Post Type or Taxonomy with the same key as one that already exists will overwrite the settings for the existing Post Type or Taxonomy with those of the import.', 'acf' ) . '';
}
?>
import_cpt_ui( $import );
}
// Check file size.
if ( empty( $_FILES['acf_import_file']['size'] ) ) {
return acf_add_admin_notice( __( 'No file selected', 'acf' ), 'warning' );
}
$file = acf_sanitize_files_array( $_FILES['acf_import_file'] );
// Check errors.
if ( $file['error'] ) {
return acf_add_admin_notice( __( 'Error uploading file. Please try again', 'acf' ), 'warning' );
}
// Check file type.
if ( pathinfo( $file['name'], PATHINFO_EXTENSION ) !== 'json' ) {
return acf_add_admin_notice( __( 'Incorrect file type', 'acf' ), 'warning' );
}
// Read JSON.
$json = file_get_contents( $file['tmp_name'] );
$json = json_decode( $json, true );
// Check if empty.
if ( ! $json || ! is_array( $json ) ) {
return acf_add_admin_notice( __( 'Import file empty', 'acf' ), 'warning' );
}
// Ensure $json is an array of posts.
if ( isset( $json['key'] ) ) {
$json = array( $json );
}
// Remember imported post ids.
$ids = array();
// Loop over json.
foreach ( $json as $to_import ) {
// Search database for existing post.
$post_type = acf_determine_internal_post_type( $to_import['key'] );
$post = acf_get_internal_post_type_post( $to_import['key'], $post_type );
if ( $post ) {
$to_import['ID'] = $post->ID;
}
// Import the post.
$to_import = acf_import_internal_post_type( $to_import, $post_type );
// Append message.
$ids[] = $to_import['ID'];
}
// Count number of imported posts.
$total = count( $ids );
// Generate text.
$text = sprintf( _n( 'Imported 1 item', 'Imported %s items', $total, 'acf' ), $total );
// Add links to text.
$links = array();
foreach ( $ids as $id ) {
$links[] = '' . get_the_title( $id ) . '';
}
$text .= ' ' . implode( ', ', $links );
// Add notice.
return acf_add_admin_notice( $text, 'success' );
//phpcs:enable WordPress.Security.NonceVerification.Missing
}
/**
* Handles the import of CPTUI post types and taxonomies.
*
* @since 6.1
*
* @param array $import_args What to import.
* @return ACF_Admin_Notice
*/
public function import_cpt_ui( $import_args ) {
if ( ! is_array( $import_args ) ) {
return acf_add_admin_notice( __( 'Nothing from Custom Post Type UI plugin selected for import.', 'acf' ), 'warning' );
}
$imported = array();
// Import any post types.
if ( in_array( 'post_types', $import_args, true ) ) {
$cptui_post_types = get_option( 'cptui_post_types' );
$instance = acf_get_internal_post_type_instance( 'acf-post-type' );
if ( ! is_array( $cptui_post_types ) || ! $instance ) {
return acf_add_admin_notice( __( 'Failed to import post types.', 'acf' ), 'warning' );
}
foreach ( $cptui_post_types as $post_type ) {
$result = $instance->import_cptui_post_type( $post_type );
if ( is_array( $result ) && isset( $result['ID'] ) ) {
$imported[] = (int) $result['ID'];
}
}
}
// Import any taxonomies.
if ( in_array( 'taxonomies', $import_args, true ) ) {
$cptui_taxonomies = get_option( 'cptui_taxonomies' );
$instance = acf_get_internal_post_type_instance( 'acf-taxonomy' );
if ( ! is_array( $cptui_taxonomies ) || ! $instance ) {
return acf_add_admin_notice( __( 'Failed to import taxonomies.', 'acf' ), 'warning' );
}
foreach ( $cptui_taxonomies as $taxonomy ) {
$result = $instance->import_cptui_taxonomy( $taxonomy );
if ( is_array( $result ) && isset( $result['ID'] ) ) {
$imported[] = (int) $result['ID'];
}
}
}
if ( ! empty( $imported ) ) {
// Generate text.
$total = count( $imported );
/* translators: %d - number of items imported from CPTUI */
$text = sprintf( _n( 'Imported %d item from Custom Post Type UI -', 'Imported %d items from Custom Post Type UI -', $total, 'acf' ), $total );
// Add links to text.
$links = array();
foreach ( $imported as $id ) {
$links[] = '' . get_the_title( $id ) . '';
}
$text .= ' ' . implode( ', ', $links );
$text .= __( '. The Custom Post Type UI plugin can be deactivated.', 'acf' );
return acf_add_admin_notice( $text, 'success' );
}
return acf_add_admin_notice( __( 'Nothing to import', 'acf' ), 'warning' );
}
}
// Initialize.
acf_register_admin_tool( 'ACF_Admin_Tool_Import' );
endif; // class_exists check.
?>