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