Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/25/2023 10:17:33 PM (3 years ago)
Author:
imath
Message:

Update all BP Blocks & Modern JS PHP registration code

  • Use the metadata argument of the bp_register_block() function to load all BP Blocks JSON files.
  • Get BP Blocks dependencies using the @wordpress/scripts index.asset.php generated files.
  • Although the BP Blocks category has been replaced by the BP Blocks collection, leave the no more used bp_block_category() function in place in case some third party plugins are using it for their custom blocks.
  • PHPUnit tests: make sure to unregister all BP Blocks when using the BP_UnitTestCase->go_to() method.

See #8842
Fixes #8457
Closes https://github.com/buddypress/buddypress/pull/92

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-blocks.php

    r13441 r13464  
    3636 * @since 9.0.0 Adds a dependency to `wp-server-side-render` if WP >= 5.3.
    3737 *              Uses a dependency to `wp-editor` otherwise.
     38 * @since 12.0.0 Uses the `@wordpress/scripts` `index.asset.php` generated file to get dependencies.
    3839 */
    3940function bp_register_block_components() {
     41        $asset      = array(
     42                'dependencies' => array(),
     43                'version'      => bp_get_version(),
     44        );
     45        $asset_file = trailingslashit( dirname( __FILE__ ) ) . 'blocks/block-components/index.asset.php';
     46
     47        if ( file_exists( $asset_file ) ) {
     48                $asset = require $asset_file;
     49        }
     50
    4051        wp_register_script(
    4152                'bp-block-components',
    42                 plugins_url( 'js/block-components.js', __FILE__ ),
    43                 array(
    44                         'wp-element',
    45                         'wp-components',
    46                         'wp-i18n',
    47                         'wp-api-fetch',
    48                         'wp-url',
    49                 ),
    50                 bp_get_version(),
     53                plugins_url( 'blocks/block-components/index.js', __FILE__ ),
     54                $asset['dependencies'],
     55                $asset['version'],
    5156                false
    5257        );
    53 
    54         // Adds BP Block Components to the `bp` global.
    55         wp_add_inline_script(
    56                 'bp-block-components',
    57                 'window.bp = window.bp || {};
    58                 bp.blockComponents = bpBlock.blockComponents;
    59                 delete bpBlock;',
    60                 'after'
    61         );
    6258}
    6359add_action( 'bp_blocks_init', 'bp_register_block_components', 1 );
     
    6763 *
    6864 * @since 9.0.0
     65 * @since 12.0.0 Adds the BuddyPress Blocks collection & uses the `@wordpress/scripts`
     66 *               `index.asset.php` generated file to get dependencies.
    6967 */
    7068function bp_register_block_assets() {
     69        $default_asset   = array(
     70                'dependencies' => array(),
     71                'version'      => bp_get_version(),
     72        );
     73        $asset_data_file = trailingslashit( dirname( __FILE__ ) ) . 'blocks/block-data/index.asset.php';
     74
     75        if ( file_exists( $asset_data_file ) ) {
     76                $asset_data = require $asset_data_file;
     77        } else {
     78                $asset_data = $default_asset;
     79        }
     80
    7181        wp_register_script(
    7282                'bp-block-data',
    73                 plugins_url( 'js/block-data.js', __FILE__ ),
    74                 array(
    75                         'wp-data',
    76                         'wp-api-fetch',
    77                         'lodash',
    78                 ),
    79                 bp_get_version(),
     83                plugins_url( 'blocks/block-data/index.js', __FILE__ ),
     84                $asset_data['dependencies'],
     85                $asset_data['version'],
    8086                false
    8187        );
    8288
    83         // Adds BP Block Assets to the `bp` global.
    84         wp_add_inline_script(
    85                 'bp-block-data',
    86                 sprintf(
    87                         'window.bp = window.bp || {};
    88                         bp.blockData = bpBlock.blockData;
    89                         bp.blockData.embedScriptURL = \'%s\';
    90                         delete bpBlock;',
    91                         esc_url_raw( includes_url( 'js/wp-embed.min.js' ) )
    92                 ),
    93                 'after'
     89        $asset_collection_file = trailingslashit( dirname( __FILE__ ) ) . 'blocks/block-collection/index.asset.php';
     90
     91        if ( file_exists( $asset_collection_file ) ) {
     92                $asset_collection = require $asset_collection_file;
     93        } else {
     94                $asset_collection = $default_asset;
     95        }
     96
     97        wp_register_script(
     98                'bp-blocks-collection',
     99                plugins_url( 'blocks/block-collection/index.js', __FILE__ ),
     100                $asset_collection['dependencies'],
     101                $asset_collection['version'],
     102                false
    94103        );
    95104}
    96105add_action( 'bp_blocks_init', 'bp_register_block_assets', 2 );
     106
     107/**
     108 * Enqueue additional BP Assets for the Block Editor.
     109 *
     110 * @since 12.0.0
     111 */
     112function bp_enqueue_block_editor_assets() {
     113        wp_enqueue_script( 'bp-blocks-collection' );
     114
     115        /**
     116         * Fires when it's time to enqueue BP Block assets.
     117         *
     118         * @since 12.0.0
     119         */
     120        do_action( 'bp_enqueue_block_editor_assets' );
     121}
     122add_action( 'enqueue_block_editor_assets', 'bp_enqueue_block_editor_assets', 9 );
    97123
    98124/**
Note: See TracChangeset for help on using the changeset viewer.