Merge pull request 'fix(vip): join REST query with & under plain permalinks' (#280) from worktree-fix-theme-toggle-overlap into main
Reviewed-on: CYL/wordpress-theme-aksrt#280
This commit is contained in:
+54
-7
@@ -11,10 +11,44 @@
|
||||
if (window.location.protocol === "https:" && siteUrl.substring(0, 5) === "http:") {
|
||||
siteUrl = "https:" + siteUrl.substring(5);
|
||||
}
|
||||
if (aksrt.restUrl) {
|
||||
return String(aksrt.restUrl).replace(/\/$/, "");
|
||||
var raw = aksrt.apiUrl || aksrt.restUrl || aksrt.rest_url || "";
|
||||
if (raw) {
|
||||
return String(raw).replace(/\/$/, "");
|
||||
}
|
||||
return siteUrl.replace(/\/$/, "") + "/wp-json/aksrt/v1";
|
||||
return siteUrl.replace(/\/$/, "") + "/index.php?rest_route=/aksrt/v1";
|
||||
}
|
||||
|
||||
/**
|
||||
* Join REST root + path [+ query].
|
||||
* Pretty: /wp-json/aksrt/v1/pay/vip/modal?vip_level=1
|
||||
* Plain: index.php?rest_route=/aksrt/v1/pay/vip/modal&vip_level=1
|
||||
* Never put a second "?" after rest_route= (that 404s).
|
||||
*/
|
||||
function restUrl(path, query) {
|
||||
path = String(path || "");
|
||||
if (path.charAt(0) !== "/") path = "/" + path;
|
||||
var base = restBase();
|
||||
var qstr = "";
|
||||
if (query && typeof query === "object") {
|
||||
var parts = [];
|
||||
Object.keys(query).forEach(function (k) {
|
||||
if (query[k] === undefined || query[k] === null) return;
|
||||
parts.push(encodeURIComponent(k) + "=" + encodeURIComponent(String(query[k])));
|
||||
});
|
||||
qstr = parts.join("&");
|
||||
} else if (typeof query === "string" && query) {
|
||||
qstr = query.replace(/^\?/, "");
|
||||
}
|
||||
|
||||
if (base.indexOf("rest_route=") !== -1) {
|
||||
var url = base + path;
|
||||
if (qstr) url += "&" + qstr;
|
||||
return url;
|
||||
}
|
||||
|
||||
var pretty = base + path;
|
||||
if (qstr) pretty += (pretty.indexOf("?") >= 0 ? "&" : "?") + qstr;
|
||||
return pretty;
|
||||
}
|
||||
|
||||
function nonceHeaders() {
|
||||
@@ -24,14 +58,28 @@
|
||||
}
|
||||
|
||||
function api(path, options) {
|
||||
var opts = Object.assign({}, options || {});
|
||||
var query = opts.query;
|
||||
if (Object.prototype.hasOwnProperty.call(opts, "query")) {
|
||||
delete opts.query;
|
||||
}
|
||||
var onlyPath = String(path || "");
|
||||
var inlineQ = null;
|
||||
var qi = onlyPath.indexOf("?");
|
||||
if (qi >= 0) {
|
||||
inlineQ = onlyPath.slice(qi + 1);
|
||||
onlyPath = onlyPath.slice(0, qi);
|
||||
}
|
||||
var url = restUrl(onlyPath, query || inlineQ);
|
||||
|
||||
var merged = Object.assign(
|
||||
{
|
||||
credentials: "same-origin",
|
||||
headers: Object.assign({ "Content-Type": "application/json" }, nonceHeaders()),
|
||||
},
|
||||
options || {}
|
||||
opts
|
||||
);
|
||||
return fetch(restBase() + path, merged).then(function (r) {
|
||||
return fetch(url, merged).then(function (r) {
|
||||
return r
|
||||
.json()
|
||||
.catch(function () {
|
||||
@@ -42,7 +90,6 @@
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function ensureModalShell() {
|
||||
var el = document.getElementById("modal_pay_uservip");
|
||||
if (el) return el;
|
||||
@@ -210,7 +257,7 @@
|
||||
if (body) body.innerHTML = '<div class="text-center padding-20 muted-2-color">加载中…</div>';
|
||||
openModal();
|
||||
|
||||
api("/pay/vip/modal?vip_level=" + encodeURIComponent(String(level)))
|
||||
api("/pay/vip/modal", { query: { vip_level: level } })
|
||||
.then(function (r) {
|
||||
loading = false;
|
||||
if (r.status === 401) {
|
||||
|
||||
Reference in New Issue
Block a user