@@ -1031,16 +1031,19 @@ public static function attachment_url_to_postid( $url ) {
10311031 * @return array
10321032 */
10331033 public static function query_relations ( $ public_ids , $ urls = array () ) {
1034- $ chunk_size = 25 ;
1035- $ results = array ();
1036- $ tablename = self ::get_relationship_table ();
1034+ global $ wpdb ;
1035+
1036+ $ wheres = array ();
1037+ $ searched_things = array ();
10371038
10381039 /**
10391040 * Filter the media context query.
10401041 *
10411042 * @hook cloudinary_media_context_query
10421043 * @since 3.2.0
1044+ *
10431045 * @param $media_context_query {string} The default media context query.
1046+ *
10441047 * @return {string}
10451048 */
10461049 $ media_context_query = apply_filters ( 'cloudinary_media_context_query ' , 'media_context = %s ' );
@@ -1050,60 +1053,43 @@ public static function query_relations( $public_ids, $urls = array() ) {
10501053 *
10511054 * @hook cloudinary_media_context_things
10521055 * @since 3.2.0
1056+ *
10531057 * @param $media_context_things {array} The default media context things.
1058+ *
10541059 * @return {array}
10551060 */
10561061 $ media_context_things = apply_filters ( 'cloudinary_media_context_things ' , array ( 'default ' ) );
10571062
1058- // Query for urls in chunks.
10591063 if ( ! empty ( $ urls ) ) {
1060- $ results = array_merge ( $ results , self ::run_chunked_query ( 'url_hash ' , $ urls , $ chunk_size , $ tablename , $ media_context_query , $ media_context_things ) );
1064+ // Do the URLS.
1065+ $ list = implode ( ', ' , array_fill ( 0 , count ( $ urls ), '%s ' ) );
1066+ $ where = "(url_hash IN( {$ list } ) AND {$ media_context_query } ) " ;
1067+ $ searched_things = array_merge ( $ searched_things , array_map ( 'md5 ' , $ urls ), $ media_context_things );
1068+ $ wheres [] = $ where ;
10611069 }
1062- // Query for public_ids in chunks.
10631070 if ( ! empty ( $ public_ids ) ) {
1064- $ results = array_merge ( $ results , self ::run_chunked_query ( 'public_hash ' , $ public_ids , $ chunk_size , $ tablename , $ media_context_query , $ media_context_things ) );
1071+ // Do the public_ids.
1072+ $ list = implode ( ', ' , array_fill ( 0 , count ( $ public_ids ), '%s ' ) );
1073+ $ where = "(public_hash IN( {$ list } ) AND {$ media_context_query } ) " ;
1074+ $ searched_things = array_merge ( $ searched_things , array_map ( 'md5 ' , $ public_ids ), $ media_context_things );
1075+ $ wheres [] = $ where ;
10651076 }
10661077
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 );
1078+ $ results = array ();
1079+
1080+ if ( ! empty ( array_filter ( $ wheres ) ) ) {
1081+ $ tablename = self ::get_relationship_table ();
1082+ $ sql = "SELECT * from {$ tablename } WHERE " . implode ( ' OR ' , $ wheres );
1083+ $ prepared = $ wpdb ->prepare ( $ sql , $ searched_things ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1084+ $ cache_key = md5 ( $ prepared );
1085+ $ results = wp_cache_get ( $ cache_key , 'cld_delivery ' );
1086+ if ( empty ( $ results ) ) {
1087+ $ results = $ wpdb ->get_results ( $ prepared , ARRAY_A );// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery
1088+ wp_cache_add ( $ cache_key , $ results , 'cld_delivery ' );
11031089 }
11041090 }
11051091
1106- return $ all_results ;
1092+ return $ results ;
11071093 }
11081094
11091095 /**
0 commit comments