Skip to content

Commit

Permalink
Merge branch 'develop' into fix/349
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinderk authored Nov 16, 2023
2 parents 09f07b5 + bbe7760 commit 30d31b4
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 216 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ jobs:
npx wp-env run cli "wp core version"
npx wp-env run cli "php --version"
- name: Copy .htaccess
run: npm run copy-htaccess

- name: Test
run: npm run cypress:run

Expand Down
5 changes: 0 additions & 5 deletions assets/css/admin-external-connection.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
}

.endpoint-result[data-endpoint-state="error"]::before,
.endpoint-result[data-endpoint-state="warning"]::before,
.endpoint-result[data-endpoint-state="valid"]::before {
content: " ";
display: inline-block;
Expand All @@ -129,10 +128,6 @@
background-color: #62ff00;
}

.endpoint-result[data-endpoint-state="warning"]::before {
background-color: #ffe000;
}

.endpoint-result .dashicons-yes {
color: #46b450;
}
Expand Down
4 changes: 0 additions & 4 deletions assets/css/admin-external-connections.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@
.connection-status.valid {
background-color: #62ff00;
}

.connection-status.warning {
background-color: #ffe000;
}
95 changes: 28 additions & 67 deletions assets/js/admin-external-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ function checkConnections() {
.done( ( response ) => {
if ( ! response.success ) {
endpointResult.setAttribute( 'data-endpoint-state', 'error' );
} else if ( response.data.errors.no_external_connection ) {
} else if (
response.data.errors.no_external_connection ||
'no' === response.data.is_authenticated ||
response.data.errors.no_distributor ||
! response.data.can_post.length
) {
endpointResult.setAttribute( 'data-endpoint-state', 'error' );

if ( response.data.endpoint_suggestion ) {
Expand All @@ -337,88 +342,44 @@ function checkConnections() {
}`,
'polite'
);
} else {
} else if ( response.data.errors.no_distributor ) {
endpointResult.innerText = __(
'No connection found.',
'distributor'
);

speak(
__( 'No connection found.', 'distributor' ),
'polite'
);
}
} else if (
response.data.errors.no_distributor ||
! response.data.can_post.length
) {
endpointResult.setAttribute( 'data-endpoint-state', 'warning' );
endpointResult.innerText = __(
'Limited connection established.',
'distributor'
);

const warnings = [];

if ( response.data.errors.no_distributor ) {
endpointResult.innerText += ` ${ __(
'Distributor not installed on remote site.',
'distributor'
) }`;
);
speak(
`${ __(
'Limited connection established.',
'distributor'
) } ${ __(
__(
'Distributor not installed on remote site.',
'distributor'
) }`,
),
'polite'
);
} else {
} else if (
'no' === response.data.is_authenticated ||
response.data.errors.no_types
) {
endpointResult.innerText = __(
'Authentication failed due to insufficient or invalid credentials.',
'distributor'
);
speak(
`${ __(
'Limited connection established.',
__(
'Authentication failed due to insufficient or invalid credentials.',
'distributor'
) }`,
),
'polite'
);
}

if ( 'no' === response.data.is_authenticated ) {
warnings.push(
__(
'Authentication failed due to invalid credentials.',
'distributor'
)
} else {
endpointResult.innerText = __(
'No connection found.',
'distributor'
);
}

if ( 'yes' === response.data.is_authenticated ) {
warnings.push(
__(
'Authentication succeeded but your account does not have permissions to create posts on the external site.',
'distributor'
)
speak(
__( 'No connection found.', 'distributor' ),
'polite'
);
}

warnings.push(
__( 'Push distribution unavailable.', 'distributor' )
);
warnings.push(
__(
'Pull distribution limited to basic content, i.e. title and content body.',
'distributor'
)
);

warnings.forEach( ( warning ) => {
const warningNode = document.createElement( 'li' );
warningNode.innerText = warning;

endpointErrors.append( warningNode );
} );
} else {
endpointResult.setAttribute( 'data-endpoint-state', 'valid' );
endpointResult.innerText = __(
Expand Down
110 changes: 107 additions & 3 deletions docs/connect-to-an-existing-post.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,107 @@
1. Connect the two sites
1. On Site A (source) add the meta data items `dt_subscriptions` and `dt_connection_map`
1. On Site B (distributed) add the meta data items `dt_original_post_url`, `dt_original_site_name`, `dt_original_site_url`, `dt_subscription_signature`, `dt_original_post_id`, `dt_original_source_id`
In a scenario where you have existing sites that share content and that content was in place prior to the addition of Distributor, it is useful to be able to connect that content together without having to delete one and push/pull the other one. Behind the scenes, Distributor stores a handful of details in post meta, so these details will need to be manually added for the links to work.

At it's simplest, you'll need to connect the two sites together and then add the proper meta (as detailed below) for the items that should be linked together, both on the source site and the receiving site.

## Network Connections

There are different pieces of data that will need to be set on each side of a connection, what we'll call as the source site and the receiving site.

The only piece of data needed on the source site to connect two items in Network Connections is the `dt_connection_map`. This is a serialized array of data that contains the mapping from the site ID to the post ID, along with a timestamp of when the item was linked. Note that the site ID and post ID should correspond to the data on the receiving site, not source site.

This data structure will look like the following:

```php
$data = [
[
'external' => [],
'internal' => [
2 => [
'post_id' => 674,
'time' => 1693494494,
],
],
],
];
```

In the above example, the `external` array will always be empty, unless an item is also linked to an external site. For the `internal` array, each item in the array will have a key that corresponds to the site ID (2 in the example above) and then the `post_id` should be the destination post ID.

As a further example, if I have a post with an ID of 100 that lives on a site with an ID of 1 and I want that post to be linked to an existing post with an ID of 50 on the site with an ID of 2, the `dt_connection_map` data that is stored with the original item (ID of 100, site ID 1) would look like the following:

```php
$data = [
[
'external' => [],
'internal' => [
2 => [
'post_id' => 50,
'time' => 1693494494,
],
],
],
];
update_post_meta( 100, 'dt_connection_map', $data );
```

And then on the receiving site, the following data is needed: `dt_original_post_id`, `dt_original_post_url`, `dt_original_blog_id` and `dt_syndicate_time`. These should all be fairly self explanatory but to use the same example from above:

- `dt_original_post_id` would be set to 100
- `dt_original_post_url` would be the full URL of that post with an ID of 100
- `dt_original_blog_id` would be set to 1
- `dt_syndicate_time` should match the same timestamp set in the connection map, in this case 1693494494

## External Connections

External Connections share a similar data structure as detailed above but they also contain a Subscriptions piece, which is more complicated to manually replicate (as such, full details on how to replicate the Subscription is not outlined here).

Similar to the above, there's a data mapping that is needed on the source site: `dt_connection_map`. This is a serialized array of data that contains the mapping from the external connection ID to the post ID, along with a timestamp of when the item was linked. Note that the post ID should correspond to the data on the receiving site, not source site.

This data structure will look like the following:

```php
$data = [
[
'external' => [
2 => [
'post_id' => 50,
'time' => 1693494494,
],
],
'internal' => [],
],
];
```

In the above example, the `internal` array will always be empty, unless an item is also linked to an interal site. For the `external` array, each item in the array will have a key that corresponds to the connection ID (2 in the example above) and then the `post_id` should be the destination post ID.

As a further example, if I have a post with an ID of 100 that lives on a site with an ID of 1 and I want that post to be linked to an existing post with an ID of 50 on the site with a connection ID of 2, the `dt_connection_map` data that is stored with the original item (ID of 100, site ID 1) would look like the following:

```php
$data = [
[
'external' => [
2 => [
'post_id' => 50,
'time' => 1693494494,
],
],
'internal' => [],
],
];
update_post_meta( 100, 'dt_connection_map', $data );
```

In addition, there needs to be a piece of data on the source site that contains the subscription information: `dt_subscriptions`. This contains a serialized array of data that links a hashed signature to the subscription post ID. This isn't something that is easily manually reproduced. Suggestion is to look at using the existing `Distributor\Subscriptions\create_subscription` function to replicate this data.

And then on the receiving site, the following data is needed: `dt_original_post_id`, `dt_original_post_url`, `dt_original_source_id`, `dt_original_site_name`, `dt_original_site_url`, `dt_subscription_signature`, `dt_full_connection` and `dt_syndicate_time`.

From the example above:

- `dt_original_post_id` would be set to 100
- `dt_original_post_url` would be the full URL of that post with an ID of 100
- `dt_original_source_id` would be set to 2 (the source connection ID)
- `dt_original_site_name` would be the name of the source site
- `dt_original_site_url` would be the URL of the source site
- `dt_subscription_signature` would be the signature of the subscription mentioned above
- `dt_full_connection` would be set to `true`
- `dt_syndicate_time` should match the same timestamp set in the connection map, in this case 1693494494
23 changes: 23 additions & 0 deletions docs/snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,26 @@ add_filter( 'dt_excluded_meta', function( $meta_keys ) {
return $meta_keys;
} );
```

### Turn off automatic updates for distributed content

```php
/**
* Prevent auto-updates from happening for network connections.
*/
add_action(
'init',
function() {
remove_action( 'wp_after_insert_post', [ '\Distributor\InternalConnections\NetworkSiteConnection', 'update_syndicated' ], 99 );
}
);

/**
* Prevent auto-updates from happening for external connections.
*/
add_action(
'init',
function() {
remove_action( 'wp_after_insert_post', 'Distributor\Subscriptions\send_notifications', 99 );
}
);
2 changes: 1 addition & 1 deletion includes/debug-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function get_external_connection_status( $external_connection_status ) {
}

if ( empty( $external_connection_status['can_post'] ) ) {
$status = __( 'warning', 'distributor' );
$status = __( 'error', 'distributor' );
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/external-connection-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function output_status_column( $column_name, $post_id ) {
}

if ( empty( $external_connection_status['can_post'] ) ) {
$status = 'warning';
$status = 'error';
}
}

Expand Down
7 changes: 5 additions & 2 deletions includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,11 @@ function check_post_types_permissions() {
);

foreach ( $types as $type ) {
$caps = $type->cap;
$response['can_get'][] = $type->name;
$caps = $type->cap;

if ( current_user_can( $caps->edit_posts ) ) {
$response['can_get'][] = $type->name;
}

if ( current_user_can( $caps->edit_posts ) && current_user_can( $caps->create_posts ) && current_user_can( $caps->publish_posts ) ) {
$response['can_post'][] = $type->name;
Expand Down
Loading

0 comments on commit 30d31b4

Please sign in to comment.