is_registered( 'query' ) ) {
register_block_pattern_category( 'query', array( 'label' => __( 'Query', 'gutenberg' ) ) );
}
$patterns = array(
'query-standard-posts' => array(
'title' => __( 'Standard', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'categories' => array( 'query' ),
'content' => '
',
),
'query-medium-posts' => array(
'title' => __( 'Image at left', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'categories' => array( 'query' ),
'content' => '
',
),
'query-small-posts' => array(
'title' => __( 'Small image and title', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'categories' => array( 'query' ),
'content' => '
',
),
'query-grid-posts' => array(
'title' => __( 'Grid', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'categories' => array( 'query' ),
'content' => '
',
),
'query-large-title-posts' => array(
'title' => __( 'Large title', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'categories' => array( 'query' ),
'content' => '
',
),
'query-offset-posts' => array(
'title' => __( 'Offset', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'categories' => array( 'query' ),
'content' => '
',
),
// Initial block pattern to be used with block transformations with patterns.
'social-links-shared-background-color' => array(
'title' => __( 'Social links with a shared background color', 'gutenberg' ),
'categories' => array( 'buttons' ),
'blockTypes' => array( 'core/social-links' ),
'viewportWidth' => 500,
'content' => '
',
),
);
foreach ( $patterns as $name => $pattern ) {
$pattern_name = 'core/' . $name;
if ( ! WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_name ) ) {
register_block_pattern( $pattern_name, $pattern );
}
}
}
/**
* Deactivate the legacy patterns bundled with WordPress.
*/
function remove_core_patterns() {
$core_block_patterns = array(
'text-two-columns',
'two-buttons',
'two-images',
'text-two-columns-with-images',
'text-three-columns-buttons',
'large-header',
'large-header-button',
'three-buttons',
'heading-paragraph',
'quote',
'query-standard-posts',
'query-medium-posts',
'query-small-posts',
'query-grid-posts',
'query-large-title-posts',
'query-offset-posts',
'social-links-shared-background-color',
);
foreach ( $core_block_patterns as $core_block_pattern ) {
$name = 'core/' . $core_block_pattern;
if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $name ) ) {
unregister_block_pattern( $name );
}
}
}
/**
* Import patterns from wordpress.org/patterns.
*/
function load_remote_patterns() {
// This is the core function that provides the same feature.
if ( function_exists( '_load_remote_block_patterns' ) ) {
return;
}
$patterns = get_transient( 'gutenberg_remote_block_patterns' );
if ( ! $patterns ) {
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$core_keyword_id = 11; // 11 is the ID for "core".
$request->set_param( 'keyword', $core_keyword_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
set_transient( 'gutenberg_remote_block_patterns', $patterns, HOUR_IN_SECONDS );
}
foreach ( $patterns as $settings ) {
$pattern_name = 'core/' . sanitize_title( $settings['title'] );
register_block_pattern( $pattern_name, (array) $settings );
}
}
add_action(
'init',
function() {
if ( ! get_theme_support( 'core-block-patterns' ) || ! function_exists( 'unregister_block_pattern' ) ) {
return;
}
remove_core_patterns();
register_gutenberg_patterns();
}
);
add_action(
'current_screen',
function( $current_screen ) {
if ( ! get_theme_support( 'core-block-patterns' ) ) {
return;
}
$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) );
if ( $current_screen->is_block_editor || $is_site_editor ) {
load_remote_patterns();
}
}
);