Skip to content

Instantly share code, notes, and snippets.

@marc-guenther
Last active August 24, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marc-guenther/39ad60345c641d752a04a65193766dee to your computer and use it in GitHub Desktop.
Save marc-guenther/39ad60345c641d752a04a65193766dee to your computer and use it in GitHub Desktop.
add "bulk-detach" option to recurring events from events-manager wordpress plugin
// ==UserScript==
// @name Bulk-Detach option for Recurring-Events
// @namespace https://wordpress.org/support/plugin/events-manager/
// @version 0.1
// @description allow bulk-detach of recurring events for Wordpress Events-Manager plugin
// @author Marc Günther
// @match *://*/*wp-admin/edit.php?post_type=event&scope=all&recurrence_id=*
// @grant none
// ==/UserScript==
/**
* TamperMonkey script which adds a new button "Detach" to the upper "Bulk Actions" popup menu in the Events-Manager Wordpress plugin.
* To use:
* - Events -> Recurring Events
* - Edit one of the events.
* - In the event, click on "You can edit individual recurrences and disassociate them with this recurring event."
* - Now you get a list of all single events, and have a new "Detach" option in "Bulk actions".
* - Selecting it will detach all selected events one by one.
*
* Implementation:
* - Store ids of all selected checkboxes in sessionStorage.
* - Iterate through all of them, and click the corresponding "Detach" link.
* - When no checkboxes are left, delete the entry from sessionStorage.
*/
(function() {
'use strict';
var storage_key = "dismiss-checkbox-ids";
// detach events belonging to comma separated checkbox ids
function detach(checkbox_ids) {
for (var checkbox_id of checkbox_ids.split(",")) {
// find the checkbox
var checkbox = document.getElementById(checkbox_id);
if (checkbox) {
// find the "Detach" link
var detach_link = checkbox.parentNode.parentNode.querySelector("a.em-detach-link");
if (detach_link) {
// disable confirmation popup and click the link
window.confirm = () => true;
detach_link.click();
return false;
}
}
}
// remove from sessionStorage after we are done
sessionStorage.removeItem(storage_key);
}
// add the "Detach" option to the upper "Bulk Actions"
var detach_option = document.createElement("option");
detach_option.text = "Detach";
document.querySelector("select#bulk-action-selector-top").add(detach_option);
// patch the "Apply" button to check for our new "Detach" option
document.querySelector("input#doaction.action.button").onclick = () => {
if (document.querySelector("select#bulk-action-selector-top").value == "Detach") {
// find ids of all selected checkboxes
var selected_checkboxes = [...document.querySelectorAll("input[type='checkbox'][id*='cb-select-']:checked")].map(el=>el.id).join(",");
// store them in the sessionStorage
sessionStorage.setItem(storage_key, selected_checkboxes);
// and start detaching the first (the rest will be detached after reload of this page)
detach(selected_checkboxes);
return false;
}
// perform original action, whatever it is
return true;
}
// look into the session storage if we still have items to detach, and do so
var selected_checkboxes = sessionStorage.getItem(storage_key);
if (selected_checkboxes) {
detach(selected_checkboxes);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment