Skip to content

Instantly share code, notes, and snippets.

@marcwieland95
Last active September 26, 2018 15:16
Show Gist options
  • Save marcwieland95/9f73dc6e461d2520c5c528e6ca95f3c7 to your computer and use it in GitHub Desktop.
Save marcwieland95/9f73dc6e461d2520c5c528e6ca95f3c7 to your computer and use it in GitHub Desktop.
WP User Object Abstraction
<?php
namespace PROJECT\User;
/**
* @author Marc Wieland <mail@marcwieland.name>
*/
class User
{
/**
* @var WP_User
*/
protected $user = null;
public function __construct(\WP_User $user = null)
{
$this->user = $user;
}
/**
* Header
* @return array
* @acfType repeater
*/
public function getHeader() {
return is_array($this->getField('header')) ? $this->getField('header') : [];
}
public function getField($selector, $trimmed = false)
{
$data = get_field($selector, sprintf('user_%s', $this->getId()));
if ($trimmed) {
$data = trim($data);
}
return $data;
}
public function getUser()
{
return $this->user;
}
private function getUserData()
{
return $this->user->data;
}
public function getId()
{
return $this->getUserData()->ID;
}
public function getName()
{
return $this->getUserData()->display_name;
}
public function getSlug()
{
return $this->getUserData()->user_nicename;
}
public function getMail()
{
return $this->getUserData()->user_email;
}
public function getCaps()
{
return $this->getUser()->caps;
}
public function getRoles()
{
return $this->getUser()->roles;
}
public function __get($name)
{
return $this->getUser()->$name;
}
public function __set($name, $value)
{
$this->getUser()->$name = $value;
}
public function __call($name, $arguments)
{
return call_user_func([$this->getUser(), $name], $arguments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment