Skip to content

Commit

Permalink
deploy: 5d24078
Browse files Browse the repository at this point in the history
  • Loading branch information
dkotter committed Nov 29, 2023
1 parent 82cc286 commit 6782583
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 14 deletions.
24 changes: 22 additions & 2 deletions classes_DistributorPost.php.html
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,9 @@ <h1 class="page-title">Source: classes/DistributorPost.php</h1>
'excerpt' => $this->post->post_excerpt,
'parent' => ! empty( $this->post->post_parent ) ? (int) $this->post->post_parent : 0,
'status' => $this->post->post_status,
'date' => $this->post->post_date,
'date_gmt' => $this->post->post_date_gmt,

'distributor_media' => $this->get_media(),
'distributor_terms' => $this->get_terms(),
'distributor_meta' => $this->get_meta(),
Expand Down Expand Up @@ -890,11 +893,19 @@ <h1 class="page-title">Source: classes/DistributorPost.php</h1>
if ( ! empty( $args['remote_post_id'] ) ) {
// Updating an existing post.
$insert['ID'] = (int) $args['remote_post_id'];
// Never update the post status when updating a post.
unset( $insert['post_status'] );
} elseif ( ! empty( $args['post_status'] ) ) {
$insert['post_status'] = $args['post_status'];
}

if ( ! empty( $args['post_status'] ) ) {
$insert['post_status'] = $args['post_status'];
if (
isset( $insert['post_status'] )
&amp;&amp; 'future' === $insert['post_status']
) {
// Set the post date to the future date.
$insert['post_date'] = $post_data['date'];
$insert['post_date_gmt'] = $post_data['date_gmt'];
}

// Post meta used by wp_insert_post, wp_update_post.
Expand Down Expand Up @@ -954,6 +965,15 @@ <h1 class="page-title">Source: classes/DistributorPost.php</h1>
protected function to_rest( $rest_args = array() ) {
$post_data = $this->post_data();

/*
* Unset dates.
*
* External connections do not allow for the pulling or pushing of
* scheduled posts so these can be ignored.
*/
unset( $post_data['date'] );
unset( $post_data['date_gmt'] );

if ( ! empty( $post_data['parent'] ) ) {
$post_data['distributor_original_post_parent'] = (int) $post_data['parent'];
}
Expand Down
4 changes: 2 additions & 2 deletions classes_InternalConnections_NetworkSiteConnection.php.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ <h1 class="page-title">Source: classes/InternalConnections/NetworkSiteConnection
$current_blog_id = get_current_blog_id();

if ( ! empty( $post_props['meta']['dt_connection_map'] ) ) {
foreach ( $post_props['meta']['dt_connection_map'] as $distributed ) {
foreach ( $post_props['meta']['dt_connection_map'] as $connection_type => $distributed ) {
$distributed = maybe_unserialize( $distributed );

if ( array_key_exists( $current_blog_id, $distributed['internal'] ) ) {
if ( 'internal' === $connection_type &amp;&amp; array_key_exists( $current_blog_id, $distributed ) ) {
$dt_pull_messages['duplicated'] = 1;
continue 2;
}
Expand Down
2 changes: 1 addition & 1 deletion debug-info.php.html
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ <h1 class="page-title">Source: debug-info.php</h1>
}

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

Expand Down
2 changes: 1 addition & 1 deletion dt_get_pull_content_rest_query_args.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="rest-api.php.html">rest-api.php</a>, <a href="rest-api.php.html#line674">line 674</a>
<a href="rest-api.php.html">rest-api.php</a>, <a href="rest-api.php.html#line677">line 677</a>
</li></ul></dd>


Expand Down
2 changes: 1 addition & 1 deletion external-connection-cpt.php.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h1 class="page-title">Source: external-connection-cpt.php</h1>
}

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

Expand Down
7 changes: 5 additions & 2 deletions rest-api.php.html
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,11 @@ <h1 class="page-title">Source: rest-api.php</h1>
);

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 ) &amp;&amp; current_user_can( $caps->create_posts ) &amp;&amp; current_user_can( $caps->publish_posts ) ) {
$response['can_post'][] = $type->name;
Expand Down
89 changes: 84 additions & 5 deletions tutorial-connect-to-an-existing-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,90 @@ <h2>Connect to an Existing Post</h2>
</header>

<article>
<ol>
<li>Connect the two sites</li>
<li>On Site A (source) add the meta data items <code>dt_subscriptions</code> and <code>dt_connection_map</code></li>
<li>On Site B (distributed) add the meta data items <code>dt_original_post_url</code>, <code>dt_original_site_name</code>, <code>dt_original_site_url</code>, <code>dt_subscription_signature</code>, <code>dt_original_post_id</code>, <code>dt_original_source_id</code></li>
</ol>
<p>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.</p>
<p>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.</p>
<h2 id="network-connections">Network Connections</h2>
<p>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.</p>
<p>The only piece of data needed on the source site to connect two items in Network Connections is the <code>dt_connection_map</code>. 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.</p>
<p>This data structure will look like the following:</p>
<pre class="prettyprint source lang-php"><code>$data = [
[
'external' => [],
'internal' => [
2 => [
'post_id' => 674,
'time' => 1693494494,
],
],
],
];
</code></pre>
<p>In the above example, the <code>external</code> array will always be empty, unless an item is also linked to an external site. For the <code>internal</code> array, each item in the array will have a key that corresponds to the site ID (2 in the example above) and then the <code>post_id</code> should be the destination post ID.</p>
<p>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 <code>dt_connection_map</code> data that is stored with the original item (ID of 100, site ID 1) would look like the following:</p>
<pre class="prettyprint source lang-php"><code>$data = [
[
'external' => [],
'internal' => [
2 => [
'post_id' => 50,
'time' => 1693494494,
],
],
],
];
update_post_meta( 100, 'dt_connection_map', $data );
</code></pre>
<p>And then on the receiving site, the following data is needed: <code>dt_original_post_id</code>, <code>dt_original_post_url</code>, <code>dt_original_blog_id</code> and <code>dt_syndicate_time</code>. These should all be fairly self explanatory but to use the same example from above:</p>
<ul>
<li><code>dt_original_post_id</code> would be set to 100</li>
<li><code>dt_original_post_url</code> would be the full URL of that post with an ID of 100</li>
<li><code>dt_original_blog_id</code> would be set to 1</li>
<li><code>dt_syndicate_time</code> should match the same timestamp set in the connection map, in this case 1693494494</li>
</ul>
<h2 id="external-connections">External Connections</h2>
<p>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).</p>
<p>Similar to the above, there's a data mapping that is needed on the source site: <code>dt_connection_map</code>. 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.</p>
<p>This data structure will look like the following:</p>
<pre class="prettyprint source lang-php"><code>$data = [
[
'external' => [
2 => [
'post_id' => 50,
'time' => 1693494494,
],
],
'internal' => [],
],
];
</code></pre>
<p>In the above example, the <code>internal</code> array will always be empty, unless an item is also linked to an interal site. For the <code>external</code> array, each item in the array will have a key that corresponds to the connection ID (2 in the example above) and then the <code>post_id</code> should be the destination post ID.</p>
<p>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 <code>dt_connection_map</code> data that is stored with the original item (ID of 100, site ID 1) would look like the following:</p>
<pre class="prettyprint source lang-php"><code>$data = [
[
'external' => [
2 => [
'post_id' => 50,
'time' => 1693494494,
],
],
'internal' => [],
],
];
update_post_meta( 100, 'dt_connection_map', $data );
</code></pre>
<p>In addition, there needs to be a piece of data on the source site that contains the subscription information: <code>dt_subscriptions</code>. 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 <code>Distributor\Subscriptions\create_subscription</code> function to replicate this data.</p>
<p>And then on the receiving site, the following data is needed: <code>dt_original_post_id</code>, <code>dt_original_post_url</code>, <code>dt_original_source_id</code>, <code>dt_original_site_name</code>, <code>dt_original_site_url</code>, <code>dt_subscription_signature</code>, <code>dt_full_connection</code> and <code>dt_syndicate_time</code>.</p>
<p>From the example above:</p>
<ul>
<li><code>dt_original_post_id</code> would be set to 100</li>
<li><code>dt_original_post_url</code> would be the full URL of that post with an ID of 100</li>
<li><code>dt_original_source_id</code> would be set to 2 (the source connection ID)</li>
<li><code>dt_original_site_name</code> would be the name of the source site</li>
<li><code>dt_original_site_url</code> would be the URL of the source site</li>
<li><code>dt_subscription_signature</code> would be the signature of the subscription mentioned above</li>
<li><code>dt_full_connection</code> would be set to <code>true</code></li>
<li><code>dt_syndicate_time</code> should match the same timestamp set in the connection map, in this case 1693494494</li>
</ul>
</article>

</section>
Expand Down
21 changes: 21 additions & 0 deletions tutorial-snippets.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ <h3 id="exclude-meta-key-from-distribution">Exclude meta key from distribution</
return $meta_keys;
} );
</code></pre>
<h3 id="turn-off-automatic-updates-for-distributed-content">Turn off automatic updates for distributed content</h3>
<pre class="prettyprint source lang-php"><code>/**
* 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 );
}
);
</code></pre>
</article>

</section>
Expand Down

0 comments on commit 6782583

Please sign in to comment.