PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/advanced-custom-fields-pro/forms/attachment.php

https://gitlab.com/sihabudinahmad/asppi
PHP | 303 lines | 95 code | 100 blank | 108 comment | 11 complexity | aaf2968e2754dd9f00b7c45140f34715 MD5 | raw file
  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. * validate_page
  35. *
  36. * This function will check if the current page is for a post/page edit form
  37. *
  38. * @type function
  39. * @date 23/06/12
  40. * @since 3.1.8
  41. *
  42. * @param n/a
  43. * @return (boolean)
  44. */
  45. function validate_page() {
  46. // global
  47. global $pagenow, $typenow, $wp_version;
  48. // validate page
  49. if( $pagenow === 'post.php' && $typenow === 'attachment' ) {
  50. return true;
  51. }
  52. // validate page
  53. if( $pagenow === 'upload.php' && version_compare($wp_version, '4.0', '>=') ) {
  54. add_action('admin_footer', array($this, 'admin_footer'), 0);
  55. return true;
  56. }
  57. // return
  58. return false;
  59. }
  60. /*
  61. * admin_enqueue_scripts
  62. *
  63. * This action is run after post query but before any admin script / head actions.
  64. * It is a good place to register all actions.
  65. *
  66. * @type action (admin_enqueue_scripts)
  67. * @date 26/01/13
  68. * @since 3.6.0
  69. *
  70. * @param N/A
  71. * @return N/A
  72. */
  73. function admin_enqueue_scripts() {
  74. // bail early if not valid page
  75. if( !$this->validate_page() ) {
  76. return;
  77. }
  78. // load acf scripts
  79. acf_enqueue_scripts();
  80. }
  81. /*
  82. * admin_footer
  83. *
  84. * This function will add acf_form_data to the WP 4.0 attachment grid
  85. *
  86. * @type action (admin_footer)
  87. * @date 11/09/2014
  88. * @since 5.0.0
  89. *
  90. * @param n/a
  91. * @return n/a
  92. */
  93. function admin_footer() {
  94. // render post data
  95. acf_form_data(array(
  96. 'post_id' => 0,
  97. 'nonce' => 'attachment',
  98. 'ajax' => 1
  99. ));
  100. }
  101. /*
  102. * edit_attachment
  103. *
  104. * description
  105. *
  106. * @type function
  107. * @date 8/10/13
  108. * @since 5.0.0
  109. *
  110. * @param $post_id (int)
  111. * @return $post_id (int)
  112. */
  113. function edit_attachment( $form_fields, $post ) {
  114. // vars
  115. $el = 'tr';
  116. $post_id = $post->ID;
  117. $args = array(
  118. 'attachment' => 'All'
  119. );
  120. // $el
  121. if( $this->validate_page() ) {
  122. //$el = 'div';
  123. }
  124. // get field groups
  125. $field_groups = acf_get_field_groups( $args );
  126. // render
  127. if( !empty($field_groups) ) {
  128. // get acf_form_data
  129. ob_start();
  130. acf_form_data(array(
  131. 'post_id' => $post_id,
  132. 'nonce' => 'attachment',
  133. ));
  134. if( $this->validate_page() ) {
  135. echo '<style type="text/css">
  136. .compat-attachment-fields {
  137. width: 100%;
  138. }
  139. tr.acf-field {
  140. display: block;
  141. margin: 0 0 13px;
  142. }
  143. tr.acf-field td.acf-label {
  144. display: block;
  145. margin: 0;
  146. }
  147. tr.acf-field td.acf-input {
  148. display: block;
  149. margin: 0;
  150. }
  151. </style>';
  152. }
  153. // $el
  154. //if( $el == 'tr' ) {
  155. echo '</td></tr>';
  156. //}
  157. foreach( $field_groups as $field_group ) {
  158. $fields = acf_get_fields( $field_group );
  159. acf_render_fields( $post_id, $fields, $el, 'field' );
  160. }
  161. // $el
  162. //if( $el == 'tr' ) {
  163. echo '<tr class="compat-field-acf-blank"><td>';
  164. //}
  165. $html = ob_get_contents();
  166. ob_end_clean();
  167. $form_fields[ 'acf-form-data' ] = array(
  168. 'label' => '',
  169. 'input' => 'html',
  170. 'html' => $html
  171. );
  172. }
  173. // return
  174. return $form_fields;
  175. }
  176. /*
  177. * save_attachment
  178. *
  179. * description
  180. *
  181. * @type function
  182. * @date 8/10/13
  183. * @since 5.0.0
  184. *
  185. * @param $post_id (int)
  186. * @return $post_id (int)
  187. */
  188. function save_attachment( $post, $attachment ) {
  189. // bail early if not valid nonce
  190. if( ! acf_verify_nonce('attachment') ) {
  191. return $post;
  192. }
  193. // validate and save
  194. if( acf_validate_save_post(true) ) {
  195. acf_save_post( $post['ID'] );
  196. }
  197. // return
  198. return $post;
  199. }
  200. }
  201. new acf_form_attachment();
  202. endif;
  203. ?>