révision
30de3a151b
5 fichiers modifiés avec 196 ajouts et 0 suppressions
@ -0,0 +1,28 @@ |
|||
# Introduction |
|||
|
|||
An implementation of the standard WordPress API methods is provided,Use the [https://github.com/kolo/xmlrpc](https://github.com/kolo/xmlrpc) library as client. |
|||
|
|||
# Todo List |
|||
- [x] [wp.newPost](#wp.newPost) |
|||
- [ ] wp.getPost |
|||
- [ ] wp.getPosts |
|||
- [ ] wp.editPost |
|||
- [ ] wp.deletePost |
|||
|
|||
# Usage |
|||
- ### wp.newPost |
|||
```go |
|||
c, err := xmlrpc.NewClient(`https://example.com/xmlrpc.php`, xmlrpc.UserInfo{ |
|||
`your username`, |
|||
`your password`, |
|||
}) |
|||
if err != nil { |
|||
log.Fatalln(err) |
|||
} |
|||
p := wordpress.NewPost(`content`, `title`, []string{`tag1`, `tag2`}, []string{`cate1`, `cate2`}) |
|||
blogID, err := c.Call(p) |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
log.Println(blogID) |
|||
``` |
@ -0,0 +1,72 @@ |
|||
package xmlrpc |
|||
|
|||
import ( |
|||
"github.com/kolo/xmlrpc" |
|||
"go-wordpress-xmlrpc/wordpress" |
|||
"io/ioutil" |
|||
"log" |
|||
"net/http" |
|||
) |
|||
|
|||
type HttpRT struct { |
|||
http.RoundTripper |
|||
} |
|||
|
|||
// NewHttpRT return a http.RoundTripper can print the request body
|
|||
func NewHttpRT(t http.RoundTripper) http.RoundTripper { |
|||
return &HttpRT{t} |
|||
} |
|||
|
|||
// RoundTrip implement a http.RoundTripper can print the request body
|
|||
func (t HttpRT) RoundTrip(req *http.Request) (*http.Response, error) { |
|||
// you can customize to get more control over connection options
|
|||
// example: print the request body
|
|||
|
|||
b, err := req.GetBody() |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
r, err := ioutil.ReadAll(b) |
|||
if err != nil { |
|||
log.Println(err) |
|||
} |
|||
log.Println(string(r)) |
|||
return t.RoundTripper.RoundTrip(req) |
|||
} |
|||
|
|||
// Client Packaging the xmlrpc client
|
|||
type Client struct { |
|||
*xmlrpc.Client |
|||
UserInfo |
|||
} |
|||
|
|||
// UserInfo wordpress's username and password
|
|||
type UserInfo struct { |
|||
Username string |
|||
Password string |
|||
} |
|||
|
|||
// NewDefaultClient default implement a http.RoundTripper can print the request body
|
|||
func NewDefaultClient(url string, info UserInfo) (*Client, error) { |
|||
t := NewHttpRT(http.DefaultTransport) |
|||
c, err := xmlrpc.NewClient(url, t) |
|||
return &Client{Client: c, UserInfo: info}, err |
|||
} |
|||
|
|||
// NewClient without http.RoundTripper
|
|||
func NewClient(url string, info UserInfo) (*Client, error) { |
|||
c, err := xmlrpc.NewClient(url, nil) |
|||
return &Client{Client: c, UserInfo: info}, err |
|||
} |
|||
|
|||
// NewCustomizeClient you can Customize your http.RoundTripper
|
|||
func NewCustomizeClient(url string, t http.RoundTripper, info UserInfo) (*Client, error) { |
|||
c, err := xmlrpc.NewClient(url, t) |
|||
return &Client{Client: c, UserInfo: info}, err |
|||
} |
|||
|
|||
// Call abstract to proxy xmlrpc call
|
|||
func (c *Client) Call(baseCall wordpress.BaseCall) (result interface{}, err error) { |
|||
err = c.Client.Call(baseCall.GetMethord(), baseCall.GetArgs(c.Username, c.Password), &result) |
|||
return result, err |
|||
} |
@ -0,0 +1,5 @@ |
|||
module go-wordpress-xmlrpc |
|||
|
|||
go 1.16 |
|||
|
|||
require github.com/kolo/xmlrpc v0.0.0-20201022064351-38db28db192b |
@ -0,0 +1,6 @@ |
|||
package wordpress |
|||
|
|||
type BaseCall interface { |
|||
GetMethord() string |
|||
GetArgs(user string, pwd string) interface{} |
|||
} |
@ -0,0 +1,85 @@ |
|||
package wordpress |
|||
|
|||
import ( |
|||
"time" |
|||
) |
|||
|
|||
type Post struct { |
|||
BlogID int |
|||
PostContent |
|||
} |
|||
|
|||
type PostContent struct { |
|||
PostType string `xmlrpc:"post_type"` |
|||
PostStatus string `xmlrpc:"post_status"` |
|||
PostTitle string `xmlrpc:"post_title"` |
|||
PostAuthor int `xmlrpc:"post_author"` |
|||
PostExcerpt string `xmlrpc:"post_excerpt"` |
|||
PostContent string `xmlrpc:"post_content"` |
|||
PostDate string `xmlrpc:"post_date"` |
|||
PostFormat string `xmlrpc:"post_format"` |
|||
PostName string `xmlrpc:"post_name"` |
|||
PostPassword string `xmlrpc:"post_password"` |
|||
CommentStatus string `xmlrpc:"comment_status"` |
|||
PingStatus string `xmlrpc:"ping_status"` |
|||
Sticky int `xmlrpc:"sticky"` |
|||
PostThumbnail int `xmlrpc:"post_thumbnail"` |
|||
PostParent int `xmlrpc:"post_parent"` |
|||
// Terms Terms `xmlrpc:"terms"`
|
|||
TermsNames TermsNames `xmlrpc:"terms_names"` |
|||
Enclosure Enclosure `xmlrpc:"enclosure"` |
|||
} |
|||
|
|||
type Terms struct { |
|||
TermID string `xmlrpc:"term_id"` |
|||
TermGroup string `xmlrpc:"term_group"` |
|||
Taxonomy string `xmlrpc:"taxonomy"` |
|||
TermTaxonomyID int `xmlrpc:"term_taxonomy_id"` |
|||
Name string `xmlrpc:"name"` |
|||
Slug string `xmlrpc:"slug"` |
|||
Description string `xmlrpc:"description"` |
|||
Parent string `xmlrpc:"parent"` |
|||
Count int `xmlrpc:"count"` |
|||
} |
|||
|
|||
type TermsNames struct { |
|||
PostCategory []string `xmlrpc:"category"` |
|||
TagsInput []string `xmlrpc:"post_tag"` |
|||
} |
|||
|
|||
type Enclosure struct { |
|||
Url string `xmlrpc:"url"` |
|||
Length int `xmlrpc:"length"` |
|||
Type string `xmlrpc:"type"` |
|||
} |
|||
|
|||
func (p Post) GetMethord() string { |
|||
return `wp.newPost` |
|||
} |
|||
|
|||
func (p Post) GetArgs(user string, pwd string) interface{} { |
|||
args := make([]interface{}, 4) |
|||
args = append(args, p.BlogID, user, pwd, p.PostContent) |
|||
return args |
|||
} |
|||
|
|||
func NewPost(content string, title string, tags []string, cate []string) (p Post) { |
|||
p.PostContent = PostContent{ |
|||
PostType: `post`, |
|||
PostStatus: `publish`, |
|||
PostTitle: title, |
|||
PostContent: content, |
|||
PostDate: time.Now().Format(`2006-01-02 15:04:05`), |
|||
TermsNames: TermsNames{ |
|||
PostCategory: cate, |
|||
TagsInput: tags, |
|||
}, |
|||
} |
|||
return p |
|||
} |
|||
|
|||
// NewSpecificPost Customize various values by yourself
|
|||
func NewSpecificPost(content PostContent) (p Post) { |
|||
p.PostContent = content |
|||
return p |
|||
} |
Chargement…
Référencer dans un nouveau ticket