forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
posts.go
200 lines (173 loc) · 5.82 KB
/
posts.go
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
package wordpress
import (
"fmt"
"net/http"
)
const (
PostStatusDraft = "draft"
PostStatusPending = "pending"
PostStatusPrivate = "private"
PostStatusPublish = "publish"
PostStatusTrash = "trash"
PostTypePost = "post"
PostTypePage = "page"
CommentStatusOpen = "open"
CommentStatusClosed = "closed"
CommentStatusApproved = "approved"
CommentStatusUnapproved = "unapproved"
PingStatusOpen = "open"
PingStatusClosed = "closed"
PostFormatStandard = "standard"
PostFormatAside = "aside"
PostFormatGallery = "gallery"
PostFormatImage = "image"
PostFormatLink = "link"
PostFormatStatus = "status"
PostFormatQuote = "quote"
PostFormatVideo = "video"
PostFormatChat = "chat"
)
type GUID struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
type Title struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
type Content struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
type Excerpt struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
type Description struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
type Caption struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
type Post struct {
collection *PostsCollection `json:"-,omitempty"`
ID int `json:"id,omitempty"`
Date string `json:"date,omitempty"`
DateGMT string `json:"date_gmt,omitempty"`
GUID GUID `json:"guid,omitempty"`
FeaturedMedia int `json:"featured_media"`
Categories []int `json:"categories"`
Link string `json:"link,omitempty"`
Modified string `json:"modified,omitempty"`
ModifiedGMT string `json:"modifiedGMT,omitempty"`
Password string `json:"password,omitempty"`
Slug string `json:"slug,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
Title Title `json:"title,omitempty"`
Content Content `json:"content,omitempty"`
Author int `json:"author,omitempty"`
Excerpt Excerpt `json:"excerpt,omitempty"`
CommentStatus string `json:"comment_status,omitempty"`
PingStatus string `json:"ping_status,omitempty"`
Format string `json:"format,omitempty"`
Sticky bool `json:"sticky,omitempty"`
}
func (entity *Post) setCollection(col *PostsCollection) {
entity.collection = col
}
func (entity *Post) Meta() *MetaCollection {
if entity.collection == nil {
// missing post.collection parent. Probably Post struct was initialized manually.
_warning("Missing parent post collection")
return nil
}
return &MetaCollection{
client: entity.collection.client,
parent: entity,
parentType: CollectionPosts,
url: fmt.Sprintf("%v/%v/%v", entity.collection.url, entity.ID, CollectionMeta),
}
}
func (entity *Post) Revisions() *RevisionsCollection {
if entity.collection == nil {
// missing post.collection parent. Probably Post struct was initialized manually, not fetched from API
_warning("Missing parent post collection")
return nil
}
return &RevisionsCollection{
client: entity.collection.client,
parent: entity,
parentType: CollectionPosts,
url: fmt.Sprintf("%v/%v/%v", entity.collection.url, entity.ID, CollectionRevisions),
}
}
func (entity *Post) Terms() *PostsTermsCollection {
if entity.collection == nil {
// missing post.collection parent. Probably Post struct was initialized manually, not fetched from API
_warning("Missing parent post collection")
return nil
}
return &PostsTermsCollection{
client: entity.collection.client,
parent: entity,
parentType: CollectionPosts,
url: fmt.Sprintf("%v/%v/%v", entity.collection.url, entity.ID, CollectionTerms),
}
}
func (entity *Post) Populate(params interface{}) (*Post, *http.Response, []byte, error) {
return entity.collection.Get(entity.ID, params)
}
type PostsCollection struct {
client *Client
url string
entityURL string
}
func (col *PostsCollection) List(params interface{}) ([]Post, *http.Response, []byte, error) {
var posts []Post
resp, body, err := col.client.List(col.url, params, &posts)
// set collection object for each entity which has sub-collection
for _, p := range posts {
p.setCollection(col)
}
return posts, resp, body, err
}
func (col *PostsCollection) Create(new *Post) (*Post, *http.Response, []byte, error) {
var created Post
resp, body, err := col.client.Create(col.url, new, &created)
created.setCollection(col)
return &created, resp, body, err
}
func (col *PostsCollection) Get(id int, params interface{}) (*Post, *http.Response, []byte, error) {
var entity Post
entityURL := fmt.Sprintf("%v/%v", col.url, id)
resp, body, err := col.client.Get(entityURL, params, &entity)
// set collection object for each entity which has sub-collection
entity.setCollection(col)
return &entity, resp, body, err
}
func (col *PostsCollection) Entity(id int) *Post {
entity := Post{
collection: col,
ID: id,
}
return &entity
}
func (col *PostsCollection) Update(id int, post *Post) (*Post, *http.Response, []byte, error) {
var updated Post
entityURL := fmt.Sprintf("%v/%v", col.url, id)
resp, body, err := col.client.Update(entityURL, post, &updated)
// set collection object for each entity which has sub-collection
updated.setCollection(col)
return &updated, resp, body, err
}
func (col *PostsCollection) Delete(id int, params interface{}) (*Post, *http.Response, []byte, error) {
var deleted Post
entityURL := fmt.Sprintf("%v/%v", col.url, id)
resp, body, err := col.client.Delete(entityURL, params, &deleted)
// set collection object for each entity which has sub-collection
deleted.setCollection(col)
return &deleted, resp, body, err
}