/** * Helper function to check a nicename for characters that won't be converted * to ASCII characters. * * Before being saved to the db, `wp_insert_user()` converts nicenames by * running them through `sanitize_user()` with the `$strict` parameter set to * `true`, then through `sanitize_title()`. This results in a user nicename that * only contains alphanumeric characters, underscores (_) and dashes (-). Rather * than silently strip invalid characters, this function allows us to inform the * editing user that their passed user nicename contains characters that won't * make it through the `wp_insert_user()` sanitization process. * * @since 1.1.0 * * @param string $nicename The nicename to check for invalid characters. * * @return bool True if the nicename contains only ASCII characters, or * characters that can be converted to ASCII. */ function ba_eas_nicename_is_ascii($nicename = '') { return ba_eas_sanitize_nicename($nicename) === ba_eas_sanitize_nicename($nicename, false); }
/** * Prepare the user nicename for updating if applicable. * * The actual updating is handled by WP as long as no errors are thrown by WP, * some third party, or us. * * @since 0.1.0 * * @param WP_Errors $errors The WP_Errors object. * @param bool $update True if user is being updated. * @param WP_User $user The WP_User object. */ function ba_eas_update_user_nicename($errors, $update, $user) { // Bail early if user can't edit the slug. if (!ba_eas_can_edit_author_slug()) { return; } // Don't run the auto-update if the current user can update their own nicename. remove_action('profile_update', 'ba_eas_auto_update_user_nicename'); // We shouldn't be here if we're not updating. if (!$update) { return; } // Validate the user_id. if (empty($user->ID)) { return; } // Check the nonce. check_admin_referer('update-user_' . $user->ID); // Stash the original user object. $_user = get_userdata($user->ID); $user_nicename = $user_nicename_custom = ''; if (isset($_POST['ba_eas_author_slug'])) { $user_nicename = trim(wp_unslash($_POST['ba_eas_author_slug'])); } if (isset($_POST['ba_eas_author_slug_custom'])) { $user_nicename_custom = trim(wp_unslash($_POST['ba_eas_author_slug_custom'])); } // Check for a custom author slug. if ('\\c\\u\\s\\t\\o\\m' === $user_nicename) { $user_nicename = $user_nicename_custom; } // Do we have an author slug? if (empty($user_nicename)) { $errors->add('user_nicename_empty', __('<strong>ERROR</strong>: An author slug cannot be blank. Please try again.', 'edit-author-slug')); return; } // Stash author slug as it was, mostly, passed. $raw_nicename = $user_nicename; // Check to see if the passed nicename contains any invalid characters. $ascii = ba_eas_nicename_is_ascii($user_nicename); // Sanitize the author slug and cache the pre-filtered, sanitized version. $user_nicename = $raw_nicename_sanitized = ba_eas_sanitize_nicename($user_nicename); /** * Filters the sanitized user nicename before any final checks are run. * * @since 1.1.0 * * @param string $user_nicename The sanitized user nicename. * @param int $user_id The user id. * @param string $raw_nicename The un-sanitized user nicename. * @param bool $ascii True if the nicename contains only characters * that can be converted to allowed ASCII characters. */ $user_nicename = ba_eas_sanitize_nicename(apply_filters('ba_eas_pre_update_user_nicename', $user_nicename, $user->ID, $raw_nicename, $ascii)); // Was the nicename filtered? $changed = $raw_nicename_sanitized !== $user_nicename; // Reset `$ascii` if the nicename was filtered. if ($changed) { $ascii = ba_eas_nicename_is_ascii($user_nicename); } // Bail and throw an error if the nicename contains invalid characters. if (!$ascii) { $errors->add('user_nicename_invalid_characters', __('<strong>ERROR</strong>: An author slug can only contain alphanumeric characters, underscores (_) and dashes (-).', 'edit-author-slug')); return; } // Bail and throw an error if the nicename is empty after sanitization. if (empty($user_nicename)) { $errors->add('user_nicename_invalid', __('<strong>ERROR</strong>: That author slug appears to be invalid. Please try something different.', 'edit-author-slug')); return; } // Bail and throw an error if the nicename contains more than 50 characters. if (mb_strlen($user_nicename) > 50) { $errors->add('user_nicename_too_long', __('<strong>ERROR</strong>: An author slug may not be longer than 50 characters.', 'edit-author-slug')); return; } // Make sure the passed nicename is different from the user's current nicename. if ($user_nicename !== $_user->user_nicename) { // Bail and throw an error if the nicename already exists. $exists = get_user_by('slug', $user_nicename); if ($exists && (int) $exists->ID !== $user->ID) { // Setup the error message. $message = __('<strong>ERROR</strong>: The author slug, %1$s, already exists. Please try something different.', 'edit-author-slug'); // Add the error message. $errors->add('user_nicename_exists', sprintf($message, '<strong><em>' . ba_eas_esc_nicename($user_nicename) . '</em></strong>')); return; } // Looks like we made it, so let's update. $user->user_nicename = $user_nicename; // Update the nicename cache. add_action('profile_update', 'ba_eas_update_nicename_cache', 10, 2); } }