Skip to content

Commit 796e6be

Browse files
authored
Merge pull request #955 from cloudinary/feature/wpml-support
Feature WPML support
2 parents 731843c + 9fea6c8 commit 796e6be

File tree

9 files changed

+427
-68
lines changed

9 files changed

+427
-68
lines changed

php/class-assets.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,8 @@ public function get_asset( $post_id, $type = 'object' ) {
10201020
global $wpdb;
10211021

10221022
$wpdb->cld_table = Utils::get_relationship_table();
1023-
$prepare = $wpdb->prepare( "SELECT * FROM $wpdb->cld_table WHERE post_id = %d;", (int) $post_id );
1023+
$media_context = Utils::get_media_context( $post_id );
1024+
$prepare = $wpdb->prepare( "SELECT * FROM $wpdb->cld_table WHERE post_id = %d AND media_context = %s;", (int) $post_id, $media_context );
10241025
$result = $wpdb->get_row( $prepare, ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared
10251026

10261027
return $this->build_item( $result );

php/class-delivery.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
use Cloudinary\Connect\Api;
1212
use Cloudinary\Media\Filter;
1313
use Cloudinary\Media\Global_Transformations;
14-
use Cloudinary\Sync;
15-
use Cloudinary\String_Replace;
1614
use Cloudinary\UI\Component\HTML;
1715
use Cloudinary\Delivery\Bypass;
1816
use Cloudinary\Relate\Relationship;
19-
use WP_Post;
20-
use Cloudinary\Utils;
2117

2218
/**
2319
* Plugin Delivery class.
@@ -335,11 +331,12 @@ public function generate_signature( $attachment_id ) {
335331
if ( $relationship instanceof Relationship ) {
336332
$public_id = $relationship->public_id;
337333
}
334+
338335
$sizes = $this->get_sized( $attachment_id );
339336
$settings_signature = self::get_settings_signature();
340337
$relation_signature = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['relationship'], true );
341338

342-
return wp_json_encode( $sizes ) . $public_id . $sql . $settings_signature . $relation_signature;
339+
return wp_json_encode( $sizes ) . $public_id . $sql . $settings_signature . $relation_signature . Utils::get_media_context( $attachment_id );
343340
}
344341

345342
/**
@@ -544,9 +541,10 @@ public static function update_size_relations_public_id( $attachment_id, $public_
544541
$relationship = Relationship::get_relationship( $attachment_id );
545542

546543
if ( $relationship instanceof Relationship ) {
547-
$relationship->public_id = $public_id;
548-
$relationship->public_hash = md5( (string) $public_id );
549-
$relationship->signature = self::get_settings_signature();
544+
$relationship->public_id = $public_id;
545+
$relationship->public_hash = md5( (string) $public_id );
546+
$relationship->signature = self::get_settings_signature();
547+
$relationship->media_context = Utils::get_media_context( $attachment_id );
550548
$relationship->save();
551549
}
552550
}
@@ -624,6 +622,7 @@ public static function create_size_relation( $attachment_id, $sized_url, $size =
624622
'post_id' => $attachment_id,
625623
'parent_path' => $parent_path,
626624
'sized_url' => $sized_url,
625+
'media_context' => Utils::get_media_context( $attachment_id ),
627626
'width' => $width_height[0] ? $width_height[0] : 0,
628627
'height' => $width_height[1] ? $width_height[1] : 0,
629628
'format' => Utils::pathinfo( $sized_url, PATHINFO_EXTENSION ),
@@ -1013,6 +1012,9 @@ public function convert_tags( $content, $context = 'view' ) {
10131012
$base = $type . ':' . $url;
10141013
$public_id = ! is_admin() ? $relation['public_id'] . '.' . $relation['format'] : null;
10151014
$cloudinary_url = $this->media->cloudinary_url( $relation['post_id'], array(), $relation['transformations'], $public_id );
1015+
if ( empty( $cloudinary_url ) ) {
1016+
continue;
1017+
}
10161018
if ( ! empty( $relation['slashed'] ) && $relation['slashed'] ) {
10171019
$aliases[ $base . '?' ] = addcslashes( $cloudinary_url . '&', '/' );
10181020
$aliases[ $base ] = addcslashes( $cloudinary_url, '/' );

php/class-plugin.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cloudinary\Delivery\Lazy_Load;
1515
use Cloudinary\Delivery\Responsive_Breakpoints;
1616
use Cloudinary\Assets as CLD_Assets;
17+
use Cloudinary\Integrations\WPML;
1718
use Cloudinary\Media\Gallery;
1819
use Cloudinary\Sync\Storage;
1920
use Cloudinary\UI\State;
@@ -30,7 +31,7 @@ final class Plugin {
3031
*
3132
* @since 0.1
3233
*
33-
* @var Admin|CLD_Assets|Connect|Dashboard|Deactivation|Delivery|Extensions|Gallery|Lazy_Load|Media|Meta_Box|Relate|Report|Responsive_Breakpoints|REST_API|State|Storage|SVG|Sync|URL|null
34+
* @var Admin|CLD_Assets|Connect|Dashboard|Deactivation|Delivery|Extensions|Gallery|Lazy_Load|Media|Meta_Box|Relate|Report|Responsive_Breakpoints|REST_API|State|Storage|SVG|Sync|URL[]|WPML|null
3435
*/
3536
public $components;
3637
/**
@@ -89,29 +90,6 @@ final class Plugin {
8990
*/
9091
public $plugin_file;
9192

92-
/**
93-
* Directory in plugin containing autoloaded classes.
94-
*
95-
* @var string
96-
*/
97-
protected $autoload_class_dir = 'php';
98-
99-
/**
100-
* Autoload matches cache.
101-
*
102-
* @var array
103-
*/
104-
protected $autoload_matches_cache = array();
105-
106-
/**
107-
* Holds the list of hooks.
108-
*
109-
* @since 0.1
110-
*
111-
* @var array
112-
*/
113-
public $hooks;
114-
11593
/**
11694
* Holds the list of keys.
11795
*/
@@ -166,6 +144,7 @@ public function init() {
166144
$this->components['relate'] = new Relate( $this );
167145
$this->components['metabox'] = new Meta_Box( $this );
168146
$this->components['url'] = new URL( $this );
147+
$this->components['wpml'] = new WPML( $this );
169148
}
170149

171150
/**

php/class-report.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function get_attachment_data( $post_id = null ) {
212212
unset( $meta[ Sync::META_KEYS['cloudinary'] ], $meta[ Sync::META_KEYS['process_log'] ], $meta['_wp_attachment_metadata'] );
213213
array_walk(
214214
$meta,
215-
static function( &$row ) {
215+
static function ( &$row ) {
216216
$row = reset( $row );
217217
}
218218
);
@@ -226,9 +226,11 @@ static function( &$row ) {
226226
}
227227

228228
$wpdb->cld_table = Utils::get_relationship_table();
229+
$media_context = Utils::get_media_context( $post->ID );
229230
$prepare = $wpdb->prepare(
230-
"SELECT * FROM {$wpdb->cld_table} WHERE post_id = %d;",
231-
$post->ID
231+
"SELECT * FROM {$wpdb->cld_table} WHERE post_id = %d AND media_context = %s;",
232+
$post->ID,
233+
$media_context
232234
);
233235
$relationship = $wpdb->get_row( $prepare ); // phpcs:ignore WordPress.DB.PreparedSQL,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
234236

php/class-utils.php

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public static function get_table_sql() {
288288
public_id varchar(1000) DEFAULT NULL,
289289
parent_path varchar(1000) DEFAULT NULL,
290290
sized_url varchar(1000) DEFAULT NULL,
291+
media_context varchar(12) DEFAULT 'default',
291292
width int(11) DEFAULT NULL,
292293
height int(11) DEFAULT NULL,
293294
format varchar(12) DEFAULT NULL,
@@ -299,7 +300,7 @@ public static function get_table_sql() {
299300
url_hash varchar(45) DEFAULT NULL,
300301
parent_hash varchar(45) DEFAULT NULL,
301302
PRIMARY KEY (id),
302-
UNIQUE KEY url_hash (url_hash),
303+
UNIQUE KEY media (url_hash, media_context),
303304
KEY post_id (post_id),
304305
KEY parent_hash (parent_hash),
305306
KEY public_hash (public_hash),
@@ -360,17 +361,27 @@ protected static function upgrade_install() {
360361
* @return array
361362
*/
362363
protected static function get_upgrade_sequence() {
363-
$sequence = array();
364+
$upgrade_sequence = array();
364365
$sequences = array(
365-
'3.0.0' => array( 'Cloudinary\Utils', 'upgrade_3_0_1' ),
366+
'3.0.0' => array(
367+
'range' => array( '3.0.0' ),
368+
'method' => array( 'Cloudinary\Utils', 'upgrade_3_0_1' ),
369+
),
370+
'3.1.9' => array(
371+
'range' => array( '3.0.1', '3.1.9' ),
372+
'method' => array( 'Cloudinary\Utils', 'upgrade_3_1_9' ),
373+
),
374+
366375
);
367-
$upgrade_versions = array_keys( $sequences );
368376
$previous_version = get_option( Sync::META_KEYS['db_version'], '3.0.0' );
369377
$current_version = get_plugin_instance()->version;
370-
if ( version_compare( $current_version, $previous_version, '>' ) ) {
371-
$index = array_search( $previous_version, $upgrade_versions, true );
372-
if ( false !== $index ) {
373-
$sequence = array_slice( $sequences, $index );
378+
foreach ( $sequences as $sequence ) {
379+
if (
380+
version_compare( $current_version, $previous_version, '>' )
381+
&& version_compare( $previous_version, reset( $sequence['range'] ), '>=' )
382+
&& version_compare( $previous_version, end( $sequence['range'] ), '<' )
383+
) {
384+
$upgrade_sequence[] = $sequence['method'];
374385
}
375386
}
376387

@@ -380,11 +391,11 @@ protected static function get_upgrade_sequence() {
380391
* @hook cloudinary_upgrade_sequence
381392
* @since 3.0.1
382393
*
383-
* @param $sequence {array} The default sequence.
394+
* @param $upgrade_sequence {array} The default sequence.
384395
*
385396
* @return {array}
386397
*/
387-
return apply_filters( 'cloudinary_upgrade_sequence', $sequence );
398+
return apply_filters( 'cloudinary_upgrade_sequence', $upgrade_sequence );
388399
}
389400

390401
/**
@@ -417,6 +428,24 @@ public static function upgrade_3_0_1() {
417428
update_option( Sync::META_KEYS['db_version'], get_plugin_instance()->version );
418429
}
419430

431+
/**
432+
* Upgrade DB from v3.0.1 to v3.1.9.
433+
*/
434+
public static function upgrade_3_1_9() {
435+
global $wpdb;
436+
$tablename = self::get_relationship_table();
437+
438+
// Add new columns.
439+
$wpdb->query( "ALTER TABLE {$tablename} ADD COLUMN `media_context` VARCHAR(12) DEFAULT 'default' AFTER `sized_url`" ); // phpcs:ignore WordPress.DB
440+
441+
// Update indexes.
442+
$wpdb->query( "ALTER TABLE {$tablename} DROP INDEX url_hash" ); // phpcs:ignore WordPress.DB
443+
$wpdb->query( "ALTER TABLE {$tablename} ADD UNIQUE INDEX media (url_hash, media_context)" ); // phpcs:ignore WordPress.DB
444+
445+
// Set DB Version.
446+
update_option( Sync::META_KEYS['db_version'], get_plugin_instance()->version );
447+
}
448+
420449
/**
421450
* Gets the URL for opening a Support Request.
422451
*
@@ -970,25 +999,30 @@ public static function attachment_url_to_postid( $url ) {
970999
public static function query_relations( $public_ids, $urls = array() ) {
9711000
global $wpdb;
9721001

973-
$wheres = array();
1002+
$media_context = self::get_media_context();
1003+
$wheres = array();
1004+
$searched_things = array();
9741005
if ( ! empty( $urls ) ) {
9751006
// Do the URLS.
976-
$list = implode( ', ', array_fill( 0, count( $urls ), '%s' ) );
977-
$wheres[] = "url_hash IN( {$list} )";
1007+
$list = implode( ', ', array_fill( 0, count( $urls ), '%s' ) );
1008+
$where = "(url_hash IN( {$list} ) AND media_context = %s)";
1009+
$searched_things = array_merge( $searched_things, array_map( 'md5', $urls ), array( $media_context ) );
1010+
$wheres[] = $where;
9781011
}
9791012
if ( ! empty( $public_ids ) ) {
9801013
// Do the public_ids.
981-
$list = implode( ', ', array_fill( 0, count( $public_ids ), '%s' ) );
982-
$wheres[] = "public_hash IN( {$list} )";
983-
$urls = array_merge( $urls, $public_ids );
1014+
$list = implode( ', ', array_fill( 0, count( $public_ids ), '%s' ) );
1015+
$where = "(public_hash IN( {$list} ) AND media_context = %s)";
1016+
$searched_things = array_merge( $searched_things, array_map( 'md5', $public_ids ), array( $media_context ) );
1017+
$wheres[] = $where;
9841018
}
9851019

9861020
$results = array();
9871021

988-
if ( ! empty( array_filter( $urls ) ) ) {
1022+
if ( ! empty( array_filter( $wheres ) ) ) {
9891023
$tablename = self::get_relationship_table();
9901024
$sql = "SELECT * from {$tablename} WHERE " . implode( ' OR ', $wheres );
991-
$prepared = $wpdb->prepare( $sql, array_map( 'md5', $urls ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1025+
$prepared = $wpdb->prepare( $sql, $searched_things ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9921026
$cache_key = md5( $prepared );
9931027
$results = wp_cache_get( $cache_key, 'cld_delivery' );
9941028
if ( empty( $results ) ) {
@@ -1083,4 +1117,32 @@ public static function descaled_url( $url ) {
10831117

10841118
return $url;
10851119
}
1120+
1121+
/**
1122+
* Get the media context.
1123+
*
1124+
* @param int|null $attachment_id The attachment ID.
1125+
*
1126+
* @return string
1127+
*/
1128+
public static function get_media_context( $attachment_id = null ) {
1129+
/**
1130+
* Filter the media context.
1131+
*
1132+
* This filter allows you to set a media context for the media for cases where the same asset is used in
1133+
* different use cases, such as in a multilingual context.
1134+
*
1135+
* @hook cloudinary_media_context
1136+
* @since 3.1.9
1137+
* @default {'default'}
1138+
*
1139+
* @param $media_context {string} The media context.
1140+
* @param $attachment_id {int|null} The attachment ID.
1141+
*
1142+
* @return {string}
1143+
*/
1144+
$context = apply_filters( 'cloudinary_media_context', 'default', $attachment_id );
1145+
1146+
return sanitize_key( $context );
1147+
}
10861148
}

php/connect/class-api.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cloudinary\Utils;
1313
use Cloudinary\Plugin;
1414
use Cloudinary\Media;
15+
use function Cloudinary\get_plugin_instance;
1516

1617
/**
1718
* Class API.
@@ -527,7 +528,11 @@ public function upload( $attachment_id, $args, $headers = array(), $try_remote =
527528
$disable_https_fetch = true;
528529
}
529530

530-
if ( ! $this->media->is_uploadable_media( $attachment_id ) ) {
531+
if ( ! $this->media ) {
532+
$this->media = get_plugin_instance()->get_component( 'media' );
533+
}
534+
535+
if ( $this->media && ! $this->media->is_uploadable_media( $attachment_id ) ) {
531536
$disable_https_fetch = false; // Remote can upload via url.
532537
// translators: variable is thread name and queue size.
533538
$action_message = sprintf( __( 'Uploading remote url: %1$s.', 'cloudinary' ), $file_url );
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Integrations class for the Cloudinary plugin.
4+
*
5+
* @package Cloudinary
6+
*/
7+
8+
namespace Cloudinary\Integrations;
9+
10+
use Cloudinary\Plugin;
11+
12+
/**
13+
* Abstract class Integrations
14+
*/
15+
abstract class Integrations {
16+
/**
17+
* The plugin instance.
18+
*
19+
* @var Plugin
20+
*/
21+
protected $plugin;
22+
23+
/**
24+
* Integrations constructor.
25+
*
26+
* @param Plugin $plugin The plugin instance.
27+
*/
28+
public function __construct( $plugin ) {
29+
if ( ! $this->can_enable() ) {
30+
return;
31+
}
32+
33+
$this->plugin = $plugin;
34+
$this->register_hooks();
35+
}
36+
37+
/**
38+
* Check if the integration can be enabled.
39+
*
40+
* @return bool
41+
*/
42+
abstract public function can_enable();
43+
44+
/**
45+
* Register hooks for the integration.
46+
*/
47+
abstract public function register_hooks();
48+
}

0 commit comments

Comments
 (0)