Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function public_news_image_validation( $valid_obj ) {
- if ( empty( $_FILES ) ) return $valid_obj;
- $form_id = $valid_obj['form']['id'];
- $formFields = &$valid_obj['form']['fields'];
- $page_number = GFFormDisplay::get_source_page( $form_id );
- foreach ( $formFields as &$field ) {
- if ( $page_number > 0 && $field["pageNumber"] != $page_number ) continue; //paged form scenario - only validate fields that are on current page
- if ( in_array(
- ( $field_type = RGFormsModel::get_input_type($field) ),
- array( 'fileupload', 'post_image' )
- )
- ) {
- $field_id = $field['id'];
- $input_name = 'input_' . $field_id;
- if ( $field['failed_validation'] ) return $valid_obj;
- $file_info = RGFormsModel::get_temp_filename( $form_id, $input_name );
- $temp_filepath = RGFormsModel::get_upload_path($form_id) . "/tmp/" . $file_info["temp_filename"];
- if ( file_exists( $temp_filepath ) ) {
- $bytes = filesize( $temp_filepath );
- $dims = @getimagesize( $temp_filepath );
- }
- elseif ( isset( $_FILES[$input_name] ) && !empty( $_FILES[$input_name] ) ) {
- $bytes = $_FILES[$input_name]['size'];
- $randname=mt_rand() . $_FILES[$input_name]['name'];
- $_FILES[$input_name]['name']=$randname;
- $dims = @getimagesize( $_FILES[$input_name]['tmp_name'] );
- }
- else return $valid_obj;
- $max_size = 1024*4000; // 4000kb
- if ( $bytes > $max_size ) {
- unset( RGFormsModel::$uploaded_files[$form_id][$input_name] );
- unset( $_FILES[$input_name] );
- $field['failed_validation'] = true;
- $field['validation_message'] = sprintf( __( 'File upload size (%s) exceeded', 'gravityforms' ), $max_size/1024 . 'kb' );
- $valid_obj['is_valid'] = false;
- return $valid_obj;
- }
- if ( ! empty( $dims ) && is_array( $dims ) ) {
- list( $up_width, $up_height ) = $dims;
- if ( min( $up_width, $up_height ) < 300 ) { //smallest image size should be at least 300px
- unset( RGFormsModel::$uploaded_files[$form_id][$input_name] );
- unset( $_FILES[$input_name] );
- $field['failed_validation'] = true;
- $field['validation_message'] = sprintf( __( 'Image should be at least %s', 'gravityforms' ), '300x300 (px)' );
- $valid_obj['is_valid'] = false;
- //return $valid_obj;
- }
- }
- }
- }
- return $valid_obj;
- }
- add_action( 'gform_validation', 'public_news_image_validation' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement