PluginProbe
User Notes / 2.1.0
User Notes v2.1.0
2.1.1 2.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 2.0.0 2.0.1
user-notes / includes / ajax.php

ajax.php in User Notes 2.1.0, at includes/ajax.php

96 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 function user_notes_note_payload($note) {
5 $edited = ($note->updated_at && $note->updated_at !== $note->created_at);
6 return array(
7 'id' => (int) $note->id,
8 'user_id' => (int) $note->user_id,
9 'author_id' => (int) $note->author_id,
10 'author' => user_notes_format_author($note->author_id),
11 'starred' => (int) $note->starred ? 1 : 0,
12 'body_raw' => $note->body,
13 'body_html' => wp_kses_post(wpautop($note->body)),
14 'created_at' => $note->created_at,
15 'updated_at' => $note->updated_at,
16 'created_rel' => user_notes_format_time($note->created_at),
17 'updated_rel' => $edited ? user_notes_format_time($note->updated_at) : '',
18 'edited' => $edited,
19 );
20 }
21
22 add_action('wp_ajax_user_notes_list', function () {
23 check_ajax_referer('user_notes_ajax', 'nonce');
24
25 $user_id = isset($_POST['user_id']) ? (int) sanitize_text_field(wp_unslash($_POST['user_id'])) : 0;
26 if (!$user_id) wp_send_json_error(array('message' => 'bad_user'), 400);
27 if (!user_notes_current_user_can_view($user_id)) wp_send_json_error(array('message' => 'forbidden'), 403);
28
29 $notes = User_Notes_Repo::get_for_user($user_id);
30 $u = get_userdata($user_id);
31
32 wp_send_json_success(array(
33 'user_id' => $user_id,
34 'user_name' => $u ? $u->display_name : sprintf('#%d', $user_id),
35 'user_login' => $u ? $u->user_login : '',
36 'avatar' => get_avatar_url($user_id, array('size' => 48)),
37 'can_edit' => user_notes_current_user_can_edit($user_id),
38 'can_delete' => user_notes_current_user_can_delete($user_id),
39 'notes' => array_map('user_notes_note_payload', $notes),
40 ));
41 });
42
43 add_action('wp_ajax_user_notes_add', function () {
44 check_ajax_referer('user_notes_ajax', 'nonce');
45
46 $user_id = isset($_POST['user_id']) ? (int) sanitize_text_field(wp_unslash($_POST['user_id'])) : 0;
47 if (!$user_id) wp_send_json_error(array('message' => 'bad_user'), 400);
48 if (!user_notes_current_user_can_edit($user_id)) wp_send_json_error(array('message' => 'forbidden'), 403);
49
50 $body = isset($_POST['body']) ? wp_kses_post(wp_unslash($_POST['body'])) : '';
51 if (trim(wp_strip_all_tags($body)) === '') wp_send_json_error(array('message' => 'empty'), 400);
52
53 $starred = !empty($_POST['starred']) ? 1 : 0;
54 $id = User_Notes_Repo::insert($user_id, get_current_user_id(), $body, $starred);
55 wp_send_json_success(user_notes_note_payload(User_Notes_Repo::get($id)));
56 });
57
58 add_action('wp_ajax_user_notes_edit', function () {
59 check_ajax_referer('user_notes_ajax', 'nonce');
60
61 $note_id = isset($_POST['note_id']) ? (int) sanitize_text_field(wp_unslash($_POST['note_id'])) : 0;
62 $note = $note_id ? User_Notes_Repo::get($note_id) : null;
63 if (!$note) wp_send_json_error(array('message' => 'not_found'), 404);
64 if (!user_notes_current_user_can_edit($note->user_id)) wp_send_json_error(array('message' => 'forbidden'), 403);
65
66 $body = isset($_POST['body']) ? wp_kses_post(wp_unslash($_POST['body'])) : '';
67 if (trim(wp_strip_all_tags($body)) === '') wp_send_json_error(array('message' => 'empty'), 400);
68
69 User_Notes_Repo::update_body($note_id, $body);
70 wp_send_json_success(user_notes_note_payload(User_Notes_Repo::get($note_id)));
71 });
72
73 add_action('wp_ajax_user_notes_toggle_star', function () {
74 check_ajax_referer('user_notes_ajax', 'nonce');
75
76 $note_id = isset($_POST['note_id']) ? (int) sanitize_text_field(wp_unslash($_POST['note_id'])) : 0;
77 $note = $note_id ? User_Notes_Repo::get($note_id) : null;
78 if (!$note) wp_send_json_error(array('message' => 'not_found'), 404);
79 if (!user_notes_current_user_can_edit($note->user_id)) wp_send_json_error(array('message' => 'forbidden'), 403);
80
81 User_Notes_Repo::set_starred($note_id, $note->starred ? 0 : 1);
82 wp_send_json_success(user_notes_note_payload(User_Notes_Repo::get($note_id)));
83 });
84
85 add_action('wp_ajax_user_notes_delete', function () {
86 check_ajax_referer('user_notes_ajax', 'nonce');
87
88 $note_id = isset($_POST['note_id']) ? (int) sanitize_text_field(wp_unslash($_POST['note_id'])) : 0;
89 $note = $note_id ? User_Notes_Repo::get($note_id) : null;
90 if (!$note) wp_send_json_error(array('message' => 'not_found'), 404);
91 if (!user_notes_current_user_can_delete($note->user_id)) wp_send_json_error(array('message' => 'forbidden'), 403);
92
93 User_Notes_Repo::delete($note_id);
94 wp_send_json_success(array('id' => $note_id));
95 });
96