-
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
File Block: Remove i18n from save function #43050
Conversation
Not using i18n in the save function is good, but just hardcoding to English strings is not good. |
Yes I agree. We could just put the filename value in the |
…d handle translation in PHP.
I've updated the PR to remove hardcoded English string in the When the block HTML is saved to the post content, the aria-label will only contain the filename. The PHP will parse the block HTML content to look for an |
Appreciate the work on this. It's on my radar to review, but I haven't quite gotten round to reviewing it yet. I'll try to prioritise for tomorrow 👍 |
}, | ||
$content | ||
); | ||
|
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.
thanks for noticing the work on the WP_HTML_Walker
- for reference, here is roughly the code that would replace what we have here…
// Update object's aria-label attribute if present in block HTML.
// Match an aria-label attribute from an object tag.
$w = new WP_HTML_Walker( $content );
if ( $w->next_tag( 'object' ) ) {
$filename = $w->get_attribute( 'aria-label' ) ?: '';
$label = ! empty( $filename )
sprintf(
/* translators: %s: filename. */
__( 'Embed of %s.' ),
$filename
)
: __( 'PDF embed' );
$w->set_attribute( 'aria-label', $label );
return $w;
}
return $content;
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.
@dmsnell Thanks for the code. Do you think we should we create a follow up issue to update the file block when the work on WP_HTML_Walker is merge ?
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.
sounds like a great idea!
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 created the follow-up PR - #60494.
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.
This works well, for any newly added blocks the invalidation is gone.
Thanks for taking such a thorough approach in this PR.
Comments are all pretty minor, I think the only required change would be to keep outputting the 'PDF embed' from the save
function for consistency, but other comments are all optional.
displayPreview, | ||
previewHeight, | ||
} = attributes; | ||
// Version of the file block with the translated aria-label. |
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'd be good to reference this PR number in this comment, as the other comment does.
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.
Indeed, I've added the PR number in the comment.
__( 'Embed of %s.' ), | ||
fileName | ||
); | ||
const pdfEmbedLabel = RichText.isEmpty( fileName ) ? '' : fileName; |
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 might be good to keep outputting 'PDF embed' here when there's no fileName
. That way the PHP rendering code is treated as a pure progressive enhancement of the JavaScript code.
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've added back the PDF embed
string. I've also added some PHP unit test to ensure the PHP rendering is working correctly in the different scenarios.
$pattern, | ||
function ( $matches ) { | ||
$filename = ! empty( $matches['filename'] ) ? $matches['filename'] : ''; | ||
$has_filename = ! empty( $filename ); |
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.
If, as per my other comment, 'PDF embed' is output from the JavaScript save function, this would need to change to something like $filename !== 'PDF embed'
. Or you could go for ! empty( $attributes['fileName'] )
.
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've updated the render function accordingly.
$content = preg_replace_callback( | ||
$pattern, | ||
function ( $matches ) { | ||
$filename = ! empty( $matches['filename'] ) ? $matches['filename'] : ''; |
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.
You could also consider using $attributes['fileName']
to simplify things, though I suppose that would mean introducing a closure.
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 originaly though of doing that but I discovered that fileName
was not present in the $attributes
array in PHP since its sourced from the HTML of the block.
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.
Apologies for the slow re-review. Thanks for all the hard work on this.
This looks good to me now.
What?
Remove calls to
__()
in file block's save function and move i18n to the PHPrender_callback
.Why?
This is an attempt at addressing #43013
As explain in the issue, file block is using
__
in its save function which cause the block to become invalid when the locale is changed.How?
This PR remove the usage of i18n calls in the save function ensuring the content saved in post content is always the same. The translation is handle in the PHP side using the
render_callback
.It might not be the best way to address the issue. The aim of this PR is to provide a new markup for new and existing valid blocks to avoid validation error in the future if the locale change. It don't handle the case for existing file block who are already breaking.
Using a tool like the
WP_HTML_Walker
#42485 could improve the manipulation of the aria attribute, currently this is done through simple regex matching.Testing Instructions