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