File tree Expand file tree Collapse file tree 3 files changed +24
-2
lines changed
src/blocks/remote-data-container/hooks Expand file tree Collapse file tree 3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -78,7 +78,22 @@ public static function execute_queries( WP_REST_Request $request ): array|WP_Err
7878 );
7979 }
8080
81- public static function permission_callback (): bool {
82- return true ;
81+ /**
82+ * Permission callback for the remote data endpoint.
83+ *
84+ * @param WP_REST_Request $request The REST request.
85+ *
86+ * @return bool|WP_Error Returns status of user permission, otherwise WP_Error.
87+ */
88+ public static function permission_callback ( WP_REST_Request $ request ): bool |WP_Error {
89+ $ post_id = (int ) $ request ->get_param ( 'post_id ' );
90+ if ( $ post_id <= 0 ) {
91+ return new WP_Error (
92+ 'rest_post_invalid_id ' ,
93+ __ ( 'Invalid post ID. ' ),
94+ array ( 'status ' => 404 )
95+ );
96+ }
97+ return current_user_can ( 'edit_post ' , $ post_id );
8398 }
8499}
Original file line number Diff line number Diff line change 11import apiFetch from '@wordpress/api-fetch' ;
2+ import { select } from '@wordpress/data' ;
3+ import { store as editorStore } from '@wordpress/editor' ;
24import { useEffect , useState } from '@wordpress/element' ;
35
46import { REMOTE_DATA_REST_API_URL } from '@/blocks/remote-data-container/config/constants' ;
@@ -177,6 +179,9 @@ export function useRemoteData( {
177179 }
178180
179181 async function fetch ( inputs : RemoteDataQueryInput [ ] ) : Promise < void > {
182+ const { getCurrentPostId } = select ( editorStore ) ;
183+ const postId = getCurrentPostId ( ) ;
184+
180185 // If there are no inputs, there is nothing to fetch. Empty query inputs
181186 // must be represented by an empty object, e.g. `[ {} ]`.
182187 if ( 0 === inputs . length ) {
@@ -192,6 +197,7 @@ export function useRemoteData( {
192197
193198 const requestData : RemoteDataApiRequest = {
194199 block_name : blockName ,
200+ post_id : postId ?? null ,
195201 query_key : queryKey ,
196202 query_inputs : inputs ,
197203 } ;
Original file line number Diff line number Diff line change @@ -95,6 +95,7 @@ interface RemoteDataInnerBlockAttributes {
9595
9696interface RemoteDataApiRequest {
9797 block_name : string ;
98+ post_id : number | string | null ;
9899 query_inputs : RemoteDataQueryInput [ ] ;
99100 query_key : string ;
100101}
You can’t perform that action at this time.
0 commit comments