-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add featured image to Media & Text block #51491
Merged
Merged
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
4289a75
Add featured image to Media & Text block
carolinan 1ff701f
Try adding the featured image option to the toolbar (replace flow)
carolinan 9505f7d
fix JS warning in templates where there is no featured image.
carolinan f1de86e
Add deprecation, update fixtures
carolinan c1f8969
Try adding index.php
carolinan e599a7d
Index.php: use the $content and only replace the image source and the…
carolinan b70c2fa
Update index.php
carolinan 27d42bc
Update index.php
carolinan f12bd39
Add alt and class names to the image tag
carolinan 02484dc
Fix white space issue
carolinan 38bd0c7
Add a placeholder for the featured image
carolinan 2d1ce2c
Remove the deprecation
carolinan ebf6555
Add condition with useFeaturedImage, remove alt text option from plac…
carolinan 87fbd79
Set a minimum height for the placeholder image
carolinan d0e6eda
Merge branch 'trunk' into add/media-text-featured-image
carolinan 759db59
Remove set_attribute 'alt' from index.php.
carolinan d7a553e
Merge branch 'trunk' into add/media-text-featured-image
carolinan 3a47c7c
Update editor.scss
carolinan 9755334
Merge branch 'trunk' into add/media-text-featured-image
carolinan 665f068
Update packages/block-library/src/media-text/index.php
carolinan 183fb7a
Merge branch 'trunk' into add/media-text-featured-image
carolinan c916464
Update packages/block-library/src/media-text/media-container.js
carolinan 4ee5cf3
Merge branch 'trunk' into add/media-text-featured-image
carolinan 7e3c6b1
Try removing the image mediaType and inserting the img tag in index.php.
carolinan ac0a606
CS fix
carolinan f89e7af
Merge branch 'trunk' into add/media-text-featured-image
carolinan 1aa6703
Add condition to save.js.
carolinan 96c3a18
Merge branch 'trunk' into add/media-text-featured-image
carolinan ffdc6f1
Merge branch 'trunk' into add/media-text-featured-image
carolinan 4c19b22
Merge branch 'trunk' into add/media-text-featured-image
carolinan f4508d5
CS: Remove the truthy default value for withIllustration in media-tex…
carolinan 04a6d07
Update the condition that outputs the img tag
carolinan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/media-text` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/media-text` block on server. | ||
* | ||
* @param array $attributes The block attributes. | ||
* @param string $content The block rendered content. | ||
* | ||
* @return string Returns the Media & Text block markup, if useFeaturedImage is true. | ||
*/ | ||
function render_block_core_media_text( $attributes, $content ) { | ||
if ( false === $attributes['useFeaturedImage'] ) { | ||
return $content; | ||
} | ||
|
||
if ( in_the_loop() ) { | ||
update_post_thumbnail_cache(); | ||
} | ||
|
||
$current_featured_image = get_the_post_thumbnail_url(); | ||
if ( ! $current_featured_image ) { | ||
return $content; | ||
} | ||
|
||
$image_tag = '<figure class="wp-block-media-text__media"><img>'; | ||
$content = preg_replace( '/<figure\s+class="wp-block-media-text__media">/', $image_tag, $content ); | ||
|
||
$processor = new WP_HTML_Tag_Processor( $content ); | ||
if ( isset( $attributes['imageFill'] ) && $attributes['imageFill'] ) { | ||
$position = '50% 50%'; | ||
if ( isset( $attributes['focalPoint'] ) ) { | ||
$position = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%'; | ||
} | ||
$processor->next_tag( 'figure' ); | ||
$processor->set_attribute( 'style', 'background-image:url(' . esc_url( $current_featured_image ) . ');background-position:' . $position . ';' ); | ||
} | ||
$processor->next_tag( 'img' ); | ||
$media_size_slug = 'full'; | ||
if ( isset( $attributes['mediaSizeSlug'] ) ) { | ||
$media_size_slug = $attributes['mediaSizeSlug']; | ||
} | ||
$processor->set_attribute( 'src', esc_url( $current_featured_image ) ); | ||
$processor->set_attribute( 'class', 'wp-image-' . get_post_thumbnail_id() . ' size-' . $media_size_slug ); | ||
|
||
$content = $processor->get_updated_html(); | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Registers the `core/media-text` block renderer on server. | ||
*/ | ||
function register_block_core_media_text() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/media-text', | ||
array( | ||
'render_callback' => 'render_block_core_media_text', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_media_text' ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we check for
featuredImageURL
andfeaturedImageAlt
asbool
in many places can we set them tofalse
if they have no value? IMO the way JS saysfalse || ''
isfalse
is better if we default to testing forfalse
? Is there any downside?Alternatively we could make
hasFeaturedImageAlt
andhasFeaturedImageURL
, respectively. set to!== ''
, then use these inbool
checks later?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this, but it meant that the alt text was set to the string "false" instead of being empty when:
It shows the text "false" inside the alt text option in the editor, and in the attribute.
So this condition on line 316 also needs to be updated.
value={ mediaAlt || featuredImageAlt }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@draganescu I think I did not fully understand what to do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means we need the empty string, In which case it means we should not check for
value={ mediaAlt || featuredImageAlt }
later but instead forvalue={ mediaAlt || featuredImageAlt !== '' }
. That's it :)