form-attachment.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * ACF Attachment Form Class
  4. *
  5. * All the logic for adding fields to attachments
  6. *
  7. * @class acf_form_attachment
  8. * @package ACF
  9. * @subpackage Forms
  10. */
  11. if ( ! class_exists( 'acf_form_attachment' ) ) :
  12. class acf_form_attachment {
  13. /*
  14. * __construct
  15. *
  16. * This function will setup the class functionality
  17. *
  18. * @type function
  19. * @date 5/03/2014
  20. * @since 5.0.0
  21. *
  22. * @param n/a
  23. * @return n/a
  24. */
  25. function __construct() {
  26. // actions
  27. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  28. // render
  29. add_filter( 'attachment_fields_to_edit', array( $this, 'edit_attachment' ), 10, 2 );
  30. // save
  31. add_filter( 'attachment_fields_to_save', array( $this, 'save_attachment' ), 10, 2 );
  32. }
  33. /*
  34. * admin_enqueue_scripts
  35. *
  36. * This action is run after post query but before any admin script / head actions.
  37. * It is a good place to register all actions.
  38. *
  39. * @type action (admin_enqueue_scripts)
  40. * @date 26/01/13
  41. * @since 3.6.0
  42. *
  43. * @param N/A
  44. * @return N/A
  45. */
  46. function admin_enqueue_scripts() {
  47. // bail early if not valid screen
  48. if ( ! acf_is_screen( array( 'attachment', 'upload' ) ) ) {
  49. return;
  50. }
  51. // load acf scripts
  52. acf_enqueue_scripts(
  53. array(
  54. 'uploader' => true,
  55. )
  56. );
  57. // actions
  58. if ( acf_is_screen( 'upload' ) ) {
  59. add_action( 'admin_footer', array( $this, 'admin_footer' ), 0 );
  60. }
  61. }
  62. /*
  63. * admin_footer
  64. *
  65. * This function will add acf_form_data to the WP 4.0 attachment grid
  66. *
  67. * @type action (admin_footer)
  68. * @date 11/09/2014
  69. * @since 5.0.0
  70. *
  71. * @param n/a
  72. * @return n/a
  73. */
  74. function admin_footer() {
  75. // render post data
  76. acf_form_data(
  77. array(
  78. 'screen' => 'attachment',
  79. 'post_id' => 0,
  80. )
  81. );
  82. ?>
  83. <script type="text/javascript">
  84. // WP saves attachment on any input change, so unload is not needed
  85. acf.unload.active = 0;
  86. </script>
  87. <?php
  88. }
  89. /*
  90. * edit_attachment
  91. *
  92. * description
  93. *
  94. * @type function
  95. * @date 8/10/13
  96. * @since 5.0.0
  97. *
  98. * @param $post_id (int)
  99. * @return $post_id (int)
  100. */
  101. function edit_attachment( $form_fields, $post ) {
  102. // vars
  103. $is_page = acf_is_screen( 'attachment' );
  104. $post_id = $post->ID;
  105. $el = 'tr';
  106. // get field groups
  107. $field_groups = acf_get_field_groups(
  108. array(
  109. 'attachment_id' => $post_id,
  110. 'attachment' => $post_id, // Leave for backwards compatibility
  111. )
  112. );
  113. // render
  114. if ( ! empty( $field_groups ) ) {
  115. // get acf_form_data
  116. ob_start();
  117. acf_form_data(
  118. array(
  119. 'screen' => 'attachment',
  120. 'post_id' => $post_id,
  121. )
  122. );
  123. // open
  124. echo '</td></tr>';
  125. // loop
  126. foreach ( $field_groups as $field_group ) {
  127. // load fields
  128. $fields = acf_get_fields( $field_group );
  129. // override instruction placement for modal
  130. if ( ! $is_page ) {
  131. $field_group['instruction_placement'] = 'field';
  132. }
  133. // render
  134. acf_render_fields( $fields, $post_id, $el, $field_group['instruction_placement'] );
  135. }
  136. // close
  137. echo '<tr class="compat-field-acf-blank"><td>';
  138. $html = ob_get_contents();
  139. ob_end_clean();
  140. $form_fields['acf-form-data'] = array(
  141. 'label' => '',
  142. 'input' => 'html',
  143. 'html' => $html,
  144. );
  145. }
  146. // return
  147. return $form_fields;
  148. }
  149. /*
  150. * save_attachment
  151. *
  152. * description
  153. *
  154. * @type function
  155. * @date 8/10/13
  156. * @since 5.0.0
  157. *
  158. * @param $post_id (int)
  159. * @return $post_id (int)
  160. */
  161. function save_attachment( $post, $attachment ) {
  162. // bail early if not valid nonce
  163. if ( ! acf_verify_nonce( 'attachment' ) ) {
  164. return $post;
  165. }
  166. // bypass validation for ajax
  167. if ( acf_is_ajax( 'save-attachment-compat' ) ) {
  168. acf_save_post( $post['ID'] );
  169. // validate and save
  170. } elseif ( acf_validate_save_post( true ) ) {
  171. acf_save_post( $post['ID'] );
  172. }
  173. // return
  174. return $post;
  175. }
  176. }
  177. new acf_form_attachment();
  178. endif;
  179. ?>