Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API code optimizations #7682

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/wp-includes/rest-api/class-wp-rest-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static function canonicalize_header_name( $key ) {
* @return string|null String value if set, null otherwise.
*/
public function get_header( $key ) {
$key = $this->canonicalize_header_name( $key );
$key = static::canonicalize_header_name( $key );

if ( ! isset( $this->headers[ $key ] ) ) {
return null;
Expand All @@ -217,7 +217,7 @@ public function get_header( $key ) {
* @return array|null List of string values if set, null otherwise.
*/
public function get_header_as_array( $key ) {
$key = $this->canonicalize_header_name( $key );
$key = static::canonicalize_header_name( $key );

if ( ! isset( $this->headers[ $key ] ) ) {
return null;
Expand All @@ -235,7 +235,7 @@ public function get_header_as_array( $key ) {
* @param string $value Header value, or list of values.
*/
public function set_header( $key, $value ) {
$key = $this->canonicalize_header_name( $key );
$key = static::canonicalize_header_name( $key );
$value = (array) $value;

$this->headers[ $key ] = $value;
Expand All @@ -250,7 +250,7 @@ public function set_header( $key, $value ) {
* @param string $value Header value, or list of values.
*/
public function add_header( $key, $value ) {
$key = $this->canonicalize_header_name( $key );
$key = static::canonicalize_header_name( $key );
$value = (array) $value;

if ( ! isset( $this->headers[ $key ] ) ) {
Expand All @@ -268,7 +268,7 @@ public function add_header( $key, $value ) {
* @param string $key Header name.
*/
public function remove_header( $key ) {
$key = $this->canonicalize_header_name( $key );
$key = static::canonicalize_header_name( $key );
unset( $this->headers[ $key ] );
}

Expand Down
8 changes: 3 additions & 5 deletions src/wp-includes/rest-api/class-wp-rest-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ public function remove_link( $rel, $href = null ) {
return;
}

if ( $href ) {
$this->links[ $rel ] = wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' );
} else {
$this->links[ $rel ] = array();
}
$this->links[ $rel ] = $href
? wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' )
: array();

if ( ! $this->links[ $rel ] ) {
unset( $this->links[ $rel ] );
Expand Down
40 changes: 15 additions & 25 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,7 @@ public function serve_request( $path = null ) {
}

if ( empty( $path ) ) {
if ( isset( $_SERVER['PATH_INFO'] ) ) {
$path = $_SERVER['PATH_INFO'];
} else {
$path = '/';
}
$path = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '/';
}

$request = new WP_REST_Request( $_SERVER['REQUEST_METHOD'], $path );
Expand Down Expand Up @@ -738,7 +734,7 @@ public static function get_compact_response_links( $response ) {
// Relation now changes from '$uri' to '$curie:$relation'.
$rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) );
preg_match( '!' . $rel_regex . '!', $rel, $matches );
if ( $matches ) {
if ( array() !== $matches ) {
$new_rel = $curie['name'] . ':' . $matches[1];
$used_curies[ $curie['name'] ] = $curie;
$links[ $new_rel ] = $items;
Expand All @@ -749,7 +745,7 @@ public static function get_compact_response_links( $response ) {
}

// Push the curies onto the start of the links array.
if ( $used_curies ) {
if ( array() !== $used_curies ) {
$links['curies'] = array_values( $used_curies );
}

Expand Down Expand Up @@ -836,7 +832,7 @@ protected function embed_links( $data, $embed = true ) {
}
}

if ( ! empty( $embedded ) ) {
if ( array() !== $embedded ) {
$data['_embedded'] = $embedded;
}

Expand Down Expand Up @@ -1161,16 +1157,14 @@ protected function match_request_to_handler( $request ) {
}
}

if ( $with_namespace ) {
$routes = array_merge( ...$with_namespace );
} else {
$routes = $this->get_routes();
}
$routes = array() !== $with_namespace
? array_merge( ...$with_namespace )
: $this->get_routes();

foreach ( $routes as $route => $handlers ) {
$match = preg_match( '@^' . $route . '$@i', $path, $matches );

if ( ! $match ) {
if ( 1 !== $match ) {
continue;
}

Expand Down Expand Up @@ -1255,7 +1249,7 @@ protected function respond_to_request( $request, $route, $handler, $response ) {
$response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request );

// Check permission specified on the route.
if ( ! is_wp_error( $response ) && ! empty( $handler['permission_callback'] ) ) {
if ( ! empty( $handler['permission_callback'] ) && ! is_wp_error( $response ) ) {
$permission = call_user_func( $handler['permission_callback'], $request );

if ( is_wp_error( $permission ) ) {
Expand Down Expand Up @@ -1286,11 +1280,9 @@ protected function respond_to_request( $request, $route, $handler, $response ) {
$dispatch_result = apply_filters( 'rest_dispatch_request', null, $request, $route, $handler );

// Allow plugins to halt the request via this filter.
if ( null !== $dispatch_result ) {
$response = $dispatch_result;
} else {
$response = call_user_func( $handler['callback'], $request );
}
$response = null === $dispatch_result
? call_user_func( $handler['callback'], $request )
: $dispatch_result;
}

/**
Expand All @@ -1316,11 +1308,9 @@ protected function respond_to_request( $request, $route, $handler, $response ) {
*/
$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );

if ( is_wp_error( $response ) ) {
$response = $this->error_to_response( $response );
} else {
$response = rest_ensure_response( $response );
}
$response = is_wp_error( $response )
? $this->error_to_response( $response )
: rest_ensure_response( $response );

$response->set_matched_route( $route );
$response->set_matched_handler( $handler );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,8 @@ public function update_item( $request ) {
wp_after_insert_post( $attachment, true, $attachment_before );

$response = $this->prepare_item_for_response( $attachment, $request );
$response = rest_ensure_response( $response );

return $response;
return rest_ensure_response( $response );
}

/**
Expand All @@ -457,11 +456,9 @@ public function update_item( $request ) {
* @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
*/
public function post_process_item( $request ) {
switch ( $request['action'] ) {
case 'create-image-subsizes':
require_once ABSPATH . 'wp-admin/includes/image.php';
wp_update_image_subsizes( $request['id'] );
break;
if ( 'create-image-subsizes' === $request['action'] ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
wp_update_image_subsizes( $request['id'] );
}

$request['context'] = 'edit';
Expand Down Expand Up @@ -567,7 +564,7 @@ public function edit_media_item( $request ) {
);
}

if ( 0 === count( $modifiers ) ) {
if ( array() === $modifiers ) {
return new WP_Error(
'rest_image_not_edited',
__( 'The image was not edited. Edit the image before applying the changes.' ),
Expand Down Expand Up @@ -722,7 +719,7 @@ public function edit_media_item( $request ) {
$new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );

// Copy the EXIF metadata from the original attachment if not generated for the edited image.
if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
if ( isset( $image_meta['image_meta'], $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
// Merge but skip empty values.
foreach ( (array) $image_meta['image_meta'] as $key => $value ) {
if ( empty( $new_image_meta['image_meta'][ $key ] ) && ! empty( $value ) ) {
Expand Down Expand Up @@ -1142,7 +1139,7 @@ protected function upload_from_data( $data, $headers, $time = null ) {
'type' => $type,
);

$size_check = self::check_upload_size( $file_data );
$size_check = $this->check_upload_size( $file_data );
if ( is_wp_error( $size_check ) ) {
return $size_check;
}
Expand Down Expand Up @@ -1310,7 +1307,7 @@ protected function upload_from_file( $files, $headers, $time = null ) {
$overrides['action'] = 'wp_handle_mock_upload';
}

$size_check = self::check_upload_size( $files['file'] );
$size_check = $this->check_upload_size( $files['file'] );
if ( is_wp_error( $size_check ) ) {
return $size_check;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ public function get_item( $request ) {
);
}

$response = $this->prepare_item_for_response( $autosave, $request );
return $response;
return $this->prepare_item_for_response( $autosave, $request );
}

/**
Expand Down Expand Up @@ -418,7 +417,7 @@ public function create_post_autosave( $post_data, array $meta = array() ) {
$revision_id = _wp_put_post_revision( $post_data, true );
}

if ( is_wp_error( $revision_id ) || 0 === $revision_id ) {
if ( 0 === $revision_id || is_wp_error( $revision_id ) ) {
return $revision_id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,14 @@ public function get_item_permissions_check( $request ) {
)
);
}
} else {
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error(
'block_cannot_read',
__( 'Sorry, you are not allowed to read blocks as this user.' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
} elseif ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error(
'block_cannot_read',
__( 'Sorry, you are not allowed to read blocks as this user.' ),
array(
'status' => rest_authorization_required_code(),
)
);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public function prepare_item_for_response( $item, $request ) {
if ( rest_is_field_included( $extra_field, $fields ) ) {
if ( isset( $block_type->$extra_field ) ) {
$field = $block_type->$extra_field;
if ( in_array( $extra_field, $deprecated_fields, true ) && is_array( $field ) ) {
if ( is_array( $field ) && in_array( $extra_field, $deprecated_fields, true ) ) {
// Since the schema only allows strings or null (but no arrays), we return the first array item.
$field = ! empty( $field ) ? array_shift( $field ) : '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public function filter_response_by_context( $data, $context ) {
* It doesn't make sense for a pattern to have rendered content on its own,
* since rendering a block requires it to be inside a post or a page.
*/
unset( $data['title']['rendered'] );
unset( $data['content']['rendered'] );
unset( $data['title']['rendered'], $data['content']['rendered'] );

// Add the core wp_pattern_sync_status meta as top level property to the response.
$data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public function get_items_permissions_check( $request ) {
__( 'Sorry, you are not allowed to read the post for this comment.' ),
array( 'status' => rest_authorization_required_code() )
);
} elseif ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) {
}

if ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) {
return new WP_Error(
'rest_cannot_read',
__( 'Sorry, you are not allowed to read comments without a post.' ),
Expand Down Expand Up @@ -427,10 +429,9 @@ public function get_item( $request ) {
return $comment;
}

$data = $this->prepare_item_for_response( $comment, $request );
$response = rest_ensure_response( $data );
$data = $this->prepare_item_for_response( $comment, $request );

return $response;
return rest_ensure_response( $data );
}

/**
Expand Down Expand Up @@ -484,15 +485,16 @@ public function create_item_permissions_check( $request ) {
);
}

if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) {
if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) {
return new WP_Error(
'rest_comment_invalid_author_ip',
/* translators: %s: Request parameter. */
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ),
array( 'status' => rest_authorization_required_code() )
);
}
if ( isset( $request['author_ip'] )
&& ! current_user_can( 'moderate_comments' )
&& ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] )
) {
return new WP_Error(
'rest_comment_invalid_author_ip',
/* translators: %s: Request parameter. */
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ),
array( 'status' => rest_authorization_required_code() )
);
}

if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) {
Expand Down Expand Up @@ -613,7 +615,7 @@ public function create_item( $request ) {
&& empty( $prepared_comment['comment_author_email'] )
&& empty( $prepared_comment['comment_author_url'] );

if ( is_user_logged_in() && $missing_author ) {
if ( $missing_author && is_user_logged_in() ) {
$user = wp_get_current_user();

$prepared_comment['user_id'] = $user->ID;
Expand All @@ -623,14 +625,14 @@ public function create_item( $request ) {
}

// Honor the discussion setting that requires a name and email address of the comment author.
if ( get_option( 'require_name_email' ) ) {
if ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) ) {
return new WP_Error(
'rest_comment_author_data_required',
__( 'Creating a comment requires valid author name and email values.' ),
array( 'status' => 400 )
);
}
if ( get_option( 'require_name_email' )
&& ( empty( $prepared_comment['comment_author'] ) || empty( $prepared_comment['comment_author_email'] ) )
) {
return new WP_Error(
'rest_comment_author_data_required',
__( 'Creating a comment requires valid author name and email values.' ),
array( 'status' => 400 )
);
}

if ( ! isset( $prepared_comment['comment_author_email'] ) ) {
Expand Down Expand Up @@ -955,7 +957,7 @@ public function delete_item( $request ) {
return $comment;
}

$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
$force = isset( $request['force'] ) && $request['force'];

/**
* Filters whether a comment can be trashed via the REST API.
Expand Down Expand Up @@ -1807,10 +1809,8 @@ protected function check_read_post_permission( $post, $request ) {
protected function check_read_permission( $comment, $request ) {
if ( ! empty( $comment->comment_post_ID ) ) {
$post = get_post( $comment->comment_post_ID );
if ( $post ) {
if ( $this->check_read_post_permission( $post, $request ) && 1 === (int) $comment->comment_approved ) {
return true;
}
if ( $post && ( 1 === (int) $comment->comment_approved ) && $this->check_read_post_permission( $post, $request ) ) {
return true;
}
}

Expand Down
Loading
Loading