Skip to content
Merged
27 changes: 27 additions & 0 deletions lib/experimental/block-comments.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
<?php

/**
* Adds support for block comments to the built-in post types.
*
* @return void
*/
function gutenberg_block_comment_add_post_type_support() {
$post_types = array( 'post', 'page' );

foreach ( $post_types as $post_type ) {
if ( ! post_type_supports( $post_type, 'editor' ) ) {
continue;
}

$supports = get_all_post_type_supports( $post_type );
$editor_supports = array( 'block-comments' => true );

// `add_post_type_support()` doesn't merge support sub-properties, so we explicitly merge it here.
if ( is_array( $supports['editor'] ) && isset( $supports['editor'][0] ) && is_array( $supports['editor'][0] ) ) {
$editor_supports = array_merge( $editor_supports, $supports['editor'][0] );
}

add_post_type_support( $post_type, 'editor', $editor_supports );
}
}
add_action( 'init', 'gutenberg_block_comment_add_post_type_support' );

/**
* Updates the comment type in the REST API.
*
Expand Down
30 changes: 30 additions & 0 deletions lib/experimental/class-gutenberg-rest-comment-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ public function create_item_permissions_check( $request ) {
);
}

if ( ! $this->check_post_type_supports_block_comments( $post->post_type ) ) {
return new WP_Error(
'rest_comment_block_comments_not_supported',
__( 'Sorry, this post type does not support block comments.', 'gutenberg' ),
array( 'status' => 403 )
);
}

return true;
}

/**
* Check if post type supports block comments.
*
* @param string $post_type Post type name.
* @return bool True if post type supports block comments, false otherwise.
*/
private function check_post_type_supports_block_comments( $post_type ) {
$supports = get_all_post_type_supports( $post_type );
if ( ! isset( $supports['editor'] ) ) {
return false;
}
if ( ! is_array( $supports['editor'] ) ) {
return false;
}
foreach ( $supports['editor'] as $item ) {
if ( is_array( $item ) && isset( $item['block-comments'] ) && true === $item['block-comments'] ) {
return true;
}
}
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export default function CollabSidebar() {
? resultComments.find( ( thread ) => thread.id === blockCommentId )
: null;

// If postId is not a valid number, do not render the comment sidebar.
// If postId is not a valid number, do not render the comment sidebar. 1Code has comments. Press enter to view.
if ( ! ( !! postId && typeof postId === 'number' ) ) {
return null;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/editor/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import PostSavedState from '../post-saved-state';
import PostViewLink from '../post-view-link';
import PreviewDropdown from '../preview-dropdown';
import ZoomOutToggle from '../zoom-out-toggle';
import PostTypeSupportCheck from '../post-type-support-check';
import { store as editorStore } from '../../store';
import {
TEMPLATE_PART_POST_TYPE,
Expand Down Expand Up @@ -196,9 +197,11 @@ function Header( {
/>
) }

{ isBlockCommentExperimentEnabled ? (
<CollabSidebar />
) : undefined }
{ isBlockCommentExperimentEnabled && (
<PostTypeSupportCheck supportKeys="editor.block-comments">
<CollabSidebar />
</PostTypeSupportCheck>
) }

{ customSaveButton }
<MoreMenu />
Expand Down
Loading