capability, 'af_export_form', [ $this, 'export_page' ] ); } /** * Display form export page * * @since 1.5.0 */ function export_page() { if ( ! isset( $_GET['form_id'] ) ) { return; } $form_id = $_GET['form_id']; $form = af_form_from_post( $form_id ); $admin_page_url = menu_page_url( 'af_export_form', false ); $form_json_link = add_query_arg( 'form_id', $form_id, $admin_page_url ); $form_json_link = add_query_arg( 'export_json', true, $form_json_link ); // Add nonce to link $form_json_link = $this->add_nonce_to_url( $form_json_link ); $form_link = get_edit_post_link( $form['post_id'] ); $code = $this->generate_form_code( $form ); ?>

" "


%s', $export_url, __( 'Export', 'advanced-forms' ) ); } /** * Wordpress won't display a page title, probably because of the submenu hack in register_admin_page. * This function is hooked to the admin_title filter and fixes the issue. * * @since 1.5.0 */ function fix_admin_title( $admin_title, $title ) { if ( get_current_screen()->id != 'admin_page_af_export_form' ) { return $admin_title; } return __( 'Export form', 'advanced-forms' ) . $admin_title; } function generate_form_code( $form ) { // Remove post ID key unset( $form['post_id'] ); $str_replace = array( " " => "\t", "'!!__(!!\'" => "__('", "!!\', !!\'" => "', '", "!!\')!!'" => "')", "array (" => "array(" ); $preg_replace = array( '/([\t\r\n]+?)array/' => 'array', '/[0-9]+ => array/' => 'array' ); // Create a var_export string of the form array $code = var_export( $form, true ); // change double spaces to tabs $code = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $code ); // correctly formats "=> array(" $code = preg_replace( array_keys( $preg_replace ), array_values( $preg_replace ), $code ); $code = esc_textarea( $code ); $output = "function register_forms() {\n"; $output .= sprintf( "\taf_register_form( %s );\n", str_replace( "\n", "\n\t", $code ) ); $output .= "}\n"; $output .= "add_action( 'af/register_forms', 'register_forms' );"; return $output; } /** * Generate a JSON file for a form and output it to trigger a download. * * @since 1.6.5 */ function export_json_file() { if ( ! isset( $_GET['form_id'] ) || ! isset( $_GET['export_json'] ) ) { return; } if ( ! current_user_can( $this->capability ) ) { wp_die( __( 'You do not have sufficient permissions to export forms.', 'advanced-forms' ), __( 'Error: Insufficient permissions', 'advanced-forms' ), [ 'back_link' => true ] ); } if ( ! $this->verify_nonce() ) { wp_die( __( 'Form could not exported due to missing or invalid nonce.', 'advanced-forms' ), __( 'Error: Invalid nonce', 'advanced-forms' ), [ 'back_link' => true ] ); } $form = af_form_from_post( $_GET['form_id'] ); // PHP versions below 5.4 don't support JSON_PRETTY_PRINT. $json_pretty_print = 128; $json = wp_json_encode( af_export_form( $form ), $json_pretty_print ); $file_name = sprintf( '%s.json', $form['key'] ); header( 'Content-disposition: attachment; filename=' . $file_name ); header( 'Content-type: application/json' ); echo $json; exit; } private function add_nonce_to_url( $url ) { return wp_nonce_url( $url, 'af_export_form', 'af_export_nonce' ); } private function verify_nonce() { $nonce = isset( $_GET['af_export_nonce'] ) ? $_GET['af_export_nonce'] : ''; return wp_verify_nonce( $nonce, 'af_export_form' ); } } return new AF_Admin_Forms_Export();