Skip to content
Open
81 changes: 81 additions & 0 deletions src/wp-admin/includes/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,87 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) {
return $tmpfname;
}

/**
* Downloads an image from a URL to a temporary file.
*
* The URL is checked before anything is requested: it must name a file whose extension
* maps to an allowed image type, and it must pass wp_http_validate_url(). The only
* request is made by download_url(), and its response is limited to the maximum upload
* size plus one byte, so an oversized file is never written to disk in full and still
* fails a check against wp_max_upload_size().
*
* The caller checks the size and the content of the downloaded file, and deletes the
* temporary file once it is done with it.
*
* @todo Use this function in WP_REST_Attachments_Controller::upload_from_url() as well.
*
* @since 7.3.0
* @access private
*
* @param string $url The URL of the image.
* @return array|WP_Error {
* The downloaded file, or a WP_Error when the URL is refused (`invalid_image_url`)
* or the download fails (the error of download_url()).
*
* @type string $name The file name, taken from the path of the URL as it is.
* @type string $tmp_name The path of the temporary file.
* }
*/
function _wp_download_image_from_url( $url ) {
$invalid_url = __( 'Invalid URL. Provide a valid, publicly reachable HTTP or HTTPS image URL.' );
if ( ! is_string( $url ) || '' === $url ) {
return new WP_Error( 'invalid_image_url', $invalid_url, array( 'status' => 400 ) );
}

// The name is taken from the URL's path as it is, without percent-decoding.
$filename = wp_basename( (string) wp_parse_url( $url, PHP_URL_PATH ) );
if ( '' === $filename ) {
return new WP_Error( 'invalid_image_url', __( 'Could not determine a filename from the provided URL.' ), array( 'status' => 400 ) );
}

$filetype = wp_check_filetype( $filename );
if ( ! $filetype['type'] || ! str_starts_with( $filetype['type'], 'image/' ) ) {
return new WP_Error( 'invalid_image_url', __( 'Only images can be uploaded from a URL.' ), array( 'status' => 400 ) );
}

if ( false === wp_http_validate_url( $url ) ) {
return new WP_Error( 'invalid_image_url', $invalid_url, array( 'status' => 400 ) );
}

/*
* One byte over the maximum is enough to fail a size check, so an oversized file is
* never written to disk in full. The limit applies to requests for this URL only and
* is removed again whatever the download gives.
*/
$max_size = (int) wp_max_upload_size();
$limit_response_size = static function ( $args, $request_url ) use ( $url, $max_size ) {
if ( $request_url === $url ) {
$args['limit_response_size'] = $max_size + 1;
}

return $args;
};

if ( $max_size > 0 ) {
add_filter( 'http_request_args', $limit_response_size, PHP_INT_MAX, 2 );
}

try {
$tmp_file = download_url( $url );
} finally {
remove_filter( 'http_request_args', $limit_response_size, PHP_INT_MAX );
}

if ( is_wp_error( $tmp_file ) ) {
return $tmp_file;
}

return array(
'name' => $filename,
'tmp_name' => $tmp_file,
);
}

/**
* Calculates and compares the MD5 of a file to its expected value.
*
Expand Down
5 changes: 5 additions & 0 deletions src/wp-includes/abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
declare( strict_types = 1 );

require_once __DIR__ . '/abilities/class-wp-abilities-content.php';
require_once __DIR__ . '/abilities/class-wp-abilities-media.php';
require_once __DIR__ . '/abilities/class-wp-abilities-settings.php';
require_once __DIR__ . '/abilities/class-wp-abilities-users.php';

Expand Down Expand Up @@ -50,6 +51,7 @@ function wp_register_core_ability_categories(): void {
*
* @since 6.9.0
* @since 7.2.0 Added the `core/content-query`, `core/settings-get`, and `core/users-query` abilities.
* @since 7.3.0 Added the `core/media-query`, `core/media-upload`, `core/media-update`, and `core/media-delete` abilities.
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
Expand Down Expand Up @@ -379,4 +381,7 @@ function wp_register_core_abilities(): void {

// Register the content abilities (currently the read-only `core/content-query`).
( new WP_Abilities_Content() )->register();

// Register the media abilities.
( new WP_Abilities_Media() )->register();
}
27 changes: 19 additions & 8 deletions src/wp-includes/abilities/class-wp-abilities-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ public function register(): void {
private function register_content_query(): void {
/*
* Post types must be registered with `show_in_abilities` before the ability is
* registered so they are included in its input schema.
* registered so they are included in its input schema. Attachments are exposed
* through the media abilities instead.
*/
$post_types = array_values( get_post_types( array( 'show_in_abilities' => true ) ) );
$post_types = $this->get_exposed_post_types();
if ( empty( $post_types ) ) {
return;
}

/*
* Internal statuses (e.g. `inherit`) are excluded here and from the statuses a query
* covers by default, so the posts of post types that rely on them (attachments)
* cannot be queried, not even by ID. Revisit if such a post type is ever exposed via
* `show_in_abilities`.
* covers by default, so the posts of post types that rely on them cannot be queried,
* not even by ID.
*/
$statuses = array_values( get_post_stati( array( 'internal' => false ) ) );

Expand Down Expand Up @@ -824,9 +824,20 @@ private function is_field_requested( string $path, array $fields ): bool {
* @return WP_Post_Type|null The post type object, or null when the post type is not exposed.
*/
private function get_exposed_post_type( $post_type ): ?WP_Post_Type {
$post_type_object = is_string( $post_type ) ? get_post_type_object( $post_type ) : null;
return is_string( $post_type ) && in_array( $post_type, $this->get_exposed_post_types(), true ) ? get_post_type_object( $post_type ) : null;
}

return empty( $post_type_object->show_in_abilities ) ? null : $post_type_object;
/**
* Returns the names of the post types exposed through the Abilities API.
*
* Attachments are never exposed: the media abilities handle them.
*
* @since 7.3.0
*
* @return list<string> The post type names.
*/
private function get_exposed_post_types(): array {
return array_values( array_diff( get_post_types( array( 'show_in_abilities' => true ) ), array( 'attachment' ) ) );
}

/**
Expand Down Expand Up @@ -1362,7 +1373,7 @@ private function get_visible_author( int $user_id ): ?WP_User {
}

if ( ! isset( $this->visible_authors[ $user_id ] ) ) {
$post_types = array_values( array_filter( get_post_types( array( 'show_in_abilities' => true ), 'names' ), 'is_post_type_viewable' ) );
$post_types = array_values( array_filter( $this->get_exposed_post_types(), 'is_post_type_viewable' ) );
$this->visible_authors[ $user_id ] = (int) count_user_posts( $user_id, $post_types ) > 0;
}

Expand Down
Loading
Loading