wpseek.com
A WordPress-centric search engine for devs and theme authors



_load_remote_featured_patterns › WordPress Function

Since5.9.0
Deprecatedn/a
_load_remote_featured_patterns ( $current_screen )
Parameters:
  • (WP_Screen) $current_screen The screen that the current request was triggered from.
    Required: Yes
Defined at:
Codex:

Register `Featured` (category) patterns from wordpress.org/patterns.



Source

function _load_remote_featured_patterns( $current_screen ) {
	if ( ! $current_screen->is_block_editor ) {
		return;
	}

	$supports_core_patterns = get_theme_support( 'core-block-patterns' );

	/** This filter is documented in wp-includes/block-patterns.php */
	$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );

	if ( ! $should_load_remote || ! $supports_core_patterns ) {
		return;
	}

	if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'featured' ) ) {
		register_block_pattern_category( 'featured', array( 'label' => __( 'Featured' ) ) );
	}

	$request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
	$featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
	$request->set_param( 'category', $featured_cat_id );
	$response = rest_do_request( $request );
	if ( $response->is_error() ) {
		return;
	}
	$patterns = $response->get_data();

	foreach ( $patterns as $pattern ) {
		$pattern_name = sanitize_title( $pattern['title'] );
		$registry     = WP_Block_Patterns_Registry::get_instance();
		// Some patterns might be already registerd as `core patterns with the `core` prefix.
		$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
		if ( ! $is_registered ) {
			register_block_pattern( $pattern_name, (array) $pattern );
		}
	}
}