'commentID', 'vc_vote' => 'vote', ); $arr = array(); foreach($fields as $inputKey => $newKey) if(empty($input[$inputKey])) return false; else $arr[$newKey] = (int)$input[$inputKey]; if( !$arr['commentID'] ) return false; if( $arr['vote'] < -1 || $arr['vote'] > 1) return fase; return $arr; } function _CVAddVote($commentID, $userID, $vote, $ip, $time) { global $wpdb; $cvTable = $wpdb->prefix . 'pitchvote'; if( $userID ) $userQuery = array('userID = %d', $userID); else $userQuery = array('ip = INET_ATON(%s)', $ip); $wpdb->query( "LOCK TABLE $cvTable WRITE" ); $alreadyVoted = $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $cvTable WHERE commentID = %d AND $userQuery[0]", $commentID, $userQuery[1])); if( !$alreadyVoted ) { $wpdb->query( $wpdb->prepare("INSERT INTO $cvTable ( commentID, userID, vote, ip, voteTime ) VALUES ( %d, %d, %d, INET_ATON(%s), %d )", $commentID, $userID, $vote, $ip, $time )); } $wpdb->query( 'UNLOCK TABLES' ); return !$alreadyVoted; } function CVDoAddVote($userID, $ip) { $options = CVGetOptions(); $response = array( 'mesage' => '', 'votes' => 0, ); if( !empty( $_POST )) $input = CVDoValidateInput( $_POST ); else $input = false; if( !$input ) $response['message'] = 'Invalid input.'; else { $commentID = $input['commentID']; if( $options['require_login'] && !$userID ) $response['message'] = 'You must login to vote.'; else { $voteRecorded = _CVAddVote( $commentID, $userID, $input['vote'], $ip, time() ); if( !$voteRecorded ) $response['message'] = 'You already voted.'; else { $response['message'] = 'Vote recorded. Thank you.'; } } if( $options['display_rating'] ) $response['votes'] = CVGetCommentVote( $commentID ); } //Don't want to require PHP 5.2 quite yet, even though everyone should be using it by now. //echo json_encode( $response ); echo <<< EOT {"message":"$response[message]","votes":$response[votes]} EOT; exit; } CVDoAddVote( $user_ID, $_SERVER['REMOTE_ADDR'] ); ?>