avatar.php
3 years ago
boolean.php
3 years ago
code.php
2 years ago
color.php
3 years ago
comment.php
4 years ago
currency.php
3 years ago
date.php
2 years ago
datetime.php
2 years ago
email.php
3 years ago
file.php
2 years ago
heading.php
2 years ago
html.php
2 years ago
link.php
3 years ago
number.php
2 years ago
oembed.php
2 years ago
paragraph.php
2 years ago
password.php
3 years ago
phone.php
3 years ago
pick.php
2 years ago
slug.php
3 years ago
taxonomy.php
4 years ago
text.php
2 years ago
time.php
2 years ago
website.php
3 years ago
wysiwyg.php
2 years ago
avatar.php
354 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 | * {@inheritdoc} |
| 72 | */ |
| 73 | public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 74 | // Clear the avatar cache for the user. |
| 75 | pods_cache_clear( true, 'pods_avatars' ); |
| 76 | pods_cache_clear( true, 'pods_avatar_urls' ); |
| 77 | |
| 78 | if ( $id ) { |
| 79 | pods_cache_clear( $id, 'pods_avatar_ids' ); |
| 80 | } else { |
| 81 | pods_cache_clear( true, 'pods_avatar_ids' ); |
| 82 | } |
| 83 | |
| 84 | return parent::save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Take over the avatar served from WordPress |
| 89 | * |
| 90 | * @param string $avatar Default Avatar Image output from WordPress. |
| 91 | * @param int|string|object $id_or_email A user ID, email address, or comment object. |
| 92 | * @param int $size Size of the avatar image. |
| 93 | * @param string $default URL to a default image to use if no avatar is available. |
| 94 | * @param string $alt Alternate text to use in image tag. Defaults to blank. |
| 95 | * |
| 96 | * @return string <img> tag for the user's avatar |
| 97 | */ |
| 98 | public function get_avatar( $avatar, $id_or_email, $size, $default = '', $alt = '' ) { |
| 99 | |
| 100 | if ( ! $this->allow_avatar_overwrite() ) { |
| 101 | return $avatar; |
| 102 | } |
| 103 | |
| 104 | $user_id = $this->get_avatar_user_id( $id_or_email ); |
| 105 | |
| 106 | if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) { |
| 107 | $avatar_cached = pods_cache_get( $user_id . '-' . $size, 'pods_avatars' ); |
| 108 | |
| 109 | if ( ! empty( $avatar_cached ) ) { |
| 110 | $avatar = $avatar_cached; |
| 111 | } else { |
| 112 | |
| 113 | $user_avatar = $this->get_avatar_id( $user_id ); |
| 114 | |
| 115 | if ( ! empty( $user_avatar ) ) { |
| 116 | $attributes = array( |
| 117 | 'alt' => '', |
| 118 | 'class' => 'avatar avatar-' . $size . ' photo', |
| 119 | ); |
| 120 | |
| 121 | if ( ! empty( $alt ) ) { |
| 122 | $attributes['alt'] = $alt; |
| 123 | } |
| 124 | |
| 125 | $user_avatar = pods_image( $user_avatar, array( $size, $size ), 0, $attributes ); |
| 126 | |
| 127 | if ( ! empty( $user_avatar ) ) { |
| 128 | $avatar = $user_avatar; |
| 129 | |
| 130 | pods_cache_set( $user_id . '-' . $size, $avatar, 'pods_avatars', WEEK_IN_SECONDS ); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return $avatar; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Take over the avatar data served from WordPress |
| 141 | * |
| 142 | * @since 2.7.22 |
| 143 | * |
| 144 | * @param array $args { |
| 145 | * Optional. Arguments to return instead of the default arguments. |
| 146 | * |
| 147 | * @type int $size Height and width of the avatar image file in pixels. Default 96. |
| 148 | * @type int $height Display height of the avatar in pixels. Defaults to $size. |
| 149 | * @type int $width Display width of the avatar in pixels. Defaults to $size. |
| 150 | * @type string $default URL for the default image or a default type. Accepts '404' (return |
| 151 | * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster), |
| 152 | * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm', |
| 153 | * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or |
| 154 | * 'gravatar_default' (the Gravatar logo). Default is the value of the |
| 155 | * 'avatar_default' option, with a fallback of 'mystery'. |
| 156 | * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. |
| 157 | * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are |
| 158 | * judged in that order. Default is the value of the 'avatar_rating' option. |
| 159 | * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. |
| 160 | * Default null. |
| 161 | * @type array $processed_args When the function returns, the value will be the processed/sanitized $args |
| 162 | * plus a "found_avatar" guess. Pass as a reference. Default null. |
| 163 | * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. |
| 164 | * } |
| 165 | * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, |
| 166 | * user email, WP_User object, WP_Post object, or WP_Comment object. |
| 167 | * @return array { |
| 168 | * Along with the arguments passed in `$args`, this will contain a couple of extra arguments. |
| 169 | * |
| 170 | * @type bool $found_avatar True if we were able to find an avatar for this user, |
| 171 | * false or not set if we couldn't. |
| 172 | * @type string $url The URL of the avatar we found. |
| 173 | * } |
| 174 | */ |
| 175 | public function get_avatar_data( $args, $id_or_email ) { |
| 176 | if ( ! $this->allow_avatar_overwrite() ) { |
| 177 | return $args; |
| 178 | } |
| 179 | |
| 180 | $return_args = $args; |
| 181 | |
| 182 | $args = wp_parse_args( |
| 183 | $args, |
| 184 | array( |
| 185 | 'size' => 96, |
| 186 | 'height' => null, |
| 187 | 'width' => null, |
| 188 | 'default' => get_option( 'avatar_default', 'mystery' ), |
| 189 | 'force_default' => false, |
| 190 | 'rating' => get_option( 'avatar_rating' ), |
| 191 | 'scheme' => null, |
| 192 | 'processed_args' => null, // If used, should be a reference. |
| 193 | 'extra_attr' => '', |
| 194 | ) |
| 195 | ); |
| 196 | |
| 197 | $size = $args['size']; |
| 198 | if ( $args['width'] !== $args['height'] ) { |
| 199 | $size = $args['width'] . 'x' . $args['height']; |
| 200 | } |
| 201 | |
| 202 | $user_id = $this->get_avatar_user_id( $id_or_email ); |
| 203 | |
| 204 | if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) { |
| 205 | $user_avatar_url = null; |
| 206 | $avatar_cached = pods_cache_get( $user_id . '-' . $size, 'pods_avatar_urls' ); |
| 207 | |
| 208 | if ( ! empty( $avatar_cached ) ) { |
| 209 | $user_avatar_url = $avatar_cached; |
| 210 | } else { |
| 211 | $user_avatar_id = $this->get_avatar_id( $user_id ); |
| 212 | |
| 213 | if ( ! empty( $user_avatar_id ) ) { |
| 214 | |
| 215 | $user_avatar_url = pods_image_url( $user_avatar_id, array( $args['width'], $args['height'] ), 0 ); |
| 216 | |
| 217 | if ( ! empty( $user_avatar_url ) ) { |
| 218 | pods_cache_set( $user_id . '-' . $size, $user_avatar_url, 'pods_avatar_urls', WEEK_IN_SECONDS ); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if ( $user_avatar_url ) { |
| 224 | $return_args['url'] = $user_avatar_url; |
| 225 | $return_args['found_avatar'] = true; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return $return_args; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Get the custom user avatar ID. |
| 234 | * |
| 235 | * @since 2.7.22 |
| 236 | * |
| 237 | * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, |
| 238 | * user email, WP_User object, WP_Post object, or WP_Comment object. |
| 239 | * |
| 240 | * @return int |
| 241 | */ |
| 242 | public function get_avatar_id( $id_or_email ) { |
| 243 | $user_id = $this->get_avatar_user_id( $id_or_email ); |
| 244 | |
| 245 | // Include PodsMeta if not already included. |
| 246 | pods_meta(); |
| 247 | $avatar_id = 0; |
| 248 | |
| 249 | if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) { |
| 250 | $avatar_cached = pods_cache_get( $user_id, 'pods_avatar_ids' ); |
| 251 | |
| 252 | if ( ! empty( $avatar_cached ) ) { |
| 253 | $avatar_id = $avatar_cached; |
| 254 | } else { |
| 255 | $avatar_field_name = pods_transient_get( 'pods_avatar_field_name' ); |
| 256 | |
| 257 | /** @var \Pods\Whatsit\Pod $user_pod */ |
| 258 | $user_pod = current( PodsMeta::$user ); |
| 259 | |
| 260 | if ( '__null__' === $avatar_field_name ) { |
| 261 | $avatar_field = false; |
| 262 | } elseif ( empty( $avatar_field_name ) ) { |
| 263 | $avatar_fields = $user_pod->get_fields( |
| 264 | [ |
| 265 | 'type' => 'avatar', |
| 266 | 'limit' => 1, |
| 267 | ] |
| 268 | ); |
| 269 | |
| 270 | if ( ! empty( $avatar_fields ) ) { |
| 271 | $avatar_field = current( $avatar_fields ); |
| 272 | |
| 273 | $avatar_field_name = $avatar_field->get_name(); |
| 274 | |
| 275 | pods_transient_set( 'pods_avatar_field_name', $avatar_field_name, WEEK_IN_SECONDS ); |
| 276 | } |
| 277 | } else { |
| 278 | $avatar_field = $user_pod->get_field( $avatar_field_name, null, false ); |
| 279 | } |
| 280 | |
| 281 | if ( ! empty( $avatar_field ) ) { |
| 282 | $pod = pods_get_instance( 'user', $user_id ); |
| 283 | |
| 284 | $avatar_id = $pod->field( $avatar_field_name . '.ID', true ); |
| 285 | |
| 286 | pods_cache_set( $user_id, $avatar_id, 'pods_avatar_ids', WEEK_IN_SECONDS ); |
| 287 | } else { |
| 288 | pods_transient_set( 'pods_avatar_field_name', '__null__', WEEK_IN_SECONDS ); |
| 289 | } |
| 290 | }//end if |
| 291 | }//end if |
| 292 | |
| 293 | return (int) $avatar_id; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Get the avatar user ID based on parameter provided. |
| 298 | * |
| 299 | * @since 2.7.22 |
| 300 | * |
| 301 | * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, |
| 302 | * user email, WP_User object, WP_Post object, or WP_Comment object. |
| 303 | * |
| 304 | * @return int |
| 305 | */ |
| 306 | public function get_avatar_user_id( $id_or_email ) { |
| 307 | $user_id = 0; |
| 308 | |
| 309 | if ( is_numeric( $id_or_email ) && 0 < $id_or_email ) { |
| 310 | $user_id = $id_or_email; |
| 311 | } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && 0 < $id_or_email->user_id ) { |
| 312 | $user_id = $id_or_email->user_id; |
| 313 | } elseif ( is_object( $id_or_email ) && isset( $id_or_email->ID ) && isset( $id_or_email->user_login ) && 0 < $id_or_email->ID ) { |
| 314 | $user_id = $id_or_email->ID; |
| 315 | } elseif ( ! is_object( $id_or_email ) && false !== strpos( $id_or_email, '@' ) ) { |
| 316 | $_user = get_user_by( 'email', $id_or_email ); |
| 317 | |
| 318 | if ( ! empty( $_user ) ) { |
| 319 | $user_id = $_user->ID; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return (int) $user_id; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Checks if we're not on WordPress admin pages where we shouldn't overwrite. |
| 328 | * |
| 329 | * @since 2.7.22 |
| 330 | * |
| 331 | * @return bool |
| 332 | */ |
| 333 | public function allow_avatar_overwrite() { |
| 334 | |
| 335 | // Don't replace for the Avatars section of the Discussion settings page. |
| 336 | if ( is_admin() && ! doing_action( 'admin_bar_menu' ) && function_exists( 'get_current_screen' ) ) { |
| 337 | $current_screen = get_current_screen(); |
| 338 | |
| 339 | $screens = array( |
| 340 | 'profile', |
| 341 | 'user-edit', |
| 342 | 'options-discussion', |
| 343 | ); |
| 344 | |
| 345 | if ( $current_screen && in_array( $current_screen->id, $screens, true ) ) { |
| 346 | return false; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | } |
| 354 |