Skip to content

Commit 4427e71

Browse files
committed
move rest filters to init
1 parent ef8c3db commit 4427e71

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
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 ( is_array( $image_meta ) && isset( $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: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,28 @@ 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+
$filter = $this;
717+
array_map(
718+
function ( $type ) use ( $filter ) {
719+
$post_type = get_post_type_object( $type );
720+
// Check if this is a rest supported type.
721+
if ( true === $post_type->show_in_rest ) {
722+
// Add filter only to rest supported types.
723+
add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 );
724+
}
725+
},
726+
$types
727+
);
728+
}
729+
708730
/**
709731
* Setup hooks for the filters.
710732
*/
@@ -723,21 +745,8 @@ public function setup_hooks() {
723745
// Filter video codes.
724746
add_filter( 'media_send_to_editor', array( $this, 'filter_video_embeds' ), 10, 3 );
725747

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-
);
748+
// Enable Rest filters.
749+
add_action( 'rest_api_init', array( $this, 'init_rest_filters' ) );
741750

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

0 commit comments

Comments
 (0)