Last active
September 8, 2021 02:47
-
-
Save DeoThemes/b8d10bc562095bb9d9de40efc1a7bf69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* After import hook | |
* This hook is fired after the import is completed | |
*/ | |
public function after_import() { | |
// Add filter of the link rel attr to avoid JSON damage. | |
add_filter( 'wp_targeted_link_rel', '__return_empty_string', 50, 1 ); | |
// Here we grab the JSON file manualy from our importer directory. | |
$json_file = file_get_contents( trailingslashit( ESS_DIR_PATH ) . 'inc/wpforms/form.json' ); | |
$json_string = $json_file; | |
if ( strpos( bin2hex( $json_file ), 'efbbbf' ) === 0 ) { | |
$json_string = substr( $json_file, 3 ); | |
} | |
$forms = json_decode( $json_string, true ); | |
if ( empty( $forms ) || ! is_array( $forms ) ) { | |
wp_die( | |
esc_html__( 'Form data cannot be imported.', 'wpforms-lite' ), | |
esc_html__( 'Error', 'wpforms-lite' ), | |
[ | |
'response' => 400, | |
] | |
); | |
} | |
foreach ( $forms as $form ) { | |
$title = ! empty( $form['settings']['form_title'] ) ? $form['settings']['form_title'] : ''; | |
$desc = ! empty( $form['settings']['form_desc'] ) ? $form['settings']['form_desc'] : ''; | |
$new_id = wp_insert_post( | |
[ | |
'import_id' => $form['id'], // we have to assign a specific ID here from the form.json | |
'post_title' => $title, | |
'post_status' => 'publish', | |
'post_type' => 'wpforms', | |
'post_excerpt' => $desc, | |
] | |
); | |
if ( $new_id ) { | |
wp_update_post( | |
[ | |
'ID' => $new_id, | |
'post_content' => wp_slash( wp_json_encode( $form ) ), | |
] | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment