From fad46ea01285ff8dc0e0ad095896a47bc9129bd6 Mon Sep 17 00:00:00 2001 From: Champ Camba Date: Tue, 31 Jul 2018 01:35:08 +0800 Subject: [PATCH] Initial File and Image Uploader --- assets/js/um-functions.js | 50 +- assets/js/um-functions.min.js | 2 +- assets/js/um-modal.js | 27 +- assets/js/um-modal.min.js | 2 +- includes/admin/core/class-admin-settings.php | 1 + includes/class-init.php | 15 + includes/core/class-ajax-common.php | 19 + includes/core/class-fields.php | 88 +- includes/core/class-files.php | 320 +++--- includes/core/class-uploader.php | 1060 ++++++++++++++++++ includes/core/class-user.php | 37 + includes/core/um-actions-profile.php | 8 +- includes/core/um-actions-register.php | 8 +- includes/core/um-filters-fields.php | 18 +- includes/um-short-functions.php | 91 +- 15 files changed, 1483 insertions(+), 263 deletions(-) create mode 100644 includes/core/class-uploader.php 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('
'+o.error+"
"),e.parents(".um-modal-body").find(".upload-statusbar").hide(0),um_modal_responsive()):jQuery.each(o,function(t,o){var a=e.parents(".um-modal-body").find(".um-single-image-preview img"),i=e.parents(".um-modal-body").find(".um-single-image-preview");a.attr("src",o),a.load(function(){e.parents(".um-modal-body").find(".um-modal-btn.um-finish-upload.disabled").removeClass("disabled"),e.parents(".um-modal-body").find(".ajax-upload-dragdrop,.upload-statusbar").hide(0),i.show(0),um_modal_responsive()})})}})}function initFileUpload_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.fileupload,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,o=jQuery.parseJSON(o),o.error&&""!=o.error?(e.parents(".um-modal-body").append('
'+o.error+"
"),e.parents(".um-modal-body").find(".upload-statusbar").hide(0),setTimeout(function(){um_modal_responsive()},1e3)):(jQuery.each(o,function(t,o){e.parents(".um-modal-body").find(".um-modal-btn.um-finish-upload.disabled").removeClass("disabled"),e.parents(".um-modal-body").find(".ajax-upload-dragdrop,.upload-statusbar").hide(0),e.parents(".um-modal-body").find(".um-single-file-preview").show(0),"icon"==t?e.parents(".um-modal-body").find(".um-single-fileinfo i").removeClass().addClass(o):"icon_bg"==t?e.parents(".um-modal-body").find(".um-single-fileinfo span.icon").css({"background-color":o}):"filename"==t?e.parents(".um-modal-body").find(".um-single-fileinfo span.filename").html(o):e.parents(".um-modal-body").find(".um-single-fileinfo a").attr("href",o)}),setTimeout(function(){um_modal_responsive()},1e3))}})}function initCrop_UM(){var e=jQuery(".um-modal .um-single-image-preview img").first(),t=jQuery(".um-modal .um-single-image-preview"),o=e.parent().attr("data-crop"),a=e.parent().attr("data-min_width"),i=e.parent().attr("data-min_height"),r=e.parent().attr("data-ratio");if(jQuery(".um-modal").find("#um_upload_single").attr("data-ratio"))var r=jQuery(".um-modal").find("#um_upload_single").attr("data-ratio"),u=r.split(":"),r=u[0];if(e.length&&""!=e.attr("src")){var d=jQuery(window).height()-(jQuery(".um-modal-footer a").height()+20)-50-jQuery(".um-modal-header:visible").height();if(e.css({height:"auto"}),t.css({height:"auto"}),jQuery(window).height()<=400?(t.css({height:d+"px","max-height":d+"px"}),e.css({height:"auto"})):(e.css({height:"auto","max-height":d+"px"}),t.css({height:e.height(),"max-height":d+"px"})),"square"==o)var s={minWidth:a,minHeight:i,dragCrop:!1,aspectRatio:1,zoomable:!1,rotatable:!1,dashed:!1,done:function(t){e.parent().attr("data-coord",Math.round(t.x)+","+Math.round(t.y)+","+Math.round(t.width)+","+Math.round(t.height))}};else if("cover"==o){Math.round(a/r)>0&&(i=Math.round(a/r));var s={minWidth:a,minHeight:i,dragCrop:!1,aspectRatio:r,zoomable:!1,rotatable:!1,dashed:!1,done:function(t){e.parent().attr("data-coord",Math.round(t.x)+","+Math.round(t.y)+","+Math.round(t.width)+","+Math.round(t.height))}}}else if("user"==o)var s={minWidth:a,minHeight:i,dragCrop:!0,aspectRatio:"auto",zoomable:!1,rotatable:!1,dashed:!1,done:function(t){e.parent().attr("data-coord",Math.round(t.x)+","+Math.round(t.y)+","+Math.round(t.width)+","+Math.round(t.height))}};0!=o&&(e.cropper(s),jQuery(".um-single-image-preview img.cropper-hidden").cropper("destroy"),jQuery(".um-single-image-preview img.lazyloaded").addClass("cropper-hidden"),jQuery(".um-single-image-preview img.lazyloaded").removeClass("lazyloaded"),jQuery(".um-single-image-preview .cropper-container").append('
'))}}function um_new_modal(e,t,o,a){var i=jQuery("body").find(".um-modal-overlay");if(0==i.length)if(jQuery(".tipsy").hide(),UM_hide_menus(),jQuery("body,html,textarea").css("overflow","hidden"),jQuery(document).bind("touchmove",function(e){e.preventDefault()}),jQuery(".um-modal").on("touchmove",function(e){e.stopPropagation()}),o?jQuery("body").append('
'):jQuery("body").append('
'),jQuery("#"+e).prependTo(".um-modal"),o){jQuery(".um-modal").find(".um-modal-photo").html("");var r=jQuery(".um-modal-photo img"),u=jQuery(window).width()-60,d=jQuery(window).height()-.25*jQuery(window).height();r.attr("src",a),r.load(function(){jQuery("#"+e).show(),jQuery(".um-modal").show(),r.css({opacity:0}),r.css({"max-width":u}),r.css({"max-height":d}),jQuery(".um-modal").css({width:r.width(),"margin-left":"-"+r.width()/2+"px"}),r.animate({opacity:1},1e3),um_modal_responsive()})}else jQuery("#"+e).show(),jQuery(".um-modal").show(),um_modal_size(t),initImageUpload_UM(jQuery(".um-modal:visible").find(".um-single-image-upload")),initFileUpload_UM(jQuery(".um-modal:visible").find(".um-single-file-upload")),um_modal_responsive()}function um_modal_responsive(){var e=jQuery(".um-modal:visible"),t=jQuery(".um-modal-body.photo:visible");if(t.length){e.removeClass("uimob340"),e.removeClass("uimob500");var o=jQuery(".um-modal-photo img"),a=jQuery(window).width()-60,i=jQuery(window).height()-.25*jQuery(window).height();o.css({opacity:0}),o.css({"max-width":a}),o.css({"max-height":i}),jQuery(".um-modal").css({width:o.width(),"margin-left":"-"+o.width()/2+"px"}),o.animate({opacity:1},1e3);var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}else if(e.length){var u=jQuery(window).width();if(e.removeClass("uimob340"),e.removeClass("uimob500"),u<=340)e.addClass("uimob340"),initCrop_UM(),e.animate({bottom:0},300);else if(u<=500)e.addClass("uimob500"),initCrop_UM(),e.animate({bottom:0},300);else if(u<=800){initCrop_UM();var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}else if(u<=960){initCrop_UM();var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}else if(u>960){initCrop_UM();var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}}}function um_remove_modal(){jQuery("img.cropper-hidden").cropper("destroy"),jQuery("body,html,textarea").css("overflow","auto"),jQuery(document).unbind("touchmove"),jQuery('.um-modal div[id^="um_"]').hide().appendTo("body"),jQuery(".um-modal,.um-modal-overlay").remove()}function um_modal_size(e){jQuery(".um-modal:visible").addClass(e)}function um_modal_add_attr(e,t){jQuery(".um-modal:visible").data(e,t)}function prepare_Modal(){0==jQuery(".um-popup-overlay").length&&(jQuery("body").append('
'),jQuery("body").append('
'),jQuery(".um-popup").addClass("loading"),jQuery("body,html").css({overflow:"hidden"}))}function remove_Modal(){jQuery(".um-popup-overlay").length&&(jQuery(document).trigger("um_before_modal_removed"),jQuery(".um-popup-autogrow:visible").length?jQuery(".um-popup-autogrow:visible").mCustomScrollbar("destroy"):jQuery(".um-popup-autogrow2:visible").length&&jQuery(".um-popup-autogrow2:visible").mCustomScrollbar("destroy"),jQuery(".tipsy").remove(),jQuery(".um-popup").empty().remove(),jQuery(".um-popup-overlay").empty().remove(),jQuery("body,html").css({overflow:"auto"}))}function show_Modal(e){jQuery(".um-popup-overlay").length&&(jQuery(".um-popup").removeClass("loading").html(e),jQuery(".um-tip-n").tipsy({gravity:"n",opacity:1,offset:3}),jQuery(".um-tip-w").tipsy({gravity:"w",opacity:1,offset:3}),jQuery(".um-tip-e").tipsy({gravity:"e",opacity:1,offset:3}),jQuery(".um-tip-s").tipsy({gravity:"s",opacity:1,offset:3}))}function responsive_Modal(){jQuery(".um-popup-overlay").length&&(ag_height=jQuery(window).height()-jQuery(".um-popup um-popup-header").outerHeight()-jQuery(".um-popup .um-popup-footer").outerHeight()-80,ag_height>350&&(ag_height=350),jQuery(".um-popup-autogrow:visible").length?(jQuery(".um-popup-autogrow:visible").css({height:ag_height+"px"}),jQuery(".um-popup-autogrow:visible").mCustomScrollbar({theme:"dark-3",mouseWheelPixels:500}).mCustomScrollbar("scrollTo","bottom",{scrollInertia:0})):jQuery(".um-popup-autogrow2:visible").length&&(jQuery(".um-popup-autogrow2:visible").css({"max-height":ag_height+"px"}),jQuery(".um-popup-autogrow2:visible").mCustomScrollbar({theme:"dark-3",mouseWheelPixels:500})))}function um_reset_field(e){jQuery(e).find("div.um-field-area").find("input,textarea,select").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").removeAttr("selected")}function um_selected(e,t){if(e==t)return"selected='selected'"}jQuery(function(){jQuery(".um-search form *").keypress(function(e){if(13==e.which)return jQuery(".um-search form").submit(),!1}),2==jQuery("input[data-key=user_password],input[data-key=confirm_user_password]").length&&UM_check_password_matched()}); \ No newline at end of file +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="",jQuery("#um_upload_single:visible").data("user_id")&&(user_id=jQuery("#um_upload_single:visible").data("user_id")),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"),user_id:user_id},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('
'+o.error+"
"),e.parents(".um-modal-body").find(".upload-statusbar").hide(0),um_modal_responsive()):(console.log(o),jQuery.each(o,function(t,o){var a=e.parents(".um-modal-body").find(".um-single-image-preview img"),i=e.parents(".um-modal-body").find(".um-single-image-preview"),r=new Date;a.attr("src",o.url+"?"+r.getTime()),a.data("file",o.file),a.load(function(){e.parents(".um-modal-body").find(".um-modal-btn.um-finish-upload.disabled").removeClass("disabled"),e.parents(".um-modal-body").find(".ajax-upload-dragdrop,.upload-statusbar").hide(0),i.show(0),um_modal_responsive()})}))},onError:function(e){console.log(e)}})}function initFileUpload_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="",jQuery("#um_upload_single:visible").data("user_id")&&(user_id=jQuery("#um_upload_single:visible").data("user_id")),e.uploadFile({url:um_scripts.fileupload,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,o=jQuery.parseJSON(o),o.error&&""!=o.error?(e.parents(".um-modal-body").append('
'+o.error+"
"),e.parents(".um-modal-body").find(".upload-statusbar").hide(0),setTimeout(function(){um_modal_responsive()},1e3)):(jQuery.each(o,function(t,o){e.parents(".um-modal-body").find(".um-modal-btn.um-finish-upload.disabled").removeClass("disabled"),e.parents(".um-modal-body").find(".ajax-upload-dragdrop,.upload-statusbar").hide(0),e.parents(".um-modal-body").find(".um-single-file-preview").show(0),"icon"==t?e.parents(".um-modal-body").find(".um-single-fileinfo i").removeClass().addClass(o):"icon_bg"==t?e.parents(".um-modal-body").find(".um-single-fileinfo span.icon").css({"background-color":o}):"filename"==t?e.parents(".um-modal-body").find(".um-single-fileinfo a").attr("data-file",o):"original_name"==t?(e.parents(".um-modal-body").find(".um-single-fileinfo a").attr("data-orignal-name",o),e.parents(".um-modal-body").find(".um-single-fileinfo span.filename").html(o)):"url"==t&&e.parents(".um-modal-body").find(".um-single-fileinfo a").attr("href",o)}),setTimeout(function(){um_modal_responsive()},1e3))},onError:function(e){console.log(e)}})}function initCrop_UM(){var e=jQuery(".um-modal .um-single-image-preview img").first(),t=jQuery(".um-modal .um-single-image-preview"),o=e.parent().attr("data-crop"),a=e.parent().attr("data-min_width"),i=e.parent().attr("data-min_height"),r=e.parent().attr("data-ratio");if(jQuery(".um-modal").find("#um_upload_single").attr("data-ratio"))var r=jQuery(".um-modal").find("#um_upload_single").attr("data-ratio"),u=r.split(":"),r=u[0];if(e.length&&""!=e.attr("src")){var d=jQuery(window).height()-(jQuery(".um-modal-footer a").height()+20)-50-jQuery(".um-modal-header:visible").height();if(e.css({height:"auto"}),t.css({height:"auto"}),jQuery(window).height()<=400?(t.css({height:d+"px","max-height":d+"px"}),e.css({height:"auto"})):(e.css({height:"auto","max-height":d+"px"}),t.css({height:e.height(),"max-height":d+"px"})),"square"==o)var s={minWidth:a,minHeight:i,dragCrop:!1,aspectRatio:1,zoomable:!1,rotatable:!1,dashed:!1,done:function(t){e.parent().attr("data-coord",Math.round(t.x)+","+Math.round(t.y)+","+Math.round(t.width)+","+Math.round(t.height))}};else if("cover"==o){Math.round(a/r)>0&&(i=Math.round(a/r));var s={minWidth:a,minHeight:i,dragCrop:!1,aspectRatio:r,zoomable:!1,rotatable:!1,dashed:!1,done:function(t){e.parent().attr("data-coord",Math.round(t.x)+","+Math.round(t.y)+","+Math.round(t.width)+","+Math.round(t.height))}}}else if("user"==o)var s={minWidth:a,minHeight:i,dragCrop:!0,aspectRatio:"auto",zoomable:!1,rotatable:!1,dashed:!1,done:function(t){e.parent().attr("data-coord",Math.round(t.x)+","+Math.round(t.y)+","+Math.round(t.width)+","+Math.round(t.height))}};0!=o&&(e.cropper(s),jQuery(".um-single-image-preview img.cropper-hidden").cropper("destroy"),jQuery(".um-single-image-preview img.lazyloaded").addClass("cropper-hidden"),jQuery(".um-single-image-preview img.lazyloaded").removeClass("lazyloaded"),jQuery(".um-single-image-preview .cropper-container").append('
'))}}function um_new_modal(e,t,o,a){var i=jQuery("body").find(".um-modal-overlay");if(0==i.length)if(jQuery(".tipsy").hide(),UM_hide_menus(),jQuery("body,html,textarea").css("overflow","hidden"),jQuery(document).bind("touchmove",function(e){e.preventDefault()}),jQuery(".um-modal").on("touchmove",function(e){e.stopPropagation()}),o?jQuery("body").append('
'):jQuery("body").append('
'),jQuery("#"+e).prependTo(".um-modal"),o){jQuery(".um-modal").find(".um-modal-photo").html("");var r=jQuery(".um-modal-photo img"),u=jQuery(window).width()-60,d=jQuery(window).height()-.25*jQuery(window).height();r.attr("src",a),r.load(function(){jQuery("#"+e).show(),jQuery(".um-modal").show(),r.css({opacity:0}),r.css({"max-width":u}),r.css({"max-height":d}),jQuery(".um-modal").css({width:r.width(),"margin-left":"-"+r.width()/2+"px"}),r.animate({opacity:1},1e3),um_modal_responsive()})}else jQuery("#"+e).show(),jQuery(".um-modal").show(),um_modal_size(t),initImageUpload_UM(jQuery(".um-modal:visible").find(".um-single-image-upload")),initFileUpload_UM(jQuery(".um-modal:visible").find(".um-single-file-upload")),um_modal_responsive()}function um_modal_responsive(){var e=jQuery(".um-modal:visible"),t=jQuery(".um-modal-body.photo:visible");if(t.length){e.removeClass("uimob340"),e.removeClass("uimob500");var o=jQuery(".um-modal-photo img"),a=jQuery(window).width()-60,i=jQuery(window).height()-.25*jQuery(window).height();o.css({opacity:0}),o.css({"max-width":a}),o.css({"max-height":i}),jQuery(".um-modal").css({width:o.width(),"margin-left":"-"+o.width()/2+"px"}),o.animate({opacity:1},1e3);var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}else if(e.length){var u=jQuery(window).width();if(e.removeClass("uimob340"),e.removeClass("uimob500"),u<=340)e.addClass("uimob340"),initCrop_UM(),e.animate({bottom:0},300);else if(u<=500)e.addClass("uimob500"),initCrop_UM(),e.animate({bottom:0},300);else if(u<=800){initCrop_UM();var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}else if(u<=960){initCrop_UM();var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}else if(u>960){initCrop_UM();var r=(jQuery(window).height()-e.innerHeight())/2+"px";e.animate({bottom:r},300)}}}function um_remove_modal(){jQuery("img.cropper-hidden").cropper("destroy"),jQuery("body,html,textarea").css("overflow","auto"),jQuery(document).unbind("touchmove"),jQuery('.um-modal div[id^="um_"]').hide().appendTo("body"),jQuery(".um-modal,.um-modal-overlay").remove()}function um_modal_size(e){jQuery(".um-modal:visible").addClass(e)}function um_modal_add_attr(e,t){jQuery(".um-modal:visible").data(e,t)}function prepare_Modal(){0==jQuery(".um-popup-overlay").length&&(jQuery("body").append('
'),jQuery("body").append('
'),jQuery(".um-popup").addClass("loading"),jQuery("body,html").css({overflow:"hidden"}))}function remove_Modal(){jQuery(".um-popup-overlay").length&&(jQuery(document).trigger("um_before_modal_removed"),jQuery(".um-popup-autogrow:visible").length?jQuery(".um-popup-autogrow:visible").mCustomScrollbar("destroy"):jQuery(".um-popup-autogrow2:visible").length&&jQuery(".um-popup-autogrow2:visible").mCustomScrollbar("destroy"),jQuery(".tipsy").remove(),jQuery(".um-popup").empty().remove(),jQuery(".um-popup-overlay").empty().remove(),jQuery("body,html").css({overflow:"auto"}))}function show_Modal(e){jQuery(".um-popup-overlay").length&&(jQuery(".um-popup").removeClass("loading").html(e),jQuery(".um-tip-n").tipsy({gravity:"n",opacity:1,offset:3}),jQuery(".um-tip-w").tipsy({gravity:"w",opacity:1,offset:3}),jQuery(".um-tip-e").tipsy({gravity:"e",opacity:1,offset:3}),jQuery(".um-tip-s").tipsy({gravity:"s",opacity:1,offset:3}))}function responsive_Modal(){jQuery(".um-popup-overlay").length&&(ag_height=jQuery(window).height()-jQuery(".um-popup um-popup-header").outerHeight()-jQuery(".um-popup .um-popup-footer").outerHeight()-80,ag_height>350&&(ag_height=350),jQuery(".um-popup-autogrow:visible").length?(jQuery(".um-popup-autogrow:visible").css({height:ag_height+"px"}),jQuery(".um-popup-autogrow:visible").mCustomScrollbar({theme:"dark-3",mouseWheelPixels:500}).mCustomScrollbar("scrollTo","bottom",{scrollInertia:0})):jQuery(".um-popup-autogrow2:visible").length&&(jQuery(".um-popup-autogrow2:visible").css({"max-height":ag_height+"px"}),jQuery(".um-popup-autogrow2:visible").mCustomScrollbar({theme:"dark-3",mouseWheelPixels:500})))}function um_reset_field(e){jQuery(e).find("div.um-field-area").find("input,textarea,select").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").removeAttr("selected")}function um_selected(e,t){if(e==t)return"selected='selected'"}jQuery(function(){jQuery(".um-search form *").keypress(function(e){if(13==e.which)return jQuery(".um-search form").submit(),!1}),2==jQuery("input[data-key=user_password],input[data-key=confirm_user_password]").length&&UM_check_password_matched()}); \ No newline at end of file diff --git a/assets/js/um-modal.js b/assets/js/um-modal.js index 54d45f87..d35ca2ab 100644 --- a/assets/js/um-modal.js +++ b/assets/js/um-modal.js @@ -72,15 +72,19 @@ jQuery(document).ready(function() { jQuery(document).on('click', '.um-finish-upload.file:not(.disabled)', function(){ var key = jQuery(this).attr('data-key'); + var preview = jQuery(this).parents('.um-modal-body').find('.um-single-file-preview').html(); um_remove_modal(); jQuery('.um-single-file-preview[data-key='+key+']').fadeIn().html( preview ); + + var file = jQuery('.um-field[data-key='+key+']').find('.um-single-fileinfo a').data('file'); jQuery('.um-single-file-preview[data-key='+key+']').parents('.um-field').find('.um-btn-auto-width').html( jQuery(this).attr('data-change') ); - jQuery('.um-single-file-preview[data-key='+key+']').parents('.um-field').find('input[type=hidden]').val( jQuery('.um-single-file-preview[data-key='+key+']').parents('.um-field').find('.um-single-fileinfo a').attr('href') ); + jQuery('.um-single-file-preview[data-key='+key+']').parents('.um-field').find('input[type="hidden"]').val( file ); + }); jQuery(document).on('click', '.um-finish-upload.image:not(.disabled)', function(){ @@ -110,26 +114,29 @@ jQuery(document).ready(function() { user_id : user_id, key: key }, - success: function(data){ - + success: function( data ){ + d = new Date(); - + if ( key == 'profile_photo') { - jQuery('.um-profile-photo-img img').attr('src', data + "?"+d.getTime()); + jQuery('.um-profile-photo-img img').attr('src', data.data.image.source_url + "?"+d.getTime()); } - + if ( key == 'cover_photo') { - jQuery('.um-cover-e').empty().html(''); + jQuery('.um-cover-e').empty().html(''); if ( jQuery('.um').hasClass('um-editing') ) { jQuery('.um-cover-overlay').show(); } } + + jQuery('.um-single-image-preview[data-key='+key+']').fadeIn().find('img').attr('src', data.data.image.source_url + "?"+d.getTime()); - jQuery('.um-single-image-preview[data-key='+key+']').fadeIn().find('img').attr('src', data + "?"+d.getTime()); um_remove_modal(); - jQuery('.um-single-image-preview[data-key='+key+']').parents('.um-field').find('.um-btn-auto-width').html( elem.attr('data-change') ); - jQuery('.um-single-image-preview[data-key='+key+']').parents('.um-field').find('input[type=hidden]').val( data ); + jQuery('.um-single-image-preview[data-key='+key+']').parents('.um-field').find('.um-btn-auto-width').html( elem.attr('data-change') ); + + jQuery('.um-single-image-preview[data-key='+key+']').parents('.um-field').find('input[type="hidden"]').val( data.data.image.filename ); + } }); diff --git a/assets/js/um-modal.min.js b/assets/js/um-modal.min.js index 7de4a3e2..2e2b284f 100644 --- a/assets/js/um-modal.min.js +++ b/assets/js/um-modal.min.js @@ -1 +1 @@ -jQuery(document).ready(function(){jQuery(document).on("click",".um-popup-overlay",function(){remove_Modal()}),jQuery(document).on("click",'.um-modal-overlay, a[data-action="um_remove_modal"]',function(){um_remove_modal()}),jQuery(document).on("click",'a[data-modal^="um_"], span[data-modal^="um_"], .um-modal a',function(e){return e.preventDefault(),!1}),jQuery(document).on("click",".um-modal .um-single-file-preview a.cancel",function(e){e.preventDefault();var a=jQuery(this).parents(".um-modal-body"),t=jQuery(this).parents(".um-modal-body").find(".um-single-fileinfo a").attr("href");return a.find(".um-single-file-preview").hide(),a.find(".ajax-upload-dragdrop").show(),a.find(".um-modal-btn.um-finish-upload").addClass("disabled"),um_modal_responsive(),jQuery.ajax({url:um_scripts.remove_file,type:"post",data:{src:t}}),!1}),jQuery(document).on("click",".um-modal .um-single-image-preview a.cancel",function(e){e.preventDefault();var a=jQuery(this).parents(".um-modal-body"),t=jQuery(this).parents(".um-modal-body").find(".um-single-image-preview img").attr("src");return jQuery("img.cropper-hidden").cropper("destroy"),a.find(".um-single-image-preview img").attr("src",""),a.find(".um-single-image-preview").hide(),a.find(".ajax-upload-dragdrop").show(),a.find(".um-modal-btn.um-finish-upload").addClass("disabled"),um_modal_responsive(),jQuery.ajax({url:um_scripts.remove_file,type:"post",data:{src:t}}),!1}),jQuery(document).on("click",".um-finish-upload.file:not(.disabled)",function(){var e=jQuery(this).attr("data-key"),a=jQuery(this).parents(".um-modal-body").find(".um-single-file-preview").html();um_remove_modal(),jQuery(".um-single-file-preview[data-key="+e+"]").fadeIn().html(a),jQuery(".um-single-file-preview[data-key="+e+"]").parents(".um-field").find(".um-btn-auto-width").html(jQuery(this).attr("data-change")),jQuery(".um-single-file-preview[data-key="+e+"]").parents(".um-field").find("input[type=hidden]").val(jQuery(".um-single-file-preview[data-key="+e+"]").parents(".um-field").find(".um-single-fileinfo a").attr("href"))}),jQuery(document).on("click",".um-finish-upload.image:not(.disabled)",function(){var e=jQuery(this),a=jQuery(this).attr("data-key"),t=jQuery(this).parents(".um-modal-body").find(".um-single-image-preview"),r=t.find("img").attr("src"),i=t.attr("data-coord");if(jQuery(this).parents("#um_upload_single").attr("data-user_id"))var u=jQuery(this).parents("#um_upload_single").attr("data-user_id");else var u=0;i?(jQuery(this).html(jQuery(this).attr("data-processing")).addClass("disabled"),jQuery.ajax({url:um_scripts.resize_image,type:"POST",data:{src:r,coord:i,user_id:u,key:a},success:function(t){d=new Date,"profile_photo"==a&&jQuery(".um-profile-photo-img img").attr("src",t+"?"+d.getTime()),"cover_photo"==a&&(jQuery(".um-cover-e").empty().html(''),jQuery(".um").hasClass("um-editing")&&jQuery(".um-cover-overlay").show()),jQuery(".um-single-image-preview[data-key="+a+"]").fadeIn().find("img").attr("src",t+"?"+d.getTime()),um_remove_modal(),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find(".um-btn-auto-width").html(e.attr("data-change")),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find("input[type=hidden]").val(t)}})):(d=new Date,jQuery(".um-single-image-preview[data-key="+a+"]").fadeIn().find("img").attr("src",r+"?"+d.getTime()),um_remove_modal(),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find(".um-btn-auto-width").html(e.attr("data-change")),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find("input[type=hidden]").val(r))}),jQuery(document).on("click",'a[data-modal^="um_"], span[data-modal^="um_"]',function(e){var a=jQuery(this).attr("data-modal");if(jQuery(this).data("modal-size"))var t=jQuery(this).data("modal-size");else var t="normal";jQuery(this).data("modal-copy")?(jQuery("#"+a).html(jQuery(this).parents(".um-field").find(".um-modal-hidden-content").html()),jQuery(this).parents(".um-profile-photo").attr("data-user_id")&&jQuery("#"+a).attr("data-user_id",jQuery(this).parents(".um-profile-photo").attr("data-user_id")),jQuery(this).parents(".um-cover").attr("data-ratio")&&jQuery("#"+a).attr("data-ratio",jQuery(this).parents(".um-cover").attr("data-ratio")),jQuery(this).parents(".um-cover").attr("data-user_id")&&jQuery("#"+a).attr("data-user_id",jQuery(this).parents(".um-cover").attr("data-user_id")),um_new_modal(a,t)):um_new_modal(a,t)})}); \ No newline at end of file +jQuery(document).ready(function(){jQuery(document).on("click",".um-popup-overlay",function(){remove_Modal()}),jQuery(document).on("click",'.um-modal-overlay, a[data-action="um_remove_modal"]',function(){um_remove_modal()}),jQuery(document).on("click",'a[data-modal^="um_"], span[data-modal^="um_"], .um-modal a',function(e){return e.preventDefault(),!1}),jQuery(document).on("click",".um-modal .um-single-file-preview a.cancel",function(e){e.preventDefault();var a=jQuery(this).parents(".um-modal-body"),t=jQuery(this).parents(".um-modal-body").find(".um-single-fileinfo a").attr("href");return a.find(".um-single-file-preview").hide(),a.find(".ajax-upload-dragdrop").show(),a.find(".um-modal-btn.um-finish-upload").addClass("disabled"),um_modal_responsive(),jQuery.ajax({url:um_scripts.remove_file,type:"post",data:{src:t}}),!1}),jQuery(document).on("click",".um-modal .um-single-image-preview a.cancel",function(e){e.preventDefault();var a=jQuery(this).parents(".um-modal-body"),t=jQuery(this).parents(".um-modal-body").find(".um-single-image-preview img").attr("src");return jQuery("img.cropper-hidden").cropper("destroy"),a.find(".um-single-image-preview img").attr("src",""),a.find(".um-single-image-preview").hide(),a.find(".ajax-upload-dragdrop").show(),a.find(".um-modal-btn.um-finish-upload").addClass("disabled"),um_modal_responsive(),jQuery.ajax({url:um_scripts.remove_file,type:"post",data:{src:t}}),!1}),jQuery(document).on("click",".um-finish-upload.file:not(.disabled)",function(){var e=jQuery(this).attr("data-key"),a=jQuery(this).parents(".um-modal-body").find(".um-single-file-preview").html();um_remove_modal(),jQuery(".um-single-file-preview[data-key="+e+"]").fadeIn().html(a);var t=jQuery(".um-field[data-key="+e+"]").find(".um-single-fileinfo a").data("file");jQuery(".um-single-file-preview[data-key="+e+"]").parents(".um-field").find(".um-btn-auto-width").html(jQuery(this).attr("data-change")),jQuery(".um-single-file-preview[data-key="+e+"]").parents(".um-field").find('input[type="hidden"]').val(t)}),jQuery(document).on("click",".um-finish-upload.image:not(.disabled)",function(){var e=jQuery(this),a=jQuery(this).attr("data-key"),t=jQuery(this).parents(".um-modal-body").find(".um-single-image-preview"),r=t.find("img").attr("src"),i=t.attr("data-coord");if(jQuery(this).parents("#um_upload_single").attr("data-user_id"))var u=jQuery(this).parents("#um_upload_single").attr("data-user_id");else var u=0;i?(jQuery(this).html(jQuery(this).attr("data-processing")).addClass("disabled"),jQuery.ajax({url:um_scripts.resize_image,type:"POST",data:{src:r,coord:i,user_id:u,key:a},success:function(t){d=new Date,"profile_photo"==a&&jQuery(".um-profile-photo-img img").attr("src",t.data.image.source_url+"?"+d.getTime()),"cover_photo"==a&&(jQuery(".um-cover-e").empty().html(''),jQuery(".um").hasClass("um-editing")&&jQuery(".um-cover-overlay").show()),jQuery(".um-single-image-preview[data-key="+a+"]").fadeIn().find("img").attr("src",t.data.image.source_url+"?"+d.getTime()),um_remove_modal(),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find(".um-btn-auto-width").html(e.attr("data-change")),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find('input[type="hidden"]').val(t.data.image.filename)}})):(d=new Date,jQuery(".um-single-image-preview[data-key="+a+"]").fadeIn().find("img").attr("src",r+"?"+d.getTime()),um_remove_modal(),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find(".um-btn-auto-width").html(e.attr("data-change")),jQuery(".um-single-image-preview[data-key="+a+"]").parents(".um-field").find("input[type=hidden]").val(r))}),jQuery(document).on("click",'a[data-modal^="um_"], span[data-modal^="um_"]',function(e){var a=jQuery(this).attr("data-modal");if(jQuery(this).data("modal-size"))var t=jQuery(this).data("modal-size");else var t="normal";jQuery(this).data("modal-copy")?(jQuery("#"+a).html(jQuery(this).parents(".um-field").find(".um-modal-hidden-content").html()),jQuery(this).parents(".um-profile-photo").attr("data-user_id")&&jQuery("#"+a).attr("data-user_id",jQuery(this).parents(".um-profile-photo").attr("data-user_id")),jQuery(this).parents(".um-cover").attr("data-ratio")&&jQuery("#"+a).attr("data-ratio",jQuery(this).parents(".um-cover").attr("data-ratio")),jQuery(this).parents(".um-cover").attr("data-user_id")&&jQuery("#"+a).attr("data-user_id",jQuery(this).parents(".um-cover").attr("data-user_id")),um_new_modal(a,t)):um_new_modal(a,t)})}); \ No newline at end of file diff --git a/includes/admin/core/class-admin-settings.php b/includes/admin/core/class-admin-settings.php index f64c315d..2583c3bb 100644 --- a/includes/admin/core/class-admin-settings.php +++ b/includes/admin/core/class-admin-settings.php @@ -2381,6 +2381,7 @@ SOAP Client: GD Library: Mail: +Exif: --- Session Configurations --- diff --git a/includes/class-init.php b/includes/class-init.php index b62ed050..b93fd6d2 100644 --- a/includes/class-init.php +++ b/includes/class-init.php @@ -540,6 +540,8 @@ if ( ! class_exists( 'UM' ) ) { $this->mobile(); $this->external_integrations(); $this->gdpr(); + $this->uploader(); + } @@ -1139,6 +1141,19 @@ if ( ! class_exists( 'UM' ) ) { return $this->classes['files']; } + + /** + * @since 2.0.21 + * + * @return um\core\Files + */ + function uploader() { + if ( empty( $this->classes['uploader'] ) ) { + $this->classes['uploader'] = new um\core\Uploader(); + } + return $this->classes['uploader']; + } + /** * @since 2.0 diff --git a/includes/core/class-ajax-common.php b/includes/core/class-ajax-common.php index a0312e36..f71f6093 100644 --- a/includes/core/class-ajax-common.php +++ b/includes/core/class-ajax-common.php @@ -32,6 +32,25 @@ if ( ! class_exists( 'um\core\AJAX_Common' ) ) { } + + add_action( 'wp_ajax_um_remove_file', array( UM()->files(), 'ajax_remove_file' ) ); + add_action( 'wp_ajax_um_delete_profile_photo', array( UM()->profile(), 'ajax_delete_profile_photo' ) ); + add_action( 'wp_ajax_um_delete_cover_photo', array( UM()->profile(), 'ajax_delete_cover_photo' ) ); + add_action( 'wp_ajax_um_select_options', array( UM()->form(), 'ajax_select_options' ) ); + add_action( 'wp_ajax_um_ajax_paginate', array( UM()->query(), 'ajax_paginate' ) ); + add_action( 'wp_ajax_um_muted_action', array( UM()->form(), 'ajax_muted_action' ) ); + add_action( 'wp_ajax_nopriv_um_remove_file', array( UM()->files(), 'ajax_remove_file' ) ); + add_action( 'wp_ajax_um_remove_file', array( UM()->files(), 'ajax_remove_file' ) ); + + add_action( 'wp_ajax_nopriv_um_fileupload', array( UM()->files(), 'ajax_file_upload' ) ); + add_action( 'wp_ajax_um_fileupload', array( UM()->files(), 'ajax_file_upload' ) ); + + add_action( 'wp_ajax_nopriv_um_imageupload', array( UM()->files(), 'ajax_image_upload' ) ); + add_action( 'wp_ajax_um_imageupload', array( UM()->files(), 'ajax_image_upload' ) ); + + add_action( 'wp_ajax_nopriv_um_resize_image', array( UM()->files(), 'ajax_resize_image' ) ); + add_action( 'wp_ajax_um_resize_image', array( UM()->files(), 'ajax_resize_image' ) ); + /** * Fallback for ajax urls diff --git a/includes/core/class-fields.php b/includes/core/class-fields.php index df1581ee..4e66ebe3 100644 --- a/includes/core/class-fields.php +++ b/includes/core/class-fields.php @@ -2219,28 +2219,21 @@ if ( ! class_exists( 'um\core\Fields' ) ) { /* Single Image Upload */ case 'image': $output .= '
'; - if (in_array( $key, array( 'profile_photo', 'cover_photo' ) )) { $field_value = ''; } else { $field_value = $this->field_value( $key, $default, $data ); } - $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 )) { - - if (!in_array( $key, array( 'profile_photo', 'cover_photo' ) )) { + if ( ! in_array( $key, array( 'profile_photo', 'cover_photo' ) ) ) { if (isset( $this->set_mode ) && $this->set_mode == 'register') { - $imgValue = $this->field_value( $key, $default, $data ); + $imgValue = UM()->uploader()->get_core_temp_url() . "/" . $this->field_value( $key, $default, $data ); } else { $imgValue = um_user_uploads_uri() . $this->field_value( $key, $default, $data ); } @@ -2248,29 +2241,20 @@ if ( ! class_exists( 'um\core\Fields' ) ) { } else { $img = ''; } - $output .= '
' . $img . '
' . __( 'Change photo', 'ultimate-member' ) . ''; - } else { - $output .= '
' . $button_text . ''; - } - $output .= '
'; - /* modal hidden */ $output .= '
'; - $output .= '
' . $modal_label . '
'; - $output .= '
'; - if (isset( $this->set_id )) { $set_id = $this->set_id; $set_mode = $this->set_mode; @@ -2278,12 +2262,9 @@ if ( ! class_exists( 'um\core\Fields' ) ) { $set_id = 0; $set_mode = ''; } - $nonce = wp_create_nonce( 'um_upload_nonce-' . $this->timestamp ); - $output .= '
'; $output .= '
' . $button_text . '
'; - $output .= ''; - $output .= '
'; - $output .= '
'; - /* end */ - if ($this->is_error( $key )) { $output .= $this->field_error( $this->show_error( $key ) ); } - $output .= '
'; - break; + break; /* Single File Upload */ case 'file': $output .= '
'; - $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 .= '
' . $modal_label . '
'; - $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; }