PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.6.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.6.6
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 2.3.2 All 195 releases
convertkit / resources / backend / js / refresh-resources.js

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

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