Skip to content

Commit c24ec94

Browse files
pkevanchriszaratemaxschmeling
authored
Update permission callback to check user capability (#661)
--------- Co-authored-by: Chris Zarate <[email protected]> Co-authored-by: Max Schmeling <[email protected]>
1 parent 560aa33 commit c24ec94

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

inc/REST/RemoteDataController.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

src/blocks/remote-data-container/hooks/useRemoteData.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import apiFetch from '@wordpress/api-fetch';
2+
import { select } from '@wordpress/data';
3+
import { store as editorStore } from '@wordpress/editor';
24
import { useEffect, useState } from '@wordpress/element';
35

46
import { 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
};

types/remote-data.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ interface RemoteDataInnerBlockAttributes {
9595

9696
interface RemoteDataApiRequest {
9797
block_name: string;
98+
post_id: number | string | null;
9899
query_inputs: RemoteDataQueryInput[];
99100
query_key: string;
100101
}

0 commit comments

Comments
 (0)