• Resolved mclaurent

    (@mclaurent)


    Hi

    I am planning on running a WP Multisite with Gutenberg. I found your plugin which seems to do exactly what I want to do: Push content from the main site into child sites (one direction only), however I am not sure how it will handle image files in blocks. For instance if I add an image block on my main site and load an image from the media library into that block, when I push page to another blog the image file is not copied over. Is there an add-on for this, or is this a new feature?
    Many thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author edward_plainview

    (@edward_plainview)

    That should work. In the changelog for version 43.16 of Broadcast I wrote:

    Add support for Gutenberg standard image blocks: wp:image {“id”:1780}. The image ID will be modified on each child post if the image is attached to the post.

    It’s a normal wp:image block you’re using, right?

    Thread Starter mclaurent

    (@mclaurent)

    Wow – thank you for the quick reply. Yes at the moment we are only using wp:image but the client has mentioned that they might add bespoke blocks in the future.
    Many thanks

    Plugin Author edward_plainview

    (@edward_plainview)

    Well, when you’re not happy with wp:image then we can see about them other blocks. Will probably have to create an add-on for all them various weird blocks people want to use… 🙂

    Thread Starter mclaurent

    (@mclaurent)

    That would be fantastic. I just found the line that handles this in the plugin.. Would you accept a PR that would add a filter to overwrite which attributes in which blocks can be overwritten? Something like:
    (website\wp-content\plugins\threewp-broadcast\src\traits\attachments.php:360-383)

    
    // This is for Gutenberg: <!-- wp:image {"id":1780} --> and <!-- wp:image {"id":1780,"align":"center"} -->
    $content = preg_replace( '/(<!-- wp:image {"id":)' . $a->old->ID . '([},])/', '${1}' . $a->new->ID . '${2}', $content, -1, $count );
    $total_count = $count;
    // This is for custom Gutenberg blocks. Will replace old attachment IDs with the new attachment IDs
    // Note that the format of the array needs to match an exact structure.
    // Expects an array which has the block name in the key and the value containing an array of attributes
    // i.e.
    // array (
    // 	'wp:image' => array( 'id'),
    // 	'mc:custom_person' => array('headshot_image_id', 'favorite_image_id')
    // );
    $image_blocks = apply_filters( 'threewp_broadcast_media_blocks', array() );
    if ( ! empty( $image_blocks ) ) {
    	foreach ($image_blocks as $block_slug => $block_atts ) {
    		foreach ( $block_atts as $block_att ) {
    			// For the current attribute in the current media_block, replace the old attachment ID with the new attachment ID
    			$content = preg_replace( '/(<!-- ' . $block_slug .' {"' . $block_att . '":)' . $a->old->ID . '([},])/', '${1}' . $a->new->ID . '${2}', $content, -1, $count );
    			$total_count += $count;
    		}
    	}
    }
    
    if ( $count > 0 )
    	$this->debug( 'Modified Gutenberg images: %s times', $total_count );
    
    • This reply was modified 4 years, 10 months ago by mclaurent.
    • This reply was modified 4 years, 10 months ago by mclaurent.
    Plugin Author edward_plainview

    (@edward_plainview)

    That’s a pretty fancy solution. 🙂

    How about, instead of modifying the trait, we do the following:

    1. I add a snippet to my collection of snippets that contains pretty much the above
    and
    2. For those that don’t want to mess around with snippets, I create an add-on that adds a UI where one can set up various slugs and attribute.

    That should satisfy both those that like to custom code and those that don’t have the time for that.

    Thread Starter mclaurent

    (@mclaurent)

    @edward_plainview That sounds fantastic. I am wondering whether the regex needs extending, in case the attribute is not the first one to follow the block name. I don’t have the custom blocks yet to be able to test that fully, but it should give us a start to give the ability to add the functionality in.
    I also like the idea of a UI to manage this, as I know some clients do like to have extra control over this level of detail. We’d just need to make sure that our list of snippets doesn’t conflict with the user’s selection of various slugs and attributes.
    Thanks for your help

    • This reply was modified 4 years, 10 months ago by mclaurent.
    Plugin Author edward_plainview

    (@edward_plainview)

    I’m figuring that the snippets will not be used at the same time as the UI add- on. Different user groups.

    Plugin Author edward_plainview

    (@edward_plainview)

    I’m working on an add-on that can parse “things” in blocks (attachments, menus, posts, terms), similar to the various Shortcode XYZ add-ons I have for shortcodes.

    Do you perhaps have an example of a block that uses multiple IDs?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Gutenberg blocks with media’ is closed to new replies.