/**
  * Update question subscribers count.
  * @param  integer $user_id User ID.
  * @param  integer $item_id Item id.
  * @param  string  $activity Activity name.
  */
 function subscriber_count($user_id, $item_id, $activity)
 {
     $q_activity = array('q_all');
     $tax_activity = array('tax_new');
     $counts = ap_subscribers_count($item_id, $activity);
     if (in_array($activity, $q_activity)) {
         update_post_meta($item_id, ANSPRESS_SUBSCRIBER_META, $counts);
     }
 }
Exemple #2
0
 /**
  * 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();
 }
/**
 * Unscubscribe user from a question
 * @param  integer  $question_id Questions ID
 * @param  boolean|integer $user_id
 * @return boolean|array
 */
function ap_remove_question_subscriber($question_id, $user_id = false)
{
    $is_subscribed = ap_is_user_subscribed($question_id);
    if ($user_id === false) {
        $user_id = get_current_user_id();
    }
    if ($is_subscribed) {
        ap_remove_subscriber($user_id, $question_id);
        $counts = ap_subscribers_count($question_id);
        //update post meta
        update_post_meta($question_id, ANSPRESS_SUBSCRIBER_META, $counts);
        return array('count' => $counts, 'action' => 'unsubscribed');
    }
    return false;
}
Exemple #4
0
/**
 * Return the subscriber count for active question
 * @return integer
 * @since 2.1
 */
function ap_question_get_the_subscriber_count()
{
    return ap_subscribers_count(ap_question_get_the_ID());
}
Exemple #5
0
/**
 * Count total numbers of followers
 * @param  integer $user_id
 * @return integer
 */
function ap_followers_count($user_id)
{
    return ap_subscribers_count($user_id, 'u_all');
}
/**
 * Output subscribe btn HTML
 * @param 	boolean|integer $action_id  Question ID or Term ID.
 * @param 	string|false    $type       Subscribe type.
 * @since 	2.0.1
 */
function ap_subscribe_btn_html($action_id = false, $type = false)
{
    global $question_category, $question_tag;
    $filter = apply_filters('ap_subscribe_btn_action_type', array('action_id' => $action_id, 'type' => $type));
    $action_id = $filter['action_id'];
    $type = $filter['type'];
    if (false === $action_id) {
        $action_id = get_question_id();
    }
    $subscribe_type = 'q_all';
    if ($type == false) {
        $subscribe_type = apply_filters('ap_subscribe_btn_type', 'q_all');
    } elseif ($type === 'category' || $type === 'tag') {
        $subscribe_type = 'tax_new_q';
    }
    $subscribed = ap_is_user_subscribed($action_id, $subscribe_type);
    $nonce = wp_create_nonce('subscribe_' . $action_id . '_' . $subscribe_type);
    $title = !$subscribed ? __('Follow', 'ap') : __('Unfollow', 'ap');
    ?>
	<div class="ap-subscribe-btn" id="<?php 
    echo 'subscribe_' . $action_id;
    ?>
">
		<a href="#" class="ap-btn<?php 
    echo $subscribed ? ' active' : '';
    ?>
" data-query="<?php 
    echo 'subscribe::' . $nonce . '::' . $action_id . '::' . $subscribe_type;
    ?>
" data-action="ajax_btn" data-cb="apSubscribeBtnCB">
            <?php 
    echo ap_icon('rss', true);
    ?>
 <span class="text"><?php 
    echo $title;
    ?>
</span>      
        </a>
        <b class="ap-btn-counter" data-view="<?php 
    echo 'subscribe_' . $action_id;
    ?>
"><?php 
    echo ap_subscribers_count($action_id, $subscribe_type);
    ?>
</b>
    </div>

	<?php 
}