Skip to content

Commit 5bde74c

Browse files
Merge pull request #1075 from cloudinary/optimize/find_attachments
Improve unknown URLs search
2 parents a6837f9 + e885345 commit 5bde74c

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

php/class-delivery.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ public function find_attachment_size_urls() {
896896
$dirs = wp_get_upload_dir();
897897
$baseurl = Utils::clean_url( $dirs['baseurl'] );
898898
$search = array();
899+
899900
foreach ( $this->unknown as $url ) {
900901
$url = ltrim( str_replace( $baseurl, '', $url ), '/' );
901902
$search[] = $url;
@@ -913,6 +914,7 @@ public function find_attachment_size_urls() {
913914
$key = md5( $sql );
914915
$cached = wp_cache_get( $key );
915916
$auto_sync = $this->sync->is_auto_sync_enabled();
917+
916918
if ( false === $cached ) {
917919
$cached = array();
918920
$results = $wpdb->get_results( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
@@ -930,12 +932,14 @@ public function find_attachment_size_urls() {
930932
* @return {int}
931933
*/
932934
$post_id = apply_filters( 'cloudinary_contextualized_post_id', $result->post_id );
935+
933936
if ( ! $this->is_deliverable( $post_id ) ) {
934937
continue;
935938
}
936939
// If we are here, it means that an attachment in the media library doesn't have a delivery for the url.
937940
// Reset the signature for delivery and add to sync, to update it.
938941
$this->create_delivery( $post_id );
942+
939943
if ( true === $auto_sync ) {
940944
$this->sync->add_to_sync( $post_id );
941945
}
@@ -1933,7 +1937,70 @@ public function prepare_delivery( $content ) {
19331937
$this->set_usability( $result, $auto_sync );
19341938
}
19351939
// Set unknowns.
1936-
$this->unknown = array_diff( $urls, array_keys( $this->known ) );
1940+
$this->unknown = $this->filter_unknown_urls( $urls );
1941+
}
1942+
1943+
/**
1944+
* Filter URLs to determine which are truly unknown, considering image size variations.
1945+
*
1946+
* This method treats image size variations (e.g., example-300x224.png) as "known"
1947+
* if their base image (e.g., example.png) exists in the known URLs, while still
1948+
* catching genuinely unknown URLs.
1949+
*
1950+
* @param array $urls All URLs found in content.
1951+
*
1952+
* @return array Array of genuinely unknown URLs.
1953+
*/
1954+
protected function filter_unknown_urls( $urls ) {
1955+
$known_keys = array_keys( $this->known );
1956+
1957+
if ( empty( $known_keys ) ) {
1958+
return $urls;
1959+
}
1960+
1961+
$known_lookup = array_flip( $known_keys );
1962+
$potential_unknown = array_diff( $urls, $known_keys );
1963+
1964+
if ( empty( $potential_unknown ) ) {
1965+
return array();
1966+
}
1967+
1968+
$truly_unknown = array();
1969+
1970+
foreach ( $potential_unknown as $url ) {
1971+
// Check if this might be a sized variation of a known image.
1972+
$base_url = $this->maybe_unsize_url( $url );
1973+
1974+
// If base image is known, skip this variation.
1975+
if ( isset( $known_lookup[ $base_url ] ) ) {
1976+
continue;
1977+
}
1978+
1979+
// Check scaled version if base wasn't found and URL was actually "unsized".
1980+
if ( $base_url !== $url ) {
1981+
$scaled_url = Utils::make_scaled_url( $base_url );
1982+
if ( isset( $known_lookup[ $scaled_url ] ) ) {
1983+
continue; // Scaled version is known, skip this variation.
1984+
}
1985+
}
1986+
1987+
// This URL is genuinely unknown.
1988+
$truly_unknown[] = $url;
1989+
}
1990+
1991+
/**
1992+
* Filter the list of truly unknown URLs after filtering out image size variations.
1993+
*
1994+
* @hook cloudinary_filter_unknown_urls
1995+
* @since 3.2.12
1996+
*
1997+
* @param array $truly_unknown The filtered list of unknown URLs.
1998+
* @param array $urls The original list of all URLs.
1999+
* @param array $known_keys The list of known URL keys.
2000+
*
2001+
* @return array The filtered list of unknown URLs.
2002+
*/
2003+
return apply_filters( 'cloudinary_filter_unknown_urls', $truly_unknown, $urls, $known_keys );
19372004
}
19382005

19392006
/**

0 commit comments

Comments
 (0)