'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 ); ?>
|
|