PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 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-post.php
secure-custom-fields / includes / forms Last commit date
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-post.php
331 lines
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 $style = '';
195 if ( is_string( $this->style ) ) {
196 $style = $this->style;
197 }
198
199 // Render dynamic field group style, using wp_strip_all_tags as this is filterable, but should only contain valid styles and no html.
200 echo '<style type="text/css" id="acf-style">' . wp_strip_all_tags( $style ) . '</style>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS only, escaped by wp_strip_all_tags.
201 }
202
203 /**
204 * render_meta_box
205 *
206 * Renders the ACF metabox HTML.
207 *
208 * @date 19/9/18
209 * @since 5.7.6
210 *
211 * @param WP_Post $post The post being edited.
212 * @param array metabox The add_meta_box() args.
213 * @return void
214 */
215 function render_meta_box( $post, $metabox ) {
216
217 // vars
218 $id = $metabox['id'];
219 $field_group = $metabox['args']['field_group'];
220
221 // Render fields.
222 $fields = acf_get_fields( $field_group );
223 acf_render_fields( $fields, $post->ID, 'div', $field_group['instruction_placement'] );
224 }
225
226 /**
227 * wp_insert_post_empty_content
228 *
229 * Allows WP to insert a new post without title or post_content if ACF data exists.
230 *
231 * @date 16/07/2014
232 * @since 5.0.1
233 *
234 * @param boolean $maybe_empty Whether the post should be considered "empty".
235 * @param array $postarr Array of post data.
236 * @return boolean
237 */
238 function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
239
240 // return false and allow insert if '_acf_changed' exists
241 if ( $maybe_empty && acf_maybe_get_POST( '_acf_changed' ) ) {
242 return false;
243 }
244
245 // return
246 return $maybe_empty;
247 }
248
249 /**
250 * Checks if the $post is allowed to be saved.
251 * Used to avoid triggering "acf/save_post" on dynamically created posts during save.
252 *
253 * @type function
254 * @date 26/06/2016
255 * @since 5.3.8
256 *
257 * @param WP_Post $post The post to check.
258 * @return boolean
259 */
260 function allow_save_post( $post ) {
261
262 // vars
263 $allow = true;
264
265 // restrict post types
266 $restrict = array( 'auto-draft', 'revision', 'acf-field', 'acf-field-group' );
267 if ( in_array( $post->post_type, $restrict ) ) {
268 $allow = false;
269 }
270
271 // disallow if the $_POST ID value does not match the $post->ID
272 $form_post_id = (int) acf_maybe_get_POST( 'post_ID' );
273 if ( $form_post_id && $form_post_id !== $post->ID ) {
274 $allow = false;
275 }
276
277 // revision (preview)
278 if ( $post->post_type == 'revision' ) {
279
280 // allow if doing preview and this $post is a child of the $_POST ID
281 if ( acf_maybe_get_POST( 'wp-preview' ) == 'dopreview' && $form_post_id === $post->post_parent ) {
282 $allow = true;
283 }
284 }
285
286 // return
287 return $allow;
288 }
289
290 /**
291 * Triggers during the 'save_post' action to save the $_POST data.
292 *
293 * @since 1.0.0
294 *
295 * @param integer $post_id The post ID.
296 * @param WP_Post $post The post object.
297 * @return integer
298 */
299 public function save_post( $post_id, $post ) {
300 // Bail early if not allowed to save this post type.
301 if ( ! $this->allow_save_post( $post ) ) {
302 return $post_id;
303 }
304
305 // Verify nonce.
306 if ( ! acf_verify_nonce( 'post' ) ) {
307 return $post_id;
308 }
309
310 // Validate for published post (allow draft to save without validation).
311 if ( $post->post_status === 'publish' ) {
312 // Bail early if validation fails.
313 if ( ! acf_validate_save_post() ) {
314 return;
315 }
316 }
317
318 acf_save_post( $post_id );
319
320 // We handle revisions differently on WP 6.4+.
321 if ( version_compare( get_bloginfo( 'version' ), '6.4', '<' ) && post_type_supports( $post->post_type, 'revisions' ) ) {
322 acf_save_post_revision( $post_id );
323 }
324
325 return $post_id;
326 }
327 }
328
329 acf_new_instance( 'ACF_Form_Post' );
330 endif;
331