Last active
December 31, 2015 00:39
-
-
Save trovster/7908923 to your computer and use it in GitHub Desktop.
Partly stripped down Classy_User class which makes interacting with a logged in WordPress user much easier.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Classy_User extends WP_User { | |
protected $_post_type = 'user', | |
$_favourites = null; | |
protected static $instance = null; | |
protected static $form_fields = array( | |
'sector' => array( | |
'default' => false, | |
'type' => 'text', | |
'label' => 'Sector', | |
), | |
); | |
/** | |
* getInstance | |
* @desc | |
* @return Classy_User|boolean | |
*/ | |
public static function getInstance() { | |
if (!isset(static::$instance)) { | |
if(Classy_User::is_logged_in()) { | |
$user = Classy_User::find_by_id(wp_get_current_user()->ID); | |
return $user; | |
} else { | |
return new Classy_User(); | |
} | |
} | |
return static::$instance; | |
} | |
/** | |
* __construct | |
* @desc | |
* @param int $id | |
* @param string $name | |
* @param int $blog_id | |
* @return Classy_User | |
*/ | |
public function __construct($id = 0, $name = '', $blog_id = '') { | |
if($id === 'setup') { | |
add_action('init', array(self::getInstance(), 'update_profile')); | |
add_action('init', array(self::getInstance(), 'update_profile_password')); | |
add_action('show_user_profile', array(self::getInstance(), 'show_edit_user_profile')); | |
add_action('edit_user_profile', array(self::getInstance(), 'show_edit_user_profile')); | |
add_action('edit_user_profile_update', array(self::getInstance(), 'edit_user_profile_update')); | |
add_action('personal_options_update', array(self::getInstance(), 'edit_user_profile_update')); | |
add_filter('user_profile_update_errors', array(self::getInstance(), 'user_profile_update_errors'), 10, 3); | |
add_filter('manage_users_columns', array(&$this, 'manage_user_columns')); | |
add_filter('manage_users_custom_column', array(&$this, 'manage_columns'), 10, 3); | |
add_action('init', array(&$this, 'register_images')); | |
add_action('init', array(&$this, 'register_rewrite_rules')); | |
add_action('wp_authenticate', array(&$this, 'wp_authenticate_with_email_address')); | |
add_action('admin_init', array(&$this, 'restrict_role_menu')); | |
add_action('wp_dashboard_setup', array(&$this, 'wp_dashboard_setup')); | |
add_action('wp_before_admin_bar_render', array(&$this, 'wp_before_admin_bar_render')); | |
add_action('profile_update', array(&$this, 'action_check_background'), 10, 2); | |
add_action('profile_update', array(&$this, 'action_check_board'), 10, 2); | |
add_action('user_register', array(&$this, 'action_check_background'), 10, 1); | |
add_action('user_register', array(&$this, 'action_check_board'), 10, 1); | |
add_action('delete_attachment', array(&$this, 'action_delete_attachment'), 10, 1); | |
add_filter('user_profile', array(&$this, 'filter_user_profile'), 10); | |
add_filter('user_like', array(&$this, 'filter_user_like'), 10); | |
add_filter('user_dislike', array(&$this, 'filter_user_dislike'), 10); | |
add_filter('author_link', array(&$this, 'filter_author_link'), 10, 3); | |
} else { | |
parent::__construct($id, $name, $blog_id); | |
} | |
return $this; | |
} | |
/** | |
* forge | |
* @param array $data | |
* @return instance | |
*/ | |
public static function forge($id = 0, $name = '', $blog_id = '') { | |
return new static($id, $name, $blog_id); | |
} | |
/** | |
* find_by_id | |
* @param int $id | |
* @return mixed | |
*/ | |
public static function find_by_id($id, $name = '', $blog_id = '') { | |
$user = get_user_by('id', $id); | |
if(is_object($user)) { | |
return self::forge($id, $name, $blog_id); | |
} | |
return false; | |
} | |
/** | |
* __isset | |
* @desc Also checks the value isn't empty | |
* @param string $key | |
* @return boolean | |
*/ | |
function __isset($key) { | |
$value = parent::__isset($key); | |
return $value ? true : false; | |
} | |
/** | |
* __set | |
* @param string $key | |
* @param string $value | |
*/ | |
function __set($key, $value) { | |
parent::__set($key, $value); | |
if($key === '_errors') { | |
$this->set_errors($value); | |
unset($this->data->$key); | |
} | |
} | |
/** | |
* get_form_fields | |
* @desc | |
* @return array | |
*/ | |
public static function get_form_fields() { | |
return static::$form_fields; | |
} | |
/** | |
* register_rewrite_rules | |
* @desc Custom rewrite rules | |
*/ | |
public function register_rewrite_rules() {} | |
/** | |
* register_images | |
* @desc | |
*/ | |
public function register_images() {} | |
/** | |
* get_post_type | |
* @return string | |
*/ | |
public function get_post_type() { | |
if(!empty($this->_post)) { | |
return $this->_post->post_type; | |
} | |
return (string) $this->_post_type; | |
} | |
/** | |
* is_logged_in | |
* @desc Proxy to is_user_logged_in | |
* @return type | |
*/ | |
public static function is_logged_in() { | |
return is_user_logged_in(); | |
} | |
/********************************************************* | |
* =Public | |
* @desc Public methods for getting data | |
*********************************************************/ | |
/** | |
* get_classes | |
* @desc | |
* @return array | |
*/ | |
public function get_classes() { | |
$classes = array(); | |
return $classes; | |
} | |
/** | |
* get_the_ID | |
* @desc | |
* @return int | |
*/ | |
public function get_the_ID() { | |
return $this->ID; | |
} | |
/** | |
* the_ID | |
* @desc | |
*/ | |
public function the_ID() { | |
echo $this->get_the_ID(); | |
} | |
/** | |
* get_username | |
* @desc | |
* @return string | |
*/ | |
public function get_username() { | |
return $this->user_login; | |
} | |
/** | |
* get_user_role | |
* @desc | |
* @return string | |
*/ | |
public function get_user_role() { | |
return array_shift($this->roles); | |
} | |
/** | |
* has_permalink | |
* @desc | |
* @return boolean | |
*/ | |
public function has_permalink() { | |
return false; | |
} | |
/** | |
* get_permalink | |
* @desc | |
* @param boolean $leavename | |
* @return string | |
*/ | |
public function get_permalink($leavename = false) { | |
return $this->has_permalink() ? '#' : '#'; | |
} | |
/** | |
* the_permalink | |
* @desc | |
*/ | |
public function the_permalink() { | |
echo apply_filters('the_permalink', $this->get_permalink()); | |
} | |
/** | |
* get_display_name | |
* @desc Show the full name, then display name, finally user login | |
* @return string | |
*/ | |
public function get_display_name() { | |
$full_name = $this->get_full_name(); | |
return !empty($full_name) ? $full_name : (isset($this->display_name) && !empty($this->display_name) ? $this->display_name : $this->user_login); | |
} | |
public function get_the_title() { | |
return $this->get_display_name(); | |
} | |
public function the_title() { | |
echo $this->get_the_title(); | |
} | |
/** | |
* get_display_name_slug | |
* @desc Slug for display name | |
* @return string | |
*/ | |
public function get_display_name_slug() { | |
return sanitize_title($this->get_display_name()); | |
} | |
/** | |
* has_forename | |
* @desc Checks the forename exists and is not empty | |
* @return boolean | |
*/ | |
public function has_forename() { | |
return isset($this->first_name) && !empty($this->first_name) ? true : false; | |
} | |
/** | |
* get_forename | |
* @desc Returns the built in 'first_name' meta data | |
* @return string | |
*/ | |
public function get_forename() { | |
return $this->has_forename() ? $this->first_name : ''; | |
} | |
/** | |
* has_surname | |
* @desc Checks the surname exists and is not empty | |
* @return boolean | |
*/ | |
public function has_surname() { | |
return isset($this->last_name) && !empty($this->last_name) ? true : false; | |
} | |
/** | |
* get_surname | |
* @desc Returns the built in 'last_name' meta data | |
* @return string | |
*/ | |
public function get_surname() { | |
return $this->has_surname() ? $this->last_name : ''; | |
} | |
/** | |
* get_full_name | |
* @desc If forename and surname are set, then it returns both | |
* @return string | |
*/ | |
public function get_full_name() { | |
return $this->has_forename() && $this->has_surname() ? sprintf('%s %s', $this->get_forename(), $this->get_surname()) : ''; | |
} | |
/** | |
* get_profile_text | |
* @desc Returns the built in 'description' meta data | |
* @return string | |
*/ | |
public function get_profile_text() { | |
return isset($this->description) && !empty($this->description) ? apply_filters('user_profile', $this->description) : ''; | |
} | |
public function get_the_content() { | |
return $this->get_profile_text(); | |
} | |
public function the_content() { | |
echo $this->get_the_content(); | |
} | |
/** | |
* has_profile_text | |
* @desc | |
* @return boolean | |
*/ | |
public function has_profile_text() { | |
return isset($this->description) && !empty($this->description) && strlen($this->description) > 0 ? true : false; | |
} | |
public function has_content() { | |
return $this->has_profile_text(); | |
} | |
/** | |
* get_website | |
* @desc Returns the built in 'url' meta data | |
* @return string | |
*/ | |
public function get_website() { | |
return isset($this->url) ? $this->url : ''; | |
} | |
/** | |
* get_email_address | |
* @desc Returns the built in 'user_email' meta data | |
* @return string | |
*/ | |
public function get_email_address() { | |
return isset($this->user_email) ? $this->user_email : ''; | |
} | |
/** | |
* get_email | |
* @desc Proxy to get_email_address() | |
* @return string | |
*/ | |
public function get_email() { | |
return $this->get_email_address(); | |
} | |
/** | |
* has_telephone | |
* @desc | |
* @param string $type | |
* @return boolean | |
*/ | |
public function has_telephone($type = null) { | |
switch(strtolower($type)) { | |
case 'mobile': | |
return isset($this->telephone_mobile) && !empty($this->telephone_mobile) ? true : false; | |
break; | |
case 'direct': | |
default: | |
return isset($this->telephone_direct) && !empty($this->telephone_direct) ? true : false; | |
break; | |
} | |
} | |
public function has_telephone_direct() { | |
return $this->has_telephone('direct'); | |
} | |
public function has_telephone_mobile() { | |
return $this->has_telephone('mobile'); | |
} | |
/** | |
* get_telephone | |
* @desc | |
* @param string $type | |
* @return string | |
*/ | |
public function get_telephone($type = null) { | |
switch(strtolower($type)) { | |
case 'mobile': | |
return $this->has_telephone('mobile') ? $this->telephone_mobile : ''; | |
break; | |
case 'direct': | |
default: | |
return $this->has_telephone('direct') ? $this->telephone_direct : ''; | |
break; | |
} | |
} | |
public function get_telephone_direct() { | |
return $this->get_telephone('direct'); | |
} | |
public function get_telephone_mobile() { | |
return $this->get_telephone('mobile'); | |
} | |
/** | |
* has_social | |
* @desc | |
* @param string $type | |
* @return boolean | |
*/ | |
public function has_social($type) { | |
switch(strtolower($type)) { | |
case 'facebook': | |
return isset($this->social_facebook) && !empty($this->social_facebook) ? true : false; | |
break; | |
case 'twitter': | |
return isset($this->social_twitter) && !empty($this->social_twitter) ? true : false; | |
break; | |
case 'linkedin': | |
return isset($this->social_linkedin) && !empty($this->social_linkedin) ? true : false; | |
break; | |
} | |
} | |
public function has_social_facebook() { | |
return $this->has_telephone('facebook'); | |
} | |
public function has_social_twitter() { | |
return $this->has_telephone('twitter'); | |
} | |
public function has_social_linkedin() { | |
return $this->has_telephone('linkedin'); | |
} | |
/** | |
* get_social | |
* @desc | |
* @param string $type | |
* @return string | |
*/ | |
public function get_social($type) { | |
switch(strtolower($type)) { | |
case 'facebook': | |
return $this->has_social('facebook') ? $this->social_facebook : ''; | |
break; | |
case 'twitter': | |
return $this->has_social('twitter') ? $this->social_twitter : ''; | |
break; | |
case 'linkedin': | |
return $this->has_social('linkedin') ? $this->social_linkedin : ''; | |
break; | |
} | |
} | |
public function get_social_facebook() { | |
return $this->get_social('facebook'); | |
} | |
public function get_social_twitter() { | |
return $this->get_social('twitter'); | |
} | |
public function get_social_linkedin() { | |
return $this->get_social('linkedin'); | |
} | |
/** | |
* data_attributes | |
* @desc Data attributes, including first letter for forename and surname | |
* @return string | |
*/ | |
public function data_attributes() { | |
$attributes = array(); | |
return implode(' ', $attributes); | |
} | |
/********************************************************* | |
* =Images | |
* @desc Profile image methods | |
*********************************************************/ | |
/** | |
* has_thumbnail | |
* @desc | |
* @return boolean | |
*/ | |
public function has_thumbnail() { | |
return isset($this->thumbnail_id) && is_numeric($this->thumbnail_id); | |
} | |
/** | |
* get_thumbnail_id | |
* @desc | |
* @return int | |
*/ | |
public function get_thumbnail_id() { | |
return $this->thumbnail_id; | |
} | |
/** | |
* get_thumbnail | |
* @desc | |
* @param string $size | |
* @param string|array $attr | |
* @return string | |
*/ | |
public function get_thumbnail($size = 'post-thumbnail', $attr = '') { | |
$output = ''; | |
if($this->has_thumbnail()) { | |
$post_id = $this->get_the_ID(); | |
$post_thumbnail_id = $this->get_thumbnail_id(); | |
$size = apply_filters('post_thumbnail_size', $size); | |
if($post_thumbnail_id) { | |
do_action('begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size); | |
if(in_the_loop()) { | |
update_post_thumbnail_cache(); | |
} | |
$html = wp_get_attachment_image($post_thumbnail_id, $size, false, $attr); | |
do_action('end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size); | |
} | |
$output = apply_filters('post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr); | |
} | |
return $output; | |
} | |
/** | |
* get_thumbnail_src | |
* @desc | |
* @param string $size | |
* @param string|array $attr | |
* @return array | |
*/ | |
public function get_thumbnail_src($size = 'post-thumbnail', $attr = '') { | |
if($this->has_thumbnail()) { | |
return wp_get_attachment_image_src($this->get_thumbnail_id(), $size, $attr); | |
} | |
return array(); | |
} | |
/** | |
* truncate | |
* @desc | |
* @param string $text | |
* @param int $limit | |
* @param string $append | |
* @return string | |
*/ | |
public static function truncate($text, $limit, $append = ' …') { | |
return substr($text, 0, $limit) . (strlen($text) > $limit ? $append : ''); | |
} | |
/********************************************************* | |
* =Admin | |
* @desc Admin methods | |
*********************************************************/ | |
/** | |
* action_wp_before_admin_bar_render | |
* @desc Remove post types from the header | |
* @global type $wp_admin_bar | |
*/ | |
public function wp_before_admin_bar_render() { | |
global $wp_admin_bar; | |
$role = $this->getInstance()->get_user_role(); | |
$wp_admin_bar->remove_menu('comments'); | |
$wp_admin_bar->remove_menu('wp-logo'); | |
$wp_admin_bar->remove_menu('wp-logo', 'about'); | |
$wp_admin_bar->remove_menu('wp-logo', 'wporg'); | |
$wp_admin_bar->remove_menu('wp-logo', 'documentation'); | |
$wp_admin_bar->remove_menu('wp-logo', 'support-forums'); | |
$wp_admin_bar->remove_menu('wp-logo', 'feedback'); | |
$wp_admin_bar->remove_menu('wp-logo', 'site-name'); | |
$wp_admin_bar->remove_menu('wp-logo', 'wp-logo-external'); | |
$wp_admin_bar->remove_menu('search', 'top-secondary'); | |
} | |
/** | |
* manage_columns | |
* @desc Populate the row values for the new columns | |
* @param string $value | |
* @param string $column | |
* @param int id | |
* @return string | |
*/ | |
public function manage_columns($value, $column, $id) { | |
$the_user = self::find_by_id($id); | |
switch($column) {} | |
return $value; | |
} | |
/** | |
* manage_user_columns | |
* @param array $columns | |
* @return array | |
*/ | |
public function manage_user_columns($columns) { | |
$date = $columns['date']; | |
$role = $columns['role']; | |
$posts = $columns['posts']; | |
unset($columns['date']); | |
unset($columns['role']); | |
unset($columns['posts']); | |
unset($columns['posts']); | |
unset($columns['email']); | |
unset($columns['posts']); | |
$columns['date'] = $date; | |
$columns['role'] = $role; | |
$columns['posts'] = $posts; | |
return $columns; | |
} | |
/** | |
* update_profile | |
* @desc Called when there is user edit post data | |
* @see http://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors | |
*/ | |
public function update_profile() { | |
if(empty($_POST) || empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'profile_update')) { | |
return false; | |
} | |
require_once ABSPATH . 'wp-admin/includes/user.php'; | |
if(!current_user_can('edit_user', $this->get_the_ID())) { | |
$errors = new WP_Error(); | |
$errors->add('edit_user', __('<strong>ERROR</strong>: You do not have permission to edit this user.')); | |
// wp_die(__('You do not have permission to edit this user.')); | |
} | |
else { | |
do_action('edit_user_profile_update', $this->get_the_ID()); | |
$errors = edit_user($this->get_the_ID()); | |
} | |
if(!is_wp_error($errors)) { | |
$redirect = add_query_arg('updated', true); | |
wp_redirect($redirect); | |
exit; | |
} else { | |
$this->_errors = $errors; | |
} | |
} | |
/** | |
* update_profile_password | |
* @desc Called when there is user edit password post data | |
* @see http://codex.wordpress.org/Function_Reference/wp_set_password | |
* @see http://codex.wordpress.org/Function_Reference/wp_hash_password | |
*/ | |
public function update_profile_password() { | |
if(empty($_POST) || empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'profile_update_password')) { | |
return false; | |
} | |
require_once ABSPATH . 'wp-admin/includes/user.php'; | |
if(!current_user_can('edit_user', $this->get_the_ID())) { | |
$errors = new WP_Error(); | |
$errors->add('edit_user', __('<strong>ERROR</strong>: You do not have permission to edit this user.')); | |
// wp_die(__('You do not have permission to edit this user.')); | |
} | |
// check the current password matches | |
$user = wp_authenticate($this->get_username(), $_POST['password-current']); | |
//wp_check_password($password, $hash, $user_id = ''); | |
if(is_wp_error($user)) { | |
$errors = $user; | |
} | |
else { | |
// check the new password and confirm password match | |
$password_new = trim($_POST['password-new']); | |
$password_new_confirm = trim($_POST['password-new-confirm']); | |
if(empty($password_new) || empty($password_new_confirm)) { | |
$errors = new WP_Error(); | |
$errors->add('password_missing', __('<strong>ERROR</strong>: You must provide a new password.')); | |
} | |
elseif($password_new !== $password_new_confirm) { | |
$errors = new WP_Error(); | |
$errors->add('password_mismatch', __('<strong>ERROR</strong>: Your new passwords do not match.')); | |
} | |
else { | |
$errors = wp_set_password($password_new, $this->get_the_ID()); | |
$user = wp_signon(array( | |
'user_login' => $this->get_username(), | |
'user_password' => $password_new, | |
'remember' => true, | |
)); | |
} | |
} | |
if(!is_wp_error($errors)) { | |
$redirect = add_query_arg('updated', 'password'); | |
wp_redirect($redirect); | |
exit; | |
} else { | |
$this->_errors = $errors; | |
} | |
} | |
/** | |
* wp_authenticate_with_email_address | |
* @desc Add email address to authentication type | |
* @param string $username | |
* @return string | |
*/ | |
public function wp_authenticate_with_email_address($username) { | |
$user = get_user_by('email', $username); | |
if(!empty($user->user_login)) { | |
$username = $user->user_login; | |
} | |
return $username; | |
} | |
/** | |
* has_errors | |
* @desc Check whether any errors exist in the session data | |
* @return boolean | |
*/ | |
public function has_errors() { | |
return !empty($_SESSION['errors']) ? true : false; | |
} | |
public function is_error() { | |
return $this->has_errors(); | |
} | |
/** | |
* get_errors | |
* @desc Return the errors, which should be an WP_Error object | |
* Erros are stored in the session data | |
* @return object|null | |
*/ | |
public function get_errors() { | |
$errors = null; | |
if(isset($_SESSION['errors'])) { | |
$errors = $_SESSION['errors']; | |
$this->unset_errors(); | |
} | |
return $errors; | |
} | |
/** | |
* set_errors | |
* @desc Called from the magic method __set | |
* Add these errors to the session data | |
* @param WP_Error | |
*/ | |
public function set_errors($errors) { | |
if(!empty($errors) && is_wp_error($errors)) { | |
$_SESSION['errors'] = $errors; | |
} | |
else { | |
$this->unset_errors(); | |
} | |
} | |
/** | |
* unset_errors | |
* @desc Remove the errors from the session data | |
*/ | |
public function unset_errors() { | |
if(isset($_SESSION['errors'])) { | |
unset($_SESSION['errors']); | |
} | |
} | |
/** | |
* is_success | |
* @desc Remove the errors from the session data | |
* @param $type string | |
* @return boolean | |
*/ | |
public function is_success($type = null) { | |
return !$this->has_errors() && !empty($_GET['updated']) && $_GET['updated'] === (!empty($type) && strtolower($type) === 'password' ? 'password' : '1') ? true : false; | |
} | |
/** | |
* user_profile_update_errors | |
* @desc | |
* @param object $errors | |
* @param boolean $update | |
* @param object $user | |
* @see http://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors | |
*/ | |
public function user_profile_update_errors($errors, $update, $user) { | |
if($update === true && !empty($user) && !empty($user->ID)) { | |
$the_user = Classy_User::find_by_id($user->ID); | |
} | |
} | |
/** | |
* show_edit_user_profile | |
* @desc | |
* @param WP_User $user | |
*/ | |
public function show_edit_user_profile($user) { | |
$user = Classy_User::find_by_id($user->ID); | |
$fields = array_filter(self::get_form_fields(), array($this, '_filter_non_default')); | |
?> | |
<h3>Additional Fields</h3> | |
<table class="form-table"> | |
<?php foreach($fields as $field => $options): ?> | |
<tr class="<?php echo implode(' ', array('field', 'field-' . $field)); ?>"> | |
<th><?php echo $user->get_label($field, $options); ?></th> | |
<td><?php echo $user->get_input($field, $options); ?></td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
<?php | |
} | |
/** | |
* get_label | |
* @desc Return <label> HTML with standardised label names, based on ID | |
* @param string $id | |
* @param array $options | |
* @return string | |
*/ | |
public function get_label($id, $options) { | |
switch($id) { | |
default: | |
$html = sprintf('<label for="%s">%s</label>', $id, $options['label']); | |
break; | |
} | |
return $html; | |
} | |
/** | |
* get_input | |
* @desc Return HTML for the inputs | |
* @param string $id | |
* @param array $options | |
* @return string | |
*/ | |
public function get_input($id, $options) { | |
$html = ''; | |
$value = $this->$id; | |
$value = method_exists($this, 'get_' . $id) && empty($value) ? $this->{'get_' . $id}() : $value; | |
$name = !empty($options['name']) ? $options['name'] : ($options['default'] === true ? $id : sprintf('custom_user_%s', $id)); | |
switch($options['type']) { | |
case 'select': | |
$method = sprintf('get_%s_select', $id); | |
if(strpos($id, 'date_') !== false) { | |
$html .= method_exists($this, $method) ? $this->{$method}($id, $name, 'day') : ''; | |
$html .= method_exists($this, $method) ? $this->{$method}($id, $name, 'month') : ''; | |
$html .= method_exists($this, $method) ? $this->{$method}($id, $name, 'year') : ''; | |
} | |
else { | |
$html = method_exists($this, $method) ? $this->{$method}($id, $name) : ''; | |
} | |
break; | |
case 'textarea': | |
$cols = 30; | |
$rows = $id === 'text_like' || $id === 'text_dislike' ? 1 : 6; | |
$html = sprintf('<textarea class="regular-text" id="%s" name="%s" cols="%s" rows="%s">%s</textarea>', $id, $name, $cols, $rows, $value); | |
break; | |
case 'checkbox': | |
$selected = $value === $options['value'] ? ' checked="checked"' : ''; | |
$html = ''; | |
$html .= sprintf('<input type="hidden" class="checkbox" id="%s" name="%s" value="%s" />', $id, $name, 0); | |
$html .= sprintf('<input type="checkbox" class="checkbox" id="%s" name="%s" value="%s" %s />', $id, $name, $options['value'], $selected); | |
break; | |
case 'text': | |
default: | |
$html = sprintf('<input class="regular-text" type="text" id="%s" name="%s" value="%s" />', $id, $name, $value); | |
break; | |
} | |
return $html; | |
} | |
/** | |
* edit_user_profile_update | |
* @desc Meta data is saved here. | |
* Meta data must be prefixed with 'custom_user_' to be saved automatically | |
* @see https://codex.wordpress.org/edit_user_profile_update | |
* @see https://codex.wordpress.org/Function_Reference/update_metadata | |
* @param int $user_id | |
* @return int | |
*/ | |
public function edit_user_profile_update($user_id) { | |
$meta_type = 'user'; | |
// cycle through each posted meta item and save | |
// by default only saves custom fields which are prefixed with custom_user_ | |
foreach($_POST as $key => $value) { | |
if(strpos($key, 'custom_user_') !== false) { | |
$key = str_replace('custom_user_', '', $key); | |
$value = !empty($value) ? $value : null; | |
self::_update_metadata($meta_type, $user_id, $key, $value); | |
if(!is_null($value) && strpos($key, 'date') !== false && is_array($value)) { | |
// store the individual parts of the date, as well | |
foreach($value as $date_part_key => $date_part_value) { | |
self::_update_metadata($meta_type, $user_id, $key . '_' . $date_part_key, $date_part_value, false); | |
} | |
} | |
} | |
} | |
return $user_id; | |
} | |
/** | |
* _update_metadata | |
* @desc Update meta data, including deleting, converting and sanitising data | |
* @param string $meta_type | |
* @param int $user_id | |
* @param string $key | |
* @param mixed $value | |
* @param boolean $convert | |
*/ | |
protected static function _update_metadata($meta_type, $user_id, $key, $value, $convert = true) { | |
$current_data = get_metadata($meta_type, $user_id, $key, true); | |
$new_data = !empty($value) ? $value : null; | |
if(is_null($new_data)) { | |
delete_metadata($meta_type, $user_id, $key); | |
} | |
else { | |
if(strpos($key, 'date') !== false && $convert === true) { | |
if(is_array($new_data)) { | |
$new_data = implode('-', $new_data); | |
} | |
$new_data = strtotime($new_data); // convert to timestamp, for example custom_user_date_dob | |
} | |
if(strpos($key, 'email') !== false) { | |
$new_data = sanitize_email($new_data); | |
} | |
if($key === 'date_joined') { | |
// also set the date registered value | |
$user_id = wp_update_user(array( | |
'ID' => $user_id, | |
'user_registered' => gmdate('Y-m-d H:i:s', $new_data) | |
)); | |
} | |
update_metadata($meta_type, $user_id, $key, $new_data, $current_data); | |
} | |
} | |
/********************************************************* | |
* =Filters | |
* @desc Specific user filters for Wordpress | |
*********************************************************/ | |
/** | |
* filter_user_profile | |
* @desc Apply `the_content` filters to profile text | |
* @param string $content | |
* @return string | |
*/ | |
public function filter_user_profile($content) { | |
$content = apply_filters('the_content', $content); | |
return $content; | |
} | |
/** | |
* restrict_role_menu | |
* @desc Remove general menu items | |
* @global array $menu | |
*/ | |
public function restrict_role_menu() { | |
global $menu; | |
$role = $this->getInstance()->get_user_role(); | |
$slugs = array( | |
'link-manager.php', | |
'edit-comments.php', | |
); | |
switch($role) { | |
case 'subscriber': | |
$slugs = array_merge($slugs, array()); | |
remove_menu_page('dashboard'); | |
case 'admin': | |
$slugs = array_merge($slugs, array( | |
'themes.php', | |
'tools.php', | |
'plugins.php', | |
'options-general.php', | |
)); | |
} | |
if(is_array($slugs) && is_array($menu)) { | |
foreach($slugs as $slug) { | |
if(!empty($slug)) { | |
remove_menu_page($slug); | |
} | |
} | |
} | |
} | |
/** | |
* wp_dashboard_setup | |
* @desc Remove boxes from the dashboard | |
*/ | |
public function wp_dashboard_setup() { | |
$role = $this->getInstance()->get_user_role(); | |
switch($role) { | |
case 'subscriber': | |
remove_meta_box('dashboard_right_now', 'dashboard', 'normal'); | |
default: | |
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); | |
remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal'); | |
remove_meta_box('dashboard_plugins', 'dashboard', 'normal'); | |
remove_meta_box('dashboard_primary', 'dashboard', 'side'); | |
remove_meta_box('dashboard_secondary', 'dashboard', 'side'); | |
break; | |
} | |
} | |
/********************************************************* | |
* =Protected | |
* @desc Protected methods | |
*********************************************************/ | |
/** | |
* _filter_non_default | |
* @desc Used with array_filter to remove any default=true fields | |
* @param array $field | |
* @return boolean | |
*/ | |
protected static function _filter_non_default($field) { | |
return $field['default'] === true ? false : true; | |
} | |
} | |
/** | |
* Hook in to WordPress | |
*/ | |
if(class_exists('Classy_User')) { | |
$user_logged_in = new Classy_User('setup'); | |
add_action(__FILE__, array(&$user_logged_in, 'init')); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(Classy_User::is_logged_in()) { | |
echo Classy_User::getInstance()->get_display_name(); | |
echo Classy_User::getInstance()->get_user_role(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment