-
Notifications
You must be signed in to change notification settings - Fork 12
/
gust-api.php
267 lines (265 loc) · 8.74 KB
/
gust-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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
class Gust_API {
static function login() {
$return = Gust::login($_POST['username'],$_POST['password']);
D::json($return);
}
static function forgotten() {
$return = Gust::retrieve_password($_POST['username']);
D::json($return);
}
static function tax_add($type) {
if (Gust::auth('edit_posts' )) {
$return = Gust::new_tax_term($type,$_POST);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function tax_update($type,$id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::update_tax_term($type,$id,$_POST);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function tax_delete($type,$id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::delete_tax_term($type,$id);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function get_meta_keys() {
if (Gust::auth('edit_posts' )) {
$return = Gust::get_meta_keys();
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post_meta_list($id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::get_meta_list($id);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post_image_list($id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::get_image_list($id);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post_set_featured($id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::set_featured($id,$_POST);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post_meta_delete($id) {
if (Gust::auth('edit_posts' )) {
if ($_SERVER['REQUEST_METHOD']=='DELETE') {
$data = file_get_contents('php://input');
parse_str($data,$_POST);
}
$return = Gust::delete_meta($id,$_POST);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post_meta_update($id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::update_meta($id,$_POST);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function tax($type) {
if (Gust::auth('edit_posts' )) {
if ($type=='post_tag') {
$return = Gust::get_tags();
}
else if ($type=='category') {
$return = Gust::get_categories();
}
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function tax_single($type,$id) {
if (Gust::auth('edit_posts' )) {
$return = Gust::get_tax_term($type,$id);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function upload($id) {
if (Gust::auth()) {
echo Gust::upload($id);
}
}
static function post_image_add($id) {
if (Gust::auth()) {
D::json(Gust::upload($id));
}
}
static function post_image_delete($id) {
if (Gust::auth('edit_posts')) {
if ($_SERVER['REQUEST_METHOD']=='DELETE') {
$data = file_get_contents('php://input');
parse_str($data,$_POST);
}
$return = Gust::delete_image($id,$_POST);
} else {
$return = array('error'=>__('You have no permission to remove this image','gust'));
}
D::json($return);
}
static function upload_delete() {
if (Gust::auth('edit_posts')) {
$return = Gust::delete($_GET['url']);
} else {
$return = array('error'=>__('You have no permission to remove this image','gust'));
}
D::json($return);
}
static function autosave_get($id) {
if (Gust::auth('edit_post',$id)) {
$return = Gust::get_autosave($id);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function autosave($id) {
if (Gust::auth('edit_post',$id)) {
$ret = Gust::put_autosave($id,$_POST);
if (is_wp_error($ret )) {
$return = array('error'=>$ret->get_error_message());
} else {
$return = array('id'=>$id);
}
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function posts() {
if (Gust::auth('edit_posts')) {
$return = Gust::get_posts(
isset($_GET['page'])?$_GET['page']:1,
isset($_GET['type'])?$_GET['type']:'post',
isset($_GET['status'])?$_GET['status']:false,
isset($_GET['limit'])?$_GET['limit']:false
);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post($id) {
if (Gust::auth('edit_post', $id )) {
$return = Gust::get_post($id);
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
static function post_delete($id) {
// TO DO move actual logic to Gust class
if (Gust::auth('delete_post',$id )) {
$ret = wp_delete_post($id);
if (is_wp_error($ret )) {
$return = array('error'=>$ret->get_error_message());
} else {
$return = array('id'=>$id);
}
} else {
$return = array('error'=>__('You have no permission to delete this post','gust'));
}
D::json($return);
}
static function post_save($id) {
// TO DO move actual logic to Gust class
if (Gust::auth('edit_post',$id)) {
if (isset($_POST['featured'])) {
if($_POST['featured']==1 && !isset($_POST['markdown'])) {
stick_post($id);
} else {
unstick_post($id);
}
$return = Gust::get_post($id);
} else if (isset($_POST['published_at']) && !isset($_POST['markdown'])) {
if ($_POST['published_at']=='NaN'){
$date = '0000-00-00 00:00:00';
} else {
$date = date('Y-m-d H:i:s',$_POST['published_at']+ get_option( 'gmt_offset' ) * 3600);
}
// }
// $date = date('Y-m-d H:i:s',$_POST['published_at']);
$arr = array(
'ID' => $id,
'edit_date' => true,
'post_date' => $date,
'post_date_gmt' => $date=='0000-00-00 00:00:00'?'0000-00-00 00:00:00':get_gmt_from_date($date)//date('Y-m-d H:i:s',$_POST['published_at']),
);
$id = wp_update_post($arr);
$return = Gust::get_post($id);
} else if (isset($_POST['slug']) && !isset($_POST['markdown']) ) {
$arr = array(
'ID' => $id,
'post_name' => $_POST['slug']
);
$id = wp_update_post($arr,true);
$return = Gust::get_post($id);
} else if (isset($_POST['markdown'])) {
update_post_meta($id,'_md',$_POST['markdown']);
if (!current_user_can('publish_post',$id) && $_POST['status']=='publish') {
$_POST['status']='pending';
}
if ( 'auto-draft' == $_POST['status'] ) {
$_POST['status'] = 'draft';
}
$arr = array(
'ID' => $id,
'post_title' => $_POST['title']?$_POST['title']:'',
'post_content' => $_POST['html']?$_POST['html']:' ',
'post_status' => $_POST['status']
);
if(!isset($_POST['type']) || $_POST['type']!='page') {
$tags=$_POST['tags'];
$tags=explode(',',$tags);
foreach ($tags as $key=>$tag) {
$no = $tag * 1;
$tags[$key] = $no?$no:$tag;
}
wp_set_post_terms($id,$tags,'post_tag');
$tags=$_POST['categories'];
$tags=explode(',',$tags);
foreach ($tags as $key=>$tag) {
$no = $tag * 1;
$tags[$key] = $no?$no:$tag;
}
wp_set_post_terms($id,$tags,'category');
}
wp_update_post($arr,true);
$return = Gust::get_post($id);
}
} else {
$return = array('error'=>__('You have no permission to edit this post','gust'));
}
D::json($return);
}
}