Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Block_Library::should_register( string $id, array $block )

Determines whether or not the block should be registered.


Description Description

There’s no "good" way to register a block only for a specific post type(s) or context (such as the post editor only and not the widgets editor).

This method uses the $pagenow global and query string variables to interpret the current screen context and register the block only in the intended context.

This creates issues if the block list is retrieve via the REST API. But we can’t avoid this given the current APIs. Especially since the WP core throw’s a notice on the widgets screen if a block is registered with a script that relies on wp-editor as a dependency.

See related issue links below.


Top ↑

Parameters Parameters

$id

(string) (Required) The block's id (without the llms/ prefix).

$block

(array) (Required) Array of block data.


Top ↑

Return Return

(boolean)


Top ↑

Source Source

File: includes/class-llms-block-library.php

	private function should_register( $id, $block ) {

		// Prevent errors if the block is already registered.
		$registry = WP_Block_Type_Registry::get_instance();
		if ( $registry->is_registered( 'llms/' . $id ) ) {
			return false;
		}

		// Ensure the block is only registered in the correct context.
		global $pagenow;
		$post_type = null;
		if ( 'post.php' === $pagenow ) {
			$id        = llms_filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
			$post_type = $id ? get_post_type( $id ) : $post_type;
		} elseif ( 'post-new.php' === $pagenow ) {
			$post_type = llms_filter_input( INPUT_GET, 'post_type' );
			$post_type = $post_type ? $post_type : 'post'; // If `$_GET` is not set it's because it's a basic post.
		}

		if ( ! is_null( $post_type ) && in_array( $post_type, $block['post_types'], true ) ) {
			return true;
		}

		return false;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.4.0 Stop using deprecated FILTER_SANITIZE_STRING.
6.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.