PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.7
Pods – Custom Content Types and Fields v3.3.7
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / avatar.php
pods / classes / fields Last commit date
avatar.php 4 months ago boolean.php 4 months ago code.php 4 months ago color.php 4 months ago comment.php 4 months ago currency.php 4 months ago date.php 4 months ago datetime.php 4 months ago email.php 4 months ago file.php 4 months ago heading.php 4 months ago html.php 4 months ago link.php 4 months ago number.php 4 months ago oembed.php 4 months ago paragraph.php 4 months ago password.php 4 months ago phone.php 4 months ago pick.php 4 months ago slug.php 4 months ago taxonomy.php 4 months ago text.php 4 months ago time.php 4 months ago website.php 4 months ago wysiwyg.php 4 months ago
avatar.php
361 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 require_once __DIR__ . '/file.php';
9
10 /**
11 * PodsField_Avatar class.
12 *
13 * @package Pods\Fields
14 */
15 class PodsField_Avatar extends PodsField_File {
16
17 /**
18 * {@inheritdoc}
19 */
20 public static $group = 'Relationships / Media';
21
22 /**
23 * {@inheritdoc}
24 */
25 public static $type = 'avatar';
26
27 /**
28 * {@inheritdoc}
29 */
30 public static $label = 'Avatar';
31
32 /**
33 * {@inheritdoc}
34 */
35 public static $pod_types = [
36 'user',
37 ];
38
39 /**
40 * {@inheritdoc}
41 */
42 public function setup() {
43
44 static::$group = __( 'Relationships / Media', 'pods' );
45 static::$label = __( 'Avatar', 'pods' );
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 public function options() {
52
53 $options = parent::options();
54
55 unset( $options[ static::$type . '_type' ], $options[ static::$type . '_allowed_extensions' ], $options[ static::$type . '_field_template' ], $options[ static::$type . '_wp_gallery_output' ], $options[ static::$type . '_wp_gallery_link' ], $options[ static::$type . '_wp_gallery_columns' ], $options[ static::$type . '_wp_gallery_random_sort' ], $options[ static::$type . '_wp_gallery_size' ] );
56
57 return $options;
58
59 }
60
61 /**
62 * {@inheritdoc}
63 */
64 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
65
66 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
67
68 $options[ static::$type . '_type' ] = 'images';
69 $options[ static::$type . '_field_template' ] = 'rows';
70 $options[ static::$type . '_wp_gallery_output' ] = 0;
71
72 parent::input( $name, $value, $options, $pod, $id );
73 }
74
75 /**
76 * {@inheritdoc}
77 */
78 public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
79 // Clear the avatar cache for the user.
80 pods_cache_clear( true, 'pods_avatars' );
81 pods_cache_clear( true, 'pods_avatar_urls' );
82
83 if ( $id ) {
84 pods_cache_clear( $id, 'pods_avatar_ids' );
85 } else {
86 pods_cache_clear( true, 'pods_avatar_ids' );
87 }
88
89 return parent::save( $value, $id, $name, $options, $fields, $pod, $params );
90 }
91
92 /**
93 * Take over the avatar served from WordPress
94 *
95 * @param string $avatar Default Avatar Image output from WordPress.
96 * @param int|string|object $id_or_email A user ID, email address, or comment object.
97 * @param int $size Size of the avatar image.
98 * @param string $default URL to a default image to use if no avatar is available.
99 * @param string $alt Alternate text to use in image tag. Defaults to blank.
100 *
101 * @return string <img> tag for the user's avatar
102 */
103 public function get_avatar( $avatar, $id_or_email, $size, $default = '', $alt = '' ) {
104
105 if ( ! $this->allow_avatar_overwrite() ) {
106 return $avatar;
107 }
108
109 $user_id = $this->get_avatar_user_id( $id_or_email );
110
111 if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) {
112 $avatar_cached = pods_cache_get( $user_id . '-' . $size, 'pods_avatars' );
113
114 if ( ! empty( $avatar_cached ) ) {
115 $avatar = $avatar_cached;
116 } else {
117
118 $user_avatar = $this->get_avatar_id( $user_id );
119
120 if ( ! empty( $user_avatar ) ) {
121 $attributes = [
122 'alt' => '',
123 'class' => 'avatar avatar-' . $size . ' photo',
124 ];
125
126 if ( ! empty( $alt ) ) {
127 $attributes['alt'] = $alt;
128 }
129
130 $user_avatar = pods_image( $user_avatar, [ $size, $size ], 0, $attributes );
131
132 if ( ! empty( $user_avatar ) ) {
133 $avatar = $user_avatar;
134
135 pods_cache_set( $user_id . '-' . $size, $avatar, 'pods_avatars', WEEK_IN_SECONDS );
136 }
137 }
138 }
139 }
140
141 return $avatar;
142 }
143
144 /**
145 * Take over the avatar data served from WordPress
146 *
147 * @since 2.7.22
148 *
149 * @param array $args {
150 * Optional. Arguments to return instead of the default arguments.
151 *
152 * @type int $size Height and width of the avatar image file in pixels. Default 96.
153 * @type int $height Display height of the avatar in pixels. Defaults to $size.
154 * @type int $width Display width of the avatar in pixels. Defaults to $size.
155 * @type string $default URL for the default image or a default type. Accepts '404' (return
156 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
157 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
158 * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or
159 * 'gravatar_default' (the Gravatar logo). Default is the value of the
160 * 'avatar_default' option, with a fallback of 'mystery'.
161 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
162 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
163 * judged in that order. Default is the value of the 'avatar_rating' option.
164 * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
165 * Default null.
166 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args
167 * plus a "found_avatar" guess. Pass as a reference. Default null.
168 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
169 * }
170 *
171 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
172 * user email, WP_User object, WP_Post object, or WP_Comment object.
173 *
174 * @return array {
175 * Along with the arguments passed in `$args`, this will contain a couple of extra arguments.
176 *
177 * @type bool $found_avatar True if we were able to find an avatar for this user,
178 * false or not set if we couldn't.
179 * @type string $url The URL of the avatar we found.
180 * }
181 */
182 public function get_avatar_data( $args, $id_or_email ) {
183 if ( ! $this->allow_avatar_overwrite() ) {
184 return $args;
185 }
186
187 $return_args = $args;
188
189 $args = wp_parse_args(
190 $args,
191 [
192 'size' => 96,
193 'height' => null,
194 'width' => null,
195 'default' => get_option( 'avatar_default', 'mystery' ),
196 'force_default' => false,
197 'rating' => get_option( 'avatar_rating' ),
198 'scheme' => null,
199 'processed_args' => null, // If used, should be a reference.
200 'extra_attr' => '',
201 ]
202 );
203
204 $size = $args['size'];
205 if ( $args['width'] !== $args['height'] ) {
206 $size = $args['width'] . 'x' . $args['height'];
207 }
208
209 $user_id = $this->get_avatar_user_id( $id_or_email );
210
211 if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) {
212 $user_avatar_url = null;
213 $avatar_cached = pods_cache_get( $user_id . '-' . $size, 'pods_avatar_urls' );
214
215 if ( ! empty( $avatar_cached ) ) {
216 $user_avatar_url = $avatar_cached;
217 } else {
218 $user_avatar_id = $this->get_avatar_id( $user_id );
219
220 if ( ! empty( $user_avatar_id ) ) {
221
222 $user_avatar_url = pods_image_url( $user_avatar_id, [ $args['width'], $args['height'] ], 0 );
223
224 if ( ! empty( $user_avatar_url ) ) {
225 pods_cache_set( $user_id . '-' . $size, $user_avatar_url, 'pods_avatar_urls', WEEK_IN_SECONDS );
226 }
227 }
228 }
229
230 if ( $user_avatar_url ) {
231 $return_args['url'] = $user_avatar_url;
232 $return_args['found_avatar'] = true;
233 }
234 }
235
236 return $return_args;
237 }
238
239 /**
240 * Get the custom user avatar ID.
241 *
242 * @since 2.7.22
243 *
244 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
245 * user email, WP_User object, WP_Post object, or WP_Comment object.
246 *
247 * @return int
248 */
249 public function get_avatar_id( $id_or_email ) {
250 $user_id = $this->get_avatar_user_id( $id_or_email );
251
252 // Include PodsMeta if not already included.
253 pods_meta();
254 $avatar_id = 0;
255
256 if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) {
257 $avatar_cached = pods_cache_get( $user_id, 'pods_avatar_ids' );
258
259 if ( ! empty( $avatar_cached ) ) {
260 $avatar_id = $avatar_cached;
261 } else {
262 $avatar_field_name = pods_transient_get( 'pods_avatar_field_name' );
263
264 /** @var \Pods\Whatsit\Pod $user_pod */
265 $user_pod = current( PodsMeta::$user );
266
267 if ( '__null__' === $avatar_field_name ) {
268 $avatar_field = false;
269 } elseif ( empty( $avatar_field_name ) ) {
270 $avatar_fields = $user_pod->get_fields(
271 [
272 'type' => 'avatar',
273 'limit' => 1,
274 ]
275 );
276
277 if ( ! empty( $avatar_fields ) ) {
278 $avatar_field = current( $avatar_fields );
279
280 $avatar_field_name = $avatar_field->get_name();
281
282 pods_transient_set( 'pods_avatar_field_name', $avatar_field_name, WEEK_IN_SECONDS );
283 }
284 } else {
285 $avatar_field = $user_pod->get_field( $avatar_field_name, null, false );
286 }
287
288 if ( ! empty( $avatar_field ) ) {
289 $pod = pods_get_instance( 'user', $user_id );
290
291 $avatar_id = $pod->field( $avatar_field_name . '.ID', true );
292
293 pods_cache_set( $user_id, $avatar_id, 'pods_avatar_ids', WEEK_IN_SECONDS );
294 } else {
295 pods_transient_set( 'pods_avatar_field_name', '__null__', WEEK_IN_SECONDS );
296 }
297 }//end if
298 }//end if
299
300 return (int) $avatar_id;
301 }
302
303 /**
304 * Get the avatar user ID based on parameter provided.
305 *
306 * @since 2.7.22
307 *
308 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
309 * user email, WP_User object, WP_Post object, or WP_Comment object.
310 *
311 * @return int
312 */
313 public function get_avatar_user_id( $id_or_email ) {
314 $user_id = 0;
315
316 if ( is_numeric( $id_or_email ) && 0 < $id_or_email ) {
317 $user_id = $id_or_email;
318 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && 0 < $id_or_email->user_id ) {
319 $user_id = $id_or_email->user_id;
320 } elseif ( is_object( $id_or_email ) && isset( $id_or_email->ID ) && isset( $id_or_email->user_login ) && 0 < $id_or_email->ID ) {
321 $user_id = $id_or_email->ID;
322 } elseif ( ! is_object( $id_or_email ) && false !== strpos( (string) $id_or_email, '@' ) ) {
323 $_user = get_user_by( 'email', $id_or_email );
324
325 if ( ! empty( $_user ) ) {
326 $user_id = $_user->ID;
327 }
328 }
329
330 return (int) $user_id;
331 }
332
333 /**
334 * Checks if we're not on WordPress admin pages where we shouldn't overwrite.
335 *
336 * @since 2.7.22
337 *
338 * @return bool
339 */
340 public function allow_avatar_overwrite() {
341
342 // Don't replace for the Avatars section of the Discussion settings page.
343 if ( is_admin() && ! doing_action( 'admin_bar_menu' ) && function_exists( 'get_current_screen' ) ) {
344 $current_screen = get_current_screen();
345
346 $screens = [
347 'profile',
348 'user-edit',
349 'options-discussion',
350 ];
351
352 if ( $current_screen && in_array( $current_screen->id, $screens, true ) ) {
353 return false;
354 }
355 }
356
357 return true;
358 }
359
360 }
361