|
8 | 8 | namespace Cloudinary\Integrations; |
9 | 9 |
|
10 | 10 | use Cloudinary\Cron; |
| 11 | +use Cloudinary\Relate\Relationship; |
11 | 12 | use Cloudinary\Utils; |
12 | 13 | use WPML\Auryn\InjectionException; |
13 | 14 | use WPML\FP\Obj; |
@@ -65,6 +66,7 @@ public function register_hooks() { |
65 | 66 | add_filter( 'cloudinary_home_url', array( $this, 'home_url' ) ); |
66 | 67 | add_action( 'cloudinary_edit_asset_permalink', array( $this, 'add_locale' ) ); |
67 | 68 | add_filter( 'cloudinary_contextualized_post_id', array( $this, 'contextualized_post_id' ) ); |
| 69 | + add_filter( 'wpml_admin_language_switcher_items', array( $this, 'language_switcher_items' ) ); |
68 | 70 | } |
69 | 71 |
|
70 | 72 | /** |
@@ -217,6 +219,41 @@ public function contextualized_post_id( $post_id ) { |
217 | 219 | return apply_filters( 'wpml_object_id', $post_id, 'attachment' ); |
218 | 220 | } |
219 | 221 |
|
| 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 | + |
220 | 257 | /** |
221 | 258 | * Register the cron action at a late stage to ensure that WPML is loaded. |
222 | 259 | * |
@@ -289,8 +326,8 @@ protected function get_unynced() { |
289 | 326 | ) |
290 | 327 | LIMIT %d"; |
291 | 328 |
|
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 |
294 | 331 |
|
295 | 332 | return $results; |
296 | 333 | } |
|
0 commit comments