acf-form-functions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // Register store for form data.
  3. acf_register_store( 'form' );
  4. /**
  5. * acf_set_form_data
  6. *
  7. * Sets data about the current form.
  8. *
  9. * @date 6/10/13
  10. * @since 5.0.0
  11. *
  12. * @param string $name The store name.
  13. * @param array $data Array of data to start the store with.
  14. * @return ACF_Data
  15. */
  16. function acf_set_form_data( $name = '', $data = false ) {
  17. return acf_get_store( 'form' )->set( $name, $data );
  18. }
  19. /**
  20. * acf_get_form_data
  21. *
  22. * Gets data about the current form.
  23. *
  24. * @date 6/10/13
  25. * @since 5.0.0
  26. *
  27. * @param string $name The store name.
  28. * @return mixed
  29. */
  30. function acf_get_form_data( $name = '' ) {
  31. return acf_get_store( 'form' )->get( $name );
  32. }
  33. /**
  34. * acf_form_data
  35. *
  36. * Called within a form to set important information and render hidden inputs.
  37. *
  38. * @date 15/10/13
  39. * @since 5.0.0
  40. *
  41. * @param void
  42. * @return void
  43. */
  44. function acf_form_data( $data = array() ) {
  45. // Apply defaults.
  46. $data = wp_parse_args(
  47. $data,
  48. array(
  49. /** @type string The current screen (post, user, taxonomy, etc). */
  50. 'screen' => 'post',
  51. /** @type int|string The ID of current post being edited. */
  52. 'post_id' => 0,
  53. /** @type bool Enables AJAX validation. */
  54. 'validation' => true,
  55. )
  56. );
  57. // Create nonce using screen.
  58. $data['nonce'] = wp_create_nonce( $data['screen'] );
  59. // Append "changed" input used within "_wp_post_revision_fields" action.
  60. $data['changed'] = 0;
  61. // Set data.
  62. acf_set_form_data( $data );
  63. // Render HTML.
  64. ?>
  65. <div id="acf-form-data" class="acf-hidden">
  66. <?php
  67. // Create hidden inputs from $data
  68. foreach ( $data as $name => $value ) {
  69. acf_hidden_input(
  70. array(
  71. 'id' => '_acf_' . $name,
  72. 'name' => '_acf_' . $name,
  73. 'value' => $value,
  74. )
  75. );
  76. }
  77. /**
  78. * Fires within the #acf-form-data element to add extra HTML.
  79. *
  80. * @date 15/10/13
  81. * @since 5.0.0
  82. *
  83. * @param array $data The form data.
  84. */
  85. do_action( 'acf/form_data', $data );
  86. do_action( 'acf/input/form_data', $data );
  87. ?>
  88. </div>
  89. <?php
  90. }
  91. /**
  92. * acf_save_post
  93. *
  94. * Saves the $_POST data.
  95. *
  96. * @date 15/10/13
  97. * @since 5.0.0
  98. *
  99. * @param int|string $post_id The post id.
  100. * @param array $values An array of values to override $_POST.
  101. * @return bool True if save was successful.
  102. */
  103. function acf_save_post( $post_id = 0, $values = null ) {
  104. // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
  105. // Override $_POST data with $values.
  106. if ( $values !== null ) {
  107. $_POST['acf'] = $values;
  108. }
  109. // Bail early if no data to save.
  110. if ( empty( $_POST['acf'] ) ) {
  111. return false;
  112. }
  113. // Set form data (useful in various filters/actions).
  114. acf_set_form_data( 'post_id', $post_id );
  115. // Filter $_POST data for users without the 'unfiltered_html' capability.
  116. if ( ! acf_allow_unfiltered_html() ) {
  117. $_POST['acf'] = wp_kses_post_deep( $_POST['acf'] );
  118. }
  119. // phpcs:enable WordPress.Security.NonceVerification.Missing
  120. // Do generic action.
  121. do_action( 'acf/save_post', $post_id );
  122. // Return true.
  123. return true;
  124. }
  125. /**
  126. * _acf_do_save_post
  127. *
  128. * Private function hooked into 'acf/save_post' to actually save the $_POST data.
  129. * This allows developers to hook in before and after ACF has actually saved the data.
  130. *
  131. * @date 11/1/19
  132. * @since 5.7.10
  133. *
  134. * @param int|string $post_id The post id.
  135. * @return void
  136. */
  137. function _acf_do_save_post( $post_id = 0 ) {
  138. // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
  139. if ( ! empty( $_POST['acf'] ) ) {
  140. acf_update_values( $_POST['acf'], $post_id ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP when saved.
  141. }
  142. // phpcs:enable WordPress.Security.NonceVerification.Missing
  143. }
  144. // Run during generic action.
  145. add_action( 'acf/save_post', '_acf_do_save_post' );