diff --git a/php/class-delivery.php b/php/class-delivery.php index ea597a1a..0a599243 100644 --- a/php/class-delivery.php +++ b/php/class-delivery.php @@ -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; } diff --git a/php/class-utils.php b/php/class-utils.php index 6ace40c8..210b4c2f 100644 --- a/php/class-utils.php +++ b/php/class-utils.php @@ -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 ) ) { @@ -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. * diff --git a/php/relate/class-relationship.php b/php/relate/class-relationship.php index 10b3aadb..c363c1ad 100644 --- a/php/relate/class-relationship.php +++ b/php/relate/class-relationship.php @@ -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 + $this->post_id, + ...$contexts + ); $data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB self::set_cache( $this->post_id, $this->context, $data );