comment_post_ID;
$type = $comment->comment_type;
$post = get_post( $post_id );
$parent = get_comment( $comment->comment_parent );
$name = empty( $comment->comment_author ) ? fcntr( 'anonymous_guest' ) : $comment->comment_author;
if ( $type == 'private' ) {
echo '
';
return;
}
// Get badge (if any)
$badge = fictioneer_get_comment_badge( get_user_by( 'id', $comment->user_id ), $comment, $post->post_author );
// Start HTML ---> ?>
comment_author ) ? fcntr( 'anonymous_guest' ) : $parent->comment_author; ?>
'GET',
'callback' => 'fictioneer_rest_get_story_comments',
'args' => array(
'post_id' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
'required' => true
),
'page' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
'sanitize_callback' => 'absint',
'default' => 1
)
),
'permission_callback' => '__return_true' // Public
)
);
}
add_action( 'rest_api_init', 'fictioneer_register_endpoint_get_story_comments' );
/**
* Sends HTML of paginated comments for a given story
*
* @since 4.0.0
* @since 5.6.3 Refactored for REST API.
*
* @param WP_REST_Request $WP_REST_Request Request object.
*
* @return WP_REST_Response|WP_Error Response or error.
*/
function fictioneer_rest_get_story_comments( WP_REST_Request $request ) {
// Rate limit
fictioneer_check_rate_limit( 'fictioneer_rest_get_story_comments', 10 );
// Validations
$story_id = $request->get_param( 'post_id' );
$story_id = isset( $story_id ) ? fictioneer_validate_id( $story_id, 'fcn_story' ) : false;
$default_error = array( 'error' => __( 'Comments could not be loaded.', 'fictioneer' ) );
// Abort if not a story
if ( ! $story_id ) {
if ( WP_DEBUG ) {
$data = array( 'error' => __( 'Provided post ID is not a story ID.', 'fictioneer' ) );
} else {
$data = $default_error;
}
return rest_ensure_response( array( 'data' => $data, 'success' => false ), 400 );
}
// Setup
$page = $request->get_param( 'page' );
$story = fictioneer_get_story_data( $story_id, true, array( 'refresh_comment_count' => true ) );
$chapter_ids = $story['chapter_ids']; // Only contains publicly visible chapters
$comments_per_page = get_option( 'comments_per_page' );
$chapter_data = [];
$report_threshold = get_option( 'fictioneer_comment_report_threshold', 10 ) ?? 10;
// Abort if no chapters have been found
if ( count( $chapter_ids ) < 1 ) {
if ( WP_DEBUG ) {
$data = array( 'error' => __( 'There are no valid chapters with comments.', 'fictioneer' ) );
} else {
$data = $default_error;
}
return rest_ensure_response( array( 'data' => $data, 'success' => false ) );
}
// Collect titles and permalinks of all chapters
foreach ( $chapter_ids as $chapter_id ) {
$chapter_data[ $chapter_id ] = [get_the_title( $chapter_id ), get_the_permalink( $chapter_id )];
}
// Get comments
$comments = get_comments(
array(
'status' => 'approve',
'post_type' => ['fcn_chapter'],
'post__in' => $chapter_ids ?: [0], // Must not be empty!
'number' => $comments_per_page,
'paged' => $page,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_marked_offensive',
'value' => [true, 1, '1'],
'compare' => 'NOT IN'
),
array(
'key' => 'fictioneer_marked_offensive',
'compare' => 'NOT EXISTS'
)
)
)
)
);
// Calculate how many more comments (if any) are there after this batch
$remaining = $story['comment_count'] - $page * $comments_per_page;
// Start buffer
ob_start();
// Make sure there are comments to display...
if ( count( $comments ) > 0 ) {
foreach ( $comments as $comment ) {
$reports = get_comment_meta( $comment->comment_ID, 'fictioneer_user_reports', true );
// Render empty if...
if ( $comment->comment_type === 'user_deleted' ) {
// Start HTML ---> ?>
= $report_threshold ) {
// Start HTML ---> ?>
?>
0 ) {
$load_n = $remaining > $comments_per_page ? $comments_per_page : $remaining;
// Start HTML ---> ?>
?>
$output, 'postId' => $story_id, 'page' => $page );
// Return buffer
return rest_ensure_response( array( 'data' => $data, 'success' => true ) );
}