@@ -79,13 +79,15 @@ function () use ( $ns, $version ) {
7979 'callback ' => [ $ this , 'set_post_scan_results ' ],
8080 'args ' => [
8181 'id ' => [
82+ 'required ' => true ,
8283 'validate_callback ' => function ( $ param ) {
8384 return is_numeric ( $ param );
8485 },
86+ 'sanitize_callback ' => 'absint ' ,
8587 ],
8688 ],
87- 'permission_callback ' => function () {
88- return current_user_can ( ' edit_posts ' );
89+ 'permission_callback ' => function ( $ request ) {
90+ return $ this -> user_can_edit_passed_post_id ( $ request );
8991 },
9092 ]
9193 );
@@ -102,7 +104,7 @@ function () use ( $ns, $version ) {
102104 'methods ' => 'GET ' ,
103105 'callback ' => [ $ this , 'get_scans_stats ' ],
104106 'permission_callback ' => function () {
105- return current_user_can ( 'read ' ); // able to access the admin dashboard.
107+ return current_user_can ( 'edit_posts ' );
106108 },
107109 ]
108110 );
@@ -119,7 +121,7 @@ function () use ( $ns, $version ) {
119121 'methods ' => 'POST ' ,
120122 'callback ' => [ $ this , 'clear_cached_scans_stats ' ],
121123 'permission_callback ' => function () {
122- return current_user_can ( 'read ' ); // able to access the admin dashboard.
124+ return current_user_can ( 'publish_posts ' );
123125 },
124126 ]
125127 );
@@ -136,7 +138,7 @@ function () use ( $ns, $version ) {
136138 'methods ' => 'GET ' ,
137139 'callback ' => [ $ this , 'get_scans_stats_by_post_type ' ],
138140 'permission_callback ' => function () {
139- return current_user_can ( 'read ' ); // able to access the admin dashboard.
141+ return current_user_can ( 'edit_posts ' );
140142 },
141143 ]
142144 );
@@ -153,7 +155,7 @@ function () use ( $ns, $version ) {
153155 'methods ' => 'GET ' ,
154156 'callback ' => [ $ this , 'get_scans_stats_by_post_types ' ],
155157 'permission_callback ' => function () {
156- return current_user_can ( 'read ' ); // able to access the admin dashboard.
158+ return current_user_can ( 'edit_posts ' );
157159 },
158160 ]
159161 );
@@ -171,13 +173,15 @@ function () use ( $ns, $version ) {
171173 'callback ' => [ $ this , 'clear_issues_for_post ' ],
172174 'args ' => [
173175 'id ' => [
176+ 'required ' => true ,
174177 'validate_callback ' => function ( $ param ) {
175178 return is_numeric ( $ param );
176179 },
180+ 'sanitize_callback ' => 'absint ' ,
177181 ],
178182 ],
179- 'permission_callback ' => function () {
180- return current_user_can ( ' edit_posts ' );
183+ 'permission_callback ' => function ( $ request ) {
184+ return $ this -> user_can_edit_passed_post_id ( $ request );
181185 },
182186 ]
183187 );
@@ -203,10 +207,29 @@ function () use ( $ns, $version ) {
203207 );
204208 }
205209
210+ /**
211+ * Check if the user can edit a post.
212+ *
213+ * This is a permission callback to replace several places where we check if the user can edit a post.
214+ *
215+ * @since 1.30.1
216+ *
217+ * @param \WP_REST_Request $request The request object passed from the REST call. This should contain the 'id' of the post to check permissions for.
218+ *
219+ * @return bool|\WP_Error
220+ */
221+ public function user_can_edit_passed_post_id ( $ request ) {
222+ if ( ! isset ( $ request ['id ' ] ) ) {
223+ return new \WP_Error ( 'rest_post_invalid_id ' , __ ( 'A required parameter is missing. ' , 'accessibility-checker ' ), [ 'status ' => 400 ] );
224+ }
225+ $ post_id = (int ) $ request ['id ' ];
226+ return current_user_can ( 'edit_post ' , $ post_id ); // able to edit the post.
227+ }
228+
206229 /**
207230 * REST handler to clear issues results for a given post ID.
208231 *
209- * @param WP_REST_Request $request The request passed from the REST call.
232+ * @param \ WP_REST_Request $request The request passed from the REST call.
210233 *
211234 * @return \WP_REST_Response
212235 */
0 commit comments