Skip to content

Commit 55d69aa

Browse files
authored
Merge pull request #965 from cloudinary/hofix/3.1.9-build-6
Update the link for the Cloudinary Assets on the admin bar language switcher
2 parents 290f7fc + 43e235e commit 55d69aa

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.9-build-5
1+
3.1.9-build-6

php/integrations/class-wpml.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Cloudinary\Integrations;
99

1010
use Cloudinary\Cron;
11+
use Cloudinary\Relate\Relationship;
1112
use Cloudinary\Utils;
1213
use WPML\Auryn\InjectionException;
1314
use WPML\FP\Obj;
@@ -65,6 +66,7 @@ public function register_hooks() {
6566
add_filter( 'cloudinary_home_url', array( $this, 'home_url' ) );
6667
add_action( 'cloudinary_edit_asset_permalink', array( $this, 'add_locale' ) );
6768
add_filter( 'cloudinary_contextualized_post_id', array( $this, 'contextualized_post_id' ) );
69+
add_filter( 'wpml_admin_language_switcher_items', array( $this, 'language_switcher_items' ) );
6870
}
6971

7072
/**
@@ -217,6 +219,41 @@ public function contextualized_post_id( $post_id ) {
217219
return apply_filters( 'wpml_object_id', $post_id, 'attachment' );
218220
}
219221

222+
/**
223+
* Update the link for the Cloudinary Assets item on the admin bar language switcher.
224+
*
225+
* @param array $languages_links The language switcher items.
226+
*
227+
* @return array
228+
*/
229+
public function language_switcher_items( $languages_links ) {
230+
foreach ( $languages_links as $language => &$link ) {
231+
$args = array();
232+
$query_args = wp_parse_url( $link['url'], PHP_URL_QUERY );
233+
parse_str( $query_args, $args );
234+
235+
// Check if we are in the context of editing an asset.
236+
if (
237+
empty( $args['page'] )
238+
|| 'cloudinary' !== $args['page']
239+
|| empty( $args['section'] )
240+
|| 'edit-asset' !== $args['section']
241+
|| empty( $args['asset'] )
242+
) {
243+
break;
244+
}
245+
246+
$relationship = new Relationship( $args['asset'] );
247+
$contextual_relationship = $relationship->get_contextualized_relationship( $language );
248+
249+
if ( ! empty( $contextual_relationship ) ) {
250+
$link['url'] = add_query_arg( 'asset', $contextual_relationship->post_id, $link['url'] );
251+
}
252+
}
253+
254+
return $languages_links;
255+
}
256+
220257
/**
221258
* Register the cron action at a late stage to ensure that WPML is loaded.
222259
*
@@ -289,8 +326,8 @@ protected function get_unynced() {
289326
)
290327
LIMIT %d";
291328

292-
$query = $wpdb->prepare( $sql, self::LIMIT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
293-
$results = array_map( 'intval', $wpdb->get_col( $query ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
329+
$query = $wpdb->prepare( $sql, self::LIMIT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
330+
$results = array_map( 'intval', $wpdb->get_col( $query ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
294331

295332
return $results;
296333
}

php/relate/class-relationship.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,29 @@ public static function get_ids_by_public_id( $public_id ) {
250250

251251
return array_map( 'intval', $ids );
252252
}
253+
254+
/**
255+
* Get the relationship in a different context.
256+
*
257+
* @param string $context The context.
258+
*
259+
* @return Relationship|null
260+
*/
261+
public function get_contextualized_relationship( $context ) {
262+
$relationship = null;
263+
if ( ! empty( $this->context ) && $this->context !== $context ) {
264+
global $wpdb;
265+
$table_name = Utils::get_relationship_table();
266+
267+
$sql = $wpdb->prepare( "SELECT post_id FROM {$table_name} WHERE url_hash = %s AND media_context = %s", $this->url_hash, $context ); // phpcs:ignore WordPress.DB
268+
$post_id = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB
269+
270+
if ( $post_id ) {
271+
$relationship = self::get_relationship( $post_id );
272+
$relationship->context = $context;
273+
}
274+
}
275+
276+
return $relationship;
277+
}
253278
}

0 commit comments

Comments
 (0)