epiphyt / embed-privacy

Embed Privacy prevents loading of embedded external content and allows your site visitors to opt-in.
https://epiph.yt/en/embed-privacy/
GNU General Public License v2.0
19 stars 12 forks source link

Regex-Replacement is done for all ocurrences, not just the first matched one #174

Closed alexmerz closed 1 year ago

alexmerz commented 1 year ago

Bug/Problem

If a regex is used to recognize a block of embedded content, the regex and the replacement with the overlay works to "generous".

Basic example is a Post with a content like:

<mycontent>content #1</mycontent>
<mycontent>content #2</mycontent>

and the regex is defined like

/<mycontent>.*<\/mycontent>/

Currently, the result will be, that the overlay will always display "content 1" for both embeds if the embed ist enabled. The reason is that $matches is generated only once for the type of embed block, in line 1301 in inc/class-embed-privacy.php:

if (
   $provider instanceof WP_Post
   && ! get_post_meta( $provider->ID, 'is_system', true )
   && get_post_meta( $provider->ID, 'is_disabled', true ) !== 'yes'
   && preg_match( $args['regex'], $content, $matches )
) {

and then preg_replace is applied without a limit in 1335.

I was able to fix the behavoir by:

I'm not sure if this problem is also directly related to this or not: When the post also contains an embedded content that triggers the population of $this->did_replacements, then the regex does not seems to be executed at all. I was able to get around this by removing the empty( $this->did_replacements ) check in line 1295. But I'm not sure, if this is the correct way to deal with this.

Steps to reproduce

  1. Create a post with two embedded blocks with identical structures but different content.
  2. Create a embed type with a regex matching the structures.
  3. Publish the post.
  4. View published post.
  5. Enable embed content.
  6. Both embeds will contain the content of the first embed.

Version

current main branch

Link

No response

Environment info

WP 6.1 Browser - all Win 11, Desktop

(issue is serverside

Code of Conduct

MatzeKitt commented 1 year ago

I'm not sure if this problem is also directly related to this or not: When the post also contains an embedded content that triggers the population of $this->did_replacements, then the regex does not seems to be executed at all. I was able to get around this by removing the empty( $this->did_replacements ) check in line 1295. But I'm not sure, if this is the correct way to deal with this.

Please create a separate issue for that. I couldn’t produce any actual problem with this information so far.