| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Model; |
| 4 |
|
| 5 |
use GraphQLRelay\Relay; |
| 6 |
use WP_User; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class User - Models the data for the User object type |
| 10 |
* |
| 11 |
* @property ?string $adminColor |
| 12 |
* @property string[]|null $capabilities |
| 13 |
* @property ?string $capKey |
| 14 |
* @property ?int $databaseId |
| 15 |
* @property ?string $description |
| 16 |
* @property ?string $email |
| 17 |
* @property string[] $enqueuedScriptsQueue |
| 18 |
* @property string[] $enqueuedStylesheetsQueue |
| 19 |
* @property string[]|null $extraCapabilities |
| 20 |
* @property ?string $firstName |
| 21 |
* @property bool $hasCommentShortcutsEnabled |
| 22 |
* @property bool $hasRichEditingEnabled |
| 23 |
* @property bool $hasSyntaxHighlightingEnabled |
| 24 |
* @property ?string $id |
| 25 |
* @property ?string $lastName |
| 26 |
* @property ?string $locale |
| 27 |
* @property ?string $name |
| 28 |
* @property ?string $nicename |
| 29 |
* @property ?string $nickname |
| 30 |
* @property ?string $registeredDate |
| 31 |
* @property string[]|null $roles |
| 32 |
* @property ?string $slug |
| 33 |
* @property string $uri |
| 34 |
* @property ?string $url |
| 35 |
* @property ?string $username |
| 36 |
* |
| 37 |
* @package WPGraphQL\Model |
| 38 |
* |
| 39 |
* @extends \WPGraphQL\Model\Model<\WP_User> |
| 40 |
*/ |
| 41 |
class User extends Model { |
| 42 |
/** |
| 43 |
* The Global Post at time of Model generation |
| 44 |
* |
| 45 |
* @var \WP_Post |
| 46 |
*/ |
| 47 |
protected $global_post; |
| 48 |
|
| 49 |
/** |
| 50 |
* The global authordata at time of Model generation |
| 51 |
* |
| 52 |
* @var \WP_User |
| 53 |
*/ |
| 54 |
protected $global_authordata; |
| 55 |
|
| 56 |
/** |
| 57 |
* User constructor. |
| 58 |
* |
| 59 |
* @param \WP_User $user The incoming WP_User object that needs modeling |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function __construct( WP_User $user ) { |
| 64 |
|
| 65 |
// Explicitly remove the user_pass early on so it doesn't show up in filters/hooks |
| 66 |
$user->user_pass = ''; |
| 67 |
$this->data = $user; |
| 68 |
|
| 69 |
$allowed_restricted_fields = [ |
| 70 |
'databaseId', |
| 71 |
'description', |
| 72 |
'enqueuedScriptsQueue', |
| 73 |
'enqueuedStylesheetsQueue', |
| 74 |
'firstName', |
| 75 |
'id', |
| 76 |
'isRestricted', |
| 77 |
'lastName', |
| 78 |
'name', |
| 79 |
'slug', |
| 80 |
'uri', |
| 81 |
'url', |
| 82 |
'userId', |
| 83 |
]; |
| 84 |
|
| 85 |
parent::__construct( 'list_users', $allowed_restricted_fields, $user->ID ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* {@inheritDoc} |
| 90 |
*/ |
| 91 |
public function setup() { |
| 92 |
global $wp_query, $post, $authordata; |
| 93 |
|
| 94 |
// Store variables for resetting at tear down |
| 95 |
$this->global_post = $post; |
| 96 |
$this->global_authordata = $authordata; |
| 97 |
|
| 98 |
if ( $this->data instanceof WP_User ) { |
| 99 |
|
| 100 |
// Reset postdata |
| 101 |
$wp_query->reset_postdata(); |
| 102 |
|
| 103 |
// Parse the query to setup global state |
| 104 |
$wp_query->parse_query( |
| 105 |
[ |
| 106 |
'author_name' => $this->data->user_nicename, |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
// Setup globals |
| 111 |
$wp_query->is_author = true; |
| 112 |
$GLOBALS['authordata'] = $this->data; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 113 |
$wp_query->queried_object = get_user_by( 'id', $this->data->ID ); |
| 114 |
$wp_query->queried_object_id = $this->data->ID; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* {@inheritDoc} |
| 120 |
*/ |
| 121 |
public function tear_down() { |
| 122 |
$GLOBALS['authordata'] = $this->global_authordata; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 123 |
$GLOBALS['post'] = $this->global_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 124 |
wp_reset_postdata(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* {@inheritDoc} |
| 129 |
*/ |
| 130 |
protected function is_private() { |
| 131 |
/** |
| 132 |
* If the user has permissions to list users. |
| 133 |
*/ |
| 134 |
if ( current_user_can( $this->restricted_cap ) ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* If the owner of the content is the current user |
| 140 |
*/ |
| 141 |
if ( true === $this->owner_matches_current_user() ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
return $this->data->is_private ?? true; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* {@inheritDoc} |
| 150 |
*/ |
| 151 |
protected function init() { |
| 152 |
if ( empty( $this->fields ) ) { |
| 153 |
$this->fields = [ |
| 154 |
'capabilities' => function () { |
| 155 |
if ( ! empty( $this->data->allcaps ) ) { |
| 156 |
|
| 157 |
/** |
| 158 |
* Reformat the array of capabilities from the user object so that it is a true |
| 159 |
* ListOf type |
| 160 |
*/ |
| 161 |
$capabilities = array_keys( |
| 162 |
array_filter( |
| 163 |
$this->data->allcaps, |
| 164 |
static function ( $cap ) { |
| 165 |
return true === $cap; |
| 166 |
} |
| 167 |
) |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
return ! empty( $capabilities ) ? $capabilities : null; |
| 172 |
}, |
| 173 |
'capKey' => function () { |
| 174 |
return ! empty( $this->data->cap_key ) ? $this->data->cap_key : null; |
| 175 |
}, |
| 176 |
'databaseId' => function () { |
| 177 |
return ! empty( $this->data->ID ) ? absint( $this->data->ID ) : null; |
| 178 |
}, |
| 179 |
'description' => function () { |
| 180 |
return ! empty( $this->data->description ) ? $this->data->description : null; |
| 181 |
}, |
| 182 |
'email' => function () { |
| 183 |
return ! empty( $this->data->user_email ) ? $this->data->user_email : null; |
| 184 |
}, |
| 185 |
'enqueuedScriptsQueue' => static function () { |
| 186 |
global $wp_scripts; |
| 187 |
do_action( 'wp_enqueue_scripts' ); |
| 188 |
$queue = $wp_scripts->queue; |
| 189 |
$wp_scripts->reset(); |
| 190 |
$wp_scripts->queue = []; |
| 191 |
|
| 192 |
return $queue; |
| 193 |
}, |
| 194 |
'enqueuedStylesheetsQueue' => static function () { |
| 195 |
global $wp_styles; |
| 196 |
do_action( 'wp_enqueue_scripts' ); |
| 197 |
$queue = $wp_styles->queue; |
| 198 |
$wp_styles->reset(); |
| 199 |
$wp_styles->queue = []; |
| 200 |
|
| 201 |
return $queue; |
| 202 |
}, |
| 203 |
'extraCapabilities' => function () { |
| 204 |
return ! empty( $this->data->allcaps ) ? array_keys( $this->data->allcaps ) : null; |
| 205 |
}, |
| 206 |
'firstName' => function () { |
| 207 |
return ! empty( $this->data->first_name ) ? $this->data->first_name : null; |
| 208 |
}, |
| 209 |
'id' => function () { |
| 210 |
return ( ! empty( $this->data->ID ) ) ? Relay::toGlobalId( 'user', (string) $this->data->ID ) : null; |
| 211 |
}, |
| 212 |
'lastName' => function () { |
| 213 |
return ! empty( $this->data->last_name ) ? $this->data->last_name : null; |
| 214 |
}, |
| 215 |
'locale' => function () { |
| 216 |
$user_locale = get_user_locale( $this->data ); |
| 217 |
|
| 218 |
return ! empty( $user_locale ) ? $user_locale : null; |
| 219 |
}, |
| 220 |
'nicename' => function () { |
| 221 |
return ! empty( $this->data->user_nicename ) ? $this->data->user_nicename : null; |
| 222 |
}, |
| 223 |
'name' => function () { |
| 224 |
return ! empty( $this->data->display_name ) ? $this->data->display_name : null; |
| 225 |
}, |
| 226 |
'nickname' => function () { |
| 227 |
return ! empty( $this->data->nickname ) ? $this->data->nickname : null; |
| 228 |
}, |
| 229 |
'registeredDate' => function () { |
| 230 |
$timestamp = ! empty( $this->data->user_registered ) ? strtotime( $this->data->user_registered ) : null; |
| 231 |
return ! empty( $timestamp ) ? gmdate( 'c', $timestamp ) : null; |
| 232 |
}, |
| 233 |
'roles' => function () { |
| 234 |
return ! empty( $this->data->roles ) ? $this->data->roles : null; |
| 235 |
}, |
| 236 |
'shouldShowAdminToolbar' => function () { |
| 237 |
$toolbar_preference_meta = get_user_meta( $this->data->ID, 'show_admin_bar_front', true ); |
| 238 |
|
| 239 |
return 'true' === $toolbar_preference_meta; |
| 240 |
}, |
| 241 |
'adminColor' => function () { |
| 242 |
$admin_color = get_user_option( 'admin_color', $this->data->ID ); |
| 243 |
|
| 244 |
return ! empty( $admin_color ) ? $admin_color : 'fresh'; |
| 245 |
}, |
| 246 |
'hasRichEditingEnabled' => function () { |
| 247 |
$rich_editing = get_user_option( 'rich_editing', $this->data->ID ); |
| 248 |
|
| 249 |
// WordPress defaults to 'true' when not set |
| 250 |
// get_user_option returns false if not set, so default to true |
| 251 |
if ( false === $rich_editing || '' === $rich_editing ) { |
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
// WordPress stores these as strings 'true' or 'false' |
| 256 |
return 'true' === $rich_editing; |
| 257 |
}, |
| 258 |
'hasSyntaxHighlightingEnabled' => function () { |
| 259 |
$syntax_highlighting = get_user_option( 'syntax_highlighting', $this->data->ID ); |
| 260 |
|
| 261 |
// WordPress defaults to 'true' when not set |
| 262 |
// get_user_option returns false if not set, so default to true |
| 263 |
if ( false === $syntax_highlighting || '' === $syntax_highlighting ) { |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
// WordPress stores these as strings 'true' or 'false' |
| 268 |
return 'true' === $syntax_highlighting; |
| 269 |
}, |
| 270 |
'hasCommentShortcutsEnabled' => function () { |
| 271 |
$comment_shortcuts = get_user_option( 'comment_shortcuts', $this->data->ID ); |
| 272 |
|
| 273 |
// WordPress defaults to 'false' when not set |
| 274 |
// get_user_option returns false if not set, so default to false |
| 275 |
if ( false === $comment_shortcuts || '' === $comment_shortcuts ) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
// WordPress stores these as strings 'true' or 'false' |
| 280 |
return 'true' === $comment_shortcuts; |
| 281 |
}, |
| 282 |
'slug' => function () { |
| 283 |
return ! empty( $this->data->user_nicename ) ? $this->data->user_nicename : null; |
| 284 |
}, |
| 285 |
'uri' => function () { |
| 286 |
$user_profile_url = get_author_posts_url( $this->data->ID ); |
| 287 |
|
| 288 |
return ! empty( $user_profile_url ) ? str_ireplace( home_url(), '', $user_profile_url ) : ''; |
| 289 |
}, |
| 290 |
'url' => function () { |
| 291 |
return ! empty( $this->data->user_url ) ? $this->data->user_url : null; |
| 292 |
}, |
| 293 |
'username' => function () { |
| 294 |
return ! empty( $this->data->user_login ) ? $this->data->user_login : null; |
| 295 |
}, |
| 296 |
|
| 297 |
// Deprecated. |
| 298 |
'userId' => function () { |
| 299 |
return $this->databaseId; |
| 300 |
}, |
| 301 |
]; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|