name = 'import'; $this->title = __( 'Import Admin Forms', 'frontend-admin' ); $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() { ?>
form_display->get_form( $form['id'] ); if ( $post ) { $form['ID'] = $post['ID']; } // Import form. $form = $this->import_form( $form ); // append message $ids[] = $form['ID']; } // Count number of imported forms. $total = count( $ids ); // Generate text. $text = sprintf( /* translators: %s: number of forms */ _n( 'Imported %s form', 'Imported %s forms', $total, 'frontend-admin' ), $total ); // Add links to text. $links = array(); foreach ( $ids as $id ) { $links[] = '' . get_the_title( $id ) . ''; } $text .= ' ' . implode( ', ', $links ); // Add notice acf_add_admin_notice( $text, 'success' ); } function import_form( $form ) { // Disable filters to ensure data is not modified by local, clone, etc. $filters = acf_disable_filters(); /* // Validate form (ensures all settings exist). $form = acf_get_valid_form( $form ); // Prepare group for import (modifies settings). $form = acf_prepare_form_for_import( $form ); */ $fields = acf_prepare_fields_for_import( $form['fields'] ); // Stores a map of field "key" => "ID". $ids = array(); // If the form has an ID, review and delete stale fields in the database. if ( isset( $form['ID'] ) ) { // Load database fields. $db_fields = acf_prepare_fields_for_import( acf_get_fields( $form['ID'] ) ); // 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 ) ) { acf_delete_field( $field['ID'] ); } } } // When importing a new form, insert a temporary post and set the form's ID. // This allows fields to be updated before the form (form ID is needed for field parent setting). if ( empty( $form['ID'] ) ) { $form['ID'] = wp_insert_post( array( 'post_type' => 'admin_form', 'post_title' => $form['title'], ) ); update_post_meta( $form['ID'], 'admin_form_type', 'general' ); update_post_meta( $form['ID'], 'form_key', $form['id'] ); } // Add form data to $ids map. $ids[ $form['id'] ] = $form['ID']; // Loop over and add fields. if ( $fields ) { foreach ( $fields as $field ) { $field['parent'] = $form['ID']; // 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']; } } // Enable filters again. acf_enable_filters( $filters ); /** * Fires immediately after a form has been imported. * * @date 12/02/2014 * @since 5.0.0 * * @param array $form The form array. */ do_action( 'frontend_admin/import_form', $form ); // return new form. return $form; } } // initialize fea_instance()->admin_tools->register_tool( 'FEA_Form_Import' ); endif; // class_exists check ?>