<?php
/*
Description: Orange-Cheers Theme Comment Plugin.
Version: 1.0
Author: rikakomoe
Author URI: https://github.com/cool2645/orange-cheers
License: MIT
License URI: https://opensource.org/licenses/MIT
*/
/*
* What is this plugin for?
*
* In WordPress one comment can have a "parent", which indicates which comment does this comment replies.
* When you reply to comments, they displays like this:
*
* Alice: Original Comment.
* Bob: I'm the comment that replies to Alice.
* Charlie: I'm the comment that replies to Bob.
*
* This looks awful. Instead, Orange Cheers displays comments like this by adding a "reply_id" meta against
* directly replying to them.
*
* Alice: Original Comment.
* Bob: @Alice I'm the comment that replies to Alice.
* Charlie: @Bob I'm the comment that replies to Bob.
*
*/
add_filter('rest_allow_anonymous_comments', '__return_true');
register_rest_field
('comment', 'meta', array(
'get_callback' => function ($data) {
return get_comment_meta($data['id'], 'reply_id', true);
},
'update_callback' => function ($value, $data) {
if (isset($value['reply_id'])) {
$hasmeta = get_comment_meta($data->comment_ID, 'reply_id', false);
if ($hasmeta) {
update_comment_meta($data->comment_ID, 'reply_id', $value['reply_id']);
} else {
add_comment_meta($data->comment_ID, 'reply_id', $value['reply_id'], true);
}
}
return true;
},));