Skip to content
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

Merged
merged 8 commits into from
Sep 12, 2022
Merged

Conversation

petitphp
Copy link
Contributor

@petitphp petitphp commented Aug 7, 2022

What?

Remove calls to __() in file block's save function and move i18n to the PHP render_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

  1. Create a new post
  2. Upload a pdf to a file block
  3. Publish the post
  4. Change to a different language
  5. Reload the post in the editor
  6. The file block is valid and displayed correctly

@petitphp petitphp changed the title Fix/43013 File Block: Remove i18n from save function Aug 7, 2022
@swissspidy
Copy link
Member

Not using i18n in the save function is good, but just hardcoding to English strings is not good.

@petitphp
Copy link
Contributor Author

petitphp commented Aug 8, 2022

Yes I agree. We could just put the filename value in the aria-label when saving the block and let the PHP side generate the complete value. That way we wouldn't have hardcoded or duplicate strings.

@petitphp
Copy link
Contributor Author

petitphp commented Aug 8, 2022

I've updated the PR to remove hardcoded English string in the save function and rewrite the PHP to be more strict when looking for the aria-label.

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 object tag with an aria-label and update it with the corresponding translated version.

@talldan
Copy link
Contributor

talldan commented Aug 9, 2022

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 👍

@talldan talldan added [Type] Bug An existing feature does not function as intended [Block] File Affects the File Block labels Aug 9, 2022
},
$content
);

Copy link
Member

@dmsnell dmsnell Aug 9, 2022

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;

Copy link
Contributor Author

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 ?

Copy link
Member

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!

Copy link
Member

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.

Copy link
Contributor

@talldan talldan left a 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.
Copy link
Contributor

@talldan talldan Aug 10, 2022

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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.

Copy link
Contributor Author

@petitphp petitphp Aug 18, 2022

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 );
Copy link
Contributor

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'] ).

Copy link
Contributor Author

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'] : '';
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@petitphp petitphp requested review from talldan and removed request for ajitbohra August 18, 2022 13:55
Copy link
Contributor

@talldan talldan left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] File Affects the File Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants