Make WordPress Core

Ticket #48039: 48039.diff

File 48039.diff, 9.5 KB (added by jorgefilipecosta, 6 years ago)
  • src/wp-includes/blocks.php

    diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
    index 6032159fb9..057648935f 100644
    a b function _restore_wpautop_hook( $content ) { 
    356356function block_version( $content ) {
    357357        return has_blocks( $content ) ? 1 : 0;
    358358}
     359
     360/**
     361 * Registers a new block style.
     362 *
     363 * @since 5.3.0
     364 *
     365 * @param string $block_name       Block type name including namespace.
     366 * @param array  $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added).
     367 *
     368 * @return boolean True if the block style was registered with success and false otherwise.
     369 */
     370function register_block_style( $block_name, $style_properties ) {
     371        return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
     372}
     373
     374/**
     375 * Unregisters a block style.
     376 *
     377 * @since 5.3.0
     378 *
     379 * @param string $block_name       Block type name including namespace.
     380 * @param array  $block_style_name Block style name.
     381 *
     382 * @return boolean True if the block style was unregistered with success and false otherwise.
     383 */
     384function unregister_block_style( $block_name, $block_style_name ) {
     385        return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
     386}
  • new file src/wp-includes/class-wp-block-styles-registry.php

    diff --git a/src/wp-includes/class-wp-block-styles-registry.php b/src/wp-includes/class-wp-block-styles-registry.php
    new file mode 100644
    index 0000000000..4f4b57ba50
    - +  
     1<?php
     2/**
     3 * Blocks API: WP_Block_Styles_Registry class
     4 *
     5 * @package Gutenberg
     6 * @since 5.3.0
     7 */
     8
     9/**
     10 * Class used for interacting with block styles.
     11 *
     12 * @since 5.3.0
     13 */
     14final class WP_Block_Styles_Registry {
     15        /**
     16         * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays.
     17         *
     18         * @since 5.3.0
     19         * @var array
     20         */
     21        private $registered_block_styles = array();
     22
     23        /**
     24         * Container for the main instance of the class.
     25         *
     26         * @since 5.3.0
     27         * @var WP_Block_Styles_Registry|null
     28         */
     29        private static $instance = null;
     30
     31        /**
     32         * Registers a block style.
     33         *
     34         * @since 5.3.0
     35         *
     36         * @param string $block_name       Block type name including namespace.
     37         * @param array  $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added).
     38         *
     39         * @return boolean True if the block style was registered with success and false otherwise.
     40         */
     41        public function register( $block_name, $style_properties ) {
     42
     43                if ( ! isset( $block_name ) || ! is_string( $block_name ) ) {
     44                        $message = __( 'Block name name must be a string.', 'gutenberg' );
     45                        _doing_it_wrong( __METHOD__, $message, '6.2.0' );
     46                        return false;
     47                }
     48
     49                if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) {
     50                        $message = __( 'Block style name must be a string.', 'gutenberg' );
     51                        _doing_it_wrong( __METHOD__, $message, '6.2.0' );
     52                        return false;
     53                }
     54
     55                $block_style_name = $style_properties['name'];
     56
     57                if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) {
     58                        $this->registered_block_styles[ $block_name ] = array();
     59                }
     60                $this->registered_block_styles[ $block_name ][ $block_style_name ] = $style_properties;
     61
     62                return true;
     63        }
     64
     65        /**
     66         * Unregisters a block style.
     67         *
     68         * @param string $block_name       Block type name including namespace.
     69         * @param array  $block_style_name Block style name.
     70         *
     71         * @return boolean True if the block style was unregistered with success and false otherwise.
     72         */
     73        public function unregister( $block_name, $block_style_name ) {
     74                if ( ! $this->is_registered( $block_name, $block_style_name ) ) {
     75                        /* translators: 1: block name, 2: block style name */
     76                        $message = sprintf( __( 'Block "%1$s" does not contain a style named "%2$s.".', 'gutenberg' ), $block_name, $block_style_name );
     77                        _doing_it_wrong( __METHOD__, $message, '6.2.0' );
     78                        return false;
     79                }
     80
     81                unset( $this->registered_block_styles[ $block_name ][ $block_style_name ] );
     82
     83                return true;
     84        }
     85
     86        /**
     87         * Retrieves an array containing the properties of a registered block style.
     88         *
     89         * @since 5.3.0
     90         *
     91         * @param string $block_name       Block type name including namespace.
     92         * @param array  $block_style_name Block style name.
     93         *
     94         * @return array Registered block style properties.
     95         */
     96        public function get_registered( $block_name, $block_style_name ) {
     97                if ( ! $this->is_registered( $block_name, $block_style_name ) ) {
     98                        return null;
     99                }
     100
     101                return $this->registered_block_styles[ $block_name ][ $block_style_name ];
     102        }
     103
     104        /**
     105         * Retrieves all registered block styles.
     106         *
     107         * @since 5.3.0
     108         *
     109         * @return array Array of arrays containing the registered block styles properties grouped per block, and per style.
     110         */
     111        public function get_all_registered() {
     112                return $this->registered_block_styles;
     113        }
     114
     115        /**
     116         * Retrieves registered block styles for a specific block.
     117         *
     118         * @since 5.3.0
     119         *
     120         * @param string $block_name Block type name including namespace.
     121         *
     122         * @return array Array whose keys are block style names and whose value are block style properties.
     123         */
     124        public function get_registered_styles_for_block( $block_name ) {
     125                if ( isset( $this->registered_block_styles[ $block_name ] ) ) {
     126                        return $this->registered_block_styles[ $block_name ];
     127                }
     128                return array();
     129        }
     130
     131        /**
     132         * Checks if a block style is registered.
     133         *
     134         * @since 5.3.0
     135         *
     136         * @param string $block_name       Block type name including namespace.
     137         * @param array  $block_style_name Block style name.
     138         *
     139         * @return bool True if the block style is registered, false otherwise.
     140         */
     141        public function is_registered( $block_name, $block_style_name ) {
     142                return isset( $this->registered_block_styles[ $block_name ][ $block_style_name ] );
     143        }
     144
     145        /**
     146         * Utility method to retrieve the main instance of the class.
     147         *
     148         * The instance will be created if it does not exist yet.
     149         *
     150         * @since 5.3.0
     151         *
     152         * @return WP_Block_Styles_Registry The main instance.
     153         */
     154        public static function get_instance() {
     155                if ( null === self::$instance ) {
     156                        self::$instance = new self();
     157                }
     158
     159                return self::$instance;
     160        }
     161}
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index fc0f4408f7..423db4d540 100644
    a b  
    501501add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
    502502add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
    503503add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 );
     504add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
     505add_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' );
    504506
    505507add_action( 'wp_default_styles', 'wp_default_styles' );
    506508add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 );
  • src/wp-includes/script-loader.php

    diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php
    index 4ad643008c..5366bfbcd0 100644
    a b function wp_enqueue_registered_block_scripts_and_styles() { 
    27802780                }
    27812781        }
    27822782}
     2783
     2784/**
     2785 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
     2786 *
     2787 * @since 5.3.0
     2788 */
     2789function enqueue_block_styles_assets() {
     2790        $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
     2791
     2792        foreach ( $block_styles as $styles ) {
     2793                foreach ( $styles as $style_properties ) {
     2794                        if ( isset( $style_properties['style_handle'] ) ) {
     2795                                wp_enqueue_style( $style_properties['style_handle'] );
     2796                        }
     2797                        if ( isset( $style_properties['inline_style'] ) ) {
     2798                                wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
     2799                        }
     2800                }
     2801        }
     2802}
     2803
     2804/**
     2805 * Function responsible for enqueuing the assets required for block styles functionality on the editor.
     2806 *
     2807 * @since 5.3.0
     2808 */
     2809function enqueue_editor_block_styles_assets() {
     2810        $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
     2811
     2812        $register_script_lines = array( '( function() {' );
     2813        foreach ( $block_styles as $block_name => $styles ) {
     2814                foreach ( $styles as $style_properties ) {
     2815                        $register_script_lines[] = sprintf(
     2816                                '       wp.blocks.registerBlockStyle( \'%s\', %s );',
     2817                                $block_name,
     2818                                wp_json_encode(
     2819                                        array(
     2820                                                'name'  => $style_properties['name'],
     2821                                                'label' => $style_properties['label'],
     2822                                        )
     2823                                )
     2824                        );
     2825                }
     2826        }
     2827        $register_script_lines[] = '} )();';
     2828        $inline_script           = implode( "\n", $register_script_lines );
     2829
     2830        wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
     2831        wp_add_inline_script( 'wp-block-styles', $inline_script );
     2832        wp_enqueue_script( 'wp-block-styles' );
     2833}
  • src/wp-settings.php

    diff --git a/src/wp-settings.php b/src/wp-settings.php
    index 681053a0d4..5822ebde5b 100644
    a b  
    256256require( ABSPATH . WPINC . '/rest-api/search/class-wp-rest-search-handler.php' );
    257257require( ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-search-handler.php' );
    258258require( ABSPATH . WPINC . '/class-wp-block-type.php' );
     259require( ABSPATH . WPINC . '/class-wp-block-styles-registry.php' );
    259260require( ABSPATH . WPINC . '/class-wp-block-type-registry.php' );
    260261require( ABSPATH . WPINC . '/class-wp-block-parser.php' );
    261262require( ABSPATH . WPINC . '/blocks.php' );