Skip to content

Commit

Permalink
Merge pull request #2763 from blakewilson/support-viewer-toolbar-visi…
Browse files Browse the repository at this point in the history
…bility

feat: support "Show Toolbar when viewing site" user setting
  • Loading branch information
jasonbahl authored Apr 12, 2023
2 parents e386ff9 + 9835f7a commit feb902c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ function ( $cap ) {

return ! empty( $user_locale ) ? $user_locale : null;
},
'shouldShowAdminToolbar' => function () {
$toolbar_preference_meta = get_user_meta( $this->data->ID, 'show_admin_bar_front', true );

return 'true' === $toolbar_preference_meta ? true : false;
},
'userId' => ! empty( $this->data->ID ) ? absint( $this->data->ID ) : null,
'uri' => function () {
$user_profile_url = get_author_posts_url( $this->data->ID );
Expand Down
44 changes: 24 additions & 20 deletions src/Type/ObjectType/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,90 +74,94 @@ public static function register_type() {
],
],
'fields' => [
'id' => [
'id' => [
'description' => __( 'The globally unique identifier for the user object.', 'wp-graphql' ),
],
'databaseId' => [
'databaseId' => [
'type' => [ 'non_null' => 'Int' ],
'description' => __( 'Identifies the primary key from the database.', 'wp-graphql' ),
'resolve' => function ( \WPGraphQL\Model\User $user ) {
return absint( $user->userId );
},
],
'capabilities' => [
'capabilities' => [
'type' => [
'list_of' => 'String',
],
'description' => __( 'A list of capabilities (permissions) granted to the user', 'wp-graphql' ),
],
'capKey' => [
'capKey' => [
'type' => 'String',
'description' => __( 'User metadata option name. Usually it will be "wp_capabilities".', 'wp-graphql' ),
],
'email' => [
'email' => [
'type' => 'String',
'description' => __( 'Email address of the user. This is equivalent to the WP_User->user_email property.', 'wp-graphql' ),
],
'firstName' => [
'firstName' => [
'type' => 'String',
'description' => __( 'First name of the user. This is equivalent to the WP_User->user_first_name property.', 'wp-graphql' ),
],
'lastName' => [
'lastName' => [
'type' => 'String',
'description' => __( 'Last name of the user. This is equivalent to the WP_User->user_last_name property.', 'wp-graphql' ),
],
'extraCapabilities' => [
'extraCapabilities' => [
'type' => [
'list_of' => 'String',
],
'description' => __( 'A complete list of capabilities including capabilities inherited from a role. This is equivalent to the array keys of WP_User->allcaps.', 'wp-graphql' ),
],
'description' => [
'description' => [
'type' => 'String',
'description' => __( 'Description of the user.', 'wp-graphql' ),
],
'username' => [
'username' => [
'type' => 'String',
'description' => __( 'Username for the user. This field is equivalent to WP_User->user_login.', 'wp-graphql' ),
],
'name' => [
'name' => [
'type' => 'String',
'description' => __( 'Display name of the user. This is equivalent to the WP_User->dispaly_name property.', 'wp-graphql' ),
],
'registeredDate' => [
'registeredDate' => [
'type' => 'String',
'description' => __( 'The date the user registered or was created. The field follows a full ISO8601 date string format.', 'wp-graphql' ),
],
'nickname' => [
'nickname' => [
'type' => 'String',
'description' => __( 'Nickname of the user.', 'wp-graphql' ),
],
'url' => [
'url' => [
'type' => 'String',
'description' => __( 'A website url that is associated with the user.', 'wp-graphql' ),
],
'slug' => [
'slug' => [
'type' => 'String',
'description' => __( 'The slug for the user. This field is equivalent to WP_User->user_nicename', 'wp-graphql' ),
],
'nicename' => [
'nicename' => [
'type' => 'String',
'description' => __( 'The nicename for the user. This field is equivalent to WP_User->user_nicename', 'wp-graphql' ),
],
'locale' => [
'locale' => [
'type' => 'String',
'description' => __( 'The preferred language locale set for the user. Value derived from get_user_locale().', 'wp-graphql' ),
],
'userId' => [
'userId' => [
'type' => 'Int',
'description' => __( 'The Id of the user. Equivalent to WP_User->ID', 'wp-graphql' ),
'deprecationReason' => __( 'Deprecated in favor of the databaseId field', 'wp-graphql' ),
],
'isRestricted' => [
'isRestricted' => [
'type' => 'Boolean',
'description' => __( 'Whether the object is restricted from the current viewer', 'wp-graphql' ),
],
'avatar' => [
'shouldShowAdminToolbar' => [
'type' => 'Boolean',
'description' => __( 'Whether the Toolbar should be displayed when the user is viewing the site.', 'wp-graphql' ),
],
'avatar' => [
'args' => [
'size' => [
'type' => 'Int',
Expand Down
60 changes: 60 additions & 0 deletions tests/wpunit/ShouldShowAdminToolbarQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

class ShouldShowAdminToolbarQueryTest extends \Codeception\TestCase\WPTestCase {

public function setUp(): void {
parent::setUp();
WPGraphQL::clear_schema();
}

public function tearDown(): void {
WPGraphQL::clear_schema();
parent::tearDown();
}

public function testViewerQuery() {

$user_id = $this->factory->user->create( [
'role' => 'administrator',
] );

$query = '
{
viewer{
userId
roles {
nodes {
name
}
}
shouldShowAdminToolbar
}
}
';

/**
* Set the current user so we can properly test the viewer query
*/
wp_set_current_user( $user_id );

$actual = graphql( [ 'query' => $query ] );

codecept_debug( $actual );

$this->assertArrayNotHasKey( 'errors', $actual );
$this->assertEquals( $user_id, $actual['data']['viewer']['userId'] );
$this->assertSame( true, $actual['data']['viewer']['shouldShowAdminToolbar'] );

// Update the user's preference to not show admin bar.
update_user_meta( $user_id, 'show_admin_bar_front', "false" );

$actual = graphql( [ 'query' => $query ] );

codecept_debug( $actual );

$this->assertArrayNotHasKey( 'errors', $actual );
$this->assertEquals( $user_id, $actual['data']['viewer']['userId'] );
$this->assertSame( false, $actual['data']['viewer']['shouldShowAdminToolbar'] );
}

}

0 comments on commit feb902c

Please sign in to comment.