Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull contents via ajax #1104

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions assets/css/admin-pull-table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
159 changes: 151 additions & 8 deletions assets/js/admin-pull.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +14,7 @@
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[
Expand All @@ -25,6 +25,7 @@
} );

if ( chooseConnection && choosePostType && form ) {
// Handle post type selection
if ( choosePostTypeBtn ) {
jQuery( choosePostTypeBtn ).on( 'click', ( event ) => {
event.preventDefault();
Expand All @@ -35,6 +36,7 @@
} );
}

// Handle search button click
if ( searchField && searchBtn ) {
jQuery( searchBtn ).on( 'click', ( event ) => {
event.preventDefault();
Expand All @@ -47,33 +49,174 @@
} );
}

if ( asDraftCheckboxes && pullLinks ) {
// Handle pull mode checkbox event
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.
for ( let i = 0; i < asDraftCheckboxes.length; ++i ) {
asDraftCheckboxes[ i ].checked = true;
}

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 {
// 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;
}

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
if ( 'bulk-syndicate' !== jQuery( '[name="action"]' ).val() ) {
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
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 );
}
}
);

const postIdsCount = postIds.length;
if ( ! postIdsCount ) {
return;
}

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(
`
<div id="distributor-pull-modal">
<div>
<div class="pull-head-section">
<h3>Pulling from <b>${ sourceLabel }</b></h3>
<div class="pull-progress">Selected: ${ postIdsCount }</div>
</div>
<br/>
<div id="pull-button-container">
<button class="button button-secondary" data-action="cancel">Cancel</button>
<button class="button button-primary" data-action="start">Start</button>
</div>
</div>
</div>
`
);

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';

function looper() {
if ( aborted ) {
jQuery( '#distributor-pull-modal' ).remove();
}

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(
`<span style="color:#a00;">${
resp.data?.message ||
'Something went wrong!'
}</span>`
);
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();
}
} );
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/classes/PullListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function column_name( $item ) {

$actions = [
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`.
'pull' => sprintf( '<a href="%s">%s</a>', 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( '<a href="#">%s</a>', $draft ? esc_html__( 'Pull as draft', 'distributor' ) : esc_html__( 'Pull', 'distributor' ) ),
'view' => '<a href="' . esc_url( $item->link ) . '">' . esc_html__( 'View', 'distributor' ) . '</a>',
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`.
'skip' => sprintf( '<a href="%s">%s</a>', 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' ) ),
Expand All @@ -355,7 +355,7 @@ public function column_name( $item ) {

$actions = [
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`.
'pull' => sprintf( '<a href="%s">%s</a>', 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( '<a href="#">%s</a>', $draft ? esc_html__( 'Pull as draft', 'distributor' ) : esc_html__( 'Pull', 'distributor' ) ),
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- see `wp_fix_server_vars()`.
'unskip' => sprintf( '<a href="%s">%s</a>', 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' => '<a href="' . esc_url( $item->link ) . '">' . esc_html__( 'View', 'distributor' ) . '</a>',
Expand Down
Loading
Loading