Skip to content
Merged
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 .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.9-build-5
3.1.9-build-6
41 changes: 39 additions & 2 deletions php/integrations/class-wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Cloudinary\Integrations;

use Cloudinary\Cron;
use Cloudinary\Relate\Relationship;
use Cloudinary\Utils;
use WPML\Auryn\InjectionException;
use WPML\FP\Obj;
Expand Down Expand Up @@ -65,6 +66,7 @@ public function register_hooks() {
add_filter( 'cloudinary_home_url', array( $this, 'home_url' ) );
add_action( 'cloudinary_edit_asset_permalink', array( $this, 'add_locale' ) );
add_filter( 'cloudinary_contextualized_post_id', array( $this, 'contextualized_post_id' ) );
add_filter( 'wpml_admin_language_switcher_items', array( $this, 'language_switcher_items' ) );
}

/**
Expand Down Expand Up @@ -217,6 +219,41 @@ public function contextualized_post_id( $post_id ) {
return apply_filters( 'wpml_object_id', $post_id, 'attachment' );
}

/**
* Update the link for the Cloudinary Assets item on the admin bar language switcher.
*
* @param array $languages_links The language switcher items.
*
* @return array
*/
public function language_switcher_items( $languages_links ) {
foreach ( $languages_links as $language => &$link ) {
$args = array();
$query_args = wp_parse_url( $link['url'], PHP_URL_QUERY );
parse_str( $query_args, $args );

// Check if we are in the context of editing an asset.
if (
empty( $args['page'] )
|| 'cloudinary' !== $args['page']
|| empty( $args['section'] )
|| 'edit-asset' !== $args['section']
|| empty( $args['asset'] )
) {
break;
}

$relationship = new Relationship( $args['asset'] );
$contextual_relationship = $relationship->get_contextualized_relationship( $language );

if ( ! empty( $contextual_relationship ) ) {
$link['url'] = add_query_arg( 'asset', $contextual_relationship->post_id, $link['url'] );
}
}

return $languages_links;
}

/**
* Register the cron action at a late stage to ensure that WPML is loaded.
*
Expand Down Expand Up @@ -289,8 +326,8 @@ protected function get_unynced() {
)
LIMIT %d";

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

return $results;
}
Expand Down
25 changes: 25 additions & 0 deletions php/relate/class-relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,29 @@ public static function get_ids_by_public_id( $public_id ) {

return array_map( 'intval', $ids );
}

/**
* Get the relationship in a different context.
*
* @param string $context The context.
*
* @return Relationship|null
*/
public function get_contextualized_relationship( $context ) {
$relationship = null;
if ( ! empty( $this->context ) && $this->context !== $context ) {
global $wpdb;
$table_name = Utils::get_relationship_table();

$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
$post_id = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB

if ( $post_id ) {
$relationship = self::get_relationship( $post_id );
$relationship->context = $context;
}
}

return $relationship;
}
}