Parse custom fields

This snippet allows for specific custom fields (postmeta) to be parsed by Broadcast and the various add-ons.

Normally, custom fields are broadcasted as-is. If you store shortcodes in custom fields, they will normally not be detected.

Using this snippet you can specify which custom fields to parse and the shortcodes in them will be automatically modified on each child blog.

/**
	@brief		Allow add-ons to preparse custom field content.
	@since		2020-02-05 10:19:51
**/
function bc_threewp_broadcast_broadcasting_started( $action )
{
	// An array of custom field names that should be modified. The array should be the same for preparsing and parsing.
	$custom_fields = [
		'test1',
	];

	$bcd = $action->broadcasting_data;		// Convenience
	foreach( $custom_fields as $custom_field )
	{
		ThreeWP_Broadcast()->debug( 'Preparsing custom field %s', $custom_field );
		$preparse_action = new \threewp_broadcast\actions\preparse_content();
		$preparse_action->broadcasting_data = $action->broadcasting_data;
		$preparse_action->content = $bcd->custom_fields()->get_single( $custom_field );
		$preparse_action->id = 'custom_field_' . $custom_field;
		$preparse_action->execute();
	}
}
add_action( 'threewp_broadcast_broadcasting_started', 'bc_threewp_broadcast_broadcasting_started' );

/**
	@brief		Allow add-ons to parse custom field content.
	@since		2020-02-05 10:19:51
**/
function bc_threewp_broadcast_broadcasting_before_restore_current_blog( $action )
{
	// An array of custom field names that should be modified. The array should be the same for preparsing and parsing.
	$custom_fields = [
		'test1',
	];

	$bcd = $action->broadcasting_data;		// Convenience
	foreach( $custom_fields as $custom_field )
	{
		ThreeWP_Broadcast()->debug( 'Parsing custom field %s', $custom_field );
		$parse_action = new \threewp_broadcast\actions\parse_content();
		$parse_action->broadcasting_data = $action->broadcasting_data;
		$parse_action->content = $bcd->custom_fields()->get_single( $custom_field );
		$parse_action->id = 'custom_field_' . $custom_field;
		$parse_action->execute();
		$new_value = $parse_action->content;
		$bcd->custom_fields()->child_fields()->update_meta( $custom_field, $new_value );
		ThreeWP_Broadcast()->debug( 'New value for custom field %s is %s', $custom_field, $new_value );
	}
}
add_action( 'threewp_broadcast_broadcasting_before_restore_current_blog', 'bc_threewp_broadcast_broadcasting_before_restore_current_blog' );