User::photo_upload( int $user_id )

Upload Profile photo from the dashboard


Parameters

$user_id

(int) (Required) User ID


Source

File: classes/User.php

	public function photo_upload( $user_id ) {

		$files              = dozent_sanitize_array( $_FILES );
		$profile_photo      = dozent_array_get( 'dozent_profile_photo_file', $files );
		$profile_photo_size = dozent_array_get( 'size', $profile_photo );
		$profile_photo_type = dozent_array_get( 'type', $profile_photo );

		if ( $profile_photo_size && strpos( $profile_photo_type, 'image' ) !== false ) {
			if ( ! function_exists( 'wp_handle_upload' ) ) {
				require_once( ABSPATH . 'wp-admin/includes/file.php' );
			}

			$upload_overrides = array( 'test_form' => false );
			$movefile         = wp_handle_upload( $profile_photo, $upload_overrides );

			if ( $movefile && ! isset( $movefile['error'] ) ) {
				$file_path = dozent_array_get( 'file', $movefile );
				$file_url  = dozent_array_get( 'url', $movefile );

				$media_id = wp_insert_attachment( array(
					'guid'           => $file_path,
					'post_mime_type' => mime_content_type( $file_path ),
					'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ),
					'post_content'   => '',
					'post_status'    => 'inherit'
				), $file_path, 0 );

				if ( $media_id ) {
					// wp_generate_attachment_metadata() won't work if you do not include this file
					require_once( ABSPATH . 'wp-admin/includes/image.php' );

					// Generate and save the attachment metas into the database
					wp_update_attachment_metadata( $media_id,
						wp_generate_attachment_metadata( $media_id, $file_path ) );

					//Update it to user profile
					update_user_meta( $user_id, '_dozent_profile_photo', $media_id );
				}
			}
		}

	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.