PluginProbe
Advanced Custom Fields (ACF®) / 5.7.9
Advanced Custom Fields (ACF®) v5.7.9
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 / form.php
form.php
281 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('ACF_Form') ) :
6
7 class ACF_Form {
8
9 /** @var array Storage for data */
10 var $data = array();
11
12
13 /*
14 * __construct
15 *
16 * This function will setup the class functionality.
17 *
18 * @type function
19 * @date 5/03/2014
20 * @since 5.0.0
21 *
22 * @param void
23 * @return void
24 */
25
26 function __construct() {
27
28 // actions
29 add_action('acf/save_post', array($this, '_save_post'), 10, 1);
30 }
31
32
33 /*
34 * set_data
35 *
36 * Sets data.
37 *
38 * @type function
39 * @date 4/03/2016
40 * @since 5.3.2
41 *
42 * @param array $data An array of data.
43 * @return array
44 */
45
46 function set_data( $data = array() ) {
47
48 // defaults
49 $data = wp_parse_args($data, array(
50 'screen' => 'post', // Current screen loaded (post, user, taxonomy, etc)
51 'post_id' => 0, // ID of current post being edited
52 'nonce' => '', // nonce used for $_POST validation (defaults to screen)
53 'validation' => 1, // enables form validation
54 'changed' => 0, // used by revisions and unload to detect change
55 ));
56
57 // crete nonce
58 $data['nonce'] = wp_create_nonce($data['screen']);
59
60 // update
61 $this->data = $data;
62
63 // return
64 return $data;
65 }
66
67
68 /*
69 * get_data
70 *
71 * Returns data.
72 *
73 * @type function
74 * @date 4/03/2016
75 * @since 5.3.2
76 *
77 * @param string $name The data anme.
78 * @return mixed The data.
79 */
80
81 function get_data( $name = false ) {
82 return isset($this->data[ $name ]) ? $this->data[ $name ] : null;
83 }
84
85 /**
86 * render_data
87 *
88 * Renders the <div id="acf-form-data"> element with hidden "form data" inputs
89 *
90 * @date 17/4/18
91 * @since 5.6.9
92 *
93 * @param array $data An array of data.
94 * @return void
95 */
96
97 function render_data( $data = array() ) {
98
99 // set form data
100 $data = $this->set_data( $data );
101
102 ?>
103 <div id="acf-form-data" class="acf-hidden">
104 <?php
105
106 // loop
107 foreach( $data as $name => $value ) {
108
109 // input
110 acf_hidden_input(array(
111 'id' => '_acf_' . $name,
112 'name' => '_acf_' . $name,
113 'value' => $value
114 ));
115 }
116
117 // actions
118 do_action('acf/form_data', $data);
119 do_action('acf/input/form_data', $data);
120
121 ?>
122 </div>
123 <?php
124 }
125
126
127 /**
128 * save_post
129 *
130 * Calls the 'acf/save_post' action allowing $_POST data to be saved
131 *
132 * @date 17/4/18
133 * @since 5.6.9
134 *
135 * @param mixed $post_id The $post_id used to save data to the DB
136 * @param array $values Optional. An optional array of data to be saved (modifies $_POST['acf'])
137 * @return boolean Returns true on success.
138 */
139
140 function save_post( $post_id = 0, $values = null ) {
141
142 // override $_POST
143 if( $values !== null ) {
144 $_POST['acf'] = $values;
145 }
146
147 // bail early if no values
148 if( empty($_POST['acf']) ) {
149 return false;
150 }
151
152 // set form data
153 $this->set_data(array(
154 'post_id' => $post_id
155 ));
156
157 // Filter $_POST data for users without the 'unfiltered_html' capability.
158 if( !current_user_can('unfiltered_html') ) {
159 $_POST['acf'] = wp_kses_post_deep( $_POST['acf'] );
160 }
161
162 // action
163 do_action('acf/save_post', $post_id);
164
165 // return
166 return true;
167 }
168
169
170 /**
171 * _save_post
172 *
173 * Saves the actual $_POST['acf'] data.
174 * Performing this logic within an action allows developers to hook in before and after data is saved.
175 *
176 * @date 24/10/2014
177 * @since 5.0.9
178 *
179 * @param mixed $post_id The $post_id used to save data to the DB
180 * @return void.
181 */
182
183 function _save_post( $post_id ) {
184
185 // bail early if empty
186 // - post data may have be modified
187 if( empty($_POST['acf']) ) {
188 return;
189 }
190
191 // loop
192 foreach( $_POST['acf'] as $key => $value ) {
193
194 // get field
195 $field = acf_get_field( $key );
196
197 // update
198 if( $field ) {
199 acf_update_value( $value, $post_id, $field );
200 }
201 }
202 }
203 }
204
205 // instantiate
206 acf_new_instance('ACF_Form');
207
208 endif; // class_exists check
209
210
211 /*
212 * acf_get_form_data
213 *
214 * alias of acf()->form->get_data()
215 *
216 * @type function
217 * @date 6/10/13
218 * @since 5.0.0
219 *
220 * @param n/a
221 * @return n/a
222 */
223
224 function acf_get_form_data( $name = '' ) {
225 return acf_get_instance('ACF_Form')->get_data( $name );
226 }
227
228
229 /*
230 * acf_set_form_data
231 *
232 * alias of acf()->form->set_data()
233 *
234 * @type function
235 * @date 6/10/13
236 * @since 5.0.0
237 *
238 * @param n/a
239 * @return n/a
240 */
241
242 function acf_set_form_data( $data = array() ) {
243 return acf_get_instance('ACF_Form')->set_data( $data );
244 }
245
246
247 /*
248 * acf_form_data
249 *
250 * description
251 *
252 * @type function
253 * @date 15/10/13
254 * @since 5.0.0
255 *
256 * @param $post_id (int)
257 * @return $post_id (int)
258 */
259
260 function acf_form_data( $data = array() ) {
261 return acf_get_instance('ACF_Form')->render_data( $data );
262 }
263
264 /*
265 * acf_save_post
266 *
267 * description
268 *
269 * @type function
270 * @date 15/10/13
271 * @since 5.0.0
272 *
273 * @param $post_id (int)
274 * @return $post_id (int)
275 */
276
277 function acf_save_post( $post_id = 0, $values = null ) {
278 return acf_get_instance('ACF_Form')->save_post( $post_id, $values );
279 }
280
281 ?>