wordpress

package module
v0.0.0-...-ebc0998 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Done = errors.New("wordpress: no more rows to read")
)

Functions

func GetCategoryIdBySlug

func GetCategoryIdBySlug(c context.Context, slug string) (int64, error)

GetCategoryIdBySlug returns the id of the category that matches the given slug

func GetOption

func GetOption(c context.Context, name string) (string, error)

GetOption returns the string value of the WordPress option

func GetTagIdBySlug

func GetTagIdBySlug(c context.Context, slug string) (int64, error)

GetTagIdBySlug returns the id of the category that matches the given slug

func NewContext

func NewContext(parent context.Context, wp *WordPress) context.Context

NewContext returns a derived context containing the database connection

Types

type Attachment

type Attachment struct {
	Object

	FileName string `json:"file_name"`

	Width  int `json:"width,omitempty"`
	Height int `json:"height,omitempty"`

	Caption string `json:"caption"`
	AltText string `json:"alt_text"`

	Url string `json:"url,omitempty"`
}

Attachment represents a WordPress attachment

func GetAttachments

func GetAttachments(c context.Context, attachmentIds ...int64) ([]*Attachment, error)

GetAttachments gets all attachment data from the database

type Category

type Category struct {
	Term

	Link string `json:"url"`
}

Category represents a WordPress category

func GetCategories

func GetCategories(c context.Context, categoryIds ...int64) ([]*Category, error)

GetCategories gets all category data from the database

func (*Category) GetChildId

func (cat *Category) GetChildId(c context.Context, slug string) (int64, error)

GetChildId returns the category id of the child looked up by it's slug

func (*Category) GetChildrenIds

func (cat *Category) GetChildrenIds(c context.Context) ([]int64, error)

GetChildrenIds returns all the ids of the category and it's children

func (*Category) MarshalJSON

func (cat *Category) MarshalJSON() ([]byte, error)

MarshalJSON marshals itself into json

type Iterator

type Iterator interface {
	Next() (int64, error)
	Cursor() string
	Slice() ([]int64, error)
}

func QueryAttachments

func QueryAttachments(c context.Context, opts *ObjectQueryOptions) (Iterator, error)

QueryAttachments returns the ids of the attachments that match the query

func QueryPosts

func QueryPosts(c context.Context, opts *ObjectQueryOptions) (Iterator, error)

QueryPosts returns the ids of the posts that match the query

func QueryUsers

func QueryUsers(c context.Context, opts *UserQueryOptions) (Iterator, error)

QueryUsers returns the ids of the users that match the query

type MenuItem struct {
	Id       int64 `json:"id"`
	ParentId int64 `json:"-"`

	Order int `json:"-"`

	Title string `json:"title"`
	Link  string `json:"url"`

	Attr    string `json:"attrs,omitempty"`
	Classes string `json:"classes,omitempty"`
	Target  string `json:"target,omitempty"`

	ObjectId int64  `json:"object_id"`
	Object   string `json:"object"`

	Type MenuItemType `json:"type"`

	Xfn string `json:"xfn,omitempty"`

	Children []*MenuItem `json:"children,omitempty"`
}

MenuItem represents a WordPress menu item

func GetMenuItems

func GetMenuItems(c context.Context, opts *ObjectQueryOptions) ([]*MenuItem, error)

GetMenuItems gets the entire menu hierarchy

It is also the most expensive operation in this package... use sparingly...

type MenuItemList []*MenuItem

MenuItemList is used for sorting menu items

func (mis MenuItemList) Count() int
func (mis MenuItemList) Len() int

Len is the number of elements in the collection.

func (mis MenuItemList) Less(i, j int) bool

Less reports whether the element with index i should sort before the element with index j.

func (mis MenuItemList) Swap(i, j int)

Swap swaps the elements with indexes i and j.

type MenuItemType string

MenuItemType represents menu item link types

i.e. post_type, taxonomy, custom

const (
	// MenuItemTypePost is a link to a specific post or page
	MenuItemTypePost MenuItemType = "post_type"

	// MenuItemTypeTaxonomy is a link to a category or post tag
	MenuItemTypeTaxonomy MenuItemType = "taxonomy"

	// MenuItemTypeCustom is a custom or external link
	MenuItemTypeCustom MenuItemType = "custom"
)
type MenuLocation struct {
	Id   int64  `json:"id"`
	Name string `json:"name"`
	Slug string `json:"slug"`
}

MenuLocation represents a WordPress menu location

func GetMenus

func GetMenus(c context.Context) ([]*MenuLocation, error)

GetMenus gets the available menus from the database

type MissingResourcesError

type MissingResourcesError []int64

func (MissingResourcesError) Error

func (ids MissingResourcesError) Error() string

type Object

type Object struct {
	// The post's ID
	Id int64 `json:"id"`

	// The post author's ID
	AuthorId int64 `json:"author"`

	// The post's local publication time.
	Date time.Time `json:"date"`

	// The post's GMT publication time.
	DateGmt time.Time `json:"-"`

	// The post's content.
	Content string `json:"content"`

	// The post's title.
	Title string `json:"title"`

	// The post's excerpt.
	Excerpt string `json:"excerpt"`

	// The post's status.
	Status PostStatus `json:"status"`

	// Whether comments are allowed.
	CommentStatus bool `json:"comment_status"`

	// Whether pings are allowed.
	PingStatus bool `json:"ping_status"`

	// The post's password in plain text.
	Password string `json:"-"`

	// The post's slug.
	Name string `json:"slug"`

	// URLs queued to be pinged.
	ToPing URLList `json:"-"`

	// URLs that have been pinged.
	Pinged URLList `json:"-"`

	// The post's local modified time.
	Modified time.Time `json:"modified"`

	// The post's GMT modified time.
	ModifiedGmt time.Time `json:"-"`

	// A utility field for post content.
	ContentFiltered string `json:"-"`

	// The post's parent post.
	ParentId int `json:"parent"`

	// The post's unique identifier, not necessarily a URL, used as the feed GUID.
	Guid string `json:"-"`

	// A field used for ordering posts.
	MenuOrder int `json:"-"`

	// The post's type. (i.e. post or page)
	Type string `json:"type"`

	// An attachment's mime type.
	MimeType string `json:"mime_type,omitempty"`

	// Cached comment count.
	CommentCount int `json:"-"`
}

Object represents a WordPress 'post' object

Not really a Post object, per se, since WP uses it for other things like pages and menu items. However it's in the 'posts' table, so... whatever...

func (*Object) GetMeta

func (obj *Object) GetMeta(c context.Context, keys ...string) (map[string]string, error)

GetMeta gets the object's metadata from the database

Returns all metadata if no metadata keys are given

func (*Object) GetTaxonomy

func (obj *Object) GetTaxonomy(c context.Context, taxonomy ...Taxonomy) (Iterator, error)

GetTaxonomy gets all term ids related to the object whose taxonomies match any of the given taxonomies

i.e. `GetTaxonomy("category")` will return all of the object's related categories

type ObjectQueryOptions

type ObjectQueryOptions struct {
	After string `param:"after"`
	Limit int    `param:"limit"`

	Order          string `param:"order_by"`
	OrderAscending bool   `param:"order_asc"`

	PostType   PostType   `param:"post_type"`
	PostStatus PostStatus `param:"post_status"`

	Author      int64   `param:"author_id"`
	AuthorIn    []int64 `param:"author_id__in"`
	AuthorNotIn []int64 `param:"author_id__not_in"`

	AuthorName      string   `param:"author_name"`
	AuthorNameIn    []string `param:"author_name__in"`
	AuthorNameNotIn []string `param:"author_name__not_in"`

	Category      int64   `param:"category_id"`
	CategoryAnd   []int64 `param:"category_id__and"`
	CategoryIn    []int64 `param:"category_id__in"`
	CategoryNotIn []int64 `param:"category_id__not_in"`

	CategoryName      string   `param:"category_name"`
	CategoryNameAnd   []string `param:"category_name__and"`
	CategoryNameIn    []string `param:"category_name__in"`
	CategoryNameNotIn []string `param:"category_name__not_in"`

	MenuId      int64   `param:"menu_id"`
	MenuIdAnd   []int64 `param:"menu_id__and_in"`
	MenuIdIn    []int64 `param:"menu_id__in"`
	MenuIdNotIn []int64 `param:"menu_id__not_in"`

	MenuName      string   `param:"menu_name"`
	MenuNameAnd   []string `param:"menu_name__and"`
	MenuNameIn    []string `param:"menu_name__in"`
	MenuNameNotIn []string `param:"menu_name__not_in"`

	Meta      string   `param:"meta"`
	MetaAnd   []string `param:"meta__and"`
	MetaIn    []string `param:"meta__in"`
	MetaNotIn []string `param:"meta__not_in"`

	Name      string   `param:"post_name"`
	NameIn    []string `param:"post_name__in"`
	NameNotIn []string `param:"post_name__not_in"`

	Parent      int64   `param:"post_parent"`
	ParentIn    []int64 `param:"post_parent__in"`
	ParentNotIn []int64 `param:"post_parent__not_in"`

	Post      int64   `param:"post_id"`
	PostIn    []int64 `param:"post_id__in"`
	PostNotIn []int64 `param:"post_id__not_in"`

	TagId      int64   `param:"tag_id"`
	TagIdAnd   []int64 `param:"tag_id__and"`
	TagIdIn    []int64 `param:"tag_id__in"`
	TagIdNotIn []int64 `param:"tag_id__not_in"`

	TagName      string   `param:"tag_name"`
	TagNameAnd   []string `param:"tag_name__and"`
	TagNameIn    []string `param:"tag_name__in"`
	TagNameNotIn []string `param:"tag_name__not_in"`

	Query string `param:"q"`

	Day   int `param:"day_of_month"`
	Month int `param:"month_num"`
	Year  int `param:"year"`

	AfterDate time.Time
}

ObjectQueryOptions represents the available parameters for querying

Somewhat similar to WP's json plugin

type Post

type Post struct {
	Object

	// The post's featured_media
	FeaturedMediaId int64 `json:"featured_media,omitempty"`

	// The post's categories
	CategoryIds []int64 `json:"categories"`

	// The post's tags
	TagIds []int64 `json:"tags"`

	// The post's metadata
	Meta map[string]string `json:"meta"`
}

Post represents a WordPress post

func GetPosts

func GetPosts(c context.Context, postIds ...int64) ([]*Post, error)

GetPosts gets all post data from the database

type PostStatus

type PostStatus string

PostStatus represents a WordPress post status

Posts in WordPress can have one of a number of statuses.

The status of a given post determines how WordPress handles that post.

const (
	// PostStatusPublish is a published post
	PostStatusPublish PostStatus = "publish"

	// PostStatusFuture is a post which has been
	// published but it's publish date is in the future
	PostStatusFuture PostStatus = "future"

	// PostStatusDraft is a draft post
	PostStatusDraft PostStatus = "draft"

	// PostStatusPending is a post which is awaiting approval
	PostStatusPending PostStatus = "pending"

	// PostStatusPrivate is a private post
	PostStatusPrivate PostStatus = "private"

	// PostStatusTrash is a post that was trashed
	PostStatusTrash PostStatus = "trash"

	// PostStatusAutoDraft is an auto-saved post
	PostStatusAutoDraft PostStatus = "auto-draft"

	// PostStatusInherit inherits its status from its parent
	PostStatusInherit PostStatus = "inherit"
)

func (PostStatus) Scan

func (s PostStatus) Scan(src interface{}) error

Scan formats incoming data from a sql database

type PostType

type PostType string

PostType represents a WordPress post type

There are five post types that are readily available to users or internally used by the WordPress installation by default:

Post, Page, Attachment, Revision, Navigation Menu Item

https://codex.wordpress.org/Post_Types

const (
	// PostTypeAttachment is an attachment
	PostTypeAttachment PostType = "attachment"

	// PostTypeNavMenuItem is a menu item
	PostTypeNavMenuItem PostType = "nav_menu_item"

	// PostTypePage is a page
	PostTypePage PostType = "page"

	// PostTypePost is a post
	PostTypePost PostType = "post"

	// PostTypeRevision is a revision
	PostTypeRevision PostType = "revision"
)

func (PostType) Scan

func (t PostType) Scan(src interface{}) error

Scan formats incoming data from a sql database

type Tag

type Tag struct {
	Term

	Link string `json:"url"`
}

Tag represents a WordPress tag

func GetTags

func GetTags(c context.Context, tagIds ...int64) ([]*Tag, error)

GetTags gets all tag data from the database

func (*Tag) MarshalJSON

func (tag *Tag) MarshalJSON() ([]byte, error)

MarshalJSON marshals itself into json

type Taxonomy

type Taxonomy string

Taxonomy represents term taxonomy names

i.e. categories, nav_menu, post_tag

const (
	// TaxonomyCategory is for post categories
	TaxonomyCategory Taxonomy = "category"

	// TaxonomyNavMenu is for menu locations
	TaxonomyNavMenu Taxonomy = "nav_menu"

	// TaxonomyPostTag is for post tags
	TaxonomyPostTag Taxonomy = "post_tag"
)

type Term

type Term struct {
	// Term ID.
	Id int64 `json:"id"`

	// The term's name.
	Name string `json:"name"`

	// The term's slug.
	Slug string `json:"slug"`

	// The term's term_group.
	Group int64 `json:"group"`

	// Term Taxonomy ID.
	TaxonomyId int64 `json:"-"`

	// The term's taxonomy name.
	Taxonomy string `json:"taxonomy"`

	// The term's description.
	Description string `json:"description"`

	// ID of a term's parent term.
	Parent int64 `json:"parent"`

	// Cached object count for this term.
	Count int64 `json:"-"`
}

Term represents a WordPress term

type TermQueryOptions

type TermQueryOptions struct {
	After string `param:"after"`
	Limit int    `param:"limit"`

	Id      int64   `param:"term_id"`
	IdIn    []int64 `param:"term_id__in"`
	IdNotIn []int64 `param:"term_id__not_in"`

	Name      string   `param:"term_name"`
	NameIn    []string `param:"term_name__in"`
	NameNotIn []string `param:"term_name__not_in"`

	ObjectId      int64   `param:"object_id"`
	ObjectIdIn    []int64 `param:"object_id__in"`
	ObjectIdNotIn []int64 `param:"object_id__not_in"`

	ParentId      int64   `param:"parent_id"`
	ParentIdIn    []int64 `param:"parent_id__in"`
	ParentIdNotIn []int64 `param:"parent_id__not_in"`

	Slug      string   `param:"term_slug"`
	SlugIn    []string `param:"term_slug__in"`
	SlugNotIn []string `param:"term_slug__not_in"`

	Taxonomy      Taxonomy   `param:"taxonomy"`
	TaxonomyIn    []Taxonomy `param:"taxonomy__in"`
	TaxonomyNotIn []Taxonomy `param:"taxonomy__not_in"`
}

TermQueryOptions represents the available parameters for querying

type URLList

type URLList []string

URLList represents a list of urls

This is just a helper to split incoming space-separated values from the database

Used for pinged and to-ping urls

func (URLList) Scan

func (list URLList) Scan(src interface{}) error

Scan formats incoming data from a sql database

type User

type User struct {
	Id   int64  `json:"id"`
	Slug string `json:"slug"`

	Name        string `json:"name"`
	Description string `json:"description"`

	Email    string `json:"-"` // don't leak my email info!! >:[
	Gravatar string `json:"gravatar"`
	Website  string `json:"url"`

	Registered time.Time `json:"-"`
}

User represents a WordPress user

func GetUsers

func GetUsers(c context.Context, userIds ...int64) ([]*User, error)

GetUsers gets all user data from the database

type UserQueryOptions

type UserQueryOptions struct {
	After string `param:"after"`
	Limit int    `param:"limit"`

	Id      int64   `param:"user_id"`
	IdIn    []int64 `param:"user_id__in"`
	IdNotIn []int64 `param:"user_id__not_in"`

	Slug      string   `param:"slug"`
	SlugIn    []string `param:"slug__in"`
	SlugNotIn []string `param:"slug__not_in"`
}

UserQueryOptions represents the available parameters for querying

type WordPress

type WordPress struct {
	TablePrefix string
	// contains filtered or unexported fields
}

WordPress represents access to the WordPress database

func New

func New(host, user, password, database string) (*WordPress, error)

New creates and returns a new WordPress connection

func (*WordPress) Close

func (wp *WordPress) Close() error

Close closes the connection to the database

Not very useful when the sql package is designed to have long lived connections

func (*WordPress) SetMaxOpenConns

func (wp *WordPress) SetMaxOpenConns(n int)

SetMaxOpenConns sets the max number open connections

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL