PluginProbe
Advanced Custom Fields (ACF®) / 6.0.0
Advanced Custom Fields (ACF®) v6.0.0
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / forms / form-widget.php
form-widget.php
333 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * ACF Widget Form Class
5 *
6 * All the logic for adding fields to widgets
7 *
8 * @class acf_form_widget
9 * @package ACF
10 * @subpackage Forms
11 */
12
13 if ( ! class_exists( 'acf_form_widget' ) ) :
14
15 class acf_form_widget {
16
17
18 /*
19 * __construct
20 *
21 * This function will setup the class functionality
22 *
23 * @type function
24 * @date 5/03/2014
25 * @since 5.0.0
26 *
27 * @param n/a
28 * @return n/a
29 */
30
31 function __construct() {
32
33 // vars
34 $this->preview_values = array();
35 $this->preview_reference = array();
36 $this->preview_errors = array();
37
38 // actions
39 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
40 add_action( 'in_widget_form', array( $this, 'edit_widget' ), 10, 3 );
41 add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 5 );
42
43 // filters
44 add_filter( 'widget_update_callback', array( $this, 'save_widget' ), 10, 4 );
45
46 }
47
48
49 /*
50 * admin_enqueue_scripts
51 *
52 * This action is run after post query but before any admin script / head actions.
53 * It is a good place to register all actions.
54 *
55 * @type action (admin_enqueue_scripts)
56 * @date 26/01/13
57 * @since 3.6.0
58 *
59 * @param N/A
60 * @return N/A
61 */
62
63 function admin_enqueue_scripts() {
64
65 // validate screen
66 if ( acf_is_screen( 'widgets' ) || acf_is_screen( 'customize' ) ) {
67
68 // valid
69
70 } else {
71
72 return;
73
74 }
75
76 // load acf scripts
77 acf_enqueue_scripts();
78
79 // actions
80 add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 1 );
81
82 }
83
84
85 /*
86 * acf_validate_save_post
87 *
88 * This function will loop over $_POST data and validate
89 *
90 * @type action 'acf/validate_save_post' 5
91 * @date 7/09/2016
92 * @since 5.4.0
93 *
94 * @param n/a
95 * @return n/a
96 */
97
98 function acf_validate_save_post() {
99
100 // bail early if not widget
101 if ( ! isset( $_POST['_acf_widget_id'] ) ) {
102 return;
103 }
104
105 // vars
106 $id = $_POST['_acf_widget_id'];
107 $number = $_POST['_acf_widget_number'];
108 $prefix = $_POST['_acf_widget_prefix'];
109
110 // validate
111 acf_validate_values( $_POST[ $id ][ $number ]['acf'], $prefix );
112
113 }
114
115
116 /*
117 * edit_widget
118 *
119 * This function will render the fields for a widget form
120 *
121 * @type function
122 * @date 11/06/2014
123 * @since 5.0.0
124 *
125 * @param $widget (object)
126 * @param $return (null)
127 * @param $instance (object)
128 * @return $post_id (int)
129 */
130
131 function edit_widget( $widget, $return, $instance ) {
132
133 // vars
134 $post_id = 0;
135 $prefix = 'widget-' . $widget->id_base . '[' . $widget->number . '][acf]';
136
137 // get id
138 if ( $widget->number !== '__i__' ) {
139
140 $post_id = "widget_{$widget->id}";
141
142 }
143
144 // get field groups
145 $field_groups = acf_get_field_groups(
146 array(
147 'widget' => $widget->id_base,
148 )
149 );
150
151 // render
152 if ( ! empty( $field_groups ) ) {
153
154 // render post data
155 acf_form_data(
156 array(
157 'screen' => 'widget',
158 'post_id' => $post_id,
159 'widget_id' => 'widget-' . $widget->id_base,
160 'widget_number' => $widget->number,
161 'widget_prefix' => $prefix,
162 )
163 );
164
165 // wrap
166 echo '<div class="acf-widget-fields acf-fields -clear">';
167
168 // loop
169 foreach ( $field_groups as $field_group ) {
170
171 // load fields
172 $fields = acf_get_fields( $field_group );
173
174 // bail if not fields
175 if ( empty( $fields ) ) {
176 continue;
177 }
178
179 // change prefix
180 acf_prefix_fields( $fields, $prefix );
181
182 // render
183 acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] );
184
185 }
186
187 // wrap
188 echo '</div>';
189
190 // jQuery selector looks odd, but is necessary due to WP adding an incremental number into the ID
191 // - not possible to find number via PHP parameters
192 if ( $widget->updated ) : ?>
193 <script type="text/javascript">
194 (function($) {
195
196 acf.doAction('append', $('[id^="widget"][id$="<?php echo $widget->id; ?>"]') );
197
198 })(jQuery);
199 </script>
200 <?php
201 endif;
202
203 }
204
205 }
206
207
208 /*
209 * save_widget
210 *
211 * This function will hook into the widget update filter and save ACF data
212 *
213 * @type function
214 * @date 27/05/2015
215 * @since 5.2.3
216 *
217 * @param $instance (array) widget settings
218 * @param $new_instance (array) widget settings
219 * @param $old_instance (array) widget settings
220 * @param $widget (object) widget info
221 * @return $instance
222 */
223
224 function save_widget( $instance, $new_instance, $old_instance, $widget ) {
225
226 // validate nonce if we're not a REST API request.
227 // the $_POST object is not available to us to validate if we're in a REST API call.
228 if ( ! ( function_exists( 'wp_is_json_request' ) && wp_is_json_request() ) ) {
229 if ( ! acf_verify_nonce( 'widget' ) ) {
230 return $instance;
231 }
232 }
233
234 // bail early if not valid (!customize + acf values + nonce).
235 if ( isset( $_POST['wp_customize'] ) || ! isset( $new_instance['acf'] ) ) {
236 return $instance;
237 }
238
239 // save
240 acf_save_post( "widget_{$widget->id}", $new_instance['acf'] );
241
242 // return
243 return $instance;
244
245 }
246
247
248 /*
249 * admin_footer
250 *
251 * This function will add some custom HTML to the footer of the edit page
252 *
253 * @type function
254 * @date 11/06/2014
255 * @since 5.0.0
256 *
257 * @param n/a
258 * @return n/a
259 */
260
261 function admin_footer() {
262 ?>
263 <script type="text/javascript">
264 (function($) {
265
266 // vars
267 acf.set('post_id', 'widgets');
268
269 // Only initialize visible fields.
270 acf.addFilter('find_fields', function( $fields ){
271
272 // not templates
273 $fields = $fields.not('#available-widgets .acf-field');
274
275 // not widget dragging in
276 $fields = $fields.not('.widget.ui-draggable-dragging .acf-field');
277
278 // return
279 return $fields;
280 });
281
282 // on publish
283 $('#widgets-right').on('click', '.widget-control-save', function( e ){
284
285 // vars
286 var $button = $(this);
287 var $form = $button.closest('form');
288
289 // validate
290 var valid = acf.validateForm({
291 form: $form,
292 event: e,
293 reset: true
294 });
295
296 // if not valid, stop event and allow validation to continue
297 if( !valid ) {
298 e.preventDefault();
299 e.stopImmediatePropagation();
300 }
301 });
302
303 // show
304 $('#widgets-right').on('click', '.widget-top', function(){
305 var $widget = $(this).parent();
306 if( $widget.hasClass('open') ) {
307 acf.doAction('hide', $widget);
308 } else {
309 acf.doAction('show', $widget);
310 }
311 });
312
313 $(document).on('widget-added', function( e, $widget ){
314
315 // - use delay to avoid rendering issues with customizer (ensures div is visible)
316 setTimeout(function(){
317 acf.doAction('append', $widget );
318 }, 100);
319 });
320
321 })(jQuery);
322 </script>
323 <?php
324
325 }
326 }
327
328 new acf_form_widget();
329
330 endif;
331
332 ?>
333