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(){ -
+
diff --git a/classes/patreon_wordpress.php b/classes/patreon_wordpress.php index 1633bef..76d5b3a 100644 --- a/classes/patreon_wordpress.php +++ b/classes/patreon_wordpress.php @@ -97,6 +97,8 @@ function __construct() { add_action( "wp_ajax_nopriv_patreon_wordpress_start_post_import", array( $this , "start_post_import" ) ); add_action( "wp_ajax_patreon_wordpress_import_next_batch_of_posts", array( $this, "import_next_batch_of_posts" ) ); add_action( "wp_ajax_nopriv_patreon_wordpress_import_next_batch_of_posts", array( $this , "import_next_batch_of_posts" ) ); + add_action( "wp_ajax_patreon_wordpress_cancel_manual_post_import", array( $this, "cancel_manual_post_import" ) ); + add_action( "wp_ajax_nopriv_patreon_wordpress_cancel_manual_post_import", array( $this , "cancel_manual_post_import" ) ); add_action( "wp_ajax_patreon_wordpress_set_update_posts_option", array( $this, "set_update_posts_option" ) ); add_action( "wp_ajax_nopriv_patreon_wordpress_set_update_posts_option", array( $this , "set_update_posts_option" ) ); add_action( "wp_ajax_patreon_wordpress_set_delete_posts_option", array( $this, "set_delete_posts_option" ) ); @@ -848,7 +850,10 @@ public static function isPatron( $user = false ) { public static function enqueueAdminScripts() { wp_enqueue_script( 'patreon-admin-js', PATREON_PLUGIN_ASSETS . '/js/admin.js', array( 'jquery' ), PATREON_WORDPRESS_VERSION, true ); - wp_localize_script( 'patreon-admin-js', 'pw_admin_js', array( 'patreon_wordpress_assets_url' => PATREON_PLUGIN_ASSETS, ) ); + wp_localize_script( 'patreon-admin-js', 'pw_admin_js', array( + 'patreon_wordpress_assets_url' => PATREON_PLUGIN_ASSETS, + 'patreon_wordpress_nonce_post_sync' => wp_create_nonce( 'patreon_wordpress_nonce_post_sync' ), + ) ); // Load image related functions only if image feature is on: @@ -859,6 +864,8 @@ public static function enqueueAdminScripts() { } + + } public static function AfterUpdateActions( $upgrader_object, $options = false ) { @@ -1142,12 +1149,15 @@ public function dismiss_admin_notice() { public function start_post_import() { if( !( is_admin() && current_user_can( 'manage_options' ) ) ) { - return; + echo 'need_admin_privileges'; + exit; + } + + if( !check_ajax_referer( 'patreon_wordpress_nonce_post_sync', 'patreon_wordpress_nonce_post_sync' ) ) { + echo 'nonce_fail'; + exit; } - if ( !isset($_REQUEST['patreon_wordpress_nonce_post_sync']) OR !wp_verify_nonce( sanitize_key( $_REQUEST['patreon_wordpress_nonce_post_sync'] ), 'patreon_wordpress_nonce_post_sync' ) ) { - return; - } update_option( 'patreon-post-import-in-progress', true ); delete_option( 'patreon-post-import-next-cursor' ); @@ -1158,12 +1168,15 @@ public function start_post_import() { public function import_next_batch_of_posts() { if( !( is_admin() && current_user_can( 'manage_options' ) ) ) { - return; + echo 'need_admin_privileges'; + exit; + } + + if( !check_ajax_referer( 'patreon_wordpress_nonce_post_sync', 'patreon_wordpress_nonce_post_sync' ) ) { + echo 'nonce_fail'; + exit; } - if ( !isset($_REQUEST['patreon_wordpress_nonce_post_sync']) OR !wp_verify_nonce( sanitize_key( $_REQUEST['patreon_wordpress_nonce_post_sync'] ), 'patreon_wordpress_nonce_post_sync' ) ) { - return; - } // Check the last time this function was triggered: $last_triggered = get_option( 'patreon-manual-import-batch-last-triggered', 0 ); @@ -1234,6 +1247,33 @@ public function import_next_batch_of_posts() { } + } + public function cancel_manual_post_import() { + + + if( !( is_admin() && current_user_can( 'manage_options' ) ) ) { + echo 'need_admin_privileges'; + exit; + } + + if( !check_ajax_referer( 'patreon_wordpress_nonce_post_sync', 'patreon_wordpress_nonce_post_sync' ) ) { + echo 'nonce_fail'; + exit; + } + + + update_option( 'patreon-post-import-in-progress', false ); + delete_option( 'patreon-post-import-next-cursor' ); + + if ( !get_option( 'patreon-post-import-in-progress', false ) AND !get_option( 'patreon-post-import-next-cursor', false ) ) { + echo 'manual_post_import_canceled'; + exit; + } + + echo 'couldnt_cancel_manual_post_import'; + exit; + + } public function save_post_sync_category() {