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
23 changes: 21 additions & 2 deletions php/relate/class-relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,28 @@ 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 );

// 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' ) );

$sql = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE post_id = %d AND media_context = %s", $this->post_id, $this->context ); // phpcs:ignore WordPress.DB
// Prepare arguments for the SQL query.
$query_args = array_merge(
array( $this->post_id ),
$contexts,
array( $this->context )
);

// phpcs:ignore WordPress.DB
$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", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$query_args
);
$data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB

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