admin-script.js
158 lines
| 1 | ;(function($){ |
| 2 | |
| 3 | 'use strict'; |
| 4 | |
| 5 | $(document).ready(function(){ |
| 6 | |
| 7 | const wrapperId = $('#spel_theme_builder_modal'); |
| 8 | const modalContent = $('.spel_theme_builder_wrapper .modal-content'); |
| 9 | const formModalId = $('#spel_template_form'); |
| 10 | |
| 11 | /** |
| 12 | * Extracts the post ID from a given URL. |
| 13 | * @param {string} url - The URL containing the post ID as a query parameter. |
| 14 | * @return {string|null} The post ID extracted from the URL, or null if not found. |
| 15 | */ |
| 16 | function getPostIdFromUrl(url) { |
| 17 | let urlParams = new URLSearchParams(new URL(url).search); |
| 18 | return urlParams.get('post'); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Shows the modal popup. |
| 23 | * Adds necessary classes and sets display property to block. |
| 24 | */ |
| 25 | function showPopupModal() { |
| 26 | wrapperId.addClass('show-popup'); |
| 27 | modalContent.addClass('show-popup'); |
| 28 | wrapperId.css('display', 'block'); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Hides the modal popup. |
| 33 | * Removes classes and sets display property to none. |
| 34 | */ |
| 35 | function hidePopupModal() { |
| 36 | wrapperId.css('display', 'none'); |
| 37 | wrapperId.removeClass('show-popup'); |
| 38 | modalContent.removeClass('show-popup'); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Event handler for click events on the column edit button. |
| 43 | * Event handler for click events on the "Add New" button for the custom post type. |
| 44 | * Shows the modal and sets the data-id attribute in the form. |
| 45 | */ |
| 46 | $(document).on('click', '.row-actions .edit a, .page-title-action', function(event) { |
| 47 | event.preventDefault(); |
| 48 | |
| 49 | let editColumnDataUrl = $(this).attr('href'); |
| 50 | let columnDataPostId = getPostIdFromUrl(editColumnDataUrl); |
| 51 | |
| 52 | |
| 53 | // Check if it is an edit operation |
| 54 | if (columnDataPostId) { |
| 55 | $.ajax({ |
| 56 | url: spel_template_object.ajaxurl, |
| 57 | method: 'POST', |
| 58 | data: { |
| 59 | action: 'spel_edit_template_post', |
| 60 | post_id: columnDataPostId, |
| 61 | security: spel_template_object.nonce |
| 62 | }, |
| 63 | success: function(response) { |
| 64 | if (response.success) { |
| 65 | formModalId.attr('data-id', columnDataPostId); |
| 66 | $('.template-modal-input-title').val(response.data.post_title); // Set the value attribute |
| 67 | $('#spel_template_type').val(response.data.type); |
| 68 | $('#spel_template_condition').val(response.data.condition); |
| 69 | $('#spel_template_status').prop('checked', response.data.status === 'yes'); |
| 70 | showPopupModal(); |
| 71 | } else { |
| 72 | alert('Error loading template.'); |
| 73 | } |
| 74 | }, |
| 75 | error: function(xhr, status, error) { |
| 76 | console.log('AJAX Error: ', status, error); |
| 77 | } |
| 78 | }); |
| 79 | } else { |
| 80 | formModalId.removeAttr('data-id'); // Clear any existing data-id |
| 81 | $('.template-modal-input-title').val(''); // Clear the title input field |
| 82 | |
| 83 | $('#spel_template_type').val('header'); |
| 84 | $('#spel_template_condition').val('entire_site'); |
| 85 | $('#spel_template_status').prop('checked', true); |
| 86 | |
| 87 | showPopupModal(); |
| 88 | } |
| 89 | |
| 90 | }); |
| 91 | |
| 92 | /** |
| 93 | * Event handler for click events on the modal close button. |
| 94 | * Hides the modal. |
| 95 | */ |
| 96 | $(document).on('click', '.modal-header .modal-close', function() { |
| 97 | hidePopupModal(); |
| 98 | }); |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Event handler for form submission. |
| 103 | * Submits the form data via AJAX to create or update a post. |
| 104 | */ |
| 105 | $(document).on('submit', '#spel_template_form', function(event) { |
| 106 | event.preventDefault(); |
| 107 | |
| 108 | let formData = $(this).serialize(); |
| 109 | let dataId = $(this).attr('data-id'); |
| 110 | |
| 111 | $.ajax({ |
| 112 | url: spel_template_object.ajaxurl, // Use the localized AJAX URL |
| 113 | method: 'POST', |
| 114 | data: { |
| 115 | action: 'spel_create_template_post', |
| 116 | form_data: formData, |
| 117 | post_id: dataId, |
| 118 | security: spel_template_object.nonce // Include the nonce |
| 119 | }, |
| 120 | success: function(response) { |
| 121 | if (response.success) { |
| 122 | alert('Template created successfully.'); |
| 123 | location.reload(); |
| 124 | } else { |
| 125 | alert('Error saving template.'); |
| 126 | } |
| 127 | }, |
| 128 | error: function(xhr, status, error) { |
| 129 | console.log('AJAX Error: ', status, error); |
| 130 | } |
| 131 | |
| 132 | }) |
| 133 | |
| 134 | }) |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Event handler for the edit with Elementor button |
| 139 | */ |
| 140 | $(document).on('click', '#spel_template_elementor_edit_mode_btn', function (event) { |
| 141 | event.preventDefault(); |
| 142 | |
| 143 | let postId = $('#spel_template_form').attr('data-id'); |
| 144 | if (postId) { |
| 145 | // Redirect to the Elementor editor |
| 146 | window.location.href = spel_template_object.editor_url + '?post=' + postId + '&action=elementor'; |
| 147 | } else { |
| 148 | console.log('Post ID is missing.'); |
| 149 | } |
| 150 | |
| 151 | }) |
| 152 | |
| 153 | |
| 154 | |
| 155 | }) |
| 156 | |
| 157 | |
| 158 | })(jQuery); |