Skip to content

Conversation

@miguelpeixe
Copy link
Member

@miguelpeixe miguelpeixe commented Dec 3, 2024

By the nature of webhooks, requests might not get processed in order so there's a chance an older version of a distributed post reaching a node. To prevent that from replacing more recent content, we'll use post_modified_gmt to bail if the stored payload is more recent.

This PR also changes the post payload date to use post_date_gmt. This change allows sites to handle timezones.

Testing

  1. Make sure you have a network setup with at least 1 hub and 1 node
  2. On the hub, create a new post, grab the post ID, and use the WP shell to distribute to the node:
$post_id = 0; // Change to your post ID.
$site_urls = [ 'http://node1.local' ]; // Change to match your setup urls.
Newspack_Network\Content_Distribution::set_post_distribution( $post_id, $site_urls );
Newspack_Network\Content_Distribution::distribute_post( $post_id );
  1. Confirm the post is added to the node
  2. Make a change to the post and hit save
  3. Confirm the changes are applied to the post in the node
  4. Back to the hub shell, manually change some content along with post_modified and distribute the post:
$post_id = 0; // Change to your post ID.
$past = '2021-01-01 00:00:00';
wp_update_post( [ 'ID' => $post_id, 'title' => 'Old Title', 'post_modified' => $past, 'post_modified_gmt' => get_gmt_from_date( $past ) ] );
Newspack_Network\Content_Distribution::distribute_post( $post_id );
  1. Confirm the post does not get updated in the node

@miguelpeixe miguelpeixe self-assigned this Dec 3, 2024
@miguelpeixe miguelpeixe marked this pull request as ready for review December 3, 2024 14:17
@miguelpeixe miguelpeixe requested a review from a team as a code owner December 3, 2024 14:17
@miguelpeixe miguelpeixe requested a review from naxoc December 6, 2024 13:18
Base automatically changed from feat/content-distribution-class to epic/content-distribution December 9, 2024 11:52
Copy link
Member

@naxoc naxoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I tested with this:

# On the hub:
wp post create --post_title="Version 1" --post_content="Oh, wow. Version 1 rocks" --post_status=publish
# Grab that post id ^^
wp eval "Newspack_Network\Content_Distribution::set_post_distribution( 148, [ 'https://node1.test' ] );"
wp eval "Newspack_Network\Content_Distribution::distribute_post( 148 );"
# ... wait for it to show up on node1

# On the hub:
wp post update 148 --post_title="Version 2." --post_content="Oh wow. Version 2 rocks"
# Now wait for the change to show up on node1

# On the hub:
wp post update 148 --post_title="Version 3." --post_content="Oh wow. Version 3 rocks" --post_modified='2021-01-01 00:00:00'  --post_modified_gmt='2020-12-31 23:00:00'
# Check that it doesn't show up.

@miguelpeixe
Copy link
Member Author

Nice! Thank you for the review, @naxoc!

@miguelpeixe miguelpeixe merged commit cd4339d into epic/content-distribution Dec 9, 2024
@miguelpeixe miguelpeixe deleted the feat/content-distribution-modified-date branch December 9, 2024 13:31
naxoc added a commit that referenced this pull request Dec 12, 2024
* feat: content distribution class

* feat: insert linked post

* refactor: use site url instead of ID

* chore: lint

* feat: return errors on post insert

* chore: lint

* feat: support webhooks request priority

* feat: unlink functionality and unit tests

* chore: lint

* feat: introduce 'linked_post_inserted' hook and listener

* chore: lint

* fix: listener hook name

* fix: typo

* chore: better docblocks

* chore: remove redundant code

* test: persist post hash

* test: remove unnecessary assertion

* refactor: distribute_post() to use handle_post_updated() method

* refactor: post hash is now network post id

* chore: update comment

* chore: Add CLI command for distribute

* feat: content distribution class (#155)

* feat: content distribution class

* feat: insert linked post

* refactor: use site url instead of ID

* chore: lint

* feat: return errors on post insert

* chore: lint

* feat: support webhooks request priority

* feat: unlink functionality and unit tests

* chore: lint

* feat: introduce 'linked_post_inserted' hook and listener

* chore: lint

* fix: listener hook name

* fix: typo

* chore: better docblocks

* chore: remove redundant code

* test: persist post hash

* test: remove unnecessary assertion

* refactor: distribute_post() to use handle_post_updated() method

* refactor: post hash is now network post id

* chore: update comment

* feat(content-distribution): prevent older content updating linked posts (#156)

* feat(content-distribution): handle post thumbnail (#157)

* Add network util class

* Almost done except for a few todos

* refactor: OOP for distributed and linked posts (#160)

* chore: Require php 8.1

* chore: Update to use util class

Also use new incoming/outgoing classes

* fix: Include correct classname

The old classname "Linked_Post" was still used instead of
"Incoming_Post" causing sync to fail.

## How to test
Probably eyeballing is enough

* chore: Add validation of outgoing post

Also move inclusion of the content distribution CLI class to better
respect the flag we are introdocuing

* chore: phpcs

* fix(content-distribution): debug post update and remove deprecated config (#165)

* fix: Don't return payload on all posts

* fix: post handling

* chore: move try-catch

* chore: Move check for networked urls

* Move more

* Move things again

* chore: Add a mock networked node

* chore: add one more node to the mock nodes

* Add a test

---------

Co-authored-by: Miguel Peixe <miguel.peixe@automattic.com>
matticbot pushed a commit that referenced this pull request Jan 10, 2025
# [2.4.0-alpha.1](v2.3.4...v2.4.0-alpha.1) (2025-01-10)

### Bug Fixes

* **content-distribution:** post insertion hook and additional meta for incoming post event ([#173](#173)) ([48df13c](48df13c))
* load text domain on init hook ([#171](#171)) ([01fb89c](01fb89c))

### Features

* content distribution - experimental ([#168](#168)) ([dc837d8](dc837d8))
* **content-distribution:** add CLI command for distribute post ([#159](#159)) ([7a43b86](7a43b86)), closes [#155](#155) [#156](#156) [#157](#157) [#160](#160) [#165](#165)
* **content-distribution:** canonical url ([#177](#177)) ([5ca60ce](5ca60ce))
* **content-distribution:** capability and admin page ([#176](#176)) ([5285285](5285285))
* **content-distribution:** control distribution meta and prevent multiple dispatches ([#170](#170)) ([e76a2dc](e76a2dc))
* **content-distribution:** editor plugin for distribution ([#167](#167)) ([e10aef4](e10aef4))
* **content-distribution:** handle status changes ([#166](#166)) ([4af5da1](4af5da1))
* **content-distribution:** log incoming post errors ([#182](#182)) ([74c9119](74c9119))
* **content-distribution:** posts column ([#178](#178)) ([8e07640](8e07640))
* **content-distribution:** reserved taxonomies ([#174](#174)) ([a2c54d2](a2c54d2))
* **content-distribution:** sync comment and ping statuses ([#179](#179)) ([90c5425](90c5425))
* **content-distribution:** sync post meta ([#163](#163)) ([353a3d8](353a3d8))
* **event-log:** collapse data ([#180](#180)) ([956219d](956219d))
* limit purchase of a network membership ([#169](#169)) ([deb2683](deb2683))
matticbot pushed a commit that referenced this pull request Jan 20, 2025
# [2.4.0](v2.3.4...v2.4.0) (2025-01-20)

### Bug Fixes

* **content-distribution:** post insertion hook and additional meta for incoming post event ([#173](#173)) ([48df13c](48df13c))
* load text domain on init hook ([#171](#171)) ([01fb89c](01fb89c))

### Features

* content distribution - experimental ([#168](#168)) ([dc837d8](dc837d8))
* **content-distribution:** add CLI command for distribute post ([#159](#159)) ([7a43b86](7a43b86)), closes [#155](#155) [#156](#156) [#157](#157) [#160](#160) [#165](#165)
* **content-distribution:** canonical url ([#177](#177)) ([5ca60ce](5ca60ce))
* **content-distribution:** capability and admin page ([#176](#176)) ([5285285](5285285))
* **content-distribution:** control distribution meta and prevent multiple dispatches ([#170](#170)) ([e76a2dc](e76a2dc))
* **content-distribution:** editor plugin for distribution ([#167](#167)) ([e10aef4](e10aef4))
* **content-distribution:** handle status changes ([#166](#166)) ([4af5da1](4af5da1))
* **content-distribution:** log incoming post errors ([#182](#182)) ([74c9119](74c9119))
* **content-distribution:** posts column ([#178](#178)) ([8e07640](8e07640))
* **content-distribution:** reserved taxonomies ([#174](#174)) ([a2c54d2](a2c54d2))
* **content-distribution:** sync comment and ping statuses ([#179](#179)) ([90c5425](90c5425))
* **content-distribution:** sync post meta ([#163](#163)) ([353a3d8](353a3d8))
* **event-log:** collapse data ([#180](#180)) ([956219d](956219d))
* limit purchase of a network membership ([#169](#169)) ([deb2683](deb2683))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants