Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion php/class-delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ protected function standardize_tag( $tag_element ) {
'resource_type' => $resource_type,
);

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

return $tag_element;
}
Expand Down
32 changes: 23 additions & 9 deletions php/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,15 +1088,7 @@ public static function query_relations( $public_ids, $urls = array() ) {
*/
$media_context_query = apply_filters( 'cloudinary_media_context_query', 'media_context = %s' );

/**
* Filter the media context things.
*
* @hook cloudinary_media_context_things
* @since 3.2.0
* @param $media_context_things {array} The default media context things.
* @return {array}
*/
$media_context_things = apply_filters( 'cloudinary_media_context_things', array( 'default' ) );
$media_context_things = self::get_media_context_things();

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

/**
* Get the media context things.
*
* @param array $media_context_things The media context things to query for. Defaults to array( 'default' ).
*
* @return array
*/
public static function get_media_context_things( $media_context_things = array( 'default' ) ) {

/**
* Filter the media context things.
*
* @hook cloudinary_media_context_things
* @since 3.2.0
* @param $media_context_things {array} The default media context things.
* @return {array}
*/
$media_context_things = apply_filters( 'cloudinary_media_context_things', $media_context_things );

return $media_context_things;
}

/**
* Get the home URL.
*
Expand Down
17 changes: 15 additions & 2 deletions php/relate/class-relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,22 @@ public function get_data() {
return $cache_data;
}
global $wpdb;
$table_name = Utils::get_relationship_table();
$table_name = Utils::get_relationship_table();
$default_contexts = Utils::get_media_context_things();

// If the context is in the default contexts, we want to query for all of them.
// 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.
$contexts = in_array( $this->context, $default_contexts, true ) ? $default_contexts : array( $this->context );

$sql = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE post_id = %d AND media_context = %s", $this->post_id, $this->context ); // phpcs:ignore WordPress.DB
// Create the context query placeholders by filling an array with the correct number of %s placeholders and then imploding it into a string.
$context_query = implode( ', ', array_fill( 0, count( $contexts ), '%s' ) );

// phpcs:ignore WordPress.DB
$sql = $wpdb->prepare(
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 suggestion: The query now searches for multiple contexts (e.g. en and default), but get_row() returns only one row. Without an ORDER BY, which row you get is up to MySQL. If a post has rows for both contexts, you want the current one (en) first and default only as a fallback.

Current code:

$sql  = $wpdb->prepare(
    "SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query})",
    $this->post_id,
    ...$contexts
);
$data = $wpdb->get_row( $sql, ARRAY_A );

Suggested fix:

$sql  = $wpdb->prepare(
    "SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query}) ORDER BY FIELD(`media_context`, %s) DESC LIMIT 1",
    $this->post_id,
    ...$contexts,
    $this->context
);
$data = $wpdb->get_row( $sql, ARRAY_A );

FIELD(media_context, %s) scores 1 for an exact match on the current context and 0 for everything else. DESC puts the exact match first.

$this->post_id,
...$contexts
);
$data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB

self::set_cache( $this->post_id, $this->context, $data );
Expand Down
Loading