Skip to content

Commit

Permalink
Revert "Avoid storing the full WP_User object in db."
Browse files Browse the repository at this point in the history
This reverts commit f26657d.
  • Loading branch information
pls78 committed Oct 25, 2023
1 parent 21550f4 commit 0bc4b30
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
25 changes: 10 additions & 15 deletions admin/class-yoast-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Yoast_Notification {
private $defaults = [
'type' => self::UPDATED,
'id' => '',
'user_id' => null,
'user' => null,
'nonce' => null,
'priority' => 0.5,
'data_json' => [],
Expand Down Expand Up @@ -110,12 +110,12 @@ public function get_id() {
}

/**
* Retrieve the id of the user to show the notification for.
* Retrieve the user to show the notification for.
*
* @return int The user id.
* @return WP_User The user to show this notification for.
*/
public function get_user() {
return $this->options['user_id'];
return $this->options['user'];
}

/**
Expand All @@ -127,7 +127,7 @@ public function get_user() {
*/
public function get_user_id() {
if ( $this->get_user() !== null ) {
return $this->get_user();
return $this->get_user()->ID;
}
return get_current_user_id();
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public function display_for_current_user() {
*/
public function match_capabilities() {
// Super Admin can do anything.
if ( is_multisite() && is_super_admin( $this->options['user_id'] ) ) {
if ( is_multisite() && is_super_admin( $this->options['user']->ID ) ) {
return true;
}

Expand Down Expand Up @@ -280,11 +280,7 @@ public function match_capabilities() {
* @return bool
*/
private function has_capability( $capability ) {
$user_id = $this->options['user_id'];
if ( ! is_numeric( $user_id ) ) {
return false;
}
$user = get_user_by( 'id', $user_id );
$user = $this->options['user'];
return $user->has_cap( $capability );
}

Expand Down Expand Up @@ -400,10 +396,9 @@ private function normalize_options( $options ) {
$options['capabilities'] = [ 'wpseo_manage_options' ];
}

// Set to the id of the current user if not supplied.
if ( $options['user_id'] === null ) {
$user = wp_get_current_user();
$options['user_id'] = $user->ID;
// Set to the current user if not supplied.
if ( $options['user'] === null ) {
$options['user'] = wp_get_current_user();
}

return $options;
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/admin/admin-features-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ private function get_admin_with_expectations() {
->with( 'https://example.org', 'dismiss-5star-upsell' )
->andReturn( 'https://example.org?_wpnonce=test-nonce' );

$admin_user = Mockery::mock( WP_User::class );
$admin_user->ID = 1;

Monkey\Functions\expect( 'wp_get_current_user' )
->once()
->andReturn( $admin_user );
Monkey\Functions\expect( 'wp_get_current_user' )
->once()
->andReturn( Mockery::mock( WP_User::class ) );

return new WPSEO_Admin();
}
Expand Down

0 comments on commit 0bc4b30

Please sign in to comment.