diff --git a/assets/js/um-functions.js b/assets/js/um-functions.js
index d0e2a1e7..b2fa6b9c 100644
--- a/assets/js/um-functions.js
+++ b/assets/js/um-functions.js
@@ -234,6 +234,10 @@ function initImageUpload_UM( trigger ) {
upload_text = '';
}
+ if( jQuery('#um_upload_single:visible').data('user_id') ){
+ user_id = jQuery('#um_upload_single:visible').data('user_id');
+ }
+
trigger.uploadFile({
url: um_scripts.imageupload,
method: "POST",
@@ -243,7 +247,8 @@ function initImageUpload_UM( trigger ) {
set_id: trigger.data('set_id'),
set_mode: trigger.data('set_mode'),
_wpnonce: trigger.data('nonce'),
- timestamp: trigger.data('timestamp')
+ timestamp: trigger.data('timestamp'),
+ user_id: user_id
},
fileName: trigger.data('key'),
allowedTypes: trigger.data('allowed_types'),
@@ -281,13 +286,17 @@ function initImageUpload_UM( trigger ) {
um_modal_responsive();
} else {
-
- jQuery.each( data, function(key, value) {
-
+ console.log( data );
+ jQuery.each( data, function( i, d ) {
+
var img_id = trigger.parents('.um-modal-body').find('.um-single-image-preview img');
var img_id_h = trigger.parents('.um-modal-body').find('.um-single-image-preview');
- img_id.attr("src", value);
+ var cache_ts = new Date();
+
+ img_id.attr("src", d.url + "?"+cache_ts.getTime() );
+ img_id.data("file", d.file );
+
img_id.load(function(){
trigger.parents('.um-modal-body').find('.um-modal-btn.um-finish-upload.disabled').removeClass('disabled');
@@ -301,6 +310,9 @@ function initImageUpload_UM( trigger ) {
}
+ },
+ onError: function ( e ){
+ console.log( e );
}
});
@@ -326,6 +338,10 @@ function initFileUpload_UM( trigger ) {
upload_text = '';
}
+ if( jQuery('#um_upload_single:visible').data('user_id') ){
+ user_id = jQuery('#um_upload_single:visible').data('user_id');
+ }
+
trigger.uploadFile({
url: um_scripts.fileupload,
method: "POST",
@@ -377,14 +393,27 @@ function initFileUpload_UM( trigger ) {
trigger.parents('.um-modal-body').find('.ajax-upload-dragdrop,.upload-statusbar').hide(0);
trigger.parents('.um-modal-body').find('.um-single-file-preview').show(0);
- if (key == 'icon') {
- trigger.parents('.um-modal-body').find('.um-single-fileinfo i').removeClass().addClass(value);
+ if ( key == 'icon' ) {
+
+ trigger.parents('.um-modal-body').find('.um-single-fileinfo i').removeClass().addClass( value );
+
} else if ( key == 'icon_bg' ) {
+
trigger.parents('.um-modal-body').find('.um-single-fileinfo span.icon').css({'background-color' : value } );
+
} else if ( key == 'filename' ) {
- trigger.parents('.um-modal-body').find('.um-single-fileinfo span.filename').html(value);
- } else {
+
+ trigger.parents('.um-modal-body').find('.um-single-fileinfo a').attr('data-file', value );
+
+ }else if( key == 'original_name' ){
+
+ trigger.parents('.um-modal-body').find('.um-single-fileinfo a').attr('data-orignal-name', value );
+ trigger.parents('.um-modal-body').find('.um-single-fileinfo span.filename').html( value );
+
+ } else if ( key == 'url' ) {
+
trigger.parents('.um-modal-body').find('.um-single-fileinfo a').attr('href', value);
+
}
});
@@ -395,6 +424,9 @@ function initFileUpload_UM( trigger ) {
}
+ },
+ onError: function ( e ){
+ console.log( e );
}
});
diff --git a/assets/js/um-functions.min.js b/assets/js/um-functions.min.js
index 481141f6..96899924 100644
--- a/assets/js/um-functions.min.js
+++ b/assets/js/um-functions.min.js
@@ -1 +1 @@
-function UM_check_password_matched(){jQuery(document).on("keyup","input[data-key=user_password],input[data-key=confirm_user_password]",function(e){var t=jQuery("input[data-key=user_password]").val(),o=jQuery("input[data-key=confirm_user_password]").val(),a=jQuery("input[data-key=user_password],input[data-key=confirm_user_password]");t||o?t!==o?a.removeClass("um-validate-matched").addClass("um-validate-not-matched"):a.removeClass("um-validate-not-matched").addClass("um-validate-matched"):a.removeClass("um-validate-matched").removeClass("um-validate-not-matched")})}function UM_hide_menus(){menu=jQuery(".um-dropdown"),menu.parents("div").find("a").removeClass("active"),menu.hide()}function UM_domenus(){jQuery(".um-dropdown").each(function(){var e=jQuery(this),t=jQuery(this).attr("data-element"),o=jQuery(this).attr("data-position");jQuery(t).addClass("um-trigger-menu-on-"+e.attr("data-trigger")),jQuery(window).width()<=1200&&"div.um-profile-edit"==t&&(o="lc"),"lc"==o&&(200>jQuery(t).find("img").width()?left_p=(jQuery(t).width()-jQuery(t).find("img").width())/2+(jQuery(t).find("img").width()-200)/2:left_p=(jQuery(t).width()-jQuery(t).find("img").width())/2,top_=parseInt(jQuery(t).find("a").css("top")),top_?(top_p=jQuery(t).find("img").height()+4+top_,left_gap=4):(top_p=jQuery(t).find("img").height()+4,left_gap=0),4==top_p&&"div.um-cover"==t?top_p=jQuery(t).height()/2+e.height()/2:4==top_p&&(top_p=jQuery(t).height()+20),gap_right=jQuery(t).width()+17,e.css({top:0,width:200,left:"auto",right:gap_right+"px","text-align":"center"}),e.find(".um-dropdown-arr").find("i").removeClass().addClass("um-icon-arrow-right-b"),e.find(".um-dropdown-arr").css({top:"4px",left:"auto",right:"-17px"})),"bc"==o&&(200>jQuery(t).find("img").width()?left_p=(jQuery(t).width()-jQuery(t).find("img").width())/2+(jQuery(t).find("img").width()-200)/2:left_p=(jQuery(t).width()-jQuery(t).find("img").width())/2,top_=parseInt(jQuery(t).find("a").css("top")),top_?(top_p=jQuery(t).find("img").height()+4+top_,left_gap=4):(top_p=jQuery(t).find("img").height()+4,left_gap=0),4==top_p&&"div.um-cover"==t?top_p=jQuery(t).height()/2+e.height()/2:4==top_p&&(top_p=jQuery(t).height()+20),e.css({top:top_p,width:200,left:left_p+left_gap,right:"auto","text-align":"center"}),e.find(".um-dropdown-arr").find("i").removeClass().addClass("um-icon-arrow-up-b"),e.find(".um-dropdown-arr").css({top:"-17px",left:e.width()/2-12,right:"auto"}))})}function um_responsive(){jQuery(".um").each(function(){element_width=jQuery(this).width(),element_width<=340?(jQuery(this).removeClass("uimob340"),jQuery(this).removeClass("uimob500"),jQuery(this).removeClass("uimob800"),jQuery(this).removeClass("uimob960"),jQuery(this).addClass("uimob340")):element_width<=500?(jQuery(this).removeClass("uimob340"),jQuery(this).removeClass("uimob500"),jQuery(this).removeClass("uimob800"),jQuery(this).removeClass("uimob960"),jQuery(this).addClass("uimob500")):element_width<=800?(jQuery(this).removeClass("uimob340"),jQuery(this).removeClass("uimob500"),jQuery(this).removeClass("uimob800"),jQuery(this).removeClass("uimob960"),jQuery(this).addClass("uimob800")):element_width<=960?(jQuery(this).removeClass("uimob340"),jQuery(this).removeClass("uimob500"),jQuery(this).removeClass("uimob800"),jQuery(this).removeClass("uimob960"),jQuery(this).addClass("uimob960")):element_width>960&&(jQuery(this).removeClass("uimob340"),jQuery(this).removeClass("uimob500"),jQuery(this).removeClass("uimob800"),jQuery(this).removeClass("uimob960")),jQuery(".um-account-nav").length>0&&jQuery(".um-account-side").is(":visible")&&0==jQuery(".um-account-tab:visible").length&&jQuery(".um-account-side li a.current").trigger("click"),jQuery(this).css("opacity",1)}),jQuery(".um-cover, .um-member-cover, .um-cover-e").each(function(){var e=jQuery(this),t=e.data("ratio"),o=e.width(),a=t.split(":");calcHeight=Math.round(o/a[0])+"px",e.height(calcHeight),e.find(".um-cover-add").height(calcHeight)}),jQuery(".um-members").each(function(){UM_Member_Grid(jQuery(this))}),UM_domenus()}function UM_Member_Grid(e){e.masonry({itemSelector:".um-member",columnWidth:".um-member",gutter:".um-gutter-sizer"})}function initImageUpload_UM(e){e.data("upload_help_text")?upload_help_text=''+e.data("upload_help_text")+"":upload_help_text="",e.data("icon")?icon='':icon="",e.data("upload_text")?upload_text=''+e.data("upload_text")+"":upload_text="",e.uploadFile({url:um_scripts.imageupload,method:"POST",multiple:!1,formData:{key:e.data("key"),set_id:e.data("set_id"),set_mode:e.data("set_mode"),_wpnonce:e.data("nonce"),timestamp:e.data("timestamp")},fileName:e.data("key"),allowedTypes:e.data("allowed_types"),maxFileSize:e.data("max_size"),dragDropStr:icon+upload_text+upload_help_text,sizeErrorStr:e.data("max_size_error"),extErrorStr:e.data("extension_error"),maxFileCountErrorStr:e.data("max_files_error"),maxFileCount:1,showDelete:!1,showAbort:!1,showDone:!1,showFileCounter:!1,showStatusAfterSuccess:!0,onSubmit:function(t){e.parents(".um-modal-body").find(".um-error-block").remove()},onSuccess:function(t,o,a){e.selectedFiles=0;try{o=jQuery.parseJSON(o)}catch(e){return void console.log(e,o)}o.error&&""!=o.error?(e.parents(".um-modal-body").append('
';
-
$output .= '
';
-
if (isset( $data['label'] )) {
$output .= $this->field_label( $label, $key, $data );
}
-
$modal_label = ( isset( $data['label'] ) ) ? $data['label'] : __( 'Upload Photo', 'ultimate-member' );
-
$output .= '
';
-
if ($this->field_value( $key, $default, $data )) {
-
- $extension = pathinfo( $this->field_value( $key, $default, $data ), PATHINFO_EXTENSION );
-
- $output .= '
';
-
/* modal hidden */
$output .= '
';
-
$output .= '';
-
$output .= '
';
-
if (isset( $this->set_id )) {
$set_id = $this->set_id;
$set_mode = $this->set_mode;
@@ -2357,7 +2328,6 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
$set_id = 0;
$set_mode = '';
}
-
$output .= '
@@ -2367,11 +2337,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
';
-
$nonce = wp_create_nonce( 'um_upload_nonce-' . $this->timestamp );
-
$output .= '
' . $button_text . '
';
-
$output .= '';
-
$output .= '
';
-
$output .= '
';
-
/* end */
-
if ($this->is_error( $key )) {
$output .= $this->field_error( $this->show_error( $key ) );
}
-
$output .= '
';
-
- break;
+
+ break;
/* Select dropdown */
case 'select':
diff --git a/includes/core/class-files.php b/includes/core/class-files.php
index 7317fc6d..83a9ee35 100644
--- a/includes/core/class-files.php
+++ b/includes/core/class-files.php
@@ -70,11 +70,15 @@ if ( ! class_exists( 'um\core\Files' ) ) {
* Remove file by AJAX
*/
function ajax_remove_file() {
+ UM()->check_frontend_ajax_nonce();
+
/**
* @var $src
*/
extract( $_REQUEST );
$this->delete_file( $src );
+
+ wp_send_json_success();
}
@@ -82,36 +86,112 @@ if ( ! class_exists( 'um\core\Files' ) ) {
* Resize image AJAX handler
*/
function ajax_resize_image() {
- $output = 0;
+ UM()->check_frontend_ajax_nonce();
+ /**
+ * @var $key
+ * @var $src
+ * @var $coord
+ * @var $user_id
+ */
extract( $_REQUEST );
- if ( !isset($src) || !isset($coord) ) die( __('Invalid parameters') );
-
- $coord_n = substr_count($coord, ",");
- if ( $coord_n != 3 ) die( __('Invalid coordinates') );
-
- $um_is_temp_image = um_is_temp_image( $src );
- if ( !$um_is_temp_image ) die( __('Invalid Image file') );
-
- $crop = explode(',', $coord );
- $crop = array_map('intval', $crop);
-
- $uri = UM()->files()->resize_image( $um_is_temp_image, $crop );
-
- // If you're updating a user
- if ( isset( $user_id ) && $user_id > 0 ) {
- $uri = UM()->files()->new_user_upload( $user_id, $um_is_temp_image, $key );
+ if ( ! isset( $src ) || ! isset( $coord ) ) {
+ wp_send_json_error( esc_js( __( 'Invalid parameters', 'ultimate-member' ) ) );
}
- $output = $uri;
+ $coord_n = substr_count( $coord, "," );
+ if ( $coord_n != 3 ) {
+ wp_send_json_error( esc_js( __( 'Invalid coordinates', 'ultimate-member' ) ) );
+ }
+
+
+ $image_path = um_is_file_owner( $src, $user_id, true );
+ if ( ! $image_path ) {
+ wp_send_json_error( esc_js( __( 'Invalid file ownership', 'ultimate-member' ) ) );
+ }
+
+ $output = UM()->uploader()->resize_image( $image_path, $src, $key, $user_id, $coord );
delete_option( "um_cache_userdata_{$user_id}" );
- if(is_array($output)){ print_r($output); }else{ echo $output; } die;
+ wp_send_json_success( $output );
}
+
+ /**
+ * Image upload by AJAX
+ */
+ function ajax_image_upload() {
+ $ret['error'] = null;
+ $ret = array();
+
+ $id = $_POST['key'];
+ $timestamp = $_POST['timestamp'];
+ $nonce = $_POST['_wpnonce'];
+ $user_id = $_POST['user_id'];
+
+ UM()->fields()->set_id = $_POST['set_id'];
+ UM()->fields()->set_mode = $_POST['set_mode'];
+
+
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_image_upload_nonce
+ * @description Change Image Upload nonce
+ * @input_vars
+ * [{"var":"$nonce","type":"bool","desc":"Nonce"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage
+ *
+ * @example
+ *
+ */
+ $um_image_upload_nonce = apply_filters("um_image_upload_nonce", true );
+
+ if( $um_image_upload_nonce ){
+ if ( ! wp_verify_nonce( $nonce, "um_upload_nonce-{$timestamp}" ) && is_user_logged_in() ) {
+ // This nonce is not valid.
+ $ret['error'] = 'Invalid nonce';
+ die( json_encode( $ret ) );
+ }
+ }
+
+ if( isset( $_FILES[ $id ]['name'] ) ) {
+
+ if( ! is_array( $_FILES[ $id ]['name'] ) ) {
+
+ $uploaded = UM()->uploader()->upload_image( $_FILES[ $id ], $user_id, $id );
+ if ( isset( $uploaded['error'] ) ){
+
+ $ret['error'] = $uploaded['error'];
+
+ }else{
+ $ts = current_time( 'timestamp' );
+ $ret[ ] = $uploaded['handle_upload'];
+ }
+
+ }
+
+ } else {
+ $ret['error'] = __('A theme or plugin compatibility issue','ultimate-member');
+ }
+
+
+ echo json_encode($ret);
+ exit;
+ }
+
/**
* Allowed image types
*
@@ -331,6 +411,7 @@ if ( ! class_exists( 'um\core\Files' ) ) {
* @return string
*/
function path_only( $file ) {
+
return trailingslashit( dirname( $file ) );
}
@@ -457,6 +538,7 @@ if ( ! class_exists( 'um\core\Files' ) ) {
* @param $destination
*/
function upload_temp_file( $source, $destination ) {
+
move_uploaded_file( $source, $destination );
}
@@ -553,31 +635,62 @@ if ( ! class_exists( 'um\core\Files' ) ) {
*
* @return mixed
*/
- function get_image_data( $file ) {
+ function get_image_data( $file ) {
- $array['size'] = filesize( $file );
+ $finfo = finfo_open( FILEINFO_MIME_TYPE );
+
+ $mime_type = finfo_file( $finfo, $file );
- $array['image'] = @getimagesize( $file );
+ if( function_exists('exif_imagetype') ){
+
+ $array_exif_image_mimes = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG );
+
+ $allowed_types = apply_filters('um_image_upload_allowed_exif_mimes', $array_exif_image_mimes );
- if ( $array['image'] > 0 ) {
+ if( ! in_array( @exif_imagetype( $file ), $allowed_types ) ) {
+
+ $array['invalid_image'] = true;
- $array['invalid_image'] = false;
+ return $array;
+ }
- list($width, $height, $type, $attr) = @getimagesize( $file );
+ }else{
+
+ $array_image_mimes = array('image/jpeg','image/png','image/gif');
+
+ $allowed_types = apply_filters('um_image_upload_allowed_mimes', $array_image_mimes );
- $array['width'] = $width;
- $array['height'] = $height;
- $array['ratio'] = $width / $height;
+ if ( ! in_array( $mime_type, $allowed_types ) ) {
- $array['extension'] = $this->get_extension_by_mime_type( $array['image']['mime'] );
+ $array['invalid_image'] = true;
- } else {
-
- $array['invalid_image'] = true;
+ return $array;
+ }
}
+ $array['size'] = filesize( $file );
+
+ $image_data = @getimagesize( $file );
+
+ $array['image'] = $image_data;
+
+ $array['invalid_image'] = false;
+
+ list($width, $height, $type, $attr) = $image_data;
+
+ $array['width'] = $width;
+
+ $array['height'] = $height;
+
+ $array['ratio'] = $width / $height;
+
+ $array['extension'] = $this->get_extension_by_mime_type( $mime_type );
+
+
return $array;
+
+
}
@@ -589,7 +702,7 @@ if ( ! class_exists( 'um\core\Files' ) ) {
*
* @return null|string|void
*/
- function check_image_upload( $file, $field ) {
+ function check_image_upload( $file, $field, $stream_photo = false ) {
$error = null;
$fileinfo = $this->get_image_data( $file );
@@ -667,8 +780,6 @@ if ( ! class_exists( 'um\core\Files' ) ) {
if ( $fileinfo['invalid_image'] == true ) {
$error = sprintf(__('Your image is invalid or too large!','ultimate-member') );
- } elseif ( isset( $data['allowed_types'] ) && !$this->in_array( $fileinfo['extension'], $data['allowed_types'] ) ) {
- $error = ( isset( $data['extension_error'] ) && !empty( $data['extension_error'] ) ) ? $data['extension_error'] : 'not allowed';
} elseif ( isset($data['min_size']) && ( $fileinfo['size'] < $data['min_size'] ) ) {
$error = $data['min_size_error'];
} elseif ( isset($data['min_width']) && ( $fileinfo['width'] < $data['min_width'] ) ) {
@@ -696,9 +807,7 @@ if ( ! class_exists( 'um\core\Files' ) ) {
$fileinfo = $this->get_file_data( $file );
$data = UM()->fields()->get_field( $field );
- if ( !$this->in_array( $extension, $data['allowed_types'] ) ) {
- $error = ( isset( $data['extension_error'] ) && !empty( $data['extension_error'] ) ) ? $data['extension_error'] : 'not allowed';
- } elseif ( isset($data['min_size']) && ( $fileinfo['size'] < $data['min_size'] ) ) {
+ if ( isset($data['min_size']) && ( $fileinfo['size'] < $data['min_size'] ) ) {
$error = $data['min_size_error'];
}
@@ -1146,81 +1255,6 @@ if ( ! class_exists( 'um\core\Files' ) ) {
}
- /**
- * Image upload by AJAX
- */
- function ajax_image_upload() {
- $ret['error'] = null;
- $ret = array();
-
- $id = $_POST['key'];
- $timestamp = $_POST['timestamp'];
- $nonce = $_POST['_wpnonce'];
-
- UM()->fields()->set_id = $_POST['set_id'];
- UM()->fields()->set_mode = $_POST['set_mode'];
-
- /**
- * UM hook
- *
- * @type filter
- * @title um_image_upload_nonce
- * @description Change Image Upload nonce
- * @input_vars
- * [{"var":"$nonce","type":"bool","desc":"Nonce"}]
- * @change_log
- * ["Since: 2.0"]
- * @usage
- *
- * @example
- *
- */
- $um_image_upload_nonce = apply_filters("um_image_upload_nonce", true );
-
- if( $um_image_upload_nonce ){
- if ( ! wp_verify_nonce( $nonce, 'um_upload_nonce-'.$timestamp ) && is_user_logged_in() ) {
- // This nonce is not valid.
- $ret['error'] = 'Invalid nonce';
- die( json_encode( $ret ) );
- }
- }
-
- if(isset($_FILES[$id]['name'])) {
-
- if(!is_array($_FILES[$id]['name'])) {
-
- $temp = $_FILES[$id]["tmp_name"];
- $file = $id."-".$_FILES[$id]["name"];
- $file = sanitize_file_name($file);
- $ext = strtolower( pathinfo($file, PATHINFO_EXTENSION) );
-
- $error = UM()->files()->check_image_upload( $temp, $id );
- if ( $error ){
-
- $ret['error'] = $error;
-
- } else {
- $file = "stream_photo_".md5($file)."_".uniqid().".".$ext;
- $ret[ ] = UM()->files()->new_image_upload_temp( $temp, $file, UM()->options()->get('image_compression') );
-
- }
-
- }
-
- } else {
- $ret['error'] = __('A theme or plugin compatibility issue','ultimate-member');
- }
- echo json_encode($ret);
- exit;
- }
-
-
/**
*
*/
@@ -1265,62 +1299,44 @@ if ( ! class_exists( 'um\core\Files' ) ) {
$um_file_upload_nonce = apply_filters("um_file_upload_nonce", true );
if ( $um_file_upload_nonce ) {
- if ( ! wp_verify_nonce( $nonce, 'um_upload_nonce-'.$timestamp ) && is_user_logged_in()) {
+ if ( ! wp_verify_nonce( $nonce, 'um_upload_nonce-'.$timestamp ) && is_user_logged_in() ) {
// This nonce is not valid.
$ret['error'] = 'Invalid nonce';
die( json_encode( $ret ) );
}
}
- if(isset($_FILES[$id]['name'])) {
- if(!is_array($_FILES[$id]['name'])) {
+ if( isset( $_FILES[ $id ]['name'] ) ) {
- $temp = $_FILES[$id]["tmp_name"];
- /**
- * UM hook
- *
- * @type filter
- * @title um_upload_file_name
- * @description Change File Upload nonce
- * @input_vars
- * [{"var":"$filename","type":"string","desc":"Filename"},
- * {"var":"$id","type":"int","desc":"ID"},
- * {"var":"$name","type":"string","desc":"Name"}]
- * @change_log
- * ["Since: 2.0"]
- * @usage
- *
- * @example
- *
- */
- $file = apply_filters( 'um_upload_file_name', $id . "-" . $_FILES[$id]["name"], $id, $_FILES[$id]["name"] );
- $file = sanitize_file_name($file);
- $extension = strtolower( pathinfo($file, PATHINFO_EXTENSION) );
+ if( ! is_array( $_FILES[ $id ]['name'] ) ) {
+
+ $uploaded = UM()->uploader()->upload_file( $_FILES[ $id ], $user_id, $id );
+ if ( isset( $uploaded['error'] ) ){
+
+ $ret['error'] = $uploaded['error'];
+
+ }else{
+
+ $uploaded_file = $uploaded['handle_upload'];
+ $ret['url'] = $uploaded_file['file_info']['name'];
+ $ret['icon'] = UM()->files()->get_fonticon_by_ext( $uploaded_file['file_info']['ext'] );
+ $ret['icon_bg'] = UM()->files()->get_fonticon_bg_by_ext( $uploaded_file['file_info']['ext'] );
+ $ret['filename'] = $uploaded_file['file_info']['basename'];
+ $ret['original_name'] = $uploaded_file['file_info']['original_name'];
+
- $error = UM()->files()->check_file_upload( $temp, $extension, $id );
- if ( $error ){
- $ret['error'] = $error;
- } else {
- $ret[] = UM()->files()->new_file_upload_temp( $temp, $file );
- $ret['icon'] = UM()->files()->get_fonticon_by_ext( $extension );
- $ret['icon_bg'] = UM()->files()->get_fonticon_bg_by_ext( $extension );
- $ret['filename'] = $file;
}
}
} else {
- $ret['error'] = __('A theme or plugin compatibility issue','ultimate-member');
+ $ret['error'] = __('A theme or plugin compatibility issue','ultimate-member');
}
+
echo json_encode($ret);
exit;
}
+
}
}
\ No newline at end of file
diff --git a/includes/core/class-uploader.php b/includes/core/class-uploader.php
new file mode 100644
index 00000000..617f65a1
--- /dev/null
+++ b/includes/core/class-uploader.php
@@ -0,0 +1,1060 @@
+core_upload_dir = "/ultimatemember/";
+ $this->user_id = get_current_user_id();
+ $this->upload_image_type = 'stream_photo';
+ $this->wp_upload_dir = wp_upload_dir();
+ $this->temp_upload_dir = "temp";
+
+ add_filter("upload_dir", array( $this, "set_upload_directory" ), 10, 1 );
+ add_filter("wp_handle_upload_prefilter", array( $this, "validate_upload" ) );
+ add_filter("um_upload_image_process__profile_photo", array( $this, "profile_photo" ), 10, 6 );
+ add_filter("um_upload_image_process__cover_photo", array( $this, "cover_photo" ), 10, 6 );
+ add_filter("um_upload_stream_image_process", array( $this, "stream_photo" ), 10, 6 );
+ add_filter("um_custom_image_handle_wall_img_upload", array( $this, "stream_photo_data"), 10, 1 );
+
+
+ }
+
+ /**
+ * Get core temporary directory path
+ *
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_core_temp_dir(){
+
+ return $this->get_upload_base_dir(). $this->temp_upload_dir;
+ }
+
+ /**
+ * Get core temporary directory URL
+ *
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_core_temp_url(){
+
+ return $this->get_upload_base_url(). $this->temp_upload_dir;
+ }
+
+ /**
+ * Get core upload directory
+ *
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_core_upload_dir(){
+
+ return $this->core_upload_dir;
+
+ }
+
+ /**
+ * Get core upload base url
+ *
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_upload_base_url(){
+
+ $wp_baseurl = $this->wp_upload_dir['baseurl'];
+
+ $this->upload_baseurl = $wp_baseurl . $this->core_upload_dir;
+
+ return $this->upload_baseurl;
+
+ }
+
+ /**
+ * Get core upload base directory
+ *
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_upload_base_dir(){
+
+ $wp_basedir = $this->wp_upload_dir['basedir'];
+
+ $this->upload_basedir = $wp_basedir . $this->core_upload_dir;
+
+ return $this->upload_basedir;
+ }
+
+ /**
+ * Get user upload base directory
+ *
+ * @param integer $user_id
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_upload_user_base_dir( $user_id = null, $create_dir = false){
+
+ if( $user_id ){
+ $this->user_id = $user_id;
+ }
+
+ $this->upload_user_basedir = $this->get_upload_base_dir() . $this->user_id;
+
+ if( $create_dir ){
+ wp_mkdir_p( $this->upload_user_basedir );
+ }
+
+ return $this->upload_user_basedir;
+
+ }
+
+ /**
+ * Get user upload base url
+ *
+ * @param integer $user_id
+ * @since 2.0.22
+ * @return string
+ */
+ public function get_upload_user_base_url( $user_id = null ){
+
+ if( $user_id ){
+ $this->user_id = $user_id;
+ }
+
+ $this->upload_user_baseurl = $this->get_upload_base_url() . $this->user_id;
+
+ return $this->upload_user_baseurl;
+
+ }
+
+ /**
+ * Validate file size
+ * @param array $file
+ * @return array
+ */
+ public function validate_upload( $file ){
+
+
+ if( 'image' == $this->upload_type ){
+
+ $error = $this->validate_image_data( $file['tmp_name'], $this->field_key );
+
+ }else if( 'file' == $this->upload_type ){
+
+ $error = $this->validate_file_data( $file['tmp_name'], $this->field_key );
+
+ }
+
+ if( $error ){
+ $file['error'] = $error;
+ }
+
+ return $file;
+ }
+
+ /**
+ * Set upload directory
+ * @param array $args
+ */
+ public function set_upload_directory( $args ){
+
+ $this->upload_baseurl = $args['baseurl'] . $this->core_upload_dir;
+ $this->upload_basedir = $args['basedir'] . $this->core_upload_dir;
+
+ if( 'image' == $this->upload_type && is_user_logged_in() ){
+ $this->upload_user_baseurl = $this->upload_baseurl . $this->user_id;
+ $this->upload_user_basedir = $this->upload_basedir . $this->user_id;
+ }else{
+ $this->upload_user_baseurl = $this->upload_baseurl . $this->temp_upload_dir;
+ $this->upload_user_basedir = $this->upload_basedir . $this->temp_upload_dir;
+ }
+
+ $args['path'] = $this->upload_user_basedir;
+ $args['url'] = $this->upload_user_baseurl;
+
+ return $args;
+ }
+
+ /**
+ * Upload Image files
+ * @param string $uploadedfile
+ * @param integer $user_id
+ * @param string $field_key
+ * @param string $upload_type
+ *
+ * @since 2.0.22
+ */
+ public function upload_image( $uploadedfile, $user_id = null, $field_key = '', $upload_type = 'stream_photo' ){
+
+
+ $response = array();
+
+ if ( ! function_exists( 'wp_handle_upload' ) ) {
+ require_once( ABSPATH . 'wp-admin/includes/file.php' );
+ }
+
+ $this->field_key = $field_key;
+
+ $this->upload_type = 'image';
+
+ $this->upload_image_type = $upload_type;
+
+ if( $user_id && is_user_logged_in() ){
+ $this->user_id = $user_id;
+ }
+
+ if( in_array( $field_key, array( 'profile_photo','cover_photo' ) ) ){
+ $this->upload_image_type = $field_key;
+ }
+
+ $field_data = UM()->fields()->get_field( $field_key );
+
+ $field_allowed_file_types = explode(",", $field_data['allowed_types'] );
+
+ $allowed_image_mimes = array();
+
+ foreach( $field_allowed_file_types as $a ){
+ $atype = wp_check_filetype( "test.{$a}" );
+ $allowed_image_mimes[ $atype['ext'] ] = $atype['type'];
+ }
+
+ $image_compression = UM()->options()->get('image_compression');
+
+ $upload_overrides = array(
+ 'test_form' => false,
+ 'mimes' => apply_filters( "um_uploader_allowed_image_mimes", $allowed_image_mimes ),
+ 'unique_filename_callback' => array( $this, "unique_filename"),
+ );
+
+ $upload_overrides = apply_filters( "um_image_upload_handler_overrides__{$field_key}", $upload_overrides );
+
+ $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
+
+ if ( isset( $movefile['error'] ) ) {
+ /*
+ * Error generated by _wp_handle_upload()
+ * @see _wp_handle_upload() in wp-admin/includes/file.php
+ */
+ $response['error'] = $movefile['error'];
+ }else{
+
+ $movefile['file'] = wp_basename( $movefile['file'] );
+
+ $file_type = wp_check_filetype( $movefile['file'] );
+
+ $movefile['file_info']['name'] = $movefile['url'];
+ $movefile['file_info']['original_name'] = $uploadedfile['name'];
+ $movefile['file_info']['basename'] = wp_basename( $movefile['file'] );
+ $movefile['file_info']['ext'] = $file_type['ext'];
+ $movefile['file_info']['type'] = $file_type['type'];
+ $movefile['file_info']['size'] = filesize( $movefile['file'] );
+ $movefile['file_info']['size_format'] = size_format( $movefile['file_info']['size'] );
+
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_before_upload_db_meta
+ * @description Update user's meta before upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"},
+ * {"var":"$key","type":"string","desc":"Meta key"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_before_upload_db_meta', 'function_name', 10, 2 );
+ * @example
+ *
+ */
+ do_action( 'um_before_upload_db_meta', $this->user_id, $field_key );
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_before_upload_db_meta_{$key}
+ * @description Update user's meta before upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_before_upload_db_meta_{$key}', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ do_action( "um_before_upload_db_meta_{$field_key}", $this->user_id );
+
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_after_upload_db_meta
+ * @description Update user's meta before upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"},
+ * {"var":"$key","type":"string","desc":"Meta key"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_after_upload_db_meta', 'function_name', 10, 2 );
+ * @example
+ *
+ */
+ do_action( 'um_after_upload_db_meta', $this->user_id, $field_key );
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_after_upload_db_meta_{$key}
+ * @description Update user's meta after upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_after_upload_db_meta_{$key}', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ do_action( "um_after_upload_db_meta_{$field_key}", $this->user_id );
+
+ update_user_meta( $this->user_id, $field_key, wp_basename( $movefile['url'] ) );
+
+ $filename = wp_basename( $movefile['url'] );
+
+ set_transient( "um_{$filename}", $movefile['file_info'], 2 * HOUR_IN_SECONDS );
+
+
+ }
+
+ $response['handle_upload'] = $movefile;
+
+ return $response;
+
+ }
+
+ /**
+ * Upload Files
+ * @param string $uploadedfile
+ * @param integer $user_id
+ * @param string $field_key
+ *
+ * @since 2.0.22
+ */
+ public function upload_file( $uploadedfile, $user_id = null, $field_key = '' ){
+
+
+ $response = array();
+
+ if ( ! function_exists( 'wp_handle_upload' ) ) {
+ require_once( ABSPATH . 'wp-admin/includes/file.php' );
+ }
+
+ $this->field_key = $field_key;
+
+ if( $user_id && is_user_logged_in() ){
+ $this->user_id = $user_id;
+ }
+
+ $this->upload_type = 'file';
+
+ $field_data = UM()->fields()->get_field( $field_key );
+
+ $field_allowed_file_types = explode(",", $field_data['allowed_types'] );
+
+ $allowed_file_mimes = array();
+
+ foreach( $field_allowed_file_types as $a ){
+ $atype = wp_check_filetype( "test.{$a}" );
+ $allowed_file_mimes[ $atype['ext'] ] = $atype['type'];
+ }
+
+ $upload_overrides = array(
+ 'test_form' => false,
+ 'mimes' => apply_filters( "um_uploader_allowed_file_mimes", $allowed_file_mimes ),
+ 'unique_filename_callback' => array( $this, "unique_filename"),
+ );
+
+ $upload_overrides = apply_filters( "um_file_upload_handler_overrides__{$field_key}", $upload_overrides );
+
+ $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
+
+ if ( isset( $movefile['error'] ) ) {
+ /*
+ * Error generated by _wp_handle_upload()
+ * @see _wp_handle_upload() in wp-admin/includes/file.php
+ */
+ $response['error'] = $movefile['error'];
+ }else{
+
+ $file_type = wp_check_filetype( $movefile['file'] );
+
+ $movefile['file_info']['name'] = $movefile['url'];
+ $movefile['file_info']['original_name'] = $uploadedfile['name'];
+ $movefile['file_info']['basename'] = wp_basename( $movefile['file'] );
+ $movefile['file_info']['ext'] = $file_type['ext'];
+ $movefile['file_info']['type'] = $file_type['type'];
+ $movefile['file_info']['size'] = filesize( $movefile['file'] );
+ $movefile['file_info']['size_format'] = size_format( $movefile['file_info']['size'] );
+
+
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_before_upload_db_meta
+ * @description Update user's meta before upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"},
+ * {"var":"$key","type":"string","desc":"Meta key"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_before_upload_db_meta', 'function_name', 10, 2 );
+ * @example
+ *
+ */
+ do_action( 'um_before_upload_db_meta', $this->user_id, $field_key );
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_before_upload_db_meta_{$key}
+ * @description Update user's meta before upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_before_upload_db_meta_{$key}', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ do_action( "um_before_upload_db_meta_{$field_key}", $this->user_id );
+
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_after_upload_db_meta
+ * @description Update user's meta before upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"},
+ * {"var":"$key","type":"string","desc":"Meta key"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_after_upload_db_meta', 'function_name', 10, 2 );
+ * @example
+ *
+ */
+ do_action( 'um_after_upload_db_meta', $this->user_id, $field_key );
+ /**
+ * UM hook
+ *
+ * @type action
+ * @title um_after_upload_db_meta_{$key}
+ * @description Update user's meta after upload
+ * @input_vars
+ * [{"var":"$user_id","type":"int","desc":"User ID"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_action( 'um_after_upload_db_meta_{$key}', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ do_action( "um_after_upload_db_meta_{$field_key}", $this->user_id );
+
+ update_user_meta( $this->user_id, $field_key, wp_basename( $movefile['url'] ) );
+
+ $filename = wp_basename( $movefile['url'] );
+
+ set_transient( "um_{$filename}", $movefile['file_info'], 2 * HOUR_IN_SECONDS );
+
+
+ }
+
+ $response['handle_upload'] = $movefile;
+
+ return $response;
+
+ }
+
+ /**
+ * Check image upload and handle errors
+ *
+ * @param $file
+ * @param $field
+ *
+ * @return null|string|void
+ */
+ public function validate_image_data( $file, $field_key ){
+ $error = null;
+
+ if ( ! function_exists( 'wp_get_image_editor' ) ) {
+ require_once( ABSPATH . 'wp-admin/includes/media.php' );
+ }
+
+ $image = wp_get_image_editor( $file );
+ $image_sizes = $image->get_size();
+
+ $image_info['width'] = $image_sizes['width'];
+ $image_info['height'] = $image_sizes['height'];
+ $image_info['ratio'] = $image_sizes['width'] / $image_sizes['height'];
+
+ $image_info['quality'] = $image->get_quality();
+
+ $image_type = wp_check_filetype( $file );
+ $image_info['extension'] = $image_type['ext'];
+ $image_info['mime']= $image_type['type'];
+ $image_info['size'] = filesize( $file );
+
+
+ $data = UM()->fields()->get_field( $field_key );
+
+ if ( $data == null ) {
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_custom_image_handle_{$field}
+ * @description Custom image handle
+ * @input_vars
+ * [{"var":"$data","type":"array","desc":"Image Data"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_filter( 'um_custom_image_handle_{$field}', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ $data = apply_filters( "um_custom_image_handle_{$field_key}", array() );
+ if ( ! $data ) {
+ $error = __( 'This media type is not recognized.', 'ultimate-member' );
+ }
+ }
+
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_image_handle_global__option
+ * @description Custom image global handle
+ * @input_vars
+ * [{"var":"$data","type":"array","desc":"Image Data"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_filter( 'um_image_handle_global__option', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ $data = apply_filters("um_image_handle_global__option", $data );
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_image_handle_{$field}__option
+ * @description Custom image handle for each $field
+ * @input_vars
+ * [{"var":"$data","type":"array","desc":"Image Data"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_filter( 'um_image_handle_{$field}__option', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ $data = apply_filters( "um_image_handle_{$field_key}__option", $data );
+
+ if ( $image_info['invalid_image'] == true ) {
+ $error = sprintf(__('Your image is invalid or too large!','ultimate-member') );
+ } elseif ( isset($data['min_size']) && ( $image_info['size'] < $data['min_size'] ) ) {
+ $error = $data['min_size_error'];
+ } elseif ( isset($data['max_file_size']) && ( $image_info['size'] > $data['max_file_size'] ) ) {
+ $error = $data['max_file_size_error'];
+ } elseif ( isset($data['min_width']) && ( $image_info['width'] < $data['min_width'] ) ) {
+ $error = sprintf(__('Your photo is too small. It must be at least %spx wide.','ultimate-member'), $data['min_width']);
+ } elseif ( isset($data['min_height']) && ( $image_info['height'] < $data['min_height'] ) ) {
+ $error = sprintf(__('Your photo is too small. It must be at least %spx wide.','ultimate-member'), $data['min_height']);
+ }
+
+
+ return $error;
+
+ }
+
+ /**
+ * Check file upload and handle errors
+ *
+ * @param $file
+ * @param $field
+ *
+ * @return null|string|void
+ */
+ public function validate_file_data( $file, $field_key ){
+ $error = null;
+
+ if ( ! function_exists( 'wp_get_image_editor' ) ) {
+ require_once( ABSPATH . 'wp-admin/includes/media.php' );
+ }
+
+ $file_type = wp_check_filetype( $file );
+ $file_info = array();
+ $file_info['extension'] = $file_type['ext'];
+ $file_info['mime']= $file_type['type'];
+ $file_info['size'] = filesize( $file );
+
+ $data = UM()->fields()->get_field( $field_key );
+
+ if ( $data == null ) {
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_custom_file_handle_{$field}
+ * @description Custom file handle
+ * @input_vars
+ * [{"var":"$data","type":"array","desc":"Image Data"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_filter( 'um_custom_file_handle_{$field}', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ $data = apply_filters( "um_custom_file_handle_{$field_key}", array() );
+ if ( ! $data ) {
+ $error = __( 'This file type is not recognized.', 'ultimate-member' );
+ }
+ }
+
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_file_handle_global__option
+ * @description Custom file global handle
+ * @input_vars
+ * [{"var":"$data","type":"array","desc":"Image Data"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_filter( 'um_file_handle_global__option', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ $data = apply_filters("um_file_handle_global__option", $data );
+ /**
+ * UM hook
+ *
+ * @type filter
+ * @title um_file_handle_{$field}__option
+ * @description Custom file handle for each $field
+ * @input_vars
+ * [{"var":"$data","type":"array","desc":"Image Data"}]
+ * @change_log
+ * ["Since: 2.0"]
+ * @usage add_filter( 'um_file_handle_{$field}__option', 'function_name', 10, 1 );
+ * @example
+ *
+ */
+ $data = apply_filters( "um_file_handle_{$field_key}__option", $data );
+
+ if ( $image_info['invalid_file'] == true ) {
+ $error = sprintf(__('Your file is invalid or too large!','ultimate-member') );
+ } elseif ( isset($data['max_file_size']) && ( $image_info['size'] > $data['max_file_size'] ) ) {
+ $error = $data['max_file_size_error'];
+ }
+
+
+ return $error;
+
+ }
+
+
+
+ /**
+ * Make unique filename
+ * @param string $filename
+ * @param string $ext
+ * @param string $dir
+ * @return string $filename
+ *
+ * @since 2.0.22
+ */
+ public function unique_filename( $filename, $ext, $dir ){
+
+ $image_type = wp_check_filetype( $ext );
+
+ $ext = $image_type['ext'];
+
+ if( 'image' == $this->upload_type ){
+
+ switch( $this->upload_image_type ){
+
+ case 'stream_photo':
+ $hashed = hash('ripemd160', time(). mt_rand(10,1000) );
+ $filename = "stream_photo_{$hashed}.{$ext}";
+ break;
+
+ case 'profile_photo':
+ $filename = "profile_photo.{$ext}";
+ break;
+
+ case 'cover_photo':
+ $filename = "cover_photo.{$ext}";
+ break;
+
+ }
+
+ }else if( 'file' == $this->upload_type ){
+ $hashed = hash('ripemd160', time(). mt_rand(10,1000) );
+ $filename = "file_{$hashed}.{$ext}";
+
+ }
+
+ $this->delete_existing_file( $filename, $ext, $dir );
+
+ return $filename;
+ }
+
+ /**
+ * Delete file
+ * @param string $filename
+ * @param string $ext
+ * @param string $dir
+ *
+ * @since 2.0.22
+ */
+ public function delete_existing_file( $filename, $ext, $dir ){
+
+ if( file_exists( $this->upload_user_basedir."/".$filename ) && ! empty( $filename ) ){
+ unlink( $this->upload_user_basedir."/".$filename );
+ }
+
+ }
+
+ /**
+ * Profile photo image process
+ * @param string $src
+ * @param integer $user_id
+ * @param string $coord
+ * @param array $crop
+ *
+ * @since 2.0.22
+ */
+ public function profile_photo( $image_path, $src, $key, $user_id, $coord, $crop ){
+
+ $sizes = UM()->options()->get( 'photo_thumb_sizes' );
+
+ $quality = UM()->options()->get( 'image_compression' );
+
+ $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor
+
+ if ( ! is_wp_error( $image ) ) {
+
+ $src_x = $crop[0];
+ $src_y = $crop[1];
+ $src_w = $crop[2];
+ $src_h = $crop[3];
+
+ $image->crop( $src_x, $src_y, $src_w, $src_h );
+
+ $image->save( $image_path );
+
+ $image->set_quality( $quality );
+
+ $sizes_array = array();
+
+ foreach( $sizes as $size ){
+ $sizes_array[ ] = array ('width' => $size );
+ }
+
+ $image->multi_resize( $sizes_array );
+
+ delete_user_meta( $user_id, 'synced_profile_photo' );
+
+ }else{
+
+ wp_send_json_error( esc_js( __( "Unable to crop image file: {$src}", 'ultimate-member' ) ) );
+
+ }
+
+ }
+
+
+ /**
+ * Cover photo image process
+ * @param string $src
+ * @param integer $user_id
+ * @param string $coord
+ * @param array $crop
+ *
+ * @since 2.0.22
+ */
+ public function cover_photo( $image_path, $src, $key, $user_id, $coord, $crop ){
+
+ $sizes = UM()->options()->get( 'cover_thumb_sizes' );
+
+ $quality = UM()->options()->get( 'image_compression' );
+
+ $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor
+
+ if ( ! is_wp_error( $image ) ) {
+
+ $src_x = $crop[0];
+ $src_y = $crop[1];
+ $src_w = $crop[2];
+ $src_h = $crop[3];
+
+ $image->crop( $src_x, $src_y, $src_w, $src_h );
+
+ $image->save( $image_path );
+
+ $image->set_quality( $quality );
+
+ $sizes_array = array();
+
+ foreach( $sizes as $size ){
+ $sizes_array[ ] = array ('width' => $size );
+ }
+
+ $image->multi_resize( $sizes_array );
+
+ }else{
+
+ wp_send_json_error( esc_js( __( "Unable to crop image file: {$src}", 'ultimate-member' ) ) );
+
+ }
+
+ }
+
+ /**
+ * Stream photo image process
+ * @param string $src
+ * @param integer $user_id
+ * @param string $coord
+ * @param array $crop
+ *
+ * @since 2.0.22
+ */
+ public function stream_photo( $image_path, $src, $key, $user_id, $coord, $crop ){
+
+ $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor
+
+ $quality = UM()->options()->get( 'image_compression' );
+
+ if ( ! is_wp_error( $image ) ) {
+
+ if( ! empty( $crop ) ){
+
+ if( ! is_array( $crop ) ){
+ $crop = explode(",", $crop );
+ }
+
+ $src_x = $crop[0];
+ $src_y = $crop[1];
+ $src_w = $crop[2];
+ $src_h = $crop[3];
+
+ $image->crop( $src_x, $src_y, $src_w, $src_h );
+ }
+
+ $image->save( $image_path );
+
+ $image->set_quality( $quality );
+
+ }else{
+
+ wp_send_json_error( esc_js( __( "Unable to crop stream image file: {$image_path}", 'ultimate-member' ) ) );
+
+ }
+
+ }
+
+ /**
+ * Set stream photo default settings
+ * @param array $args
+ * @return array
+ *
+ * @since 2.0.22
+ */
+ public function stream_photo_data( $args ){
+
+ $args['max_file_size'] = apply_filters("um_upload_images_stream_maximum_file_size", 9999999 );
+ $args['max_file_size_error'] = sprintf(__("Maximum file size allowed: ".size_format( $args['max_file_size'] ),'ultimate-member') );
+
+ return $args;
+ }
+
+ /**
+ * Resize Image
+ * @param string $image_path
+ * @param string $src
+ * @param string $key
+ * @param integer $user_id
+ * @param string $coord
+ * @return string $src
+ *
+ * @since 2.0.22
+ */
+ public function resize_image( $image_path, $src, $key, $user_id, $coord ){
+
+ $crop = explode( ',', $coord );
+ $crop = array_map( 'intval', $crop );
+
+ do_action("um_upload_image_process__{$key}", $image_path, $src, $key, $user_id, $coord, $crop );
+
+ if( ! in_array( $key, array('profile_photo','cover_photo') ) ){
+ do_action("um_upload_stream_image_process", $image_path, $src, $key, $user_id, $coord, $crop );
+ }
+
+ $ret = array();
+ $ret['image']['source_url'] = $src;
+ $ret['image']['source_path'] = $image_path;
+ $ret['image']['filename'] = wp_basename( $image_path );
+
+ return $ret;
+
+ }
+
+
+ }
+
+}
\ No newline at end of file
diff --git a/includes/core/class-user.php b/includes/core/class-user.php
index 5904c37f..1aea2867 100644
--- a/includes/core/class-user.php
+++ b/includes/core/class-user.php
@@ -1885,5 +1885,42 @@ if ( ! class_exists( 'um\core\User' ) ) {
return $hash_email_address;
}
+
+ /**
+ * Move temporary files
+ *
+ * @param array $user_id
+ * @param array $files
+ */
+ function move_temporary_files( $user_id, $files ){
+ foreach ( $files as $key => $filename ) {
+ if( empty( $filename ) ) continue;
+ $user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id, true );
+
+ $temp_file_path = UM()->uploader()->get_core_temp_dir() . "/" . $filename;
+
+ if( file_exists( $temp_file_path ) ){
+
+
+ $extra_hash = hash( 'crc32b', current_time('timestamp') );
+
+ if ( strpos( $filename , 'stream_photo_' ) !== false ) {
+ $new_filename = str_replace("stream_photo_","stream_photo_{$extra_hash}_", $filename );
+ }else{
+ $new_filename = str_replace("file_","file_{$extra_hash}_", $filename );
+ }
+ $file = $user_basedir. "/" . $new_filename;
+ if( rename( $temp_file_path, $file ) ){
+ $file_info = get_transient("um_{$filename}");
+ update_user_meta( $user_id, $key, $new_filename );
+ update_user_meta( $user_id, "{$key}_metadata", $file_info );
+ delete_transient("um_{$filename}");
+ }
+ }
+
+ }
+ }
+
+
}
}
\ No newline at end of file
diff --git a/includes/core/um-actions-profile.php b/includes/core/um-actions-profile.php
index 2af4ac9e..268ce27c 100644
--- a/includes/core/um-actions-profile.php
+++ b/includes/core/um-actions-profile.php
@@ -234,7 +234,7 @@ function um_user_edit_profile( $args ) {
if ( isset( $args['submitted'][ $key ] ) ) {
if ( isset( $fields[ $key ]['type'] ) && in_array( $fields[ $key ]['type'], array( 'image', 'file' ) ) &&
- ( um_is_temp_upload( $args['submitted'][ $key ] ) || $args['submitted'][ $key ] == 'empty_file' ) ) {
+ ( um_is_temp_file( $args['submitted'][ $key ] ) || $args['submitted'][ $key ] == 'empty_file' ) ) {
$files[ $key ] = $args['submitted'][ $key ];
@@ -391,7 +391,11 @@ function um_user_edit_profile( $args ) {
* ?>
*/
do_action( 'um_before_user_upload', um_user( 'ID' ), $files );
- UM()->user()->update_files( $files );
+
+ //UM()->user()->update_files( $files );
+
+ UM()->user()->move_temporary_files( um_user( 'ID' ), $files );
+
/**
* UM hook
*
diff --git a/includes/core/um-actions-register.php b/includes/core/um-actions-register.php
index addab9b4..b1e35e96 100644
--- a/includes/core/um-actions-register.php
+++ b/includes/core/um-actions-register.php
@@ -637,7 +637,7 @@ function um_registration_save_files( $user_id, $args ) {
if ( isset( $args['submitted'][$key] ) ) {
if ( isset( $fields[$key]['type'] ) && in_array( $fields[$key]['type'], array( 'image', 'file' ) ) &&
- ( um_is_temp_upload( $args['submitted'][$key] ) || $args['submitted'][$key] == 'empty_file' )
+ ( um_is_temp_file( $args['submitted'][$key] ) || $args['submitted'][$key] == 'empty_file' )
) {
$files[$key] = $args['submitted'][$key];
@@ -692,7 +692,11 @@ function um_registration_save_files( $user_id, $args ) {
* ?>
*/
do_action( 'um_before_user_upload', $user_id, $files );
- UM()->user()->update_files( $files );
+
+ //UM()->user()->update_files( $files );
+
+ UM()->user()->move_temporary_files( $user_id, $files );
+
/**
* UM hook
*
diff --git a/includes/core/um-filters-fields.php b/includes/core/um-filters-fields.php
index 089fdf4c..767dceb3 100644
--- a/includes/core/um-filters-fields.php
+++ b/includes/core/um-filters-fields.php
@@ -224,21 +224,25 @@ add_filter( 'um_profile_field_filter_hook__date', 'um_profile_field_filter_hook_
*/
function um_profile_field_filter_hook__file( $value, $data ) {
$uri = um_user_uploads_uri() . $value;
- $extension = pathinfo( $uri, PATHINFO_EXTENSION);
-
- if ( !file_exists( um_user_uploads_dir() . $value ) ) {
- $value = __('This file has been removed.');
+ $file_type = wp_check_filetype( $value );
+
+ if ( ! file_exists( um_user_uploads_dir() . $value ) ) {
+ $value = __('This file has been removed.','ultimate-member');
} else {
+ $file_info = um_user( $data['metakey']."_metadata" );
+ if( isset( $file_info['original_name'] ) && ! empty( $file_info['original_name'] ) ){
+ $value = $file_info['original_name'];
+ }
$value = '
';
}
-
+
return $value;
}
add_filter( 'um_profile_field_filter_hook__file', 'um_profile_field_filter_hook__file', 99, 2 );
diff --git a/includes/um-short-functions.php b/includes/um-short-functions.php
index cf218dd0..6c299d94 100644
--- a/includes/um-short-functions.php
+++ b/includes/um-short-functions.php
@@ -774,6 +774,63 @@ function um_is_temp_image( $url ) {
}
+/**
+ * Check user's file ownership
+ * @param string $url
+ * @param integer $user_id
+ * @return bool
+ */
+function um_is_file_owner( $url, $user_id = null, $image_path = false ){
+
+ if( strpos( $url, "/uploads/ultimatemember/{$user_id}/" ) !== false && is_user_logged_in() ){
+
+ $user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id );
+
+ $filename = wp_basename( parse_url( $url, PHP_URL_PATH ) );
+
+ $file = $user_basedir . '/' . $filename;
+ if( file_exists( $file ) ){
+ if( $image_path ){
+ return $file;
+ }
+
+ return true;
+ }
+ }else{
+ $user_basedir = UM()->uploader()->get_upload_user_base_dir( 'temp' );
+
+ $filename = wp_basename( parse_url( $url, PHP_URL_PATH ) );
+
+ $file = $user_basedir . '/' . $filename;
+ if( file_exists( $file ) ){
+ if( $image_path ){
+ return $file;
+ }
+
+ return true;
+ }
+ }
+ return false;
+}
+
+
+/**
+ * Check if file is temporary
+ * @param string $filename
+ * @return bool
+ */
+function um_is_temp_file( $filename ){
+ $user_basedir = UM()->uploader()->get_upload_user_base_dir( 'temp' );
+
+ $file = $user_basedir . '/' . $filename;
+
+ if( file_exists( $file ) ){
+ return true;
+ }
+ return false;
+}
+
+
/**
* Get core page url
*
@@ -1645,8 +1702,8 @@ function um_user_uploads_dir() {
*/
function um_closest_num( $array, $number ) {
sort( $array );
- foreach ($array as $a) {
- if ($a >= $number) return $a;
+ foreach ( $array as $a ) {
+ if ( $a >= $number ) return $a;
}
return end( $array );
@@ -1664,17 +1721,20 @@ function um_closest_num( $array, $number ) {
function um_get_cover_uri( $image, $attrs ) {
$uri = false;
$ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
- if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . '/cover_photo' . $ext )) {
- $uri = um_user_uploads_uri() . 'cover_photo' . $ext . '?' . current_time( 'timestamp' );
+ if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/cover_photo{$ext}" ) ) {
+ $uri = um_user_uploads_uri() . "/cover_photo{$ext}?" . current_time( 'timestamp' );
}
- if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . '/cover_photo-' . $attrs . $ext )) {
- $uri = um_user_uploads_uri() . 'cover_photo-' . $attrs . $ext . '?' . current_time( 'timestamp' );
+
+ if ( file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/cover_photo-{$attrs}x{$attrs}{$ext}" ) ) {
+ $uri = um_user_uploads_uri() . "/cover_photo-{$attrs}x{$attrs}{$ext}?". current_time( 'timestamp' );
+ }else if ( file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/cover_photo-{$attrs}{$ext}" ) ) {
+ $uri = um_user_uploads_uri() . "/cover_photo-{$attrs}{$ext}?" . current_time( 'timestamp' );
}
-
return $uri;
}
+
/**
* get avatar URL instead of image
*
@@ -1701,7 +1761,6 @@ function um_get_avatar_uri( $image, $attrs ) {
$uri = false;
$find = false;
$ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
-
/**
* UM hook
*
@@ -1724,26 +1783,26 @@ function um_get_avatar_uri( $image, $attrs ) {
* ?>
*/
$cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) );
-
if( $attrs == 'original' && file_exists( um_user_uploads_dir() . "profile_photo{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo{$ext}";
- } else if (file_exists( um_user_uploads_dir() . "profile_photo-{$attrs}{$ext}" )) {
+ } else if ( file_exists( um_user_uploads_dir() . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) {
+ $uri = um_user_uploads_uri() . "profile_photo-{$attrs}x{$attrs}{$ext}";
+ } else if ( file_exists( um_user_uploads_dir() . "profile_photo-{$attrs}{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo-{$attrs}{$ext}";
} else {
$sizes = UM()->options()->get( 'photo_thumb_sizes' );
- if (is_array( $sizes )) $find = um_closest_num( $sizes, $attrs );
-
- if (file_exists( um_user_uploads_dir() . "profile_photo-{$find}{$ext}" )) {
+ if ( is_array( $sizes ) ) $find = um_closest_num( $sizes, $attrs );
+ if ( file_exists( um_user_uploads_dir() . "profile_photo-{$find}x{$find}{$ext}" ) ) {
+ $uri = um_user_uploads_uri() . "profile_photo-{$find}x{$find}{$ext}";
+ }else if ( file_exists( um_user_uploads_dir() . "profile_photo-{$find}{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo-{$find}{$ext}";
- } else if (file_exists( um_user_uploads_dir() . "profile_photo{$ext}" )) {
+ } else if ( file_exists( um_user_uploads_dir() . "profile_photo{$ext}" ) ) {
$uri = um_user_uploads_uri() . "profile_photo{$ext}";
}
}
-
if ( !empty( $cache_time ) ) {
$uri .= "?{$cache_time}";
}
-
return $uri;
}