diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index 0d690ef4ae..1451710b78 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -283,34 +283,36 @@ if ( $user_id ) { $body_placeholder = apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block' ), $post ); $editor_settings = array( - 'alignWide' => $align_wide, - 'availableTemplates' => $available_templates, - 'allowedBlockTypes' => $allowed_block_types, - 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), - 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), - 'disablePostFormats' => ! current_theme_supports( 'post-formats' ), + 'alignWide' => $align_wide, + 'availableTemplates' => $available_templates, + 'allowedBlockTypes' => $allowed_block_types, + 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'disablePostFormats' => ! current_theme_supports( 'post-formats' ), /** This filter is documented in wp-admin/edit-form-advanced.php */ - 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), - 'bodyPlaceholder' => $body_placeholder, - 'isRTL' => is_rtl(), - 'autosaveInterval' => AUTOSAVE_INTERVAL, - 'maxUploadFileSize' => $max_upload_size, - 'allowedMimeTypes' => get_allowed_mime_types(), - 'styles' => $styles, - 'imageSizes' => $available_image_sizes, - 'imageDimensions' => $image_dimensions, - 'richEditingEnabled' => user_can_richedit(), - 'postLock' => $lock_details, - 'postLockUtils' => array( + 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title' ), $post ), + 'bodyPlaceholder' => $body_placeholder, + 'isRTL' => is_rtl(), + 'autosaveInterval' => AUTOSAVE_INTERVAL, + 'maxUploadFileSize' => $max_upload_size, + 'allowedMimeTypes' => get_allowed_mime_types(), + 'styles' => $styles, + 'imageSizes' => $available_image_sizes, + 'imageDimensions' => $image_dimensions, + 'richEditingEnabled' => user_can_richedit(), + 'postLock' => $lock_details, + 'postLockUtils' => array( 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), '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 // 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 ); diff --git a/src/wp-includes/class-wp-block-pattern-categories-registry.php b/src/wp-includes/class-wp-block-pattern-categories-registry.php new file mode 100644 index 0000000000..a346730b6b --- /dev/null +++ b/src/wp-includes/class-wp-block-pattern-categories-registry.php @@ -0,0 +1,152 @@ +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 ); +} diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php new file mode 100644 index 0000000000..96df2da2f1 --- /dev/null +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -0,0 +1,155 @@ +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 ); +} diff --git a/src/wp-settings.php b/src/wp-settings.php index a78be1c091..f351d84cfb 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -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-users.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-type-registry.php'; require ABSPATH . WPINC . '/class-wp-block-parser.php';