Block Editor: Add the Block Patterns API.

Backport WP_Block_Pattern_Categories_Registry, WP_Block_Patterns_Registry
and block patterns registration functions from the Gutenberg repository.

Props ocean90, ellatrix
Fixes #50445.

Built from https://develop.svn.wordpress.org/trunk@48156


git-svn-id: http://core.svn.wordpress.org/trunk@47925 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
youknowriad 2020-06-24 12:59:11 +00:00
parent a45e10c6dc
commit 6d927e494c
5 changed files with 332 additions and 21 deletions

View File

@ -283,34 +283,36 @@ if ( $user_id ) {
$body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post ); $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post );
$editor_settings = array( $editor_settings = array(
'alignWide' => $align_wide, 'alignWide' => $align_wide,
'availableTemplates' => $available_templates, 'availableTemplates' => $available_templates,
'allowedBlockTypes' => $allowed_block_types, 'allowedBlockTypes' => $allowed_block_types,
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ),
'disablePostFormats' => ! current_theme_supports( 'post-formats' ), 'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
/** This filter is documented in wp-admin/edit-form-advanced.php */ /** This filter is documented in wp-admin/edit-form-advanced.php */
'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
'bodyPlaceholder' => $body_placeholder, 'bodyPlaceholder' => $body_placeholder,
'isRTL' => is_rtl(), 'isRTL' => is_rtl(),
'autosaveInterval' => AUTOSAVE_INTERVAL, 'autosaveInterval' => AUTOSAVE_INTERVAL,
'maxUploadFileSize' => $max_upload_size, 'maxUploadFileSize' => $max_upload_size,
'allowedMimeTypes' => get_allowed_mime_types(), 'allowedMimeTypes' => get_allowed_mime_types(),
'styles' => $styles, 'styles' => $styles,
'imageSizes' => $available_image_sizes, 'imageSizes' => $available_image_sizes,
'imageDimensions' => $image_dimensions, 'imageDimensions' => $image_dimensions,
'richEditingEnabled' => user_can_richedit(), 'richEditingEnabled' => user_can_richedit(),
'postLock' => $lock_details, 'postLock' => $lock_details,
'postLockUtils' => array( 'postLockUtils' => array(
'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ),
'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
), ),
'__experimentalBlockPatterns' => WP_Block_Patterns_Registry::get_instance()->get_all_registered(),
'__experimentalBlockPatternCategories' => WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
// Whether or not to load the 'postcustom' meta box is stored as a user meta // Whether or not to load the 'postcustom' meta box is stored as a user meta
// field so that we're not always loading its assets. // field so that we're not always loading its assets.
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ), 'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
); );
$autosave = wp_get_post_autosave( $post_ID ); $autosave = wp_get_post_autosave( $post_ID );

View File

@ -0,0 +1,152 @@
<?php
/**
* Blocks API: WP_Block_Pattern_Categories_Registry class
*
* @package WordPress
* @subpackage Blocks
* @since 5.5.0
*/
/**
* Class used for interacting with block pattern categories.
*/
final class WP_Block_Pattern_Categories_Registry {
/**
* Registered block pattern categories array.
*
* @var array
*/
private $registered_categories = array();
/**
* Container for the main instance of the class.
*
* @var WP_Block_Pattern_Categories_Registry|null
*/
private static $instance = null;
/**
* Registers a pattern category.
*
* @since 5.5.0
*
* @param string $category_name Pattern category name.
* @param array $category_properties Array containing the properties of the category: label.
* @return bool True if the pattern was registered with success and false otherwise.
*/
public function register( $category_name, $category_properties ) {
if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
_doing_it_wrong( __METHOD__, __( 'Block pattern category name must be a string.' ), '5.5.0' );
return false;
}
$this->registered_categories[ $category_name ] = array_merge(
array( 'name' => $category_name ),
$category_properties
);
return true;
}
/**
* Unregisters a pattern category.
*
* @since 5.5.0
*
* @param string $category_name Pattern name including namespace.
* @return bool True if the pattern was unregistered with success and false otherwise.
*/
public function unregister( $category_name ) {
if ( ! $this->is_registered( $category_name ) ) {
/* translators: 1: Block pattern name. */
$message = sprintf( __( 'Block pattern category "%1$s" not found.' ), $category_name );
_doing_it_wrong( __METHOD__, $message, '5.5.0' );
return false;
}
unset( $this->registered_categories[ $category_name ] );
return true;
}
/**
* Retrieves an array containing the properties of a registered pattern category.
*
* @since 5.5.0
*
* @param string $category_name Pattern category name.
* @return array Registered pattern properties.
*/
public function get_registered( $category_name ) {
if ( ! $this->is_registered( $category_name ) ) {
return null;
}
return $this->registered_categories[ $category_name ];
}
/**
* Retrieves all registered pattern categories.
*
* @since 5.5.0
*
* @return array Array of arrays containing the registered pattern categories properties.
*/
public function get_all_registered() {
return array_values( $this->registered_categories );
}
/**
* Checks if a pattern category is registered.
*
* @since 5.5.0
*
* @param string $category_name Pattern category name.
* @return bool True if the pattern category is registered, false otherwise.
*/
public function is_registered( $category_name ) {
return isset( $this->registered_categories[ $category_name ] );
}
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 5.5.0
*
* @return WP_Block_Pattern_Categories_Registry The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Registers a new pattern category.
*
* @since 5.5.0
*
* @param string $category_name Pattern category name.
* @param array $category_properties Array containing the properties of the category.
* @return bool True if the pattern category was registered with success and false otherwise.
*/
function register_block_pattern_category( $category_name, $category_properties ) {
return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
}
/**
* Unregisters a pattern category.
*
* @since 5.5.0
*
* @param string $category_name Pattern category name including namespace.
* @return bool True if the pattern category was unregistered with success and false otherwise.
*/
function unregister_block_pattern_category( $category_name ) {
return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
}

View File

@ -0,0 +1,155 @@
<?php
/**
* Blocks API: WP_Block_Patterns_Registry class
*
* @package WordPress
* @subpackage Blocks
* @since 5.5.0
*/
/**
* Class used for interacting with patterns.
*
* @since 5.5.0
*/
final class WP_Block_Patterns_Registry {
/**
* Registered patterns array.
*
* @var array
*/
private $registered_patterns = array();
/**
* Container for the main instance of the class.
*
* @var WP_Block_Patterns_Registry|null
*/
private static $instance = null;
/**
* Registers a pattern.
*
* @since 5.5.0
*
* @param string $pattern_name Pattern name including namespace.
* @param array $pattern_properties Array containing the properties of the pattern: label, content.
* @return bool True if the pattern was registered with success and false otherwise.
*/
public function register( $pattern_name, $pattern_properties ) {
if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) {
_doing_it_wrong( __METHOD__, s__( 'Pattern name must be a string.' ), '5.5.0' );
return false;
}
$this->registered_patterns[ $pattern_name ] = array_merge(
$pattern_properties,
array( 'name' => $pattern_name )
);
return true;
}
/**
* Unregisters a pattern.
*
* @since 5.5.0
*
* @param string $pattern_name Pattern name including namespace.
* @return bool True if the pattern was unregistered with success and false otherwise.
*/
public function unregister( $pattern_name ) {
if ( ! $this->is_registered( $pattern_name ) ) {
/* translators: 1: Pattern name. */
$message = sprintf( __( 'Pattern "%1$s" not found.' ), $pattern_name );
_doing_it_wrong( __METHOD__, $message, '5.5.0' );
return false;
}
unset( $this->registered_patterns[ $pattern_name ] );
return true;
}
/**
* Retrieves an array containing the properties of a registered pattern.
*
* @since 5.5.0
*
* @param string $pattern_name Pattern name including namespace.
* @return array Registered pattern properties.
*/
public function get_registered( $pattern_name ) {
if ( ! $this->is_registered( $pattern_name ) ) {
return null;
}
return $this->registered_patterns[ $pattern_name ];
}
/**
* Retrieves all registered patterns.
*
* @since 5.5.0
*
* @return array Array of arrays containing the registered patterns properties,
* and per style.
*/
public function get_all_registered() {
return array_values( $this->registered_patterns );
}
/**
* Checks if a pattern is registered.
*
* @since 5.5.0
*
* @param string $pattern_name Pattern name including namespace.
* @return bool True if the pattern is registered, false otherwise.
*/
public function is_registered( $pattern_name ) {
return isset( $this->registered_patterns[ $pattern_name ] );
}
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 5.5.0
*
* @return WP_Block_Patterns_Registry The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Registers a new pattern.
*
* @since 5.5.0
*
* @param string $pattern_name Pattern name including namespace.
* @param array $pattern_properties Array containing the properties of the pattern.
* @return bool True if the pattern was registered with success and false otherwise.
*/
function register_block_pattern( $pattern_name, $pattern_properties ) {
return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
}
/**
* Unregisters a pattern.
*
* @since 5.5.0
*
* @param string $pattern_name Pattern name including namespace.
* @return bool True if the pattern was unregistered with success and false otherwise.
*/
function unregister_block_pattern( $pattern_name ) {
return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name );
}

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-alpha-48155'; $wp_version = '5.5-alpha-48156';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -274,6 +274,8 @@ require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-posts.php';
require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-taxonomies.php'; require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-taxonomies.php';
require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-users.php'; require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-users.php';
require ABSPATH . WPINC . '/class-wp-block-type.php'; require ABSPATH . WPINC . '/class-wp-block-type.php';
require ABSPATH . WPINC . '/class-wp-block-pattern-categories-registry.php';
require ABSPATH . WPINC . '/class-wp-block-patterns-registry.php';
require ABSPATH . WPINC . '/class-wp-block-styles-registry.php'; require ABSPATH . WPINC . '/class-wp-block-styles-registry.php';
require ABSPATH . WPINC . '/class-wp-block-type-registry.php'; require ABSPATH . WPINC . '/class-wp-block-type-registry.php';
require ABSPATH . WPINC . '/class-wp-block-parser.php'; require ABSPATH . WPINC . '/class-wp-block-parser.php';