Skip to content
Permalink
Browse files

fix: JSON strings url replacement, improves elementor's compatibility

  • Loading branch information...
selul committed Feb 13, 2019
1 parent c26cdab commit 2ff9e9bb1b8f49685218923efd16fb079f6b131b
Showing with 104 additions and 22 deletions.
  1. +87 −18 inc/manager.php
  2. +16 −3 tests/test-replacer.php
  3. +1 −1 themeisle-hash.json
@@ -169,25 +169,74 @@ public function replace_meta( $metadata, $object_id, $meta_key, $single ) {
return $metadata;
}
return $this->replace_content( $current_meta, 'elementor' );
return $this->process_urls_from_json( $current_meta );
}
// Return original if the check does not pass
return $metadata;
}
/**
* Filter raw content for urls.
* Process json string.
*
* @param string $html HTML to filter.
* @param string $context Context for $html.
* @param string $json Json string.
*
* @return mixed Filtered content.
* @return string Processed string.
*/
public function replace_content( $html, $context = 'raw' ) {
$html = $this->process_images_from_content( $html );
public function process_urls_from_json( $json ) {
$extracted_urls = $this->extract_image_urls_from_content( $html );
$extracted_urls = $this->extract_urls_from_json( $json );
return $this->do_url_replacement( $json, $extracted_urls );
}
/**
* Extract urls used as values in json string, i.e not prefixed by =("|') char.
*
* @param string $content Raw json string.
*
* @return array array of urls.
*/
public function extract_urls_from_json( $content ) {
$regex = '/(?<!(=|\\\\)(?:"|\'|"))(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')/';
preg_match_all(
$regex,
$content,
$urls
);
return $this->normalize_urls( $urls[0] );
}
/**
* Normalize extracted urls.
*
* @param array $urls Raw urls extracted.
*
* @return array Normalized array.
*/
private function normalize_urls( $urls ) {
$urls = array_map(
function ( $value ) {
return rtrim( html_entity_decode( $value ), '\\' );
},
$urls
);
$urls = array_unique( $urls );
return array_values( $urls );
}
/**
* Process string content and replace possible urls.
*
* @param string $html String content.
* @param array $extracted_urls Urls to check.
*
* @return string Processed html.
*/
private function do_url_replacement( $html, $extracted_urls ) {
$extracted_urls = apply_filters( 'optml_extracted_urls', $extracted_urls );
if ( empty( $extracted_urls ) ) {
@@ -212,6 +261,21 @@ function ( $url ) {
return $html;
}
/**
* Filter raw HTML content for urls.
*
* @param string $html HTML to filter.
*
* @return mixed Filtered content.
*/
public function replace_content( $html ) {
$html = $this->process_images_from_content( $html );
$html = $this->process_urls_from_content( $html );
return $html;
}
/**
* Adds a filter with detected images tags and the content.
*
@@ -306,6 +370,20 @@ public static function strip_header_from_content( $content ) {
return str_replace( $matches[0], '', $content );
}
/**
* Process url replacement from raw html strings.
*
* @param string $html Raw html.
*
* @return string Processed string.
*/
public function process_urls_from_content( $html ) {
$extracted_urls = $this->extract_image_urls_from_content( $html );
return $this->do_url_replacement( $html, $extracted_urls );
}
/**
* Method to extract images from content.
*
@@ -321,16 +399,7 @@ public function extract_image_urls_from_content( $content ) {
$urls
);
$urls = array_map(
function ( $value ) {
return rtrim( html_entity_decode( $value ), '\\' );
},
$urls[0]
);
$urls = array_unique( $urls );
return array_values( $urls );
return $this->normalize_urls( $urls[0] );
}
/**
@@ -34,7 +34,7 @@ class Test_Replacer extends WP_UnitTestCase {
';
const IMAGE_SIZE_NO_CLASS = '<div id="wp-custom-header" class="wp-custom-header"><img src="http://example.org/wp-content/themes/twentyseventeen/assets/images/header-100x100.png" alt="Test" /></div></div>';
const ELEMENTOR_DATA = '[{"id":"428f250c","elType":"section","settings":{"structure":"33","content_width":{"unit":"px","size":1140},"content_position":"middle","gap":"extended","padding":{"unit":"px","top":"10","right":"0","bottom":"10","left":"0","isLinked":false},"padding_mobile":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":true}},"elements":[{"id":"1b041a88","elType":"column","settings":{"_column_size":25,"_inline_size":20.66,"_inline_size_tablet":25,"_inline_size_mobile":50,"content_position":"top"},"elements":[{"id":"34d685ef","elType":"widget","settings":{"image":{"id":36009,"url":"https:\/\/www.example.org\/wp-content\/uploads\/2018\/05\/codeinwp-logo.svg"},"image_size":"full","link_to":"custom","link":{"url":"https:\/\/www.example.org\/","is_external":"","nofollow":""},"align":"left","width":{"unit":"px","size":120},"space":{"unit":"%","size":100},"opacity":{"unit":"px","size":1},"_margin":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":false},"_element_id":"logo"},"elements":[],"widgetType":"image"}],"isInner":false},{"id":"437f5756","elType":"column","settings":{"_column_size":50,"_inline_size":71.992000000000004,"_inline_size_tablet":70,"_inline_size_mobile":40,"padding_mobile":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":true}},"elements":[{"id":"3c7d3ebf","elType":"widget","settings":{"align_items":"right","pointer":"none","color_menu_item":"#0a4266","menu_typography_typography":"custom","menu_typography_font_weight":"bold","menu_typography_text_transform":"lowercase","color_menu_item_hover":"#ec4646","color_menu_item_active":"#ec4646","menu_typography_font_size":{"unit":"px","size":18},"_margin":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":false},"indicator":"none","dropdown":"mobile","full_width":"stretch","menu_typography_font_size_tablet":{"unit":"px","size":18},"padding_horizontal_menu_item_tablet":{"unit":"px","size":14},"toggle_size":{"unit":"px","size":25},"_padding":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":false},"color_dropdown_item":"#0a4266","color_dropdown_item_hover":"#ec4646","background_color_dropdown_item_hover":"rgba(0,0,0,0)","dropdown_typography_typography":"custom","dropdown_typography_font_family":"proxima-nova","dropdown_typography_font_size":{"unit":"px","size":16},"menu_typography_font_size_mobile":{"unit":"px","size":18},"dropdown_typography_font_size_mobile":{"unit":"px","size":25},"dropdown_typography_text_transform":"lowercase","menu":"main-menu-homepage"},"elements":[],"widgetType":"nav-menu"}],"isInner":false},{"id":"7fafd26c","elType":"column","settings":{"_column_size":25,"_inline_size":7.3479999999999999,"_inline_size_tablet":5,"_inline_size_mobile":2},"elements":[{"id":"8f16004","elType":"widget","settings":{"image":{"url":"https:\/\/www.example.org\/wp-content\/uploads\/2018\/05\/test.png","id":36135},"image_size":"custom","width":{"unit":"px","size":30},"space":{"unit":"%","size":30},"_element_id":"header-trigger"},"elements":[],"widgetType":"image"}],"isInner":false}],"isInner":false}]';
const ELEMENTOR_DATA = '[{"id":"428f250c","elType":"section","settings":{"structure":"<img alt=\"\"src=\"https:\/\/www.example.org\/wp-content\/uploads\/2018\/05\/test2.png\" \/>","content_width":{"unit":"px","size":1140},"content_position":"middle","gap":"extended","padding":{"unit":"px","top":"10","right":"0","bottom":"10","left":"0","isLinked":false},"padding_mobile":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":true}},"elements":[{"id":"1b041a88","elType":"column","settings":{"_column_size":25,"_inline_size":20.66,"_inline_size_tablet":25,"_inline_size_mobile":50,"content_position":"top"},"elements":[{"id":"34d685ef","elType":"widget","settings":{"image":{"id":36009,"url":"https:\/\/www.example.org\/wp-content\/uploads\/2018\/05\/codeinwp-logo.svg"},"image_size":"full","link_to":"custom","link":{"url":"https:\/\/www.example.org\/","is_external":"","nofollow":""},"align":"left","width":{"unit":"px","size":120},"space":{"unit":"%","size":100},"opacity":{"unit":"px","size":1},"_margin":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":false},"_element_id":"logo"},"elements":[],"widgetType":"image"}],"isInner":false},{"id":"437f5756","elType":"column","settings":{"_column_size":50,"_inline_size":71.992000000000004,"_inline_size_tablet":70,"_inline_size_mobile":40,"padding_mobile":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":true}},"elements":[{"id":"3c7d3ebf","elType":"widget","settings":{"align_items":"right","pointer":"none","color_menu_item":"#0a4266","menu_typography_typography":"custom","menu_typography_font_weight":"bold","menu_typography_text_transform":"lowercase","color_menu_item_hover":"#ec4646","color_menu_item_active":"#ec4646","menu_typography_font_size":{"unit":"px","size":18},"_margin":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":false},"indicator":"none","dropdown":"mobile","full_width":"stretch","menu_typography_font_size_tablet":{"unit":"px","size":18},"padding_horizontal_menu_item_tablet":{"unit":"px","size":14},"toggle_size":{"unit":"px","size":25},"_padding":{"unit":"px","top":"0","right":"0","bottom":"0","left":"0","isLinked":false},"color_dropdown_item":"#0a4266","color_dropdown_item_hover":"#ec4646","background_color_dropdown_item_hover":"rgba(0,0,0,0)","dropdown_typography_typography":"custom","dropdown_typography_font_family":"proxima-nova","dropdown_typography_font_size":{"unit":"px","size":16},"menu_typography_font_size_mobile":{"unit":"px","size":18},"dropdown_typography_font_size_mobile":{"unit":"px","size":25},"dropdown_typography_text_transform":"lowercase","menu":"main-menu-homepage"},"elements":[],"widgetType":"nav-menu"}],"isInner":false},{"id":"7fafd26c","elType":"column","settings":{"_column_size":25,"_inline_size":7.3479999999999999,"_inline_size_tablet":5,"_inline_size_mobile":2},"elements":[{"id":"8f16004","elType":"widget","settings":{"image":{"url":"https:\/\/www.example.org\/wp-content\/uploads\/2018\/05\/test.png","id":36135},"image_size":"custom","width":{"unit":"px","size":30},"space":{"unit":"%","size":30},"_element_id":"header-trigger"},"elements":[],"widgetType":"image"}],"isInner":false}],"isInner":false}]';
const TEST_STAGING = '<div class="before-footer">
<div class="codeinwp-container">
@@ -146,12 +146,25 @@ public function test_non_allowed_extensions() {
}
public function test_elementor_data() {
$replaced_content = Optml_Manager::instance()->replace_content( ( self::ELEMENTOR_DATA ), 'elementor' );
$replaced_content = Optml_Manager::instance()->process_urls_from_json( ( self::ELEMENTOR_DATA ) );
$this->assertContains( 'i.optimole.com', $replaced_content );
//Test if wrong extension is still present in the output.
//Ensure the json is not corrupted after replacement.
$this->assertTrue( is_array( json_decode( $replaced_content ) ) );
//Make sure the image tag is unprocessed.
$this->assertContains( "src=\\\"https:\/\/www.example.org\/wp-content", $replaced_content );
//Do the html replacement.
$replaced_content = Optml_Manager::instance()->replace_content( ( $replaced_content ) );
//The content should be sucessfully processed.
$this->assertNotContains( "\"https:\/\/www.example.org\/wp-content", $replaced_content );
}
public function test_max_size_height() {
$new_url = Optml_Manager::instance()->replace_content( 'http://example.org/wp-content/themes/test/assets/images/header.png', [
'width' => 99999,
@@ -1 +1 @@
{"optimole-wp.php":"ac1b296e575224597f0cba676855333b"}
{"optimole-wp.php":"20a82b4dd2709b542f65f917fa90d828"}

0 comments on commit 2ff9e9b

Please sign in to comment.
You can’t perform that action at this time.