Skip to content
Merged
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
1 change: 1 addition & 0 deletions assets/css/admin-external-connection.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

#dt_external_connection_details p,
#dt_external_connection_details .dt-roles-allowed,
#dt_external_connection_details .post-types-permissions,
#dt_external_connection_details .connection-field-wrap {
margin-bottom: 2em;
}
Expand Down
5 changes: 5 additions & 0 deletions assets/css/admin-pull-table.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
.status-sync .check-column {
display: none;
}

#pull-errors ul ul {
list-style: disc;
padding-left: 20px;
}
10 changes: 9 additions & 1 deletion assets/css/push.css
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
}
}

#wpadminbar #distributor-push-wrapper .messages * {
#wpadminbar #distributor-push-wrapper .messages > * {
display: none;
}

Expand All @@ -402,6 +402,14 @@
display: block;
}

#wpadminbar #distributor-push-wrapper .messages ul.details {
padding-left: 1.5rem;
}

#wpadminbar #distributor-push-wrapper .messages ul.details li {
list-style: disc;
}

#distributor-push-wrapper .loader-item {
position: relative;
display: -webkit-box;
Expand Down
29 changes: 25 additions & 4 deletions assets/js/admin-external-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,22 @@ jQuery( authorizeConnectionButton ).on( 'click', ( event ) => {
&& ! Object.prototype.hasOwnProperty.call( response.data, 'version' )
) {
jQuery( wizardError[0] ).text( dt.no_distributor );
} else {
jQuery( wizardError[0] ).text( dt.noconnection );
return;
}

jQuery( wizardError[0] ).text( dt.noconnection );

if (
Object.prototype.hasOwnProperty.call( response, 'data' )
&& Array.isArray( response.data )
&& 0 < response.data.length
&& Object.prototype.hasOwnProperty.call( response.data[0], 'code' )
&& Object.prototype.hasOwnProperty.call( response.data[0], 'message' )
) {
jQuery( wizardError[0] ).append( '<br/>' );
response.data.forEach( ( error ) => {
jQuery( wizardError[0] ).append( `${error.message} ${error.code ? `(${ error.code })` : ''} <br/>` );
} );
}

return;
Expand Down Expand Up @@ -249,8 +263,15 @@ function checkConnections() {
endpointResult.innerText += ` ${ dt.no_distributor }`;
wp.a11y.speak( `${ dt.limited_connection } ${ dt.no_distributor }`, 'polite' );
} else {
endpointResult.innerText += ` ${ dt.bad_auth }`;
wp.a11y.speak( `${ dt.limited_connection } ${ dt.bad_auth }`, 'polite' );
wp.a11y.speak( `${ dt.limited_connection }`, 'polite' );
}

if ( 'no' === response.data.is_authenticated ) {
warnings.push( dt.bad_auth );
}

if ( 'yes' === response.data.is_authenticated ) {
warnings.push( dt.no_permissions );
}

warnings.push( dt.no_push );
Expand Down
71 changes: 57 additions & 14 deletions assets/js/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jQuery( window ).on( 'load', () => {
let connectionsSearchInput = '';
let postStatusInput = '';
let asDraftInput = '';
let errorDetails = '';

distributorMenuItem.appendChild( distributorPushWrapper );

Expand All @@ -56,6 +57,7 @@ jQuery( window ).on( 'load', () => {
connectionsSearchInput = document.getElementById( 'dt-connection-search' );
postStatusInput = document.getElementById( 'dt-post-status' );
asDraftInput = document.getElementById( 'dt-as-draft' );
errorDetails = document.querySelector( '.dt-error ul.details' );

if ( null !== connectionsNewList ){
connectionsNewListChildren = connectionsNewList.querySelectorAll( '.add-connection' );
Expand Down Expand Up @@ -87,33 +89,69 @@ jQuery( window ).on( 'load', () => {
/**
* Handle UI error changes
*/
function doError() {
function doError( messages ) {
distributorPushWrapper.classList.add( 'message-error' );
errorDetails.innerText = '';

setTimeout( () => {
distributorPushWrapper.classList.remove( 'message-error' );
}, 6000 );
_.each( prepareMessages( messages ), function( message ) {
const errorItem = document.createElement( 'li' );
errorItem.innerText = message;
errorDetails.appendChild( errorItem );
} );
}

/**
* Prepare error messages for printing.
*
* @param {string|array} messages Error messages.
*/
function prepareMessages( messages ) {
if ( ! _.isArray( messages ) ) {
return [ messages ];
}

return _.map( messages, function( message ) {
if ( _.isString( message ) ) {
return message;
}
if ( _.has( message, 'message' ) ) {
return message.message;
}
} );
}

/**
* Handle UI success changes
*/
function doSuccess( results ) {
let error = false;
let success = false;
const errors = {};

[ 'internal', 'external' ].map( type => {
_.each( results[type], ( result, connectionId ) => {
if ( 'fail' === result.status ) {
error = true;
} else {
if ( 'success' === result.status ) {
dtConnections[ `${type}${ connectionId }` ].syndicated = result.url;
success = true;
}

if ( ! _.isEmpty( result.errors ) ) {
errors[ `${type}${ connectionId }` ] = result.errors;
}
} );
} );

if ( error ) {
doError();
} else {
if ( ! _.isEmpty( errors ) ) {
const formattedErrors = _.map( errors, function( messages, connectionId ) {
return `${dtConnections[ connectionId ].name }:\n${
_.map( messages, function( message ) {
return `- ${message}\n`;
} )}`;
} );

doError( formattedErrors );
}

if ( success && _.isEmpty( errors ) ) {
distributorPushWrapper.classList.add( 'message-success' );

connectionsSelected.classList.add( 'empty' );
Expand Down Expand Up @@ -271,18 +309,23 @@ jQuery( window ).on( 'load', () => {
setTimeout( () => {
distributorTopMenu.classList.remove( 'syncing' );

if ( ! response.success ) {
doError( response.data );
return;
}

if ( ! response.data || ! response.data.results ) {
doError();
doError( dt.messages.empty_result );
return;
}

doSuccess( response.data.results );
}, 500 );
} ).error( () => {
} ).error( ( xhr, textStatus, errorThrown ) => {
setTimeout( () => {
distributorTopMenu.classList.remove( 'syncing' );

doError();
doError( `${dt.messages.ajax_error} ${errorThrown}` );
}, 500 );
} );
} );
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Connection {
* @param int $item_id Item ID to push if an update.
* @param array $args Array of args to push.
* @since 0.8
* @return bool|WP_Error
* @return array|\WP_Error
*/
abstract public function push( $item_id, $args = array() );

Expand Down
114 changes: 60 additions & 54 deletions includes/classes/ExternalConnections/WordPressExternalConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public function pull( $items ) {
* @param int $post_id Post id
* @param array $args Post args to push.
* @since 0.8
* @return bool|\WP_Error
* @return array|\WP_Error
*/
public function push( $post_id, $args = array() ) {
if ( empty( $post_id ) ) {
Expand Down Expand Up @@ -658,7 +658,15 @@ public function push( $post_id, $args = array() ) {
\Distributor\Subscriptions\create_subscription( $post_id, $body_array['id'], untrailingslashit( $this->base_url ), $signature );
}

return $body_array['id'];
$remote_post = array(
'id' => $body_array['id'],
);

if ( ! empty( $body_array['push-errors'] ) ) {
$remote_post['push-errors'] = $body_array['push-errors'];
}

return $remote_post;
}

/**
Expand Down Expand Up @@ -733,6 +741,7 @@ public function check_connections() {
} else {
$response = wp_remote_get( untrailingslashit( $this->base_url ), $this->auth_handler->format_get_args( array( 'timeout' => self::$timeout ) ) );
}

$body = wp_remote_retrieve_body( $response );

if ( is_wp_error( $response ) || empty( $body ) ) {
Expand Down Expand Up @@ -787,70 +796,67 @@ public function check_connections() {
} else {
$types_response = wp_remote_get( $types_path, $this->auth_handler->format_get_args( array( 'timeout' => self::$timeout ) ) );
}

$types_body = wp_remote_retrieve_body( $types_response );
$types = json_decode( $types_body, true );

if ( is_wp_error( $types_response ) || empty( $types_body ) ) {
if ( is_wp_error( $types_response ) || 200 !== wp_remote_retrieve_response_code( $types_response ) || empty( $types_body ) || empty( $types ) ) {
$output['errors']['no_types'] = 'no_types';
} else {
$types = json_decode( $types_body, true );
$can_get = array();
$can_post = array();

if ( 200 !== wp_remote_retrieve_response_code( $types_response ) || empty( $types ) ) {
$output['errors']['no_types'] = 'no_types';
} else {
$can_get = array();
$can_post = array();

$permission_url = untrailingslashit( $this->base_url ) . '/' . self::$namespace . '/distributor/post-types-permissions';

if ( function_exists( 'vip_safe_wp_remote_get' ) && \Distributor\Utils\is_vip_com() ) {
$permission_response = vip_safe_wp_remote_get(
$permission_url,
false,
3,
3,
10,
$this->auth_handler->format_get_args()
);
} else {
$permission_url = untrailingslashit( $this->base_url ) . '/' . self::$namespace . '/distributor/post-types-permissions';

$permission_response = wp_remote_get(
$permission_url,
$this->auth_handler->format_get_args(
array(
'timeout' => self::$timeout,
)
if ( function_exists( 'vip_safe_wp_remote_get' ) && \Distributor\Utils\is_vip_com() ) {
$permission_response = vip_safe_wp_remote_get(
$permission_url,
false,
3,
3,
10,
$this->auth_handler->format_get_args()
);
} else {
$permission_response = wp_remote_get(
$permission_url,
$this->auth_handler->format_get_args(
array(
'timeout' => self::$timeout,
)
);
}
)
);
}

$permissions = json_decode( wp_remote_retrieve_body( $permission_response ) );
$permissions = json_decode( wp_remote_retrieve_body( $permission_response ) );

if (
is_wp_error( $permission_response )
|| empty( $permissions )
|| ! isset( $permissions->can_get )
|| ! isset( $permissions->can_post )
) {
$output['errors']['no_permissions'] = 'no_permissions';
} else {
$can_get = array_values(
array_filter(
$permissions->can_get,
[ $this, 'not_distributor_internal_post_type' ]
)
);
$output['is_authenticated'] = isset( $permissions->is_authenticated ) ? $permissions->is_authenticated : 'na';

$can_post = array_values(
array_filter(
$permissions->can_post,
[ $this, 'not_distributor_internal_post_type' ]
)
);
}
if (
is_wp_error( $permission_response )
|| empty( $permissions )
|| ! isset( $permissions->can_get )
|| ! isset( $permissions->can_post )
) {
$output['errors']['no_permissions'] = 'no_permissions';
} else {
$can_get = array_values(
array_filter(
$permissions->can_get,
[ $this, 'not_distributor_internal_post_type' ]
)
);

$output['can_get'] = $can_get;
$output['can_post'] = $can_post;
$can_post = array_values(
array_filter(
$permissions->can_post,
[ $this, 'not_distributor_internal_post_type' ]
)
);
}

$output['can_get'] = $can_get;
$output['can_post'] = $can_post;
}

return $output;
Expand Down
Loading