Skip to content

Commit

Permalink
Add block to pattern conversion and pattern taxonomy (#51144)
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz authored and aaronrobertshaw committed Jun 13, 2023
1 parent 7bb3e12 commit 5258eb2
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 13 deletions.
159 changes: 159 additions & 0 deletions lib/compat/wordpress-6.3/block-patterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
/**
* Overrides Core's wp-includes/block-patterns.php to add new wp_patterns taxonomy for WP 6.3.
*
* @package gutenberg
*/

/**
* Adds a new taxonomy for organizing patterns.
*
* Note: This should be removed when the minimum required WP version is >= 6.3.
*
* @see https://github.com/WordPress/gutenberg/pull/51144
*
* @return void
*/
function gutenberg_register_taxonomy_patterns() {
$labels = array(
'name' => _x( 'Patterns', 'taxonomy general name' ),
'singular_name' => _x( 'Pattern', 'taxonomy singular name' ),
'search_items' => __( 'Search Patterns' ),
'all_items' => __( 'All Pattern Categories' ),
'edit_item' => __( 'Edit Pattern Category' ),
'update_item' => __( 'Update Pattern Category' ),
'add_new_item' => __( 'Add New Pattern Category' ),
'new_item_name' => __( 'New Pattern Category Name' ),
'menu_name' => __( 'Pattern' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'wp_pattern' ),
);
register_taxonomy( 'wp_pattern', array( 'wp_block' ), $args );
}
add_action( 'init', 'gutenberg_register_taxonomy_patterns' );

/**
* Add categories to new wp_patterns taxonomy for WP 6.3.
*
* @package gutenberg
*/
function gutenberg_register_wp_patterns_taxonomy_categories() {
$categories = array(
array(
'slug' => 'banner',
'label' => _x( 'Banners', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Patterns used for adding banners', 'gutenberg' ),
),
array(
'slug' => 'buttons',
'label' => _x( 'Buttons', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Patterns that contain buttons and call to actions.', 'gutenberg' ),
),
array(
'slug' => 'columns',
'label' => _x( 'Columns', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Multi-column patterns with more complex layouts.', 'gutenberg' ),
),
array(
'slug' => 'text',
'label' => _x( 'Text', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Patterns containing mostly text.', 'gutenberg' ),
),
array(
'slug' => 'query',
'label' => _x( 'Posts', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Display your latest posts in lists, grids or other layouts.', 'gutenberg' ),
),
array(
'slug' => 'featured',
'label' => _x( 'Featured', 'Block pattern category', 'gutenberg' ),
'description' => __( 'A set of high quality curated patterns.', 'gutenberg' ),
),

// Register new core block pattern categories.
array(
'slug' => 'call-to-action',
'label' => _x( 'Call to Action', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Sections whose purpose is to trigger a specific action.', 'gutenberg' ),
),
array(
'slug' => 'team',
'label' => _x( 'Team', 'Block pattern category', 'gutenberg' ),
'description' => __( 'A variety of designs to display your team members.', 'gutenberg' ),
),
array(
'slug' => 'testimonials',
'label' => _x( 'Testimonials', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Share reviews and feedback about your brand/business.', 'gutenberg' ),
),
array(
'slug' => 'services',
'label' => _x( 'Services', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Briefly describe what your business does and how you can help.', 'gutenberg' ),
),
array(
'slug' => 'contact',
'label' => _x( 'Contact', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Display your contact information.', 'gutenberg' ),
),
array(
'slug' => 'about',
'label' => _x( 'About', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Introduce yourself.', 'gutenberg' ),
),
array(
'slug' => 'portfolio',
'label' => _x( 'Portfolio', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Showcase your latest work.', 'gutenberg' ),
),
array(
'slug' => 'gallery',
'label' => _x( 'Gallery', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Different layouts for displaying images.', 'gutenberg' ),
),
array(
'slug' => 'media',
'label' => _x( 'Media', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Different layouts containing video or audio.', 'gutenberg' ),
),
array(
'slug' => 'posts',
'label' => _x( 'Posts', 'Block pattern category', 'gutenberg' ),
'description' => __( 'Display your latest posts in lists, grids or other layouts.', 'gutenberg' ),
),
// Site building pattern categories.
array(
'slug' => 'footer',
'label' => _x( 'Footers', 'Block pattern category', 'gutenberg' ),
'description' => __( 'A variety of footer designs displaying information and site navigation.', 'gutenberg' ),
),
array(
'slug' => 'header',
'label' => _x( 'Headers', 'Block pattern category', 'gutenberg' ),
'description' => __( 'A variety of header designs displaying your site title and navigation.', 'gutenberg' ),
),
);

foreach ( $categories as $category ) {
if ( empty( term_exists( $category['slug'], 'wp_pattern' ) ) ) {
wp_insert_term(
$category['label'],
'wp_pattern',
array(
'slug' => $category['slug'],
'description' => $category['description'],
)
);
}
}
}
add_action( 'init', 'gutenberg_register_wp_patterns_taxonomy_categories', 20 );
77 changes: 77 additions & 0 deletions lib/compat/wordpress-6.3/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,80 @@ function gutenberg_add_selectors_property_to_block_type_settings( $settings, $me
return $settings;
}
add_filter( 'block_type_metadata_settings', 'gutenberg_add_selectors_property_to_block_type_settings', 10, 2 );

/**
* Adds custom fields support to the wp_block post type so a partial and unsynced option can be added.
*
* Note: This should be removed when the minimum required WP version is >= 6.3.
*
* @see https://github.com/WordPress/gutenberg/pull/51144
*
* @param array $args Register post type args.
* @param string $post_type The post type string.
*
* @return array Register post type args.
*/
function gutenberg_add_custom_fields_to_wp_block( $args, $post_type ) {

if ( 'wp_block' === $post_type ) {
array_push( $args['supports'], 'custom-fields' );
}

return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_add_custom_fields_to_wp_block', 10, 2 );

/**
* Adds wp_block_sync_status and wp_block_category_name meta fields to the wp_block post type so a partial and unsynced option can be added.
*
* Note: This should be removed when the minimum required WP version is >= 6.3.
*
* @see https://github.com/WordPress/gutenberg/pull/51144
*
* @return void
*/
function gutenberg_wp_block_register_post_meta() {
$post_type = 'wp_block';
register_post_meta(
$post_type,
'wp_block',
array(
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
'sanitize_callback' => 'gutenberg_wp_block_sanitize_post_meta',
'single' => true,
'type' => 'object',
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => array(
'sync_status' => array(
'type' => 'string',
),
'slug' => array(
'type' => 'string',
),
),
),
),
)
);
}
/**
* Sanitizes the array of wp_block post meta categories array.
*
* Note: This should be removed when the minimum required WP version is >= 6.3.
*
* @see https://github.com/WordPress/gutenberg/pull/51144
*
* @param array $meta_value Array of values to sanitize.
*
* @return array Sanitized array of values.
*/
function gutenberg_wp_block_sanitize_post_meta( $meta_value ) {
$meta_value['sync_status'] = sanitize_text_field( $meta_value['sync_status'] );
$meta_value['slug'] = sanitize_text_field( $meta_value['slug'] );
return $meta_value;
}
add_action( 'init', 'gutenberg_wp_block_register_post_meta' );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php';
require_once __DIR__ . '/compat/wordpress-6.3/link-template.php';
require_once __DIR__ . '/compat/wordpress-6.3/behaviors.php';
require_once __DIR__ . '/compat/wordpress-6.3/block-patterns.php';

// Experimental.
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '@wordpress/block-editor';
import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
import { ungroup } from '@wordpress/icons';
import { cloneBlock } from '@wordpress/blocks';

export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
const hasAlreadyRendered = useHasRecursion( ref );
Expand All @@ -54,11 +55,25 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
const { __experimentalConvertBlockToStatic: convertBlockToStatic } =
useDispatch( reusableBlocksStore );

const { replaceBlocks } = useDispatch( blockEditorStore );

const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
'wp_block',
{ id: ref }
);

if (
hasResolved &&
record?.meta?.wp_block?.sync_status === 'notSynced' &&
blocks?.length > 0
) {
replaceBlocks(
clientId,
blocks.map( ( block ) => cloneBlock( block ) )
);
}

const [ title, setTitle ] = useEntityProp(
'postType',
'wp_block',
Expand Down

0 comments on commit 5258eb2

Please sign in to comment.