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

https://gitlab.com/Svyrydov/test-project · PHP · 344 lines · 107 code · 109 blank · 128 comment · 12 complexity · 8539a364bf63e8ec1a4b5690d8483b4b MD5 · raw file

  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(array(
  99. 'comment' => get_post_type( $comment->comment_post_ID )
  100. ));
  101. // render
  102. if( !empty($field_groups) ) {
  103. // render post data
  104. acf_form_data(array(
  105. 'post_id' => $post_id,
  106. 'nonce' => 'comment'
  107. ));
  108. foreach( $field_groups as $field_group ) {
  109. // load fields
  110. $fields = acf_get_fields( $field_group );
  111. // vars
  112. $o = array(
  113. 'id' => 'acf-'.$field_group['ID'],
  114. 'key' => $field_group['key'],
  115. //'style' => $field_group['style'],
  116. 'label' => $field_group['label_placement'],
  117. 'edit_url' => '',
  118. 'edit_title' => __('Edit field group', 'acf'),
  119. //'visibility' => $visibility
  120. );
  121. // edit_url
  122. if( $field_group['ID'] && acf_current_user_can_admin() ) {
  123. $o['edit_url'] = admin_url('post.php?post=' . $field_group['ID'] . '&action=edit');
  124. }
  125. ?>
  126. <div id="acf-<?php echo $field_group['ID']; ?>" class="stuffbox">
  127. <h3 class="hndle"><?php echo $field_group['title']; ?></h3>
  128. <div class="inside">
  129. <?php acf_render_fields( $post_id, $fields, 'div', $field_group['instruction_placement'] ); ?>
  130. <script type="text/javascript">
  131. if( typeof acf !== 'undefined' ) {
  132. acf.postbox.render(<?php echo json_encode($o); ?>);
  133. }
  134. </script>
  135. </div>
  136. </div>
  137. <?php
  138. }
  139. }
  140. }
  141. /*
  142. * comment_form_field_comment
  143. *
  144. * description
  145. *
  146. * @type function
  147. * @date 18/04/2016
  148. * @since 5.3.8
  149. *
  150. * @param $post_id (int)
  151. * @return $post_id (int)
  152. */
  153. function comment_form_field_comment( $html ) {
  154. // global
  155. global $post;
  156. // vars
  157. $post_id = false;
  158. // get field groups
  159. $field_groups = acf_get_field_groups(array(
  160. 'comment' => $post->post_type
  161. ));
  162. // bail early if no field groups
  163. if( !$field_groups ) return $html;
  164. // ob
  165. ob_start();
  166. // render post data
  167. acf_form_data(array(
  168. 'post_id' => $post_id,
  169. 'nonce' => 'comment'
  170. ));
  171. foreach( $field_groups as $field_group ) {
  172. $fields = acf_get_fields( $field_group );
  173. acf_render_fields( $post_id, $fields, 'p', $field_group['instruction_placement'] );
  174. }
  175. // append
  176. $html .= ob_get_contents();
  177. ob_end_clean();
  178. // return
  179. return $html;
  180. }
  181. /*
  182. * save_comment
  183. *
  184. * This function will save the comment data
  185. *
  186. * @type function
  187. * @date 19/10/13
  188. * @since 5.0.0
  189. *
  190. * @param comment_id (int)
  191. * @return n/a
  192. */
  193. function save_comment( $comment_id ) {
  194. // bail early if not valid nonce
  195. if( ! acf_verify_nonce('comment') ) {
  196. return $comment_id;
  197. }
  198. // validate and save
  199. if( acf_validate_save_post(true) ) {
  200. acf_save_post( "comment_{$comment_id}" );
  201. }
  202. }
  203. /*
  204. * admin_footer
  205. *
  206. * description
  207. *
  208. * @type function
  209. * @date 27/03/2015
  210. * @since 5.1.5
  211. *
  212. * @param $post_id (int)
  213. * @return $post_id (int)
  214. */
  215. function admin_footer() {
  216. ?>
  217. <script type="text/javascript">
  218. (function($) {
  219. // vars
  220. var $spinner = $('#publishing-action .spinner');
  221. // create spinner if not exists (may exist in future WP versions)
  222. if( !$spinner.exists() ) {
  223. // create spinner
  224. $spinner = $('<span class="spinner"></span>');
  225. // append
  226. $('#publishing-action').prepend( $spinner );
  227. }
  228. })(jQuery);
  229. </script>
  230. <?php
  231. }
  232. }
  233. new acf_form_comment();
  234. endif;
  235. ?>