forked from o1egl/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpages.go
143 lines (117 loc) · 4.36 KB
/
pages.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
package wordpress
import (
"context"
"fmt"
"log"
)
// Page represents a WordPress page.
type Page struct {
collection *PagesService
ID int `json:"id,omitempty"`
Date Time `json:"date,omitempty"`
DateGMT Time `json:"date_gmt,omitempty"`
GUID RenderedString `json:"guid,omitempty"`
Link string `json:"link,omitempty"`
Modified Time `json:"modified,omitempty"`
ModifiedGMT Time `json:"modifiedGMT,omitempty"`
Password string `json:"password,omitempty"`
Slug string `json:"slug,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
Parent int `json:"parent,omitempty"`
Title RenderedString `json:"title,omitempty"`
Content RenderedString `json:"content,omitempty"`
Author int `json:"author,omitempty"`
Excerpt RenderedString `json:"excerpt,omitempty"`
FeaturedImage int `json:"featured_image,omitempty"`
CommentStatus string `json:"comment_status,omitempty"`
PingStatus string `json:"ping_status,omitempty"`
MenuOrder int `json:"menu_order,omitempty"`
Template string `json:"template,omitempty"`
}
func (entity *Page) setService(c *PagesService) {
entity.collection = c
}
// Revisions gets the revisions of a single page.
func (entity *Page) Revisions() *RevisionsService {
if entity.collection == nil {
// missing page.collection parent. Probably Page struct was initialized manually, not fetched from API
log.Println("[go-wordpress] Missing parent page collection")
return nil
}
return &RevisionsService{
service: service(*entity.collection),
parent: entity,
parentType: "pages",
url: fmt.Sprintf("%v/%v/%v", "pages", entity.ID, "revisions"),
}
}
// Populate will fill a manually initialized page with the collection information.
func (entity *Page) Populate(ctx context.Context, params interface{}) (*Page, *Response, error) {
return entity.collection.Get(ctx, entity.ID, params)
}
// PagesService provides access to the page related functions in the WordPress REST API.
type PagesService service
// List returns a list of pages.
func (c *PagesService) List(ctx context.Context, opts *PageListOptions) ([]*Page, *Response, error) {
u, err := addOptions("pages", opts)
if err != nil {
return nil, nil, err
}
req, err := c.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
pages := []*Page{}
resp, err := c.client.Do(ctx, req, &pages)
if err != nil {
return nil, resp, err
}
// set collection object for each entity which has sub-collection
for _, p := range pages {
p.setService(c)
}
return pages, resp, nil
}
// Create creates a new page.
func (c *PagesService) Create(ctx context.Context, newPage *Page) (*Page, *Response, error) {
var created Page
resp, err := c.client.Create(ctx, "pages", newPage, &created)
created.setService(c)
return &created, resp, err
}
// Get returns a single page for the given id.
func (c *PagesService) Get(ctx context.Context, id int, params interface{}) (*Page, *Response, error) {
var entity Page
entityURL := fmt.Sprintf("pages/%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 page for the given id.
func (c *PagesService) Entity(id int) *Page {
entity := Page{
collection: c,
ID: id,
}
return &entity
}
// Update updates a single page with the given id.
func (c *PagesService) Update(ctx context.Context, id int, page *Page) (*Page, *Response, error) {
var updated Page
entityURL := fmt.Sprintf("pages/%v", id)
resp, err := c.client.Update(ctx, entityURL, page, &updated)
// set collection object for each entity which has sub-collection
updated.setService(c)
return &updated, resp, err
}
// Delete removes the page with the given id.
func (c *PagesService) Delete(ctx context.Context, id int, params interface{}) (*Page, *Response, error) {
var deleted Page
entityURL := fmt.Sprintf("pages/%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
}