Skip to content

Commit

Permalink
fix: image replacement on json strings with html entities encoded, co…
Browse files Browse the repository at this point in the history
…nflicting with variation form Woocommerce fixes #81
  • Loading branch information
selul committed Mar 22, 2019
1 parent 3a84250 commit 2bae741
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 3 additions & 4 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ public function extract_urls_from_json( $content ) {
* @return array Normalized array.
*/
private function normalize_urls( $urls ) {

$urls = array_map(
function ( $value ) {
return rtrim( html_entity_decode( $value ), '\\' );
return rtrim( html_entity_decode( $value ), '\\";\'' );
},
$urls
);

$urls = array_unique( $urls );

return array_values( $urls );
Expand Down Expand Up @@ -434,13 +434,12 @@ public function process_urls_from_content( $html ) {
* @return array
*/
public function extract_image_urls_from_content( $content ) {
$regex = '/(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\??[\w|=|&|\-|\.|:]*)/';
$regex = '/(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:]*)?/';
preg_match_all(
$regex,
$content,
$urls
);

return $this->normalize_urls( $urls[0] );
}

Expand Down
21 changes: 21 additions & 0 deletions tests/test-replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ public function setUp() {

}

public function test_wc_json_replacement() {
$html = [
'image' => "https://www.example.org/wp-content/uploads/2018/05/brands.png",
'image2' => "https://www.example.org/wp-content/uploads/2018/05/brands.png?test=123",
'image3' => "https://www.example.org/wp-content/uploads/2018/05/brands.png?test=123&anther=test",
];


$html = wp_json_encode( $html );
$html = _wp_specialchars( $html, ENT_QUOTES, 'UTF-8', true );

$replaced_content = Optml_Manager::instance()->process_urls_from_content( $html );
$this->assertEquals( 3, substr_count( $replaced_content, 'i.optimole.com' ) );

$replaced_content = wp_specialchars_decode( $replaced_content, ENT_QUOTES );
$replaced_content = json_decode( $replaced_content, true );

$this->assertArrayHasKey( 'image', $replaced_content );

}

public function test_image_tags() {

$found_images = Optml_Manager::parse_images_from_html( self::IMG_TAGS );
Expand Down

0 comments on commit 2bae741

Please sign in to comment.