PluginProbe
Advanced Custom Fields (ACF®) / 6.2.8
Advanced Custom Fields (ACF®) v6.2.8
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-post.php

form-post.php in Advanced Custom Fields (ACF®) 6.2.8, at includes/forms/form-post.php

328 lines 8.4 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' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'ACF_Form_Post' ) ) :
8
9 class ACF_Form_Post {
10
11 /** @var string The first field groups style CSS. */
12 var $style = '';
13
14 /**
15 * __construct
16 *
17 * Sets up the class functionality.
18 *
19 * @date 5/03/2014
20 * @since 5.0.0
21 *
22 * @param void
23 * @return void
24 */
25 function __construct() {
26
27 // initialize on post edit screens
28 add_action( 'load-post.php', array( $this, 'initialize' ) );
29 add_action( 'load-post-new.php', array( $this, 'initialize' ) );
30
31 // save
32 add_filter( 'wp_insert_post_empty_content', array( $this, 'wp_insert_post_empty_content' ), 10, 2 );
33 add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
34 }
35
36
37 /**
38 * initialize
39 *
40 * Sets up Form functionality.
41 *
42 * @date 19/9/18
43 * @since 5.7.6
44 *
45 * @param void
46 * @return void
47 */
48 function initialize() {
49
50 // globals
51 global $typenow;
52
53 $acf_post_types = acf_get_internal_post_types();
54
55 foreach ( $acf_post_types as $post_type ) {
56 remove_meta_box( 'submitdiv', $post_type, 'side' );
57 }
58
59 // restrict specific post types
60 $restricted = array_merge( $acf_post_types, array( 'acf-taxonomy', 'attachment' ) );
61 if ( in_array( $typenow, $restricted ) ) {
62 return;
63 }
64
65 // enqueue scripts
66 acf_enqueue_scripts(
67 array(
68 'uploader' => true,
69 )
70 );
71
72 // actions
73 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
74 }
75
76 /**
77 * add_meta_boxes
78 *
79 * Adds ACF metaboxes for the given $post_type and $post.
80 *
81 * @date 19/9/18
82 * @since 5.7.6
83 *
84 * @param string $post_type The post type.
85 * @param WP_Post $post The post being edited.
86 * @return void
87 */
88 function add_meta_boxes( $post_type, $post ) {
89
90 // Storage for localized postboxes.
91 $postboxes = array();
92
93 // Get field groups for this screen.
94 $field_groups = acf_get_field_groups(
95 array(
96 'post_id' => $post->ID,
97 'post_type' => $post_type,
98 )
99 );
100
101 // Loop over field groups.
102 if ( $field_groups ) {
103 foreach ( $field_groups as $field_group ) {
104
105 // vars
106 $id = "acf-{$field_group['key']}"; // acf-group_123
107 $title = $field_group['title']; // Group 1
108 $context = $field_group['position']; // normal, side, acf_after_title
109 $priority = 'high'; // high, core, default, low
110
111 // Reduce priority for sidebar metaboxes for best position.
112 if ( $context == 'side' ) {
113 $priority = 'core';
114 }
115
116 /**
117 * Filters the metabox priority.
118 *
119 * @date 23/06/12
120 * @since 3.1.8
121 *
122 * @param string $priority The metabox priority (high, core, default, low).
123 * @param array $field_group The field group array.
124 */
125 $priority = apply_filters( 'acf/input/meta_box_priority', $priority, $field_group );
126
127 // Localize data
128 $postboxes[] = array(
129 'id' => $id,
130 'key' => $field_group['key'],
131 'style' => $field_group['style'],
132 'label' => $field_group['label_placement'],
133 'edit' => acf_get_field_group_edit_link( $field_group['ID'] ),
134 );
135
136 // Add the meta box.
137 add_meta_box( $id, acf_esc_html( $title ), array( $this, 'render_meta_box' ), $post_type, $context, $priority, array( 'field_group' => $field_group ) );
138 }
139
140 // Set style from first field group.
141 $this->style = acf_get_field_group_style( $field_groups[0] );
142
143 // Localize postboxes.
144 acf_localize_data(
145 array(
146 'postboxes' => $postboxes,
147 )
148 );
149 }
150
151 // remove postcustom metabox (removes expensive SQL query)
152 if ( acf_get_setting( 'remove_wp_meta_box' ) ) {
153 remove_meta_box( 'postcustom', false, 'normal' );
154 }
155
156 // Add hidden input fields.
157 add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ) );
158
159 /**
160 * Fires after metaboxes have been added.
161 *
162 * @date 13/12/18
163 * @since 5.8.0
164 *
165 * @param string $post_type The post type.
166 * @param WP_Post $post The post being edited.
167 * @param array $field_groups The field groups added.
168 */
169 do_action( 'acf/add_meta_boxes', $post_type, $post, $field_groups );
170 }
171
172 /**
173 * Called after the title and before the content editor to render the after title metaboxes.
174 * Also renders the CSS required to hide the "hide-on-screen" elements on the page based on the field group settings.
175 *
176 * @since 5.7.6
177 */
178 public function edit_form_after_title() {
179
180 // globals
181 global $post, $wp_meta_boxes;
182
183 // render post data
184 acf_form_data(
185 array(
186 'screen' => 'post',
187 'post_id' => $post->ID,
188 )
189 );
190
191 // render 'acf_after_title' metaboxes
192 do_meta_boxes( get_current_screen(), 'acf_after_title', $post );
193
194 if ( ! empty( $this->style ) ) {
195 // render dynamic field group style, using wp_strip_all_tags as this is filterable, but should only contain valid styles and no html.
196 echo '<style type="text/css" id="acf-style">' . wp_strip_all_tags( $this->style ) . '</style>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS only, escaped by wp_strip_all_tags.
197 }
198 }
199
200 /**
201 * render_meta_box
202 *
203 * Renders the ACF metabox HTML.
204 *
205 * @date 19/9/18
206 * @since 5.7.6
207 *
208 * @param WP_Post $post The post being edited.
209 * @param array metabox The add_meta_box() args.
210 * @return void
211 */
212 function render_meta_box( $post, $metabox ) {
213
214 // vars
215 $id = $metabox['id'];
216 $field_group = $metabox['args']['field_group'];
217
218 // Render fields.
219 $fields = acf_get_fields( $field_group );
220 acf_render_fields( $fields, $post->ID, 'div', $field_group['instruction_placement'] );
221 }
222
223 /**
224 * wp_insert_post_empty_content
225 *
226 * Allows WP to insert a new post without title or post_content if ACF data exists.
227 *
228 * @date 16/07/2014
229 * @since 5.0.1
230 *
231 * @param boolean $maybe_empty Whether the post should be considered "empty".
232 * @param array $postarr Array of post data.
233 * @return boolean
234 */
235 function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
236
237 // return false and allow insert if '_acf_changed' exists
238 if ( $maybe_empty && acf_maybe_get_POST( '_acf_changed' ) ) {
239 return false;
240 }
241
242 // return
243 return $maybe_empty;
244 }
245
246 /**
247 * Checks if the $post is allowed to be saved.
248 * Used to avoid triggering "acf/save_post" on dynamically created posts during save.
249 *
250 * @type function
251 * @date 26/06/2016
252 * @since 5.3.8
253 *
254 * @param WP_Post $post The post to check.
255 * @return boolean
256 */
257 function allow_save_post( $post ) {
258
259 // vars
260 $allow = true;
261
262 // restrict post types
263 $restrict = array( 'auto-draft', 'revision', 'acf-field', 'acf-field-group' );
264 if ( in_array( $post->post_type, $restrict ) ) {
265 $allow = false;
266 }
267
268 // disallow if the $_POST ID value does not match the $post->ID
269 $form_post_id = (int) acf_maybe_get_POST( 'post_ID' );
270 if ( $form_post_id && $form_post_id !== $post->ID ) {
271 $allow = false;
272 }
273
274 // revision (preview)
275 if ( $post->post_type == 'revision' ) {
276
277 // allow if doing preview and this $post is a child of the $_POST ID
278 if ( acf_maybe_get_POST( 'wp-preview' ) == 'dopreview' && $form_post_id === $post->post_parent ) {
279 $allow = true;
280 }
281 }
282
283 // return
284 return $allow;
285 }
286
287 /**
288 * Triggers during the 'save_post' action to save the $_POST data.
289 *
290 * @since 1.0.0
291 *
292 * @param integer $post_id The post ID.
293 * @param WP_Post $post The post object.
294 * @return integer
295 */
296 public function save_post( $post_id, $post ) {
297 // Bail early if not allowed to save this post type.
298 if ( ! $this->allow_save_post( $post ) ) {
299 return $post_id;
300 }
301
302 // Verify nonce.
303 if ( ! acf_verify_nonce( 'post' ) ) {
304 return $post_id;
305 }
306
307 // Validate for published post (allow draft to save without validation).
308 if ( $post->post_status === 'publish' ) {
309 // Bail early if validation fails.
310 if ( ! acf_validate_save_post() ) {
311 return;
312 }
313 }
314
315 acf_save_post( $post_id );
316
317 // We handle revisions differently on WP 6.4+.
318 if ( version_compare( get_bloginfo( 'version' ), '6.4', '<' ) && post_type_supports( $post->post_type, 'revisions' ) ) {
319 acf_save_post_revision( $post_id );
320 }
321
322 return $post_id;
323 }
324 }
325
326 acf_new_instance( 'ACF_Form_Post' );
327 endif;
328