phpwordpresswoocommerceavatar

The problem of not uploading the avatar on edit-account page


I use the one user avatar plugin, I added the possibility of uploading an avatar to edit-account page, But when I click on the save changes button, nothing happens. I am using the following code :

// Add enctype to form to allow image upload
function action_woocommerce_edit_account_form_tag() {
    echo 'enctype="multipart/form-data"';
} 
add_action( 'woocommerce_edit_account_form_tag', 'action_woocommerce_edit_account_form_tag' );

function action_woocommerce_edit_account_form_start() {
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <input type="file" class="wpua-file" name="wpua-file">
    </p>
    <?php
}
add_action( 'woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form_start' );

    function wpua_action_process_option_updateeee( $user_id ) {
        global  $blog_id,
                $post,
                $wpdb,
                $wp_user_avatar,
                $wpua_force_file_uploader,
                $wpua_resize_crop,
                $wpua_resize_h,
                $wpua_resize_upload,
                $wpua_resize_w,
                $wpua_admin;

        // Check if user has publish_posts capability
        if ( $wp_user_avatar->wpua_is_author_or_above() && ! $wpua_force_file_uploader ) {
            $wpua_id = isset( $_POST['wp-user-avatar'] ) ? absint( $_POST['wp-user-avatar'] ) : 0;

            // Remove old attachment postmeta
            delete_metadata( 'post', null, '_wp_attachment_wp_user_avatar', $user_id, true );

            // Create new attachment postmeta
            add_post_meta( $wpua_id, '_wp_attachment_wp_user_avatar', $user_id );

            // Update usermeta
            update_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', $wpua_id );
        } else {
            // Remove attachment info if avatar is blank
            if ( isset( $_POST['wp-user-avatar'] ) && empty( $_POST['wp-user-avatar'] ) ) {
                // Delete other uploads by user
                $q = array(
                    'author'         => $user_id,
                    'post_type'      => 'attachment',
                    'post_status'    => 'inherit',
                    'posts_per_page' => '-1',
                    'meta_query'     => array(
                        array(
                            'key'     => '_wp_attachment_wp_user_avatar',
                            'value'   => "",
                            'compare' => '!='
                        ),
                    ),
                );

                $avatars_wp_query = new WP_Query( $q );

                while( $avatars_wp_query->have_posts() ) {
                    $avatars_wp_query->the_post();

                    wp_delete_attachment( $post->ID );
                }

                wp_reset_query();

                // Remove attachment postmeta
                delete_metadata( 'post', null, '_wp_attachment_wp_user_avatar', $user_id, true );

                // Remove usermeta
                update_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', '' );
            }

            // Create attachment from upload
            if ( isset( $_POST['submit'] ) && $_POST['submit'] && ! empty( $_FILES['wpua-file'] ) ) {
                $file = $_FILES['wpua-file'];
                $name = isset( $file['name'] ) ? sanitize_file_name( $file['name'] ) : '';
                $type = isset( $file['type'] ) ? sanitize_mime_type( $file['type'] ) : '';
                $file = wp_handle_upload( $file, array(
                    'test_form' => false,
                ) );

                if ( isset( $file['url'] ) ) {
                    if ( ! empty( $type ) && preg_match( '/(jpe?g|gif|png)$/i' , $type ) ) {
                        // Resize uploaded image
                        if ( 1 == (bool) $wpua_resize_upload ) {
                            // Original image
                            $uploaded_image = wp_get_image_editor( $file['file'] );

                            // Check for errors
                            if ( ! is_wp_error( $uploaded_image ) ) {
                                // Resize image
                                $uploaded_image->resize( $wpua_resize_w, $wpua_resize_h, $wpua_resize_crop );

                                // Save image
                                $uploaded_image->save( $file['file'] );
                            }
                        }

                        // Break out file info
                        $name_parts = pathinfo( $name );
                        $name       = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) );
                        $url        = $file['url'];
                        $file       = $file['file'];
                        $title      = $name;

                        // Use image exif/iptc data for title if possible
                        if ( $image_meta = @wp_read_image_metadata( $file ) ) {
                            if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
                                $title = $image_meta['title'];
                            }
                        }

                        // Construct the attachment array
                        $attachment = array(
                            'guid'           => $url,
                            'post_mime_type' => $type,
                            'post_title'     => $title,
                            'post_content'   => '',
                        );

                        // This should never be set as it would then overwrite an existing attachment
                        if ( isset( $attachment['ID'] ) ) {
                            unset( $attachment['ID'] );
                        }

                        // Save the attachment metadata
                        $attachment_id = wp_insert_attachment( $attachment, $file );

                        if ( ! is_wp_error( $attachment_id ) ) {
                            // Delete other uploads by user
                            $q = array(
                                'author'         => $user_id,
                                'post_type'      => 'attachment',
                                'post_status'    => 'inherit',
                                'posts_per_page' => '-1',
                                'meta_query'     => array(
                                    array(
                                        'key'     => '_wp_attachment_wp_user_avatar',
                                        'value'   => '',
                                        'compare' => '!=',
                                    ),
                                ),
                            );

                            $avatars_wp_query = new WP_Query( $q );

                            while ( $avatars_wp_query->have_posts() ){
                                $avatars_wp_query->the_post();

                                wp_delete_attachment($post->ID);
                            }

                            wp_reset_query();

                            wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );

                            // Remove old attachment postmeta
                            delete_metadata( 'post', null, '_wp_attachment_wp_user_avatar', $user_id, true );

                            // Create new attachment postmeta
                            update_post_meta( $attachment_id, '_wp_attachment_wp_user_avatar', $user_id );

                            // Update usermeta
                            update_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', $attachment_id );
                        }
                    }
                }
            }
        }
    }
add_action( 'woocommerce_save_account_details', 'wpua_action_process_option_updateeee', 10, 1 );

I took wpua_action_process_option_updateeee function exactly from the avatar plugin, I spent my time one day, but I didn't realize my mistake. I'm confused, where is my mistake?


Solution

  • I found the problem, I made a slight change in function wpua_action_process_option_updateeee :

    if ( isset( $_POST['submit'] ) && $_POST['submit'] && ! empty( $_FILES['wpua-file'] ) ) {
    

    I changed to

    if ( ! empty( $_FILES['wpua-file'] ) ) {