Пример #1
0
 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'))));
     }
 }
Пример #2
0
 /**
  * Actions to run after posting a comment
  * @param  object|array $comment Comment object.
  */
 public function publish_comment($comment)
 {
     $comment = (object) $comment;
     $post = get_post($comment->comment_post_ID);
     if ($post->post_type == 'question') {
         // Set updated meta for sorting purpose.
         update_post_meta($comment->comment_post_ID, ANSPRESS_UPDATED_META, current_time('mysql'));
         // Subscribe to current question.
         ap_add_question_subscriber($comment->comment_post_ID, $comment->user_id, 'comment', $comment->comment_post_ID);
         ap_insert_notification($comment->user_id, $post->post_author, 'comment_on_question', array('post_id' => $post->ID, 'comment_id' => $comment->comment_ID));
     } elseif ($post->post_type == 'answer') {
         $post_id = wp_get_post_parent_id($comment->comment_post_ID);
         // Set updated meta for sorting purpose.
         update_post_meta($post_id, ANSPRESS_UPDATED_META, current_time('mysql'));
         ap_add_question_subscriber($post_id, $comment->user_id, 'comment', $comment->comment_post_ID);
         ap_insert_notification($comment->user_id, $post->post_author, 'comment_on_answer', array('post_id' => $post->ID, 'comment_id' => $comment->comment_ID));
     }
 }