Exemple #1
0
/**
 * Add a follower
 * @param  integer  		$current_user_id   	Current user_id
 * @param  integer  		$user_to_follow 	user to follow
 * @return integer
 */
function ap_add_follower($current_user_id, $user_to_follow)
{
    $row = ap_new_subscriber($current_user_id, $user_to_follow, 'u_all');
    if ($row !== false) {
        do_action('ap_added_follower', $user_to_follow, $current_user_id);
    }
    return $row;
}
Exemple #2
0
 /**
  * Move subscribers from ap_meta table to ap_subscribers table.
  * @since 2.4
  */
 public function move_subscribers()
 {
     if (!current_user_can('manage_options')) {
         return;
     }
     global $wpdb;
     $count = $wpdb->get_var("SELECT count(*) FROM {$wpdb->prefix}ap_meta WHERE apmeta_type = 'subscriber' ");
     $i = 1;
     while ($count >= $i) {
         $subscribe = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}ap_meta WHERE apmeta_type = 'subscriber' LIMIT 0,100");
         if ($subscribe) {
             $ids_to_remove = array();
             foreach ($subscribe as $s) {
                 $type = 'q_all';
                 $question_id = 0;
                 $ids_to_remove[] = $s->apmeta_id;
                 if ($s->apmeta_param == '') {
                     $question_id = $s->apmeta_actionid;
                 }
                 if ($s->apmeta_param == 'tag' || $s->apmeta_param == 'category') {
                     $type = 'tax_new_q';
                 }
                 ap_new_subscriber($s->apmeta_userid, $s->apmeta_actionid, $type, $question_id);
             }
             if (!empty($ids_to_remove)) {
                 $ids_to_remove = implode(',', $ids_to_remove);
                 $wpdb->query("DELETE FROM {$wpdb->prefix}ap_meta WHERE find_in_set(apmeta_id, '{$ids_to_remove}') ");
             }
         }
         $i = $i + 100;
     }
     $count = $wpdb->get_var("SELECT count(*) FROM {$wpdb->prefix}ap_meta WHERE apmeta_type = 'subscriber' ");
     if ($count < 1) {
         update_option('ap_subscribers_moved', true);
     }
 }
Exemple #3
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();
 }
/**
 * 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);
    }
}
 /**
  * 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);
     }
 }