WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.49k stars 4.18k forks source link

allowed_block_types_all - it's not possible to limit to specific embeds #37084

Closed steveangstrom closed 2 years ago

steveangstrom commented 2 years ago

Description

allowed_block_types_all is intended to allow devs to limit blocks to (for instance) Headings, paragraphs, and YouTube embeds. This is not the case.

For the YouTube insertion to work requires the root core/embed , and adding this brings with it all the the myriad other embeds. Conversely : omitting core/embed and only setting the specific block we want core/embed-youtube causes all youtube insertions to fail in the editor .

Consequently the options are :

  1. A working YouTube embed, but all other embeds show (using core/embed)
  2. A broken YouTube embed

Step-by-step reproduction instructions

WORKING EXAMPLE - ALLOWS YOUTUBE EMBED:
Add this code, with the core/embed for a working YouTube embed (but all other embeds erroneously present)

function filter_allowed_block_types_when_post_provided( $allowed_block_types, $editor_context ) {
    if ( ! empty( $editor_context->post ) ) {
            $allowed_block_types =  array(
                'core/paragraph',
                'core/heading',
                'core/video',
                'core/embed',
                'core-embed/youtube',
            );
            return $allowed_block_types;
        }
        return $allowed_block_types;
    }
add_filter( 'allowed_block_types_all', 'filter_allowed_block_types_when_post_provided', 10, 2 );

Now try the post editor, you should be limited to creating headings, paragraphs, and videos. Paste in a youtube URL, it will auto-embed. Correctly.

But .. now use the Block adder + to see the full list of what blocks are "allowed" and available, and every embed is there, despite being absent from the allowed array.

so now try the array without core/embed

FAIL EXAMPLE swap the array above with this one

$allowed_block_types =  array(
        'core/paragraph',
        'core/heading',
        'core/video',
        'core-embed/youtube',
    );

This ought to allow paragraphs, headings, videos and youtube videos

Test it: paste in a YouTube URL, RESULT: it does not auto embed.

Try using the video block to add the youtube url as an embed RESULT: failure

OUTCOME

There is consequently no way to explicitly allow specific embeds.

the absence of the core/embed disables the child core/embed-youtube while the presence of core/embed enables the hundreds of embeds which are explicitly disallowed.

Screenshots, screen recording, code snippet

No response

Environment info

WP 5.8.2 Gutenberg 12.0.1 PHP 7.4

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

skorasaurus commented 2 years ago

Hi,

Thanks for reporting; This has been already been reported in https://github.com/WordPress/gutenberg/issues/27913 . Feel free to add your reaction and follow its progress in the issue.

steveangstrom commented 2 years ago

I think it says everything that the filter "allowed_block_types" has been deprecated in the time that bug has been active. I didn't it because that filter was phased out in WP5.8 https://developer.wordpress.org/reference/hooks/allowed_block_types/

allowed_block_types_all is the new filter

yet nobody fixed this issue because its the SECOND CLASS CITIZEN PHP This is an important issue.

We cant add YouTube videos! You changed the filter in 5.8 and left the filter broken, who signed that off?

it's just dismissed as "not cool enough to fix" it's been ignored for a YEAR. Perhaps everyone is too focused on the latest shiny nonsense.

steveangstrom commented 2 years ago

For anyone following after me, searching for a patch. This is what I resorted to

First the PHP, this allows use of the contextual filter (which JS does not)


function filter_allowed_block_types_when_post_provided( $allowed_block_types, $editor_context ) {
        if ( ! empty( $editor_context->post ) ) {
            $allowed_block_types =  array(
                'core/paragraph',
                'core/image',
                'core/heading',
                'core/gallery',
                'core/list',
                'core/audio',
                'core/cover',
                'core/file',
                'core/video',
                'core/table',
                'core/html',
                'core/preformatted',
                'core/pullquote',
                'core/button',
                'core/columns',
                'core/separator',
                'core/spacer',
                'core/shortcode',
                'core/embed',
                'core-embed/youtube',
                'acf/team-member',
                'gravityforms/form',
            );
            return $allowed_block_types;
        }
        return $allowed_block_types;
    }

add_filter( 'allowed_block_types_all', 'filter_allowed_block_types_when_post_provided', 10, 2 );

Now the JS - to kill off those unwanted hundreds of ugly micro embeds, leaving only the ones we want, YouTube and Vimeo auto embedding

wp.domReady(function () {
  const allowedEmbedBlocks = [
    'vimeo',
    'youtube',
  ];
  wp.blocks.getBlockVariations('core/embed').forEach(function (blockVariation) {
    if (-1 === allowedEmbedBlocks.indexOf(blockVariation.name)) {
      wp.blocks.unregisterBlockVariation('core/embed', blockVariation.name);
    }
  });
});
skorasaurus commented 2 years ago

Hi @steveangstrom ;

I'm glad you found a resolution and thank you for sharing it. I don't influence the priorities for Gutenberg; in fact, I completely agree with you that fixing existing features should be a higher priority.