From 5de5302cf90e12b41fe5bc757cd25221d2a1419f Mon Sep 17 00:00:00 2001 From: JK Date: Fri, 30 Jun 2023 16:11:08 +0600 Subject: [PATCH 1/3] Code commenting --- assets/js/admin-pull.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/assets/js/admin-pull.js b/assets/js/admin-pull.js index 3de9f9255..3e939c2af 100755 --- a/assets/js/admin-pull.js +++ b/assets/js/admin-pull.js @@ -15,6 +15,7 @@ const form = document.getElementById( 'posts-filter' ); const asDraftCheckboxes = document.querySelectorAll( '[name=dt_as_draft]' ); const pullLinks = document.querySelectorAll( '.distributor_page_pull .pull a' ); +// Change target website to pull contents from jQuery( chooseConnection ).on( 'change', ( event ) => { document.location = event.currentTarget.options[ @@ -25,6 +26,7 @@ jQuery( chooseConnection ).on( 'change', ( event ) => { } ); if ( chooseConnection && choosePostType && form ) { + // Handle post type selection if ( choosePostTypeBtn ) { jQuery( choosePostTypeBtn ).on( 'click', ( event ) => { event.preventDefault(); @@ -35,6 +37,7 @@ if ( chooseConnection && choosePostType && form ) { } ); } + // Handle search button click if ( searchField && searchBtn ) { jQuery( searchBtn ).on( 'click', ( event ) => { event.preventDefault(); @@ -47,9 +50,11 @@ if ( chooseConnection && choosePostType && form ) { } ); } + // Handle pull mode checkbox event if ( asDraftCheckboxes && pullLinks ) { jQuery( asDraftCheckboxes ).on( 'change', ( event ) => { if ( event.currentTarget.checked ) { + // Check all pull mode checkbox as there are multiple. Ideally before and after post list. for ( let i = 0; i < asDraftCheckboxes.length; ++i ) { asDraftCheckboxes[ i ].checked = true; } @@ -61,6 +66,7 @@ if ( chooseConnection && choosePostType && form ) { pullLinks[ i ].text = __( 'Pull as draft', 'distributor' ); } } else { + // Uncheck all pull mode checkbox as there are multiple. Ideally before and after post list. for ( let i = 0; i < asDraftCheckboxes.length; ++i ) { asDraftCheckboxes[ i ].checked = false; } From b4ac3fb8940c60d15a930df3473251ed53ffbbba Mon Sep 17 00:00:00 2001 From: JK Date: Fri, 30 Jun 2023 21:40:38 +0600 Subject: [PATCH 2/3] Pull contents via ajax finalized --- assets/css/admin-pull-table.scss | 22 +++ assets/js/admin-pull.js | 132 ++++++++++++++++- includes/classes/PullListTable.php | 4 +- includes/pull-ui.php | 218 ++++++++++------------------- 4 files changed, 225 insertions(+), 151 deletions(-) diff --git a/assets/css/admin-pull-table.scss b/assets/css/admin-pull-table.scss index a5d37a417..6c22b64cb 100644 --- a/assets/css/admin-pull-table.scss +++ b/assets/css/admin-pull-table.scss @@ -18,6 +18,28 @@ padding-left: 10px; } } + + #distributor-pull-modal{ + position: fixed; + left: 0; + right: 0; + top: 0; + bottom: 0; + background: rgba(0, 0, 0, 50%); + display: flex; + align-items: center; + z-index: 9999; + + &>div{ + margin: 0 auto; + display: block; + width: 300px; + background: white; + padding: 20px; + border-radius: 10px; + box-shadow: 1px 2px 15px rgba(0, 0, 0, 30%); + } + } } .wp-list-table .disabled { diff --git a/assets/js/admin-pull.js b/assets/js/admin-pull.js index 3e939c2af..43b297471 100755 --- a/assets/js/admin-pull.js +++ b/assets/js/admin-pull.js @@ -51,7 +51,7 @@ if ( chooseConnection && choosePostType && form ) { } // Handle pull mode checkbox event - if ( asDraftCheckboxes && pullLinks ) { + if ( asDraftCheckboxes ) { jQuery( asDraftCheckboxes ).on( 'change', ( event ) => { if ( event.currentTarget.checked ) { // Check all pull mode checkbox as there are multiple. Ideally before and after post list. @@ -60,9 +60,6 @@ if ( chooseConnection && choosePostType && form ) { } for ( let i = 0; i < pullLinks.length; ++i ) { - pullLinks[ i ].href = addQueryArgs( pullLinks[ i ].href, { - dt_as_draft: 'draft' /*eslint camelcase: 0*/, - } ); pullLinks[ i ].text = __( 'Pull as draft', 'distributor' ); } } else { @@ -72,14 +69,135 @@ if ( chooseConnection && choosePostType && form ) { } for ( let i = 0; i < pullLinks.length; ++i ) { - pullLinks[ i ].href = addQueryArgs( pullLinks[ i ].href, { - dt_as_draft: '' /*eslint camelcase: 0*/, - } ); pullLinks[ i ].text = __( 'Pull', 'distributor' ); } } } ); } + + // Pull content via ajax + jQuery( '#doaction, #doaction2' ).on( 'click', e => { + // Check action + var action = jQuery( '[name="action"]' ).val(); + if ( action != 'bulk-syndicate' ) { + return; + } + e.preventDefault(); + openModal(); + }); + + jQuery( '.distributor_page_pull .pull a' ).on( 'click', function( e ) { + e.preventDefault(); + jQuery( this ).closest( 'tr' ).find( '.check-column input[type="checkbox"]' ).prop( 'checked', true ); + openModal(); + }); + + function openModal() { + // Prepare data + var aborted = false; + var post_ids = []; + var source_label = jQuery('#pull_connections option:selected').text(); + + jQuery( '#the-list .check-column input[type="checkbox"]:checked' ).each( function() { + var id = parseInt( jQuery( this ).val() ); + if ( id && post_ids.indexOf( id ) === -1 ) { + post_ids.push( id ); + } + } ); + + var post_ids_count = post_ids.length; + if ( ! post_ids_count ) { + alert( 'Please select posts to pull' ); + return; + } + + function log( custom_content ) { + jQuery( '#distributor-pull-modal .pull-progress' ).html( custom_content || `Pulled: ${post_ids_count-post_ids.length}/${post_ids_count}` ); + } + + // Create modal for pulling via ajax + jQuery( '#distributor-pull-modal' ).remove(); + jQuery( 'body' ).append( + ` +
+
+
+

Pulling from ${source_label}

+
Selected: ${post_ids_count}
+
+
+
+ + +
+
+
+ ` + ); + + jQuery( '#distributor-pull-modal' ).on( 'click', '[data-action="start"]', function() { + jQuery( this ).prop( 'disabled', true ); + + var excludes = [ 'post[]', 'action2', 'page', 'paged' ]; + var form_data = {}; + jQuery( '#posts-filter' ).serializeArray().forEach( field => { + if ( excludes.indexOf( field.name ) == -1 ) { + form_data[ field.name ] = field.value; + } + }); + form_data.action = 'distributor_pull_content'; + + function looper() { + if ( aborted ) { + jQuery('#distributor-pull-modal').remove() + } + + log(); + + form_data.post_id = post_ids.shift(); + var xhr = new XMLHttpRequest(); + + jQuery.ajax({ + url: window.ajaxurl, + type: 'POST', + data: form_data, + xhr: function() { + return xhr; + }, + success: function (resp) { + if ( aborted ) { + return; + } + + if ( ! resp.success || ! resp.data?.redirect_to ) { + log( `${resp.data?.message || 'Something went wrong!'}`); + return; + } + + log(); + + if ( post_ids.length ) { + // Call the pull again for remaing post + looper(); + } else { + // Redirect to where it asks to + window.location.assign( resp.data.redirect_to ); + } + } + }); + } + + looper(); + }).on( 'click', '[data-action="cancel"]', function() { + aborted = true; + jQuery( '#distributor-pull-modal' ).remove(); + + // Refresh the page if any post already pulled. + if ( post_ids_count > post_ids.length ) { + window.location.reload(); + } + } ); + }; } /** diff --git a/includes/classes/PullListTable.php b/includes/classes/PullListTable.php index 531d6f408..24107395a 100644 --- a/includes/classes/PullListTable.php +++ b/includes/classes/PullListTable.php @@ -339,7 +339,7 @@ public function column_name( $item ) { $actions = [ //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`. - 'pull' => sprintf( '%s', esc_url( wp_nonce_url( admin_url( 'admin.php?page=pull&action=syndicate&_wp_http_referer=' . rawurlencode( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) . '&post=' . $item->ID . '&connection_type=' . $connection_type . '&connection_id=' . $connection_id . '&pull_post_type=' . $item->post_type . '&dt_as_draft=' . $draft ), 'bulk-distributor_page_pull' ) ), $draft ? esc_html__( 'Pull as draft', 'distributor' ) : esc_html__( 'Pull', 'distributor' ) ), + 'pull' => sprintf( '%s', $draft ? esc_html__( 'Pull as draft', 'distributor' ) : esc_html__( 'Pull', 'distributor' ) ), 'view' => '' . esc_html__( 'View', 'distributor' ) . '', //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`. 'skip' => sprintf( '%s', esc_url( wp_nonce_url( admin_url( 'admin.php?page=pull&action=skip&_wp_http_referer=' . rawurlencode( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) . '&post=' . $item->ID . '&connection_type=' . $connection_type . '&connection_id=' . $connection_id ), 'dt_skip' ) ), esc_html__( 'Skip', 'distributor' ) ), @@ -355,7 +355,7 @@ public function column_name( $item ) { $actions = [ //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`. - 'pull' => sprintf( '%s', esc_url( wp_nonce_url( admin_url( 'admin.php?page=pull&action=syndicate&_wp_http_referer=' . rawurlencode( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) . '&post=' . $item->ID . '&connection_type=' . $connection_type . '&connection_id=' . $connection_id . '&pull_post_type=' . $item->post_type . '&dt_as_draft=' . $draft ), 'bulk-distributor_page_pull' ) ), $draft ? esc_html__( 'Pull as draft', 'distributor' ) : esc_html__( 'Pull', 'distributor' ) ), + 'pull' => sprintf( '%s', $draft ? esc_html__( 'Pull as draft', 'distributor' ) : esc_html__( 'Pull', 'distributor' ) ), //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`. 'unskip' => sprintf( '%s', esc_url( wp_nonce_url( admin_url( 'admin.php?page=pull&action=unskip&_wp_http_referer=' . rawurlencode( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) . '&post=' . $item->ID . '&connection_type=' . $connection_type . '&connection_id=' . $connection_id ), 'dt_unskip' ) ), esc_html__( 'Unskip', 'distributor' ) ), 'view' => '' . esc_html__( 'View', 'distributor' ) . '', diff --git a/includes/pull-ui.php b/includes/pull-ui.php index f56c02a73..737ce6f3e 100644 --- a/includes/pull-ui.php +++ b/includes/pull-ui.php @@ -25,6 +25,8 @@ function() { add_filter( 'set-screen-option', __NAMESPACE__ . '\set_screen_option', 10, 3 ); } ); + + add_action( 'wp_ajax_distributor_pull_content', __NAMESPACE__ . '\pull_contents' ); } /** @@ -193,102 +195,96 @@ function screen_option() { } /** - * Process content changing actions + * Pull contents via ajax call * - * @since 0.8 + * @return void */ -function process_actions() { - global $connection_list_table; +function pull_contents() { global $dt_pull_messages; - switch ( $connection_list_table->current_action() ) { - case 'syndicate': - case 'bulk-syndicate': - if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'bulk-distributor_page_pull' ) ) { - exit; - } - - // Filter documented above. - if ( ! current_user_can( apply_filters( 'dt_pull_capabilities', 'manage_options' ) ) ) { - wp_die( - '

' . esc_html__( 'Cheatin’ uh?', 'distributor' ) . '

' . - '

' . esc_html__( 'Sorry, you are not allowed to add this item.', 'distributor' ) . '

', - 403 - ); - } - - if ( empty( $_GET['pull_post_type'] ) || empty( $_GET['connection_type'] ) || empty( $_GET['connection_id'] ) || empty( $_GET['post'] ) ) { - break; - } + // Verify nonce + if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bulk-distributor_page_pull' ) ) { + wp_send_json_error( array( 'message' => __( 'Invalid Session!', 'distributor' ) ) ); + exit; + } - $posts = array_map( 'intval', (array) wp_unslash( $_GET['post'] ) ); - $post_type = sanitize_text_field( $_GET['pull_post_type'] ); - $post_status = ! empty( $_GET['dt_as_draft'] ) && 'draft' === $_GET['dt_as_draft'] ? 'draft' : ''; - - $posts = array_map( - function( $remote_post_id ) use ( $post_type, $post_status ) { - return [ - 'remote_post_id' => $remote_post_id, - 'post_type' => $post_type, - 'post_status' => $post_status, - ]; - }, - $posts - ); + // Capability check + if ( ! current_user_can( apply_filters( 'dt_pull_capabilities', 'manage_options' ) ) ) { + wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to add this item.', 'distributor' ) ) ); + exit; + } - if ( 'external' === $_GET['connection_type'] ) { - $connection = \Distributor\ExternalConnection::instantiate( intval( $_GET['connection_id'] ) ); - $new_posts = $connection->pull( $posts ); - $error_key = "external_{$connection->id}"; + // Input data check + if ( empty( $_POST['pull_post_type'] ) || empty( $_POST['connection_type'] ) || empty( $_POST['connection_id'] ) || empty( $_POST['post_id'] ) ) { + wp_send_json_error( array( 'message' => __( 'Invalid data!', 'distributor' ) ) ); + exit; + } - foreach ( $posts as $key => $post_array ) { - if ( is_wp_error( $new_posts[ $key ] ) ) { - continue; - } - \Distributor\Subscriptions\create_remote_subscription( $connection, $post_array['remote_post_id'], $new_posts[ $key ] ); - } - } else { - $site = get_site( intval( $_GET['connection_id'] ) ); - $connection = new \Distributor\InternalConnections\NetworkSiteConnection( $site ); - $new_posts = $connection->pull( $posts ); - $error_key = "internal_{$connection->site->blog_id}"; - } + // Prepare arguments + $new_post = null; + $post_type = sanitize_text_field( $_POST['pull_post_type'] ); + $post_status = ! empty( $_POST['dt_as_draft'] ) && 'draft' === $_POST['dt_as_draft'] ? 'draft' : ''; + $post = array( + 'remote_post_id' => (int) $_POST['post_id'], + 'post_type' => $post_type, + 'post_status' => $post_status, + ); - $post_id_mappings = array(); - $pull_errors = array(); + // Pull contents external or internal + if ( 'external' === $_POST['connection_type'] ) { + $connection = \Distributor\ExternalConnection::instantiate( intval( $_POST['connection_id'] ) ); + $new_posts = $connection->pull( array( $post ) ); + $new_post = is_array( $new_posts ) ? $new_posts[0] ?? null : null; - foreach ( $posts as $key => $post_array ) { - if ( is_wp_error( $new_posts[ $key ] ) ) { - $pull_errors[ $post_array['remote_post_id'] ] = [ $new_posts[ $key ]->get_error_message() ]; - continue; - } - $post_id_mappings[ $post_array['remote_post_id'] ] = $new_posts[ $key ]; + if ( ! empty( $new_post ) ) { + \Distributor\Subscriptions\create_remote_subscription( $connection, $post['remote_post_id'], $new_post ); + } + } else { + $site = get_site( intval( $_POST['connection_id'] ) ); + $connection = new \Distributor\InternalConnections\NetworkSiteConnection( $site ); + $new_posts = $connection->pull( array( $post ) ); + $new_post = is_array( $new_posts ) ? $new_posts[0] ?? null : null; + } - $media_errors = get_transient( 'dt_media_errors_' . $new_posts[ $key ] ); + if ( empty( $new_post ) || $is_error = is_wp_error( $new_post ) ) { + wp_send_json_error( array( 'message' => $is_error ? $new_post->get_error_message() : __( 'Pull Failed!', 'distributor' ) ) ); + exit; + } - if ( ! empty( $media_errors ) ) { - delete_transient( 'dt_media_errors_' . $new_posts[ $key ] ); - $pull_errors[ $post_array['remote_post_id'] ] = $media_errors; - } - } + // Delete media error + $media_errors = get_transient( 'dt_media_errors_' . $new_post ); + if ( ! empty( $media_errors ) ) { + delete_transient( 'dt_media_errors_' . $new_post ); + } - if ( ! empty( $pull_errors ) ) { - set_transient( 'dt_connection_pull_errors_' . $error_key, $pull_errors, DAY_IN_SECONDS ); - } + // Log mapping + $connection->log_sync( array( $post['remote_post_id'] => $new_post ), 0, false ); - $connection->log_sync( $post_id_mappings ); + if ( empty( $dt_pull_messages['duplicated'] ) ) { + setcookie( 'dt-syndicated', 1, time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() ); + } - if ( empty( $dt_pull_messages['duplicated'] ) ) { - setcookie( 'dt-syndicated', 1, time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() ); - } + if ( ! empty( $dt_pull_messages['duplicated'] ) ) { + setcookie( 'dt-duplicated', 1, time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() ); + } - if ( ! empty( $dt_pull_messages['duplicated'] ) ) { - setcookie( 'dt-duplicated', 1, time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() ); - } + // Redirect to the pulled content tab + wp_send_json_success( + array( + 'redirect_to' => add_query_arg( 'status', 'pulled', wp_get_referer() ), + ) + ); + exit; +} - // Redirect to the pulled content tab - wp_safe_redirect( add_query_arg( 'status', 'pulled', wp_get_referer() ) ); - exit; +/** + * Process content changing actions + * + * @since 0.8 + */ +function process_actions( ) { + global $connection_list_table; + switch ( $connection_list_table->current_action() ) { case 'bulk-skip': case 'skip': if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'dt_skip' ) && ! wp_verify_nonce( $_GET['_wpnonce'], 'bulk-distributor_page_pull' ) ) { @@ -524,8 +520,6 @@ function dashboard() { - - prepare_items(); ?> pull_error ) ) : ?> @@ -560,63 +554,3 @@ function dashboard() { id}"; - } else { - $error_key = "internal_{$connection_now->site->blog_id}"; - } - - $pull_errors = get_transient( 'dt_connection_pull_errors_' . $error_key ); - - if ( empty( $pull_errors ) ) { - return; - } - - delete_transient( 'dt_connection_pull_errors_' . $error_key ); - - $post_ids = array_keys( $pull_errors ); - - $_posts = $connection_now->remote_get( [ 'post__in' => $post_ids ] ); - $posts = []; - - if ( empty( $_posts ) ) { - return; - } - - foreach ( $_posts['items'] as $post ) { - $posts[ $post->ID ] = $post->post_title; - } - ?> - -
-

-
    - $errors ) : ?> -
  • - : -
      - -
    • - -
    -
  • - -
-
- - Date: Mon, 10 Jul 2023 16:16:16 +0600 Subject: [PATCH 3/3] ES Lint fix --- assets/js/admin-pull.js | 187 ++++++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 84 deletions(-) diff --git a/assets/js/admin-pull.js b/assets/js/admin-pull.js index 43b297471..4418771dd 100755 --- a/assets/js/admin-pull.js +++ b/assets/js/admin-pull.js @@ -1,7 +1,6 @@ import '../css/admin-pull-table.scss'; import jQuery from 'jquery'; -import { addQueryArgs } from '@wordpress/url'; import { __ } from '@wordpress/i18n'; const { document } = window; @@ -76,54 +75,65 @@ if ( chooseConnection && choosePostType && form ) { } // Pull content via ajax - jQuery( '#doaction, #doaction2' ).on( 'click', e => { + jQuery( '#doaction, #doaction2' ).on( 'click', ( e ) => { // Check action - var action = jQuery( '[name="action"]' ).val(); - if ( action != 'bulk-syndicate' ) { + if ( 'bulk-syndicate' !== jQuery( '[name="action"]' ).val() ) { return; } e.preventDefault(); openModal(); - }); + } ); - jQuery( '.distributor_page_pull .pull a' ).on( 'click', function( e ) { + jQuery( '.distributor_page_pull .pull a' ).on( 'click', function ( e ) { e.preventDefault(); - jQuery( this ).closest( 'tr' ).find( '.check-column input[type="checkbox"]' ).prop( 'checked', true ); + jQuery( this ) + .closest( 'tr' ) + .find( '.check-column input[type="checkbox"]' ) + .prop( 'checked', true ); openModal(); - }); - + } ); + function openModal() { // Prepare data - var aborted = false; - var post_ids = []; - var source_label = jQuery('#pull_connections option:selected').text(); - - jQuery( '#the-list .check-column input[type="checkbox"]:checked' ).each( function() { - var id = parseInt( jQuery( this ).val() ); - if ( id && post_ids.indexOf( id ) === -1 ) { - post_ids.push( id ); + let aborted = false; + const postIds = []; + + jQuery( '#the-list .check-column input[type="checkbox"]:checked' ).each( + function () { + const id = parseInt( jQuery( this ).val() ); + if ( id && postIds.indexOf( id ) === -1 ) { + postIds.push( id ); + } } - } ); + ); - var post_ids_count = post_ids.length; - if ( ! post_ids_count ) { - alert( 'Please select posts to pull' ); + const postIdsCount = postIds.length; + if ( ! postIdsCount ) { return; } - function log( custom_content ) { - jQuery( '#distributor-pull-modal .pull-progress' ).html( custom_content || `Pulled: ${post_ids_count-post_ids.length}/${post_ids_count}` ); + function log( customContent ) { + jQuery( '#distributor-pull-modal .pull-progress' ).html( + customContent || + `Pulled: ${ + postIdsCount - postIds.length + }/${ postIdsCount }` + ); } // Create modal for pulling via ajax + const sourceLabel = jQuery( + '#pull_connections option:selected' + ).text(); + jQuery( '#distributor-pull-modal' ).remove(); jQuery( 'body' ).append( `
-

Pulling from ${source_label}

-
Selected: ${post_ids_count}
+

Pulling from ${ sourceLabel }

+
Selected: ${ postIdsCount }

@@ -135,69 +145,78 @@ if ( chooseConnection && choosePostType && form ) { ` ); - jQuery( '#distributor-pull-modal' ).on( 'click', '[data-action="start"]', function() { - jQuery( this ).prop( 'disabled', true ); - - var excludes = [ 'post[]', 'action2', 'page', 'paged' ]; - var form_data = {}; - jQuery( '#posts-filter' ).serializeArray().forEach( field => { - if ( excludes.indexOf( field.name ) == -1 ) { - form_data[ field.name ] = field.value; - } - }); - form_data.action = 'distributor_pull_content'; - - function looper() { - if ( aborted ) { - jQuery('#distributor-pull-modal').remove() - } - - log(); - - form_data.post_id = post_ids.shift(); - var xhr = new XMLHttpRequest(); - - jQuery.ajax({ - url: window.ajaxurl, - type: 'POST', - data: form_data, - xhr: function() { - return xhr; - }, - success: function (resp) { - if ( aborted ) { - return; - } - - if ( ! resp.success || ! resp.data?.redirect_to ) { - log( `${resp.data?.message || 'Something went wrong!'}`); - return; + jQuery( '#distributor-pull-modal' ) + .on( 'click', '[data-action="start"]', function () { + jQuery( this ).prop( 'disabled', true ); + + const excludes = [ 'post[]', 'action2', 'page', 'paged' ]; + const formData = {}; + jQuery( '#posts-filter' ) + .serializeArray() + .forEach( ( field ) => { + if ( excludes.indexOf( field.name ) === -1 ) { + formData[ field.name ] = field.value; } + } ); + formData.action = 'distributor_pull_content'; - log(); - - if ( post_ids.length ) { - // Call the pull again for remaing post - looper(); - } else { - // Redirect to where it asks to - window.location.assign( resp.data.redirect_to ); - } + function looper() { + if ( aborted ) { + jQuery( '#distributor-pull-modal' ).remove(); } - }); - } - looper(); - }).on( 'click', '[data-action="cancel"]', function() { - aborted = true; - jQuery( '#distributor-pull-modal' ).remove(); - - // Refresh the page if any post already pulled. - if ( post_ids_count > post_ids.length ) { - window.location.reload(); - } - } ); - }; + log(); + + formData.post_id = postIds.shift(); + const xhr = new window.XMLHttpRequest(); + + jQuery.ajax( { + url: window.ajaxurl, + type: 'POST', + data: formData, + xhr() { + return xhr; + }, + success( resp ) { + if ( aborted ) { + return; + } + + if ( ! resp.success || ! resp.data?.redirect_to ) { + log( + `${ + resp.data?.message || + 'Something went wrong!' + }` + ); + return; + } + + log(); + + if ( postIds.length ) { + // Call the pull again for remaing post + looper(); + } else { + // Redirect to where it asks to + window.location.assign( resp.data.redirect_to ); + } + }, + } ); + } + + looper(); + } ) + .on( 'click', '[data-action="cancel"]', function () { + aborted = true; + jQuery( '#distributor-pull-modal' ).remove(); + + // Refresh the page if any post already pulled. + if ( postIdsCount > postIds.length ) { + window.location.reload(); + } + } ); + } } /**