Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions php/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,24 +355,6 @@ public function add_url_replacements() {
wp_safe_redirect( $referrer );
exit;
}
if ( ! empty( $this->delivery->known ) ) {

foreach ( $this->delivery->known as $url => $set ) {
if ( is_int( $set ) || empty( $set['public_id'] ) ) {
continue;
}
$public_id = $set['public_id'];
if ( ! empty( $set['format'] ) ) {
$public_id .= '.' . $set['format'];
}
$cloudinary_url = $this->media->cloudinary_url( $set['post_id'], array( $set['width'], $set['height'] ), null, $public_id );
if ( $cloudinary_url ) {
// Late replace on unmatched urls (links, inline styles etc..), both http and https.
String_Replace::replace( 'http:' . $url, $cloudinary_url );
String_Replace::replace( 'https:' . $url, $cloudinary_url );
}
}
}
}

/**
Expand Down
101 changes: 67 additions & 34 deletions php/class-delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function filter_out_cloudinary( $content ) {
$transformations = $this->media->get_transformations_from_string( $original_url );

if ( 'image' === $this->media->get_resource_type( $result['post_id'] ) ) {
$attachment_url = wp_get_attachment_image_url( $result['post_id'], $size );
$attachment_url = wp_get_attachment_image_url( $result['post_id'], $size );
} else {
$attachment_url = wp_get_attachment_url( $result['post_id'] );
}
Expand Down Expand Up @@ -567,7 +567,7 @@ protected function setup_hooks() {
// Add filters.
add_filter( 'content_save_pre', array( $this, 'filter_out_cloudinary' ) );
add_action( 'save_post', array( $this, 'remove_replace_cache' ) );
add_action( 'cloudinary_string_replace', array( $this, 'catch_urls' ) );
add_action( 'cloudinary_string_replace', array( $this, 'catch_urls' ), 10, 2 );
add_filter( 'post_thumbnail_html', array( $this, 'process_featured_image' ), 100, 3 );

add_filter( 'cloudinary_current_post_id', array( $this, 'get_current_post_id' ) );
Expand Down Expand Up @@ -669,6 +669,12 @@ public function setup() {
*/
protected function init_delivery() {

// Reset internals.
$this->known = array();
$this->unknown = array();
$this->found_urls = array();
$this->unusable = array();

add_filter( 'wp_calculate_image_srcset', array( $this->media, 'image_srcset' ), 10, 5 );

/**
Expand Down Expand Up @@ -807,7 +813,6 @@ protected function get_context_cache() {
*/
public function get_media_tags( $content, $tags = 'img|video' ) {
$images = array();
$urls = '';
if ( preg_match_all( '#(?P<tags><(' . $tags . ')[^>]*\>){1}#is', $content, $found ) ) {
$count = count( $found[0] );
for ( $i = 0; $i < $count; $i ++ ) {
Expand All @@ -822,16 +827,17 @@ public function get_media_tags( $content, $tags = 'img|video' ) {
* Convert media tags from Local to Cloudinary, and register with String_Replace.
*
* @param string $content The HTML to find tags and prep replacement in.
* @param string $context The content of the content.
*
* @return array
*/
public function convert_tags( $content ) {
public function convert_tags( $content, $context = 'view' ) {
$has_cache = $this->get_context_cache();
$type = is_ssl() ? 'https' : 'http';
if ( Utils::is_amp() ) {
$type = 'amp';
}
if ( ! empty( $has_cache[ $type ] ) ) {
if ( 'view' === $context && ! empty( $has_cache[ $type ] ) ) {
$cached = $has_cache[ $type ];
}

Expand Down Expand Up @@ -863,29 +869,50 @@ public function convert_tags( $content ) {
continue;
}
$this->current_post_id = $set['context'];
// Use cached item if found.
if ( isset( $cached[ $set['original'] ] ) ) {
$replacements[ $set['original'] ] = $cached[ $set['original'] ];
} else {
// Register replacement.
$replacements[ $set['original'] ] = $this->rebuild_tag( $set );

// We only rebuild tags in the view context.
if ( 'view' === $context ) {
// Use cached item if found.
if ( isset( $cached[ $set['original'] ] ) ) {
$replacements[ $set['original'] ] = $cached[ $set['original'] ];
} else {
// Register replacement.
$replacements[ $set['original'] ] = $this->rebuild_tag( $set );
}
}
$this->current_post_id = null;

// Check for src aliases.
if ( isset( $this->found_urls[ $set['base_url'] ] ) ) {
$base = dirname( $set['base_url'] );
foreach ( $this->found_urls[ $set['base_url'] ] as $size => $file_name ) {
$local_url = $type . ':' . path_join( $base, $file_name );
if ( isset( $cached[ $local_url ] ) ) {
$aliases[ $local_url ] = $cached[ $local_url ];
continue;
}
$cloudinary_url = $this->media->cloudinary_url( $set['id'], explode( 'x', $size ), $set['transformations'], $set['atts']['data-public-id'], $set['overwrite_transformations'] );
$aliases[ $local_url ] = $cloudinary_url;
}

// Create aliases for urls where were found, but not found with an ID in a tag.
// Create the Full/Scaled items first.
foreach ( $this->known as $url => $relation ) {
if ( $url === $relation['public_id'] ) {
continue; // We don't need the public_id relation item.
}
$base = $type . ':' . $url;
$public_id = ! is_admin() ? $relation['public_id'] . '.' . $relation['format'] : null;
$aliases[ $base ] = $this->media->cloudinary_url( $relation['post_id'], array(), $relation['transformations'], $public_id );
}

// Create the sized found relations second.
foreach ( $this->found_urls as $url => $sizes ) {
if ( ! isset( $this->known[ $url ] ) ) {
continue;
}
$base = $type . ':' . $url;
$relation = $this->known[ $url ];
$public_id = ! is_admin() ? $relation['public_id'] . '.' . $relation['format'] : null;
foreach ( $sizes as $size => $file_name ) {
$local_url = path_join( dirname( $base ), $file_name );
if ( isset( $cached[ $local_url ] ) ) {
$aliases[ $local_url ] = $cached[ $local_url ];
continue;
}
$aliases[ $local_url ] = $this->media->cloudinary_url( $relation['post_id'], explode( 'x', $size ), $relation['transformations'], $public_id );
}
}

// Move aliases to the end of the run, after images.
if ( ! empty( $aliases ) ) {
$replacements = array_merge( $replacements, $aliases );
Expand Down Expand Up @@ -1526,11 +1553,15 @@ public function prepare_delivery( $content ) {
$urls = array_merge( $desized, $scaled );
$urls = array_values( $urls ); // resets the index.

// clean out empty urls.
$cloudinary_urls = array_filter( $base_urls, array( $this->media, 'is_cloudinary_url' ) ); // clean out empty urls.
// Clean URLS for search.
$public_ids = array_filter( array_map( array( $this->media, 'get_public_id_from_url' ), $cloudinary_urls ) );

$public_ids = array();
// Lets only look for Cloudinary URLs on the frontend.
if ( ! is_admin() ) {
// clean out empty urls.
$cloudinary_urls = array_filter( $base_urls, array( $this->media, 'is_cloudinary_url' ) ); // clean out empty urls.
// Clean URLS for search.
$all_public_ids = array_filter( array_map( array( $this->media, 'get_public_id_from_url' ), $cloudinary_urls ) );
$public_ids = array_unique( $all_public_ids );
}
if ( empty( $urls ) && empty( $public_ids ) ) {
return; // Bail since theres nothing.
}
Expand Down Expand Up @@ -1586,22 +1617,24 @@ public function query_relations( $public_ids, $urls = array() ) {
* Catch attachment URLS from HTML content.
*
* @param string $content The HTML to catch URLS from.
* @param string $context The content of the content.
*/
public function catch_urls( $content ) {
public function catch_urls( $content, $context = 'view' ) {

$this->init_delivery();
$this->prepare_delivery( $content );
$known = $this->convert_tags( $content );

if ( ! empty( $this->known ) ) {
$known = $this->convert_tags( $content, $context );
// Replace the knowns.
foreach ( $known as $src => $replace ) {
String_Replace::replace( $src, $replace );
}
}
// Attempt to get the unknowns.
if ( ! empty( $this->unknown ) ) {
$this->find_attachment_size_urls();
}

// Replace the knowns.
foreach ( $known as $src => $replace ) {
String_Replace::replace( $src, $replace );
}

}
}
8 changes: 6 additions & 2 deletions php/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ protected function get_cache_key( $args ) {
*
* @param int $attachment_id The id of the attachment.
* @param array|string $size The wp size to set for the URL.
* @param array $transformations Set of transformations to apply to this url.
* @param array|string $transformations Set of transformations to apply to this url.
* @param string|null $cloudinary_id Optional forced cloudinary ID.
* @param bool $overwrite_transformations Flag url is a breakpoint URL to stop re-applying default transformations.
*
Expand Down Expand Up @@ -1260,6 +1260,9 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
$set_size = $this->prepare_size( $attachment_id, $size );
}
// Prepare transformations.
if ( ! empty( $transformations ) && is_string( $transformations ) ) {
$transformations = $this->get_transformations_from_string( $transformations, $resource_type );
}
$pre_args['transformation'] = $this->get_transformations( $attachment_id, $transformations, $overwrite_transformations );

// Make a copy as not to destroy the options in \Cloudinary::cloudinary_url().
Expand Down Expand Up @@ -2772,6 +2775,8 @@ public function apply_media_library_filters( $query ) {

if ( ! empty( $result ) ) {
$query->set( 'post__in', $result );
} else {
$query->set( 'post__in', array( 0 ) );
}
}
}
Expand Down Expand Up @@ -2838,7 +2843,6 @@ public function setup() {
add_action( 'print_media_templates', array( $this, 'media_template' ) );
add_action( 'wp_enqueue_media', array( $this, 'editor_assets' ) );
add_action( 'wp_ajax_cloudinary-down-sync', array( $this, 'down_sync_asset' ) );
add_action( 'rest_api_init', array( $this, 'add_live_url_filters' ) );

// Filter to add cloudinary folder.
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
Expand Down
Loading