Skip to content

Commit b01ab67

Browse files
David CramerMarco Pereirinha
authored andcommitted
Merge pull request #203 from cloudinary/fix/custom-posttype-featured
Ensure rest post types have custom fields support
1 parent 2f95c30 commit b01ab67

File tree

1 file changed

+19
-1
lines changed
  • cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ public function pre_filter_rest_content( $response, $post, $request ) {
548548
$content = $data['content']['raw'];
549549
$data['content']['raw'] = $this->filter_out_local( $content );
550550

551+
// Handle meta if missing due to custom-fields not being supported.
552+
if ( ! isset( $data['meta'] ) ) {
553+
$data['meta'] = $request->get_param( 'meta' );
554+
if ( null === $data['meta'] ) {
555+
// If null, meta param doesn't exist, so it's not a save edit, but a load edit.
556+
$disable = get_post_meta( $post->ID, Global_Transformations::META_FEATURED_IMAGE_KEY, true );
557+
// Add the value to the data meta.
558+
$data['meta'][ Global_Transformations::META_FEATURED_IMAGE_KEY ] = $disable;
559+
} else {
560+
// 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'] );
562+
}
563+
}
551564
$response->set_data( $data );
552565
}
553566

@@ -716,7 +729,12 @@ public function setup_hooks() {
716729
$filter = $this;
717730
array_map(
718731
function ( $type ) use ( $filter ) {
719-
add_filter( 'rest_prepare_' . $type, array( $filter, 'pre_filter_rest_content' ), 10, 3 );
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+
}
720738
},
721739
$types
722740
);

0 commit comments

Comments
 (0)