Skip to content

Commit

Permalink
Update to user_displayname that fixes unintended caching of first pre…
Browse files Browse the repository at this point in the history
…ferences.

Closes #1863. Code from @andrew13.
  • Loading branch information
adamfairholm committed Sep 18, 2012
1 parent 897e2be commit 5d19310
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions system/cms/modules/users/helpers/user_helper.php
Expand Up @@ -79,37 +79,45 @@ function role_or_die($module, $role, $redirect_to = 'admin', $message = '')
* if false it returns just the display name.
* @return string
*/
function user_displayname($user, $linked = TRUE)
function user_displayname($user, $linked = true)
{
if (is_numeric($user))
{
$user = ci()->ion_auth->get_user($user);
}

$user = (array) $user;

// Static var used for cache
if ( ! isset($_users))
{
static $_users = array();
}

// check it exists
if (isset($_users[$user['id']]))
{
return $_users[$user['id']];
}

$user_name = empty($user['display_name']) ? $user['username'] : $user['display_name'];

if (ci()->settings->enable_profiles and $linked)
{
$user_name = anchor('user/'.$user['id'], $user_name);
}

$_users[$user['id']] = $user_name;

return $user_name;
// User is numeric and user hasn't been pulled yet isn't set.
if (is_numeric($user))
{
$user = ci()->ion_auth->get_user($user);
}

$user = (array) $user;
$name = empty($user['display_name']) ? $user['username'] : $user['display_name'];

// Static var used for cache
if ( ! isset($_users))
{
static $_users = array();
}

// check if it exists
if (isset($_users[$user['id']]))
{
if( ! empty( $_users[$user['id']]['profile_link'] ) and $linked)
{
return $_users[$user['id']]['profile_link'];
}
else
{
return $name;
}
}

// Set cached variable.
if (ci()->settings->enable_profiles and $linked)
{
$_users[$user['id']]['profile_link'] = anchor('user/'.$user['id'], $name);
return $_users[$user['id']]['profile_link'];
}

// Not cached, Not linked. get_user caches the result so no need to cache non linked
return $name;
}

/* End of file users/helpers/user_helper.php */

0 comments on commit 5d19310

Please sign in to comment.