説明なし

form-post.php 7.9KB

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