Skip to content

Instantly share code, notes, and snippets.

@sandeepwisetr
Created June 9, 2017 13:14
Show Gist options
  • Save sandeepwisetr/12207e09493a03b8682c05cb3c5b63af to your computer and use it in GitHub Desktop.
Save sandeepwisetr/12207e09493a03b8682c05cb3c5b63af to your computer and use it in GitHub Desktop.
var WPA = {
send_ajax: function (t, n, z) {
var cel;
if (typeof $ == 'undefined' || $ == "undefined") {
var $ = jQuery;
}
var e = {}, i = "/wp-admin/admin-ajax.php", o = t.find(".spinner").length;
var met=(t.attr("method")=="post" || t.attr("method")=="get")?t.attr("method"):"post";
if (typeof z != "undefined") {
e = z;
}
if ("undefined" != typeof t.attr("action") && "" != t.attr("action") && (i = t.attr("action")), o > 0 && t.find(".spinner").css({
visibility: "visible"
}), t.length > 0 && t != '') {
var r, a;
cel=t;
t.find("input").each(function () {
var t = $(this).attr("name"),
n = $(this).attr("type");
if ("checkbox" == n || "radio" == n) {
var i = $(this).is(":checked");
console.log(i), 1 == i && (e[t] = !0);
if (1 == i) {
if (n == "checkbox") {
if ($(this).val() != "") {
if ($("input[name='" + t + "']").length > 0) {
var x = [];
$("input[name='" + t + "']").each(function () {
var c = $(this).is(":checked");
if (c == 1) {
if ($(this).val() != "") {
x.push($(this).val());
} else {
x.push(c);
}
}
});
if (x.length > 0) {
e[t] = x;
}
}
}
}
if (n == "radio") {
if ($(this).val() != "") {
e[t] = $(this).val();
}
}
}
} else
"button" != n && (a = $(this).val(), e[t] = a)
}), t.find("textarea").each(function () {
r = $(this).attr("name"), a = $(this).val(), e[r] = a
}), t.find("select").each(function () {
r = $(this).attr("name"), a = $(this).val(), e[r] = a
}), t.find("button").each(function () {
r = $(this).attr("name"), a = $(this).val(), e[r] = a
})
}
e.action = n, e = WPA.applyFilters("wpa_before_ajax", e), Object.keys(e).length > 0 && $.ajax(
WPA.applyFilters("wpa_before_requestData", {
url: i,
method: met,
data: e,
success: function (t, e, i) {
WPA.doAction("wpa_after_success", t, e, i, n,cel)
},
error: function (t, e, i) {
WPA.doAction("wpa_after_error", t, e, i, n,cel)
},
complete: function (e, i) {
o > 0 && t.find(".spinner").css({
visibility: "visible"
}), WPA.doAction("wpa_after_complete", t, e, i, n,cel)
}
}, n, t)
)
},
hooks: {action: {}, filter: {}}, addAction: function (t, n, e, i) {
WPA.addHook("action", t, n, e, i)
}, addFilter: function (t, n, e, i) {
WPA.addHook("filter", t, n, e, i)
}, doAction: function (t) {
WPA.doHook("action", t, arguments)
}, applyFilters: function (t) {
return WPA.doHook("filter", t, arguments)
}, removeAction: function (t, n) {
WPA.removeHook("action", t, n)
}, removeFilter: function (t, n, e) {
WPA.removeHook("filter", t, n, e)
}, addHook: function (t, n, e, i, o) {
void 0 == WPA.hooks[t][n] && (WPA.hooks[t][n] = []);
var r = WPA.hooks[t][n];
void 0 == o && (o = n + "_" + r.length), WPA.hooks[t][n].push({tag: o, callable: e, priority: i})
}, doHook: function (t, n, e) {
if (e = Array.prototype.slice.call(e, 1), void 0 != WPA.hooks[t][n]) {
var i, o = WPA.hooks[t][n];
o.sort(function (t, n) {
return t.priority - n.priority
});
for (var r = 0; r < o.length; r++)
i = o[r].callable, "function" != typeof i && (i = window[i]), "action" == t ? i.apply(null, e) : e[0] = i.apply(null, e)
}
return"filter" == t ? e[0] : void 0
}, removeHook: function (t, n, e, i) {
if (void 0 != WPA.hooks[t][n])
for (var o = WPA.hooks[t][n], r = o.length - 1; r >= 0; r--)
void 0 != i && i != o[r].tag || void 0 != e && e != o[r].priority || o.splice(r, 1)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment