PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.3
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
convertkit / resources / backend / js / refresh-resources.js

refresh-resources.js in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at resources/backend/js/refresh-resources.js

271 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Forms, Tags and Products.
100 // Forms.
101 response.forms.forEach(function (item) {
102 document
103 .querySelector(
104 field + ' optgroup[data-resource=forms]'
105 )
106 .appendChild(
107 new Option(
108 item.name +
109 ' [' +
110 (item.format ? item.format : 'inline') +
111 ']',
112 'form_' + item.id,
113 false,
114 selectedOption === 'form_' + item.id
115 )
116 );
117 });
118
119 // Tags.
120 response.tags.forEach(function (item) {
121 document
122 .querySelector(
123 field + ' optgroup[data-resource=tags]'
124 )
125 .appendChild(
126 new Option(
127 item.name,
128 'tag_' + item.id,
129 false,
130 selectedOption === 'tag_' + item.id
131 )
132 );
133 });
134
135 // Products.
136 response.products.forEach(function (item) {
137 document
138 .querySelector(
139 field + ' optgroup[data-resource=products]'
140 )
141 .appendChild(
142 new Option(
143 item.name,
144 'product_' + item.id,
145 false,
146 selectedOption === 'product_' + item.id
147 )
148 );
149 });
150 break;
151
152 default:
153 // Populate select options from response data.
154 response.forEach(function (item) {
155 // Define label.
156 let label = '';
157 switch (resource) {
158 case 'forms':
159 label =
160 item.name +
161 ' [' +
162 (item.format !== ''
163 ? item.format
164 : 'inline') +
165 ']';
166 break;
167
168 default:
169 label = item.name;
170 break;
171 }
172
173 // Add option.
174 document
175 .querySelector(field)
176 .add(
177 new Option(
178 label,
179 item.id,
180 false,
181 selectedOption === item.id ? true : false
182 )
183 );
184 });
185 break;
186 }
187
188 // Trigger a change event on the select field, to allow Select2 instances to repopulate their options.
189 document.querySelector(field).dispatchEvent(new Event('change'));
190
191 // Enable button and remove is-refreshing class.
192 button.disabled = false;
193 button.classList.remove('is-refreshing');
194 })
195 .catch(function (error) {
196 if (convertkit_admin_refresh_resources.debug) {
197 console.error(error);
198 }
199
200 // Remove any existing error notices that might be displayed.
201 convertKitRefreshResourcesRemoveNotices();
202
203 // Show error notice.
204 convertKitRefreshResourcesOutputErrorNotice(error);
205
206 // Enable button and remove is-refreshing class.
207 button.disabled = false;
208 button.classList.remove('is-refreshing');
209 });
210 }
211
212 /**
213 * Removes any existing ConvertKit WordPress style error notices.
214 *
215 * @since 1.9.8.3
216 */
217 function convertKitRefreshResourcesRemoveNotices() {
218 // If we're editing a Page, Post or Custom Post Type in Gutenberg, use wp.data.dispatch to remove the error.
219 if (convertKitEditingPostInGutenberg()) {
220 // Gutenberg Editor.
221 wp.data.dispatch('core/notices').removeNotice('convertkit-error');
222 return;
223 }
224
225 // Classic Editor, WP_List_Table (Bulk/Quick edit) or Edit Term.
226 document.querySelectorAll('div.convertkit-error').forEach(function (div) {
227 div.remove();
228 });
229 }
230
231 /**
232 * Removes any existing ConvertKit WordPress style error notices, before outputting
233 * an error notice.
234 *
235 * @since 1.9.8.3
236 *
237 * @param {string} message Error message to display.
238 */
239 function convertKitRefreshResourcesOutputErrorNotice(message) {
240 // Prefix the message with the Plugin name.
241 message = 'ConvertKit: ' + message;
242
243 // If we're editing a Page, Post or Custom Post Type in Gutenberg, use wp.data.dispatch to show the error.
244 if (convertKitEditingPostInGutenberg()) {
245 // Gutenberg Editor.
246 wp.data
247 .dispatch('core/notices')
248 .createErrorNotice(message, { id: 'convertkit-error' });
249 return;
250 }
251
252 // Classic Editor, WP_List_Table (Bulk/Quick edit) or Edit Term.
253 const notice =
254 '<div id="message" class="error convertkit-error notice is-dismissible"><p>' +
255 message +
256 '</p></div>';
257
258 // Append the WordPress style error notice, depending on the screen.
259 const insertLocation =
260 document.querySelector('hr.wp-header-end') ||
261 document.querySelector('#ajax-response');
262 if (insertLocation) {
263 insertLocation.insertAdjacentHTML('afterend', notice);
264
265 // Notify WordPress that a new dismissible notification exists, triggering WordPress' makeNoticesDismissible() function,
266 // which adds a dismiss button and binds necessary events to hide the notification.
267 // We can't directly call makeNoticesDismissible(), as its minified function name will be different.
268 document.dispatchEvent(new Event('wp-updates-notice-added'));
269 }
270 }
271