Skip to content

Commit 8c4137e

Browse files
authored
Merge pull request #459 from cloudinary/fix/taxonomy-transformations-featured-image
Fix taxonomy transformations
2 parents f7fd55c + 1b31708 commit 8c4137e

File tree

3 files changed

+102
-32
lines changed

3 files changed

+102
-32
lines changed

php/class-delivery.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class Delivery implements Setup {
4040
*/
4141
protected $filter;
4242

43+
/**
44+
* Hold the Post ID.
45+
*
46+
* @var null|int
47+
*/
48+
protected $current_post_id = null;
49+
4350
/**
4451
* The meta data cache key to store URLS.
4552
*
@@ -65,6 +72,8 @@ public function __construct( Plugin $plugin ) {
6572
protected function setup_hooks() {
6673
add_filter( 'cloudinary_filter_out_local', '__return_false' );
6774
add_action( 'update_option_cloudinary_media_display', array( $this, 'clear_cache' ) );
75+
add_filter( 'cloudinary_post_id_taxonomy', array( $this, 'get_current_post_id' ) );
76+
add_filter( 'the_content', array( $this, 'add_post_id' ) );
6877
}
6978

7079
/**
@@ -74,6 +83,36 @@ public function clear_cache() {
7483
delete_post_meta_by_key( self::META_CACHE_KEY );
7584
}
7685

86+
/**
87+
* Add the Post ID to images and videos.
88+
*
89+
* @param string $content The content.
90+
*
91+
* @return string
92+
*/
93+
public function add_post_id( $content ) {
94+
return str_replace(
95+
array(
96+
'wp-image-',
97+
'wp-video-',
98+
),
99+
array(
100+
'wp-post-' . get_the_ID() . ' wp-image-',
101+
'wp-post-' . get_the_ID() . ' wp-video-',
102+
),
103+
$content
104+
);
105+
}
106+
107+
/**
108+
* Get the current post ID.
109+
*
110+
* @return int|null
111+
*/
112+
public function get_current_post_id() {
113+
return $this->current_post_id;
114+
}
115+
77116
/**
78117
* Setup component.
79118
*/
@@ -98,6 +137,8 @@ public function process_featured_image( $html, $post_id, $attachment_id ) {
98137
// Get tag element.
99138
$tag_element = $this->parse_element( $html );
100139
$tag_element['atts']['class'][] = 'wp-image-' . $attachment_id;
140+
$tag_element['atts']['class'][] = 'wp-post-' . $post_id;
141+
101142
if ( true === (bool) $this->media->get_post_meta( $post_id, Global_Transformations::META_FEATURED_IMAGE_KEY, true ) ) {
102143
$tag_element['atts']['class'][] = 'cld-overwrite';
103144
}
@@ -207,13 +248,16 @@ public function convert_tags( $content ) {
207248
$replacements = array();
208249
$attachment_ids = array();
209250
foreach ( $tags as $element ) {
210-
$attachment_id = $this->filter->get_id_from_tag( $element );
251+
$attachment_id = $this->filter->get_id_from_tag( $element );
252+
$this->current_post_id = $this->filter->get_id_from_tag( $element, 'wp-post-' );
253+
211254
if ( empty( $attachment_id ) ) {
212255
continue;
213256
}
214257
// Register replacement.
215258
$replacements[ $element ] = $this->rebuild_tag( $element, $attachment_id );
216259
$attachment_ids[] = $attachment_id;
260+
$this->current_post_id = null;
217261
}
218262

219263
// Create other image sizes for ID's found.

php/media/class-filter.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
namespace Cloudinary\Media;
99

1010
use Cloudinary\Connect\Api;
11+
use Cloudinary\Media;
1112
use Cloudinary\Utils;
13+
use WP_Post;
14+
use WP_REST_Request;
15+
use WP_REST_Response;
1216

1317
/**
1418
* Class Filter.
@@ -22,16 +26,16 @@ class Filter {
2226
*
2327
* @since 0.1
2428
*
25-
* @var \Cloudinary\Media Instance of the plugin.
29+
* @var Media Instance of the plugin.
2630
*/
2731
private $media;
2832

2933
/**
3034
* Filter constructor.
3135
*
32-
* @param \Cloudinary\Media $media The plugin.
36+
* @param Media $media The plugin.
3337
*/
34-
public function __construct( \Cloudinary\Media $media ) {
38+
public function __construct( Media $media ) {
3539
$this->media = $media;
3640
$this->setup_hooks();
3741
}
@@ -87,13 +91,13 @@ public function get_video_shortcodes( $html ) {
8791
* Get the attachment ID from the media tag.
8892
*
8993
* @param string $asset The media tag.
90-
*
91-
* @return int|false
94+
* @param string $type The type.
95+
* @return int|null
9296
*/
93-
public function get_id_from_tag( $asset ) {
94-
$attachment_id = false;
97+
public function get_id_from_tag( $asset, $type = 'wp-image-|wp-video-' ) {
98+
$attachment_id = null;
9599
// Get attachment id from class name.
96-
if ( preg_match( '#class=["|\']?[^"\']*(wp-image-|wp-video-)([\d]+)[^"\']*["|\']?#i', $asset, $found ) ) {
100+
if ( preg_match( '#class=["|\']?[^"\']*(' . $type . ')([\d]+)[^"\']*["|\']?#i', $asset, $found ) ) {
97101
$attachment_id = intval( $found[2] );
98102
}
99103

@@ -442,9 +446,9 @@ public function filter_attachment_for_js( $attachment ) {
442446
/**
443447
* Return a Cloudinary URL for an attachment used in a REST REQUEST.
444448
*
445-
* @param \WP_REST_Response $attachment The attachment array to be used in JS.
449+
* @param WP_REST_Response $attachment The attachment array to be used in JS.
446450
*
447-
* @return \WP_REST_Response
451+
* @return WP_REST_Response
448452
* @uses filter:rest_prepare_attachment
449453
*/
450454
public function filter_attachment_for_rest( $attachment ) {
@@ -546,13 +550,13 @@ public function filter_video_embeds( $html, $id, $attachment ) {
546550
}
547551

548552
/**
549-
* Filter out local urls in an 'edit' context rest request ( i.e for Gutenburg ).
553+
* Filter out local urls in an 'edit' context rest request ( i.e for Gutenberg ).
550554
*
551-
* @param \WP_REST_Response $response The post data array to save.
552-
* @param \WP_Post $post The current post.
553-
* @param \WP_REST_Request $request The request object.
555+
* @param WP_REST_Response $response The post data array to save.
556+
* @param WP_Post $post The current post.
557+
* @param WP_REST_Request $request The request object.
554558
*
555-
* @return \WP_REST_Response
559+
* @return WP_REST_Response
556560
*/
557561
public function pre_filter_rest_content( $response, $post, $request ) {
558562
$context = $request->get_param( 'context' );

php/media/class-global-transformations.php

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

1010
use Cloudinary\Settings\Setting;
11+
use WP_Post;
1112

1213
/**
1314
* Class Global Transformations.
@@ -241,25 +242,23 @@ public function get_transformations( $type ) {
241242
*
242243
* @param string $type The type to get.
243244
*
244-
* @return array
245+
* @return string
245246
*/
246247
public function get_taxonomy_transformations( $type ) {
247248
$return_transformations = '';
248-
if ( in_the_loop() ) {
249-
$post = get_post();
250-
if ( ! empty( $post ) ) {
251-
$transformations = array();
252-
$terms = $this->get_terms( $post->ID );
253-
if ( ! empty( $terms ) ) {
254-
foreach ( $terms as $item ) {
255-
$transformation = $this->get_term_transformations( $item['term']->term_id, $type );
256-
if ( ! empty( $transformation[ $type . '_freeform' ] ) ) {
257-
$transformations[] = trim( $transformation[ $type . '_freeform' ] );
258-
}
249+
$post = $this->get_current_post();
250+
if ( $post ) {
251+
$transformations = array();
252+
$terms = $this->get_terms( $post->ID );
253+
if ( ! empty( $terms ) ) {
254+
foreach ( $terms as $item ) {
255+
$transformation = $this->get_term_transformations( $item['term']->term_id, $type );
256+
if ( ! empty( $transformation[ $type . '_freeform' ] ) ) {
257+
$transformations[] = trim( $transformation[ $type . '_freeform' ] );
259258
}
260-
// Join the freeform.
261-
$return_transformations = implode( '/', (array) $transformations );
262259
}
260+
// Join the freeform.
261+
$return_transformations = implode( '/', (array) $transformations );
263262
}
264263
}
265264

@@ -273,8 +272,8 @@ public function get_taxonomy_transformations( $type ) {
273272
*/
274273
public function is_taxonomy_overwrite() {
275274
$apply_type = false;
276-
if ( in_the_loop() ) {
277-
$post = get_post();
275+
$post = $this->get_current_post();
276+
if ( $post ) {
278277
$apply_type = get_post_meta( $post->ID, self::META_APPLY_KEY . '_terms', true );
279278
}
280279

@@ -563,6 +562,29 @@ public function save_overwrite_transformations_featured_image( $post_id ) {
563562
}
564563
}
565564

565+
/**
566+
* Get the current post.
567+
*
568+
* @return WP_Post|null
569+
*/
570+
protected function get_current_post() {
571+
/**
572+
* Filter the post ID.
573+
*
574+
* @hook cloudinary_post_id
575+
* @default null
576+
*
577+
* @return {WP_Post|null}
578+
*/
579+
$post_id = apply_filters( 'cloudinary_post_id', null );
580+
581+
if ( is_null( $post_id ) ) {
582+
return null;
583+
}
584+
585+
return get_post( $post_id );
586+
}
587+
566588
/**
567589
* Setup hooks for the filters.
568590
*/

0 commit comments

Comments
 (0)