<?php $app->get('/comments/unit/:id', function ($id) { include_once 'libs/comment.php'; echo json_encode(fetch_comments_unit($id)); }); $app->post('/comments/unit/', function ($id) { include_once 'libs/comment.php'; $request = get_request_json(); echo json_encode(insert_comment($userId, $request['comment'], $request['unit'])); }); $app->put('/comments/unit/', function ($id) { include_once 'libs/comment.php'; $request = get_request_json(); echo json_encode(update_comment($userId, $request['comment'], $request['unit'])); }); $app->post('/comments/unit/response', function ($id) { include_once 'libs/comment.php'; $request = get_request_json(); echo json_encode(insert_response($userId, $request['comment'], $request['unit'])); }); $app->post('/comments/rate', function ($id) { include_once 'libs/comment.php'; $request = get_request_json(); echo json_encode(rate_comment($userId, $request['comment'], $request['unit'])); });
$current_user = wp_get_current_user(); if (get_option('q_answerratings') != "TURE") { return; } if (get_option('q_logintorate') == "TRUE" && !is_user_logged_in()) { echo "use must login to rate an answer"; //check whether user has permission to rate return; } $rated_comments = get_user_meta($current_user->ID, 'q_ratedanswers'); if (strpos($rated_comments, $_GET['comment_id']) > 0) { echo "youve already rated this comment"; return; } try { $result = rate_comment($_GET['comment_id'], $_GET['rating']); update_user_meta($current_user->ID, 'q_ratedanswers', $rated_comments . "," . $_GET['comment_id'] . ":" . $_GET['rating']); echo $result; } catch (Exception $e) { echo "Rating comment failed: please try again later"; return; } function rate_comment($comment_id, $rating) { $rating = substr(trim($rating), 0, 1); $metastring = get_comment_meta($comment_id, 'qans_ratingmeta'); //use a string <averagerating>:<#ofratings> $metadata = explode(":", $metastring[0]); if ($metadata[1] == 0) { $metastring = $rating . ":1"; $newrating = $rating;