Make WordPress Core


Ignore:
Timestamp:
07/17/2018 07:21:50 AM (7 years ago)
Author:
pento
Message:

REST API: Attachments controller should respect upload limits.

When the REST API is in use on WordPress multisite, the WP_REST_Attachments_Controller should respect the "Max upload file size" and "Site upload space" site options.

Props flixos90, danielbachhuber.
Fixes #43751.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r43437 r43462  
    560560        );
    561561
     562        $size_check = self::check_upload_size( $file_data );
     563        if ( is_wp_error( $size_check ) ) {
     564            return $size_check;
     565        }
     566
    562567        $overrides = array(
    563568            'test_form' => false,
     
    727732        }
    728733
     734        $size_check = self::check_upload_size( $files['file'] );
     735        if ( is_wp_error( $size_check ) ) {
     736            return $size_check;
     737        }
     738
    729739        /** Include admin functions to get access to wp_handle_upload() */
    730740        require_once ABSPATH . 'wp-admin/includes/admin.php';
     
    764774    }
    765775
     776    /**
     777     * Determine if uploaded file exceeds space quota on multisite.
     778     *
     779     * Replicates check_upload_size().
     780     *
     781     * @since 4.9.8
     782     *
     783     * @param array $file $_FILES array for a given file.
     784     * @return true|WP_Error True if can upload, error for errors.
     785     */
     786    protected function check_upload_size( $file ) {
     787        if ( ! is_multisite() ) {
     788            return true;
     789        }
     790
     791        if ( get_site_option( 'upload_space_check_disabled' ) ) {
     792            return true;
     793        }
     794
     795        $space_left = get_upload_space_available();
     796
     797        $file_size = filesize( $file['tmp_name'] );
     798        if ( $space_left < $file_size ) {
     799            /* translators: %s: required disk space in kilobytes */
     800            return new WP_Error( 'rest_upload_limited_space', sprintf( __( 'Not enough space to upload. %s KB needed.' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) ), array( 'status' => 400 ) );
     801        }
     802
     803        if ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
     804            /* translators: %s: maximum allowed file size in kilobytes */
     805            return new WP_Error( 'rest_upload_file_too_big', sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) ), array( 'status' => 400 ) );
     806        }
     807
     808        if ( upload_is_user_over_quota( false ) ) {
     809            return new WP_Error( 'rest_upload_user_quota_exceeded', __( 'You have used your space quota. Please delete files before uploading.' ), array( 'status' => 400 ) );
     810        }
     811        return true;
     812    }
     813
    766814}
Note: See TracChangeset for help on using the changeset viewer.