Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to enable blocks for oik-plugins and blocks CPTs #21

Open
bobbingwide opened this issue Oct 19, 2019 · 1 comment
Open

Add logic to enable blocks for oik-plugins and blocks CPTs #21

bobbingwide opened this issue Oct 19, 2019 · 1 comment

Comments

@bobbingwide
Copy link
Owner

bobbingwide commented Oct 19, 2019

Some plugins create blocks in categories that they create for themselves.
They use code like this. See https://developer.wordpress.org/block-editor/developers/filters/block-filters/#managing-block-categories

As you can see, this is only good for posts.
How can we change the code so that the custom categories are registered regardless of the post type? Is this time for a super hack or just some hardcoded category registration?

function my_plugin_block_categories( $categories, $post ) {
    if ( $post->post_type !== 'post' ) {
        return $categories;
    }
    return array_merge(
        $categories,
        array(
            array(
                'slug' => 'my-category',
                'title' => __( 'My category', 'my-plugin' ),
                'icon'  => 'wordpress',
            ),
        )
    );
}
add_filter( 'block_categories', 'my_plugin_block_categories', 10, 2 );
@bobbingwide
Copy link
Owner Author

bobbingwide commented Nov 5, 2019

Other plugins only enqueue their scripts for selected post types. e.g. in slides/index.php the filter function for admin_enqueue_scripts begins

add_action( 'admin_enqueue_scripts', function() {
	if ( get_post_type() !== 'presentation' ) {
		return;
	}

Attempting to override this, by commenting out the return, led to an error while trying to edit any oik-plugins CPT.

TypeError: Cannot read property 'presentation-background-color' of undefined
    at render (https://blocks.wp.a2z/wp-content/plugins/slide/index.js?ver=1572947862:1114:31)
    at renderWithHooks (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:15246:18)
    at mountIndeterminateComponent (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:17480:13)
    at beginWork$1 (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:18624:16)
    at HTMLUnknownElement.callCallback (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:341:14)
    at Object.invokeGuardedCallbackDev (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:391:16)
    at invokeGuardedCallback (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:448:31)
    at beginWork$$1 (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:23355:7)
    at performUnitOfWork (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:22346:12)
    at workLoopSync (https://blocks.wp.a2z/wp-content/plugins/gutenberg/vendor/react-dom.8b3dda97.js:22323:22)

For other posts types the results were also rather interesting.
I automatically generated a block CPT for the Slide block.
When I edited it every block became a slide.

The Chrome console log is interesting reading, showing:

Block validation failed for slide/slide
messages for each block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant