Changeset 13321
- Timestamp:
- 08/28/2022 09:41:20 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-block.php
r12994 r13321 47 47 * 48 48 * @since 6.0.0 49 * @since 11.0.0 Add support for WP Block API v2 { apiVersion: 2 }. 49 50 * 50 51 * @param array $args { … … 52 53 * used by `WP_Block_Type`. Below are BP specific arguments. 53 54 * 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. 72 78 * } 73 79 */ … … 77 83 } 78 84 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', 91 110 ); 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 } 92 130 93 131 if ( ! isset( $wp_args['name'] ) || ! $wp_args['name'] || ! isset( $wp_args['editor_script'] ) || ! $wp_args['editor_script'] ) { 94 132 $this->block = new WP_Error( 'missing_parameters', __( 'The `name` or `editor_script` required keys are missing.', 'buddypress' ) ); 95 133 } 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 96 147 // Get specific BP Blocks arguments. 97 148 $bp_args = array_intersect_key( 98 149 $args, 99 150 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(), 108 163 ) 109 164 ); 110 165 111 166 // Register the scripts. 167 $this->registered_scripts = array(); 112 168 $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 ) { 116 174 if ( ! isset( $wp_args[ $script_handle_key ] ) || ! $wp_args[ $script_handle_key ] ) { 117 175 continue; … … 181 239 unset( $wp_args['name'] ); 182 240 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 183 246 // Set the Block Type. 184 247 $this->block = new WP_Block_Type( $name, $wp_args ); … … 189 252 // Load Block translations if found. 190 253 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 191 259 /** 192 260 * Filter here to use a custom directory to look for the JSON translation file into. … … 194 262 * @since 6.0.0 195 263 * 196 * @param string $ valueAbsolute 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. 197 265 * @param string $editor_script The editor's script handle. 198 266 * @param string $name The block's name. 199 267 */ 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 } 201 274 202 275 /** … … 205 278 * @since 6.0.0 206 279 * 207 * @param string $ valueThe custom domain for the JSON translation file.280 * @param string $textdomain The custom domain for the JSON translation file. 208 281 * @param string $editor_script The editor's script handle. 209 282 * @param string $name The block's name. 210 283 */ 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 ); 212 285 213 286 // Try to load the translation.
Note: See TracChangeset
for help on using the changeset viewer.