Skip to content

Commit

Permalink
feat: adds compatibility for schemaless and relative image URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
preda-bogdan authored and selul committed Dec 13, 2019
1 parent b48355f commit e3886eb
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 26 deletions.
24 changes: 17 additions & 7 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,11 @@ public function init() {
self::$filters = $this->settings->get_filters();
add_filter(
'optml_possible_lazyload_flags',
function( $strings = array() ) {
function ( $strings = array() ) {
foreach ( self::$filters[ Optml_Settings::FILTER_TYPE_LAZYLOAD ][ Optml_Settings::FILTER_CLASS ] as $rule_flag => $status ) {
$strings[] = $rule_flag;
}

return $strings;
},
10,
Expand Down Expand Up @@ -310,9 +311,6 @@ public function set_properties() {
'secret' => $service_data['cdn_secret'],
)
);
$this->site_mappings['//i0.wp.com/'] = '//';
$this->site_mappings['//i1.wp.com/'] = '//';
$this->site_mappings['//i2.wp.com/'] = '//';

if ( defined( 'OPTML_SITE_MIRROR' ) && constant( 'OPTML_SITE_MIRROR' ) ) {
$this->site_mappings[ rtrim( get_home_url(), '/' ) ] = rtrim( constant( 'OPTML_SITE_MIRROR' ), '/' );
Expand All @@ -326,9 +324,12 @@ public function set_properties() {
)
);

$this->allowed_sources = $this->extract_domain_from_urls( $service_data['whitelist'] );

$this->is_allowed_site = count( array_diff_key( $this->possible_sources, $this->allowed_sources ) ) > 0;
$this->allowed_sources = $this->extract_domain_from_urls( $service_data['whitelist'] );
// Allways allow Photon urls.
$this->allowed_sources['i0.wp.com'] = true;
$this->allowed_sources['i1.wp.com'] = true;
$this->allowed_sources['i2.wp.com'] = true;
$this->is_allowed_site = count( array_diff_key( $this->possible_sources, $this->allowed_sources ) ) > 0;

$this->max_height = $this->settings->get( 'max_height' );
$this->max_width = $this->settings->get( 'max_width' );
Expand All @@ -342,6 +343,15 @@ public function set_properties() {
);
}

/**
* Method to expose upload resource property.
*
* @return null
*/
public function get_upload_resource() {
return $this->upload_resource;
}

/**
* List to resizes and save the crop for later re-use.
*
Expand Down
18 changes: 12 additions & 6 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,15 @@ private function do_url_replacement( $html, $extracted_urls ) {
return $html;
}

$urls = array_combine( $extracted_urls, $extracted_urls );
$urls = array_map(
function ( $url ) {
$upload_resource = $this->tag_replacer->get_upload_resource();
$urls = array_combine( $extracted_urls, $extracted_urls );
$urls = array_map(
function ( $url ) use ( $upload_resource ) {
$is_relative = strpos( $url, $upload_resource['content_path'] ) === 0;
if ( $is_relative ) {
$url = $upload_resource['content_host'] . $url;
}

$is_slashed = strpos( $url, '\/' ) !== false;
$url = html_entity_decode( $url );
$new_url = apply_filters( 'optml_content_url', $url );
Expand All @@ -346,7 +352,7 @@ function ( $url ) {
);

foreach ( $urls as $origin => $replace ) {
$html = preg_replace( '/(?<!\/)' . preg_quote( $origin, '/' ) . '/m', $replace, $html );
$html = preg_replace( '/(?<![\/|:|\\w])' . preg_quote( $origin, '/' ) . '/m', $replace, $html );
}

return $html;
Expand Down Expand Up @@ -495,14 +501,14 @@ public function process_urls_from_content( $html ) {
* @return array
*/
public function extract_image_urls_from_content( $content ) {
$regex = '/(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*(?:[' . Optml_Config::$chars . '])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?/';
$regex = '/(?:[(|\s\';",])((?:http|\/|\\\\){1}(?:[\/:,\\\\.\-\d_' . Optml_Config::$chars . ']{10,}\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')))(?=(?:|\?|"|&|,|\s|\'|\)|\||\\\\|}))/U';
preg_match_all(
$regex,
$content,
$urls
);

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

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/test-lazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function test_replacement_with_jetpack_photon() {

$replaced_content = Optml_Manager::instance()->replace_content( $content );
$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertNotContains( 'i0.wp.com', $replaced_content );
$this->assertContains( 'i0.wp.com', $replaced_content );
}

public function test_lazyload_only_gif() {
Expand Down
Loading

0 comments on commit e3886eb

Please sign in to comment.