wpDiscuz - WordPress Comment Plugin
Share:
Notifications
Clear all

Can I use wpdiscuz in my theme function.php file in an endpoint to add new vote?


stevara
Posts: 10
Topic starter
(@stevara)
Active Member
Joined: 4 months ago

Sorry if this is too simple but I am new with php.

I need to change the number of votes from my react native app. I have an endpoint in function.php but I don't know how to use wpdiscuz to add a like or dislike to comment. My code looks like this:

add_action( 'rest_api_init', function () {
        register_rest_route( 'wp/v2/comments_extended', '/change/(?P\d+)/(?P[a-z]+)', array(
                'methods' => 'PUT',
                'callback' => 'change_comment_votes'
        ) );
} );

function change_comment_votes( $request ) {
    global $wpdb;
    
    $commentId = $request['comment_id'];
    $voteType = $request['vote_type'];
        
    if (strcmp($voteType, 'like') === 0) {

    }
    else {

    }
}

Or is there a different way to this if at all possible?

10 Replies
Asti
Posts: 4613
 Asti
Support
(@asti)
Support member
Joined: 4 years ago

Hi @stevara,

We're really sorry, but there is no way for adding like/dislike via REST API.

You can view the "voteOnComment" method in the WpdiscuzHelperAjax class, it'll help you better understand how the wpDiscuz votes are realised. 

Reply
stevara
Posts: 10
Topic starter
(@stevara)
Active Member
Joined: 4 months ago

I see that update_comment_meta function is being called where I guess comment_meta table is being updated. And also updateVoteType function in WPDiscuzDBManager.php where  wc_users_voted table is updated.

Can I get the meta_value field from comment_meta in my endpoint, change it and then update the comment_meta database?

Also with the wc_users_voted table but I see that I will need to add new row there (although there is an UPDATE statement in updateVoteType). The user_id will then need to be an ip address of the user if I'm right? (I allow anonymous commenting and voting). And if id field in that table is somehow set to be auto filled..

Reply
8 Replies
Asti
 Asti
Support
(@asti)
Joined: 4 years ago

Support member
Posts: 4613

@stevara,

Can I get the meta_value field from comment_meta in my endpoint, change it and then update the comment_meta database?

Yes, you're right. 

Also with the wc_users_voted table but I see that I will need to add new row there (although there is an UPDATE statement in updateVoteType). The user_id will then need to be an ip address of the user if I'm right?

This is also correct. You can use the user IP as a value of the user ID. Just a bit of correction. You should use the INSERT instead of the UPDATE statement in updateVoteType. 

Also, please use the code below for deleting the caches:

if ($this->options->thread_display["mostVotedByDefault"]) {
                do_action("wpdiscuz_reset_comments_cache", $commentId);
            } else {
                do_action("wpdiscuz_reset_comments_extra_cache", $commentId);
            }
            do_action("wpdiscuz_clean_post_cache", $commentId, "comment_voted");

 

Reply
stevara
(@stevara)
Joined: 4 months ago

Active Member
Posts: 10

@asti I see now that I also have to reset the cache. But how can I access options->thread_display in my function.php file?

I see that options is a WpdiscuzOption object with dbManager as parameter (WPDiscuzCore.php)

$this->dbManager = new WpdiscuzDBManager();
$this->options = new WpdiscuzOptions($this->dbManager);
Reply
Asti
 Asti
Support
(@asti)
Joined: 4 years ago

Support member
Posts: 4613

@stevara,

Please use this one: 

if (wpDiscuz()->options->thread_display["mostVotedByDefault"] ) {
                do_action("wpdiscuz_reset_comments_cache", $commentId);
            } else {
                do_action("wpdiscuz_reset_comments_extra_cache", $commentId);
            }
            do_action("wpdiscuz_clean_post_cache", $commentId, "comment_voted");
Reply
stevara
(@stevara)
Joined: 4 months ago

Active Member
Posts: 10

@asti Everything works now. The number of likes or dislikes change as they should. But just to clear the thing that is bothering me. Update on commentmeta works and practically just that update and the code below (that you sent with $postId instead of $commentId - think that was a mistake)

if (wpDiscuz()->options->thread_display["mostVotedByDefault"] ) {
                do_action("wpdiscuz_reset_comments_cache", $postId);
            } else {
                do_action("wpdiscuz_reset_comments_extra_cache", $postId);
            }
            do_action("wpdiscuz_clean_post_cache", $postId, "comment_voted");

do the job.

The insert statement on wc_users_voted table doesn't add new row (and $wpdb->last_error is just empty string). Maybe I'm making a mistake somewhere:

$vote = 1;
$date = current_time("timestamp");
$userIp = md5(getUserIP());

// $commentId and $postId are valid numbers and getUserIp() return my ip address

$sql = $wpdb->prepare("INSERT INTO `{$wpdb->wc_users_voted}` (`user_id`, `comment_id`, `vote_type`,`is_guest`,`post_id`,`date`)VALUES(%s,%d,%d,%d,%d,%d);", $userIp, $commentId, $vote, 1, $postId, $date);
$wpdb->query($sql);
Reply
stevara
(@stevara)
Joined: 4 months ago

Active Member
Posts: 10

last_error is: Unexpected '<'

I didn't set show_errors() before...

Reply
Asti
 Asti
Support
(@asti)
Joined: 4 years ago

Support member
Posts: 4613

@stevara,

There is no such a $wpdb->wc_users_voted table in the wpdb. You should replace this {$wpdb->wc_users_voted} with this wp_wc_users_voted string. 

In your case, the wp_ can be different. 

Reply
stevara
(@stevara)
Joined: 4 months ago

Active Member
Posts: 10

@asti

But when I used commentmeta, I didn't put that prefix and it worked.

$wpdb->update($wpdb->commentmeta, ...) -- this is working

$wpdb->insert($wpdb->wc_users_voted, ...) --- this is not

 

Reply
Asti
 Asti
Support
(@asti)
Joined: 4 years ago

Support member
Posts: 4613

@stevara,

this is wordpress defult table, 

$wpdb->update($wpdb->commentmeta, ...) -- this is working

but this one is not a defult 

$wpdb->insert($wpdb->wc_users_voted, ...) --- this is not

This $wpdb->insert($wpdb->wc_users_voted, ...) should be a $wpdb->insert('wp_wc_users_voted', ...)

Reply
Share: