feat: publisher, local business and home crumb settings
- Publisher and Local business tabs with the C20 refusals (sameAs lines, person as local business, address, times, country, coordinates) - the publisher node from the settings, as an Organization, a Person or the chosen local business type with address, geo and opening hours - P8 publisher preview with Google's required and recommended findings (classes/schema Validate) - the home crumb name on the General tab; every new key in the settings API and seo_update_settings
This commit is contained in:
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -40,8 +41,28 @@ const (
|
||||
errVerifyFilenameTaken = "golem15.seo::lang.settings.verify_filename_taken"
|
||||
errTemplateUnknown = "golem15.seo::lang.settings.template_unknown"
|
||||
errDomainInvalid = "golem15.seo::lang.settings.domain_invalid"
|
||||
errValueInvalid = "golem15.seo::lang.api.value_invalid"
|
||||
errSameAsInvalid = "golem15.seo::lang.settings.same_as_invalid"
|
||||
errURLInvalid = "golem15.seo::lang.settings.url_invalid"
|
||||
errEmailInvalid = "golem15.seo::lang.settings.email_invalid"
|
||||
errLocalPerson = "golem15.seo::lang.settings.local_person"
|
||||
errLocalAddress = "golem15.seo::lang.settings.local_address_required"
|
||||
errTimeInvalid = "golem15.seo::lang.settings.time_invalid"
|
||||
errHoursOrder = "golem15.seo::lang.settings.hours_order"
|
||||
errCountryInvalid = "golem15.seo::lang.settings.country_invalid"
|
||||
errCoordinateInvalid = "golem15.seo::lang.settings.coordinate_invalid"
|
||||
)
|
||||
|
||||
// LocalTypes are the LocalBusiness types the settings offer (UI-SPEC C20).
|
||||
var LocalTypes = []string{
|
||||
"LocalBusiness", "ProfessionalService", "Store", "FoodEstablishment",
|
||||
"HealthAndBeautyBusiness", "MedicalBusiness", "LegalService",
|
||||
"HomeAndConstructionBusiness", "AutomotiveBusiness", "FinancialService",
|
||||
}
|
||||
|
||||
// Days are the day codes of the opening hours, Monday first.
|
||||
var Days = []string{"mon", "tue", "wed", "thu", "fri", "sat", "sun"}
|
||||
|
||||
// QueryParam is one row of the D-07 query-parameter list: a name (a
|
||||
// trailing * matches a prefix), its kind, and an optional path pattern the
|
||||
// row is limited to.
|
||||
@@ -174,7 +195,12 @@ func (Settings) Fillable() []string {
|
||||
"noindex_paths", "search_paths", "url_host", "url_slash",
|
||||
"url_lowercase", "verify_google", "verify_bing", "verify_yandex",
|
||||
"verify_other", "verify_files", "links_new_tab", "nofollow_domains",
|
||||
"sponsored_domains",
|
||||
"sponsored_domains", "breadcrumb_home", "publisher_type",
|
||||
"publisher_name", "publisher_logo", "publisher_url", "publisher_email",
|
||||
"publisher_phone", "publisher_same_as", "local_enabled", "local_type",
|
||||
"local_price_range", "local_street", "local_postal_code", "local_city",
|
||||
"local_region", "local_country", "local_latitude", "local_longitude",
|
||||
"local_hours",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,6 +304,108 @@ func (s Settings) Problems() map[string][]Problem {
|
||||
out[field] = p
|
||||
}
|
||||
}
|
||||
for field, list := range s.PublisherProblems() {
|
||||
out[field] = append(out[field], list...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// timePattern is a 24-hour HH:MM time.
|
||||
var timePattern = regexp.MustCompile(`^([01][0-9]|2[0-3]):[0-5][0-9]$`)
|
||||
|
||||
// countryPattern is a two-letter country code.
|
||||
var countryPattern = regexp.MustCompile(`^[A-Za-z]{2}$`)
|
||||
|
||||
// HTTPSURL reports whether s is an absolute https URL with a host.
|
||||
func HTTPSURL(s string) bool {
|
||||
u, err := url.Parse(strings.TrimSpace(s))
|
||||
return err == nil && u.Scheme == "https" && u.Host != "" && !strings.ContainsAny(s, " \t\n<>\"")
|
||||
}
|
||||
|
||||
// oneOfList reports whether v is one of list.
|
||||
func oneOfList(v string, list []string) bool {
|
||||
for _, item := range list {
|
||||
if v == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PublisherProblems lists the refusals of the Publisher and Local
|
||||
// business tabs (UI-SPEC C20, D-45, T-33-25): the publisher type, https
|
||||
// sameAs lines (with the line number), the website, the email, a person
|
||||
// as a local business, the business's address, country, coordinates and
|
||||
// opening hours (local_hours.<i>.opens / .closes).
|
||||
func (s Settings) PublisherProblems() map[string][]Problem {
|
||||
out := map[string][]Problem{}
|
||||
add := func(field string, p Problem) { out[field] = append(out[field], p) }
|
||||
if s.PublisherType != "" && !oneOfList(s.PublisherType, []string{PublisherOrganization, PublisherPerson}) {
|
||||
add("publisher_type", Problem{Key: errValueInvalid})
|
||||
}
|
||||
n := 0
|
||||
for _, line := range strings.Split(strings.ReplaceAll(s.PublisherSameAs, "\r\n", "\n"), "\n") {
|
||||
n++
|
||||
if v := strings.TrimSpace(line); v != "" && !HTTPSURL(v) {
|
||||
add("publisher_same_as", Problem{Key: errSameAsInvalid, Params: map[string]string{"line": strconv.Itoa(n)}})
|
||||
break
|
||||
}
|
||||
}
|
||||
if v := strings.TrimSpace(s.PublisherURL); v != "" && !HTTPSURL(v) {
|
||||
add("publisher_url", Problem{Key: errURLInvalid})
|
||||
}
|
||||
if v := strings.TrimSpace(s.PublisherEmail); v != "" {
|
||||
local, domain, ok := strings.Cut(v, "@")
|
||||
if !ok || local == "" || !strings.Contains(domain, ".") || strings.ContainsAny(v, " <>\"") {
|
||||
add("publisher_email", Problem{Key: errEmailInvalid})
|
||||
}
|
||||
}
|
||||
if !s.LocalEnabled {
|
||||
return out
|
||||
}
|
||||
if s.PublisherType == PublisherPerson {
|
||||
add("local_enabled", Problem{Key: errLocalPerson})
|
||||
}
|
||||
if s.LocalType != "" && !oneOfList(s.LocalType, LocalTypes) {
|
||||
add("local_type", Problem{Key: errValueInvalid})
|
||||
}
|
||||
if strings.TrimSpace(s.LocalStreet) == "" || strings.TrimSpace(s.LocalPostalCode) == "" || strings.TrimSpace(s.LocalCity) == "" {
|
||||
for field, v := range map[string]string{"local_street": s.LocalStreet, "local_postal_code": s.LocalPostalCode, "local_city": s.LocalCity} {
|
||||
if strings.TrimSpace(v) == "" {
|
||||
add(field, Problem{Key: errLocalAddress})
|
||||
}
|
||||
}
|
||||
}
|
||||
if v := strings.TrimSpace(s.LocalCountry); v != "" && !countryPattern.MatchString(v) {
|
||||
add("local_country", Problem{Key: errCountryInvalid})
|
||||
}
|
||||
coordinate := func(field, v string, limit float64) {
|
||||
if v = strings.TrimSpace(v); v == "" {
|
||||
return
|
||||
}
|
||||
f, err := strconv.ParseFloat(strings.ReplaceAll(v, ",", "."), 64)
|
||||
if err != nil || f < -limit || f > limit {
|
||||
add(field, Problem{Key: errCoordinateInvalid})
|
||||
}
|
||||
}
|
||||
coordinate("local_latitude", s.LocalLatitude, 90)
|
||||
coordinate("local_longitude", s.LocalLongitude, 180)
|
||||
for i, h := range s.Hours() {
|
||||
base := fmt.Sprintf("local_hours.%d.", i)
|
||||
if !oneOfList(h.Day, Days) {
|
||||
add(base+"day", Problem{Key: errValueInvalid})
|
||||
}
|
||||
okOpens, okCloses := timePattern.MatchString(h.Opens), timePattern.MatchString(h.Closes)
|
||||
if !okOpens {
|
||||
add(base+"opens", Problem{Key: errTimeInvalid})
|
||||
}
|
||||
if !okCloses {
|
||||
add(base+"closes", Problem{Key: errTimeInvalid})
|
||||
}
|
||||
if okOpens && okCloses && h.Closes <= h.Opens {
|
||||
add(base+"closes", Problem{Key: errHoursOrder})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user