/** * Get follow button html * @param integer $user_to_follow user_id to follow or unfollow * @return string */ function ap_get_follow_button($user_to_follow) { $current_user = get_current_user_id(); $nonce = wp_create_nonce('follow_' . $user_to_follow . '_' . $current_user); $following = ap_is_user_following($user_to_follow, $current_user); $title = $following ? __('Unfollow', 'ap') : __('Follow', 'ap'); $output = '<a href="#" id="follow_' . $user_to_follow . '" class="ap-btn ap-btn-follow' . ($following ? ' active' : '') . '" data-action="ap_follow" data-query="ap_ajax_action=follow&user_id=' . $user_to_follow . '&__nonce=' . $nonce . '">' . $title . '</a>'; return $output; }
/** * Get follow button html * @param integer $user_to_follow user_id to follow or unfollow * @param array $text custom button text * @return string */ function ap_get_follow_button($user_to_follow, $text = false) { $current_user = get_current_user_id(); if ($current_user == $user_to_follow) { return; } $nonce = wp_create_nonce('follow_' . $user_to_follow . '_' . $current_user); $following = ap_is_user_following($user_to_follow, $current_user); if (false === $text) { $title = $following ? __('Unfollow', 'anspress-question-answer') : __('Follow', 'anspress-question-answer'); } else { $title = $following ? $text[0] : $text[1]; } $output = '<a href="#" id="follow_' . $user_to_follow . '" class="ap-btn ap-btn-follow' . ($following ? ' active' : '') . '" data-action="ap_follow" data-query="ap_ajax_action=follow&user_id=' . $user_to_follow . '&__nonce=' . $nonce . '">' . $title . '</a>'; return $output; }
/** * Process follow ajax callback */ public function follow() { $user_to_follow = (int) $_POST['user_id']; $current_user_id = get_current_user_id(); if (!ap_verify_nonce('follow_' . $user_to_follow . '_' . $current_user_id)) { $this->something_wrong(); } if (!is_user_logged_in()) { $this->send('please_login'); } if ($user_to_follow == $current_user_id) { $this->send('cannot_follow_yourself'); } $is_following = ap_is_user_following($user_to_follow, $current_user_id); $elm = '#follow_' . $user_to_follow; if ($is_following) { ap_remove_follower($current_user_id, $user_to_follow); $this->send(array('message' => 'unfollow', 'action' => 'unfollow', 'do' => array('updateText' => array($elm, __('Follow', 'anspress-question-answer')), 'toggle_active_class' => $elm))); } else { ap_add_follower($current_user_id, $user_to_follow); $this->send(array('message' => 'follow', 'action' => 'follow', 'do' => array('updateText' => array('#follow_' . $user_to_follow, __('Unfollow', 'anspress-question-answer')), 'toggle_active_class' => $elm))); } }
public function follow() { $user_to_follow = (int) $_POST['user_id']; $current_user_id = get_current_user_id(); if (!wp_verify_nonce($_POST['__nonce'], 'follow_' . $user_to_follow . '_' . $current_user_id)) { ap_send_json(ap_ajax_responce('something_wrong')); return; } if (!is_user_logged_in()) { ap_send_json(ap_ajax_responce('please_login')); return; } if ($user_to_follow == $current_user_id) { ap_send_json(ap_ajax_responce('cannot_follow_yourself')); return; } $is_following = ap_is_user_following($user_to_follow, $current_user_id); if ($is_following) { ap_remove_follower($current_user_id, $user_to_follow); ap_send_json(ap_ajax_responce(array('message' => 'unfollow', 'action' => 'unfollow', 'container' => '#follow_' . $user_to_follow, 'do' => 'updateText', 'text' => __('Follow', 'ap')))); return; } else { ap_add_follower($current_user_id, $user_to_follow); ap_send_json(ap_ajax_responce(array('message' => 'follow', 'action' => 'follow', 'container' => '#follow_' . $user_to_follow, 'do' => 'updateText', 'text' => __('Unfollow', 'ap')))); } }