| 1 |
/** |
| 2 |
* Refresh Resources |
| 3 |
* |
| 4 |
* @package ConvertKit |
| 5 |
* @author ConvertKit |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Refreshes resources when the Refresh button is clicked. |
| 10 |
* |
| 11 |
* @since 1.9.8.0 |
| 12 |
*/ |
| 13 |
jQuery( document ).ready( |
| 14 |
function( $ ) { |
| 15 |
|
| 16 |
$( 'button.wp-convertkit-refresh-resources' ).on( |
| 17 |
'click', |
| 18 |
function( e ) { |
| 19 |
|
| 20 |
// Prevent default button behaviour. |
| 21 |
e.preventDefault(); |
| 22 |
|
| 23 |
// Remove any existing error notices that might be displayed. |
| 24 |
convertKitRefreshResourcesRemoveNotices(); |
| 25 |
|
| 26 |
// Fetch some DOM elements. |
| 27 |
var button = this, |
| 28 |
resource = $( button ).data( 'resource' ), |
| 29 |
field = $( button ).data( 'field' ); |
| 30 |
|
| 31 |
// Disable button. |
| 32 |
$( button ).prop( 'disabled', true ); |
| 33 |
|
| 34 |
// Perform AJAX request to refresh resource. |
| 35 |
$.ajax( |
| 36 |
{ |
| 37 |
type: 'POST', |
| 38 |
data: { |
| 39 |
action: 'convertkit_admin_refresh_resources', |
| 40 |
nonce: convertkit_admin_refresh_resources.nonce, |
| 41 |
resource: resource // e.g. forms, landing_pages, tags. |
| 42 |
}, |
| 43 |
url: convertkit_admin_refresh_resources.ajaxurl, |
| 44 |
success: function ( response ) { |
| 45 |
|
| 46 |
if ( convertkit_admin_refresh_resources.debug ) { |
| 47 |
console.log( response ); |
| 48 |
} |
| 49 |
|
| 50 |
// Show an error if the request wasn't successful. |
| 51 |
if ( ! response.success ) { |
| 52 |
// Show error notice. |
| 53 |
convertKitRefreshResourcesOutputErrorNotice( response.data ); |
| 54 |
|
| 55 |
// Enable button. |
| 56 |
$( button ).prop( 'disabled', false ); |
| 57 |
|
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Get currently selected option. |
| 62 |
var selectedOption = $( field ).val(); |
| 63 |
|
| 64 |
// Remove existing select options. |
| 65 |
$( 'option', $( field ) ).each( |
| 66 |
function() { |
| 67 |
// Skip if data-preserve-on-refresh is specified, as this means we want to keep this specific option. |
| 68 |
// This will be present on the 'None' and 'Default' options. |
| 69 |
if ( typeof $( this ).data( 'preserve-on-refresh' ) !== 'undefined' ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
// Remove this option. |
| 74 |
$( this ).remove(); |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
// Populate select options from response data. |
| 79 |
response.data.forEach( |
| 80 |
function( item ) { |
| 81 |
$( field ).append( new Option( item.name, item.id, false, ( selectedOption == item.id ? true : false ) ) ); |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
// Trigger a change event on the select field, to allow Select2 instances to repopulate their options. |
| 86 |
$( field ).trigger( 'change' ); |
| 87 |
|
| 88 |
// Enable button. |
| 89 |
$( button ).prop( 'disabled', false ); |
| 90 |
} |
| 91 |
} |
| 92 |
).fail( |
| 93 |
function ( response ) { |
| 94 |
if ( convertkit_admin_refresh_resources.debug ) { |
| 95 |
console.log( response ); |
| 96 |
} |
| 97 |
|
| 98 |
// Remove any existing error notices that might be displayed. |
| 99 |
convertKitRefreshResourcesRemoveNotices(); |
| 100 |
|
| 101 |
// Show error notice. |
| 102 |
convertKitRefreshResourcesOutputErrorNotice( 'ConvertKit: ' + response.status + ' ' + response.statusText ); |
| 103 |
|
| 104 |
// Enable button. |
| 105 |
$( button ).prop( 'disabled', false ); |
| 106 |
} |
| 107 |
); |
| 108 |
|
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
/** |
| 116 |
* Removes any existing ConvertKit WordPress style error notices. |
| 117 |
* |
| 118 |
* @since 1.9.8.3 |
| 119 |
*/ |
| 120 |
function convertKitRefreshResourcesRemoveNotices() { |
| 121 |
|
| 122 |
// If we're editing a Page, Post or Custom Post Type in Gutenberg, use wp.data.dispatch to remove the error. |
| 123 |
if ( typeof wp !== 'undefined' && typeof wp.blocks !== 'undefined' ) { |
| 124 |
// Gutenberg Editor. |
| 125 |
wp.data.dispatch( 'core/notices' ).removeNotice( 'convertkit-error' ); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Classic Editor, WP_List_Table (Bulk/Quick edit) or Edit Term. |
| 130 |
( function( $ ) { |
| 131 |
|
| 132 |
$( 'div.convertkit-error' ).remove(); |
| 133 |
|
| 134 |
} )( jQuery ); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Removes any existing ConvertKit WordPress style error notices, before outputting |
| 140 |
* an error notice. |
| 141 |
* |
| 142 |
* @since 1.9.8.3 |
| 143 |
* |
| 144 |
* @param string message Error message to display. |
| 145 |
*/ |
| 146 |
function convertKitRefreshResourcesOutputErrorNotice( message ) { |
| 147 |
|
| 148 |
// Prefix the message with the Plugin name. |
| 149 |
message = 'ConvertKit: ' + message; |
| 150 |
|
| 151 |
// If we're editing a Page, Post or Custom Post Type in Gutenberg, use wp.data.dispatch to show the error. |
| 152 |
if ( typeof wp !== 'undefined' && typeof wp.blocks !== 'undefined' ) { |
| 153 |
// Gutenberg Editor. |
| 154 |
wp.data.dispatch( 'core/notices' ).createErrorNotice( |
| 155 |
message, |
| 156 |
{ |
| 157 |
id: 'convertkit-error' |
| 158 |
} |
| 159 |
); |
| 160 |
|
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Classic Editor, WP_List_Table (Bulk/Quick edit) or Edit Term. |
| 165 |
( function( $ ) { |
| 166 |
|
| 167 |
var notice = '<div id="message" class="error convertkit-error notice is-dismissible"><p>' + message + '</p></div>'; |
| 168 |
|
| 169 |
// Append the WordPress style error notice, depending on the screen. |
| 170 |
if ( $( 'hr.wp-header-end' ).length > 0 ) { |
| 171 |
$( 'hr.wp-header-end' ).after( notice ); |
| 172 |
} else if ( $( '#ajax-response' ).length > 0 ) { |
| 173 |
$( '#ajax-response' ).after( notice ); |
| 174 |
} |
| 175 |
|
| 176 |
// Notify WordPress that a new dismissible notification exists, triggering WordPress' makeNoticesDismissible() function, |
| 177 |
// which adds a dismiss button and binds necessary events to hide the notification. |
| 178 |
// We can't directly call makeNoticesDismissible(), as its minified function name will be different. |
| 179 |
$( document ).trigger( 'wp-updates-notice-added' ); |
| 180 |
|
| 181 |
} )( jQuery ); |
| 182 |
|
| 183 |
} |
| 184 |
|