From 4427e712b964bf66a06f4195381b2e19ee441852 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 18 Sep 2020 07:44:05 +0200 Subject: [PATCH 1/4] move rest filters to init --- .../php/class-media.php | 2 +- .../php/media/class-filter.php | 39 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 7cf4a4dde..372cbc80e 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -1812,7 +1812,7 @@ public function setup() { * @return array */ public function match_file_name_with_cloudinary_source( $image_meta, $attachment_id ) { - if ( $this->has_public_id( $attachment_id ) ) { + if ( is_array( $image_meta ) && isset( $image_meta['file'] ) && $this->has_public_id( $attachment_id ) ) { $cld_file = 'v' . $this->get_cloudinary_version( $attachment_id ) . '/' . $this->get_cloudinary_id( $attachment_id ); if ( false === strpos( $image_meta['file'], $cld_file ) ) { $image_meta['file'] = $cld_file; diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index 93b39205e..c3b2d75ac 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -705,6 +705,28 @@ public function filter_image_block_pre_render( $block, $source_block ) { return $block; } + + /** + * Add filters for Rest API handling. + */ + public function init_rest_filters() { + // Gutenberg compatibility. + add_filter( 'rest_prepare_attachment', array( $this, 'filter_attachment_for_rest' ) ); + $types = get_post_types_by_support( 'editor' ); + $filter = $this; + array_map( + function ( $type ) use ( $filter ) { + $post_type = get_post_type_object( $type ); + // Check if this is a rest supported type. + if ( true === $post_type->show_in_rest ) { + // Add filter only to rest supported types. + add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); + } + }, + $types + ); + } + /** * Setup hooks for the filters. */ @@ -723,21 +745,8 @@ public function setup_hooks() { // Filter video codes. add_filter( 'media_send_to_editor', array( $this, 'filter_video_embeds' ), 10, 3 ); - // Gutenberg compatibility. - add_filter( 'rest_prepare_attachment', array( $this, 'filter_attachment_for_rest' ) ); - $types = get_post_types_by_support( 'editor' ); - $filter = $this; - array_map( - function ( $type ) use ( $filter ) { - $post_type = get_post_type_object( $type ); - // Check if this is a rest supported type. - if ( true === $post_type->show_in_rest ) { - // Add filter only to rest supported types. - add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); - } - }, - $types - ); + // Enable Rest filters. + add_action( 'rest_api_init', array( $this, 'init_rest_filters' ) ); // Remove editors to prevent users from manually editing images in WP. add_filter( 'wp_image_editors', array( $this, 'disable_editors_maybe' ) ); From b8f7af1954aa5ac8b7da5499a44ac7abf14e986d Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 18 Sep 2020 12:02:50 +0200 Subject: [PATCH 2/4] address PR issues --- .../php/class-media.php | 2 +- .../php/media/class-filter.php | 22 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 372cbc80e..f12dc2294 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -1812,7 +1812,7 @@ public function setup() { * @return array */ public function match_file_name_with_cloudinary_source( $image_meta, $attachment_id ) { - if ( is_array( $image_meta ) && isset( $image_meta['file'] ) && $this->has_public_id( $attachment_id ) ) { + if ( is_array( $image_meta ) && ! empty( $image_meta['file'] ) && $this->has_public_id( $attachment_id ) ) { $cld_file = 'v' . $this->get_cloudinary_version( $attachment_id ) . '/' . $this->get_cloudinary_id( $attachment_id ); if ( false === strpos( $image_meta['file'], $cld_file ) ) { $image_meta['file'] = $cld_file; diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index c3b2d75ac..dc9560a4a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -712,19 +712,15 @@ public function filter_image_block_pre_render( $block, $source_block ) { public function init_rest_filters() { // Gutenberg compatibility. add_filter( 'rest_prepare_attachment', array( $this, 'filter_attachment_for_rest' ) ); - $types = get_post_types_by_support( 'editor' ); - $filter = $this; - array_map( - function ( $type ) use ( $filter ) { - $post_type = get_post_type_object( $type ); - // Check if this is a rest supported type. - if ( true === $post_type->show_in_rest ) { - // Add filter only to rest supported types. - add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); - } - }, - $types - ); + $types = get_post_types_by_support( 'editor' ); + foreach ( $types as $type ) { + $post_type = get_post_type_object( $type ); + // Check if this is a rest supported type. + if ( true === $post_type->show_in_rest ) { + // Add filter only to rest supported types. + add_filter( 'rest_prepare_' . $type, array( $this, 'pre_filter_rest_content' ), 10, 3 ); + } + } } /** From 8509a101c2ed347af3f6597bb2ee5d6699fd4748 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Thu, 17 Sep 2020 16:41:04 +0100 Subject: [PATCH 3/4] Fix a typo on stored meta --- .../php/media/class-filter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index dc9560a4a..580a87644 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -558,7 +558,7 @@ public function pre_filter_rest_content( $response, $post, $request ) { $data['meta'][ Global_Transformations::META_FEATURED_IMAGE_KEY ] = $disable; } else { // If the param was found, its a save edit, to update the meta data. - update_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, (bool) $data['meta'] ); + update_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, (bool) $data['meta'][ Global_Transformations::META_FEATURED_IMAGE_KEY ] ); } } $response->set_data( $data ); From bda58f2982e2e726d96beb017a37e910ced86315 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 18 Sep 2020 12:09:47 +0200 Subject: [PATCH 4/4] remove is_Array --- .../php/class-media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index f12dc2294..79deba678 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -1812,7 +1812,7 @@ public function setup() { * @return array */ public function match_file_name_with_cloudinary_source( $image_meta, $attachment_id ) { - if ( is_array( $image_meta ) && ! empty( $image_meta['file'] ) && $this->has_public_id( $attachment_id ) ) { + if ( ! empty( $image_meta['file'] ) && $this->has_public_id( $attachment_id ) ) { $cld_file = 'v' . $this->get_cloudinary_version( $attachment_id ) . '/' . $this->get_cloudinary_id( $attachment_id ); if ( false === strpos( $image_meta['file'], $cld_file ) ) { $image_meta['file'] = $cld_file;