/** * Process ajax user avatar upload request. * Sanitize file and pass to upload_file(). Rename image to md5 and store file * name in user meta. Also remove existing avtar if exists * @return void */ public function avatar_upload() { if (ap_user_can_upload_avatar() && ap_verify_nonce('upload_avatar_' . get_current_user_id())) { $photo = $this->upload_photo('thumbnail'); if (false === $photo) { ap_send_json(ap_ajax_responce(array('message' => $this->upload_error, 'message_type' => 'error'))); } $file = str_replace('\\', '\\\\', $photo['file']); $photo['file'] = $file; $photo['small_url'] = str_replace(basename($photo['url']), 'small_' . basename($photo['url']), $photo['url']); $small_name = str_replace(basename($photo['file']), 'small_' . basename($photo['file']), $photo['file']); $photo['small_file'] = $small_name; $userid = get_current_user_id(); // Remove previous image. $previous_avatar = get_user_meta($userid, '_ap_avatar', true); if ($previous_avatar['file'] && file_exists($previous_avatar['file'])) { unlink($previous_avatar['file']); } if ($previous_avatar['small_file'] && file_exists($previous_avatar['small_file'])) { unlink($previous_avatar['small_file']); } // Resize thumbnail. $image = wp_get_image_editor($file); if (!is_wp_error($image)) { $image->resize(200, 200, true); $image->save($file); $image->resize(50, 50, true); $image->save($small_name); } update_user_meta($userid, '_ap_avatar', $photo); do_action('ap_after_avatar_upload', $userid, $photo); ap_ajax_json(array('status' => true, 'action' => 'avatar_uploaded', 'user_id' => $userid, 'message' => __('Avatar uploaded successfully.', 'anspress-question-answer'), 'html' => get_avatar($userid, 150))); } ap_ajax_json(array('message' => __('There was an error while uploading avatar, please check your image', 'anspress-question-answer'), 'message_type' => 'error')); }
/** * Send JSON response and terminate * @param array|string $result Ajax response. */ public function send($result) { ap_send_json(ap_ajax_responce($result)); }
public function upload_post_image() { if (!ap_user_can_upload_image()) { $this->result = array('message' => 'no_permission'); return; } $user_id = get_current_user_id(); $file = $_FILES['post_upload_image']; if ($file['size'] > ap_opt('max_upload_size')) { $this->result = array('message_type' => 'error', 'message' => sprintf(__('File cannot be uploaded, size is bigger then %d Byte'), ap_opt('max_upload_size'))); return; } if (ap_user_upload_limit_crossed($user_id)) { $this->result = array('message' => 'upload_limit_crossed'); return; } if (!is_user_logged_in()) { $this->result = array('message' => 'no_permission'); return; } if (!isset($_POST['__nonce']) || !wp_verify_nonce($_POST['__nonce'], 'upload_image_' . $user_id)) { ap_send_json(ap_ajax_responce('something_wrong')); } if (!empty($file) && is_array($file) && $file['error'] == 0) { $attachment_id = ap_upload_user_file($file); if ($attachment_id !== false) { ap_send_json(ap_ajax_responce(array('action' => 'upload_post_image', 'html' => wp_get_attachment_image($attachment_id, 'full'), 'message' => 'post_image_uploaded', 'attachment_id' => $attachment_id))); } } ap_send_json(ap_ajax_responce('something_wrong')); }
function ap_ajax_json($response) { ap_send_json(ap_ajax_responce($response)); }
public function markread_notification() { $id = (int) $_POST['id']; if (isset($_POST['id']) && !wp_verify_nonce($_POST['__nonce'], 'ap_markread_notification_' . $id) && !is_user_logged_in()) { ap_send_json(ap_ajax_responce('something_wrong')); return; } elseif (!wp_verify_nonce($_POST['__nonce'], 'ap_markread_notification_' . get_current_user_id()) && !is_user_logged_in()) { ap_send_json(ap_ajax_responce('something_wrong')); return; } if (isset($_POST['id'])) { $notification = ap_get_notification_by_id($id); if ($notification && ($notification['apmeta_actionid'] == get_current_user_id() || is_super_admin())) { $row = ap_update_meta(array('apmeta_type' => 'notification'), array('apmeta_id' => $notification['apmeta_id'])); if ($row !== false) { ap_send_json(ap_ajax_responce(array('message' => 'mark_read_notification', 'action' => 'mark_read_notification', 'container' => '.ap-notification-' . $notification['apmeta_id'], 'view' => array('notification_count' => ap_get_total_unread_notification())))); } } } else { $row = ap_notification_mark_all_read(get_current_user_id()); if ($row !== false) { ap_send_json(ap_ajax_responce(array('message' => 'mark_read_notification', 'action' => 'mark_all_read', 'container' => '#ap-notification-dropdown', 'view' => array('notification_count' => '0')))); } } //if process reached here then there must be something wrong ap_send_json(ap_ajax_responce('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; }