Skip to content

Commit

Permalink
Temporary fix to serve the view file for the File block
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jun 24, 2021
1 parent 8b16704 commit fe0d7d0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
23 changes: 18 additions & 5 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ function generate_block_asset_handle( $block_name, $field_name ) {
if ( 0 === strpos( $field_name, 'editor' ) ) {
$asset_handle .= '-editor';
}
if ( 0 === strpos( $field_name, 'view' ) ) {
$asset_handle .= '-view';
}
return $asset_handle;
}

$field_mappings = array(
'editorScript' => 'editor-script',
'script' => 'script',
'viewScript' => 'view-script',
'editorStyle' => 'editor-style',
'style' => 'style',
);
Expand Down Expand Up @@ -96,12 +100,14 @@ function register_block_script_handle( $metadata, $field_name ) {
);
return false;
}
$script_asset = require $script_asset_path;
$result = wp_register_script(
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], ABSPATH . WPINC );
$script_uri = $is_core_block ? includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . '/' . $script_path ) : plugins_url( $script_path, $metadata['file'] );
$script_asset = require $script_asset_path;
$result = wp_register_script(
$script_handle,
plugins_url( $script_path, $metadata['file'] ),
$script_asset['dependencies'],
$script_asset['version']
$script_uri,
isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array(),
isset( $script_asset['version'] ) ? $script_asset['version'] : false,
);
if ( ! $result ) {
return false;
Expand Down Expand Up @@ -306,6 +312,13 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
);
}

if ( ! empty( $metadata['viewScript'] ) ) {
$settings['view_script'] = register_block_script_handle(
$metadata,
'viewScript'
);
}

if ( ! empty( $metadata['editorStyle'] ) ) {
$settings['editor_style'] = register_block_style_handle(
$metadata,
Expand Down
10 changes: 4 additions & 6 deletions src/wp-includes/blocks/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
*/

/**
* When the `core/file` block is rendering, check if we need to enqueue the `'wp-block-library-file` script.
* When the `core/file` block is rendering, check if we need to enqueue the `'wp-block-file-view` script.
*
* @param array $attributes The block attributes.
* @param array $content The block content.
*
* @return string Returns the block content.
*/
function render_block_core_file( $attributes, $content ) {
if ( ! empty( $attributes['displayPreview'] ) ) {
// Check if it's already enqueued, so we don't add the inline script multiple times.
if ( ! wp_script_is( 'wp-block-library-file' ) ) {
wp_enqueue_script( 'wp-block-library-file', plugins_url( 'file/frontend.js', __FILE__ ) );
}
$should_load_view_script = ! empty( $attributes['displayPreview'] ) && ! wp_script_is( 'wp-block-file-view' );
if ( $should_load_view_script ) {
wp_enqueue_script( 'wp-block-file-view' );
}

return $content;
Expand Down
3 changes: 2 additions & 1 deletion src/wp-includes/blocks/file/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"title": "File",
"category": "media",
"description": "Add a link to a downloadable file.",
"keywords": [ "document", "pdf", "download" ],
"keywords": ["document", "pdf", "download"],
"textdomain": "default",
"attributes": {
"id": {
Expand Down Expand Up @@ -51,6 +51,7 @@
"anchor": true,
"align": true
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-file-editor",
"style": "wp-block-file"
}
25 changes: 17 additions & 8 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,39 @@ class WP_Block_Type {
public $provides_context = null;

/**
* Block type editor script handle.
* Block type editor only script handle.
*
* @since 5.0.0
* @var string|null
*/
public $editor_script = null;

/**
* Block type front end script handle.
* Block type front end and editor script handle.
*
* @since 5.0.0
* @var string|null
*/
public $script = null;

/**
* Block type editor style handle.
* Block type front end only script handle.
*
* @since 5.0.0
* @var string|null
*/
public $view_script = null;

/**
* Block type editor only style handle.
*
* @since 5.0.0
* @var string|null
*/
public $editor_style = null;

/**
* Block type front end style handle.
* Block type front end and editor style handle.
*
* @since 5.0.0
* @var string|null
Expand Down Expand Up @@ -219,10 +227,11 @@ class WP_Block_Type {
* @type array|null $attributes Block type attributes property schemas.
* @type array $uses_context Context values inherited by blocks of this type.
* @type array|null $provides_context Context provided by blocks of this type.
* @type string|null $editor_script Block type editor script handle.
* @type string|null $script Block type front end script handle.
* @type string|null $editor_style Block type editor style handle.
* @type string|null $style Block type front end style handle.
* @type string|null $editor_script Block type editor only script handle.
* @type string|null $script Block type front end and editor script handle.
* @type string|null $view_script Block type front end only script handle.
* @type string|null $editor_style Block type editor only style handle.
* @type string|null $style Block type front end and editor style handle.
* }
*/
public function __construct( $block_type, $args = array() ) {
Expand Down

0 comments on commit fe0d7d0

Please sign in to comment.