Skip to content

Commit f7bbc18

Browse files
authored
VideoPress: dont allow editing video data when doesn't belong to site (#30438)
* [not verified] request, set and expose videoBelongToSite data * [not verified] propagate videoBelongToSite between hooks * [not verified] propagate videoBelongToSite to the details panel * [not verified] disable title and description when doesn't belong * [not verified] set video belong true as default * [not verified] changelog * [not verified] check ownership with envelope mode * [not verified] handle normal and envelope mode * [not verified] change response body * [not verified] update hook to the new endpoint shape
1 parent 9ef05bf commit f7bbc18

File tree

9 files changed

+41
-6
lines changed

9 files changed

+41
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: added
3+
4+
VideoPress: dont allow editing video data when the video doesn't belong to the site

projects/packages/videopress/src/class-wpcom-rest-api-v2-endpoint-videopress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ public function videopress_video_belong_to_site( $request ) {
209209
}
210210

211211
if ( ! $found_guid ) {
212-
return rest_ensure_response( false );
212+
return rest_ensure_response( array( 'video-belong-to-site' => false ) );
213213
}
214214

215-
return rest_ensure_response( $found_guid === $video_guid );
215+
return rest_ensure_response( array( 'video-belong-to-site' => $found_guid === $video_guid ) );
216216
}
217217

218218
/**

projects/packages/videopress/src/client/block-editor/blocks/video/components/details-panel/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default function DetailsPanel( {
3939
setAttributes,
4040
isRequestingVideoData,
4141
updateError,
42+
videoBelongToSite,
4243
}: DetailsPanelProps ) {
4344
const { title, description } = attributes;
4445
const { hasIncompleteChapters } = useChaptersLiveParsing( description );
@@ -68,7 +69,7 @@ export default function DetailsPanel( {
6869
filename?.length ? `${ filename } video` : __( 'Video title', 'jetpack-videopress-pkg' )
6970
}
7071
onChange={ value => setAttributes( { title: value } ) }
71-
disabled={ isRequestingVideoData || !! updateError }
72+
disabled={ isRequestingVideoData || !! updateError || ! videoBelongToSite }
7273
/>
7374

7475
<TextareaControl
@@ -77,7 +78,7 @@ export default function DetailsPanel( {
7778
placeholder={ __( 'Video description', 'jetpack-videopress-pkg' ) }
7879
onChange={ value => setAttributes( { description: value } ) }
7980
rows={ descriptionControlRows }
80-
disabled={ isRequestingVideoData || !! updateError }
81+
disabled={ isRequestingVideoData || !! updateError || ! videoBelongToSite }
8182
help={ descriptionHelp }
8283
/>
8384

projects/packages/videopress/src/client/block-editor/blocks/video/edit.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export default function VideoPressEdit( {
158158
error: syncError,
159159
isOverwriteChapterAllowed,
160160
isGeneratingPoster,
161+
videoBelongToSite,
161162
} = useSyncMedia( attributes, setAttributes );
162163

163164
const { filename, private_enabled_for_site: privateEnabledForSite } = videoData;
@@ -523,6 +524,7 @@ export default function VideoPressEdit( {
523524
isAutoGeneratedChapter={ isOverwriteChapterAllowed }
524525
updateError={ syncError }
525526
isRequestingVideoData={ isRequestingVideoData }
527+
videoBelongToSite={ videoBelongToSite }
526528
{ ...{ attributes, setAttributes } }
527529
/>
528530

projects/packages/videopress/src/client/block-editor/blocks/video/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export type DetailsPanelProps = VideoControlProps & {
162162
isAutoGeneratedChapter: boolean;
163163
updateError: object | null;
164164
isRequestingVideoData: boolean;
165+
videoBelongToSite: boolean;
165166
};
166167

167168
export type VideoPreviewProps = {

projects/packages/videopress/src/client/block-editor/hooks/use-sync-media/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export function useSyncMedia(
152152
setAttributes: VideoBlockSetAttributesProps
153153
): UseSyncMedia {
154154
const { id, guid, isPrivate } = attributes;
155-
const { videoData, isRequestingVideoData } = useVideoData( {
155+
const { videoData, isRequestingVideoData, videoBelongToSite } = useVideoData( {
156156
id,
157157
guid,
158158
skipRatingControl: true,
@@ -425,6 +425,7 @@ export function useSyncMedia(
425425
forceInitialState: updateInitialState,
426426
videoData,
427427
isRequestingVideoData,
428+
videoBelongToSite,
428429
error,
429430
isOverwriteChapterAllowed,
430431
isGeneratingPoster,

projects/packages/videopress/src/client/block-editor/hooks/use-sync-media/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type UseSyncMedia = {
99
error: object | null;
1010
isOverwriteChapterAllowed: boolean;
1111
isGeneratingPoster: boolean;
12+
videoBelongToSite: boolean;
1213
};
1314

1415
export type ArrangeTracksAttributesProps = [ Array< TrackProps >, boolean ];

projects/packages/videopress/src/client/block-editor/hooks/use-video-data/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* External dependencies
33
*/
4+
import apiFetch from '@wordpress/api-fetch';
45
import { useEffect, useState, Platform } from '@wordpress/element';
56
import { decodeEntities } from '@wordpress/html-entities';
67
import debugFactory from 'debug';
@@ -34,6 +35,7 @@ export default function useVideoData( {
3435
}: UseVideoDataArgumentsProps ): UseVideoDataProps {
3536
const [ videoData, setVideoData ] = useState< VideoDataProps >( {} );
3637
const [ isRequestingVideoData, setIsRequestingVideoData ] = useState( false );
38+
const [ videoBelongToSite, setVideoBelongToSite ] = useState( true );
3739

3840
useEffect( () => {
3941
// Skip check for native as only simple WordPress.com sites are supported in the current native block.
@@ -94,6 +96,28 @@ export default function useVideoData( {
9496
is_private: response.is_private,
9597
private_enabled_for_site: response.private_enabled_for_site,
9698
} );
99+
100+
// Check if the video belongs to the current site.
101+
try {
102+
const doesBelong: {
103+
'video-belong-to-site'?: boolean;
104+
body?: {
105+
'video-belong-to-site'?: boolean;
106+
};
107+
} = await apiFetch( {
108+
path: `/wpcom/v2/videopress/${ guid }/check-ownership/${ response.post_id }`,
109+
method: 'GET',
110+
} );
111+
112+
// Response shape can change depending on the envelope mode.
113+
setVideoBelongToSite(
114+
typeof doesBelong?.[ 'video-belong-to-site' ] === 'boolean'
115+
? doesBelong[ 'video-belong-to-site' ]
116+
: !! doesBelong?.body?.[ 'video-belong-to-site' ]
117+
);
118+
} catch ( error ) {
119+
debug( 'Error checking if video belongs to site', error );
120+
}
97121
} catch ( errorData ) {
98122
setIsRequestingVideoData( false );
99123
throw new Error( errorData?.message ?? errorData );
@@ -106,5 +130,5 @@ export default function useVideoData( {
106130
}
107131
}, [ id, guid ] );
108132

109-
return { videoData, isRequestingVideoData };
133+
return { videoData, isRequestingVideoData, videoBelongToSite };
110134
}

projects/packages/videopress/src/client/block-editor/hooks/use-video-data/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export type VideoDataProps = {
3030
export type UseVideoDataProps = {
3131
videoData: VideoDataProps;
3232
isRequestingVideoData: boolean;
33+
videoBelongToSite: boolean;
3334
};

0 commit comments

Comments
 (0)