/** * Checks if user is already following a user. * @param integer $user_to_follow User to follow. * @param integer $current_user_id Current user id. */ function ap_is_user_following($user_to_follow, $current_user_id = false) { if ($current_user_id === false) { $user_id = get_current_user_id(); } if ($current_user_id > 0) { return ap_is_user_subscribed($user_to_follow, 'u_all', $current_user_id); } return false; }
/** * Subscribe user for post comments * @param object $comment Comment object. */ public function after_new_comment($comment) { $post = get_post($comment->comment_post_ID); $type = 'q_post'; $question_id = $post->ID; if ('answer' == $post->post_type) { $type = 'a_all'; $question_id = $post->post_parent; } if (!ap_is_user_subscribed($comment->comment_post_ID, $type, $comment->user_id)) { ap_new_subscriber($comment->user_id, $comment->comment_post_ID, $type, $question_id); } }
public function subscribe() { $action_id = (int) $_POST['action_id']; $type = sanitize_text_field($_POST['type']); if (!wp_verify_nonce($_POST['__nonce'], 'subscribe_' . $action_id . '_' . $type)) { ap_send_json(ap_ajax_responce('something_wrong')); return; } if (!is_user_logged_in()) { ap_send_json(ap_ajax_responce('please_login')); return; } if ($type === 'category') { $subscribe_type = 'category'; } elseif ($type === 'tag') { $subscribe_type = 'tag'; } else { $subscribe_type = false; } $user_id = get_current_user_id(); $is_subscribed = ap_is_user_subscribed($action_id, $user_id, $subscribe_type); if ($is_subscribed) { if ($subscribe_type === false) { $row = ap_remove_question_subscriber($action_id); } else { $row = ap_remove_subscriber($user_id, $action_id, $subscribe_type); } if (FALSE !== $row) { ap_send_json(ap_ajax_responce(array('message' => 'unsubscribed', 'action' => 'unsubscribed', 'container' => '#subscribe_' . $action_id . ' b', 'do' => 'updateHtml', 'html' => __('Follow question', 'ap')))); } } else { if ($subscribe_type === false) { ap_add_question_subscriber($action_id); } else { ap_add_subscriber($user_id, $action_id, $subscribe_type); } ap_send_json(ap_ajax_responce(array('message' => 'subscribed', 'action' => 'subscribed', 'container' => '#subscribe_' . $action_id . ' b', 'do' => 'updateHtml', 'html' => __('Unfollow question', 'ap')))); } }
/** * Process ajax subscribe request. */ public function subscribe() { $action_id = (int) $_POST['args'][0]; $type = sanitize_text_field($_POST['args'][1]); if (!ap_verify_nonce('subscribe_' . $action_id . '_' . $type)) { $this->something_wrong(); } if (!is_user_logged_in()) { $this->send('please_login'); } $question_id = 0; if ('tax_new_q' === $type) { $subscribe_type = 'tax_new_q'; } else { $subscribe_type = 'q_all'; $question_id = $action_id; } $user_id = get_current_user_id(); $is_subscribed = ap_is_user_subscribed($action_id, $subscribe_type, $user_id); $elm = '#subscribe_' . $action_id . ' .ap-btn'; if ($is_subscribed) { $row = ap_remove_subscriber($action_id, $user_id, $subscribe_type); if (false !== $row) { $count = ap_subscribers_count($action_id, $subscribe_type); $this->send(array('message' => 'unsubscribed', 'action' => 'unsubscribed', 'do' => array('updateHtml' => $elm . ' .text', 'toggle_active_class' => $elm), 'count' => $count, 'html' => __('Follow', 'anspress-question-answer'), 'view' => array('subscribe_' . $action_id => $count))); } } else { $row = ap_new_subscriber($user_id, $action_id, $subscribe_type, $question_id); if (false !== $row) { $count = ap_subscribers_count($action_id, $subscribe_type); $this->send(array('message' => 'subscribed', 'action' => 'subscribed', 'do' => array('updateHtml' => '#subscribe_' . $action_id . ' .text', 'toggle_active_class' => $elm), 'count' => $count, 'html' => __('Unfollow', 'anspress-question-answer'), 'view' => array('subscribe_' . $action_id => $count))); } } $this->something_wrong(); }
/** * Output subscribe btn HTML * @param boolean|integer $action_id Question ID or Term ID * @return string * @since 2.0.1 */ function ap_subscribe_btn_html($action_id = false, $type = false) { global $question_category, $question_tag; if ($action_id === false) { if (is_question()) { $action_id = get_question_id(); } elseif (is_question_category()) { $action_id = $question_category->term_id; } elseif (is_question_tag()) { $action_id = $question_tag->term_id; } } if ($type == false) { if (is_question_category()) { $subscribe_type = 'category'; } elseif (is_question_tag()) { $subscribe_type = 'tag'; } else { $subscribe_type = false; } } else { if ($type === 'category') { $subscribe_type = 'category'; } elseif ($type === 'tag') { $subscribe_type = 'tag'; } else { $subscribe_type = false; } } $subscribed = ap_is_user_subscribed($action_id, false, $subscribe_type); $nonce = wp_create_nonce('subscribe_' . $action_id . '_' . $subscribe_type); $title = !$subscribed ? __('Follow question', 'ap') : __('Unfollow question', 'ap'); ?> <div class="ap-subscribe" id="<?php echo 'subscribe_' . $action_id; ?> "> <a href="#" class="ap-btn-toggle<?php echo $subscribed ? ' active' : ''; ?> " data-query="ap_ajax_action=subscribe&action_id=<?php echo $action_id; ?> &__nonce=<?php echo $nonce; ?> &type=<?php echo $subscribe_type; ?> " data-action="ap_subscribe" data-args="<?php echo $action_id . '-' . $nonce; ?> "> <span class="apicon-toggle-on"></span> <span class="apicon-toggle-off"></span> </a> <b><?php echo $title; ?> </b> </div> <?php }
/** * Subscribe a user for a question. */ function ap_subscribe_question($posta, $user_id = false) { if (!is_object($posta) || !isset($posta->post_type)) { $posta = get_post($posta); } // Return if not question. if ('question' != $posta->post_type) { return false; } if (false === $user_id) { $user_id = $posta->post_author; } if (!ap_is_user_subscribed($posta->ID, 'q_all', $user_id)) { ap_new_subscriber($user_id, $posta->ID, 'q_all', $posta->ID); } }