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 / core / core-gutenberg.php

core-gutenberg.php in Advanced Forms for ACF trunk, at core/core-gutenberg.php

282 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class AF_Core_Gutenberg {
4 function __construct() {
5 add_action( 'acf/init', array( $this, 'register_block'));
6 add_filter( 'acf/load_field/name=af_block_form', array( $this, 'populate_form_select_field'), 10, 1 );
7 add_filter( 'acf/load_field/name=af_block_exclude_fields', array( $this, 'populate_exclude_fields_field'), 10, 1 );
8
9 add_action( 'acf/init', array( $this, 'register_block_fields' ) );
10 add_filter( 'af/form/gutenberg/fields', array( $this, 'add_general_block_settings' ), 10, 1 );
11
12 add_action( 'wp_ajax_af_gutenberg_get_form_data', array( $this, 'ajax_get_form_data' ), 10, 0 );
13 }
14
15 function register_block() {
16 acf_register_block_type(array(
17 'name' => 'advanced-form',
18 'title' => __( 'Advanced Form', 'advanced-forms' ),
19 'description' => __( 'Include a form from Advanced Forms' ),
20 'render_callback' => array( $this, 'render' ),
21 'enqueue_assets' => 'af_enqueue',
22 'category' => 'formatting',
23 'icon' => 'list-view',
24 ));
25 }
26
27 function render( $block, $content, $is_preview ) {
28 $form_key = get_field( 'af_block_form' );
29 if ( ! $form_key ) {
30 ?>
31 <div class="af-block-preview">
32 <div class="af-block-no-form"><?php _e( 'No form selected', 'advanced-forms' ); ?></div>
33 </div>
34 <?php
35 return;
36 }
37
38 $form = af_get_form( $form_key );
39
40 // The form can't be previewed in the Gutenberg editor as ACF will try to validate and save the
41 // fields in the form as if they were fields on the post. Instead we show a placeholder.
42 if ( $is_preview ) {
43 ?>
44 <div class="af-block-preview">
45 <div class="af-block-description"><?php _e( 'Form', 'advanced-forms' ); ?></div>
46 <div class="af-block-form-name"><?php echo $form['title']; ?></div>
47 <div class="af-block-subtitle"><?php _e( 'Preview page to view form', 'advanced-forms' ); ?></div>
48 </div>
49 <?php
50 return;
51 }
52
53 $args = array(
54 'ajax' => get_field( 'af_block_ajax' ),
55 );
56
57 if ( $submit_text = get_field( 'af_block_submit_text' ) ) {
58 $args['submit_text'] = $submit_text;
59 }
60
61 switch ( get_field( 'af_block_after_submission' ) ) {
62 case 'redirect_link':
63 $args['redirect'] = get_field( 'af_block_redirect_to_link' );
64 break;
65 case 'redirect_url':
66 $args['redirect'] = get_field( 'af_block_redirect_to_url' );
67 break;
68 }
69
70 if ( $excluded_fields = get_field( 'af_block_exclude_fields' ) ) {
71 $args['exclude_fields'] = $excluded_fields;
72 }
73
74 $args = apply_filters( 'af/form/gutenberg/args', $args, $form );
75
76 advanced_form( $form_key, $args );
77 }
78
79 function register_block_fields() {
80 $fields = apply_filters( 'af/form/gutenberg/fields', array() );
81
82 acf_add_local_field_group(array(
83 'key' => 'group_af_block',
84 'title' => 'Advanced Forms Gutenberg Block',
85 'fields' => $fields,
86 'location' => array(
87 array(
88 array(
89 'param' => 'block',
90 'operator' => '==',
91 'value' => 'acf/advanced-form',
92 ),
93 ),
94 ),
95 ));
96 }
97
98 function add_general_block_settings( $fields ) {
99 $fields[] = array(
100 'key' => 'field_af_block_tab_general',
101 'label' => __( 'General', 'advanced-form' ),
102 'name' => 'af_block_tab_general',
103 'type' => 'tab',
104 );
105
106 $fields[] = array(
107 'key' => 'field_af_block_form',
108 'label' => __( 'Form', 'advanced-form' ),
109 'name' => 'af_block_form',
110 'instructions' => sprintf(
111 '<a class="edit-form-link">%s &rarr;</span></a>',
112 __( 'Edit form settings', 'advanced-forms' )
113 ),
114 'type' => 'select',
115 'ui' => 1,
116 'return_format' => 'value',
117 );
118
119 $fields[] = array(
120 'key' => 'field_af_block_ajax',
121 'label' => __( 'Use AJAX submissions?', 'advanced-form' ),
122 'instructions' => 'Enables form submissions without a page reload',
123 'name' => 'af_block_ajax',
124 'type' => 'true_false',
125 'ui' => 1,
126 'wrapper' => array (
127 'width' => '50',
128 ),
129 );
130
131 $fields[] = array(
132 'key' => 'field_af_block_submit_text',
133 'label' => __( 'Submit button text', 'advanced-forms' ),
134 'name' => 'af_block_submit_text',
135 'type' => 'text',
136 'placeholder' => 'Submit',
137 'wrapper' => array (
138 'width' => '50',
139 ),
140 );
141
142 $fields[] = array(
143 'key' => 'field_af_block_after_submission',
144 'label' => __( 'After submission', 'advanced-forms' ),
145 'name' => 'af_block_after_submission',
146 'type' => 'radio',
147 'choices' => array(
148 'success_message' => 'Show success message',
149 'redirect_link' => 'Redirect to another page',
150 'redirect_url' => 'Redirect to a custom URL',
151 ),
152 'wrapper' => array(
153 'width' => '50',
154 ),
155 );
156
157 $fields[] = array(
158 'key' => 'field_af_block_success_message_info',
159 'label' => __( 'Success message', 'advanced-forms' ),
160 'name' => 'af_block_success_message_info',
161 'type' => 'message',
162 'message' => __( 'You can configure the success message in your <a class="edit-form-link">form settings</a>', 'advanced-forms' ),
163 'conditional_logic' => array(
164 array(
165 array(
166 'field' => 'field_af_block_after_submission',
167 'operator' => '==',
168 'value' => 'success_message',
169 ),
170 ),
171 ),
172 'wrapper' => array(
173 'width' => '50',
174 ),
175 );
176
177 $fields[] = array(
178 'key' => 'field_af_block_redirect_to_link',
179 'label' => __( 'Redirect to', 'advanced-forms' ),
180 'name' => 'af_block_redirect_to_link',
181 'type' => 'page_link',
182 'required' => true,
183 'conditional_logic' => array(
184 array(
185 array(
186 'field' => 'field_af_block_after_submission',
187 'operator' => '==',
188 'value' => 'redirect_link',
189 ),
190 ),
191 ),
192 'wrapper' => array(
193 'width' => '50',
194 ),
195 );
196
197 $fields[] = array(
198 'key' => 'field_af_block_redirect_to_url',
199 'label' => __( 'Redirect to', 'advanced-forms' ),
200 'name' => 'af_block_redirect_to_url',
201 'type' => 'url',
202 'required' => true,
203 'conditional_logic' => array(
204 array(
205 array(
206 'field' => 'field_af_block_after_submission',
207 'operator' => '==',
208 'value' => 'redirect_url',
209 ),
210 ),
211 ),
212 'wrapper' => array(
213 'width' => '50',
214 ),
215 );
216
217 $fields[] = array(
218 'key' => 'field_af_block_exclude_fields',
219 'label' => __( 'Exclude fields', 'advanced-forms' ),
220 'name' => 'af_block_exclude_fields',
221 'type' => 'select',
222 'required' => false,
223 'choices' => array(),
224 'multiple' => 1,
225 'ui' => 1,
226 'ajax' => 1,
227 'placeholder' => __( 'Select fields to exclude from the form', 'advanced-forms' ),
228 );
229
230 return $fields;
231 }
232
233 function populate_form_select_field( $field ) {
234 $choices = array();
235 foreach ( af_get_forms() as $form ) {
236 $field['choices'][ $form['key'] ] = $form['title'];
237 }
238
239 return $field;
240 }
241
242 function populate_exclude_fields_field( $field ) {
243 if ( ! isset( $_POST['form'] ) ) {
244 return $field;
245 }
246
247 $form_key = $_POST['form'];
248 $fields = af_get_form_fields( $form_key );
249
250 $choices = array();
251 foreach ( $fields as $field ) {
252 $choices[ $field['key'] ] = $field['label'];
253 }
254 $field['choices'] = $choices;
255
256 return $field;
257 }
258
259 function ajax_get_form_data() {
260 if ( ! isset( $_POST['form_key'] ) ) {
261 wp_send_json_error( array(
262 'error' => 'Missing form key',
263 ), 400 );
264 wp_die();
265 }
266
267 $form_key = $_POST['form_key'];
268 $form = af_get_form( $form_key );
269 $fields = af_get_form_fields( $form_key );
270
271 $edit_link = $form['post_id'] ? get_edit_post_link( $form['post_id'], '&' ) : NULL;
272
273 wp_send_json_success(array(
274 'form' => $form,
275 'fields' => $fields,
276 'edit_url' => $edit_link,
277 ));
278 wp_die();
279 }
280 }
281
282 new AF_Core_Gutenberg();