diff --git a/assets/js/admin.js b/assets/js/admin.js index 0423553..e95c91b 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -89,7 +89,6 @@ e.preventDefault(); var pw_input_target = jQuery( this ).attr( 'pw_input_target' ); - var patreon_wordpress_nonce_post_sync = jQuery( this ).attr( 'patreon_wordpress_nonce_post_sync' ); jQuery.ajax({ url: ajaxurl, @@ -97,7 +96,7 @@ dataType : 'html', data: { action: 'patreon_wordpress_start_post_import', - patreon_wordpress_nonce_post_sync: patreon_wordpress_nonce_post_sync, + patreon_wordpress_nonce_post_sync: pw_admin_js.patreon_wordpress_nonce_post_sync, }, success: function( response ) { @@ -109,12 +108,24 @@ return; } + if ( response == 'need_admin_privileges') { + jQuery( '#patreon_wp_post_import_status' ).html( 'You need admin privileges to be able to do this' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'nonce_fail') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Form expired - please reload the page and try again' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + jQuery( '#patreon_wp_post_import_status' ).html( 'Started a post import' ); jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#129500' ); // Replace the button with post batch import button - jQuery( '#patreon_post_import_button_container' ).html( '' ); + jQuery( '#patreon_post_import_button_container' ).html( '' ); jQuery( '#post_import_status_heading' ).html( 'Ongoing post import' ); jQuery( '#post_import_info_text' ).html( "Posts will be imported automatically every 5 minutes. If they are not, or you want to do it faster, click to import next batch of posts. This will import the next batch of posts in the queue. You can do this every 10 seconds." ); @@ -127,7 +138,6 @@ e.preventDefault(); var pw_input_target = jQuery( this ).attr( 'pw_input_target' ); - var patreon_wordpress_nonce_post_sync = jQuery( this ).attr( 'patreon_wordpress_nonce_post_sync' ); jQuery.ajax({ url: ajaxurl, @@ -135,7 +145,7 @@ dataType : 'html', data: { action: 'patreon_wordpress_import_next_batch_of_posts', - patreon_wordpress_nonce_post_sync: patreon_wordpress_nonce_post_sync, + patreon_wordpress_nonce_post_sync: pw_admin_js.patreon_wordpress_nonce_post_sync, }, beforeSend: function(e) { jQuery( '#patreon_wp_post_import_status' ).html( 'Importing next batch...' ); @@ -151,6 +161,20 @@ return; } + + if ( response == 'need_admin_privileges') { + jQuery( '#patreon_wp_post_import_status' ).html( 'You need admin privileges to be able to do this' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'nonce_fail') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Form expired - please reload the page and try again' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'time_limit_error') { jQuery( '#patreon_wp_post_import_status' ).html( 'You can trigger next batch every 10 seconds. Please wait a few seconds more.' ); jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); @@ -216,6 +240,124 @@ }, }); + }); + jQuery(document).on( 'click', '#patreon_wordpress_cancel_manual_post_import', function(e) { + + e.preventDefault(); + var pw_input_target = jQuery( this ).attr( 'pw_input_target' ); + + jQuery.ajax({ + url: ajaxurl, + type:"POST", + dataType : 'html', + data: { + action: 'patreon_wordpress_cancel_manual_post_import', + patreon_wordpress_nonce_post_sync: pw_admin_js.patreon_wordpress_nonce_post_sync, + }, + beforeSend: function(e) { + }, + success: function( response ) { + + jQuery( '#patreon_wp_post_import_status' ).empty(); + + if ( response == 'apiv2fail') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Wrong api version! Please upgrade to v2 using the tutorial here' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'time_limit_error') { + jQuery( '#patreon_wp_post_import_status' ).html( 'You can trigger next batch every 10 seconds. Please wait a few seconds more.' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + + if ( response == 'need_admin_privileges') { + jQuery( '#patreon_wp_post_import_status' ).html( 'You need admin privileges to be able to do this' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'nonce_fail') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Form expired - please reload the page and try again' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'no_ongoing_post_import') { + jQuery( '#patreon_wp_post_import_status' ).html( 'There is no ongoing post import' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + + // Replace the post import setting info with original + jQuery( '#patreon_post_import_button_container' ).html( '' ); + jQuery( '#post_import_status_heading' ).html( 'Start a post import' ); + jQuery( '#post_import_info_text' ).html( "Start an import of your posts from Patreon if you haven't done it before. After import of existing posts is complete, new posts will automatically be imported and existing posts automatically updated so you don't need to do this again." ); + + return; + } + + if ( response == 'did_not_import_any_post') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Failed to import any post...' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'expired_or_lost_cursor_deleted') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Expired or lost page cursor deleted. Post import will restart from scratch...' ); + return; + } + + if ( response == 'throttled_internally') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Patreon api was contacted too frequently. Please wait a few minutes and try again...' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + if ( response == 'couldnt_get_posts') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Failed to get posts from Patreon...' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + if ( response == 'post_import_ended') { + + jQuery( '#patreon_wp_post_import_status' ).html( 'Post import ended' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#129500' ); + + // Replace the post import setting info with original + jQuery( '#patreon_post_import_button_container' ).html( '' ); + jQuery( '#post_import_status_heading' ).html( 'Start a post import' ); + jQuery( '#post_import_info_text' ).html( "Start an import of your posts from Patreon if you haven't done it before. After import of existing posts is complete, new posts will automatically be imported and existing posts automatically updated so you don't need to do this again." ); + + return; + } + + if ( response == 'imported_posts') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Imported next batch' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#129500' ); + return; + } + + if ( response == 'manual_post_import_canceled') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Canceled' ); + jQuery( '#patreon_post_import_button_container' ).html( '' ); + jQuery( '#post_import_status_heading' ).html( 'Start a post import' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#129500' ); + return; + } + + if ( response == 'couldnt_cancel_manual_post_import') { + jQuery( '#patreon_wp_post_import_status' ).html( 'Could not cancel import' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + return; + } + + jQuery( '#patreon_wp_post_import_status' ).html( 'An unexpected issue occurred' ); + jQuery( '#patreon_wp_post_import_status' ).css( 'color', '#f31d00' ); + + }, + }); + }); jQuery(document).on( 'click', '#patreon_wordpress_disconnect_patreon_account', function(e) { diff --git a/classes/patreon_options.php b/classes/patreon_options.php index d335dfe..bcd3c02 100644 --- a/classes/patreon_options.php +++ b/classes/patreon_options.php @@ -684,6 +684,7 @@ function patreon_plugin_setup_page(){ $post_import_status = 'No post import ongoing'; $post_import_status_color = "9d9d9d"; + $post_import_cancel = ''; $post_import_button = ''; $import_post_info_text = "Start an import of your posts from Patreon if you haven't done it before. After import of existing posts is complete, new posts will automatically be imported and existing posts automatically updated so you don't need to do this again."; $import_post_info_header = "Start a post import"; @@ -691,7 +692,8 @@ function patreon_plugin_setup_page(){ if ( get_option( 'patreon-post-import-in-progress', false ) ) { $post_import_status = "There is an ongoing post import"; $post_import_status_color = "129500"; - $post_import_button = ''; + $post_import_button = ''; + $post_import_cancel = ''; $import_post_info_text = "Posts will be imported automatically every 5 minutes. If they are not, or you want to do it faster, click to import next batch of posts. This will import the next batch of posts in the queue. You can do this every 10 seconds."; $import_post_info_header = "Ongoing post import"; } @@ -710,7 +712,7 @@ function patreon_plugin_setup_page(){