Post actions from the child to the parent

Normally, Broadcast will only do to the child posts what is done to the parent: if you trash the parent post, the child posts are also trashed.

To enable functionality the other way, use this snippet.

add_action( 'trashed_post', 'bc_trash_post' );
add_action( 'untrashed_post', 'bc_untrash_post' );
add_action( 'delete_post', 'bc_delete_post' );

function bc_delete_post( $post_id )
	bc_trash_untrash_delete_post( 'wp_delete_post', $post_id );

function bc_trash_post( $post_id )
	bc_trash_untrash_delete_post( 'wp_trash_post', $post_id );

function bc_untrash_post( $post_id )
	bc_trash_untrash_delete_post( 'wp_untrash_post', $post_id );

	@brief		This is a generic function that does the same thing on the parent as is happening on the child: trash, untrash, delete.
	@details	Checks if the post is a broadcasted child and, if so, executes the same $command on the parent post.
	@see		src/traits/post_actions/trash_untrash_delete_post
	@since		2017-10-12 20:38:29
function bc_trash_untrash_delete_post( $command, $post_id )
	// Prevent loops.
	global $__bc_trash_untrash_delete_post;
	if ( isset( $__bc_trash_untrash_delete_post ) )

	$blog_id = get_current_blog_id();

	// Find out if this post is a child.
	$broadcast_data = ThreeWP_Broadcast()->get_post_broadcast_data( $blog_id, $post_id );
	$parent = $broadcast_data->get_linked_parent();
	// No parent = no child.
	if ( ! $parent )

	$__bc_trash_untrash_delete_post = true;

	// Go to the parent and do the same thing to it.
	switch_to_blog( $parent[ 'blog_id' ] );
	$command( $parent[ 'post_id' ] );

	unset( $__bc_trash_untrash_delete_post );