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

Show 'View' link in push UI for external connections #571

Merged
merged 2 commits into from
Apr 10, 2020
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
22 changes: 8 additions & 14 deletions assets/js/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,14 @@ jQuery( window ).on( 'load', () => {
function doSuccess( results ) {
let error = false;

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

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

if ( error ) {
Expand Down
66 changes: 54 additions & 12 deletions includes/push-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,25 @@ function get_connections() {
$connection = \Distributor\ExternalConnection::instantiate( $external_connection->ID );

if ( ! is_wp_error( $connection ) ) {
$syndicated = false;

if ( ! empty( $connection_map['external'][ (int) $external_connection->ID ] ) ) {
$post_syndicated = $connection_map['external'][ (int) $external_connection->ID ];
if ( ! empty( $post_syndicated['post_id'] ) ) {
$syndicated = sprintf(
'%1$s/?p=%2$d',
get_site_url_from_rest_url( $connection->base_url ),
$post_syndicated['post_id']
);
}
}

$dom_connections[ 'external' . $connection->id ] = [
'type' => 'external',
'id' => $connection->id,
'url' => $connection->base_url,
'name' => $connection->name,
'syndicated' => ! empty( $connection_map['external'][ (int) $external_connection->ID ] ) ? $connection_map['external'][ (int) $external_connection->ID ] : false,
'syndicated' => $syndicated,
];
}
}
Expand Down Expand Up @@ -281,6 +294,11 @@ function ajax_push() {
'post_id' => (int) $remote_id,
'date' => date( 'F j, Y g:i a' ),
'status' => 'success',
'url' => sprintf(
'%1$s/?p=%2$d',
get_site_url_from_rest_url( $external_connection_url ),
(int) $remote_id
),
);

$external_connection->log_sync( array( $remote_id => $_POST['postId'] ) );
Expand Down Expand Up @@ -412,6 +430,32 @@ function menu_button( $wp_admin_bar ) {
);
}

/**
* Get Site URL from REST URL.
* We can not assume the REST API prefix is wp-json because it can be changed to
* a custom prefix.
*
* @param string $rest_url REST URL. Eg: domain.com/wp-json
*
* @return string Site URL.
*/
function get_site_url_from_rest_url( $rest_url ) {
$_url = explode( '/', $rest_url );

if ( count( $_url ) < 2 ) {
return $rest_url;
}

array_pop( $_url );
$url = implode( '/', $_url );

if ( false === strpos( $url, 'http' ) ) {
$url = '//' . $url;
}

return $url;
}

/**
* Build distributor push menu dropdown HTML
*
Expand Down Expand Up @@ -470,18 +514,16 @@ function menu_content() {

<div class="new-connections-list">
<# for ( var key in connections ) { #>
<# if ( 'external' === connections[ key ]['type'] ) { #>
<div class="add-connection<# if ( ! _.isEmpty( connections[ key ]['syndicated'] ) ) { #> syndicated<# } #>" data-connection-type="external" data-connection-id="{{ connections[ key ]['id'] }}">
<div class="add-connection<# if ( ! _.isEmpty( connections[ key ]['syndicated'] ) ) { #> syndicated<# } #>" data-connection-type="{{ connections[ key ]['type'] }}" data-connection-id="{{ connections[ key ]['id'] }}">
<# if ( 'external' === connections[ key ]['type'] ) { #>
<span>{{ connections[ key ]['name'] }}</span>
</div>
<# } else { #>
<div class="add-connection<# if ( ! _.isEmpty( connections[ key ]['syndicated'] ) ) { #> syndicated<# } #>" data-connection-type="internal" data-connection-id="{{ connections[ key ]['id'] }}">
<# } else { #>
<span>{{ connections[ key ]['url'] }}</span>
<# if ( ! _.isEmpty( connections[ key ]['syndicated'] ) ) { #>
<a href="{{ connections[ key ]['syndicated'] }}"><?php esc_html_e( 'View', 'distributor' ); ?></a>
<# } #>
</div>
<# } #>
<# } #>
<# if ( ! _.isEmpty( connections[ key ]['syndicated'] ) && connections[ key ]['syndicated'] ) { #>
<a href="{{ connections[ key ]['syndicated'] }}"><?php esc_html_e( 'View', 'distributor' ); ?></a>
<# } #>
</div>
<# } #>
</div>
</div>
Expand Down Expand Up @@ -543,7 +585,7 @@ function menu_content() {
<span>{{{ connection.name }}}</span>
<# } #>

<# if ('internal' === connection.type && connection.syndicated) { #>
<# if (connection.syndicated) { #>
<a href="{{ connection.syndicated }}"><?php esc_html_e( 'View', 'distributor' ); ?></a>
<# } #>
</div>
Expand Down
1 change: 1 addition & 0 deletions tests/wpacceptance/PushMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testConnectionCrossOutOnPush() {

// See crossed out element
$I->seeElement( '#distributor-push-wrapper .new-connections-list .add-connection.syndicated[data-connection-id="2"]' );
$I->seeText( 'View', '#distributor-push-wrapper .new-connections-list .add-connection.syndicated[data-connection-id="2"]' );
}

/**
Expand Down