upload_limits = $upload_limits; $this->validation_errors = $validation_errors; } public function validate_file( $listing, $file ) { $upload_limits = $this->get_listing_upload_limits( $listing ); if ( ! in_array( $file->get_mime_type(), $upload_limits['mime_types'] ) ) { $message = __( 'The type of the uploaded file is not allowed.', 'another-wordpress-classifieds-plugin' ); $this->throw_file_validation_exception( $file, $message ); } if ( ! $this->upload_limits->can_add_file_to_listing( $listing, $file ) ) { $message = $this->validation_errors->get_cannot_add_more_files_of_type_error_message(); $this->throw_file_validation_exception( $file, $message ); } if ( ! file_exists( $file->get_path() ) ) { $message = __( 'The file was not found in the temporary uploads directory.', 'another-wordpress-classifieds-plugin' ); $this->throw_file_validation_exception( $file, $message ); } $this->validate_file_size( $file, $upload_limits ); $this->additional_verifications( $file, $upload_limits ); } abstract protected function get_listing_upload_limits( $listing ); private function throw_file_validation_exception( $file, $message ) { $message = str_replace( '', '' . $file->get_real_name() . '', $message ); throw new AWPCP_Exception( $message ); } private function validate_file_size( $file, $upload_limits ) { $filesize = filesize( $file->get_path() ); if ( empty( $filesize ) || $filesize <= 0 ) { $message = __( 'There was an error trying to find out the file size of the file .', 'another-wordpress-classifieds-plugin' ); $this->throw_file_validation_exception( $file, $message ); } if ( $filesize > $upload_limits['max_file_size'] ) { $message = $this->validation_errors->get_file_is_too_large_error_message(); $message = sprintf( $message, '' . $file->get_real_name() . '', $upload_limits['max_file_size'] ); throw new AWPCP_Exception( $message ); } if ( $filesize < $upload_limits['min_file_size'] ) { $message = __( 'The file is smaller than the minimum allowed file size of bytes. The file was not uploaded.', 'another-wordpress-classifieds-plugin' ); $message = str_replace( '', $upload_limits['min_file_size'], $message ); $this->throw_file_validation_exception( $file, $message ); } } protected function additional_verifications( $file, $upload_limits ) { // nothing here! } }