public function ap_vote_for_close() { $args = explode('-', sanitize_text_field($_POST['args'])); if (wp_verify_nonce($args[1], 'close_' . $args[0])) { $voted_closed = ap_is_user_voted_closed($args[0]); $type = 'close'; $userid = get_current_user_id(); if ($voted_closed) { // if already in voted for close then remove it $row = ap_remove_vote($type, $userid, $args[0]); $counts = ap_post_close_vote($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_CLOSE_META, $counts); $result = apply_filters('ap_cast_unclose_result', array('row' => $row, 'action' => 'removed', 'text' => __('Close', 'ap') . ' (' . $counts . ')', 'title' => __('Vote for closing', 'ap'), 'message' => __('Your close request has been removed', 'ap'))); } else { $row = ap_add_vote($userid, $type, $args[0]); $counts = ap_post_close_vote($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_CLOSE_META, $counts); $result = apply_filters('ap_cast_close_result', array('row' => $row, 'action' => 'added', 'text' => __('Close', 'ap') . ' (' . $counts . ')', 'title' => __('Undo your vote', 'ap'), 'message' => __('Your close request has been sent', 'ap'))); } } else { $result = array('action' => false, 'message' => _('Something went wrong', 'ap')); } die(json_encode($result)); }
/** * Process voting button. * @since 2.0.1.1 */ public function vote() { $post_id = (int) $_POST['post_id']; if (!ap_verify_nonce('vote_' . $post_id)) { $this->something_wrong(); } if (!is_user_logged_in()) { $this->send('please_login'); } $post = get_post($post_id); if ($post->post_author == get_current_user_id() && !is_super_admin()) { $this->send('cannot_vote_own_post'); } $type = sanitize_text_field($_POST['type']); $type = $type == 'up' ? 'vote_up' : 'vote_down'; if ('question' == $post->post_type && ap_opt('disable_down_vote_on_question') && 'vote_down' == $type) { $this->send('voting_down_disabled'); } elseif ('answer' === $post->post_type && ap_opt('disable_down_vote_on_answer') && 'vote_down' === $type) { $this->send('voting_down_disabled'); } $userid = get_current_user_id(); $is_voted = ap_is_user_voted($post_id, 'vote', $userid); if (is_object($is_voted) && $is_voted->count > 0) { // If user already voted and click that again then reverse. if ($is_voted->type == $type) { ap_remove_vote($type, $userid, $post_id, $post->post_author); $counts = ap_post_votes($post_id); // Update post meta. update_post_meta($post_id, ANSPRESS_VOTE_META, $counts['net_vote']); do_action('ap_undo_vote', $post_id, $counts); $action = 'undo'; $count = $counts['net_vote']; do_action('ap_undo_' . $type, $post_id, $counts); $this->send(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'undo_vote')); } else { $this->send('undo_vote_your_vote'); } } else { ap_add_vote($userid, $type, $post_id, $post->post_author); $counts = ap_post_votes($post_id); // Update post meta. update_post_meta($post_id, ANSPRESS_VOTE_META, $counts['net_vote']); do_action('ap_' . $type, $post_id, $counts); $action = 'voted'; $count = $counts['net_vote']; $this->send(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'voted')); } }
public function ap_follow() { $args = $_POST['args']; if (wp_verify_nonce($args['nonce'], 'follow_' . $args['user'])) { $userid = (int) sanitize_text_field($args['user']); $user_following = ap_is_user_voted($userid, 'follow', get_current_user_id()); $user = get_userdata($userid); $user_name = $user->data->display_name; if (!$user_following) { $row = ap_add_vote(get_current_user_id(), 'follow', $userid); $action = 'follow'; $text = __('Unfollow', 'ap'); $title = sprintf(__('Unfollow %s', 'ap'), $user_name); $message = sprintf(__('You are now following %s', 'ap'), $user_name); } else { $row = ap_remove_vote('follow', get_current_user_id(), $userid); $action = 'unfollow'; $text = __('Follow', 'ap'); $title = sprintf(__('Follow %s', 'ap'), $user_name); $message = sprintf(__('You unfollowed %s', 'ap'), $user_name); } if ($row !== FALSE) { $followers = ap_count_vote(false, 'follow', $userid); $following = ap_count_vote(get_current_user_id(), 'follow'); update_user_meta($userid, AP_FOLLOWERS_META, $followers); update_user_meta(get_current_user_id(), AP_FOLLOWING_META, $following); $result = apply_filters('ap_follow_result', array('row' => $row, 'action' => $action, 'text' => $text, 'id' => $userid, 'title' => $title, 'message' => $message, 'following_count' => $following, 'followers_count' => $followers)); echo json_encode($result); } else { echo json_encode(array('action' => false, 'message' => _('Unable to process your request, please try again.', 'ap'))); } } else { echo json_encode(array('action' => false, 'message' => _('Something went wrong', 'ap'))); } die; }