Skip to content

Commit

Permalink
= 4.1.4.2 =
Browse files Browse the repository at this point in the history
~ Modified: function upload, crop avatar
~ Added: function remove avatar
  • Loading branch information
tungnxt89 committed Jan 14, 2022
1 parent 425da7c commit d1dc4af
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 53 deletions.
20 changes: 15 additions & 5 deletions assets/src/js/frontend/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
e.preventDefault();
const self = this;
$.ajax( {
url: '?lp-ajax=save-uploaded-user-avatar',
//url: '?lp-ajax=save-uploaded-user-avatar',
url: ( typeof lpGlobalSettings !== 'undefined' ? lpGlobalSettings.ajax : '' ).addQueryVar( 'action', 'learnpress_save-uploaded-user-avatar' ),
data: this.$( '.lp-avatar-crop-image' ).serializeJSON(),
type: 'post',
success( response ) {
Expand Down Expand Up @@ -53,11 +54,20 @@
return;
}

this.$().removeAttr( 'data-custom' );
this.$( '.profile-picture' ).toggleClass( 'profile-avatar-current' );
this.$( '#submit' ).prop( 'disabled', false );
const el = this;

$( '.lp-user-profile-avatar' ).html( this.$( '.profile-avatar-current' ).find( 'img' ).clone() );
$.ajax( {
url: ( typeof lpGlobalSettings !== 'undefined' ? lpGlobalSettings.ajax : '' ).addQueryVar( 'action', 'learnpress_remove-avatar' ),
data: {},
type: 'post',
success( response ) {
el.$().removeAttr( 'data-custom' );
el.$( '.profile-picture' ).toggleClass( 'profile-avatar-current' );
el.$( '#submit' ).prop( 'disabled', false );

$( '.lp-user-profile-avatar' ).html( el.$( '.profile-avatar-current' ).find( 'img' ).clone() );
},
} );
},
_upload( e ) {
e.preventDefault();
Expand Down
101 changes: 101 additions & 0 deletions inc/admin/class-lp-admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,12 +1196,39 @@ public static function update_order_status() {
die();
}*/

/**
* Upload avatar of user
*
* @editor tungnx
* @modify 4.1.4.2
*/
public static function upload_user_avatar() {
$user_id = get_current_user_id();

if ( ! $user_id ) {
return;
}

$file = $_FILES['lp-upload-avatar'];
$upload_dir = learn_press_user_profile_picture_upload_dir();

add_filter( 'upload_dir', array( __CLASS__, '_user_avatar_upload_dir' ), 10000 );

$file_info_arr = explode( '.', $file['name'] );
$file_info_arr_length = count( $file_info_arr );
$file_ext_index = $file_info_arr_length - 1;
$file_ext = $file_info_arr[ $file_ext_index ];
$file['name'] = $user_id . '.' . $file_ext;

// Delete old image if exists
$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
if ( $path_img ) {
$path = $upload_dir['basedir'] . '/' . $path_img;
if ( file_exists( $path ) ) {
@unlink( $path );
}
}

$result = wp_handle_upload(
$file,
array(
Expand All @@ -1212,6 +1239,7 @@ public static function upload_user_avatar() {
remove_filter( 'upload_dir', array( __CLASS__, '_user_avatar_upload_dir' ), 10000 );
if ( is_array( $result ) ) {
$result['name'] = $upload_dir['subdir'] . '/' . basename( $result['file'] );
update_user_meta( $user_id, '_lp_profile_picture', $result['name'] );
unset( $result['file'] );
} else {
$result = array(
Expand All @@ -1221,6 +1249,77 @@ public static function upload_user_avatar() {
learn_press_send_json( $result );
}

/**
* Crop avatar of user
*
* @editor tungnx
* @return void
*/
public static function save_uploaded_user_avatar() {
$avatar_data = wp_parse_args(
LP_Request::get( 'lp-user-avatar-crop' ),
array(
'name' => '',
'width' => '',
'height' => '',
'points' => '',
'nonce' => '',
)
);

$current_user_id = get_current_user_id();

if ( ! wp_verify_nonce( $avatar_data['nonce'], 'save-uploaded-profile-' . $current_user_id ) ) {
die( 'ERROR VERIFY NONCE!' );
}

$url = learn_press_update_user_profile_avatar();
if ( $url ) {
learn_press_send_json(
array(
'success' => true,
'avatar' => sprintf( '<img src="%s" />', $url ),
)
);
};

wp_die();
}

/**
* Remove avatar of user
*
* @author tungnx
* @since 4.1.4.2
* @version 1.0.0
* @return void
*/
public static function remove_avatar() {
$response = new LP_REST_Response();

try {
$user_id = get_current_user_id();
if ( ! $user_id ) {
throw new Exception( __( 'User is invalid', 'learnpress' ) );
}

// Delete old image if exists
$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
if ( $path_img ) {
$upload_dir = learn_press_user_profile_picture_upload_dir();
$path = $upload_dir['basedir'] . '/' . $path_img;
if ( file_exists( $path ) ) {
unlink( $path );
$response->status = 'success';
}
}
} catch ( Throwable $e ) {
$response->message = $e->getMessage();
}

wp_send_json( $response );
}

public static function _user_avatar_upload_dir( $dir ) {
$dir = learn_press_user_profile_picture_upload_dir();

Expand Down Expand Up @@ -1260,6 +1359,8 @@ public static function update_order_exports() {

if ( defined( 'DOING_AJAX' ) ) {
add_action( 'wp_ajax_learnpress_upload-user-avatar', array( 'LP_Admin_Ajax', 'upload_user_avatar' ) );
add_action( 'wp_ajax_learnpress_save-uploaded-user-avatar', array( 'LP_Admin_Ajax', 'save_uploaded_user_avatar' ) );
add_action( 'wp_ajax_learnpress_remove-avatar', array( 'LP_Admin_Ajax', 'remove_avatar' ) );
}

add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) );
Expand Down
43 changes: 5 additions & 38 deletions inc/class-lp-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public static function init() {
'checkout-user-email-exists:nopriv',
'recover-order',
'request-become-a-teacher:nonce',
'upload-user-avatar',
// 'upload-user-avatar',
'checkout:nopriv',
'complete-lesson',
'finish-course', // finish_course.
// 'retake-course', // retake_course.
'external-link:nopriv',
'save-uploaded-user-avatar',
// 'save-uploaded-user-avatar',
'load-more-courses',
);

Expand All @@ -40,7 +40,7 @@ public static function init() {
LP_Request::register_ajax( $action, $callback );
}

add_action( 'wp_ajax_learnpress_upload-user-avatar', array( __CLASS__, 'upload_user_avatar' ) );
//add_action( 'wp_ajax_learnpress_upload-user-avatar', array( __CLASS__, 'upload_user_avatar' ) );
}

public static function load_more_courses() {
Expand Down Expand Up @@ -171,7 +171,7 @@ public static function checkout_user_email_exists() {
learn_press_maybe_send_json( $response );
}

public static function upload_user_avatar() {
/*public static function upload_user_avatar() {
$file = $_FILES['lp-upload-avatar'];
$upload_dir = learn_press_user_profile_picture_upload_dir();
Expand All @@ -196,40 +196,7 @@ public static function upload_user_avatar() {
}
learn_press_send_json( $result );
}

public static function save_uploaded_user_avatar() {
$avatar_data = wp_parse_args(
LP_Request::get( 'lp-user-avatar-crop' ),
array(
'name' => '',
'width' => '',
'height' => '',
'points' => '',
'nonce' => '',
)
);

$current_user_id = get_current_user_id();

if ( ! wp_verify_nonce( $avatar_data['nonce'], 'save-uploaded-profile-' . $current_user_id ) ) {
die( 'ERROR VERIFY NONCE!' );
}

$url = learn_press_update_user_profile_avatar();
if ( $url ) {
$user = learn_press_get_current_user();

learn_press_send_json(
array(
'success' => true,
'avatar' => sprintf( '<img src="%s" />', $url ),
)
);
};

wp_die();
}
}*/

public static function _user_avatar_upload_dir( $dir ) {
$dir = learn_press_user_profile_picture_upload_dir();
Expand Down
16 changes: 6 additions & 10 deletions inc/user/lp-user-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ function learn_press_update_user_profile() {
*/
function learn_press_update_user_profile_avatar() {
$user_id = get_current_user_id();
$data = learn_press_get_request( 'lp-user-avatar-crop' );

if ( ! $user_id ) {
return new WP_Error( 2, 'User is invalid!' );
Expand All @@ -1092,9 +1093,11 @@ function learn_press_update_user_profile_avatar() {
return false;
}

$data = learn_press_get_request( 'lp-user-avatar-crop' );
$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );

if ( ! $data || ! ( $path = $upload_dir['basedir'] . $data['name'] ) && file_exists( $path ) ) {
$path = $upload_dir['basedir'] . $path_img;

if ( ! file_exists( $path ) ) {
return false;
}

Expand Down Expand Up @@ -1144,15 +1147,8 @@ function learn_press_update_user_profile_avatar() {
$new_avatar = false;

if ( file_exists( $output ) ) {

$old_avatar = get_user_meta( $user_id, '_lp_profile_picture', true );

if ( file_exists( $upload_dir['basedir'] . '/' . $old_avatar ) ) {
@unlink( $upload_dir['basedir'] . '/' . $old_avatar );
}

$new_avatar = preg_replace( '!^/!', '', $upload_dir['subdir'] ) . '/' . $newname;
update_user_meta( $user_id, '_lp_profile_picture', $new_avatar );
update_user_meta( $user_id, '_lp_profile_picture', '/' . $new_avatar );
update_user_meta( $user_id, '_lp_profile_picture_changed', 'yes' );

$new_avatar = $upload_dir['baseurl'] . '/' . $new_avatar;
Expand Down

0 comments on commit d1dc4af

Please sign in to comment.