2018-12-04 14:01:14 +01:00
< ? php
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
if ( ! class_exists ( 'ACF_Admin_Tool_Import' ) ) :
class ACF_Admin_Tool_Import extends ACF_Admin_Tool {
/**
* initialize
*
* This function will initialize the admin tool
*
* @ date 10 / 10 / 17
* @ since 5.6 . 3
*
* @ param n / a
* @ return n / a
*/
function initialize () {
// vars
$this -> name = 'import' ;
$this -> title = __ ( " Import Field Groups " , 'acf' );
$this -> icon = 'dashicons-upload' ;
}
/**
* html
*
* This function will output the metabox HTML
*
* @ date 10 / 10 / 17
* @ since 5.6 . 3
*
* @ param n / a
* @ return n / a
*/
function html () {
?>
< p >< ? php _e ( 'Select the Advanced Custom Fields JSON file you would like to import. When you click the import button below, ACF will import the field groups.' , 'acf' ); ?> </p>
< div class = " acf-fields " >
< ? php
acf_render_field_wrap ( array (
'label' => __ ( 'Select File' , 'acf' ),
'type' => 'file' ,
'name' => 'acf_import_file' ,
'value' => false ,
'uploader' => 'basic' ,
));
?>
</ div >
< p class = " acf-submit " >
< input type = " submit " class = " button button-primary " value = " <?php _e('Import File', 'acf'); ?> " />
</ p >
< ? php
}
/**
* submit
*
* This function will run when the tool ' s form has been submit
*
* @ date 10 / 10 / 17
* @ since 5.6 . 3
*
* @ param n / a
* @ return n / a
*/
function submit () {
2021-08-04 17:09:29 +02:00
// Check file size.
2018-12-04 14:01:14 +01:00
if ( empty ( $_FILES [ 'acf_import_file' ][ 'size' ]) ) {
2021-08-04 17:09:29 +02:00
return acf_add_admin_notice ( __ ( " No file selected " , 'acf' ), 'warning' );
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Get file data.
2018-12-04 14:01:14 +01:00
$file = $_FILES [ 'acf_import_file' ];
2021-08-04 17:09:29 +02:00
// Check errors.
2018-12-04 14:01:14 +01:00
if ( $file [ 'error' ] ) {
2021-08-04 17:09:29 +02:00
return acf_add_admin_notice ( __ ( " Error uploading file. Please try again " , 'acf' ), 'warning' );
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Check file type.
2018-12-04 14:01:14 +01:00
if ( pathinfo ( $file [ 'name' ], PATHINFO_EXTENSION ) !== 'json' ) {
2021-08-04 17:09:29 +02:00
return acf_add_admin_notice ( __ ( " Incorrect file type " , 'acf' ), 'warning' );
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Read JSON.
2018-12-04 14:01:14 +01:00
$json = file_get_contents ( $file [ 'tmp_name' ] );
$json = json_decode ( $json , true );
2021-08-04 17:09:29 +02:00
// Check if empty.
if ( ! $json || ! is_array ( $json ) ) {
return acf_add_admin_notice ( __ ( " Import file empty " , 'acf' ), 'warning' );
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Ensure $json is an array of groups.
2018-12-04 14:01:14 +01:00
if ( isset ( $json [ 'key' ]) ) {
2021-08-04 17:09:29 +02:00
$json = array ( $json );
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Remeber imported field group ids.
2018-12-04 14:01:14 +01:00
$ids = array ();
2021-08-04 17:09:29 +02:00
// Loop over json
2018-12-04 14:01:14 +01:00
foreach ( $json as $field_group ) {
2021-08-04 17:09:29 +02:00
// Search database for existing field group.
$post = acf_get_field_group_post ( $field_group [ 'key' ] );
if ( $post ) {
$field_group [ 'ID' ] = $post -> ID ;
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Import field group.
$field_group = acf_import_field_group ( $field_group );
2018-12-04 14:01:14 +01:00
2021-08-04 17:09:29 +02:00
// append message
$ids [] = $field_group [ 'ID' ];
2018-12-04 14:01:14 +01:00
}
2021-08-04 17:09:29 +02:00
// Count number of imported field groups.
$total = count ( $ids );
// Generate text.
$text = sprintf ( _n ( 'Imported 1 field group' , 'Imported %s field groups' , $total , 'acf' ), $total );
2018-12-04 14:01:14 +01:00
2021-08-04 17:09:29 +02:00
// Add links to text.
$links = array ();
foreach ( $ids as $id ) {
$links [] = '<a href="' . get_edit_post_link ( $id ) . '">' . get_the_title ( $id ) . '</a>' ;
}
$text .= ' ' . implode ( ', ' , $links );
// Add notice
acf_add_admin_notice ( $text , 'success' );
2018-12-04 14:01:14 +01:00
}
}
// initialize
acf_register_admin_tool ( 'ACF_Admin_Tool_Import' );
endif ; // class_exists check
?>