'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'pdf' => 'application/pdf', ]; /** * Upload multiple files. * * @param array $files Uploaded files. * @return array */ public function upload( array $files ): array { $files = $this->normalize_files( $files ); $uploaded = []; foreach ( $files as $key => $file ) { $this->validate_file( $file ); $attachment_id = $this->upload_file( $file ); $uploaded[] = $attachment_id; } return $uploaded; } /** * Upload a single file as a WordPress attachment. * * @param array $file Normalized file data. * @return int * @throws \RuntimeException If the sideload fails. */ private function upload_file( array $file ): int { require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $attachment_id = media_handle_sideload( $file, 0 ); if ( is_wp_error( $attachment_id ) ) { throw new \RuntimeException( esc_html( $attachment_id->get_error_message() ) ); } return $attachment_id; } /** * Validate a single uploaded file. * * @param array $file Normalized file data. * @return void * @throws \RuntimeException If the file fails any validation check. */ private function validate_file( array $file ): void { // PHP error check. if ( UPLOAD_ERR_OK !== $file['error'] ) { throw new \RuntimeException( 'File upload failed.' ); } // File size check. if ( $file['size'] > self::MAX_FILE_SIZE ) { throw new \RuntimeException( esc_html( sprintf( /* translators: 1: file name, 2: maximum file size in MB. */ __( 'The file "%1$s" exceeds the maximum allowed size of %2$d MB.', 'aarambha-case-management' ), $file['name'], self::MAX_FILE_SIZE / MB_IN_BYTES ) ) ); } // Extension check. $extension = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) ); $allowed_extensions = array_keys( self::ALLOWED_MIME_TYPES ); if ( ! in_array( $extension, $allowed_extensions, true ) ) { throw new \RuntimeException( esc_html( sprintf( /* translators: %s: file name. */ __( 'The file "%s" has an invalid file extension.', 'aarambha-case-management' ), $file['name'] ) ) ); } // MIME type check. $file_type = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'] ); if ( empty( $file_type['type'] ) || ! in_array( $file_type['type'], self::ALLOWED_MIME_TYPES, true ) ) { throw new \RuntimeException( esc_html( sprintf( /* translators: %s: file name. */ __( 'The file "%s" has an invalid MIME type.', 'aarambha-case-management' ), $file['name'] ) ) ); } } /** * Normalize the uploaded files array. * * @param array $files Raw $_FILES array. * @return array */ private function normalize_files( array $files ): array { $normalized = []; if ( empty( $files['name'] ) ) { return $normalized; } foreach ( $files['name'] as $index => $name ) { $normalized[] = [ 'name' => $name, 'type' => $files['type'][ $index ], 'tmp_name' => $files['tmp_name'][ $index ], 'error' => $files['error'][ $index ], 'size' => $files['size'][ $index ], ]; } return $normalized; } }