OpenSanghaFoundation / OSF

Bug/Feature requests tracking and documentation managament
https://opensanghafoundation.org
0 stars 0 forks source link

User profile photo added in the registration page disappeared #18

Closed coiby closed 2 months ago

coiby commented 2 months ago

Describe the bug

User profile photo uploaded during registration disappears.

To Reproduce Steps to reproduce the behavior:

  1. Go to https://opensanghafoundation.org/newsite/register-2/
  2. Upload a profile photo
  3. Finish registration (you'll be redirected https://opensanghafoundation.org/newsite/account)

Expected behavior The uploaded profile photo should be displayed as the avatar.

coiby commented 2 months ago

This feature Enable Profile Photo uploader in Register and Account forms can be easily enabled after installing the um-profile-photo extention locally but it's still broken after installing it upon OSF. After re-adding the "Profile Photo" field in the registration form, the features works as expected.

One clue I find is when the feature is broken, the uploaded image is named as profile_photo_temp.jpeg temporarily before finishing registration. As a comparison, when this feature works, the name is profile_photo_bdaab7392d4c5c2d986723414bd0a573_temp.jpeg.

I managed to locate the logical of naming a temp image and find the unique_filename_callback handler isn't registered to photo_name because UM()->uploader()->upload_image_type == profile_photo for OSF,

// plugins/um-profile-photo/src/Core.php
    public function upload_handler( $override_handler ) {

        if ( 'stream_photo' === UM()->uploader()->upload_image_type && 'register_profile_photo' === UM()->uploader()->field_key ) {
            if ( defined( 'UM_IS_EXTENDED' ) ) {
                $override_handler['unique_filename_callback'] = array( um_extended_plugin()->profile_photo(), 'photo_name' );
            } else {
                $override_handler['unique_filename_callback'] = array( um_extended_profilephoto_plugin(), 'photo_name' );
            }
        }

        return $override_handler;
    }

    /**
     * Change filename
     *
     * @param string $dir Directory name.
     * @param string $filename Uploading file name.
     * @param string $ext Extension of the uploading file.
     */
    public function photo_name( $dir, $filename, $ext ) {

        $temp_profile_id = isset( $_COOKIE['um-register-profile-photo'] ) ? sanitize_key( $_COOKIE['um-register-profile-photo'] ) : null;

        if ( empty( $ext ) ) {
                $image_type = wp_check_filetype( $filename );
                $ext        = strtolower( trim( $image_type['ext'], ' \/.' ) );
        } else {
                $ext = strtolower( trim( $ext, ' \/.' ) );
        }

        $filename = "profile_photo_{$temp_profile_id}_temp.{$ext}";

        UM()->uploader()->delete_existing_file( $filename, $ext, $dir );

        return $filename;
    }

And the reason for UM()->uploader()->upload_image_type == profile_photo" is because $field_key=profile_photo for OSF,

// plugins/ultimate-member/includes/core/class-uploader.php
        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' );
            }
            if ( empty( $field_key ) ) {
                $field_key = 'custom_field';
            }

            $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;
            }

The field_key comes from the old version of extension which define the metakey as profile_photo,

function um_predefined_fields_hook_profile_photo( $arr ) {

    $arr['profile_photo'] = array(
        'title' => __( 'Profile Photo', 'ultimate-member' ),
        'metakey' => 'profile_photo',
        'type' => 'image',
        ...
    );

    return $arr;

}
add_filter( 'um_predefined_fields_hook', 'um_predefined_fields_hook_profile_photo', 99999, 1 );

The new version actually defines it as register_profile_photo

    public function predefined_fields( $arr ) {

        $arr['register_profile_photo'] = array(
            'title'       => __( 'Profile Photo', 'um-extended' ),
            'metakey'     => 'register_profile_photo',
            'type'        => 'image',
            ...
        );
        return $arr;
    }