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

Fix usage of preg_quote() #10998

Merged
merged 1 commit into from
Oct 25, 2018
Merged

Fix usage of preg_quote() #10998

merged 1 commit into from
Oct 25, 2018

Conversation

schlessera
Copy link
Member

The end tag pattern is currently generated like this:

$end_tag_pattern = '/<!--\s+\/wp:' . str_replace( '/', '\/', preg_quote( $block_name ) ) . '\s+-->/';

Instead of separately using a string replace to replace / with \/, it is preferable to use the second argument $delimiter that the preg_quote() function provides.

See http://php.net/manual/en/function.preg-quote.php

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

The end tag pattern is currently generated like this:
```
$end_tag_pattern = '/<!--\s+\/wp:' . str_replace( '/', '\/', preg_quote( $block_name ) ) . '\s+-->/';
```

Instead of separately using a string replace to replace `/` with `\/`, it is preferable to use the second argument `$delimiter` that the `preg_quote()` function provides.

See http://php.net/manual/en/function.preg-quote.php
@Soean Soean added the [Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f label Oct 24, 2018
@mtias mtias requested a review from dmsnell October 24, 2018 11:36
@schlessera
Copy link
Member Author

Note also that the behavior of preg_quote() is not consistent across PHP versions, so it might make sense to provide a wp_preg_quote() polyfill.

From the PHP docs at http://php.net/manual/en/function.preg-quote.php :
image 2018-10-24 at 4 12 59 pm

@@ -232,7 +232,7 @@ function do_blocks( $content ) {
$inner_content = '';

if ( ! $is_self_closing ) {
$end_tag_pattern = '/<!--\s+\/wp:' . str_replace( '/', '\/', preg_quote( $block_name ) ) . '\s+-->/';
$end_tag_pattern = '/<!--\s+\/wp:' . preg_quote( $block_name, '/' ) . '\s+-->/';
Copy link
Member

Choose a reason for hiding this comment

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

this is a great change. but…while we're at it, what if we changed the delimiter too so remove a tiny bit of escaping?

'~<!--\s+/wp:' . preg_quote( $block_name, '~' ) . '\s+-->~'

just a thought, but it only saves one escape 🤷‍♂️

Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

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

This seems like a good change to me from the code perspective!

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Second argument seems to have been designed for this, so makes total sense 👍

#10998 (comment) also seems reasonable, though not strictly related to what's being proposed for improvement here. Worth a separate issue / ticket?

@aduth aduth merged commit a696e50 into master Oct 25, 2018
@aduth aduth deleted the fix/preg-quote-usage branch October 25, 2018 22:08
antpb pushed a commit to antpb/gutenberg that referenced this pull request Oct 26, 2018
The end tag pattern is currently generated like this:
```
$end_tag_pattern = '/<!--\s+\/wp:' . str_replace( '/', '\/', preg_quote( $block_name ) ) . '\s+-->/';
```

Instead of separately using a string replace to replace `/` with `\/`, it is preferable to use the second argument `$delimiter` that the `preg_quote()` function provides.

See http://php.net/manual/en/function.preg-quote.php
daniloercoli added a commit that referenced this pull request Oct 26, 2018
…rnmobile/merge-blocks-on-backspace

* 'master' of https://github.com/WordPress/gutenberg:
  Do not add isDirty prop to DOM (#11093)
  Format API (#10209)
  Remove 4.2 deprecated features (#10952)
  Update `@wordpress/hooks` README to include namespace mention (#11061)
  Feature: save lock control via actions (#10649)
  Fix usage of `preg_quote()` (#10998)
  Update plugin version to 4.1.1 (#11078)
  Improve preloading request code (#11007)
  Fix dynamic blocks not rendering in the frontend (#11050)
  Media & Text: Fixing vertical alignment of the image (#11025)
  Date: Mark getSettings as experimental (#10636)
  Improve handling of centered 1-col galleries with small images (#11040)
  Use better help text for ALT text input; fixes #8391. (#11052)

# Conflicts:
#	packages/editor/src/components/rich-text/index.native.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Parsing Related to efforts to improving the parsing of a string of data and converting it into a different f
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants