/** * Process voting button * @return void * @since 2.0.1.1 */ public function vote() { $post_id = (int) $_POST['post_id']; if (!wp_verify_nonce($_POST['__nonce'], 'vote_' . $post_id)) { ap_send_json(ap_ajax_responce('something_wrong')); return; } if (!is_user_logged_in()) { ap_send_json(ap_ajax_responce('please_login')); return; } $post = get_post($post_id); if ($post->post_author == get_current_user_id()) { ap_send_json(ap_ajax_responce('cannot_vote_own_post')); return; } $type = sanitize_text_field($_POST['type']); $type = $type == 'up' ? 'vote_up' : 'vote_down'; $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); ap_send_json(ap_ajax_responce(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'undo_vote'))); } else { ap_send_json(ap_ajax_responce('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']; ap_send_json(ap_ajax_responce(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'voted'))); } }
/** * 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')); } }
/** * Output voting button. * * @param int $post * * @return null|string * * @since 0.1 */ function ap_vote_btn($post = false, $echo = true) { if (false === $post) { global $post; } if ('answer' == $post->post_type && ap_opt('disable_voting_on_answer')) { return; } if ('question' == $post->post_type && ap_opt('disable_voting_on_question')) { return; } $nonce = wp_create_nonce('vote_' . $post->ID); $vote = ap_is_user_voted($post->ID, 'vote'); $voted = $vote ? true : false; $type = $vote ? $vote->type : ''; $html = ''; $html .= '<div data-id="' . $post->ID . '" class="ap-vote net-vote" data-action="vote">'; $html .= '<a class="' . ap_icon('vote_up') . ' ap-tip vote-up' . ($voted ? ' voted' : '') . ($type == 'vote_down' ? ' disable' : '') . '" data-query="ap_ajax_action=vote&type=up&post_id=' . $post->ID . '&__nonce=' . $nonce . '" href="#" title="' . __('Up vote this post', 'ap') . '"></a>'; $html .= '<span class="net-vote-count" data-view="ap-net-vote" itemprop="upvoteCount">' . ap_net_vote() . '</span>'; if ('question' == $post->post_type && !ap_opt('disable_down_vote_on_question') || 'answer' == $post->post_type && !ap_opt('disable_down_vote_on_answer')) { $html .= '<a data-tipposition="bottom center" class="' . ap_icon('vote_down') . ' ap-tip vote-down' . ($voted ? ' voted' : '') . ($type == ' vote_up' ? ' disable' : '') . '" data-query="ap_ajax_action=vote&type=down&post_id=' . $post->ID . '&__nonce=' . $nonce . '" href="#" title="' . __('Down vote this post', 'ap') . '"></a>'; } $html .= '</div>'; if ($echo) { echo $html; } else { return $html; } }
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; }