From 0a4f8ea0c6600c01fe22fbd2874139a8ca11f1fe Mon Sep 17 00:00:00 2001 From: joao Date: Thu, 30 Jun 2022 13:17:14 +0100 Subject: [PATCH 1/3] Adding functionality to upload cover on register. --- um-cover-photo/README.md | 9 ++ um-cover-photo/um-cover-photo.php | 214 ++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 um-cover-photo/README.md create mode 100644 um-cover-photo/um-cover-photo.php diff --git a/um-cover-photo/README.md b/um-cover-photo/README.md new file mode 100644 index 0000000..66b6560 --- /dev/null +++ b/um-cover-photo/README.md @@ -0,0 +1,9 @@ +## Ultimate Member - Enable Profile Photo in Register form + +Enable users to upload their profile photo in Register form + +- Just add the pre-defined field "Profile Photo" to the Register form via UM Form Builder! + +## License + +GNU Version 2 or Any Later Version diff --git a/um-cover-photo/um-cover-photo.php b/um-cover-photo/um-cover-photo.php new file mode 100644 index 0000000..d24d4c3 --- /dev/null +++ b/um-cover-photo/um-cover-photo.php @@ -0,0 +1,214 @@ + __('Cover Photo','ultimate-member'), + 'metakey' => 'register_cover_photo', + 'type' => 'image', + 'label' => __('Change your cover photo','ultimate-member'), + 'upload_text' => __('Upload profile cover here','ultimate-member'), + 'icon' => 'um-faicon-picture-o', + 'crop' => 2, + 'editable' => 1, + 'max_size' => ( UM()->options()->get('cover_photo_max_size') ) ? UM()->options()->get('cover_photo_max_size') : 999999999, + 'modal_size' => 'large', + 'ratio' => str_replace(':1','',UM()->options()->get('profile_cover_ratio')), + 'min_width' => UM()->options()->get('cover_min_width') + ); + + + return $arr; +} +add_filter( 'um_predefined_fields_hook', 'um_predefined_fields_hook_cover_photo', 99999, 1 ); + +/** + * Multiply Cover Photo with different sizes + * + * @param integer $user_id the user ID. + */ +function um_registration_set_cover_photo( $user_id, $args ) { + + + if( isset( $args['form_id'] )) $req = 'register_cover_photo-' . $args['form_id']; + else $req = 'register_cover_photo'; + if( ! isset( $_REQUEST[$req] ) ) return; + //if ( strpos( $_REQUEST['register_profile_photo'], '_temp.') <= -1 ) { + //return; + //} + + if( is_user_logged_in() ) { + UM()->files()->delete_core_user_photo( $user_id, 'cover_photo' ); + } + + $user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id, true ); + + $temp_dir = UM()->uploader()->get_core_temp_dir() . DIRECTORY_SEPARATOR; + + $temp_profile_photo = array_slice( scandir( $temp_dir ), 2); + + $temp_profile_id = isset( $_COOKIE['um-register-cover-photo'] ) ? $_COOKIE['um-register-cover-photo'] : null; + + foreach( $temp_profile_photo as $i => $p ){ + if ( strpos($p, "_photo_{$temp_profile_id}_temp") !== false ) { + $profile_p = $p; + } + } + + if( empty( $profile_p ) ) return; + + $temp_image_path = $temp_dir . DIRECTORY_SEPARATOR . $profile_p; + $new_image_path = $user_basedir . DIRECTORY_SEPARATOR . $profile_p; + + $image = wp_get_image_editor( $temp_image_path ); + + $file_info = wp_check_filetype_and_ext( $temp_image_path, $profile_p ); + + $ext = $file_info['ext']; + + $new_image_name = str_replace( $profile_p, "cover_photo.{$ext}", $new_image_path ); + + $sizes = UM()->options()->get( 'photo_thumb_sizes' ); + + $quality = UM()->options()->get( 'image_compression' ); + + + if ( ! is_wp_error( $image ) ) { + + $image->save( $new_image_name ); + + $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_cover_photo' ); + update_user_meta( $user_id, 'cover_photo', "cover_photo.{$ext}" ); + update_user_meta( $user_id, 'register_cover_photo', "cover_photo.{$ext}" ); + @unlink( $temp_image_path ); + + } + +} + +add_action( 'um_after_user_account_updated', 'um_registration_set_cover_photo', 1, 2 ); +add_action( 'um_registration_set_extra_data', 'um_registration_set_cover_photo', 1, 2 ); + + +/** + * Set Temporary user id + */ +function um_register_cover_photo_set_temp_user_id() { + + $temp_profile_id = isset( $_COOKIE['um-register-cover-photo'] ) ? $_COOKIE['um-register-cover-photo'] : null; + if ( ! $temp_profile_id ) { + setcookie( 'um-register-cover-photo', md5( time() ), time() + 3600, COOKIEPATH, COOKIE_DOMAIN ); + } + +} +add_action( 'template_redirect', 'um_register_cover_photo_set_temp_user_id' ); + +/** + * Set handler callback for filename + */ +function um_register_cover_photo_upload_handler( $override_handler ) { + + if ( 'stream_photo' == UM()->uploader()->upload_image_type && 'register_cover_photo' == UM()->uploader()->field_key ) { + + $override_handler['unique_filename_callback'] = 'um_register_cover_photo_name'; + } + + return $override_handler; +} +add_filter( 'um_image_upload_handler_overrides__register_cover_photo', 'um_register_cover_photo_upload_handler', 99999 ); + +/** + * Change filename + */ +function um_register_cover_photo_name( $dir, $filename, $ext ) { + $temp_profile_id = isset( $_COOKIE['um-register-cover-photo'] ) ? $_COOKIE['um-register-cover-photo'] : null; + + return "cover_photo_{$temp_profile_id}_temp{$ext}"; + +} + +/** + * Support cover photo uploader in Account form + */ +function um_register_display_cover_photo_in_account( $field_atts, $key, $data ) { + + if ( 'register_cover_photo' == $key && um_is_core_page( 'account' ) ) { + + $profile_photo = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . DIRECTORY_SEPARATOR . um_profile( 'cover_photo' ) . '?ts=' . current_time( 'timestamp' ); + + $field_atts['data-cover_photo'] = array( $profile_photo ); + } + + return $field_atts; +} +add_filter( 'um_field_extra_atts', 'um_register_display_cover_photo_in_account', 10, 3 ); + +/** + * Clear cover photo cache + */ +function um_register_display_cover_photo_script() { + + if( ! um_is_core_page( 'account' ) ) return; + + ?> + + files()->delete_core_user_photo( get_current_user_id(), 'cover_photo' ); + } + wp_send_json_success(); + +} +add_action( 'wp_ajax_um_remove_file', 'um_register_delete_cover_photo_from_account', 1 ); From 5735dcd5532a369e8955962f0c932ba18a9fead8 Mon Sep 17 00:00:00 2001 From: joao Date: Thu, 30 Jun 2022 13:18:18 +0100 Subject: [PATCH 2/3] Update README.md --- um-cover-photo/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/um-cover-photo/README.md b/um-cover-photo/README.md index 66b6560..a4987b1 100644 --- a/um-cover-photo/README.md +++ b/um-cover-photo/README.md @@ -1,8 +1,8 @@ ## Ultimate Member - Enable Profile Photo in Register form -Enable users to upload their profile photo in Register form +Enable users to upload their cover photo in Register form -- Just add the pre-defined field "Profile Photo" to the Register form via UM Form Builder! +- Just add the pre-defined field "Cover Photo" to the Register form via UM Form Builder! ## License From 7ae78ae753a1c8993a6d96560163d79363362d5d Mon Sep 17 00:00:00 2001 From: joao Date: Thu, 30 Jun 2022 14:43:34 +0100 Subject: [PATCH 3/3] Update README.md --- um-cover-photo/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/um-cover-photo/README.md b/um-cover-photo/README.md index a4987b1..58e1917 100644 --- a/um-cover-photo/README.md +++ b/um-cover-photo/README.md @@ -1,4 +1,4 @@ -## Ultimate Member - Enable Profile Photo in Register form +## Ultimate Member - Enable Cover Photo in Register form Enable users to upload their cover photo in Register form