Skip to content

Commit 8e8f4c6

Browse files
authored
Merge pull request #1103 from Automattic/pattern-edit-fix
Protect against pattern edits
2 parents 816eed6 + fcece4d commit 8e8f4c6

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Diff for: php/class-coauthors-endpoint.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ public function get_coauthors_search_results( $request ): WP_REST_Response {
152152
public function get_coauthors( $request ): WP_REST_Response {
153153
$response = array();
154154

155-
$this->_build_authors_response( $response, $request );
155+
if ( ! $this->request_is_for_wp_block_post_type( $request ) && ! $this->is_pattern_sync_operation() ) {
156+
$this->_build_authors_response( $response, $request );
157+
}
156158

157159
return rest_ensure_response( $response );
158160
}
@@ -233,6 +235,36 @@ public function _build_authors_response( &$response, $request ): void {
233235
}
234236
}
235237

238+
/**
239+
* Check if the request is for a wp_block post type.
240+
*
241+
* @param WP_REST_Request $request Request object.
242+
* @return bool
243+
*/
244+
private function request_is_for_wp_block_post_type( WP_REST_Request $request ): bool {
245+
return 'wp_block' === get_post_type( $request->get_param( 'post_id' ) );
246+
}
247+
248+
/**
249+
* Check if this is a pattern sync operation.
250+
*
251+
* @return bool
252+
*/
253+
private function is_pattern_sync_operation(): bool {
254+
$referer = wp_get_referer();
255+
if ( ! $referer ) {
256+
return false;
257+
}
258+
259+
$query_string = wp_parse_url( $referer, PHP_URL_QUERY );
260+
if ( ! $query_string ) {
261+
return false;
262+
}
263+
264+
parse_str( $query_string, $query_vars );
265+
return ! empty( $query_vars['post'] ) && 'wp_block' === get_post_type( $query_vars['post'] );
266+
}
267+
236268
/**
237269
* Add filters to REST endpoints for each post that
238270
* supports co-authors.

Diff for: tests/Integration/EndpointsTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ public function test_authors_get_coauthors(): void {
180180
$this->assertEquals( 'author', $get_response->data[0]['userNicename'] );
181181
}
182182

183+
/**
184+
* @covers CoAuthors\API\Endpoints::get_coauthors
185+
*/
186+
public function test_get_coauthors_wp_block_post_type(): void {
187+
$post_id = self::factory()->post->create( array( 'post_type' => 'wp_block' ) );
188+
$request = new \WP_REST_Request( 'GET', '/coauthors/v1/authors/' . $post_id );
189+
$request->set_param( 'post_id', $post_id );
190+
191+
$response = $this->_api->get_coauthors( $request );
192+
$this->assertEmpty( $response->get_data() );
193+
}
194+
195+
/**
196+
* @covers CoAuthors\API\Endpoints::get_coauthors
197+
*/
198+
public function test_get_coauthors_pattern_sync(): void {
199+
$post_id = self::factory()->post->create();
200+
$block_id = self::factory()->post->create( array( 'post_type' => 'wp_block' ) );
201+
$_SERVER['HTTP_REFERER'] = admin_url( sprintf( 'post.php?post=%d&action=edit', $block_id ) );
202+
203+
$request = new \WP_REST_Request( 'GET', '/coauthors/v1/authors/' . $post_id );
204+
$request->set_param( 'post_id', $post_id );
205+
206+
$response = $this->_api->get_coauthors( $request );
207+
$this->assertEmpty( $response->get_data() );
208+
209+
unset( $_SERVER['HTTP_REFERER'] );
210+
}
211+
183212
/**
184213
* @covers \CoAuthors\API\Endpoints::update_coauthors
185214
*/

0 commit comments

Comments
 (0)