Skip to content

Commit

Permalink
Merge pull request #151 from Patreon/1.8.9
Browse files Browse the repository at this point in the history
1.8.9
  • Loading branch information
codebard authored Apr 23, 2024
2 parents a0735be + c07cdc5 commit 382b7d9
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 16 deletions.
152 changes: 147 additions & 5 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,14 @@

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,
type:"POST",
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 ) {

Expand All @@ -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( '<button id="patreon_wordpress_import_next_batch_of_posts" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="">Import next batch</button>' );
jQuery( '#patreon_post_import_button_container' ).html( '<button id="patreon_wordpress_import_next_batch_of_posts" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="" style="margin-right: 10px;">Import next batch</button><button id="patreon_wordpress_cancel_manual_post_import" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="">Cancel</button>' );
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." );

Expand All @@ -127,15 +138,14 @@

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,
type:"POST",
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...' );
Expand All @@ -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' );
Expand Down Expand Up @@ -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 <a href="https://www.patreondevelopers.com/t/how-to-upgrade-your-patreon-wordpress-to-use-api-v2/3249" target="_blank">here</a>' );
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( '<button id="patreon_wordpress_start_post_import" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="">Start an import</button>' );
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( '<button id="patreon_wordpress_start_post_import" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="">Start an import</button>' );
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( '<button id="patreon_wordpress_start_post_import" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="">Start an import</button>' );
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) {
Expand Down
6 changes: 4 additions & 2 deletions classes/patreon_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,14 +684,16 @@ function patreon_plugin_setup_page(){

$post_import_status = 'No post import ongoing';
$post_import_status_color = "9d9d9d";
$post_import_cancel = '';
$post_import_button = '<button id="patreon_wordpress_start_post_import" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="" patreon_wordpress_nonce_post_sync="' . wp_create_nonce('patreon_wordpress_nonce_post_sync') .'">Start an 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";

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 = '<button id="patreon_wordpress_import_next_batch_of_posts" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="" patreon_wordpress_nonce_post_sync="' . wp_create_nonce('patreon_wordpress_nonce_post_sync') .'">Import next batch</button>';
$post_import_button = '<button id="patreon_wordpress_import_next_batch_of_posts" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="" style="margin-right: 10px;">Import next batch</button>';
$post_import_cancel = '<button id="patreon_wordpress_cancel_manual_post_import" class="button button-primary button-large" pw_input_target="#patreon_wp_post_import_status" target="">Cancel</button>';
$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";
}
Expand All @@ -710,7 +712,7 @@ function patreon_plugin_setup_page(){
</th>
<td>

<div id="patreon_post_import_button_container"><?php echo $post_import_button; ?></div>
<div id="patreon_post_import_button_container"><?php echo $post_import_button; ?><?php echo $post_import_cancel; ?></div>
</td>
</tr>
</table>
Expand Down
58 changes: 49 additions & 9 deletions classes/patreon_wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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:

Expand All @@ -859,6 +864,8 @@ public static function enqueueAdminScripts() {

}



}
public static function AfterUpdateActions( $upgrader_object, $options = false ) {

Expand Down Expand Up @@ -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' );

Expand All @@ -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 );
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit 382b7d9

Please sign in to comment.