/** * Flag button html. * * @return string * * @since 0.9 */ function ap_flag_btn_html($echo = false) { if (!is_user_logged_in()) { return; } global $post; $flagged = ap_is_user_flagged(); $total_flag = ap_flagged_post_meta($post->ID); $nonce = wp_create_nonce('flag_' . $post->ID); $title = !$flagged ? __('Flag this post', 'ap') : __('You have flagged this post', 'ap'); $output = '<a id="flag_' . $post->ID . '" data-action="ajax_btn" data-query="flag_post::' . $nonce . '::' . $post->ID . '" class="flag-btn' . (!$flagged ? ' can-flagged' : '') . '" href="#" title="' . $title . '">' . __('Flag ', 'ap') . '<span class="ap-data-view ap-view-count-' . $total_flag . '" data-view="' . $post->ID . '_flag_count">' . $total_flag . '</span></a>'; if ($echo) { echo $output; } else { return $output; } }
/** * Ajax callback to process post flag button * @since 2.0.0 */ public function flag_post() { $post_id = (int) $_POST['args'][0]; if (!ap_verify_nonce('flag_' . $post_id) || !is_user_logged_in()) { $this->something_wrong(); } $userid = get_current_user_id(); $is_flagged = ap_is_user_flagged($post_id); if ($is_flagged) { $this->send(array('message' => 'already_flagged')); } else { ap_add_flag($userid, $post_id); $count = ap_post_flag_count($post_id); // Update post meta. update_post_meta($post_id, ANSPRESS_FLAG_META, $count); $this->send(array('message' => 'flagged', 'action' => 'flagged', 'view' => array($post_id . '_flag_count' => $count), 'count' => $count)); } $this->something_wrong(); }
/** * Flag a post as inappropriate * @return void * @since 2.0.0-alpha2 */ public function flag_post() { $post_id = (int) $_POST['post_id']; if (!wp_verify_nonce($_POST['__nonce'], 'flag_' . $post_id) && is_user_logged_in()) { ap_send_json(ap_ajax_responce('something_wrong')); return; } $userid = get_current_user_id(); $is_flagged = ap_is_user_flagged($post_id); if ($is_flagged) { ap_send_json(ap_ajax_responce(array('message' => 'already_flagged'))); echo json_encode(array('action' => false, 'message' => __('You already flagged this post', 'ap'))); } else { ap_add_flag($userid, $post_id); $count = ap_post_flag_count($post_id); //update post meta update_post_meta($post_id, ANSPRESS_FLAG_META, $count); ap_send_json(ap_ajax_responce(array('message' => 'flagged', 'action' => 'flagged', 'view' => array($post_id . '_flag_count' => $count), 'count' => $count))); } die; }
public function ap_submit_flag_note() { $args = explode('-', sanitize_text_field($_POST['args'])); $note_id = sanitize_text_field($_POST['note_id']); $other_note = sanitize_text_field($_POST['other_note']); if (wp_verify_nonce($args[1], 'flag_submit_' . $args[0])) { global $wpdb; $userid = get_current_user_id(); $is_flagged = ap_is_user_flagged($args[0]); if ($is_flagged) { // if already then return echo json_encode(array('action' => false, 'message' => __('You already flagged this post', 'ap'))); } else { if ($note_id != 'other') { $row = ap_add_flag($userid, $args[0], $note_id); } else { $row = ap_add_flag($userid, $args[0], NULL, $other_note); } $counts = ap_post_flag_count($args[0]); //update post meta update_post_meta($args[0], ANSPRESS_FLAG_META, $counts); echo json_encode(array('row' => $row, 'action' => 'flagged', 'text' => __('Flag', 'ap') . ' (' . $counts . ')', 'title' => __('You have flagged this post', 'ap'), 'message' => __('This post is notified to moderator. Thank you for helping us', 'ap'))); } } else { echo '0' . __('Please try again', 'ap'); } die; }