1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144:
<?php
namespace VSP\Core\Traits;
defined( 'ABSPATH' ) || exit;
use VSP\Helper;
use WPOnion\Exception\Cache_Not_Found;
trait WP {
public static function is_user_role( $role = null, $current_role = null ) {
if ( in_array( $role, array( 'logedout', 'loggedout', 'visitor' ), true ) ) {
$role = 'visitor';
}
if ( null === $current_role ) {
$current_role = Helper::current_user( true );
}
return ( $role === $current_role );
}
public static function current_user( $user_role_only = true ) {
$user_role = wp_get_current_user();
if ( true === $user_role_only ) {
$user_roles = $user_role->roles;
$user_role = array_shift( $user_roles );
if ( null === $user_role ) {
$user_role = 'visitor';
}
}
return $user_role;
}
public static function current_user_id() {
return get_current_user_id();
}
public static function filter_roles( $required = array() ) {
return self::filter_array_data( $required, self::user_roles_lists( false ) );
}
public static function user_roles_lists( $only_slug = false ) {
try {
$user_roles = vsp_get_cache( 'vsp/user_roles/options' );
} catch ( Cache_Not_Found $exception ) {
$user_roles = array();
foreach ( self::get_user_roles() as $slug => $data ) {
$user_roles[ $slug ] = $data['name'];
}
vsp_set_cache( 'vsp/user_roles/options', $user_roles );
}
return ( true === $only_slug ) ? array_keys( $user_roles ) : $user_roles;
}
public static function user_role_title( $slug, $default = false ) {
$roles = self::user_roles_lists();
return ( isset( $roles[ $slug ] ) ) ? $roles[ $slug ] : $default;
}
public static function get_user_roles( $only_wp = false ) {
try {
$user_roles = vsp_get_cache( 'vsp/user_roles/list' );
} catch ( Cache_Not_Found $exception ) {
$user_roles = array();
if ( function_exists( 'wp_roles' ) ) {
$user_roles = wp_roles()->roles;
$user_roles['visitor'] = array( 'name' => esc_html__( 'Visitor / Logged-Out User', 'vsp-framework' ) );
}
vsp_set_cache( 'vsp/user_roles/list', $user_roles );
}
if ( true === $only_wp ) {
unset( $user_roles['visitor'] );
return $user_roles;
}
return $user_roles;
}
public static function get_site_name() {
return ( is_multisite() ) ? get_blog_details()->blogname : get_bloginfo( 'name' );
}
}