PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / modules / wpemerge-app-core / src / Avatar / Avatar.php

Avatar.php in Depicter — Popup & Slider Builder 1.3.2, at modules/wpemerge-app-core/src/Avatar/Avatar.php

182 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmergeAppCore
4 * @author Atanas Angelov <hi@atanas.dev>
5 * @copyright 2017-2020 Atanas Angelov
6 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 * @link https://wpemerge.com/
8 */
9
10 namespace WPEmergeAppCore\Avatar;
11
12 use WP_Comment;
13
14 class Avatar {
15 /**
16 * Default avatar attachment id.
17 *
18 * @var integer
19 */
20 protected $default_avatar_id = 0;
21
22 /**
23 * User meta keys that should be used as the avatar, in order.
24 *
25 * @var string[]
26 */
27 protected $avatar_user_meta_keys = [];
28
29 /**
30 * Bootstrap.
31 *
32 * @return void
33 */
34 public function bootstrap() {
35 add_filter( 'get_avatar_url', [$this, 'filterAvatar'], 10, 3 );
36 }
37
38 /**
39 * Set the default avatar to an attachment id.
40 *
41 * @param integer $attachment_id
42 * @return void
43 */
44 public function setDefault( $attachment_id ) {
45 $this->default_avatar_id = intval( $attachment_id );
46 }
47
48 /**
49 * Add a meta key which should be checked for a valid attachment id
50 *
51 * @param string $user_meta_key
52 * @return void
53 */
54 public function addUserMetaKey( $user_meta_key ) {
55 $this->avatar_user_meta_keys[] = strval( $user_meta_key );
56 }
57
58 /**
59 * Remove a previously added meta key
60 *
61 * @param string $user_meta_key
62 * @return void
63 */
64 public function removeUserMetaKey( $user_meta_key ) {
65 $filter = function( $meta_key ) use ( $user_meta_key ) {
66 return $meta_key !== $user_meta_key;
67 };
68 $this->avatar_user_meta_keys = array_filter( $this->avatar_user_meta_keys, $filter );
69 }
70
71 /**
72 * Converts an id_or_email to an ID if possible.
73 *
74 * @param integer|string|WP_Comment $id_or_email
75 * @return integer|string
76 */
77 protected function idOrEmailToId( $id_or_email ) {
78 if ( is_a( $id_or_email, WP_Comment::class ) ) {
79 return intval( $id_or_email->user_id );
80 }
81
82 if ( ! is_numeric( $id_or_email ) ) {
83 $user = get_user_by( 'email', $id_or_email );
84 if ( $user ) {
85 return intval( $user->ID );
86 }
87 }
88
89 return strval( $id_or_email );
90 }
91
92 /**
93 * Returns a size (name or [widget, height]) for the given avatar arguments.
94 *
95 * @param array $arguments
96 * @return array<integer>|string
97 */
98 protected function getSize( $arguments ) {
99 $size = 'full';
100
101 if ( ! empty( $arguments['width'] ) && ! empty( $arguments['height'] ) ) {
102 $size = [ intval( $arguments['width'] ), intval( $arguments['height'] ) ];
103 } elseif ( ! empty( $arguments['size'] ) ) {
104 $size = [ intval( $arguments['size'] ), intval( $arguments['size'] ) ];
105 }
106
107 return $size;
108 }
109
110 /**
111 * Get attachment fallback chain for the user avatar.
112 *
113 * @param integer $user_id
114 * @return array<integer>
115 */
116 protected function getAttachmentFallbackChain( $user_id ) {
117 $chain = [];
118
119 foreach ( $this->avatar_user_meta_keys as $user_meta_key ) {
120 $attachment_id = get_user_meta( $user_id, $user_meta_key, true );
121 if ( is_numeric( $attachment_id ) ) {
122 $chain[] = intval( $attachment_id );
123 }
124 }
125
126 if ( $this->default_avatar_id !== 0 ) {
127 $chain[] = $this->default_avatar_id;
128 }
129
130 return $chain;
131 }
132
133 /**
134 * Get avatar url
135 *
136 * @param integer $id
137 * @param array<integer>|string $size
138 * @return string|null
139 */
140 protected function getAvatarUrl( $id, $size ) {
141 $attachments_fallback_chain = $this->getAttachmentFallbackChain( $id );
142
143 foreach ( $attachments_fallback_chain as $attachment_id ) {
144 $image = wp_get_attachment_image_src( $attachment_id, $size );
145 if ( ! empty( $image ) ) {
146 return $image[0];
147 }
148 }
149
150 return null;
151 }
152
153 /**
154 * Filter an avatar url based on the default avatar attachment id and registered meta keys.
155 *
156 * @param string $url
157 * @param integer|string $id_or_email
158 * @param array $args
159 * @return string
160 */
161 public function filterAvatar( $url, $id_or_email, $args ) {
162 if ( ! empty( $args['force_default'] ) ) {
163 return $url;
164 }
165
166 if ( $this->default_avatar_id === 0 && empty( $this->avatar_user_meta_keys ) ) {
167 return $url;
168 }
169
170 $id = $this->idOrEmailToId( $id_or_email );
171
172 if ( is_numeric( $id ) ) {
173 $filtered_url = $this->getAvatarUrl( (int) $id, $this->getSize( $args ) );
174 if ( $filtered_url !== null ) {
175 $url = $filtered_url;
176 }
177 }
178
179 return $url;
180 }
181 }
182