distributor icon indicating copy to clipboard operation
distributor copied to clipboard

Is there a way to distribute on first publish rather than after?

Open zsection opened this issue 5 years ago • 11 comments

Hi, perhaps I'm missing this but is there a way to distribute the post upon publishing? At the moment, I only see the option to after publishing. Thanks

zsection avatar May 02 '19 12:05 zsection

@zsection you are correct, the Push functionality is present post-publish in the toolbar.

jeffpaul avatar May 02 '19 17:05 jeffpaul

@zsection you could likely add code that distributed content on publish using WordPress hooks. Currently the UI also involves choosing which site(s) to distribute to, and whether to distribute as draft, you would need to handle those choices in your post publish hook.

adamsilverstein avatar May 02 '19 17:05 adamsilverstein

@jeffpaul @adamsilverstein I've been attempting to work on a distribute-on-publish solution (External Connection) but have been having a hard time trying to figure out the correct order/hooks to use to get this done. Would either of you happen to have a sample or direction on where to start on this?

I believe it would be do_action( 'dt_push_post', $response, $post_body, $type_url, $post_id, $args, $this ); but some guidance would be much appreciated!

Kpudlo avatar May 30 '19 17:05 Kpudlo

You basically need to copy/paste/edit this code https://github.com/10up/distributor/blob/develop/includes/push-ui.php#L118-L134 and execute on the WP transition_post_status hook.

tlovett1 avatar Jun 06 '19 21:06 tlovett1

@tlovett1 thanks for the details!

@Kpudlo let us know if you run into further questions, we'd otherwise welcome a PR as you get the time!

jeffpaul avatar Jun 07 '19 03:06 jeffpaul

You basically need to copy/paste/edit this code https://github.com/10up/distributor/blob/develop/includes/push-ui.php#L118-L134 and execute on the WP transition_post_status hook.

Would this be suitable?

--

function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
	if ( ( 'publish' === $new_status && 'publish' !== $old_status )
		&& 'my-custom-post-type' === $post->post_type
	) {
			$connection_map  = (array) get_post_meta( $post->ID, 'dt_connection_map', true );
	$dom_connections = [];

	if ( empty( $connection_map['external'] ) ) {
		$connection_map['external'] = [];
	}

	if ( empty( $connection_map['internal'] ) ) {
		$connection_map['internal'] = [];
	}

	if ( ! empty( \Distributor\Connections::factory()->get_registered()['networkblog'] ) ) {
		$sites = \Distributor\InternalConnections\NetworkSiteConnection::get_available_authorized_sites( 'push' );

		foreach ( $sites as $site_array ) {
			if ( in_array( $post->post_type, $site_array['post_types'], true ) ) {
				$connection = new \Distributor\InternalConnections\NetworkSiteConnection( $site_array['site'] );
	}
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );

Sukalo0290 avatar Jul 13 '23 13:07 Sukalo0290

@peterwilsoncc perhaps this snippet would be good to add to the developer docs site?

jeffpaul avatar Aug 01 '23 12:08 jeffpaul

You basically need to copy/paste/edit this code https://github.com/10up/distributor/blob/develop/includes/push-ui.php#L118-L134 and execute on the WP transition_post_status hook.

Would this be suitable?

--

function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
	if ( ( 'publish' === $new_status && 'publish' !== $old_status )
		&& 'my-custom-post-type' === $post->post_type
	) {
			$connection_map  = (array) get_post_meta( $post->ID, 'dt_connection_map', true );
	$dom_connections = [];

	if ( empty( $connection_map['external'] ) ) {
		$connection_map['external'] = [];
	}

	if ( empty( $connection_map['internal'] ) ) {
		$connection_map['internal'] = [];
	}

	if ( ! empty( \Distributor\Connections::factory()->get_registered()['networkblog'] ) ) {
		$sites = \Distributor\InternalConnections\NetworkSiteConnection::get_available_authorized_sites( 'push' );

		foreach ( $sites as $site_array ) {
			if ( in_array( $post->post_type, $site_array['post_types'], true ) ) {
				$connection = new \Distributor\InternalConnections\NetworkSiteConnection( $site_array['site'] );
	}
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );

Dear Sir

Where did you added this code ?

(i removed lines 118 to 134 in push-ui.php and added above snippet , but its not working)

ankupu avatar Dec 19 '23 01:12 ankupu