PluginProbe
Advanced Forms for ACF / trunk
Advanced Forms for ACF vtrunk
1.9.0 1.9.1 1.9.2.1 1.9.3 1.9.3.1 1.9.3.2 1.9.3.3 1.9.3.4 1.9.3.5 1.9.3.6 1.9.3.7 1.9.3.8 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.3.1 1.0.3.2 1.0.3.3 1.0.4 1.1.0 1.1.1 1.2.0 1.3.0 All 51 releases
advanced-forms / admin / forms / forms-export.php

forms-export.php in Advanced Forms for ACF trunk, at admin/forms/forms-export.php

194 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class AF_Admin_Forms_Export {
4
5 private $capability = 'edit_pages';
6
7 function __construct() {
8 add_action( 'admin_menu', array( $this, 'register_admin_page' ), 10, 0 );
9 add_action( 'admin_init', array( $this, 'export_json_file' ), 10, 0 );
10 add_action( 'af/admin/form/actions', array( $this, 'add_export_button' ), 15, 1 );
11 add_filter( 'admin_title', array( $this, 'fix_admin_title' ), 10, 2 );
12 }
13
14 /**
15 * Register hidden admin page for form export.
16 *
17 * @since 1.5.0
18 */
19 function register_admin_page() {
20 // By using add_submenu_page without a parent the page won't be shown in the admin menu.
21 add_submenu_page(
22 'admin.php',
23 'Export form',
24 'Export',
25 $this->capability,
26 'af_export_form',
27 [ $this, 'export_page' ]
28 );
29 }
30
31 /**
32 * Display form export page
33 *
34 * @since 1.5.0
35 */
36 function export_page() {
37 if ( ! isset( $_GET['form_id'] ) ) {
38 return;
39 }
40
41 $form_id = $_GET['form_id'];
42 $form = af_form_from_post( $form_id );
43
44 $admin_page_url = menu_page_url( 'af_export_form', false );
45 $form_json_link = add_query_arg( 'form_id', $form_id, $admin_page_url );
46 $form_json_link = add_query_arg( 'export_json', true, $form_json_link );
47
48 // Add nonce to link
49 $form_json_link = $this->add_nonce_to_url( $form_json_link );
50
51 $form_link = get_edit_post_link( $form['post_id'] );
52 $code = $this->generate_form_code( $form );
53 ?>
54 <div class="af-form-export wrap">
55 <h1 class="wp-heading-inline"><?php _e( 'Exporting', 'advanced-forms' ); ?> "<?php echo $form['title']; ?>
56 "</h1>
57 <a href="<?php echo $form_link; ?>"
58 class="page-title-action"><?php _e( 'Back to form', 'advanced-forms' ) ?></a>
59 <hr class="wp-header-end"/>
60
61 <div id="poststuff">
62 <div class="postbox">
63 <div class="inside">
64 <h2><?php _e( 'Export JSON', 'advanced-forms' ); ?></h2>
65 <p>
66 <?php _e( 'Export your form as a JSON file. The form can then be imported on any site at Forms &rarr; Import.', 'advanced-forms' ); ?>
67 </p>
68 <a class="button button-primary" href="<?php echo $form_json_link; ?>">
69 <?php _e( 'Export file', 'advanced-forms' ); ?>
70 </a>
71 </div>
72
73 <div class="inside">
74 <h2><?php _e( 'Register programmatically', 'advanced-forms' ); ?></h2>
75 <p>
76 <?php _e( 'Place the following code in your theme or plugin to register your form programmatically.', 'advanced-forms' ); ?>
77 <button class="copy-button button button-small" data-copied-text="Copied!">Copy to
78 clipboard
79 </button>
80 </p>
81 <pre class="export-code"><?php echo $code; ?></pre>
82 </div>
83 </div>
84 </div>
85 </div>
86 <?php
87 }
88
89 function add_export_button( $form_id ) {
90 $export_url = add_query_arg( 'form_id', $form_id, menu_page_url( 'af_export_form', false ) );
91 echo sprintf( '<a href="%s" class="button button-large">%s</a>', $export_url, __( 'Export', 'advanced-forms' ) );
92 }
93
94 /**
95 * Wordpress won't display a page title, probably because of the submenu hack in register_admin_page.
96 * This function is hooked to the admin_title filter and fixes the issue.
97 *
98 * @since 1.5.0
99 */
100 function fix_admin_title( $admin_title, $title ) {
101 if ( get_current_screen()->id != 'admin_page_af_export_form' ) {
102 return $admin_title;
103 }
104
105 return __( 'Export form', 'advanced-forms' ) . $admin_title;
106 }
107
108 function generate_form_code( $form ) {
109 // Remove post ID key
110 unset( $form['post_id'] );
111
112 $str_replace = array(
113 " " => "\t",
114 "'!!__(!!\'" => "__('",
115 "!!\', !!\'" => "', '",
116 "!!\')!!'" => "')",
117 "array (" => "array("
118 );
119 $preg_replace = array(
120 '/([\t\r\n]+?)array/' => 'array',
121 '/[0-9]+ => array/' => 'array'
122 );
123
124 // Create a var_export string of the form array
125 $code = var_export( $form, true );
126
127 // change double spaces to tabs
128 $code = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $code );
129
130 // correctly formats "=> array("
131 $code = preg_replace( array_keys( $preg_replace ), array_values( $preg_replace ), $code );
132
133 $code = esc_textarea( $code );
134
135 $output = "function register_forms() {\n";
136 $output .= sprintf( "\taf_register_form( %s );\n", str_replace( "\n", "\n\t", $code ) );
137 $output .= "}\n";
138 $output .= "add_action( 'af/register_forms', 'register_forms' );";
139
140 return $output;
141 }
142
143 /**
144 * Generate a JSON file for a form and output it to trigger a download.
145 *
146 * @since 1.6.5
147 */
148 function export_json_file() {
149 if ( ! isset( $_GET['form_id'] ) || ! isset( $_GET['export_json'] ) ) {
150 return;
151 }
152
153 if ( ! current_user_can( $this->capability ) ) {
154 wp_die(
155 __( 'You do not have sufficient permissions to export forms.', 'advanced-forms' ),
156 __( 'Error: Insufficient permissions', 'advanced-forms' ),
157 [ 'back_link' => true ]
158 );
159 }
160
161 if ( ! $this->verify_nonce() ) {
162 wp_die(
163 __( 'Form could not exported due to missing or invalid nonce.', 'advanced-forms' ),
164 __( 'Error: Invalid nonce', 'advanced-forms' ),
165 [ 'back_link' => true ]
166 );
167 }
168
169 $form = af_form_from_post( $_GET['form_id'] );
170
171 // PHP versions below 5.4 don't support JSON_PRETTY_PRINT.
172 $json_pretty_print = 128;
173 $json = wp_json_encode( af_export_form( $form ), $json_pretty_print );
174
175 $file_name = sprintf( '%s.json', $form['key'] );
176 header( 'Content-disposition: attachment; filename=' . $file_name );
177 header( 'Content-type: application/json' );
178
179 echo $json;
180 exit;
181 }
182
183 private function add_nonce_to_url( $url ) {
184 return wp_nonce_url( $url, 'af_export_form', 'af_export_nonce' );
185 }
186
187 private function verify_nonce() {
188 $nonce = isset( $_GET['af_export_nonce'] ) ? $_GET['af_export_nonce'] : '';
189 return wp_verify_nonce( $nonce, 'af_export_form' );
190 }
191
192 }
193
194 return new AF_Admin_Forms_Export();