PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / user.php

user.php in Defender Security – Malware Scanner, Login Security & Firewall 3.1.0, at src/traits/user.php

154 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Traits;
4
5 use Calotes\Helper\Array_Cache;
6 use WP_Defender\Model\Notification;
7
8 trait User {
9 /**
10 * Get user display.
11 *
12 * @param null|int $user_id
13 *
14 * @return string
15 */
16 public function get_user_display( $user_id = null ) {
17 if ( is_null( $user_id ) ) {
18 return __( 'Guest', 'wpdef' );
19 }
20
21 $user = $this->get_user( $user_id );
22 if ( ! is_object( $user ) ) {
23 return __( 'Guest', 'wpdef' );
24 }
25
26 return $user->display_name;
27 }
28
29 /**
30 * Check if current request is from Hub.
31 *
32 * @since 2.7.0
33 * @return bool
34 */
35 protected function is_hub_request() {
36 return isset( $_GET['wpmudev-hub'] ) && ! empty( $_GET['wpmudev-hub'] ); // phpcs:ignore
37 }
38
39 /**
40 * Get source of action. This can be a request from the Hub, a logged-in user.
41 * Todo: expand for WP-CLI, REST sources.
42 *
43 * @since 2.7.0
44 * @return string
45 */
46 public function get_source_of_action() {
47 return $this->is_hub_request()
48 ? __( 'Hub', 'wpdef' )
49 : $this->get_user_display( get_current_user_id() );
50 }
51
52 /**
53 * @param null|int $user_id
54 *
55 * @return bool|mixed|string|\WP_User|null
56 */
57 public function get_user_login( $user_id = null ) {
58 $user = $this->get_user( $user_id );
59 if ( ! is_object( $user ) ) {
60 return $user;
61 }
62
63 return $user->user_login;
64 }
65
66 /**
67 * @param null|int $user_id
68 *
69 * @return string|null
70 */
71 public function get_current_user_email( $user_id = null ) {
72 $user = $this->get_user( $user_id );
73 if ( ! is_object( $user ) ) {
74 return $user;
75 }
76
77 return $user->user_email;
78 }
79
80 /**
81 * @param null|int $user_id
82 *
83 * @return array|null
84 */
85 public function get_current_user_role( $user_id = null ) {
86 $user = $this->get_user( $user_id );
87 if ( ! is_object( $user ) ) {
88 return null;
89 }
90
91 return empty( $user->roles ) ? null : ucfirst( array_shift( $user->roles ) );
92 }
93
94 /**
95 * @param null|int|\WP_User $user_id
96 *
97 * @return bool|mixed|\WP_User|null
98 */
99 private function get_user( $user_id ) {
100 if ( $user_id instanceof \WP_User ) {
101 return $user_id;
102 }
103
104 if ( ! is_user_logged_in() ) {
105 return __( 'Guest', 'wpdef' );
106 }
107 if ( null === $user_id ) {
108 $user = wp_get_current_user();
109 } else {
110 $cache = Array_Cache::get( $user_id, 'user' );
111 if ( null !== $cache ) {
112 if ( empty( $cache->roles ) ) {
113 $cache_roles = Array_Cache::get( 'roles_' . $user_id, 'user' );
114 if ( null !== $cache_roles ) {
115 $cache->roles = $cache_roles;
116 }
117 }
118
119 $user = $cache;
120 } else {
121 $user = get_user_by( 'id', $user_id );
122 if ( is_object( $user ) ) {
123 Array_Cache::set( $user_id, $user, 'user' );
124 Array_Cache::set( 'roles_' . $user_id, $user->roles, 'user' );
125 }
126 }
127 }
128
129 return $user;
130 }
131
132 /**
133 * Return the default user for recipient, should be the current user.
134 *
135 * @return array
136 */
137 public function get_default_recipient() {
138 // @since 3.0.0 Fix 'Guest'-line.
139 if ( ! is_user_logged_in() ) {
140 return array();
141 }
142 $user_id = get_current_user_id();
143
144 return array(
145 'name' => $this->get_user_display( $user_id ),
146 'id' => $user_id,
147 'email' => $this->get_current_user_email( $user_id ),
148 'role' => $this->get_current_user_role( $user_id ),
149 'avatar' => get_avatar_url( $this->get_current_user_email( $user_id ) ),
150 'status' => Notification::USER_SUBSCRIBED,
151 );
152 }
153 }
154