Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zsection opened this issue May 2, 2019 · 11 comments
Open

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

zsection opened this issue May 2, 2019 · 11 comments
Assignees
Labels
type:question Further information is requested.

Comments

@zsection
Copy link

zsection commented May 2, 2019

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

@jeffpaul
Copy link
Member

jeffpaul commented May 2, 2019

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

@jeffpaul jeffpaul added the type:question Further information is requested. label May 2, 2019
@adamsilverstein
Copy link

@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.

@Kpudlo
Copy link

Kpudlo commented May 30, 2019

@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!

@jeffpaul jeffpaul added this to the 2.0.0 milestone Jun 6, 2019
@tlovett1
Copy link
Member

tlovett1 commented Jun 6, 2019

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.

@jeffpaul
Copy link
Member

jeffpaul commented Jun 7, 2019

@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 jeffpaul added the needs:feedback This requires reporter feedback to better understand the request. label Jun 13, 2019
@jeffpaul jeffpaul modified the milestones: 3.0.0, Future Release Jul 24, 2019
@jeffpaul jeffpaul added type:enhancement New feature or request. and removed needs:feedback This requires reporter feedback to better understand the request. type:question Further information is requested. labels Jul 24, 2019
@Sukalo0290
Copy link

Sukalo0290 commented Jul 13, 2023

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 );

@jeffpaul jeffpaul added type:question Further information is requested. and removed type:enhancement New feature or request. labels Jul 13, 2023
@jeffpaul jeffpaul removed this from the Future Release milestone Jul 13, 2023
@jeffpaul
Copy link
Member

jeffpaul commented Aug 1, 2023

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

@ankupu
Copy link

ankupu commented Dec 19, 2023

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)

@Kirov-B
Copy link

Kirov-B commented Oct 7, 2024

function digicube_run_on_publish_only( $new_status, $old_status, $post ) {

    if ( ( 'publish' === $new_status && 'publish' !== $old_status ) 
        && in_array($post->post_type, ['post', 'stores']) 
    ) {

        $connection_map = (array) get_post_meta( $post->ID, 'dt_connection_map', true );
        
        if ( empty( $connection_map['external'] ) ) {
            $connection_map['external'] = [];
        }
        if ( empty( $connection_map['internal'] ) ) {
            $connection_map['internal'] = [];
        }
        if ( get_current_blog_id() === 1 ) {
            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 ) ) {
                        if ( $site_array['site']->blog_id !== 1 ) { 
                            $connection = new \Distributor\InternalConnections\NetworkSiteConnection( $site_array['site'] );
                            $new_post_id = $connection->push( $post->ID );
                            if ( is_array( $new_post_id ) && isset( $new_post_id['id'] ) ) {
                                $new_post_id = $new_post_id['id'];
                            }
                            if ( ! is_wp_error( $new_post_id ) && is_int( $new_post_id ) ) {
                                $connection_map['internal'][$site_array['site']->blog_id] = [
                                    'post_id' => $new_post_id, 
                                    'time'    => time(),    
                                ];

                            }
                        }
                    }
                }

                update_post_meta( $post->ID, 'dt_connection_map', $connection_map );
            }
        }
    }
}
add_action( 'transition_post_status', 'digicube_run_on_publish_only', 10, 3 ); 

I had the same issue, I did to manage to fix it like this.

If some one still needs it :)

@ankupu
Copy link

ankupu commented Oct 8, 2024

Where above code should be used, is it in functions.php ? under "edit theme"

@Kirov-B
Copy link

Kirov-B commented Oct 11, 2024

Where above code should be used, is it in functions.php ? under "edit theme"

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Further information is requested.
Projects
None yet
Development

No branches or pull requests

9 participants