-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
107 lines (68 loc) · 3.41 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
class INotesPress_API {
public static function get_notes_meta() {
global $wpdb;
if( !(new self)->is_admin() ) wp_send_json_error([], 401);
$query = "SELECT parent_id, parent_type, COUNT(id) AS notes from ".$wpdb->prefix."inotespress GROUP BY parent_type, parent_id;";
$result = $wpdb->get_results($query);
wp_send_json_success($result);
}
public static function get_notes($request) {
global $wpdb;
if( !(new self)->is_admin() ) wp_send_json_error([], 401);
$url_params = $request->get_url_params();
$query = $wpdb->prepare("SELECT * from ".$wpdb->prefix."inotespress where parent_id = '%s' and parent_type = '%s';", $url_params['id'], $url_params['type']);
$notes = $wpdb->get_results($query);
foreach ($notes as $k => $note) {
$userdata = INotesPress_Helper::get_userdata($note->created_by);
$notes[$k]->creator = $userdata->display_name;
$notes[$k]->avatar = INotesPress_Helper::get_avatar_url($userdata->user_email);
$notes[$k]->modified_at = strtotime($note->modified_at) * 1000;
}
wp_send_json_success($notes);
}
public static function add_note($request) {
global $wpdb;
if( !(new self)->is_admin() ) wp_send_json_error([], 401);
$url_params = $request->get_url_params();
$body = json_decode($request->get_body(), true);
$query = "INSERT INTO ".$wpdb->prefix."inotespress(note, parent_id, parent_type, created_by, modified_at) VALUES (%s, %d, %s, %s, %s);";
$note = $body['note'];
$parent = $url_params['id'];
$type = $url_params['type'];
$creator = get_current_user_id();
$date = date('Y-m-d H:i:s');
$notes = $wpdb->query($wpdb->prepare($query, $note, $parent, $type, $creator, $date));
INotesPress_API::get_notes($request);
}
public static function update_note($request) {
global $wpdb;
if( !(new self)->is_admin() ) wp_send_json_error([], 401);
$url_params = $request->get_url_params();
$body = json_decode($request->get_body(), true);
$query = "UPDATE ".$wpdb->prefix."inotespress SET note=%s, modified_at=%s WHERE id=%s AND created_by=%s";
$note = $body['note'];
$creator = get_current_user_id();
$date = date('Y-m-d H:i:s');
$notes = $wpdb->query($wpdb->prepare($query, $note, $date, $url_params['noteid'], $creator));
INotesPress_API::get_notes($request);
}
public static function delete_note($request) {
global $wpdb;
if( !(new self)->is_admin() ) wp_send_json_error([], 401);
$url_params = $request->get_url_params();
$query = "DELETE from ".$wpdb->prefix."inotespress WHERE id=%s AND created_by=%s";
$creator = get_current_user_id();
$notes = $wpdb->query($wpdb->prepare($query, $url_params['noteid'], $creator));
INotesPress_API::get_notes($request);
}
private static function is_admin() {
$user = wp_get_current_user();
return array_intersect(['administrator', 'editor', 'author', 'contributor'], $user->roles);
}
public static function delete_note_by_id($type, $id) {
global $wpdb;
$query = "DELETE from ".$wpdb->prefix."inotespress WHERE parent_type=%s AND parent_id=%s";
$wpdb->query($wpdb->prepare($query, $type, $id));
}
}