Skip to content

Commit b52246e

Browse files
authored
Merge pull request #1159 from cloudinary/bugfix/wpml-lazy-loading
Fix WPML integration for Cloudinary media inserted prior to WPML activation
2 parents e0e1a9f + bc33e6e commit b52246e

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

php/class-delivery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ protected function standardize_tag( $tag_element ) {
13981398
'resource_type' => $resource_type,
13991399
);
14001400

1401-
$tag_element['atts']['data-public-id'] = $this->plugin->get_component( 'connect' )->api->get_public_id( $tag_element['id'], $args );
1401+
$tag_element['atts']['data-public-id'] = $this->plugin->get_component( 'connect' )->api->get_public_id( $tag_element['id'], $args, $public_id );
14021402

14031403
return $tag_element;
14041404
}

php/class-utils.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,15 +1088,7 @@ public static function query_relations( $public_ids, $urls = array() ) {
10881088
*/
10891089
$media_context_query = apply_filters( 'cloudinary_media_context_query', 'media_context = %s' );
10901090

1091-
/**
1092-
* Filter the media context things.
1093-
*
1094-
* @hook cloudinary_media_context_things
1095-
* @since 3.2.0
1096-
* @param $media_context_things {array} The default media context things.
1097-
* @return {array}
1098-
*/
1099-
$media_context_things = apply_filters( 'cloudinary_media_context_things', array( 'default' ) );
1091+
$media_context_things = self::get_media_context_things();
11001092

11011093
// Query for urls in chunks.
11021094
if ( ! empty( $urls ) ) {
@@ -1261,6 +1253,28 @@ public static function get_media_context( $attachment_id = null ) {
12611253
return sanitize_key( $context );
12621254
}
12631255

1256+
/**
1257+
* Get the media context things.
1258+
*
1259+
* @param array $media_context_things The media context things to query for. Defaults to array( 'default' ).
1260+
*
1261+
* @return array
1262+
*/
1263+
public static function get_media_context_things( $media_context_things = array( 'default' ) ) {
1264+
1265+
/**
1266+
* Filter the media context things.
1267+
*
1268+
* @hook cloudinary_media_context_things
1269+
* @since 3.2.0
1270+
* @param $media_context_things {array} The default media context things.
1271+
* @return {array}
1272+
*/
1273+
$media_context_things = apply_filters( 'cloudinary_media_context_things', $media_context_things );
1274+
1275+
return $media_context_things;
1276+
}
1277+
12641278
/**
12651279
* Get the home URL.
12661280
*

php/relate/class-relationship.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,28 @@ public function get_data() {
7979
return $cache_data;
8080
}
8181
global $wpdb;
82-
$table_name = Utils::get_relationship_table();
82+
$table_name = Utils::get_relationship_table();
83+
$default_contexts = Utils::get_media_context_things();
84+
85+
// If the context is in the default contexts, we want to query for all of them.
86+
// This ensures that a media uploaded with a previous default context will still be found, even if the default context has changed since it was uploaded.
87+
$contexts = in_array( $this->context, $default_contexts, true ) ? $default_contexts : array( $this->context );
88+
89+
// Create the context query placeholders by filling an array with the correct number of %s placeholders and then imploding it into a string.
90+
$context_query = implode( ', ', array_fill( 0, count( $contexts ), '%s' ) );
8391

84-
$sql = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE post_id = %d AND media_context = %s", $this->post_id, $this->context ); // phpcs:ignore WordPress.DB
92+
// Prepare arguments for the SQL query.
93+
$query_args = array_merge(
94+
array( $this->post_id ),
95+
$contexts,
96+
array( $this->context )
97+
);
98+
99+
// phpcs:ignore WordPress.DB
100+
$sql = $wpdb->prepare(
101+
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query}) ORDER BY FIELD(`media_context`, %s) DESC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
102+
$query_args
103+
);
85104
$data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB
86105

87106
self::set_cache( $this->post_id, $this->context, $data );

0 commit comments

Comments
 (0)