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