From 06a67c80804c7e32edc9573fb55bd45b0a6f54ae Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 16 Sep 2020 06:58:57 +0200 Subject: [PATCH 1/4] Ensure rest post types have custom fields support --- .../php/media/class-filter.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 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 dcd9ee20a..d614dc9d6 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 @@ -716,7 +716,16 @@ public function setup_hooks() { $filter = $this; array_map( function ( $type ) use ( $filter ) { - add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); + $post_type = get_post_type_object( $type ); + // Check if this is a rest supported type. + if ( true === $post_type->show_in_rest ) { + if ( post_type_supports( $type, 'thumbnail' ) && ! post_type_supports( $type, 'custom-fields' ) ) { + // Add custom fields support to allow working with the meta data in Gutenberg. + add_post_type_support( $type, 'custom-fields' ); + } + + add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); + } }, $types ); From 33bcc03a9bdbfaad5ba3b523c2448a1e71db4274 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 16 Sep 2020 12:19:52 +0200 Subject: [PATCH 2/4] handle meta if custom-fields are not supported. --- .../php/media/class-filter.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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 d614dc9d6..45372940c 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 @@ -548,6 +548,19 @@ public function pre_filter_rest_content( $response, $post, $request ) { $content = $data['content']['raw']; $data['content']['raw'] = $this->filter_out_local( $content ); + // Handle meta if missing due to custom-fields not being supported. + if ( ! isset( $data['meta'] ) ) { + $data['meta'] = $request->get_param( 'meta' ); + if ( null === $data['meta'] ) { + // If null, param doesn't exists, so it's not a save edit, but a load edit. + $disable = get_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, true ); + // Add the value to the data meta. + $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, $data['meta'] ); + } + } $response->set_data( $data ); } @@ -719,11 +732,7 @@ 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 ) { - if ( post_type_supports( $type, 'thumbnail' ) && ! post_type_supports( $type, 'custom-fields' ) ) { - // Add custom fields support to allow working with the meta data in Gutenberg. - add_post_type_support( $type, 'custom-fields' ); - } - + // Add filter only to rest supported types. add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); } }, From ce01c140cb7421513ff9ba9b12f1ccf56745b0c4 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 16 Sep 2020 12:22:06 +0200 Subject: [PATCH 3/4] clean up --- .../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 45372940c..743dce59f 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 @@ -552,7 +552,7 @@ public function pre_filter_rest_content( $response, $post, $request ) { if ( ! isset( $data['meta'] ) ) { $data['meta'] = $request->get_param( 'meta' ); if ( null === $data['meta'] ) { - // If null, param doesn't exists, so it's not a save edit, but a load edit. + // If null, meta param doesn't exist, so it's not a save edit, but a load edit. $disable = get_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, true ); // Add the value to the data meta. $data['meta'][ Global_Transformations::META_FEATURED_IMAGE_KEY ] = $disable; From b96385ed448450b2d795fffe8ed0292d7f92575f Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 16 Sep 2020 12:41:48 +0200 Subject: [PATCH 4/4] cast as bool --- .../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 743dce59f..93b39205e 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, $data['meta'] ); + update_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, (bool) $data['meta'] ); } } $response->set_data( $data );