form-comment.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /*
  3. * ACF Comment Form Class
  4. *
  5. * All the logic for adding fields to comments
  6. *
  7. * @class acf_form_comment
  8. * @package ACF
  9. * @subpackage Forms
  10. */
  11. if ( ! class_exists( 'acf_form_comment' ) ) :
  12. class acf_form_comment {
  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( 'comment_form_field_comment', array( $this, 'comment_form_field_comment' ), 999, 1 );
  30. // add_action( 'comment_form_logged_in_after', array( $this, 'add_comment') );
  31. // add_action( 'comment_form', array( $this, 'add_comment') );
  32. // save
  33. add_action( 'edit_comment', array( $this, 'save_comment' ), 10, 1 );
  34. add_action( 'comment_post', array( $this, 'save_comment' ), 10, 1 );
  35. }
  36. /*
  37. * validate_page
  38. *
  39. * This function will check if the current page is for a post/page edit form
  40. *
  41. * @type function
  42. * @date 23/06/12
  43. * @since 3.1.8
  44. *
  45. * @param n/a
  46. * @return (boolean)
  47. */
  48. function validate_page() {
  49. // global
  50. global $pagenow;
  51. // validate page
  52. if ( $pagenow == 'comment.php' ) {
  53. return true;
  54. }
  55. // return
  56. return false;
  57. }
  58. /*
  59. * admin_enqueue_scripts
  60. *
  61. * This action is run after post query but before any admin script / head actions.
  62. * It is a good place to register all actions.
  63. *
  64. * @type action (admin_enqueue_scripts)
  65. * @date 26/01/13
  66. * @since 3.6.0
  67. *
  68. * @param n/a
  69. * @return n/a
  70. */
  71. function admin_enqueue_scripts() {
  72. // validate page
  73. if ( ! $this->validate_page() ) {
  74. return;
  75. }
  76. // load acf scripts
  77. acf_enqueue_scripts();
  78. // actions
  79. add_action( 'admin_footer', array( $this, 'admin_footer' ), 10, 1 );
  80. add_action( 'add_meta_boxes_comment', array( $this, 'edit_comment' ), 10, 1 );
  81. }
  82. /*
  83. * edit_comment
  84. *
  85. * This function is run on the admin comment.php page and will render the ACF fields within custom metaboxes to look native
  86. *
  87. * @type function
  88. * @date 19/10/13
  89. * @since 5.0.0
  90. *
  91. * @param $comment (object)
  92. * @return n/a
  93. */
  94. function edit_comment( $comment ) {
  95. // vars
  96. $post_id = "comment_{$comment->comment_ID}";
  97. // get field groups
  98. $field_groups = acf_get_field_groups(
  99. array(
  100. 'comment' => get_post_type( $comment->comment_post_ID ),
  101. )
  102. );
  103. // render
  104. if ( ! empty( $field_groups ) ) {
  105. // render post data
  106. acf_form_data(
  107. array(
  108. 'screen' => 'comment',
  109. 'post_id' => $post_id,
  110. )
  111. );
  112. foreach ( $field_groups as $field_group ) {
  113. // load fields
  114. $fields = acf_get_fields( $field_group );
  115. // vars
  116. $o = array(
  117. 'id' => 'acf-' . $field_group['ID'],
  118. 'key' => $field_group['key'],
  119. // 'style' => $field_group['style'],
  120. 'label' => $field_group['label_placement'],
  121. 'edit_url' => '',
  122. 'edit_title' => __( 'Edit field group', 'acf' ),
  123. // 'visibility' => $visibility
  124. );
  125. // edit_url
  126. if ( $field_group['ID'] && acf_current_user_can_admin() ) {
  127. $o['edit_url'] = admin_url( 'post.php?post=' . $field_group['ID'] . '&action=edit' );
  128. }
  129. ?>
  130. <div id="acf-<?php echo $field_group['ID']; ?>" class="stuffbox">
  131. <h3 class="hndle"><?php echo $field_group['title']; ?></h3>
  132. <div class="inside">
  133. <?php acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] ); ?>
  134. <script type="text/javascript">
  135. if( typeof acf !== 'undefined' ) {
  136. acf.newPostbox(<?php echo json_encode( $o ); ?>);
  137. }
  138. </script>
  139. </div>
  140. </div>
  141. <?php
  142. }
  143. }
  144. }
  145. /*
  146. * comment_form_field_comment
  147. *
  148. * description
  149. *
  150. * @type function
  151. * @date 18/04/2016
  152. * @since 5.3.8
  153. *
  154. * @param $post_id (int)
  155. * @return $post_id (int)
  156. */
  157. function comment_form_field_comment( $html ) {
  158. // global
  159. global $post;
  160. // vars
  161. $post_id = false;
  162. // get field groups
  163. $field_groups = acf_get_field_groups(
  164. array(
  165. 'comment' => $post->post_type,
  166. )
  167. );
  168. // bail early if no field groups
  169. if ( ! $field_groups ) {
  170. return $html;
  171. }
  172. // enqueue scripts
  173. acf_enqueue_scripts();
  174. // ob
  175. ob_start();
  176. // render post data
  177. acf_form_data(
  178. array(
  179. 'screen' => 'comment',
  180. 'post_id' => $post_id,
  181. )
  182. );
  183. echo '<div class="acf-comment-fields acf-fields -clear">';
  184. foreach ( $field_groups as $field_group ) {
  185. $fields = acf_get_fields( $field_group );
  186. acf_render_fields( $fields, $post_id, 'p', $field_group['instruction_placement'] );
  187. }
  188. echo '</div>';
  189. // append
  190. $html .= ob_get_contents();
  191. ob_end_clean();
  192. // return
  193. return $html;
  194. }
  195. /*
  196. * save_comment
  197. *
  198. * This function will save the comment data
  199. *
  200. * @type function
  201. * @date 19/10/13
  202. * @since 5.0.0
  203. *
  204. * @param comment_id (int)
  205. * @return n/a
  206. */
  207. function save_comment( $comment_id ) {
  208. // bail early if not valid nonce
  209. if ( ! acf_verify_nonce( 'comment' ) ) {
  210. return $comment_id;
  211. }
  212. // kses
  213. if ( isset( $_POST['acf'] ) ) {
  214. $_POST['acf'] = wp_kses_post_deep( $_POST['acf'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized with wp_kses_post_deep().
  215. }
  216. // validate and save
  217. if ( acf_validate_save_post( true ) ) {
  218. acf_save_post( "comment_{$comment_id}" );
  219. }
  220. }
  221. /*
  222. * admin_footer
  223. *
  224. * description
  225. *
  226. * @type function
  227. * @date 27/03/2015
  228. * @since 5.1.5
  229. *
  230. * @param $post_id (int)
  231. * @return $post_id (int)
  232. */
  233. function admin_footer() {
  234. ?>
  235. <script type="text/javascript">
  236. (function($) {
  237. // vars
  238. var $spinner = $('#publishing-action .spinner');
  239. // create spinner if not exists (may exist in future WP versions)
  240. if( !$spinner.exists() ) {
  241. // create spinner
  242. $spinner = $('<span class="spinner"></span>');
  243. // append
  244. $('#publishing-action').prepend( $spinner );
  245. }
  246. })(jQuery);
  247. </script>
  248. <?php
  249. }
  250. }
  251. new acf_form_comment();
  252. endif;
  253. ?>