Validate::validate_upload( object $field )

Description #

Validate an upload field.

Parameters #

  • $field
    object (Required) Instance of @see AP_Field object.

Source #

File: lib/class-validate.php

	public static function validate_upload( $field ) {
		$args  = $field->get( 'upload_options' );
		$value = $field->unsafe_value();

		if ( ! empty( $value ) ) {

			// Check if user have permission to upload files.
			if ( ! ap_user_can_upload() ) {
				$field->add_error( 'deny-upload', __( 'You are not allowed to upload file(s)', 'anspress-question-answer' ) );
			}

			$is_numeric = wp_is_numeric_array( $value );

			if (
				( false === $args['multiple'] && $is_numeric ) ||
				( true === $args['multiple'] && count( $value ) > $args['max_files'] )
			) {
				$field->add_error(
					'max-uploads',
					sprintf(
						// Translators: %1$d contain maximum files user can upload, %2$s contain label of field.
						__( 'You cannot upload more than %1$d file in field %2$s', 'anspress-question-answer' ),
						$args['max_files'],
						$field->get( 'label' )
					)
				);
			}

			// Check if allowed mimes.
			$valid_mimes = self::file_valid_type( $field );
			if ( false !== $valid_mimes ) {
				$field->add_error( 'mimes-not-allowed', __( 'File type is not allowed to upload.', 'anspress-question-answer' ) );
			}

			// Check if file have any error.
			$error = self::file_have_error( $field );
			if ( false !== $error ) {
				$field->add_error( 'upload-file-error', $error );
			}

			// Check file size.
			$file_size = self::file_size_error( $field );
			if ( false !== $file_size ) {
				$field->add_error(
					'max-size-upload',
					sprintf(
						// Translators: %s contain maximum file size user can upload.
						__( 'File(s) size is bigger than %s MB', 'anspress-question-answer' ),
						round( ap_opt( 'max_upload_size' ) / ( 1024 * 1024 ), 2 )
					)
				);
			}
		}
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment