| 1 |
/** |
| 2 |
* Refresh Resources |
| 3 |
* |
| 4 |
* @author ConvertKit |
| 5 |
*/ |
| 6 |
|
| 7 |
document.addEventListener( |
| 8 |
'DOMContentLoaded', |
| 9 |
convertKitRefreshResourcesInitEventListeners |
| 10 |
); |
| 11 |
|
| 12 |
/** |
| 13 |
* Binds click event listeners to refresh resource buttons. |
| 14 |
* |
| 15 |
* @since 2.4.4 |
| 16 |
*/ |
| 17 |
function convertKitRefreshResourcesInitEventListeners() { |
| 18 |
// Refreshes resources when the Refresh button is clicked. |
| 19 |
document |
| 20 |
.querySelectorAll('button.wp-convertkit-refresh-resources') |
| 21 |
.forEach(function (element) { |
| 22 |
element.addEventListener( |
| 23 |
'click', |
| 24 |
function (e) { |
| 25 |
e.preventDefault(); |
| 26 |
convertKitRefreshResources(element); |
| 27 |
}, |
| 28 |
false |
| 29 |
); |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Refresh resources when a button is clicked. |
| 35 |
* |
| 36 |
* @since 2.4.4 |
| 37 |
* |
| 38 |
* @param {Object} button Button element. |
| 39 |
*/ |
| 40 |
function convertKitRefreshResources(button) { |
| 41 |
// Remove any existing error notices that might be displayed. |
| 42 |
convertKitRefreshResourcesRemoveNotices(); |
| 43 |
|
| 44 |
const resource = button.dataset.resource, |
| 45 |
field = button.dataset.field; |
| 46 |
|
| 47 |
// Disable button and set is-refreshing class. |
| 48 |
button.disabled = true; |
| 49 |
button.classList.add('is-refreshing'); |
| 50 |
|
| 51 |
// Perform AJAX request to refresh resource. |
| 52 |
fetch(convertkit_admin_refresh_resources.ajaxurl + resource, { |
| 53 |
method: 'POST', |
| 54 |
headers: { |
| 55 |
'Content-Type': 'application/json', |
| 56 |
'X-WP-Nonce': convertkit_admin_refresh_resources.nonce, |
| 57 |
}, |
| 58 |
}) |
| 59 |
.then(function (response) { |
| 60 |
// Convert response JSON string to object. |
| 61 |
return response.json(); |
| 62 |
}) |
| 63 |
.then(function (response) { |
| 64 |
if (convertkit_admin_refresh_resources.debug) { |
| 65 |
console.log(response); |
| 66 |
} |
| 67 |
|
| 68 |
// Show an error if the request wasn't successful. |
| 69 |
if (typeof response.code !== 'undefined') { |
| 70 |
// Show error notice. |
| 71 |
convertKitRefreshResourcesOutputErrorNotice(response.message); |
| 72 |
|
| 73 |
// Enable button and remove is-refreshing class. |
| 74 |
button.disabled = false; |
| 75 |
button.classList.remove('is-refreshing'); |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
const selectedOption = document.querySelector(field).value; |
| 81 |
|
| 82 |
// Remove existing select options. |
| 83 |
document |
| 84 |
.querySelectorAll(field + ' option') |
| 85 |
.forEach(function (option) { |
| 86 |
// Skip if data-preserve-on-refresh is specified, as this means we want to keep this specific option. |
| 87 |
// This will be present on the 'None' and 'Default' options. |
| 88 |
if (option.dataset.preserveOnRefresh !== undefined) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Remove this option. |
| 93 |
option.remove(); |
| 94 |
}); |
| 95 |
|
| 96 |
// Depending on the resource we're refreshing, populate the <select> options. |
| 97 |
switch (resource) { |
| 98 |
case 'restrict_content': |
| 99 |
// Populate select `optgroup`` from response data, which comprises of Tags and Products. |
| 100 |
// Tags. |
| 101 |
response.tags.forEach(function (item) { |
| 102 |
document |
| 103 |
.querySelector( |
| 104 |
field + ' optgroup[data-resource=tags]' |
| 105 |
) |
| 106 |
.appendChild( |
| 107 |
new Option( |
| 108 |
item.name, |
| 109 |
'tag_' + item.id, |
| 110 |
false, |
| 111 |
selectedOption === 'tag_' + item.id |
| 112 |
) |
| 113 |
); |
| 114 |
}); |
| 115 |
|
| 116 |
// Products. |
| 117 |
response.products.forEach(function (item) { |
| 118 |
document |
| 119 |
.querySelector( |
| 120 |
field + ' optgroup[data-resource=products]' |
| 121 |
) |
| 122 |
.appendChild( |
| 123 |
new Option( |
| 124 |
item.name, |
| 125 |
'product_' + item.id, |
| 126 |
false, |
| 127 |
selectedOption === 'product_' + item.id |
| 128 |
) |
| 129 |
); |
| 130 |
}); |
| 131 |
break; |
| 132 |
|
| 133 |
default: |
| 134 |
// Populate select options from response data. |
| 135 |
response.forEach(function (item) { |
| 136 |
// Define label. |
| 137 |
let label = ''; |
| 138 |
switch (resource) { |
| 139 |
case 'forms': |
| 140 |
label = |
| 141 |
item.name + |
| 142 |
' [' + |
| 143 |
(item.format !== '' |
| 144 |
? item.format |
| 145 |
: 'inline') + |
| 146 |
']'; |
| 147 |
break; |
| 148 |
|
| 149 |
default: |
| 150 |
label = item.name; |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
// Add option. |
| 155 |
document |
| 156 |
.querySelector(field) |
| 157 |
.add( |
| 158 |
new Option( |
| 159 |
label, |
| 160 |
item.id, |
| 161 |
false, |
| 162 |
selectedOption === item.id ? true : false |
| 163 |
) |
| 164 |
); |
| 165 |
}); |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
// Trigger a change event on the select field, to allow Select2 instances to repopulate their options. |
| 170 |
document.querySelector(field).dispatchEvent(new Event('change')); |
| 171 |
|
| 172 |
// Enable button and remove is-refreshing class. |
| 173 |
button.disabled = false; |
| 174 |
button.classList.remove('is-refreshing'); |
| 175 |
}) |
| 176 |
.catch(function (error) { |
| 177 |
if (convertkit_admin_refresh_resources.debug) { |
| 178 |
console.error(error); |
| 179 |
} |
| 180 |
|
| 181 |
// Remove any existing error notices that might be displayed. |
| 182 |
convertKitRefreshResourcesRemoveNotices(); |
| 183 |
|
| 184 |
// Show error notice. |
| 185 |
convertKitRefreshResourcesOutputErrorNotice(error); |
| 186 |
|
| 187 |
// Enable button and remove is-refreshing class. |
| 188 |
button.disabled = false; |
| 189 |
button.classList.remove('is-refreshing'); |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Removes any existing ConvertKit WordPress style error notices. |
| 195 |
* |
| 196 |
* @since 1.9.8.3 |
| 197 |
*/ |
| 198 |
function convertKitRefreshResourcesRemoveNotices() { |
| 199 |
// If we're editing a Page, Post or Custom Post Type in Gutenberg, use wp.data.dispatch to remove the error. |
| 200 |
if (convertKitEditingPostInGutenberg()) { |
| 201 |
// Gutenberg Editor. |
| 202 |
wp.data.dispatch('core/notices').removeNotice('convertkit-error'); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
// Classic Editor, WP_List_Table (Bulk/Quick edit) or Edit Term. |
| 207 |
document.querySelectorAll('div.convertkit-error').forEach(function (div) { |
| 208 |
div.remove(); |
| 209 |
}); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Removes any existing ConvertKit WordPress style error notices, before outputting |
| 214 |
* an error notice. |
| 215 |
* |
| 216 |
* @since 1.9.8.3 |
| 217 |
* |
| 218 |
* @param {string} message Error message to display. |
| 219 |
*/ |
| 220 |
function convertKitRefreshResourcesOutputErrorNotice(message) { |
| 221 |
// Prefix the message with the Plugin name. |
| 222 |
message = 'ConvertKit: ' + message; |
| 223 |
|
| 224 |
// If we're editing a Page, Post or Custom Post Type in Gutenberg, use wp.data.dispatch to show the error. |
| 225 |
if (convertKitEditingPostInGutenberg()) { |
| 226 |
// Gutenberg Editor. |
| 227 |
wp.data |
| 228 |
.dispatch('core/notices') |
| 229 |
.createErrorNotice(message, { id: 'convertkit-error' }); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
// Classic Editor, WP_List_Table (Bulk/Quick edit) or Edit Term. |
| 234 |
const notice = |
| 235 |
'<div id="message" class="error convertkit-error notice is-dismissible"><p>' + |
| 236 |
message + |
| 237 |
'</p></div>'; |
| 238 |
|
| 239 |
// Append the WordPress style error notice, depending on the screen. |
| 240 |
const insertLocation = |
| 241 |
document.querySelector('hr.wp-header-end') || |
| 242 |
document.querySelector('#ajax-response'); |
| 243 |
if (insertLocation) { |
| 244 |
insertLocation.insertAdjacentHTML('afterend', notice); |
| 245 |
|
| 246 |
// Notify WordPress that a new dismissible notification exists, triggering WordPress' makeNoticesDismissible() function, |
| 247 |
// which adds a dismiss button and binds necessary events to hide the notification. |
| 248 |
// We can't directly call makeNoticesDismissible(), as its minified function name will be different. |
| 249 |
document.dispatchEvent(new Event('wp-updates-notice-added')); |
| 250 |
} |
| 251 |
} |
| 252 |
|