<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13321] trunk/src/bp-core/classes/class-bp-block.php: BP Block API: add WP Block API v2 support</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13321">13321</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13321","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-08-28 21:41:20 +0000 (Sun, 28 Aug 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/8731">#8731</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreclassesclassbpblockphp">trunk/src/bp-core/classes/class-bp-block.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcoreclassesclassbpblockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-block.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-block.php      2022-08-19 15:06:51 UTC (rev 13320)
+++ trunk/src/bp-core/classes/class-bp-block.php        2022-08-28 21:41:20 UTC (rev 13321)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -46,29 +46,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Construct the BuddyPress Block.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 11.0.0 Add support for WP Block API v2 { apiVersion: 2 }.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $args {
</span><span class="cx" style="display: block; padding: 0 10px">         *     The registration arguments for the BP Block. Part of the arguments are the ones
</span><span class="cx" style="display: block; padding: 0 10px">         *     used by `WP_Block_Type`. Below are BP specific arguments.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *     @type string $name               The name of the block (eg: `bp/member`).
-        *     @type string $editor_script_url  URL to the JavaScript main file of the BP Block
-        *                                      to load into the Block Editor.
-        *     @type array  $editor_script_deps The list of JavaScript dependency handles for the
-        *                                      BP Block main file.
-        *     @type string $script_url         URL to the JavaScript file to load into the Block
-        *                                      Editor and on front-end.
-        *     @type array  $script_deps        The list of JavaScript dependency handles for the
-        *                                      JavaScript file to load into the Block Editor and
-        *                                      on front-end.
-        *     @type string $editor_style_url   URL to the CSS main file of the BP Block to load
-        *                                      into the Block Editor.
-        *     @type array  $editor_style_deps  The list of CSS dependency handles for the
-        *                                      CSS main file.
-        *     @type string $style_url          URL to the CSS file to load into the Block Editor
-        *                                      and on front-end.
-        *     @type array  $style_deps         The list of CSS dependency handles for the CSS file
-        *                                      to load into the Block Editor and on front-end.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *     @type string $editor_script_url   URL to the JavaScript main file of the BP Block
+        *                                       to load into the Block Editor.
+        *     @type array  $editor_script_deps  The list of JavaScript dependency handles for the
+        *                                       BP Block main file.
+        *     @type string $script_url          URL to the JavaScript file to load into the Block
+        *                                       Editor and on front-end.
+        *     @type array  $script_deps         The list of JavaScript dependency handles for the
+        *                                       JavaScript file to load into the Block Editor and
+        *                                       on front-end.
+        *     @type string $view_script_url     URL to the JavaScript file to load on front-end.
+        *     @type array  $view_script_deps    The list of JavaScript dependency handles for the
+        *                                       JavaScript file to load on front-end.
+        *     @type string $editor_style_url    URL to the CSS main file of the BP Block to load
+        *                                       into the Block Editor.
+        *     @type array  $editor_style_deps   The list of CSS dependency handles for the
+        *                                       CSS main file.
+        *     @type string $style_url           URL to the CSS file to load into the Block Editor
+        *                                       and on front-end.
+        *     @type array  $style_deps          The list of CSS dependency handles for the CSS file
+        *                                       to load into the Block Editor and on front-end.
+        *     @type string $domain_path         The path to the folder where custom block translations
+        *                                       are located.
+        *     @type array  $buddypress_contexts The list of BuddyPress contexts a block can be loaded into.
</ins><span class="cx" style="display: block; padding: 0 10px">          * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $args ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -76,43 +82,95 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        _doing_it_wrong( __METHOD__, esc_html__( 'BP Blocks needs to be registered hooking `bp_blocks_init`', 'buddypress' ), '6.0.0' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $min     = bp_core_get_minified_asset_suffix();
-               $wp_args = array_intersect_key(
-                       $args,
-                       array(
-                               'name'            => '',
-                               'render_callback' => '',
-                               'attributes'      => '',
-                               'editor_script'   => '',
-                               'script'          => '',
-                               'editor_style'    => '',
-                               'style'           => '',
-                       )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $min          = bp_core_get_minified_asset_suffix();
+               $metadata_map = array(
+                       'ancestor'        => 'ancestor',
+                       'apiVersion'      => 'api_version',
+                       'attributes'      => 'attributes',
+                       'category'        => 'category',
+                       'description'     => 'description',
+                       'editorScript'    => 'editor_script',
+                       'editorStyle'     => 'editor_style',
+                       'example'         => 'example',
+                       'icon'            => 'icon',
+                       'keywords'        => 'keywords',
+                       'name'            => 'name',
+                       'parent'          => 'parent',
+                       'providesContext' => 'provides_context',
+                       'script'          => 'script',
+                       'style'           => 'style',
+                       'styles'          => 'styles',
+                       'supports'        => 'supports',
+                       'textdomain'      => 'textdomain',
+                       'title'           => 'title',
+                       'usesContext'     => 'uses_context',
+                       'variations'      => 'variations',
+                       'version'         => 'version',
+                       'viewScript'      => 'view_script',
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Init WordPress Block $args.
+               $wp_args = array();
+
+               // rekey $args.
+               foreach ( $args as $arg_key => $arg ) {
+                       $snake_case_key = '';
+
+                       if ( isset( $metadata_map[ $arg_key ] ) ) {
+                               $snake_case_key             = $metadata_map[ $arg_key ];
+                               $wp_args[ $snake_case_key ] = $arg;
+                       } elseif ( in_array( $arg_key, $metadata_map, true ) ) {
+                               $wp_args[ $arg_key ] = $arg;
+                       }
+               }
+
+               if ( isset( $args['render_callback'] ) && $args['render_callback'] ) {
+                       $wp_args['render_callback'] = $args['render_callback'];
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $wp_args['name'] ) || ! $wp_args['name'] || ! isset( $wp_args['editor_script'] ) || ! $wp_args['editor_script'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->block = new WP_Error( 'missing_parameters', __( 'The `name` or `editor_script` required keys are missing.', 'buddypress' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( isset( $wp_args['api_version'], $args['plugin_url'] ) && 2 === (int) $wp_args['api_version'] ) {
+                               foreach ( array( 'editor_script', 'editor_style', 'script', 'style', 'view_script' ) as $asset_key ) {
+                                       if ( ! isset( $wp_args[ $asset_key ] ) ) {
+                                               continue;
+                                       }
+
+                                       $asset_abs_uri_key          = $asset_key . '_url';
+                                       $args[ $asset_abs_uri_key ] = trailingslashit( $args['plugin_url'] ) . remove_block_asset_path_prefix( $wp_args[ $asset_key ] );
+                                       $args[ $asset_key ]         = str_replace( '/', '-', $wp_args['name'] ) . '-' . str_replace( '_', '-', $asset_key );
+                                       $wp_args[ $asset_key ]      = $args[ $asset_key ];
+                               }
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Get specific BP Blocks arguments.
</span><span class="cx" style="display: block; padding: 0 10px">                        $bp_args = array_intersect_key(
</span><span class="cx" style="display: block; padding: 0 10px">                                $args,
</span><span class="cx" style="display: block; padding: 0 10px">                                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'editor_script_url'  => '',
-                                       'editor_script_deps' => array(),
-                                       'script_url'         => '',
-                                       'script_deps'        => array(),
-                                       'editor_style_url'   => '',
-                                       'editor_style_deps'  => array(),
-                                       'style_url'          => '',
-                                       'style_deps'         => array(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'editor_script_url'   => '',
+                                       'editor_script_deps'  => array(),
+                                       'script_url'          => '',
+                                       'script_deps'         => array(),
+                                       'view_script_url'     => '',
+                                       'view_script_deps'    => array(),
+                                       'editor_style_url'    => '',
+                                       'editor_style_deps'   => array(),
+                                       'style_url'           => '',
+                                       'style_deps'          => array(),
+                                       'domain_path'         => null,
+                                       'buddypress_contexts' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 )
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Register the scripts.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $this->registered_scripts = array();
</ins><span class="cx" style="display: block; padding: 0 10px">                         $version                  = bp_get_version();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->registered_scripts = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( isset( $wp_args['version'] ) && $wp_args['version'] ) {
+                               $version = $wp_args['version'];
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        foreach ( array( 'editor_script', 'script' ) as $script_handle_key ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 foreach ( array( 'editor_script', 'script', 'view_script' ) as $script_handle_key ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( ! isset( $wp_args[ $script_handle_key ] ) || ! $wp_args[ $script_handle_key ] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        continue;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -180,6 +238,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $name = $wp_args['name'];
</span><span class="cx" style="display: block; padding: 0 10px">                                unset( $wp_args['name'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // Used to restrict blocks to specific BuddyPress contexts.
+                               if ( isset( $bp_args['buddypress_contexts'] ) ) {
+                                       $wp_args['buddypress_contexts'] = $bp_args['buddypress_contexts'];
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 // Set the Block Type.
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->block = new WP_Block_Type( $name, $wp_args );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -188,27 +251,37 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Load Block translations if found.
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( $this->block->editor_script ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        $domain_path = null;
+                                       if ( isset( $bp_args['domain_path'] ) && is_dir( $bp_args['domain_path'] ) ) {
+                                               $domain_path = $bp_args['domain_path'];
+                                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                                         * Filter here to use a custom directory to look for the JSON translation file into.
</span><span class="cx" style="display: block; padding: 0 10px">                                         *
</span><span class="cx" style="display: block; padding: 0 10px">                                         * @since 6.0.0
</span><span class="cx" style="display: block; padding: 0 10px">                                         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                         * @param string $value         Absolute path to the directory to look for the JSON translation file into.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                  * @param string $domain_path   Absolute path to the directory to look for the JSON translation file into.
</ins><span class="cx" style="display: block; padding: 0 10px">                                          * @param string $editor_script The editor's script handle.
</span><span class="cx" style="display: block; padding: 0 10px">                                         * @param string $name          The block's name.
</span><span class="cx" style="display: block; padding: 0 10px">                                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $translation_dir = apply_filters( 'bp_block_translation_dir', null, $this->block->editor_script, $name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $translation_dir = apply_filters( 'bp_block_translation_dir', $domain_path, $this->block->editor_script, $name );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        $textdomain = 'buddypress';
+                                       if ( isset( $wp_args['textdomain'] ) && $wp_args['textdomain'] ) {
+                                               $textdomain = $wp_args['textdomain'];
+                                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                                         * Filter here to use a custom domain for the JSON translation file.
</span><span class="cx" style="display: block; padding: 0 10px">                                         *
</span><span class="cx" style="display: block; padding: 0 10px">                                         * @since 6.0.0
</span><span class="cx" style="display: block; padding: 0 10px">                                         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                         * @param string $value         The custom domain for the JSON translation file.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                  * @param string $textdomain    The custom domain for the JSON translation file.
</ins><span class="cx" style="display: block; padding: 0 10px">                                          * @param string $editor_script The editor's script handle.
</span><span class="cx" style="display: block; padding: 0 10px">                                         * @param string $name          The block's name.
</span><span class="cx" style="display: block; padding: 0 10px">                                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $translation_domain = apply_filters( 'bp_block_translation_domain', 'buddypress', $this->block->editor_script, $name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $translation_domain = apply_filters( 'bp_block_translation_domain', $textdomain, $this->block->editor_script, $name );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        // Try to load the translation.
</span><span class="cx" style="display: block; padding: 0 10px">                                        $translated = wp_set_script_translations( $this->block->editor_script, $translation_domain, $translation_dir );
</span></span></pre>
</div>
</div>

</body>
</html>