[wp-hackers] Ajax Button Submission Failing - 50% of the time? Comment Voting Plugin Help
Andrew Bartel
andrew.bartel at gmail.com
Tue Nov 19 05:07:24 UTC 2013
Can you put it in a gist or something? It's hard to debug code from gmail.
And what specifically is it failing on? If you just go through clicking
up vote on comments where is it failing versus not failing? Is it on
comments that are being up voted that have already been up voted/removed?
How many levels of recursion deep have you tested it? I promise you're
not hitting some arbitrary 60% point of failure in http requests or packet
drops.
Have you console.log(ged) every step of the way to see where the point of
failure is? I've never met someone who writes code who doesn't have to go
line by line var dumping, echoing, alerting, console logging, debug
outputting, etc every so often, it's part of the process. Sometimes you
write hundreds of lines in a day (or more) and it just works, other times
you bang your head against the keyboard for hours on a few lines and feel
like you're getting no where, it's part of the process.
-Andrew
On Sun, Nov 17, 2013 at 10:21 PM, BenderisGreat
<greglancaster71 at gmail.com>wrote:
> I wrote this code today, and I cant figure why half the time the json
> response is a failure. Its a comment voting plugin, and its my second
> plugin I have ever coded. Could you guys please take a look and if
> something stands out as an obvious issue share that with me?
>
> The idea was to have a reddit.com style voting system. You can click
> upvote
> or downvote - and if you have already upvoted and click upvote it removed
> your vote alrogether. Same goes for downvote. The codes record in the
> database about 40% of the time from my testing. The comments I am testing
> on ARE threaded, and I thought maybe that played a role but looking at the
> comment IDs they all match up so I am unsure what the cause could be.
>
> Here is the entirety of my code:
>
> *CREATE DB CODE: *
> function ams_comment_voting_database()
> {
> global $wpdb;
> global $ams_comment_voting;
> $ams_comment_voting = $wpdb->prefix . 'ams_comment_voting';
>
> if($wpdb->get_var("show tables like '$ams_comment_voting'") !=
> $ams_comment_voting)
> {
>
> $sql = "CREATE TABLE ". $ams_comment_voting ." (
> id mediumint(9) NOT NULL AUTO_INCREMENT,
> voter_id int(6) NOT NULL,
> comment_id int(6) NOT NULL,
> comment_pid int(6) NOT NULL,
> author_id int(6) NOT NULL,
> vote_value tinyint(1) NOT NULL,
> UNIQUE KEY id (id)
> PRIMARY KEY (voter_id, comment_id)
> );";
>
> require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
> $wpdb->query($sql);
> }
> }
>
> register_activation_hook(__FILE__,'ams_comment_voting_database');
>
>
>
> * VOTING FUNCTIONS & SCRIPTS *
>
> <?php //comment voting system
> add_action( 'wp_enqueue_scripts', 'amc_comment_vote_ajax' );
>
> function amc_comment_vote_ajax() {
> wp_register_script( "amc_comment_vote",
> WP_PLUGIN_URL.'/vote_comm/js/amc_comment_vote.js', array('jquery') );
> wp_localize_script( 'amc_comment_vote', 'anotherAjax', array( 'ajaxurl'
> => admin_url( 'admin-ajax.php' )));
>
> wp_enqueue_script( 'jquery' );
> wp_enqueue_script( 'amc_comment_vote' );
>
> }
>
>
> function ams_comment_vote_count($content) {
> global $wpdb, $comment;
>
> $comment_ID = get_comment_ID();
> $comment = get_comment($comment_ID);
> $cuid = $comment->comment_ID;
> $vote_up = '<button id="upvote" type="button"
> data-comment="'.$comment_ID.'"></button>';
> $vote_down = '<button id="downvote" type="button"
> data-comment="'.$comment_ID.'" ></button>';
>
> $count_votes = $wpdb->get_col( $wpdb->prepare ( "SELECT
> SUM(vote_value)
> FROM ".$wpdb->prefix."ams_comment_voting WHERE comment_id = %d",
> $comment_ID));
> foreach ($count_votes as $id => $cID) {
>
> if (!$cID) {
> return '<div id="vote_buttons"><div class="votes">0</div> Votes' .
> $vote_up
> . $vote_down . '<br></div>' . $content;
> }
>
> return '<div id="vote_buttons"><div class="votes">' . $cID .
> '</div> Votes'
> . $vote_up . $vote_down . '<br></div>' . $content;
> }
> }
> add_action('comment_text', 'ams_comment_vote_count');
>
>
> add_action('wp_ajax_amc_comment_vote', 'amc_comment_vote');
> add_action('wp_ajax_nopriv_amc_comment_vote', 'amc_comment_vote');
> function amc_comment_vote() {
> global $wpdb, $comment, $post, $current_user;
> get_currentuserinfo();
>
>
> $cID = $_POST['commentid'];
> $voter_id = $current_user->ID;
> $grabFromCID = get_comment($cID);
>
> $author_id = $grabFromCID->user_id;
> $comment_post_id = $grabFromCID->comment_post_ID;
> $vote_replace = '';
>
>
> $vote_direction = $_POST['direction'];
> $voteValueArray = array( 'upvote' => 1,
>
> 'downvote' => -1,
> );
> if( array_key_exists( $vote_direction, $voteValueArray ) ){
> $vote_value = $voteValueArray[ $vote_direction ];
> } else { $vote_value = 0; }
>
>
>
> $alreadyVotedUp = $wpdb->get_row( $wpdb->prepare ( "SELECT * FROM
> ".$wpdb->prefix."ams_comment_voting WHERE comment_id = %d AND voter_id =
> %d", $cID, $voter_id));
>
> if (isset($alreadyVotedUp)) {
>
> $current_value = $alreadyVotedUp->vote_value;
> $replaceVoteValue ='';
> if ($current_value === 1 && $vote_value === 1) {
> $replaceVoteValue = 0;
> } elseif ($current_value === 1 && $vote_value === -1) {
> $replaceVoteValue = -1;
> } elseif ($current_value === -1 && $vote_value === 1) {
> $replaceVoteValue = 1;
> } elseif ($current_value === -1 && $vote_value === -1) {
> $replaceVoteValue = 0;
> } elseif ($current_value === 0 && $vote_value === 1) {
> $replaceVoteValue = 1;
> } elseif ($current_value === 0 && $vote_value === -1) {
> $replaceVoteValue = -1;
> }
>
> } else {
> $replaceVoteValue = NULL;
> }
>
>
>
> $tableName = $wpdb->prefix . 'ams_comment_voting';
>
> $sql = $wpdb->prepare("INSERT INTO $tableName (`voter_id`, `comment_id`,
> `comment_pid`, `author_id`, `vote_value`) VALUES (%d, %d, %d, %d, %d) ON
> DUPLICATE KEY UPDATE vote_value = %d", $voter_id, $cID, $comment_post_id,
> $author_id, $vote_value, $replaceVoteValue);
>
>
>
> $wpdb->query($sql);
>
>
> $response = array( 'success' => true );
> wp_send_json_success($response);
>
> die();
>
> }
>
>
> *THE JQUERY / AJAX SUBMISSION *
>
> jQuery( document ).ready( function( $ ) {
> $( 'button#upvote, button#downvote').click( function() {
>
>
> $(this).css('color', 'red');
> $upVoteButton = $( 'button#upvote' );
>
> $clickedButton = $(event.target);
> var commentid = $(event.target).attr('data-comment');
> var direction = $(event.target).attr('id')
>
> var data = {
> 'action': 'amc_comment_vote',
> 'commentid': commentid,
> 'direction': direction
> }
>
> $.ajax({
> type: 'post',
> url: anotherAjax.ajaxurl,
> data: data,
> success: function ( response ) {
> if ( ! response.success ) {
>
> alert( 'FAIL!' );
> }
> else {
> alert( 'Success!' );
> $clickedButton.parent().siblings('.votes').html(function(i, val) {
> return
> +val+1 });
> }
> }
> });
> });
> });
>
>
>
> --
> View this message in context:
> http://wordpress-hackers.1065353.n5.nabble.com/Ajax-Button-Submission-Failing-50-of-the-time-Comment-Voting-Plugin-Help-tp42870.html
> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list