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