Skip to:
Content

BuddyPress.org

Changeset 13321


Ignore:
Timestamp:
08/28/2022 09:41:20 PM (3 years ago)
Author:
imath
Message:

BP Block API: add WP Block API v2 support

The BP Block API now contains all new v2 settings support as well as improvements about language translation loading for external BuddyPress plugins and a specific buddypress_contexts setting to control in which BuddyPress context a block can be loaded (eg: the future BP Activity Block editor).

Fixes #8731

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-block.php

    r12994 r13321  
    4747     *
    4848     * @since 6.0.0
     49     * @since 11.0.0 Add support for WP Block API v2 { apiVersion: 2 }.
    4950     *
    5051     * @param array $args {
     
    5253     *     used by `WP_Block_Type`. Below are BP specific arguments.
    5354     *
    54      *     @type string $name               The name of the block (eg: `bp/member`).
    55      *     @type string $editor_script_url  URL to the JavaScript main file of the BP Block
    56      *                                      to load into the Block Editor.
    57      *     @type array  $editor_script_deps The list of JavaScript dependency handles for the
    58      *                                      BP Block main file.
    59      *     @type string $script_url         URL to the JavaScript file to load into the Block
    60      *                                      Editor and on front-end.
    61      *     @type array  $script_deps        The list of JavaScript dependency handles for the
    62      *                                      JavaScript file to load into the Block Editor and
    63      *                                      on front-end.
    64      *     @type string $editor_style_url   URL to the CSS main file of the BP Block to load
    65      *                                      into the Block Editor.
    66      *     @type array  $editor_style_deps  The list of CSS dependency handles for the
    67      *                                      CSS main file.
    68      *     @type string $style_url          URL to the CSS file to load into the Block Editor
    69      *                                      and on front-end.
    70      *     @type array  $style_deps         The list of CSS dependency handles for the CSS file
    71      *                                      to load into the Block Editor and on front-end.
     55     *     @type string $editor_script_url   URL to the JavaScript main file of the BP Block
     56     *                                       to load into the Block Editor.
     57     *     @type array  $editor_script_deps  The list of JavaScript dependency handles for the
     58     *                                       BP Block main file.
     59     *     @type string $script_url          URL to the JavaScript file to load into the Block
     60     *                                       Editor and on front-end.
     61     *     @type array  $script_deps         The list of JavaScript dependency handles for the
     62     *                                       JavaScript file to load into the Block Editor and
     63     *                                       on front-end.
     64     *     @type string $view_script_url     URL to the JavaScript file to load on front-end.
     65     *     @type array  $view_script_deps    The list of JavaScript dependency handles for the
     66     *                                       JavaScript file to load on front-end.
     67     *     @type string $editor_style_url    URL to the CSS main file of the BP Block to load
     68     *                                       into the Block Editor.
     69     *     @type array  $editor_style_deps   The list of CSS dependency handles for the
     70     *                                       CSS main file.
     71     *     @type string $style_url           URL to the CSS file to load into the Block Editor
     72     *                                       and on front-end.
     73     *     @type array  $style_deps          The list of CSS dependency handles for the CSS file
     74     *                                       to load into the Block Editor and on front-end.
     75     *     @type string $domain_path         The path to the folder where custom block translations
     76     *                                       are located.
     77     *     @type array  $buddypress_contexts The list of BuddyPress contexts a block can be loaded into.
    7278     * }
    7379     */
     
    7783        }
    7884
    79         $min     = bp_core_get_minified_asset_suffix();
    80         $wp_args = array_intersect_key(
    81             $args,
    82             array(
    83                 'name'            => '',
    84                 'render_callback' => '',
    85                 'attributes'      => '',
    86                 'editor_script'   => '',
    87                 'script'          => '',
    88                 'editor_style'    => '',
    89                 'style'           => '',
    90             )
     85        $min          = bp_core_get_minified_asset_suffix();
     86        $metadata_map = array(
     87            'ancestor'        => 'ancestor',
     88            'apiVersion'      => 'api_version',
     89            'attributes'      => 'attributes',
     90            'category'        => 'category',
     91            'description'     => 'description',
     92            'editorScript'    => 'editor_script',
     93            'editorStyle'     => 'editor_style',
     94            'example'         => 'example',
     95            'icon'            => 'icon',
     96            'keywords'        => 'keywords',
     97            'name'            => 'name',
     98            'parent'          => 'parent',
     99            'providesContext' => 'provides_context',
     100            'script'          => 'script',
     101            'style'           => 'style',
     102            'styles'          => 'styles',
     103            'supports'        => 'supports',
     104            'textdomain'      => 'textdomain',
     105            'title'           => 'title',
     106            'usesContext'     => 'uses_context',
     107            'variations'      => 'variations',
     108            'version'         => 'version',
     109            'viewScript'      => 'view_script',
    91110        );
     111
     112        // Init WordPress Block $args.
     113        $wp_args = array();
     114
     115        // rekey $args.
     116        foreach ( $args as $arg_key => $arg ) {
     117            $snake_case_key = '';
     118
     119            if ( isset( $metadata_map[ $arg_key ] ) ) {
     120                $snake_case_key             = $metadata_map[ $arg_key ];
     121                $wp_args[ $snake_case_key ] = $arg;
     122            } elseif ( in_array( $arg_key, $metadata_map, true ) ) {
     123                $wp_args[ $arg_key ] = $arg;
     124            }
     125        }
     126
     127        if ( isset( $args['render_callback'] ) && $args['render_callback'] ) {
     128            $wp_args['render_callback'] = $args['render_callback'];
     129        }
    92130
    93131        if ( ! isset( $wp_args['name'] ) || ! $wp_args['name'] || ! isset( $wp_args['editor_script'] ) || ! $wp_args['editor_script'] ) {
    94132            $this->block = new WP_Error( 'missing_parameters', __( 'The `name` or `editor_script` required keys are missing.', 'buddypress' ) );
    95133        } else {
     134            if ( isset( $wp_args['api_version'], $args['plugin_url'] ) && 2 === (int) $wp_args['api_version'] ) {
     135                foreach ( array( 'editor_script', 'editor_style', 'script', 'style', 'view_script' ) as $asset_key ) {
     136                    if ( ! isset( $wp_args[ $asset_key ] ) ) {
     137                        continue;
     138                    }
     139
     140                    $asset_abs_uri_key          = $asset_key . '_url';
     141                    $args[ $asset_abs_uri_key ] = trailingslashit( $args['plugin_url'] ) . remove_block_asset_path_prefix( $wp_args[ $asset_key ] );
     142                    $args[ $asset_key ]         = str_replace( '/', '-', $wp_args['name'] ) . '-' . str_replace( '_', '-', $asset_key );
     143                    $wp_args[ $asset_key ]      = $args[ $asset_key ];
     144                }
     145            }
     146
    96147            // Get specific BP Blocks arguments.
    97148            $bp_args = array_intersect_key(
    98149                $args,
    99150                array(
    100                     'editor_script_url'  => '',
    101                     'editor_script_deps' => array(),
    102                     'script_url'         => '',
    103                     'script_deps'        => array(),
    104                     'editor_style_url'   => '',
    105                     'editor_style_deps'  => array(),
    106                     'style_url'          => '',
    107                     'style_deps'         => array(),
     151                    'editor_script_url'   => '',
     152                    'editor_script_deps'  => array(),
     153                    'script_url'          => '',
     154                    'script_deps'         => array(),
     155                    'view_script_url'     => '',
     156                    'view_script_deps'    => array(),
     157                    'editor_style_url'    => '',
     158                    'editor_style_deps'   => array(),
     159                    'style_url'           => '',
     160                    'style_deps'          => array(),
     161                    'domain_path'         => null,
     162                    'buddypress_contexts' => array(),
    108163                )
    109164            );
    110165
    111166            // Register the scripts.
     167            $this->registered_scripts = array();
    112168            $version                  = bp_get_version();
    113             $this->registered_scripts = array();
    114 
    115             foreach ( array( 'editor_script', 'script' ) as $script_handle_key ) {
     169            if ( isset( $wp_args['version'] ) && $wp_args['version'] ) {
     170                $version = $wp_args['version'];
     171            }
     172
     173            foreach ( array( 'editor_script', 'script', 'view_script' ) as $script_handle_key ) {
    116174                if ( ! isset( $wp_args[ $script_handle_key ] ) || ! $wp_args[ $script_handle_key ] ) {
    117175                    continue;
     
    181239                unset( $wp_args['name'] );
    182240
     241                // Used to restrict blocks to specific BuddyPress contexts.
     242                if ( isset( $bp_args['buddypress_contexts'] ) ) {
     243                    $wp_args['buddypress_contexts'] = $bp_args['buddypress_contexts'];
     244                }
     245
    183246                // Set the Block Type.
    184247                $this->block = new WP_Block_Type( $name, $wp_args );
     
    189252                // Load Block translations if found.
    190253                if ( $this->block->editor_script ) {
     254                    $domain_path = null;
     255                    if ( isset( $bp_args['domain_path'] ) && is_dir( $bp_args['domain_path'] ) ) {
     256                        $domain_path = $bp_args['domain_path'];
     257                    }
     258
    191259                    /**
    192260                     * Filter here to use a custom directory to look for the JSON translation file into.
     
    194262                     * @since 6.0.0
    195263                     *
    196                      * @param string $value         Absolute path to the directory to look for the JSON translation file into.
     264                     * @param string $domain_path   Absolute path to the directory to look for the JSON translation file into.
    197265                     * @param string $editor_script The editor's script handle.
    198266                     * @param string $name          The block's name.
    199267                     */
    200                     $translation_dir = apply_filters( 'bp_block_translation_dir', null, $this->block->editor_script, $name );
     268                    $translation_dir = apply_filters( 'bp_block_translation_dir', $domain_path, $this->block->editor_script, $name );
     269
     270                    $textdomain = 'buddypress';
     271                    if ( isset( $wp_args['textdomain'] ) && $wp_args['textdomain'] ) {
     272                        $textdomain = $wp_args['textdomain'];
     273                    }
    201274
    202275                    /**
     
    205278                     * @since 6.0.0
    206279                     *
    207                      * @param string $value         The custom domain for the JSON translation file.
     280                     * @param string $textdomain    The custom domain for the JSON translation file.
    208281                     * @param string $editor_script The editor's script handle.
    209282                     * @param string $name          The block's name.
    210283                     */
    211                     $translation_domain = apply_filters( 'bp_block_translation_domain', 'buddypress', $this->block->editor_script, $name );
     284                    $translation_domain = apply_filters( 'bp_block_translation_domain', $textdomain, $this->block->editor_script, $name );
    212285
    213286                    // Try to load the translation.
Note: See TracChangeset for help on using the changeset viewer.