123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'ACF_Form_Post' ) ) :
- class ACF_Form_Post {
-
- var $style = '';
-
- function __construct() {
-
- add_action( 'load-post.php', array( $this, 'initialize' ) );
- add_action( 'load-post-new.php', array( $this, 'initialize' ) );
-
- add_filter( 'wp_insert_post_empty_content', array( $this, 'wp_insert_post_empty_content' ), 10, 2 );
- add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
- }
-
- function initialize() {
-
- global $typenow;
- remove_meta_box( 'submitdiv', 'acf-field-group', 'side' );
-
- $restricted = array( 'acf-field-group', 'attachment' );
- if ( in_array( $typenow, $restricted ) ) {
- return;
- }
-
- acf_enqueue_scripts(
- array(
- 'uploader' => true,
- )
- );
-
- add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
- }
-
- function add_meta_boxes( $post_type, $post ) {
-
- $postboxes = array();
-
- $field_groups = acf_get_field_groups(
- array(
- 'post_id' => $post->ID,
- 'post_type' => $post_type,
- )
- );
-
- if ( $field_groups ) {
- foreach ( $field_groups as $field_group ) {
-
- $id = "acf-{$field_group['key']}";
- $title = $field_group['title'];
- $context = $field_group['position'];
- $priority = 'high';
-
- if ( $context == 'side' ) {
- $priority = 'core';
- }
-
- $priority = apply_filters( 'acf/input/meta_box_priority', $priority, $field_group );
-
- $postboxes[] = array(
- 'id' => $id,
- 'key' => $field_group['key'],
- 'style' => $field_group['style'],
- 'label' => $field_group['label_placement'],
- 'edit' => acf_get_field_group_edit_link( $field_group['ID'] ),
- );
-
- add_meta_box( $id, acf_esc_html( $title ), array( $this, 'render_meta_box' ), $post_type, $context, $priority, array( 'field_group' => $field_group ) );
- }
-
- $this->style = acf_get_field_group_style( $field_groups[0] );
-
- acf_localize_data(
- array(
- 'postboxes' => $postboxes,
- )
- );
- }
-
- if ( acf_get_setting( 'remove_wp_meta_box' ) ) {
- remove_meta_box( 'postcustom', false, 'normal' );
- }
-
- add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ) );
-
- do_action( 'acf/add_meta_boxes', $post_type, $post, $field_groups );
- }
-
- function edit_form_after_title() {
-
- global $post, $wp_meta_boxes;
-
- acf_form_data(
- array(
- 'screen' => 'post',
- 'post_id' => $post->ID,
- )
- );
-
- do_meta_boxes( get_current_screen(), 'acf_after_title', $post );
-
- echo '<style type="text/css" id="acf-style">' . $this->style . '</style>';
- }
-
- function render_meta_box( $post, $metabox ) {
-
- $id = $metabox['id'];
- $field_group = $metabox['args']['field_group'];
-
- $fields = acf_get_fields( $field_group );
- acf_render_fields( $fields, $post->ID, 'div', $field_group['instruction_placement'] );
- }
-
- function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
-
- if ( $maybe_empty && acf_maybe_get_POST( '_acf_changed' ) ) {
- return false;
- }
-
- return $maybe_empty;
- }
-
- function allow_save_post( $post ) {
-
- $allow = true;
-
- $restrict = array( 'auto-draft', 'revision', 'acf-field', 'acf-field-group' );
- if ( in_array( $post->post_type, $restrict ) ) {
- $allow = false;
- }
-
- $form_post_id = (int) acf_maybe_get_POST( 'post_ID' );
- if ( $form_post_id && $form_post_id !== $post->ID ) {
- $allow = false;
- }
-
- if ( $post->post_type == 'revision' ) {
-
- if ( acf_maybe_get_POST( 'wp-preview' ) == 'dopreview' && $form_post_id === $post->post_parent ) {
- $allow = true;
- }
- }
-
- return $allow;
- }
-
- function save_post( $post_id, $post ) {
-
- if ( ! $this->allow_save_post( $post ) ) {
- return $post_id;
- }
-
- if ( ! acf_verify_nonce( 'post' ) ) {
- return $post_id;
- }
-
- if ( $post->post_status == 'publish' ) {
-
- if ( ! acf_validate_save_post() ) {
- return;
- }
- }
-
- acf_save_post( $post_id );
-
- if ( post_type_supports( $post->post_type, 'revisions' ) ) {
- acf_save_post_revision( $post_id );
- }
-
- return $post_id;
- }
- }
- acf_new_instance( 'ACF_Form_Post' );
- endif;
|