2023-01-18 15:04:12 +00:00
|
|
|
package plugins
|
|
|
|
|
2023-01-19 14:02:45 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-20 14:30:55 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
2023-01-21 11:31:23 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-12-23 12:17:42 +00:00
|
|
|
"net/url"
|
2023-01-19 14:02:45 +00:00
|
|
|
"regexp"
|
2024-01-13 13:15:54 +00:00
|
|
|
"strconv"
|
2023-01-19 14:02:45 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2023-01-18 15:04:12 +00:00
|
|
|
|
|
|
|
type PageEle struct {
|
|
|
|
PrevEle string
|
|
|
|
NextEle string
|
|
|
|
DotsEle string
|
|
|
|
MiddleEle string
|
|
|
|
CurrentEle string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TwentyFifteenPagination() PageEle {
|
|
|
|
return twentyFifteen
|
|
|
|
}
|
2023-12-20 14:30:55 +00:00
|
|
|
func TwentyFifteenCommentPagination() CommentPageEle {
|
|
|
|
return twentyFifteenComment
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommentPageEle struct {
|
|
|
|
PageEle
|
|
|
|
}
|
2023-01-18 15:04:12 +00:00
|
|
|
|
|
|
|
var twentyFifteen = PageEle{
|
|
|
|
PrevEle: `<a class="prev page-numbers" href="%s">上一页</a>`,
|
|
|
|
NextEle: `<a class="next page-numbers" href="%s">下一页</a>`,
|
|
|
|
DotsEle: `<span class="page-numbers dots">…</span>`,
|
|
|
|
MiddleEle: `<a class="page-numbers" href="%s"><span class="meta-nav screen-reader-text">页 </span>%d</a>
|
|
|
|
`,
|
|
|
|
CurrentEle: `<span aria-current="page" class="page-numbers current">
|
|
|
|
<span class="meta-nav screen-reader-text">页 </span>%d</span>`,
|
|
|
|
}
|
2023-12-20 14:30:55 +00:00
|
|
|
var twentyFifteenComment = CommentPageEle{
|
|
|
|
PageEle{
|
2023-12-23 14:49:34 +00:00
|
|
|
PrevEle: `<div class="nav-previous"><a href="%s">%s</a></div>`,
|
|
|
|
NextEle: `<div class="nav-next"><a href="%s">%s</a></div>`,
|
2023-12-20 14:30:55 +00:00
|
|
|
},
|
|
|
|
}
|
2023-01-18 15:04:12 +00:00
|
|
|
|
2023-02-14 15:44:48 +00:00
|
|
|
func (p PageEle) Current(page, totalPage, totalRow int) string {
|
2023-01-18 15:04:12 +00:00
|
|
|
return fmt.Sprintf(p.CurrentEle, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PageEle) Prev(url string) string {
|
|
|
|
return fmt.Sprintf(p.PrevEle, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PageEle) Next(url string) string {
|
|
|
|
return fmt.Sprintf(p.NextEle, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PageEle) Dots() string {
|
|
|
|
return p.DotsEle
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PageEle) Middle(page int, url string) string {
|
|
|
|
return fmt.Sprintf(p.MiddleEle, url, page)
|
|
|
|
}
|
2023-01-19 14:02:45 +00:00
|
|
|
|
|
|
|
var reg = regexp.MustCompile(`(/page)/(\d+)`)
|
2023-12-20 14:30:55 +00:00
|
|
|
var commentReg = regexp.MustCompile(`/comment-page-(\d+)`)
|
2023-01-19 14:02:45 +00:00
|
|
|
|
2024-01-13 13:15:54 +00:00
|
|
|
var queryParam = []string{"paged", "cat", "m", "author", "tag"}
|
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
func (p PageEle) Urls(u url.URL, page int, isTLS bool) string {
|
|
|
|
var path, query = u.Path, u.RawQuery
|
2024-01-13 13:15:54 +00:00
|
|
|
if path == "/" {
|
|
|
|
v := u.Query()
|
|
|
|
for _, q := range queryParam {
|
|
|
|
if v.Get(q) != "" {
|
|
|
|
v.Set("paged", strconv.Itoa(page))
|
|
|
|
return str.Join(path, "?", v.Encode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:02:45 +00:00
|
|
|
if !strings.Contains(path, "/page/") {
|
|
|
|
path = fmt.Sprintf("%s%s", path, "/page/1")
|
|
|
|
}
|
|
|
|
if page == 1 {
|
|
|
|
path = reg.ReplaceAllString(path, "")
|
|
|
|
} else {
|
|
|
|
s := fmt.Sprintf("$1/%d", page)
|
|
|
|
path = reg.ReplaceAllString(path, s)
|
|
|
|
}
|
|
|
|
path = strings.Replace(path, "//", "/", -1)
|
|
|
|
if path == "" {
|
|
|
|
path = "/"
|
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
if query != "" {
|
|
|
|
return str.Join(path, "?", query)
|
|
|
|
}
|
|
|
|
return path
|
2023-01-19 14:02:45 +00:00
|
|
|
}
|
2023-12-20 14:30:55 +00:00
|
|
|
|
2023-12-23 12:17:42 +00:00
|
|
|
func (p CommentPageEle) Urls(u url.URL, page int, isTLS bool) string {
|
|
|
|
var path, query = u.Path, u.RawQuery
|
2024-01-13 13:15:54 +00:00
|
|
|
if path == "/" {
|
|
|
|
v := u.Query()
|
|
|
|
if v.Get("p") != "" {
|
|
|
|
v.Set("cpage", strconv.Itoa(page))
|
|
|
|
return str.Join(path, "?", v.Encode(), "#comments")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 14:30:55 +00:00
|
|
|
if !strings.Contains(path, "/comment-page-") {
|
2024-01-13 13:15:54 +00:00
|
|
|
path = fmt.Sprintf("%s%s", path, "/comment-page-1")
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
2023-12-23 16:20:35 +00:00
|
|
|
path = commentReg.ReplaceAllString(path, fmt.Sprintf("/comment-page-%d", page))
|
2023-12-20 14:30:55 +00:00
|
|
|
path = strings.Replace(path, "//", "/", -1)
|
2024-01-13 13:15:54 +00:00
|
|
|
ur := path
|
|
|
|
if query != "" {
|
|
|
|
ur = str.Join(path, "?", query)
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
2024-01-13 13:15:54 +00:00
|
|
|
ur = str.Join(ur, "#comments")
|
2023-12-23 16:20:35 +00:00
|
|
|
return ur
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p CommentPageEle) Middle(page int, url string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
func (p CommentPageEle) Dots() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
func (p CommentPageEle) Current(page, totalPage, totalRow int) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
func (p CommentPageEle) Prev(url string) string {
|
|
|
|
return fmt.Sprintf(p.PrevEle, url, helper.Or(wpconfig.GetOption("comment_order") == "asc", "较早评论", "较新评论"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p CommentPageEle) Next(url string) string {
|
|
|
|
return fmt.Sprintf(p.NextEle, url, helper.Or(wpconfig.GetOption("comment_order") == "asc", "较新评论", "较早评论"))
|
|
|
|
}
|
2023-12-23 12:17:42 +00:00
|
|
|
|
|
|
|
type PaginationNav struct {
|
|
|
|
Currents func(page, totalPage, totalRows int) string
|
|
|
|
Prevs func(url string) string
|
|
|
|
Nexts func(url string) string
|
|
|
|
Dotss func() string
|
|
|
|
Middles func(page int, url string) string
|
|
|
|
Urlss func(u url.URL, page int, isTLS bool) string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PaginationNav) Current(page, totalPage, totalRows int) string {
|
|
|
|
return p.Currents(page, totalPage, totalRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PaginationNav) Prev(url string) string {
|
|
|
|
return p.Prevs(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PaginationNav) Next(url string) string {
|
|
|
|
return p.Nexts(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PaginationNav) Dots() string {
|
|
|
|
return p.Dotss()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PaginationNav) Middle(page int, url string) string {
|
|
|
|
return p.Middles(page, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PaginationNav) Urls(u url.URL, page int, isTLS bool) string {
|
|
|
|
return p.Urlss(u, page, isTLS)
|
|
|
|
}
|