Skip to content

Auto Distribution feature.#1321

Merged
dkotter merged 18 commits intodevelopfrom
try/auto-distribute-feature
Sep 2, 2025
Merged

Auto Distribution feature.#1321
dkotter merged 18 commits intodevelopfrom
try/auto-distribute-feature

Conversation

@peterwilsoncc
Copy link
Copy Markdown
Collaborator

@peterwilsoncc peterwilsoncc commented Jun 16, 2025

Description of the Change

Allows developers to enable auto-distribution of posts via the use of a filter.

Todo:

  • Limit allowed post types
  • Limit allowed connections by type
  • Limit allowed connections by type + ID
  • Filter to modify default status to draft
  • Maybe hide push/pull distribution UI when enabled

Closes #456

How to test the Change

  1. Create a multisite network
  2. Add several sub-sites
  3. Create a single site
  4. Connect the single site as an external connection to the main site of the MS network. If using a local environment, ensure that the two can communicate if using a virtual machine
  5. Enable the feature by adding the following as an MU-plugin add_filter( 'dt_auto_distribution_enabled', '__return_true' );
  6. Publish a post on the main site of the MS network
  7. A few moments after pressing publish, ensure the post has been distributed to all connections. You can do this via the distributor drop-down in the admin bar if you wait
  8. Ensure the site is published on the destination sites.
  9. Add the following to your test plugin add_filter( 'dt_auto_distribution_default_status', function() { return 'draft'; } );
  10. Publish a post on the main site of the MS network
  11. A few moments after pressing publish, ensure the post has been distributed to all connections
  12. Ensure the post is a draft on each of the connections
  13. Remove the draft filter created above from your test plugin
  14. Add the following to the test plugin:
add_filter( 'dt_auto_distribute_post', function() {
 // Randomise the result to true or false for testing purposes.
 return (bool) rand( 0, 1 );
}, 10, 3 );
  1. Publish a post on the main site of the MS network
  2. A few moments after pressing publish, ensure the post has been distributed to approximately half of the connections. Note: This test will work better with more sites, as it reduces the chance of dumb luck distributing to all of the sites/none of the sites
  3. Remove the test plugin from your site to avoid confusion at a later date.

Changelog Entry

Added - Auto-distribution of posts via a filter

Credits

Props @peterwilsoncc

Checklist:

@github-actions github-actions bot added this to the 2.2.0 milestone Jun 16, 2025
@jeffpaul jeffpaul moved this to In Progress in Open Source Practice Jun 17, 2025
@peterwilsoncc peterwilsoncc force-pushed the try/auto-distribute-feature branch from 32adb91 to 1b9dda1 Compare August 4, 2025 04:36
@peterwilsoncc peterwilsoncc requested a review from dkotter August 5, 2025 05:02
@peterwilsoncc peterwilsoncc marked this pull request as ready for review August 5, 2025 05:02
@peterwilsoncc peterwilsoncc requested a review from a team as a code owner August 5, 2025 05:02
@github-actions github-actions bot added the needs:code-review This requires code review. label Aug 5, 2025
Copy link
Copy Markdown
Contributor

@faisal-alvi faisal-alvi left a comment

Choose a reason for hiding this comment

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

@peterwilsoncc Thanks so much for the work on this!

I tested the feature locally and noticed that two copies are created when a post is auto-distributed. I assume only one copy should be created. Could you please take a look?

image

I’ve also shared a couple of additional points below for your consideration.

Lastly, it might be helpful to add some test coverage for this feature to ensure long-term stability.

* @param integer $user_id User ID.
* @return array
*/
function get_connections( $post_id = 0, $user_id = 0 ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just wondering – do we already have some of these common functions in a helper class or utility file that we could leverage here? That might help keep things consistent and reusable.

Same question for the other functions added below, like get_external_connections and get_internal_connections.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We have similar code in Distributor\PushUI\get_connections() that is behind a nonce and capability check, so not appropriate in this instance as it needs to run on cron.

Do you want me to see if I can share the functionality between this and the admin-ajax endpoint and move this to the utils namespace?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, if you believe separating those common functions and moving them to the utils namespace would be beneficial for future enhancements (and it does not require too much efforts), then we should go ahead with that. Otherwise, I’m also fine keeping it as is for now; happy to go with your judgment on this.

@peterwilsoncc
Copy link
Copy Markdown
Collaborator Author

@faisal-alvi I'm not seeing the double posting. Is there any chance you have a site on a multisite instance set up as both an internal and external connection?

I'll try to figure out how to test this as the unit tests use mocks. I'll check the reliability of wp-cron in wp-env.

@faisal-alvi
Copy link
Copy Markdown
Contributor

@peterwilsoncc Regarding the double posting: I have a multisite setup (subdirectory structure) with one main site and one subsite. I had set up an external connection from the subsite by adding the main site URL; that was the only external connection configured.

However, it seems that in a multisite network, the sites are already internally connected by default, and we might not need to set up an external connection manually. Is that correct?

When I published a post from the subsite, two copies were auto-distributed to the main site. But when I removed the external connection and tested again, only one copy was distributed; so it looks like the feature works as expected, and the duplicate was likely due to the external + internal connection both being active.

If my assumption is correct that sites in a multisite are automatically connected internally, could you clarify how we can limit distribution to specific subsites, rather than distributing to all?

As for the tests, I’d lean towards adding an E2E test rather than a PHP unit test. If you think it’s feasible and worth the effort, then I think adding an E2E test would be a solid approach.

Copy link
Copy Markdown
Collaborator

@dkotter dkotter left a comment

Choose a reason for hiding this comment

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

Overall this looks good to me, noting Faisal has a number of comments he's already added. One thing that may be nice to add is a filter around the connections, making it easier to remove some of those from auto-distribution. Right now you can filter the posts that get auto-distributed and the post types that get auto-distributed but not seeing an easy way to filter the connections that get auto-distributed to

@peterwilsoncc
Copy link
Copy Markdown
Collaborator Author

However, it seems that in a multisite network, the sites are already internally connected by default, and we might not need to set up an external connection manually. Is that correct?

@faisal-alvi Yes, that's correct. Without auto-distribution it's fine to do so but with auto-distribution it will cause the double posting you're seeing. I'll have to figure out how to handle this in the tests.

Overall this looks good to me, noting Faisal has a number of comments he's already added. One thing that may be nice to add is a filter around the connections, making it easier to remove some of those from auto-distribution. Right now you can filter the posts that get auto-distributed and the post types that get auto-distributed but not seeing an easy way to filter the connections that get auto-distributed to

@dkotter I was thinking a developer might be able to use the same filter for the purpose:

add_filter( 'dt_auto_distribute_post', 'no_external', 10, 4 );

function no_external( $should_distribute, $post, $user_id, $connection_type ) {
	if ( $connection_type === 'external' ) {
		return false;
	}
	return $should_distribute;
}

Is that adequate or would you prefer another filter so we can bypass the relevant code in get_connections()?

@dkotter
Copy link
Copy Markdown
Collaborator

dkotter commented Aug 7, 2025

I was thinking a developer might be able to use the same filter for the purpose:

Yeah, I think that works fine. Probably worth adding this to our documentation snippets

@jeffpaul
Copy link
Copy Markdown
Member

@peterwilsoncc thoughts on looking to cover #1317 here as well?

@dkotter dkotter mentioned this pull request Aug 26, 2025
21 tasks
faisal-alvi
faisal-alvi previously approved these changes Aug 29, 2025
@github-project-automation github-project-automation bot moved this from In Progress to QA Testing in Open Source Practice Aug 29, 2025
@dkotter
Copy link
Copy Markdown
Collaborator

dkotter commented Aug 29, 2025

@peterwilsoncc Is there anything else you were wanting to change/add here or is this good for merge?

@peterwilsoncc
Copy link
Copy Markdown
Collaborator Author

Is there anything else you were wanting to change/add here or is this good for merge?

I pushed 2dbe340 as a one final thing: it modifies when the auto-distribution setup runs to wait until plugins_loaded, 20 to give a little more room for feature plugins to run.

Otherwise, I'm happy if you are.

@dkotter dkotter merged commit dcf8510 into develop Sep 2, 2025
18 checks passed
@dkotter dkotter deleted the try/auto-distribute-feature branch September 2, 2025 22:14
@github-project-automation github-project-automation bot moved this from QA Testing to Done in Open Source Practice Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:code-review This requires code review.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Add ability to auto-distribute to all connected sites (internal and external)

4 participants