@@ -620,6 +620,9 @@ public static function is_admin() {
620620 * @return array
621621 */
622622 public static function extract_urls ( $ content ) {
623+ // Remove inline SVG data URIs, as they can cause parsing issues when extracting URLs.
624+ $ content = self ::strip_inline_svg_data_uris ( $ content );
625+
623626 preg_match_all (
624627 "#([ \"']?)( "
625628 . '(?:[\w-]+:)?//? '
@@ -642,6 +645,25 @@ public static function extract_urls( $content ) {
642645 return array_values ( $ post_links );
643646 }
644647
648+ /**
649+ * Strip inline SVG data URIs from content.
650+ *
651+ * @param string $content The content to process.
652+ *
653+ * @return string The content with SVG data URIs removed.
654+ */
655+ public static function strip_inline_svg_data_uris ( $ content ) {
656+ // Pattern to match the data URI structure: data:image/svg+xml;base64,<base64-encoded-data>.
657+ $ svg_data_uri_pattern = '/data:image\/svg\+xml;base64,[a-zA-Z0-9\/\+\=]+/i ' ;
658+
659+ // Remove all occurrences of SVG data URIs from the content.
660+ $ cleaned_content = preg_replace ( $ svg_data_uri_pattern , '' , $ content );
661+
662+ // In case an error occurred, we return the original content to avoid data loss.
663+ return is_null ( $ cleaned_content ) ? $ content : $ cleaned_content ;
664+ }
665+
666+
645667 /**
646668 * Is saving metadata.
647669 *
@@ -1009,19 +1031,16 @@ public static function attachment_url_to_postid( $url ) {
10091031 * @return array
10101032 */
10111033 public static function query_relations ( $ public_ids , $ urls = array () ) {
1012- global $ wpdb ;
1013-
1014- $ wheres = array ();
1015- $ searched_things = array ();
1034+ $ chunk_size = 25 ;
1035+ $ results = array ();
1036+ $ tablename = self ::get_relationship_table ();
10161037
10171038 /**
10181039 * Filter the media context query.
10191040 *
10201041 * @hook cloudinary_media_context_query
10211042 * @since 3.2.0
1022- *
10231043 * @param $media_context_query {string} The default media context query.
1024- *
10251044 * @return {string}
10261045 */
10271046 $ media_context_query = apply_filters ( 'cloudinary_media_context_query ' , 'media_context = %s ' );
@@ -1031,43 +1050,60 @@ public static function query_relations( $public_ids, $urls = array() ) {
10311050 *
10321051 * @hook cloudinary_media_context_things
10331052 * @since 3.2.0
1034- *
10351053 * @param $media_context_things {array} The default media context things.
1036- *
10371054 * @return {array}
10381055 */
10391056 $ media_context_things = apply_filters ( 'cloudinary_media_context_things ' , array ( 'default ' ) );
10401057
1058+ // Query for urls in chunks.
10411059 if ( ! empty ( $ urls ) ) {
1042- // Do the URLS.
1043- $ list = implode ( ', ' , array_fill ( 0 , count ( $ urls ), '%s ' ) );
1044- $ where = "(url_hash IN( {$ list } ) AND {$ media_context_query } ) " ;
1045- $ searched_things = array_merge ( $ searched_things , array_map ( 'md5 ' , $ urls ), $ media_context_things );
1046- $ wheres [] = $ where ;
1060+ $ results = array_merge ( $ results , self ::run_chunked_query ( 'url_hash ' , $ urls , $ chunk_size , $ tablename , $ media_context_query , $ media_context_things ) );
10471061 }
1062+ // Query for public_ids in chunks.
10481063 if ( ! empty ( $ public_ids ) ) {
1049- // Do the public_ids.
1050- $ list = implode ( ', ' , array_fill ( 0 , count ( $ public_ids ), '%s ' ) );
1051- $ where = "(public_hash IN( {$ list } ) AND {$ media_context_query } ) " ;
1052- $ searched_things = array_merge ( $ searched_things , array_map ( 'md5 ' , $ public_ids ), $ media_context_things );
1053- $ wheres [] = $ where ;
1064+ $ results = array_merge ( $ results , self ::run_chunked_query ( 'public_hash ' , $ public_ids , $ chunk_size , $ tablename , $ media_context_query , $ media_context_things ) );
10541065 }
10551066
1056- $ results = array ();
1057-
1058- if ( ! empty ( array_filter ( $ wheres ) ) ) {
1059- $ tablename = self ::get_relationship_table ();
1060- $ sql = "SELECT * from {$ tablename } WHERE " . implode ( ' OR ' , $ wheres );
1061- $ prepared = $ wpdb ->prepare ( $ sql , $ searched_things ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1062- $ cache_key = md5 ( $ prepared );
1063- $ results = wp_cache_get ( $ cache_key , 'cld_delivery ' );
1064- if ( empty ( $ results ) ) {
1065- $ results = $ wpdb ->get_results ( $ prepared , ARRAY_A );// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery
1066- wp_cache_add ( $ cache_key , $ results , 'cld_delivery ' );
1067+ return $ results ;
1068+ }
1069+ /**
1070+ * Run a chunked query and merge results.
1071+ *
1072+ * @param string $field The DB field to query (url_hash or public_hash).
1073+ * @param array $items The items to query.
1074+ * @param int $chunk_size Number of items per chunk.
1075+ * @param string $tablename The table name.
1076+ * @param string $media_context_query The media context SQL.
1077+ * @param array $media_context_things The media context values.
1078+ * @return array
1079+ */
1080+ protected static function run_chunked_query ( $ field , $ items , $ chunk_size , $ tablename , $ media_context_query , $ media_context_things ) {
1081+ global $ wpdb ;
1082+
1083+ $ all_results = array ();
1084+ $ chunks = array_chunk ( $ items , $ chunk_size );
1085+
1086+ foreach ( $ chunks as $ chunk ) {
1087+ $ list = implode ( ', ' , array_fill ( 0 , count ( $ chunk ), '%s ' ) );
1088+ $ where = "( {$ field } IN( {$ list } ) AND {$ media_context_query } ) " ;
1089+ $ searched_things = array_merge ( array_map ( 'md5 ' , $ chunk ), $ media_context_things );
1090+ $ sql = "SELECT * from {$ tablename } WHERE {$ where }" ;
1091+ $ prepared = $ wpdb ->prepare ( $ sql , $ searched_things ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1092+ $ cache_key = md5 ( $ prepared );
1093+ $ chunk_results = wp_cache_get ( $ cache_key , 'cld_delivery ' );
1094+
1095+ if ( empty ( $ chunk_results ) ) {
1096+ $ chunk_results = $ wpdb ->get_results ( $ prepared , ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery
1097+
1098+ wp_cache_add ( $ cache_key , $ chunk_results , 'cld_delivery ' );
1099+ }
1100+
1101+ if ( ! empty ( $ chunk_results ) ) {
1102+ $ all_results = array_merge ( $ all_results , $ chunk_results );
10671103 }
10681104 }
10691105
1070- return $ results ;
1106+ return $ all_results ;
10711107 }
10721108
10731109 /**
0 commit comments