Skip to content

Commit f0af113

Browse files
authored
Merge pull request #209 from cloudinary/hotfix/featured-image-import
move rest filters to init
2 parents ef8c3db + bda58f2 commit f0af113

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ public function setup() {
18121812
* @return array
18131813
*/
18141814
public function match_file_name_with_cloudinary_source( $image_meta, $attachment_id ) {
1815-
if ( $this->has_public_id( $attachment_id ) ) {
1815+
if ( ! empty( $image_meta['file'] ) && $this->has_public_id( $attachment_id ) ) {
18161816
$cld_file = 'v' . $this->get_cloudinary_version( $attachment_id ) . '/' . $this->get_cloudinary_id( $attachment_id );
18171817
if ( false === strpos( $image_meta['file'], $cld_file ) ) {
18181818
$image_meta['file'] = $cld_file;

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public function pre_filter_rest_content( $response, $post, $request ) {
558558
$data['meta'][ Global_Transformations::META_FEATURED_IMAGE_KEY ] = $disable;
559559
} else {
560560
// If the param was found, its a save edit, to update the meta data.
561-
update_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, (bool) $data['meta'] );
561+
update_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, (bool) $data['meta'][ Global_Transformations::META_FEATURED_IMAGE_KEY ] );
562562
}
563563
}
564564
$response->set_data( $data );
@@ -705,6 +705,24 @@ public function filter_image_block_pre_render( $block, $source_block ) {
705705
return $block;
706706
}
707707

708+
709+
/**
710+
* Add filters for Rest API handling.
711+
*/
712+
public function init_rest_filters() {
713+
// Gutenberg compatibility.
714+
add_filter( 'rest_prepare_attachment', array( $this, 'filter_attachment_for_rest' ) );
715+
$types = get_post_types_by_support( 'editor' );
716+
foreach ( $types as $type ) {
717+
$post_type = get_post_type_object( $type );
718+
// Check if this is a rest supported type.
719+
if ( true === $post_type->show_in_rest ) {
720+
// Add filter only to rest supported types.
721+
add_filter( 'rest_prepare_' . $type, array( $this, 'pre_filter_rest_content' ), 10, 3 );
722+
}
723+
}
724+
}
725+
708726
/**
709727
* Setup hooks for the filters.
710728
*/
@@ -723,21 +741,8 @@ public function setup_hooks() {
723741
// Filter video codes.
724742
add_filter( 'media_send_to_editor', array( $this, 'filter_video_embeds' ), 10, 3 );
725743

726-
// Gutenberg compatibility.
727-
add_filter( 'rest_prepare_attachment', array( $this, 'filter_attachment_for_rest' ) );
728-
$types = get_post_types_by_support( 'editor' );
729-
$filter = $this;
730-
array_map(
731-
function ( $type ) use ( $filter ) {
732-
$post_type = get_post_type_object( $type );
733-
// Check if this is a rest supported type.
734-
if ( true === $post_type->show_in_rest ) {
735-
// Add filter only to rest supported types.
736-
add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 );
737-
}
738-
},
739-
$types
740-
);
744+
// Enable Rest filters.
745+
add_action( 'rest_api_init', array( $this, 'init_rest_filters' ) );
741746

742747
// Remove editors to prevent users from manually editing images in WP.
743748
add_filter( 'wp_image_editors', array( $this, 'disable_editors_maybe' ) );

0 commit comments

Comments
 (0)