forked from o1egl/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 9
/
posts.go
198 lines (164 loc) · 5.93 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
package wordpress
import (
"context"
"fmt"
"log"
)
// Constants for different post values.
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"
)
// RenderedString contains a raw and rendered version of a string such as title, content, excerpt, etc.
type RenderedString struct {
Raw string `json:"raw,omitempty"`
Rendered string `json:"rendered,omitempty"`
}
// Post represents a WordPress post.
type Post struct {
collection *PostsService
Author int `json:"author,omitempty"`
Categories []int `json:"categories,omitempty"`
CommentStatus string `json:"comment_status,omitempty"`
Content RenderedString `json:"content,omitempty"`
Date Time `json:"date,omitempty"`
DateGMT Time `json:"date_gmt,omitempty"`
Excerpt RenderedString `json:"excerpt,omitempty"`
FeaturedMedia int `json:"featured_media,omitempty"`
Format string `json:"format,omitempty"`
GUID RenderedString `json:"guid,omitempty"`
ID int `json:"id,omitempty"`
Link string `json:"link,omitempty"`
Modified Time `json:"modified,omitempty"`
ModifiedGMT Time `json:"modified_gmt,omitempty"`
Password string `json:"password,omitempty"`
PingStatus string `json:"ping_status,omitempty"`
Slug string `json:"slug,omitempty"`
Status string `json:"status,omitempty"`
Sticky bool `json:"sticky,omitempty"`
Subtitle string `json:"wps_subtitle,omitempty"`
Tags []int `json:"tags,omitempty"`
Template string `json:"template,omitempty"`
Title RenderedString `json:"title,omitempty"`
Type string `json:"type,omitempty"`
}
func (entity *Post) setService(c *PostsService) {
entity.collection = c
}
// Revisions gets the revisions of a single post.
func (entity *Post) Revisions() *RevisionsService {
if entity.collection == nil {
// missing post.collection parent. Probably Post struct was initialized manually, not fetched from API
log.Println("[go-wordpress] Missing parent post collection")
return nil
}
return &RevisionsService{
service: service(*entity.collection),
parent: entity,
parentType: "posts",
url: fmt.Sprintf("%v/%v/%v", "posts", entity.ID, "revisions"),
}
}
// Terms gets the terms of a single post.
func (entity *Post) Terms() *PostsTermsService {
if entity.collection == nil {
// missing post.collection parent. Probably Post struct was initialized manually, not fetched from API
log.Println("[go-wordpress] Missing parent post collection")
return nil
}
return &PostsTermsService{
client: entity.collection.client,
parent: entity,
parentType: "posts",
url: fmt.Sprintf("%v/%v/%v", "posts", entity.ID, "terms"),
}
}
// Populate will fill a manually initialized post with the collection information.
func (entity *Post) Populate(ctx context.Context, params interface{}) (*Post, *Response, error) {
return entity.collection.Get(ctx, entity.ID, params)
}
// PostsService provides access to the post related functions in the WordPress REST API.
type PostsService service
// List returns a list of posts.
func (c *PostsService) List(ctx context.Context, opts *PostListOptions) ([]*Post, *Response, error) {
u, err := addOptions("posts", opts)
if err != nil {
return nil, nil, err
}
req, err := c.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
posts := []*Post{}
resp, err := c.client.Do(ctx, req, &posts)
if err != nil {
return nil, resp, err
}
// set collection object for each entity which has sub-collection
for _, p := range posts {
p.setService(c)
}
return posts, resp, nil
}
// Create creates a new post.
func (c *PostsService) Create(ctx context.Context, newPost *Post) (*Post, *Response, error) {
var created Post
resp, err := c.client.Create(ctx, "posts", newPost, &created)
created.setService(c)
return &created, resp, err
}
// Get returns a single post for the given id.
func (c *PostsService) Get(ctx context.Context, id int, params interface{}) (*Post, *Response, error) {
var entity Post
entityURL := fmt.Sprintf("posts/%v", id)
resp, err := c.client.Get(ctx, entityURL, params, &entity)
// set collection object for each entity which has sub-collection
entity.setService(c)
return &entity, resp, err
}
// Entity returns a basic post for the given id.
func (c *PostsService) Entity(id int) *Post {
entity := Post{
collection: c,
ID: id,
}
return &entity
}
// Update updates a single post with the given id.
func (c *PostsService) Update(ctx context.Context, id int, post *Post) (*Post, *Response, error) {
var updated Post
entityURL := fmt.Sprintf("posts/%v", id)
resp, err := c.client.Update(ctx, entityURL, post, &updated)
// set collection object for each entity which has sub-collection
updated.setService(c)
return &updated, resp, err
}
// Delete removes the post with the given id.
func (c *PostsService) Delete(ctx context.Context, id int, params interface{}) (*Post, *Response, error) {
var deleted Post
entityURL := fmt.Sprintf("posts/%v", id)
resp, err := c.client.Delete(ctx, entityURL, params, &deleted)
// set collection object for each entity which has sub-collection
deleted.setService(c)
return &deleted, resp, err
}