'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', ); /** * Hook everything. * * @return void */ public function register(): void { ProfileSection::add( 'show_user_profile', array( $this, 'render_field' ), 'free', 20 ); ProfileSection::add( 'edit_user_profile', array( $this, 'render_field' ), 'free', 20 ); ProfileSection::add( 'user_new_form', array( $this, 'render_field' ), 'free', 20 ); // Core's profile forms can't carry a file unless told to. add_action( 'user_edit_form_tag', array( $this, 'form_enctype' ) ); add_action( 'user_new_form_tag', array( $this, 'form_enctype' ) ); add_action( 'user_profile_update_errors', array( $this, 'check_upload' ), 10, 3 ); add_action( 'personal_options_update', array( $this, 'save_avatar' ) ); add_action( 'edit_user_profile_update', array( $this, 'save_avatar' ) ); add_action( 'user_register', array( $this, 'save_avatar' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'pre_get_avatar_data', array( $this, 'resolve_avatar' ), 10, 2 ); } /** * Let the profile form send the plain avatar upload (for users without * the Media Library), which it can't by default. * * @return void */ public function form_enctype(): void { if ( ! $this->can_use_media_library( get_current_user_id() ) && $this->allow_all_users() ) { echo ' enctype="multipart/form-data"'; } } /** * Refuse the profile save with a message when the chosen avatar file * can't be used, instead of saving everything else and dropping it. * * @param \WP_Error $errors Errors so far. * @param bool $update Whether an existing user is being updated. * @param \stdClass $user The user being saved. * @return void */ public function check_upload( \WP_Error $errors, bool $update, $user ): void { unset( $update, $user ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- core checked the profile form's nonce before this hook; save_avatar() checks this field's own. $file = $_FILES['aiou_user_avatar_file'] ?? null; if ( ! is_array( $file ) || empty( $file['name'] ) || $this->can_use_media_library( get_current_user_id() ) || ! $this->allow_all_users() ) { return; } $type = wp_check_filetype_and_ext( (string) $file['tmp_name'], sanitize_file_name( (string) $file['name'] ), self::MIMES ); if ( ! empty( $file['error'] ) || (int) $file['size'] > wp_max_upload_size() ) { $errors->add( 'aiou_avatar_upload', sprintf( /* translators: %s: maximum upload size */ __( 'The avatar could not be uploaded. Please choose an image smaller than %s.', 'all-in-one-utilities' ), size_format( wp_max_upload_size() ) ) ); } elseif ( empty( $type['type'] ) ) { $errors->add( 'aiou_avatar_upload', __( 'The avatar must be a JPG, PNG, GIF or WebP image.', 'all-in-one-utilities' ) ); } } /** * Whether all logged-in users (not just those with Media Library * access) may set a custom avatar. * * @return bool */ private function allow_all_users(): bool { return (bool) aiou_get_setting( 'users', 'user_avatar', 'allow_all_users', 0 ); } /** * Whether the given user (defaults to the current user) gets the * Media Library picker rather than the plain upload fallback. * * @param int $user_id User id of the person doing the editing. * @return bool */ private function can_use_media_library( int $user_id ): bool { return user_can( $user_id, 'upload_files' ); } /** * Enqueue the media picker script/styles on profile screens only. * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue_assets( string $hook_suffix ): void { if ( ! in_array( $hook_suffix, array( 'profile.php', 'user-edit.php', 'user-new.php' ), true ) ) { return; } if ( ! $this->can_use_media_library( get_current_user_id() ) ) { return; } wp_enqueue_media(); wp_enqueue_style( 'aiou-admin', AIOU_URL . 'assets/css/admin.css', array(), \WordTenance\AllInOneUtilities\Admin\Menu::asset_version( 'assets/css/admin.css' ) ); wp_enqueue_script( 'aiou-admin', AIOU_URL . 'assets/js/admin-settings.js', array(), \WordTenance\AllInOneUtilities\Admin\Menu::asset_version( 'assets/js/admin-settings.js' ), true ); wp_localize_script( 'aiou-admin', 'aiouAdmin', array( 'mediaTitle' => __( 'Select avatar', 'all-in-one-utilities' ), 'mediaButton' => __( 'Use this image', 'all-in-one-utilities' ), ) ); } /** * Render the avatar field on the profile/add-new-user/edit-user screens. * * @param WP_User|string $user_or_type A WP_User on show/edit_user_profile, or a type string on user_new_form. * @return void */ public function render_field( $user_or_type ): void { $user = $user_or_type instanceof WP_User ? $user_or_type : null; $viewer = get_current_user_id(); $use_lib = $this->can_use_media_library( $viewer ); if ( ! $use_lib && ! $this->allow_all_users() ) { return; } $current_id = $user ? absint( get_user_meta( $user->ID, self::META_KEY, true ) ) : 0; wp_nonce_field( self::NONCE_ACTION, self::NONCE_FIELD ); ?>

can_use_media_library( $viewer ) ) { if ( ! isset( $_POST['aiou_user_avatar_id'] ) ) { return; } $attachment_id = absint( $_POST['aiou_user_avatar_id'] ); if ( $attachment_id && wp_attachment_is_image( $attachment_id ) ) { update_user_meta( $user_id, self::META_KEY, $attachment_id ); } else { delete_user_meta( $user_id, self::META_KEY ); } return; } if ( ! $this->allow_all_users() ) { return; } if ( ! empty( $_POST['aiou_user_avatar_remove'] ) ) { delete_user_meta( $user_id, self::META_KEY ); return; } if ( empty( $_FILES['aiou_user_avatar_file']['name'] ) ) { return; } $attachment_id = $this->handle_upload( $_FILES['aiou_user_avatar_file'], $user_id ); if ( $attachment_id ) { update_user_meta( $user_id, self::META_KEY, $attachment_id ); } } /** * Validate and process a plain file upload into a new attachment. * * @param array $file The $_FILES entry. * @param int $user_id The uploading user (becomes the attachment's author, same as any other upload). * @return int Attachment id, or 0 on failure. */ private function handle_upload( array $file, int $user_id ): int { if ( ! empty( $file['error'] ) ) { return 0; } if ( $file['size'] > wp_max_upload_size() ) { return 0; } if ( ! function_exists( 'wp_handle_upload' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; } $overrides = array( 'test_form' => false, 'mimes' => self::MIMES, ); $moved = wp_handle_upload( $file, $overrides ); if ( ! isset( $moved['file'], $moved['url'], $moved['type'] ) ) { return 0; } $attachment_id = wp_insert_attachment( array( 'guid' => $moved['url'], 'post_mime_type' => $moved['type'], 'post_title' => sanitize_file_name( pathinfo( $file['name'], PATHINFO_FILENAME ) ), 'post_content' => '', 'post_status' => 'inherit', 'post_author' => $user_id, ), $moved['file'] ); if ( ! $attachment_id ) { return 0; } wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $moved['file'] ) ); return (int) $attachment_id; } /** * Resolve a custom avatar (or the configured fallback) via the single * modern avatar-resolution hook. * * @param array $args Avatar arguments (see `get_avatar_data()`). * @param int|string|object $id_or_email User id/email, or a WP_User/WP_Post/WP_Comment. * @return array */ public function resolve_avatar( array $args, $id_or_email ): array { $user_id = $this->resolve_user_id( $id_or_email ); if ( $user_id ) { $attachment_id = absint( get_user_meta( $user_id, self::META_KEY, true ) ); if ( $attachment_id && wp_attachment_is_image( $attachment_id ) ) { $size = (int) ( $args['size'] ?? 96 ); $src = wp_get_attachment_image_src( $attachment_id, array( $size, $size ) ); if ( $src ) { $args['url'] = $src[0]; $args['found_avatar'] = true; return $args; } } } if ( aiou_get_setting( 'users', 'user_avatar', 'disable_gravatar', 0 ) ) { $default_id = absint( aiou_get_setting( 'users', 'user_avatar', 'default_avatar', 0 ) ); $size = (int) ( $args['size'] ?? 96 ); if ( $default_id && wp_attachment_is_image( $default_id ) ) { $src = wp_get_attachment_image_src( $default_id, array( $size, $size ) ); if ( $src ) { $args['url'] = $src[0]; return $args; } } $args['url'] = includes_url( 'images/blank.gif' ); return $args; } return $args; } /** * Resolve the various shapes `pre_get_avatar_data` can receive down to * a user id, or 0 if it doesn't correspond to a registered user. * * @param int|string|object $id_or_email User id/email, or a WP_User/WP_Post/WP_Comment. * @return int */ private function resolve_user_id( $id_or_email ): int { if ( $id_or_email instanceof WP_User ) { return $id_or_email->ID; } if ( $id_or_email instanceof WP_Post ) { return (int) $id_or_email->post_author; } if ( $id_or_email instanceof WP_Comment ) { if ( ! empty( $id_or_email->user_id ) ) { return (int) $id_or_email->user_id; } if ( ! empty( $id_or_email->comment_author_email ) ) { $user = get_user_by( 'email', $id_or_email->comment_author_email ); return $user ? $user->ID : 0; } return 0; } if ( is_numeric( $id_or_email ) ) { return (int) $id_or_email; } if ( is_string( $id_or_email ) && is_email( $id_or_email ) ) { $user = get_user_by( 'email', $id_or_email ); return $user ? $user->ID : 0; } return 0; } }