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

Add template editor rendering #69

Merged
merged 24 commits into from
May 7, 2021
Merged

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Apr 27, 2021

Changes

  • Renders the CSS in the React editor
  • Renders the markup in the React template editor on the front-end and the 'Front-end Preview':
    the-markup9on

Testing instructions

  1. composer install && npm install && npm run dev
  2. /wp-admin > Custom Blocks > Add New
  3. Give the block a title of 'Testing Template'
  4. Add a field
  5. Give the field a label of 'Text':
    the-field-label
  6. Click 'Template Editor'
  7. Enter this:
<div class="foo">
    {{text}}
</div>

templa-sdf

  1. Click the 'CSS' button:
    the-sdf
  2. Enter this:
.foo {
    color: green;
}
  1. Click 'Save draft'
  2. Click 'Editor Preview
  3. Enter some text:
    entering-in
  4. Click 'Front-end Preview'
  5. Expected: The text you entered is there, and it's green:
    when-ijsd

kienstra added 20 commits April 27, 2021 13:39
Just remove /** DocBlock syntax for comments
and use // instead.
This should test that it actually works
better than testing helper methods.
Also, add a comment for where the
template editor markup will be rendered.
Though this doesn't work yet,
it'll need to look for {{}}.
… 'foo' )

The integration test is passing,
but there are still edge cases to handle.
… 'foo' )

The integration test is passing,
but there are still edge cases to handle.
Simply add a return
at the end of the if before it.
Ensure that this renders in {{}}
as expected.
It's possible, though probably not advisable,
that users have field names with { or }.
If they have a tutorial on Mustach,
they might need to render {{example}}
without this doing block_field( 'example' ).
So they can escape it like \{\{example\}\}
and this will strip the escaping on rendering.
Also, don't render the CSS again
in the same document.
This is a little hacky,
but the tests are passing ;).
To test render_css().
render_markup() is tested in
integration tests.
…ndered

If there was no template to render,
look for CSS to render in the <style>.
}

if ( ! empty( $this->blocks[ "genesis-custom-blocks/{$name}" ]['templateMarkup'] ) ) {
$this->template_editor->render_markup( $this->blocks[ "genesis-custom-blocks/{$name}" ]['templateMarkup'] );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This renders the markup:

renders-markup

*
* @inheritdoc
*/
public function setUp() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The inheriting classes can implement this instead.

A DockBlock was just copied,
and it doesn't help much.
@kienstra kienstra marked this pull request as ready for review May 5, 2021 07:12
I couldn't figure the fields out,
there's no official documentation on it,
and the type of the option looks wrong.
@kienstra kienstra requested a review from mikemcalister May 5, 2021 08:17
Copy link
Contributor

@dreamwhisper dreamwhisper left a comment

Choose a reason for hiding this comment

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

This is working great for me.

I noticed that in the editor preview, the image has a max height on it so it doesn't match the front end preview, but not a big deal because in the acutal editor, when you click outside the block, it displays similar in the editor and front end.

However, it would be a nice touch to account for theme CSS in this, but not necessarily in this PR. For example, in this case, I have a heading in the template editor:

Screen Shot 2021-05-06 at 3 52 51 PM

But, it displays this way in the preview:
Screen Shot 2021-05-06 at 3 50 56 PM

And this way on the front end:
Screen Shot 2021-05-06 at 3 55 58 PM

Really nice job, @kienstra!

@kienstra
Copy link
Contributor Author

kienstra commented May 6, 2021

@dreamwhisper,
Thanks so much for your review!

However, it would be a nice touch to account for theme CSS in this, but not necessarily in this PR. For example, in this case, I have a heading in the template editor:

Great idea, your front-end photo looks a lot better, using theme CSS. I'll keep that in mind for future PRs.

Copy link

@mikemcalister mikemcalister left a comment

Choose a reason for hiding this comment

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

Looks great, Ryan. I'm really excited to see this update, I bet a lot of customers will enjoy this.

return;
}

if ( ! current_user_can( 'edit_posts' ) || ! isset( $templates[0] ) ) {
Copy link

@johnstonphilip johnstonphilip May 7, 2021

Choose a reason for hiding this comment

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

I wonder if it makes sense to introduce a custom user permission for this? Maybe current_user_can ( 'edit_genesis_custom_blocks' ) or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, we could use genesis_custom_block_edit_block, but I'm not too sure it would make a big difference.

Here's the main context they would see this in:

the-template-file

If we used genesis_custom_block_edit_block, it would mean editors wouldn't see it.

But either way would probably be fine.


// Escape characters before { should be stripped, like \{\{example\}\}.
// Like if they have a tutorial on Mustache and need the template to render {{example}}.
echo preg_replace( '#\\\{\\\{(\S+?)\\\}\\\}#', '{{\1}}', $rendered ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

Choose a reason for hiding this comment

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

That's a pretty simple regex pattern, nice.

Choose a reason for hiding this comment

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

Nice that you could write about mustache too.

Choose a reason for hiding this comment

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

I do wonder if this could use escaping as well. Just concerned that security researchers might have an issue with this, or even the core plugins team.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. Hm...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about df28138? It's escaped with wp_kses_post().

$this->blocks_with_rendered_css[] = $block_name;

?>
<style><?php echo $css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></style>

Choose a reason for hiding this comment

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

Is there any way to escape this? I can't really think of a way that would be straightforward enough. Just concerning anytime output is not escaped.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Let me think about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about wp_strip_all_tags() with eb4bc47?

The 'Additional CSS' Customizer control does something very similar.

Though it also validates it client-side.

@johnstonphilip
Copy link

This might be out of scope, but when testing I noticed that changes I make to the markup are not reflected in the frontend preview unless I save. It seems like it would be preferable (to me anyway) to see those previews prior to saving.

@kienstra
Copy link
Contributor Author

kienstra commented May 7, 2021

Hi @johnstonphilip,

This might be out of scope, but when testing I noticed that changes I make to the markup are not reflected in the frontend preview unless I save. It seems like it would be preferable (to me anyway) to see those previews prior to saving.

That's a good point, it's not ideal.

The idea is to use the front-end preview that is saved, not just the one that's edited.

That's because the back-end only knows about the registered fields that were saved.

So if the editor sends new fields that aren't saved to the back-end, there will be an error in the preview.

Still, I'll keep that it in mind. It's not a great UX.

kienstra added 2 commits May 7, 2021 11:27
This only escapes markup,
not CSS.
It's not ideal,
but shouldn't allow JS.
$this->blocks_with_rendered_css[] = $block_name;

?>
<style><?php echo wp_strip_all_tags( $css ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></style>

Choose a reason for hiding this comment

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

I like it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@johnstonphilip, thanks a lot!

@johnstonphilip johnstonphilip self-requested a review May 7, 2021 18:45
Copy link

@johnstonphilip johnstonphilip left a comment

Choose a reason for hiding this comment

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

Good stuff @kienstra

@kienstra
Copy link
Contributor Author

kienstra commented May 7, 2021

@johnstonphilip, thanks a lot for reviewing this!

@kienstra
Copy link
Contributor Author

kienstra commented May 7, 2021

Hi @mikemcalister,

Looks great, Ryan. I'm really excited to see this update, I bet a lot of customers will enjoy this.

Thanks so much for reviewing this! Yeah, I hope they like it 😄

@kienstra kienstra merged commit 3670937 into develop May 7, 2021
@kienstra kienstra deleted the add/template-editor-rendering branch May 7, 2021 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants