Shortcodes: Replace local shortcodes in the post, featured image and custom fields

This snippet is used with the Local Shortcodes part of the Shortcodes add-on to replace the local shortcodes in various places:

  • The child post itself
    • Title
    • Excerpt
    • Slug (name)
  • The child post’s custom fields
  • The child post’s featured image
    • Alt-text
    • Caption
    • Description

The various bits of data are searched for all of the local shortcodes on the blog and replaced, with or without brackets depending on the code snippet. Some places like the post slug, don’t allow for square brackets.

For the featured image’s data can only be replaced when the image is first copied, or when the Update Attachments add-on is active to copy the fresh, shortcoded texts from the parent blog.

/**
	@brief		Parse parts of the post through local shortcodes.
	@since		2021-03-11 19:39:58
**/
add_action( 'threewp_broadcast_broadcasting_modify_post', function( $action )
{
	// Check that the Broadcast Shortcodes add-on is active.
	if ( ! function_exists( 'broadcast_shortcodes' ) )
		return;

	$bc = ThreeWP_Broadcast();				// Convenience
	$bcd = $action->broadcasting_data;		// Convenience
	$local_shortcodes = \threewp_broadcast\premium_pack\shortcodes\Local_Shortcodes::reload();
	$modified_post = $bcd->modified_post;	// Convenience

	foreach( [
		// The array consists of POST_KEY_TO_FILTER => USE_BRACKETS
		// These are the post keys we are going to replace.
		'post_title' => true,		// Shortcode brackets are fine in the title.
		'post_name' => false,		// Slugs definitely don't like brackets
		'post_excerpt' => true,		// Shortcode brackets are fine in the excerpt.
	] as $post_item => $brackets )
	{
		foreach( $local_shortcodes as $shortcode )
		{
			$key = $shortcode->get_name();

			if ( $brackets )
				$key = '[' . $key . ']';

			$value = $shortcode->get_content();
			$bc->debug( 'Replace Local Shortcodes: Replacing shortcode %s in %s', $key, $post_item );
			$modified_post->$post_item = str_replace( $key, $value, $modified_post->$post_item );
		}
	}
} );

/**
	@brief		Filter the featured image's text using local shortcodes.
	@since		2021-03-11 20:19:23
**/
add_action( 'threewp_broadcast_broadcasting_before_restore_current_blog', function( $action )
{
	// Check that the Broadcast Shortcodes add-on is active.
	if ( ! function_exists( 'broadcast_shortcodes' ) )
		return;

	$bc = ThreeWP_Broadcast();				// Convenience
	$bcd = $action->broadcasting_data;		// Convenience
	$local_shortcodes = \threewp_broadcast\premium_pack\shortcodes\Local_Shortcodes::reload();

	$thumbnail_id = get_post_meta( $bcd->new_post( 'ID' ), '_thumbnail_id', true );
	if( ! $thumbnail_id )
		return $bc->debug( 'Replace Local Shortcodes: No featured image for this post.' );

	// The description and caption are stored in the post itself.
	$post = get_post( $thumbnail_id );
	$new_data = [
		'ID' => $thumbnail_id,
	];
	foreach( [
		// The array consists of POST_KEY_TO_FILTER => USE_BRACKETS
		'post_content' => true,		// Description, brackets are OK
		'post_excerpt' => true,		// Caption, brackets are OK
	] as $post_item => $brackets )
	{
		foreach( $local_shortcodes as $shortcode )
		{
			$key = $shortcode->get_name();

			if ( $brackets )
				$key = '[' . $key . ']';

			$value = $shortcode->get_content();
			$new_data[ $post_item ] = str_replace( $key, $value, $post->$post_item );
		}
	}
	$bc->debug( 'Replace Local Shortcodes: Updating featured image %s', $new_data );
	wp_update_post( $new_data );

	// The alt text is stored in a custom field.
	$alt_text = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
	// Replace the shortcodes, with brackets?
	$brackets = true;
	foreach( $local_shortcodes as $shortcode )
	{
		$key = $shortcode->get_name();

		if ( $brackets )
			$key = '[' . $key . ']';

		$value = $shortcode->get_content();
		$alt_text = str_replace( $key, $value, $alt_text );
	}
	delete_post_meta( $thumbnail_id, '_wp_attachment_image_alt' );
	update_post_meta( $thumbnail_id, '_wp_attachment_image_alt', $alt_text );
} );

/**
	@brief		Filter the custom fields through local shortcodes.
	@since		2021-03-11 20:19:23
**/
add_action( 'threewp_broadcast_broadcasting_before_restore_current_blog', function( $action )
{
	// Check that the Broadcast Shortcodes add-on is active.
	if ( ! function_exists( 'broadcast_shortcodes' ) )
		return;

	$bc = ThreeWP_Broadcast();				// Convenience
	$bcd = $action->broadcasting_data;		// Convenience
	$local_shortcodes = \threewp_broadcast\premium_pack\shortcodes\Local_Shortcodes::reload();

	// Replace the shortcodes, with brackets?
	$brackets = true;

	$cf = $bcd->custom_fields()->child_fields()->load();
	foreach( $cf as $field_key => $field_values )
	{
		foreach( $field_values as $field_value )
		{
			foreach( $local_shortcodes as $shortcode )
			{
				$key = $shortcode->get_name();

				if ( $brackets )
					$key = '[' . $key . ']';

				$value = $shortcode->get_content();
				$field_value = str_replace( $key, $value, $field_value );
			}
			$cf->update_meta( $field_key, $field_value );
		}
	}
} );