The effects of alcohol on sustanon 250 leucine for – real weight loss & bodybuilding benefits?
Search
Close
AI Search
Classic Search
 Search Phrase:
 Search Type:
Advanced search options
 Search in Forums:
 Search in date period:

 Sort Search Results by:

Filter by custom fields

Topic prefix

AI Assistant
Can I use wpdiscuz ...
 
Notifications
Clear all

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

11 Posts
2 Users
1 Reactions
3,245 Views
(@stevara)
Posts: 9
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 
[#3621]

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?


 
Posted : 24/01/2022 2:14 am
Asti
 Asti
(@asti)
Posts: 8257
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

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. 


In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 24/01/2022 1:26 pm
(@stevara)
Posts: 9
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

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..


 
Posted : 24/01/2022 2:22 pm
Asti
 Asti
(@asti)
Posts: 8257
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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");

 


In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 24/01/2022 2:40 pm
(@stevara)
Posts: 9
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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);

 
Posted : 24/01/2022 2:53 pm
Asti
 Asti
(@asti)
Posts: 8257
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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");

In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 24/01/2022 3:17 pm
(@stevara)
Posts: 9
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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);

 
Posted : 24/01/2022 8:00 pm
(@stevara)
Posts: 9
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

last_error is: Unexpected '<'

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


 
Posted : 24/01/2022 8:21 pm
Asti
 Asti
(@asti)
Posts: 8257
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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. 


In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 26/01/2022 12:54 pm
(@stevara)
Posts: 9
Active Member
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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

 


 
Posted : 26/01/2022 2:49 pm
Asti
 Asti
(@asti)
Posts: 8257
Illustrious Member Support
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
 

@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', ...)


In case you want to say thank you! 🙂
We'd really appreciate if you leave a good review on the plugin page.
This is the best way to say thank you to this project and the support team.

 
Posted : 26/01/2022 3:38 pm
stevara reacted