Wordpress MU-Plugin Name: Aldine catalog toggle fix Description: Adds the missing book_id to Aldine's "In Catalog" AJAX request on Network Admin > Sites. This is a must use plugin and should be the directory wordpress/wp-content/mu-plugins
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

<?php
/**
* Plugin Name: Aldine catalog toggle fix
* Description: Adds the missing book_id to Aldine's "In Catalog" AJAX request on Network Admin > Sites.
*/
add_action( 'admin_print_footer_scripts', function () {
if ( ! is_network_admin() || ( $GLOBALS['pagenow'] ?? '' ) !== 'sites.php' ) {
return;
}
?>
<script id="aldine-catalog-fix">
(function () {
var lastBookId = null;
console.debug('[aldine-fix] loaded');
function remember(ev) {
var el = ev.target;
if (!el || !el.classList || !el.classList.contains('in-catalog')) { return; }
var row = el.closest('tr');
var cb = row ? row.querySelector('input[name="allblogs[]"], th.check-column input[type="checkbox"]') : null;
lastBookId = cb ? cb.value : null;
console.debug('[aldine-fix] ' + ev.type + ' on in-catalog, book_id =', lastBookId);
}
// Capture phase, several events, so we run before Aldine's handler whatever it listens to.
['mousedown', 'keydown', 'click', 'change'].forEach(function (t) {
document.addEventListener(t, remember, true);
});
// Patch at the XHR level so it works whichever copy of jQuery Aldine uses.
var origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function (body) {
if (typeof body === 'string'
&& body.indexOf('action=pressbooks_aldine_update_catalog') !== -1
&& !/(^|&)book_id=\d+/.test(body)) {
if (lastBookId) {
body += '&book_id=' + encodeURIComponent(lastBookId);
console.debug('[aldine-fix] added book_id', lastBookId);
} else {
console.warn('[aldine-fix] catalog request seen but no book_id captured');
}
}
return origSend.call(this, body);
};
})();
</script>
<?php
}, 99 );