-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-rest-api-post.php
142 lines (118 loc) · 2.92 KB
/
wp-rest-api-post.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
<?php
require_once('wp-rest-api-app.php');
Class WPRestAPI_Post extends WPRestAPI_App {
function __construct() {
parent::__construct();
}
public function get_archive() {
$posts = $this->curl_get(
$this->site . 'wp-json/wp/v2/posts/',
array('status' => 'any'),
true
);
// notices
if(empty($posts)) {
return $this->notice_empty();
}
// data
return $posts;
}
public function get_status_color($status) {
switch ($status) {
case 'publish':
echo 'primary';
break;
case 'draft':
echo 'secondary';
break;
case 'private':
echo 'warning';
break;
default:
echo 'dark';
break;
}
}
public function add_post() {
// no post
if(! $_POST) return false;
// empty content
if($_POST['title'] == '' || $_POST['status'] == '' || $_POST['content'] == '') {
echo '<div class="alert alert-danger" role="alert">Please fill all fields!</div>';
return false;
}
// add or edit
if(isset($_POST['post_id']) && intval($_POST['post_id']) > 0) {
$url_entry_point = $this->site . 'wp-json/wp/v2/posts/' . intval($_POST['post_id']);
}
else {
$url_entry_point = $this->site . 'wp-json/wp/v2/posts/';
}
// do curl
$response = $this->curl_post(
$url_entry_point,
array(
'title' => $_POST['title'],
'status' => $_POST['status'],
'content' => $_POST['content'],
'fields[name]' => isset($_POST['name']) ? $_POST['name'] : '',
'fields[address]' => isset($_POST['address']) ? $_POST['address'] : '',
'fields[gender]' => isset($_POST['gender']) ? $_POST['gender'] : '',
)
);
// success or not
if(isset($response->id)) {
// noticea
$string = '<div class="alert alert-primary" role="alert">Your post has been saved!. ';
$string.= 'Now you can <a href="edit-post.php?id=' . $response->id . '">edit your post</a> ';
$string.= 'or continue to <a href="archive.php">view the archive</a></div>';
echo $string;
require_once('footer.php');
exit();
}
else {
echo '<div class="alert alert-danger" role="alert">' . $response->message . '</div>';
}
}
public function get_post($post_id = 0) {
// invalid id notice
if(intval($post_id) <= 0) {
echo $this->notice_empty();
}
$post = $this->curl_get(
$this->site . 'wp-json/wp/v2/posts/' . intval($post_id),
array('status' => 'any'),
true
);
// notices
if(empty($post)) {
echo $this->notice_empty();
}
// data
return $post;
}
public function delete_post($post_id) {
// invalid id notice
if(intval($post_id) <= 0) {
echo $this->notice_empty();
}
$post = $this->curl_delete(
$this->site . 'wp-json/wp/v2/posts/' . intval($post_id),
true
);
// data
return $post;
}
public function trash_post($post_id) {
// invalid id notice
if(intval($post_id) <= 0) {
echo $this->notice_empty();
}
$post = $this->curl_delete(
$this->site . 'wp-json/wp/v2/posts/' . intval($post_id),
false
);
// data
return $post;
}
}