Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add is_current and profile_link methods #2924

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 45 additions & 7 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
*/
public static function build(WP_User $wp_user): self
{
$user = new static();

Check failure on line 111 in src/User.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Unsafe usage of new static().
$user->init($wp_user);

return $user;
Expand Down Expand Up @@ -200,6 +200,18 @@
return $this->meta($field_name);
}

/**
* Check if the user object is the current user
*
* @api
*
* @return bool true if the user is the current user
*/
public function is_current(): bool
{
return \get_current_user_id() === $this->ID;
}

/**
* Get the name of the User
*
Expand All @@ -216,7 +228,7 @@
* @param string $name The name of the user. Default `display_name`.
* @param User $user The user object.
*/
return \apply_filters('timber/user/name', $this->display_name, $this);

Check failure on line 231 in src/User.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Access to an undefined property Timber\User::$display_name.
}

/**
Expand Down Expand Up @@ -279,13 +291,13 @@
}

/**
* Creates an associative array with user role slugs and their translated names.
*
* @internal
* @since 1.8.5
* @param array $roles user roles.
* @return array|null
*/
* Creates an associative array with user role slugs and their translated names.
*
* @internal
* @since 1.8.5
* @param array $roles user roles.
* @return array|null
*/
protected function get_roles($roles)
{
if (empty($roles)) {
Expand Down Expand Up @@ -345,6 +357,32 @@
return $this->roles;
}

/**
* Gets the profile link to the user’s profile in the WordPress admin if the ID in the user object
* is the same as the current user’s ID.
*
* @api
* @since 2.1.0
* @example
*
* Get the profile URL for the current user:
*
* ```twig
* {% if user.profile_link %}
* <a href="{{ user.profile_link }}">My profile</a>
* {% endif %}
* ```
* @return string|null The profile link for the current user.
*/
public function profile_link(): ?string
{
if (!$this->is_current()) {
return null;
}

return \get_edit_profile_url($this->ID);
}

/**
* Checks whether a user has a capability.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/test-timber-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,34 @@ public function testLinks()
$this->assertEquals('16th', $user->president);
}

public function testIsCurrent()
{
$uid = $this->factory->user->create([
'display_name' => 'Charles vonderZwen',
'user_login' => 'cvanderzwen',
]);

$user = Timber::get_user($uid);

wp_set_current_user($uid);

$this->assertTrue($user->is_current());
}

public function testProfileLink()
{
$uid = $this->factory->user->create([
'display_name' => 'Boaty McBoatface',
'user_login' => 'BMcBoatface',
]);

wp_set_current_user($uid);

$user = Timber::get_user($uid);

$this->assertEquals('http://example.org/wp-admin/profile.php', $user->profile_link());
}

public function testAvatar()
{
// Restore integration-free Class Map for users.
Expand Down