From b01ab677fa25676d82fbfd5a91a69739f608bf51 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 16 Sep 2020 13:02:40 +0200 Subject: [PATCH 1/8] Merge pull request #203 from cloudinary/fix/custom-posttype-featured Ensure rest post types have custom fields support --- .../php/media/class-filter.php | 20 ++++++++++++++++++- 1 file changed, 19 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..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 @@ -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, 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; + } 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'] ); + } + } $response->set_data( $data ); } @@ -716,7 +729,12 @@ 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 ) { + // Add filter only to rest supported types. + add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 ); + } }, $types ); From 2bbe7c3b4b411c46f27b25e433fed21329270d89 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 16 Sep 2020 14:20:10 +0100 Subject: [PATCH 2/8] Merge pull request #205 from cloudinary/hotfix/storage-link Update link to storage setting --- .../php/sync/class-storage.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index f2bb63527..0d69186f4 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -103,7 +103,10 @@ public function maybe_disable_connect( $field, $slug ) { $field['description'] = sprintf( // translators: Placeholders are tags. __( 'You can’t currently change your environment variable as your storage setting is set to "Cloudinary only". Update your %1$s storage settings %2$s and sync your assets to WordPress storage to enable this setting.', 'cloudinary' ), - '', + sprintf( + '', + add_query_arg( 'page', 'cld_sync_media', admin_url( 'admin.php' ) ) + ), '' ); $field['disabled'] = true; From ef8c3dba76ffb2ba8a202f381bc52820d691c311 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 16 Sep 2020 13:04:56 +0200 Subject: [PATCH 3/8] prepare version --- .../cloudinary.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/cloudinary.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/cloudinary.php index feb8cf906..af7d1a8a7 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/cloudinary.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/cloudinary.php @@ -3,7 +3,7 @@ * Plugin Name: Cloudinary * Plugin URI: https://cloudinary.com/documentation/wordpress_integration * Description: With the Cloudinary plugin, you can upload and manage your media assets in the cloud, then deliver them to your users through a fast content delivery network, improving your website’s loading speed and overall user experience. Apply multiple transformations and take advantage of a full digital asset management solution without leaving WordPress. - * Version: 2.2.0 + * Version: 2.2.1 * Author: Cloudinary Ltd., XWP * Author URI: https://cloudinary.com/ * License: GPLv2+ From 4427e712b964bf66a06f4195381b2e19ee441852 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 18 Sep 2020 07:44:05 +0200 Subject: [PATCH 4/8] 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 5/8] 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 6/8] 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 7/8] 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; From 23d30e063ba2aceb2e587f1087968c0a93a12fec Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 23 Sep 2020 00:40:13 +0100 Subject: [PATCH 8/8] Typecast variable --- .../php/class-media.php | 4 ++-- 1 file changed, 2 insertions(+), 2 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 79deba678..84e49ac7f 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 @@ -1726,7 +1726,7 @@ public function get_upload_options( $attachment_id ) { */ public function maybe_overwrite_featured_image( $attachment_id ) { $overwrite = false; - if ( $this->doing_featured_image && $this->doing_featured_image === $attachment_id ) { + if ( $this->doing_featured_image && $this->doing_featured_image === (int) $attachment_id ) { $overwrite = (bool) $this->get_post_meta( get_the_ID(), Global_Transformations::META_FEATURED_IMAGE_KEY, true ); } @@ -1740,7 +1740,7 @@ public function maybe_overwrite_featured_image( $attachment_id ) { * @param int $attachment_id The thumbnail ID. */ public function set_doing_featured( $post_id, $attachment_id ) { - $this->doing_featured_image = $attachment_id; + $this->doing_featured_image = (int) $attachment_id; add_action( 'end_fetch_post_thumbnail_html', function () { $this->doing_featured_image = false; } );