icons
7 months ago
custom-post-types.js
7 months ago
settings-src.css
8 months ago
settings.css
7 months ago
custom-post-types.js
143 lines
| 1 | /** |
| 2 | * Custom Post Types Admin Script |
| 3 | * |
| 4 | * @package FrontBlocks |
| 5 | * @author Closemarketing |
| 6 | * @copyright 2025 Closemarketing |
| 7 | */ |
| 8 | |
| 9 | (function($) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | $(document).ready(function() { |
| 13 | const cptToggle = $('#enable_custom_post_types'); |
| 14 | const cptBuilder = $('#frbl-cpt-builder'); |
| 15 | const createBtn = $('#frbl-create-cpt-btn'); |
| 16 | const cptNameInput = $('#frbl-cpt-name'); |
| 17 | |
| 18 | // Toggle visibility of CPT builder. |
| 19 | if (cptToggle.length) { |
| 20 | cptToggle.on('change', function() { |
| 21 | if ($(this).is(':checked')) { |
| 22 | cptBuilder.slideDown(300); |
| 23 | } else { |
| 24 | cptBuilder.slideUp(300); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | // Initial state. |
| 29 | if (cptToggle.is(':checked')) { |
| 30 | cptBuilder.show(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Handle create button click. |
| 35 | if (createBtn.length && typeof frontblocksCpt !== 'undefined') { |
| 36 | createBtn.on('click', function(e) { |
| 37 | e.preventDefault(); |
| 38 | |
| 39 | const cptName = cptNameInput.val().trim(); |
| 40 | |
| 41 | if (!cptName) { |
| 42 | alert(frontblocksCpt.i18n.error || 'Please enter a post type name.'); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | // Disable button and show loading state. |
| 47 | const originalText = createBtn.text(); |
| 48 | createBtn.prop('disabled', true).text(frontblocksCpt.i18n.creating || 'Creating...'); |
| 49 | |
| 50 | // AJAX request. |
| 51 | $.ajax({ |
| 52 | url: frontblocksCpt.ajaxUrl, |
| 53 | type: 'POST', |
| 54 | data: { |
| 55 | action: 'frontblocks_create_cpt', |
| 56 | nonce: frontblocksCpt.nonce, |
| 57 | cpt_name: cptName, |
| 58 | }, |
| 59 | success: function(response) { |
| 60 | if (response.success) { |
| 61 | // Show success message. |
| 62 | alert(response.data.message || frontblocksCpt.i18n.success); |
| 63 | |
| 64 | // Clear input. |
| 65 | cptNameInput.val(''); |
| 66 | |
| 67 | // Reload page to show new CPT in list. |
| 68 | window.location.reload(); |
| 69 | } else { |
| 70 | alert(response.data.message || frontblocksCpt.i18n.error); |
| 71 | createBtn.prop('disabled', false).text(originalText); |
| 72 | } |
| 73 | }, |
| 74 | error: function() { |
| 75 | alert(frontblocksCpt.i18n.error || 'An error occurred. Please try again.'); |
| 76 | createBtn.prop('disabled', false).text(originalText); |
| 77 | }, |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | // Allow Enter key to trigger create. |
| 82 | cptNameInput.on('keypress', function(e) { |
| 83 | if (e.which === 13) { |
| 84 | e.preventDefault(); |
| 85 | createBtn.trigger('click'); |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | // Handle delete button click. |
| 91 | $(document).on('click', '.frontblocks-delete-cpt', function(e) { |
| 92 | e.preventDefault(); |
| 93 | |
| 94 | const button = $(this); |
| 95 | const cptKey = button.data('cpt-key'); |
| 96 | const cptName = button.data('cpt-name') || cptKey; |
| 97 | |
| 98 | if (!cptKey) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Confirm deletion. |
| 103 | if (!confirm('¿Estás seguro de que quieres eliminar el post type "' + cptName + '"? Esta acción no se puede deshacer.')) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | // Disable button and show loading state. |
| 108 | const originalText = button.text(); |
| 109 | button.prop('disabled', true).text('Eliminando...'); |
| 110 | |
| 111 | // AJAX request. |
| 112 | $.ajax({ |
| 113 | url: frontblocksCpt.ajaxUrl, |
| 114 | type: 'POST', |
| 115 | data: { |
| 116 | action: 'frontblocks_delete_cpt', |
| 117 | nonce: frontblocksCpt.nonce, |
| 118 | cpt_key: cptKey, |
| 119 | }, |
| 120 | success: function(response) { |
| 121 | if (response.success) { |
| 122 | // Remove the CPT row from the list. |
| 123 | button.closest('[data-cpt-key="' + cptKey + '"]').fadeOut(300, function() { |
| 124 | $(this).remove(); |
| 125 | }); |
| 126 | |
| 127 | // Show success message. |
| 128 | alert(response.data.message || 'Post type eliminado correctamente.'); |
| 129 | } else { |
| 130 | alert(response.data.message || 'Error al eliminar el post type.'); |
| 131 | button.prop('disabled', false).text(originalText); |
| 132 | } |
| 133 | }, |
| 134 | error: function() { |
| 135 | alert('Ocurrió un error. Por favor, intenta de nuevo.'); |
| 136 | button.prop('disabled', false).text(originalText); |
| 137 | }, |
| 138 | }); |
| 139 | }); |
| 140 | }); |
| 141 | })(jQuery); |
| 142 | |
| 143 |