Sindbad~EG File Manager
<?php
namespace RealPress\Helpers;
use RealPress\Helpers\Forms\FloorPlan;
use RealPress\Helpers\Fields\FileUpload;
/**
* Class Validation
* @package RealPress\Helpers
*/
class Validation {
/**
* @param $value
* @param $field
* @param $user_id
*
* @return array
*/
public static function validate_floor_plan( $value, $field, $user_id ) {
if ( $field['type'] instanceof FloorPlan ) {
foreach ( $value as $key => $floor_plan ) {
if ( empty( $floor_plan['image'] ) ) {
continue;
}
$image_id = $floor_plan['image'];
$plan_image_field = $field['fields']['image'] ?? array();
$value[ $key ]['image'] = self::validate_file_upload( $image_id, $plan_image_field, $user_id );
}
}
return $value;
}
/**
* @param array $value
*
* @return array
*/
public static function validate_additional_fields( array $value ) {
foreach ( $value as $key => $fields ) {
if ( empty( $fields['label'] ) || empty( $fields['value'] ) ) {
unset( $value[ $key ] );
}
}
return array_values( $value );
}
/**
* @param $attachment_id
* @param $max_file_size
*
* @return bool
*/
public static function is_valid_max_file_size( $attachment_id, $max_file_size ) {
if ( empty( $attachment_id ) ) {
return true;
}
$file_size = 0;
$attachment = wp_get_attachment_metadata( $attachment_id );
if ( $attachment ) {
$file_size = $attachment['filesize'];
}
if ( empty( $max_file_size ) ) {
return true;
}
$max_file_size = $max_file_size * 1024;
if ( $file_size <= $max_file_size ) {
return true;
}
return false;
}
/**
* @param $attachment_id
* @param array $max_size
*
* @return bool
*/
public static function is_valid_max_size( $attachment_id, array $max_size ) {
if ( empty( $attachment_id ) || empty( $max_size ) ) {
return true;
}
$attachment = wp_get_attachment_metadata( $attachment_id );
if ( $attachment['width'] <= $max_size['width'] && $attachment['height'] <= $max_size['height'] ) {
return true;
}
return false;
}
/**
* @param $value
* @param $cb
*
* @return array|mixed|string
*/
public static function sanitize_params_submitted( $value, $type_content = 'text' ) {
$value = wp_unslash( $value );
if ( is_string( $value ) ) {
$value = trim( $value );
switch ( $type_content ) {
case 'html':
$value = General::ksesHTML( $value );
break;
case 'textarea':
$value = implode( '\n', array_map( 'sanitize_textarea_field', explode( '\n', $value ) ) );
break;
case 'key':
$value = sanitize_key( $value );
break;
default:
$value = sanitize_text_field( $value );
}
} elseif ( is_array( $value ) ) {
return array_map( array( __CLASS__, 'sanitize_params_submitted' ), $value );
}
return $value;
}
/**
* @param $value
* @param $field
*
* @return float|int|mixed|string
*/
public static function validate_number( $value, $field ) {
if ( is_numeric( $value ) ) {
if ( isset( $field['min'] ) ) {
if ( ! is_numeric( $field['min'] ) || $value < $field['min'] ) {
return '';
}
}
if ( isset( $field['max'] ) ) {
if ( ! is_numeric( $field['max'] ) || $value > $field['max'] ) {
return '';
}
}
}
return $value;
}
/**
* @param $value
* @param $field
* @param $user_id
*
* @return mixed|string
*/
public static function validate_file_upload( $value, $field, $user_id ) {
if ( $field['type'] instanceof FileUpload ) {
if ( empty( $field['multiple'] ) ) {
if ( ! empty( $field['max_file_size'] ) ) {
$is_valid_max_file_size = self::is_valid_max_file_size( $value, $field['max_file_size'] );
if ( ! $is_valid_max_file_size ) {
$value = '';
}
}
if ( isset( $field['max_size'] ) && ! empty( $field['max_size'] ) ) {
$is_valid_max_size = self::is_valid_max_size( $value, $field['max_size'] );
if ( ! $is_valid_max_size ) {
$value = '';
}
}
$attachment = get_post( $value );
if ( $attachment === null || is_wp_error( $attachment ) ) {
return '';
}
if ( ! current_user_can( 'administrator' ) ) {
if ( intval( $attachment->post_author ) !== intval( $user_id ) ) {
return '';
}
}
} else {
$value = explode( ',', $value );
$attachment_ids = array();
foreach ( $value as $attachment_id ) {
if ( ! empty( $field['max_file_size'] ) ) {
$is_valid_max_file_size = self::is_valid_max_file_size( $attachment_id, $field['max_file_size'] );
if ( ! $is_valid_max_file_size ) {
continue;
}
}
if ( ! empty( $field['max_size'] ) ) {
$is_valid_max_size = self::is_valid_max_size( $attachment_id, $field['max_size'] );
if ( ! $is_valid_max_size ) {
continue;
}
}
if ( ! current_user_can( 'administrator' ) ) {
$attachment = get_post( $attachment_id );
if ( $attachment === null || is_wp_error( $attachment ) ) {
continue;
}
if ( intval( $attachment->post_author ) !== intval( $user_id ) ) {
continue;
}
}
$attachment_ids[] = $attachment_id;
}
$value = implode( ',', $attachment_ids );
}
}
return $value;
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists