Skip to content

Commit d8304a1

Browse files
Fallback to default media context within relationship query
1 parent d66c27c commit d8304a1

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,22 @@ 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 );
8388

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
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' ) );
91+
92+
// phpcs:ignore WordPress.DB
93+
$sql = $wpdb->prepare(
94+
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
95+
$this->post_id,
96+
...$contexts
97+
);
8598
$data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB
8699

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

0 commit comments

Comments
 (0)