Like comment and replies

 

Here is the full code that registers the custom APIs for replies, likes, comments, and retrieving comment details with associated user data in WordPress. I've also included the expected payloads for the APIs.

Full Code with APIs and Payloads

// Register the custom REST API route for replies
function register_custom_replies_endpoint() {
    register_rest_route( 'custom/v1', '/comment/reply', array(
        'methods' => 'POST',
        'callback' => 'handle_reply_to_comment',
        'permission_callback' => function() {
            return current_user_can( 'edit_posts' );
        }
    ));
}
add_action( 'rest_api_init', 'register_custom_replies_endpoint' );

// Handle Reply to Comment
function handle_reply_to_comment( $data ) {
    $comment_id = isset( $data['comment_id'] ) ? intval( $data['comment_id'] ) : 0;
    $content = isset( $data['content'] ) ? sanitize_text_field( $data['content'] ) : '';

    if ( empty( $comment_id ) || empty( $content ) ) {
        return new WP_Error( 'missing_data', 'Comment ID or content is missing', array( 'status' => 400 ) );
    }

    $user_id = get_current_user_id();
    if ( !$user_id ) {
        return new WP_Error( 'not_logged_in', 'You must be logged in to reply', array( 'status' => 401 ) );
    }

    $parent_comment = get_comment( $comment_id );
    if ( !$parent_comment ) {
        return new WP_Error( 'invalid_comment', 'The comment you are replying to does not exist', array( 'status' => 400 ) );
    }

    $reply_data = array(
        'comment_post_ID' => $parent_comment->comment_post_ID,
        'comment_parent' => $comment_id,
        'comment_author' => wp_get_current_user()->user_login,
        'comment_author_email' => wp_get_current_user()->user_email,
        'comment_content' => $content,
        'user_id' => $user_id,
        'comment_date' => current_time( 'mysql' ),
        'comment_approved' => 1,
    );

    $reply_id = wp_insert_comment( $reply_data );

    if ( is_wp_error( $reply_id ) ) {
        return new WP_Error( 'reply_creation_failed', 'Failed to create reply', array( 'status' => 500 ) );
    }

    return array(
        'reply_id' => $reply_id,
        'comment_id' => $comment_id,
        'content' => $content,
        'author_id' => $user_id,
    );
}

// Register the custom REST API route to get comments with associated user data
function register_custom_get_comments_with_user_endpoint() {
    register_rest_route( 'custom/v1', '/get-comments-with-user', array(
        'methods' => 'GET',
        'callback' => 'get_total_comments_and_users_with_details',
        'permission_callback' => 'is_user_logged_in',
    ));
}
add_action( 'rest_api_init', 'register_custom_get_comments_with_user_endpoint' );

// Callback function to get the total number of comments, user IDs, and user details for a post
function get_total_comments_and_users_with_details( $data ) {
    $post_id = $data['post_id'];

    if ( ! $post_id ) {
        return new WP_Error( 'invalid_data', 'Post ID is missing', array( 'status' => 400 ) );
    }

    // Get the comments associated with the post
    $comments = get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve', // Only approved comments
    ));

    if ( empty( $comments ) ) {
        return rest_ensure_response( array( 
            'total_comments' => 0, 
            'user_ids' => [], 
            'comments' => [] 
        ) );
    }

    // Arrays to store user IDs and comment data
    $user_ids = array();
    $comment_data = array();

    foreach ( $comments as $comment ) {
        if ( ! in_array( $comment->user_id, $user_ids ) && $comment->user_id > 0 ) {
            $user_ids[] = $comment->user_id;
        }

        $user = get_user_by( 'id', $comment->user_id );

        if ( $user ) {
            $user_details = array(
                'user_id' => $user->ID,
                'user_name' => $user->display_name,
                'user_email' => $user->user_email,
                'user_avatar' => get_avatar_url( $user->ID ),
            );
        } else {
            $user_details = null;
        }

        $comment_data[] = array(
            'comment_id' => $comment->comment_ID,
            'comment_content' => $comment->comment_content,
            'comment_author' => $comment->comment_author,
            'comment_date' => $comment->comment_date,
            'user_id' => $comment->user_id,
            'user_details' => $user_details
        );
    }

    return rest_ensure_response( array( 
        'total_comments' => count( $comments ), 
        'user_ids' => $user_ids, 
        'comments' => $comment_data 
    ));
}

// Register the custom REST API route for liking a post
function register_custom_like_endpoint() {
    register_rest_route( 'custom/v1', '/like', array(
        'methods' => 'POST',
        'callback' => 'like_post',
        'permission_callback' => 'is_user_logged_in',
    ));

    register_rest_route( 'custom/v1', '/unlike', array(
        'methods' => 'POST',
        'callback' => 'unlike_post',
        'permission_callback' => 'is_user_logged_in',
    ));
}
add_action( 'rest_api_init', 'register_custom_like_endpoint' );

// Callback function to like a post
function like_post( $data ) {
    $post_id = $data['post_id'];
    $user_id = get_current_user_id();

    if ( ! $post_id || ! $user_id ) {
        return new WP_Error( 'invalid_data', 'Post ID or user ID is missing', array( 'status' => 400 ) );
    }

    $likes = get_post_meta( $post_id, '_likes', true );
    if ( ! is_array( $likes ) ) {
        $likes = array();
    }

    if ( in_array( $user_id, $likes ) ) {
        return new WP_Error( 'already_liked', 'You have already liked this post', array( 'status' => 400 ) );
    }

    $likes[] = $user_id;
    update_post_meta( $post_id, '_likes', $likes );

    return rest_ensure_response( array( 'message' => 'Post liked successfully' ) );
}

// Callback function to unlike a post
function unlike_post( $data ) {
    $post_id = $data['post_id'];
    $user_id = get_current_user_id();

    if ( ! $post_id || ! $user_id ) {
        return new WP_Error( 'invalid_data', 'Post ID or user ID is missing', array( 'status' => 400 ) );
    }

    $likes = get_post_meta( $post_id, '_likes', true );
    if ( ! is_array( $likes ) || ! in_array( $user_id, $likes ) ) {
        return new WP_Error( 'not_liked', 'You have not liked this post', array( 'status' => 400 ) );
    }

    $likes = array_diff( $likes, array( $user_id ) );
    update_post_meta( $post_id, '_likes', $likes );

    return rest_ensure_response( array( 'message' => 'Post unliked successfully' ) );
}

// Register the custom REST API route to get total users who liked a post
function register_custom_get_likes_endpoint() {
    register_rest_route( 'custom/v1', '/get-likes', array(
        'methods' => 'GET',
        'callback' => 'get_total_likes_and_users',
        'permission_callback' => 'is_user_logged_in',
    ));
}
add_action( 'rest_api_init', 'register_custom_get_likes_endpoint' );

// Callback function to get the total number of users who liked a post
function get_total_likes_and_users( $data ) {
    $post_id = $data['post_id'];

    if ( ! $post_id ) {
        return new WP_Error( 'invalid_data', 'Post ID is missing', array( 'status' => 400 ) );
    }

    $likes = get_post_meta( $post_id, '_likes', true );

    if ( ! is_array( $likes ) ) {
        return rest_ensure_response( array( 'total_likes' => 0, 'user_ids' => [] ) );
    }

    return rest_ensure_response( array( 'total_likes' => count( $likes ), 'user_ids' => $likes ) );
}

// Register the custom REST API routes for comments
function register_custom_comment_endpoints() {
    // Make Comment Endpoint
    register_rest_route(
        'custom/v1', 
        '/comment', 
        array(
            'methods' => 'POST', 
            'callback' => 'handle_make_comment', 
            'permission_callback' => function() {
                return current_user_can( 'edit_posts' );
            }
        )
    );

    // Delete Comment Endpoint
    register_rest_route(
        'custom/v1

', '/comment/delete', array( 'methods' => 'POST', 'callback' => 'handle_delete_comment', 'permission_callback' => function() { return current_user_can( 'delete_posts' ); } ) ); } add_action( 'rest_api_init', 'register_custom_comment_endpoints' );

// Handle Making a Comment function handle_make_comment( $data ) { $post_id = $data['post_id']; $content = $data['content'];

if ( empty( $post_id ) || empty( $content ) ) {
    return new WP_Error( 'missing_data', 'Post ID or content is missing', array( 'status' => 400 ) );
}

$user_id = get_current_user_id();

if ( !$user_id ) {
    return new WP_Error( 'not_logged_in', 'You must be logged in to comment', array( 'status' => 401 ) );
}

$comment_data = array(
    'comment_post_ID' => $post_id,
    'comment_author' => wp_get_current_user()->user_login,
    'comment_author_email' => wp_get_current_user()->user_email,
    'comment_content' => $content,
    'user_id' => $user_id,
    'comment_date' => current_time( 'mysql' ),
    'comment_approved' => 1,
);

$comment_id = wp_insert_comment( $comment_data );

if ( is_wp_error( $comment_id ) ) {
    return new WP_Error( 'comment_creation_failed', 'Failed to create comment', array( 'status' => 500 ) );
}

return array(
    'comment_id' => $comment_id,
    'content' => $content,
);

}

// Handle Deleting a Comment function handle_delete_comment( $data ) { $comment_id = $data['comment_id'];

if ( empty( $comment_id ) ) {
    return new WP_Error( 'missing_data', 'Comment ID is missing', array( 'status' => 400 ) );
}

$comment = get_comment( $comment_id );
if ( !$comment || $comment->user_id != get_current_user_id() ) {
    return new WP_Error( 'unauthorized', 'You are not authorized to delete this comment', array( 'status' => 403 ) );
}

wp_delete_comment( $comment_id, true );

return rest_ensure_response( array( 'message' => 'Comment deleted successfully' ) );

}


### Explanation:

1. **Handling Replies**: 
   - `POST /comment/reply`: To post a reply to a specific comment.
   - `GET /comment/replies`: To fetch the replies for a specific comment.

2. **Likes**: 
   - `POST /like`: To like a post.
   - `POST /unlike`: To unlike a post.
   - `GET /get-likes`: To retrieve all the users who liked a post.

3. **Comments**: 
   - `POST /comment`: To add a comment to a post.
   - `POST /comment/delete`: To delete a comment.

4. **User Information**: 
   - All the routes provide user details such as avatar, email, and name.

### Example Payloads:
- **POST /comment/reply**:
  ```json
  {
    "comment_id": 123,
    "content": "This is a reply."
  }
  • POST /like:

    {
      "post_id": 123
    }
    
  • POST /comment:

    {
      "post_id": 123,
      "content": "This is a comment."
    }
    
  • GET /get-comments-with-user:

    {
      "post_id": 123
    }
    

Comments

Popular posts from this blog

Frida Server Setup And Audit Syscalls of Android Application

ডপলার ক্রিয়া

Biot-Savart এর সূত্র (৪)