@@ -288,6 +288,7 @@ public static function get_table_sql() {
288288 public_id varchar(1000) DEFAULT NULL,
289289 parent_path varchar(1000) DEFAULT NULL,
290290 sized_url varchar(1000) DEFAULT NULL,
291+ media_context varchar(12) DEFAULT 'default',
291292 width int(11) DEFAULT NULL,
292293 height int(11) DEFAULT NULL,
293294 format varchar(12) DEFAULT NULL,
@@ -299,7 +300,7 @@ public static function get_table_sql() {
299300 url_hash varchar(45) DEFAULT NULL,
300301 parent_hash varchar(45) DEFAULT NULL,
301302 PRIMARY KEY (id),
302- UNIQUE KEY url_hash (url_hash),
303+ UNIQUE KEY media (url_hash, media_context ),
303304 KEY post_id (post_id),
304305 KEY parent_hash (parent_hash),
305306 KEY public_hash (public_hash),
@@ -360,17 +361,27 @@ protected static function upgrade_install() {
360361 * @return array
361362 */
362363 protected static function get_upgrade_sequence () {
363- $ sequence = array ();
364+ $ upgrade_sequence = array ();
364365 $ sequences = array (
365- '3.0.0 ' => array ( 'Cloudinary\Utils ' , 'upgrade_3_0_1 ' ),
366+ '3.0.0 ' => array (
367+ 'range ' => array ( '3.0.0 ' ),
368+ 'method ' => array ( 'Cloudinary\Utils ' , 'upgrade_3_0_1 ' ),
369+ ),
370+ '3.1.9 ' => array (
371+ 'range ' => array ( '3.0.1 ' , '3.1.9 ' ),
372+ 'method ' => array ( 'Cloudinary\Utils ' , 'upgrade_3_1_9 ' ),
373+ ),
374+
366375 );
367- $ upgrade_versions = array_keys ( $ sequences );
368376 $ previous_version = get_option ( Sync::META_KEYS ['db_version ' ], '3.0.0 ' );
369377 $ current_version = get_plugin_instance ()->version ;
370- if ( version_compare ( $ current_version , $ previous_version , '> ' ) ) {
371- $ index = array_search ( $ previous_version , $ upgrade_versions , true );
372- if ( false !== $ index ) {
373- $ sequence = array_slice ( $ sequences , $ index );
378+ foreach ( $ sequences as $ sequence ) {
379+ if (
380+ version_compare ( $ current_version , $ previous_version , '> ' )
381+ && version_compare ( $ previous_version , reset ( $ sequence ['range ' ] ), '>= ' )
382+ && version_compare ( $ previous_version , end ( $ sequence ['range ' ] ), '< ' )
383+ ) {
384+ $ upgrade_sequence [] = $ sequence ['method ' ];
374385 }
375386 }
376387
@@ -380,11 +391,11 @@ protected static function get_upgrade_sequence() {
380391 * @hook cloudinary_upgrade_sequence
381392 * @since 3.0.1
382393 *
383- * @param $sequence {array} The default sequence.
394+ * @param $upgrade_sequence {array} The default sequence.
384395 *
385396 * @return {array}
386397 */
387- return apply_filters ( 'cloudinary_upgrade_sequence ' , $ sequence );
398+ return apply_filters ( 'cloudinary_upgrade_sequence ' , $ upgrade_sequence );
388399 }
389400
390401 /**
@@ -417,6 +428,24 @@ public static function upgrade_3_0_1() {
417428 update_option ( Sync::META_KEYS ['db_version ' ], get_plugin_instance ()->version );
418429 }
419430
431+ /**
432+ * Upgrade DB from v3.0.1 to v3.1.9.
433+ */
434+ public static function upgrade_3_1_9 () {
435+ global $ wpdb ;
436+ $ tablename = self ::get_relationship_table ();
437+
438+ // Add new columns.
439+ $ wpdb ->query ( "ALTER TABLE {$ tablename } ADD COLUMN `media_context` VARCHAR(12) DEFAULT 'default' AFTER `sized_url` " ); // phpcs:ignore WordPress.DB
440+
441+ // Update indexes.
442+ $ wpdb ->query ( "ALTER TABLE {$ tablename } DROP INDEX url_hash " ); // phpcs:ignore WordPress.DB
443+ $ wpdb ->query ( "ALTER TABLE {$ tablename } ADD UNIQUE INDEX media (url_hash, media_context) " ); // phpcs:ignore WordPress.DB
444+
445+ // Set DB Version.
446+ update_option ( Sync::META_KEYS ['db_version ' ], get_plugin_instance ()->version );
447+ }
448+
420449 /**
421450 * Gets the URL for opening a Support Request.
422451 *
@@ -970,25 +999,30 @@ public static function attachment_url_to_postid( $url ) {
970999 public static function query_relations ( $ public_ids , $ urls = array () ) {
9711000 global $ wpdb ;
9721001
973- $ wheres = array ();
1002+ $ media_context = self ::get_media_context ();
1003+ $ wheres = array ();
1004+ $ searched_things = array ();
9741005 if ( ! empty ( $ urls ) ) {
9751006 // Do the URLS.
976- $ list = implode ( ', ' , array_fill ( 0 , count ( $ urls ), '%s ' ) );
977- $ wheres [] = "url_hash IN( {$ list } ) " ;
1007+ $ list = implode ( ', ' , array_fill ( 0 , count ( $ urls ), '%s ' ) );
1008+ $ where = "(url_hash IN( {$ list } ) AND media_context = %s) " ;
1009+ $ searched_things = array_merge ( $ searched_things , array_map ( 'md5 ' , $ urls ), array ( $ media_context ) );
1010+ $ wheres [] = $ where ;
9781011 }
9791012 if ( ! empty ( $ public_ids ) ) {
9801013 // Do the public_ids.
981- $ list = implode ( ', ' , array_fill ( 0 , count ( $ public_ids ), '%s ' ) );
982- $ wheres [] = "public_hash IN( {$ list } ) " ;
983- $ urls = array_merge ( $ urls , $ public_ids );
1014+ $ list = implode ( ', ' , array_fill ( 0 , count ( $ public_ids ), '%s ' ) );
1015+ $ where = "(public_hash IN( {$ list } ) AND media_context = %s) " ;
1016+ $ searched_things = array_merge ( $ searched_things , array_map ( 'md5 ' , $ public_ids ), array ( $ media_context ) );
1017+ $ wheres [] = $ where ;
9841018 }
9851019
9861020 $ results = array ();
9871021
988- if ( ! empty ( array_filter ( $ urls ) ) ) {
1022+ if ( ! empty ( array_filter ( $ wheres ) ) ) {
9891023 $ tablename = self ::get_relationship_table ();
9901024 $ sql = "SELECT * from {$ tablename } WHERE " . implode ( ' OR ' , $ wheres );
991- $ prepared = $ wpdb ->prepare ( $ sql , array_map ( ' md5 ' , $ urls ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1025+ $ prepared = $ wpdb ->prepare ( $ sql , $ searched_things ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9921026 $ cache_key = md5 ( $ prepared );
9931027 $ results = wp_cache_get ( $ cache_key , 'cld_delivery ' );
9941028 if ( empty ( $ results ) ) {
@@ -1083,4 +1117,32 @@ public static function descaled_url( $url ) {
10831117
10841118 return $ url ;
10851119 }
1120+
1121+ /**
1122+ * Get the media context.
1123+ *
1124+ * @param int|null $attachment_id The attachment ID.
1125+ *
1126+ * @return string
1127+ */
1128+ public static function get_media_context ( $ attachment_id = null ) {
1129+ /**
1130+ * Filter the media context.
1131+ *
1132+ * This filter allows you to set a media context for the media for cases where the same asset is used in
1133+ * different use cases, such as in a multilingual context.
1134+ *
1135+ * @hook cloudinary_media_context
1136+ * @since 3.1.9
1137+ * @default {'default'}
1138+ *
1139+ * @param $media_context {string} The media context.
1140+ * @param $attachment_id {int|null} The attachment ID.
1141+ *
1142+ * @return {string}
1143+ */
1144+ $ context = apply_filters ( 'cloudinary_media_context ' , 'default ' , $ attachment_id );
1145+
1146+ return sanitize_key ( $ context );
1147+ }
10861148}
0 commit comments