diff --git a/admin/class-yoast-notification.php b/admin/class-yoast-notification.php index 6f54226b9ee..d4b1ea7090b 100644 --- a/admin/class-yoast-notification.php +++ b/admin/class-yoast-notification.php @@ -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' => [], @@ -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']; } /** @@ -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(); } @@ -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; } @@ -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 ); } @@ -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; diff --git a/tests/unit/admin/admin-features-test.php b/tests/unit/admin/admin-features-test.php index 80ef0eee2fd..9763e2de3c4 100644 --- a/tests/unit/admin/admin-features-test.php +++ b/tests/unit/admin/admin-features-test.php @@ -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(); }