Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/compat/wordpress-7.0/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Block compatibility functions for WordPress 7.0
*
* Adds support for the `mediaRoles` property in block.json metadata.
* This ensures that the mediaRoles property is passed to JavaScript
* when blocks are registered via register_block_type_from_metadata().
*
* @package gutenberg
*/

/**
* Filters the settings determined from the block type metadata.
*
* WordPress core doesn't yet support the mediaRoles property in block.json.
* This filter adds it to the registered block type so it gets passed to JavaScript.
*
* @param WP_Block_Type $block_type Block type object.
* @param array $metadata Block metadata from block.json.
* @return WP_Block_Type Modified block type with mediaRoles if present.
*/
function gutenberg_add_media_roles_to_block_type( $block_type, $metadata ) {
// Check if mediaRoles exists in the metadata.
if ( isset( $metadata['mediaRoles'] ) && is_array( $metadata['mediaRoles'] ) ) {
// Add mediaRoles as a public property on the block type.
// This will be included when the block type is passed to JavaScript.
$block_type->media_roles = $metadata['mediaRoles'];
}

return $block_type;
}

add_filter( 'register_block_type_args', 'gutenberg_add_media_roles_to_block_type', 10, 2 );
23 changes: 23 additions & 0 deletions lib/compat/wordpress-7.0/rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* PHP and WordPress configuration compatibility functions for the Gutenberg
* editor plugin changes related to REST API.
*
* @package gutenberg
*/

/**
* Adds mediaRoles to the block type REST API response.
* @since 7.0.0
*
* @param WP_REST_Response $response The response object.
* @param WP_Block_Type $block_type Block type object.
* @return WP_REST_Response Modified response with mediaRoles if present.
*/
function gutenberg_add_media_roles_to_rest_response( $response, $block_type ) {
if ( isset( $block_type->media_roles ) ) {
$response->data['media_roles'] = $block_type->media_roles;
}
return $response;
}
add_filter( 'rest_prepare_block_type', 'gutenberg_add_media_roles_to_rest_response', 10, 2 );
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.9/client-assets.php';

// WordPress 7.0 compat.
require __DIR__ . '/compat/wordpress-7.0/blocks.php';
require __DIR__ . '/compat/wordpress-7.0/php-only-blocks.php';
require __DIR__ . '/compat/wordpress-7.0/rest-api.php';

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,82 @@
*/
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { privateApis as blocksPrivateApis } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

const { getMediaRoleAttributes } = unlock( blocksPrivateApis );

// Maximum number of images to display in a list view row.
const MAX_IMAGES = 3;
const IMAGE_GETTERS = {
'core/image': ( { clientId, attributes } ) => {
if ( attributes.url ) {
return {
url: attributes.url,
alt: attributes.alt || '',
clientId,
};
}
},
'core/cover': ( { clientId, attributes } ) => {
if ( attributes.backgroundType === 'image' && attributes.url ) {
return {
url: attributes.url,
alt: attributes.alt || '',
clientId,
};
}
},
'core/media-text': ( { clientId, attributes } ) => {
if ( attributes.mediaType === 'image' && attributes.mediaUrl ) {
return {
url: attributes.mediaUrl,
alt: attributes.mediaAlt || '',
clientId,
};
}
},
'core/gallery': ( { innerBlocks } ) => {
const images = [];
const getValues = !! innerBlocks?.length
? IMAGE_GETTERS[ innerBlocks[ 0 ].name ]
: undefined;
if ( ! getValues ) {
return images;
}

for ( const innerBlock of innerBlocks ) {
const img = getValues( innerBlock );
if ( img ) {
images.push( img );
function getImagesFromBlock( block, isExpanded ) {
// Don't show images for expanded blocks (inner blocks shown separately).
if ( isExpanded && block.innerBlocks?.length ) {
return [];
}
const images = [];
// First, try to get an image from the block itself.
const blockImage = getImageFromMediaBlock( block );
if ( blockImage ) {
images.push( blockImage );
}
// Recursively check direct inner blocks.
// TODO: this is intentional for the first draft as we might not even want to
// support this for every block with child blocks. This covers the `gallery` block
// but also adds the `image` info for other blocks, like a `Group` with an Image block.
if ( block.innerBlocks?.length ) {
for ( const innerBlock of block.innerBlocks ) {
const innerImage = getImageFromMediaBlock( innerBlock );
if ( innerImage ) {
images.push( innerImage );
}
if ( images.length >= MAX_IMAGES ) {
return images;
break;
}
}

return images;
},
};

function getImagesFromBlock( block, isExpanded ) {
const getImages = IMAGE_GETTERS[ block.name ];
const images = !! getImages ? getImages( block ) : undefined;

if ( ! images ) {
return [];
}
return images;
}

if ( ! Array.isArray( images ) ) {
return [ images ];
function getImageFromMediaBlock( block ) {
const mediaAttributes = getMediaRoleAttributes(
block.name,
block.attributes
);
// Only show images in list view and only when there are media attributes and a url.
if (
! mediaAttributes ||
mediaAttributes.type !== 'image' ||
! mediaAttributes.url
) {
return null;
}

return isExpanded ? [] : images;
return {
url: mediaAttributes.url,
alt: mediaAttributes.alt || '',
clientId: block.clientId,
};
}

/**
* Get a block's preview images for display within a list view row.
*
* TODO: Currently only supports images from the core/image and core/gallery
* blocks. This should be expanded to support other blocks that have images,
* potentially via an API that blocks can opt into / provide their own logic.
* Uses the `mediaRoles` block config to generically extract media information
* from any block that defines media roles.
*
* @param {Object} props Hook properties.
* @param {string} props.clientId The block's clientId.
* @param {boolean} props.isExpanded Whether or not the block is expanded in the list view.
* @return {Array} Images.
*/
export default function useListViewImages( { clientId, isExpanded } ) {
const { block } = useSelect(
( select ) => {
return { block: select( blockEditorStore ).getBlock( clientId ) };
},
const block = useSelect(
( select ) => select( blockEditorStore ).getBlock( clientId ),
[ clientId ]
);
const images = useMemo( () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/audio/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
"clientNavigation": true
}
},
"mediaRoles": {
"id": "id",
"url": "src",
"type": { "value": "audio" }
},
"editorStyle": "wp-block-audio-editor",
"style": "wp-block-audio"
}
6 changes: 6 additions & 0 deletions packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
},
"allowedBlocks": true
},
"mediaRoles": {
"id": "id",
"url": "url",
"alt": "alt",
"type": { "attribute": "backgroundType" }
},
Comment on lines +151 to +156

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly, the type can have a hardcoded value or derive it from a specific attribute. Is that correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is needed to handle both blocks without a type attribute like Image and the ones that have a type.

What I haven't thought much right now is what happens if blocks have already a similar type attribute, but its values are not strictly among image|video|audio. This is not the case for the core blocks, but something that we might need to consider for 3rd party development.

"selectors": {
"filter": {
"duotone": ".wp-block-cover > .wp-block-cover__image-background, .wp-block-cover > .wp-block-cover__video-background"
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@
"__experimentalSkipSerialization": true
}
},
"mediaRoles": {
"id": "id",
"url": "url",
"alt": "alt",
"type": { "value": "image" }
},
"selectors": {
"border": ".wp-block-image img, .wp-block-image .wp-block-image__crop-area, .wp-block-image .components-placeholder",
"shadow": ".wp-block-image img, .wp-block-image .wp-block-image__crop-area, .wp-block-image .components-placeholder",
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/media-text/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
},
"allowedBlocks": true
},
"mediaRoles": {
"id": "mediaId",
"url": "mediaUrl",
"alt": "mediaAlt",
"type": { "attribute": "mediaType" }
},
"editorStyle": "wp-block-media-text-editor",
"style": "wp-block-media-text"
}
5 changes: 5 additions & 0 deletions packages/block-library/src/video/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
"clientNavigation": true
}
},
"mediaRoles": {
"id": "id",
"url": "src",
"type": { "value": "video" }
},
"editorStyle": "wp-block-video-editor",
"style": "wp-block-video"
}
4 changes: 2 additions & 2 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import { lock } from '../lock-unlock';
import { isContentBlock } from './utils';
import { isContentBlock, getMediaRoleAttributes } from './utils';

// The blocktype is the most important concept within the block API. It defines
// all aspects of the block configuration and its interfaces, including `edit`
Expand Down Expand Up @@ -177,4 +177,4 @@ export {
} from './constants';

export const privateApis = {};
lock( privateApis, { isContentBlock } );
lock( privateApis, { isContentBlock, getMediaRoleAttributes } );
1 change: 1 addition & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function getBlockSettingsFromMetadata( { textdomain, ...metadata } ) {
'variations',
'blockHooks',
'allowedBlocks',
'mediaRoles',
];

const settings = Object.fromEntries(
Expand Down
Loading