Skip to content

Commit e885345

Browse files
committed
Improve unknown URLs search
1 parent 76e1580 commit e885345

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

php/class-delivery.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ public function find_attachment_size_urls() {
883883
$dirs = wp_get_upload_dir();
884884
$baseurl = Utils::clean_url( $dirs['baseurl'] );
885885
$search = array();
886+
886887
foreach ( $this->unknown as $url ) {
887888
$url = ltrim( str_replace( $baseurl, '', $url ), '/' );
888889
$search[] = $url;
@@ -900,6 +901,7 @@ public function find_attachment_size_urls() {
900901
$key = md5( $sql );
901902
$cached = wp_cache_get( $key );
902903
$auto_sync = $this->sync->is_auto_sync_enabled();
904+
903905
if ( false === $cached ) {
904906
$cached = array();
905907
$results = $wpdb->get_results( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
@@ -917,12 +919,14 @@ public function find_attachment_size_urls() {
917919
* @return {int}
918920
*/
919921
$post_id = apply_filters( 'cloudinary_contextualized_post_id', $result->post_id );
922+
920923
if ( ! $this->is_deliverable( $post_id ) ) {
921924
continue;
922925
}
923926
// If we are here, it means that an attachment in the media library doesn't have a delivery for the url.
924927
// Reset the signature for delivery and add to sync, to update it.
925928
$this->create_delivery( $post_id );
929+
926930
if ( true === $auto_sync ) {
927931
$this->sync->add_to_sync( $post_id );
928932
}
@@ -1920,7 +1924,70 @@ public function prepare_delivery( $content ) {
19201924
$this->set_usability( $result, $auto_sync );
19211925
}
19221926
// Set unknowns.
1923-
$this->unknown = array_diff( $urls, array_keys( $this->known ) );
1927+
$this->unknown = $this->filter_unknown_urls( $urls );
1928+
}
1929+
1930+
/**
1931+
* Filter URLs to determine which are truly unknown, considering image size variations.
1932+
*
1933+
* This method treats image size variations (e.g., example-300x224.png) as "known"
1934+
* if their base image (e.g., example.png) exists in the known URLs, while still
1935+
* catching genuinely unknown URLs.
1936+
*
1937+
* @param array $urls All URLs found in content.
1938+
*
1939+
* @return array Array of genuinely unknown URLs.
1940+
*/
1941+
protected function filter_unknown_urls( $urls ) {
1942+
$known_keys = array_keys( $this->known );
1943+
1944+
if ( empty( $known_keys ) ) {
1945+
return $urls;
1946+
}
1947+
1948+
$known_lookup = array_flip( $known_keys );
1949+
$potential_unknown = array_diff( $urls, $known_keys );
1950+
1951+
if ( empty( $potential_unknown ) ) {
1952+
return array();
1953+
}
1954+
1955+
$truly_unknown = array();
1956+
1957+
foreach ( $potential_unknown as $url ) {
1958+
// Check if this might be a sized variation of a known image.
1959+
$base_url = $this->maybe_unsize_url( $url );
1960+
1961+
// If base image is known, skip this variation.
1962+
if ( isset( $known_lookup[ $base_url ] ) ) {
1963+
continue;
1964+
}
1965+
1966+
// Check scaled version if base wasn't found and URL was actually "unsized".
1967+
if ( $base_url !== $url ) {
1968+
$scaled_url = Utils::make_scaled_url( $base_url );
1969+
if ( isset( $known_lookup[ $scaled_url ] ) ) {
1970+
continue; // Scaled version is known, skip this variation.
1971+
}
1972+
}
1973+
1974+
// This URL is genuinely unknown.
1975+
$truly_unknown[] = $url;
1976+
}
1977+
1978+
/**
1979+
* Filter the list of truly unknown URLs after filtering out image size variations.
1980+
*
1981+
* @hook cloudinary_filter_unknown_urls
1982+
* @since 3.2.12
1983+
*
1984+
* @param array $truly_unknown The filtered list of unknown URLs.
1985+
* @param array $urls The original list of all URLs.
1986+
* @param array $known_keys The list of known URL keys.
1987+
*
1988+
* @return array The filtered list of unknown URLs.
1989+
*/
1990+
return apply_filters( 'cloudinary_filter_unknown_urls', $truly_unknown, $urls, $known_keys );
19241991
}
19251992

19261993
/**

0 commit comments

Comments
 (0)