Making WordPress.org


Ignore:
Timestamp:
08/13/2021 02:25:40 AM (3 years ago)
Author:
dd32
Message:

Theme Directory: Return a WP_Error object from WPORG_Themes_Upload::process_upload() instead of an array of error messages.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/upload.php

    r11175 r11176  
    4545    $notice = '';
    4646
    47     if ( ! empty( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'wporg-themes-upload' ) && 'upload' === $_POST['action'] ) {
     47    if (
     48        ! empty( $_POST['_wpnonce'] ) &&
     49        wp_verify_nonce( $_POST['_wpnonce'], 'wporg-themes-upload' ) &&
     50        'upload' === $_POST['action']
     51    ) {
    4852        $messages = wporg_themes_process_upload();
    4953
    50         if ( ! empty( $messages ) ) {
    51             $notice_content = "";
     54        $notice_content = '';
    5255
    53             if ( is_array( $messages ) ) {
    54                 foreach ( $messages as $message){
    55                     $notice_content .= "<li>{$message}</li>";
    56                 }
    57             } else {
    58                 $notice_content = "<li>{$messages}</li>";
     56        if ( is_wp_error( $messages ) ) {
     57            foreach ( $messages->get_error_codes() as $code ) {
     58                $message         = $messages->get_error_message( $code );
     59                $notice_content .= "<li class='error-code-{$code}'>{$message}</li>";
    5960            }
     61        } else {
     62            $notice_content = "<li>{$messages}</li>";
     63        }
    6064
    61             $notice = "<div class='notice notice-warning notice-large'><ul>{$notice_content}</ul></div>";
    62         }
     65        $notice = "<div class='notice notice-warning notice-large'><ul>{$notice_content}</ul></div>";
    6366    }
    6467
     
    8083 * Runs basic checks and hands off to the upload processor.
    8184 *
    82  * @return string Failure or success message.
     85 * @return WP_Error|string Failure or success message.
    8386 */
    8487function wporg_themes_process_upload( ) {
    8588    if ( ! is_user_logged_in() ) {
    86         return __( 'You must be logged in to upload a new theme.', 'wporg-themes' );
     89        return new WP_Error(
     90            'not_logged_in',
     91            __( 'You must be logged in to upload a new theme.', 'wporg-themes' )
     92        );
    8793    }
    8894
    8995    if ( empty( $_FILES['zip_file'] ) ) {
    90         return __( 'Error in file upload.', 'wporg-themes' );
     96        return new WP_Error(
     97            'invalid_upload',
     98            __( 'Error in file upload.', 'wporg-themes' )
     99        );
    91100    }
    92101
    93102    if ( ! class_exists( 'WPORG_Themes_Upload' ) ) {
    94         include_once plugin_dir_path( __FILE__ ) . 'class-wporg-themes-upload.php';
     103        include_once __DIR__ . '/class-wporg-themes-upload.php';
    95104    }
    96105
    97     $upload = new WPORG_Themes_Upload;
     106    $upload  = new WPORG_Themes_Upload;
    98107    $message = $upload->process_upload( $_FILES['zip_file'] );
    99108
Note: See TracChangeset for help on using the changeset viewer.