ACF_Field_Group::import_post
Imports an ACF post into the database.
Method of the class: ACF_Field_Group{}
Hooks from the method
Returns
Array.
Usage
$ACF_Field_Group = new ACF_Field_Group(); $ACF_Field_Group->import_post( $post );
- $post(array) (required)
- The ACF post array.
Changelog
| Since 6.1 | Introduced. |
ACF_Field_Group::import_post() ACF Field Group::import post code ACF 6.8.8
public function import_post( $post ) {
// Disable filters to ensure data is not modified by local, clone, etc.
$filters = acf_disable_filters();
// Validate the post (ensures all settings exist).
$post = $this->get_valid_post( $post );
// Prepare post for import (modifies settings).
$post = $this->prepare_post_for_import( $post );
// Prepare fields for import (modifies settings).
$fields = acf_prepare_fields_for_import( $post['fields'] );
// Stores a map of field "key" => "ID".
$ids = array();
// If the field group has an ID, review and delete stale fields in the database.
if ( $post['ID'] ) {
// Load database fields.
$db_fields = acf_prepare_fields_for_import( acf_get_fields( $post ) );
// Generate map of "index" => "key" data.
$keys = wp_list_pluck( $fields, 'key' );
// Loop over db fields and delete those who don't exist in $new_fields.
foreach ( $db_fields as $field ) {
// Add field data to $ids map.
$ids[ $field['key'] ] = $field['ID'];
// Delete field if not in $keys.
if ( ! in_array( $field['key'], $keys, true ) ) {
acf_delete_field( $field['ID'] );
}
}
}
// When importing a new field group, insert a temporary post and set the field group's ID.
// This allows fields to be updated before the field group (field group ID is needed for field parent setting).
if ( ! $post['ID'] ) {
$post['ID'] = wp_insert_post(
array(
'post_title' => $post['key'],
'post_type' => $this->post_type,
)
);
}
// Add field group data to $ids map.
$ids[ $post['key'] ] = $post['ID'];
// Loop over and add fields.
if ( $fields ) {
foreach ( $fields as $field ) {
// Replace any "key" references with "ID".
if ( isset( $ids[ $field['key'] ] ) ) {
$field['ID'] = $ids[ $field['key'] ];
}
if ( isset( $ids[ $field['parent'] ] ) ) {
$field['parent'] = $ids[ $field['parent'] ];
}
// Save field.
$field = acf_update_field( $field );
// Add field data to $ids map for children.
$ids[ $field['key'] ] = $field['ID'];
}
}
// Save field group.
$post = $this->update_post( $post );
// Enable filters again.
acf_enable_filters( $filters );
/**
* Fires immediately after an ACF post has been imported.
*
* @date 12/02/2014
* @since 5.0.0
*
* @param array $post The ACF post array.
*/
do_action( "acf/import_{$this->hook_name}", $post );
return $post;
}