Skip to content

Commit

Permalink
adds delete deprecated terms option
Browse files Browse the repository at this point in the history
  • Loading branch information
kasprzyk-sz committed Oct 31, 2024
1 parent 8868e02 commit 20f763c
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 10 deletions.
54 changes: 54 additions & 0 deletions wp-content/plugins/embl-taxonomy/assets/embl-taxonomy.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,58 @@
$modal.showModal();
startSync(new URL(path));
});

// Bail if no delete button
const $deleteButton = document.querySelector('#embl-taxonomy-delete-deprecated');
if (!$deleteButton) return;

// Confirmation modal for deletion
const $deleteModal = document.createElement('dialog');
$deleteModal.id = 'embl-delete-modal';
$deleteModal.innerHTML = `
<h3><b>Confirm Deletion</b></h3>
<p>Are you sure you want to delete all deprecated terms? This action cannot be undone.</p>
<button id="confirm-delete" class="button button-small">Delete</button>
<button id="cancel-delete" class="button button-small">Cancel</button>
`;
document.body.appendChild($deleteModal);

// Show confirmation modal on delete button click
$deleteButton.addEventListener('click', (ev) => {
ev.preventDefault();
$deleteModal.showModal();
});

// Handle delete confirmation
$deleteModal.querySelector('#confirm-delete').addEventListener('click', async () => {
$deleteModal.close();
$deleteButton.disabled = true;
$deleteButton.innerText = 'Deleting...';

try {
const response = await fetch(window.emblTaxonomySettings.deletePath, {
method: 'POST',
headers: {
'X-WP-Nonce': window.emblTaxonomySettings.token
}
});
const json = await response.json();

if (json.success) {
alert('All deprecated terms have been deleted successfully.');
window.location.reload();
} else {
throw new Error(json.error || 'Failed to delete deprecated terms.');
}
} catch (error) {
alert(`Error: ${error.message}`);
$deleteButton.disabled = false;
$deleteButton.innerText = 'Delete all deprecated terms';
}
});

// Handle delete cancellation
$deleteModal.querySelector('#cancel-delete').addEventListener('click', () => {
$deleteModal.close();
});
})();
106 changes: 96 additions & 10 deletions wp-content/plugins/embl-taxonomy/includes/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct() {
if (is_admin()) {
add_action('admin_notices', array($this, 'action_admin_notices'));
add_action('admin_enqueue_scripts', array($this, 'action_admin_enqueue'));

}

// Add columns for term meta
Expand Down Expand Up @@ -111,12 +112,12 @@ public function register_taxonomy() {
'show_in_menu' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => EMBL_Taxonomy::TAXONOMY_NAME . 'edit_terms',
'delete_terms' => EMBL_Taxonomy::TAXONOMY_NAME . 'delete_terms',
'assign_terms' => 'edit_posts'
)
'capabilities' => array(
'manage_terms' => 'manage_options', // Restricting to administrators
'edit_terms' => 'manage_options',
'delete_terms' => 'manage_options',
'assign_terms' => 'manage_options'
)
)
);

Expand Down Expand Up @@ -148,12 +149,20 @@ public function register_rest_api() {
register_rest_route(EMBL_Taxonomy::TAXONOMY_NAME . '/v1', '/sync/', array(
'methods' => 'POST',
'callback' => array($this, 'sync_taxonomy'),
// Secure the API route
// Header token from `wp_localize_script` below is required
'permission_callback' => function () {
return current_user_can('manage_categories');
return current_user_can('manage_categories');
}
));
));

// Delete deprecated terms route
register_rest_route(EMBL_Taxonomy::TAXONOMY_NAME . '/v1', '/delete-deprecated/', array(
'methods' => 'POST',
'callback' => array($this, 'delete_deprecated_terms'),
'permission_callback' => function() {
return current_user_can('manage_categories');
}
));


// Add taxonomy terms assigned to posts as field for WP REST API
register_rest_field(
Expand Down Expand Up @@ -214,6 +223,44 @@ static public function get_wp_taxonomy() {
}
return $wp_taxonomy;
}
public function delete_deprecated_terms() {
// Get all deprecated terms
$terms = get_terms(array(
'taxonomy' => EMBL_Taxonomy::TAXONOMY_NAME,
'meta_key' => EMBL_Taxonomy::META_DEPRECATED,
'meta_value' => '1',
'fields' => 'ids',
'hide_empty' => false, // Set to false to include terms without posts
));

// Check for errors or empty list
if (is_wp_error($terms)) {
return new WP_Error('term_query_error', __('Error querying terms: ' . $terms->get_error_message(), 'embl'), array('status' => 500));
}

if (empty($terms)) {
return rest_ensure_response(array(
'success' => true,
'message' => __('No deprecated terms found to delete.', 'embl')
));
}

// Delete each deprecated term
$deleted_terms = [];
foreach ($terms as $term_id) {
$result = wp_delete_term($term_id, EMBL_Taxonomy::TAXONOMY_NAME);
if (is_wp_error($result)) {
return new WP_Error('delete_error', __('Error deleting term ID ' . $term_id . ': ' . $result->get_error_message(), 'embl'), array('status' => 500));
} else {
$deleted_terms[] = $term_id; // Collect deleted term IDs
}
}

return rest_ensure_response(array(
'success' => true,
'message' => sprintf(__('Successfully deleted %d deprecated terms.', 'embl'), count($deleted_terms))
));
}

/**
* Read and parse the EMBL Taxonomy API as JSON
Expand Down Expand Up @@ -690,8 +737,46 @@ public function action_admin_notices() {
}
}
}
// Add notice for deleting or displaying deprecated terms (all pages for administrators)
if (current_user_can('administrator')) {
$deprecated_count = $this->get_deprecated_terms_count(); // Assume this function returns count of deprecated terms
if ($deprecated_count > 0) {
printf('<div class="%1$s"><p><span>%2$s</span> %3$s %4$s</p></div>',
esc_attr('notice notice-error'),
esc_html(sprintf(
__('There are %1$d deprecated terms that may need review.', 'embl'),
$deprecated_count
)),
sprintf(
'<button id="embl-taxonomy-delete-deprecated" type="button" data-href="%1$s" class="button button-small">%2$s</button>',
esc_attr('edit-tags.php?taxonomy=' . EMBL_Taxonomy::TAXONOMY_NAME . '&delete_deprecated=true'),
esc_html(__('Delete all deprecated terms', 'embl'))
),
sprintf(
'<button id="embl-taxonomy-show-deprecated" type="button" data-href="%1$s" class="button button-small">%2$s</button>',
esc_attr('edit-tags.php?taxonomy=' . EMBL_Taxonomy::TAXONOMY_NAME . '&filter=deprecated'),
esc_html(__('Show only deprecated terms', 'embl'))
)
);
}
}




}

private function get_deprecated_terms_count() {
$terms = get_terms(array(
'taxonomy' => EMBL_Taxonomy::TAXONOMY_NAME,
'meta_key' => EMBL_Taxonomy::META_DEPRECATED,
'meta_value' => '1',
'hide_empty' => false, // Ensure all deprecated terms are counted
));
return is_array($terms) ? count($terms) : 0;
}


/**
* Filter `pre_insert_term`
* Gatekeep new taxonomy terms being inserted
Expand Down Expand Up @@ -750,6 +835,7 @@ public function action_admin_enqueue() {
),
'redirect' => esc_url_raw(admin_url('edit-tags.php?taxonomy=' . EMBL_Taxonomy::TAXONOMY_NAME . '&synced=true')),
'path' => esc_url_raw(rest_url(EMBL_Taxonomy::TAXONOMY_NAME . '/v1/sync')),
'deletePath' => esc_url_raw(rest_url(EMBL_Taxonomy::TAXONOMY_NAME . '/v1/delete-deprecated')),
'token' => wp_create_nonce('wp_rest')
));

Expand Down

0 comments on commit 20f763c

Please sign in to comment.