form-comment.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * ACF Comment Form Class |
| 5 | * |
| 6 | * All the logic for adding fields to comments |
| 7 | * |
| 8 | * @class acf_form_comment |
| 9 | * @package ACF |
| 10 | * @subpackage Forms |
| 11 | */ |
| 12 | |
| 13 | if( ! class_exists('acf_form_comment') ) : |
| 14 | |
| 15 | class acf_form_comment { |
| 16 | |
| 17 | |
| 18 | /* |
| 19 | * __construct |
| 20 | * |
| 21 | * This function will setup the class functionality |
| 22 | * |
| 23 | * @type function |
| 24 | * @date 5/03/2014 |
| 25 | * @since 5.0.0 |
| 26 | * |
| 27 | * @param n/a |
| 28 | * @return n/a |
| 29 | */ |
| 30 | |
| 31 | function __construct() { |
| 32 | |
| 33 | // actions |
| 34 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 35 | |
| 36 | |
| 37 | // render |
| 38 | add_filter('comment_form_field_comment', array($this, 'comment_form_field_comment'), 999, 1); |
| 39 | |
| 40 | //add_action( 'comment_form_logged_in_after', array( $this, 'add_comment') ); |
| 41 | //add_action( 'comment_form', array( $this, 'add_comment') ); |
| 42 | |
| 43 | |
| 44 | // save |
| 45 | add_action( 'edit_comment', array( $this, 'save_comment' ), 10, 1 ); |
| 46 | add_action( 'comment_post', array( $this, 'save_comment' ), 10, 1 ); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * validate_page |
| 53 | * |
| 54 | * This function will check if the current page is for a post/page edit form |
| 55 | * |
| 56 | * @type function |
| 57 | * @date 23/06/12 |
| 58 | * @since 3.1.8 |
| 59 | * |
| 60 | * @param n/a |
| 61 | * @return (boolean) |
| 62 | */ |
| 63 | |
| 64 | function validate_page() { |
| 65 | |
| 66 | // global |
| 67 | global $pagenow; |
| 68 | |
| 69 | |
| 70 | // validate page |
| 71 | if( $pagenow == 'comment.php' ) { |
| 72 | |
| 73 | return true; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // return |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* |
| 84 | * admin_enqueue_scripts |
| 85 | * |
| 86 | * This action is run after post query but before any admin script / head actions. |
| 87 | * It is a good place to register all actions. |
| 88 | * |
| 89 | * @type action (admin_enqueue_scripts) |
| 90 | * @date 26/01/13 |
| 91 | * @since 3.6.0 |
| 92 | * |
| 93 | * @param n/a |
| 94 | * @return n/a |
| 95 | */ |
| 96 | |
| 97 | function admin_enqueue_scripts() { |
| 98 | |
| 99 | // validate page |
| 100 | if( ! $this->validate_page() ) { |
| 101 | |
| 102 | return; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | |
| 107 | // load acf scripts |
| 108 | acf_enqueue_scripts(); |
| 109 | |
| 110 | |
| 111 | // actions |
| 112 | add_action('admin_footer', array($this, 'admin_footer'), 10, 1); |
| 113 | add_action('add_meta_boxes_comment', array($this, 'edit_comment'), 10, 1); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /* |
| 119 | * edit_comment |
| 120 | * |
| 121 | * This function is run on the admin comment.php page and will render the ACF fields within custom metaboxes to look native |
| 122 | * |
| 123 | * @type function |
| 124 | * @date 19/10/13 |
| 125 | * @since 5.0.0 |
| 126 | * |
| 127 | * @param $comment (object) |
| 128 | * @return n/a |
| 129 | */ |
| 130 | |
| 131 | function edit_comment( $comment ) { |
| 132 | |
| 133 | // vars |
| 134 | $post_id = "comment_{$comment->comment_ID}"; |
| 135 | |
| 136 | |
| 137 | // get field groups |
| 138 | $field_groups = acf_get_field_groups(array( |
| 139 | 'comment' => get_post_type( $comment->comment_post_ID ) |
| 140 | )); |
| 141 | |
| 142 | |
| 143 | // render |
| 144 | if( !empty($field_groups) ) { |
| 145 | |
| 146 | // render post data |
| 147 | acf_form_data(array( |
| 148 | 'screen' => 'comment', |
| 149 | 'post_id' => $post_id |
| 150 | )); |
| 151 | |
| 152 | |
| 153 | foreach( $field_groups as $field_group ) { |
| 154 | |
| 155 | // load fields |
| 156 | $fields = acf_get_fields( $field_group ); |
| 157 | |
| 158 | |
| 159 | // vars |
| 160 | $o = array( |
| 161 | 'id' => 'acf-'.$field_group['ID'], |
| 162 | 'key' => $field_group['key'], |
| 163 | //'style' => $field_group['style'], |
| 164 | 'label' => $field_group['label_placement'], |
| 165 | 'edit_url' => '', |
| 166 | 'edit_title' => __('Edit field group', 'acf'), |
| 167 | //'visibility' => $visibility |
| 168 | ); |
| 169 | |
| 170 | |
| 171 | // edit_url |
| 172 | if( $field_group['ID'] && acf_current_user_can_admin() ) { |
| 173 | |
| 174 | $o['edit_url'] = admin_url('post.php?post=' . $field_group['ID'] . '&action=edit'); |
| 175 | |
| 176 | } |
| 177 | |
| 178 | ?> |
| 179 | <div id="acf-<?php echo $field_group['ID']; ?>" class="stuffbox"> |
| 180 | <h3 class="hndle"><?php echo $field_group['title']; ?></h3> |
| 181 | <div class="inside"> |
| 182 | <?php acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] ); ?> |
| 183 | <script type="text/javascript"> |
| 184 | if( typeof acf !== 'undefined' ) { |
| 185 | |
| 186 | acf.newPostbox(<?php echo json_encode($o); ?>); |
| 187 | |
| 188 | } |
| 189 | </script> |
| 190 | </div> |
| 191 | </div> |
| 192 | <?php |
| 193 | |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | * comment_form_field_comment |
| 203 | * |
| 204 | * description |
| 205 | * |
| 206 | * @type function |
| 207 | * @date 18/04/2016 |
| 208 | * @since 5.3.8 |
| 209 | * |
| 210 | * @param $post_id (int) |
| 211 | * @return $post_id (int) |
| 212 | */ |
| 213 | |
| 214 | function comment_form_field_comment( $html ) { |
| 215 | |
| 216 | // global |
| 217 | global $post; |
| 218 | |
| 219 | |
| 220 | // vars |
| 221 | $post_id = false; |
| 222 | |
| 223 | |
| 224 | // get field groups |
| 225 | $field_groups = acf_get_field_groups(array( |
| 226 | 'comment' => $post->post_type |
| 227 | )); |
| 228 | |
| 229 | |
| 230 | // bail early if no field groups |
| 231 | if( !$field_groups ) return $html; |
| 232 | |
| 233 | |
| 234 | // enqueue scripts |
| 235 | acf_enqueue_scripts(); |
| 236 | |
| 237 | |
| 238 | // ob |
| 239 | ob_start(); |
| 240 | |
| 241 | // render post data |
| 242 | acf_form_data(array( |
| 243 | 'screen' => 'comment', |
| 244 | 'post_id' => $post_id |
| 245 | )); |
| 246 | |
| 247 | echo '<div class="acf-comment-fields acf-fields -clear">'; |
| 248 | |
| 249 | foreach( $field_groups as $field_group ) { |
| 250 | |
| 251 | $fields = acf_get_fields( $field_group ); |
| 252 | |
| 253 | acf_render_fields( $fields, $post_id, 'p', $field_group['instruction_placement'] ); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | echo '</div>'; |
| 258 | |
| 259 | |
| 260 | // append |
| 261 | $html .= ob_get_contents(); |
| 262 | ob_end_clean(); |
| 263 | |
| 264 | |
| 265 | // return |
| 266 | return $html; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * save_comment |
| 273 | * |
| 274 | * This function will save the comment data |
| 275 | * |
| 276 | * @type function |
| 277 | * @date 19/10/13 |
| 278 | * @since 5.0.0 |
| 279 | * |
| 280 | * @param comment_id (int) |
| 281 | * @return n/a |
| 282 | */ |
| 283 | |
| 284 | function save_comment( $comment_id ) { |
| 285 | |
| 286 | // bail early if not valid nonce |
| 287 | if( !acf_verify_nonce('comment') ) { |
| 288 | return $comment_id; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | // kses |
| 293 | if( isset($_POST['acf']) ) { |
| 294 | $_POST['acf'] = wp_kses_post_deep( $_POST['acf'] ); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | // validate and save |
| 299 | if( acf_validate_save_post(true) ) { |
| 300 | acf_save_post( "comment_{$comment_id}" ); |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /* |
| 307 | * admin_footer |
| 308 | * |
| 309 | * description |
| 310 | * |
| 311 | * @type function |
| 312 | * @date 27/03/2015 |
| 313 | * @since 5.1.5 |
| 314 | * |
| 315 | * @param $post_id (int) |
| 316 | * @return $post_id (int) |
| 317 | */ |
| 318 | |
| 319 | function admin_footer() { |
| 320 | |
| 321 | ?> |
| 322 | <script type="text/javascript"> |
| 323 | (function($) { |
| 324 | |
| 325 | // vars |
| 326 | var $spinner = $('#publishing-action .spinner'); |
| 327 | |
| 328 | |
| 329 | // create spinner if not exists (may exist in future WP versions) |
| 330 | if( !$spinner.exists() ) { |
| 331 | |
| 332 | // create spinner |
| 333 | $spinner = $('<span class="spinner"></span>'); |
| 334 | |
| 335 | |
| 336 | // append |
| 337 | $('#publishing-action').prepend( $spinner ); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | })(jQuery); |
| 342 | </script> |
| 343 | <?php |
| 344 | |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
| 349 | new acf_form_comment(); |
| 350 | |
| 351 | endif; |
| 352 | |
| 353 | ?> |