Created
March 27, 2023 09:53
-
-
Save ipokkel/7a026f6002232a56e1dc448ff58da6fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Allow users to add an avatar at checkout for the Basic User Avatars plugin. | |
* | |
* This recipe assumes that you have the Basic User Avatar plugin installed and activated. | |
* @link https://wordpress.org/plugins/basic-user-avatars/ | |
* | |
* This recipe requires a custom user field created with the name basic_user_avatar. | |
* The field should be set to "No" for the display on the profile option for either | |
* the group (Show fields on user profile?) or the field (Show field on user profile?). | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
// After a pmpro checkout, check if the user has a meta field basic_user_avatar and if so, update the user's avatar. | |
function user_field_file_upload_to_basic_user_avatars( $user_id ) { | |
global $pmpro_user_fields; | |
// Let's only do this if the Basic User Avatar plugin is active. | |
if ( ! class_exists( 'basic_user_avatars' ) ) { | |
return; | |
} | |
// Get the user. | |
$user = get_user_by( 'id', $user_id ); | |
// Get the user's avatar array. | |
$basic_user_avatar = get_user_meta( $user_id, 'basic_user_avatar', false ); | |
// Let's bail if we don't have the required user meta. | |
if ( empty( $basic_user_avatar ) ) { | |
return; | |
} | |
// Get the upload directory and path. | |
$upload_dir = wp_upload_dir(); | |
$upload_path = $upload_dir['basedir'] . '/pmpro-register-helper/' . $user->user_login . '/'; | |
$upload_url = $upload_dir['baseurl'] . '/pmpro-register-helper/' . $user->user_login . '/'; | |
// Let's check if basic_user_avatar meta exists. | |
if ( ! empty( $basic_user_avatar ) ) { | |
$basic_user_avatar = $basic_user_avatar[0]; | |
// Let's check if the basic_user_avatar meta data is in the PMPro file upload format. | |
if ( ! isset( $basic_user_avatar['full'] ) && isset( $basic_user_avatar['fullurl'] ) && ! empty( $basic_user_avatar['fullurl'] ) ) { | |
// Let's create the new basic_user_avatar array. | |
$new_basic_user_avatar_array = array( | |
'full' => $basic_user_avatar['fullurl'], | |
); | |
// Let's create a avatar file path array. | |
$basic_user_avatar_path_array = array( | |
'full' => $basic_user_avatar['fullpath'], | |
); | |
// Get the filename. | |
$filename = $basic_user_avatar['filename']; | |
// Get the file extension. | |
$file_extension = pathinfo( $filename, PATHINFO_EXTENSION ); | |
// Image sizes for the avatar. | |
$size = array( 32, 64, 128, 256 ); | |
// Loop through the image sizes and create the new images. | |
foreach ( $size as $s ) { | |
// create the new image | |
$image = wp_get_image_editor( $upload_path . $filename ); | |
if ( ! is_wp_error( $image ) ) { | |
$image->resize( $s, $s, true ); | |
$image->save( $upload_path . $filename . '-' . $s . 'x' . $s . '.' . $file_extension ); | |
// add the new image to the array. | |
$new_basic_user_avatar_array[ $s ] = $upload_url . $filename . '-' . $s . 'x' . $s . '.' . $file_extension; | |
$basic_user_avatar_path_array[ $s . '_path' ] = $upload_path . $filename . '-' . $s . 'x' . $s . '.' . $file_extension; | |
} | |
} | |
// Let's make sure all the required images exists. | |
$all_images_exist = true; | |
foreach ( $basic_user_avatar_path_array as $path ) { | |
if ( ! file_exists( $path ) ) { | |
$all_images_exist = false; | |
break; | |
} | |
} | |
// Update the user meta if all the images exist. | |
if ( $all_images_exist ) { | |
// We don't need the old preview image anymore, lets delete it. | |
wp_delete_file( $basic_user_avatar['previewpath'] ); | |
// Set the meta for Basic User Avatars. | |
update_user_meta( $user_id, 'basic_user_avatar', $new_basic_user_avatar_array ); | |
} | |
} | |
} | |
} | |
add_action( 'pmpro_after_checkout', 'user_field_file_upload_to_basic_user_avatars', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment