| 1 |
"use strict"; |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
const FLAG = "kingAddonsSubmissionsBound"; |
| 5 |
|
| 6 |
/** |
| 7 |
* Bind admin submissions handlers once. |
| 8 |
* |
| 9 |
* @returns {void} |
| 10 |
*/ |
| 11 |
const bindOnce = () => { |
| 12 |
if (window[FLAG]) { |
| 13 |
return; |
| 14 |
} |
| 15 |
window[FLAG] = true; |
| 16 |
|
| 17 |
$(document).ready(function () { |
| 18 |
// Remove default "Add New" button on submissions list page. |
| 19 |
$(".page-title-action").remove(); |
| 20 |
|
| 21 |
/* -------------------- LIST TABLE (read/unread toggle) -------------------- */ |
| 22 |
$("body").on("click.kingAddonsSubmissions", ".column-read_status", function () { |
| 23 |
const $row = $(this).parent(); |
| 24 |
const rowId = $row.attr("id") || ""; |
| 25 |
const postId = rowId.replace("post-", ""); |
| 26 |
const currentText = ($(this).text() || "").trim(); |
| 27 |
const readStatus = currentText === "Read" ? "0" : "1"; |
| 28 |
|
| 29 |
const nonce = window.KingAddonsSubmissions ? window.KingAddonsSubmissions.nonce : ""; |
| 30 |
const ajaxurl = window.KingAddonsSubmissions ? window.KingAddonsSubmissions.ajaxurl : ""; |
| 31 |
if (!ajaxurl || !nonce || !postId) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$.post(ajaxurl, { |
| 36 |
action: "king_addons_submissions_update_read_status", |
| 37 |
post_id: postId, |
| 38 |
read_status: readStatus, |
| 39 |
nonce, |
| 40 |
}); |
| 41 |
}); |
| 42 |
|
| 43 |
/* -------------------- SINGLE SUBMISSION SCREEN -------------------- */ |
| 44 |
const $postForm = $("#post"); |
| 45 |
if ($postForm.length) { |
| 46 |
// Add hidden input to store changes (only once). |
| 47 |
if (!$("#king_addons_submission_changes").length) { |
| 48 |
$("<input>", { |
| 49 |
type: "hidden", |
| 50 |
id: "king_addons_submission_changes", |
| 51 |
name: "king_addons_submission_changes", |
| 52 |
}).appendTo($postForm); |
| 53 |
} |
| 54 |
|
| 55 |
let changes = {}; |
| 56 |
|
| 57 |
// Initially lock inputs. |
| 58 |
$postForm |
| 59 |
.find(".king-addons-submissions-wrap input, .king-addons-submissions-wrap textarea") |
| 60 |
.each(function () { |
| 61 |
const $el = $(this); |
| 62 |
if ($el.is('[type="checkbox"],[type="radio"]')) { |
| 63 |
$el.prop("disabled", true); |
| 64 |
} else { |
| 65 |
$el.prop("readonly", true); |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
// Track edits (scoped to the submissions UI). |
| 70 |
$postForm.off("change.kingAddonsSubmissions"); |
| 71 |
$postForm.on( |
| 72 |
"change.kingAddonsSubmissions", |
| 73 |
".king-addons-submissions-wrap input, .king-addons-submissions-wrap textarea", |
| 74 |
function () { |
| 75 |
const $field = $(this); |
| 76 |
let key = $field.attr("id") || ""; |
| 77 |
const value = []; |
| 78 |
|
| 79 |
if ($field.is('[type="checkbox"],[type="radio"]')) { |
| 80 |
value[0] = $field.attr("type"); |
| 81 |
value[1] = []; |
| 82 |
value[2] = $field |
| 83 |
.closest(".king-addons-submissions-wrap") |
| 84 |
.find("label:first-of-type") |
| 85 |
.text(); |
| 86 |
key = $field |
| 87 |
.closest(".king-addons-submissions-wrap") |
| 88 |
.find("label:first-of-type") |
| 89 |
.attr("for"); |
| 90 |
|
| 91 |
$field |
| 92 |
.closest(".king-addons-submissions-wrap") |
| 93 |
.find("input") |
| 94 |
.each(function () { |
| 95 |
const $input = $(this); |
| 96 |
value[1].push([ |
| 97 |
$input.val(), |
| 98 |
$input.is(":checked"), |
| 99 |
$input.attr("name"), |
| 100 |
$input.attr("id"), |
| 101 |
]); |
| 102 |
}); |
| 103 |
} else { |
| 104 |
value[0] = $field.attr("type"); |
| 105 |
value[1] = $field.val(); |
| 106 |
value[2] = $field.prev("label").text(); |
| 107 |
} |
| 108 |
|
| 109 |
changes[key] = value; |
| 110 |
$("#king_addons_submission_changes").val(JSON.stringify(changes)); |
| 111 |
} |
| 112 |
); |
| 113 |
|
| 114 |
// Toggle edit mode (scoped). |
| 115 |
$postForm.off("click.kingAddonsSubmissions"); |
| 116 |
$postForm.on("click.kingAddonsSubmissions", ".king-addons-edit-submissions", function (e) { |
| 117 |
e.preventDefault(); |
| 118 |
const $btn = $(this); |
| 119 |
$("#king_addons_submission_changes").val(""); |
| 120 |
|
| 121 |
$postForm |
| 122 |
.find(".king-addons-submissions-wrap input, .king-addons-submissions-wrap textarea") |
| 123 |
.each(function () { |
| 124 |
const $el = $(this); |
| 125 |
const isLocked = $el.prop("readonly") || $el.prop("disabled"); |
| 126 |
if (isLocked) { |
| 127 |
$el.prop("readonly", false).prop("disabled", false); |
| 128 |
$btn.text("Cancel"); |
| 129 |
return; |
| 130 |
} |
| 131 |
if ($el.is('[type="checkbox"],[type="radio"]')) { |
| 132 |
$el.prop("disabled", true); |
| 133 |
} else { |
| 134 |
$el.prop("readonly", true); |
| 135 |
} |
| 136 |
$btn.text("Edit"); |
| 137 |
}); |
| 138 |
}); |
| 139 |
} |
| 140 |
|
| 141 |
// Highlight unread rows on list screen. |
| 142 |
$(".king-addons-submission-unread") |
| 143 |
.closest("tr") |
| 144 |
.addClass("king-addons-submission-unread-column"); |
| 145 |
|
| 146 |
/* --------------- Sidebar meta on single submission --------------- */ |
| 147 |
if ($("#postbox-container-1").find("#submitdiv").length && window.KingAddonsSubmissions) { |
| 148 |
const s = window.KingAddonsSubmissions; |
| 149 |
$("#minor-publishing").remove(); |
| 150 |
$("#submitdiv .postbox-header h2").text("Extra Info"); |
| 151 |
|
| 152 |
const info = [ |
| 153 |
["Form", `<a href="${s.form_page_editor}" target="_blank">${s.form_name} (${s.form_id})</a>`], |
| 154 |
["Page", `<a href="${s.form_page_url}" target="_blank">${s.form_page}</a>`], |
| 155 |
["Created at", s.post_created], |
| 156 |
["Updated at", s.post_updated], |
| 157 |
["User IP", s.agent_ip], |
| 158 |
["User Agent", s.form_agent], |
| 159 |
]; |
| 160 |
|
| 161 |
info.forEach(function (row) { |
| 162 |
$("<div>", { |
| 163 |
class: "misc-pub-section", |
| 164 |
html: `${row[0]}: <span class="king-addons-submissions-meta">${row[1]}</span>`, |
| 165 |
}).insertBefore("#major-publishing-actions"); |
| 166 |
}); |
| 167 |
|
| 168 |
// Reveal meta boxes. |
| 169 |
$("#postbox-container-1, #postbox-container-2").css("opacity", 1); |
| 170 |
} |
| 171 |
}); |
| 172 |
}; |
| 173 |
|
| 174 |
bindOnce(); |
| 175 |
})(jQuery); |