Automattic / Automattic/jetpack

Photon clobbers images in post_content when image is linked and link has '&'

Open
#6,179 1 comment 0 reactions 0 assignees View on GitHub
[Feature] Photon [Pri] Low Bug
Dominant language
PHP
Stars
1.8k
Forks
898
Avg merge
1d 18h
Merged PRs (30d)
774

Description

Given the following data stored in:

```

1. Circle Pay


circle-pay
Pay friends (and get paid) with just an email or phone number. Instantly. No fees. Split the tab, pay your half of the cable bill, chip in for a group gift, beg mom for cash and more.

```

When I download the `article.json` from Alley Interactive's [Apple News](https://github.com/alleyinteractive/apple-news) plugin, the linked image is stripped from the content and I see the following error:

```
[26-Jan-2017 15:31:31 UTC] PHP Warning: preg_replace(): Unknown modifier '0' in /Users/danielbachhuber/projects/vip/wp-content/plugins/jetpack/class.photon.php on line 332
```

The source of the error is:

```
// If present, replace the link href with a Photoned URL for the full-size image.
if ( ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) {
$new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $new_tag, 1 );
}
```

More specifically, my original image link:

```
http://vip.dev/wp-content/uploads/sites/3/2016/09/circle-pay.jpeg?quality=98&strip=all
```

Has been transformed to:

```
http://vip.dev/wp-content/uploads/sites/3/2016/09/circle-pay.jpeg?quality=98&strip=all
```

Notice `&` is now `&`. This transformation is made by `wptexturize()`.

Because `#` is the delimiter in the regex, `$images['link_url'][ $index ]` breaks the regex, which means the image ends up getting stripped entirely.

Contributor guide

Open the contributing guide

Research direction

Start in wp-content/plugins/jetpack/class.photon.php around line 332 and inspect how the image link URL is inserted into the regular expression. Reproduce the issue with the provided post_content containing an encoded ampersand, then verify that the linked image remains in the content and the preg_replace warning no longer occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend, content
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.