Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

PodsField_Avatar::get_avatar()   F

Complexity

Conditions 28
Paths 385

Size

Total Lines 82
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 45
nc 385
nop 5
dl 0
loc 82
rs 3.7736
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once( PODS_DIR . 'classes/fields/file.php' );
3
4
/**
5
 * @package Pods\Fields
6
 */
7
class PodsField_Avatar extends PodsField_File {
8
9
	/**
10
	 * Field Type Group
11
	 *
12
	 * @var string
13
	 * @since 2.0
14
	 */
15
	public static $group = 'Relationships / Media';
16
17
	/**
18
	 * Field Type Identifier
19
	 *
20
	 * @var string
21
	 * @since 2.0
22
	 */
23
	public static $type = 'avatar';
24
25
	/**
26
	 * Field Type Label
27
	 *
28
	 * @var string
29
	 * @since 2.0
30
	 */
31
	public static $label = 'Avatar';
32
33
	/**
34
	 * Pod Types supported on (true for all, false for none, or give array of specific types supported)
35
	 *
36
	 * @var array|bool
37
	 * @since 2.1
38
	 */
39
	public static $pod_types = array( 'user' );
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44
	public function __construct() {
45
46
		self::$label = __( 'Avatar', 'pods' );
47
	}
48
49
	/**
50
	 * {@inheritdoc}
51
	 */
52
	public function options() {
53
54
		$options = parent::options();
55
56
		unset( $options[ self::$type . '_type' ] );
57
		unset( $options[ self::$type . '_allowed_extensions' ] );
58
		unset( $options[ self::$type . '_field_template' ] );
59
		unset( $options[ self::$type . '_wp_gallery_output' ] );
60
		unset( $options[ self::$type . '_wp_gallery_link' ] );
61
		unset( $options[ self::$type . '_wp_gallery_columns' ] );
62
		unset( $options[ self::$type . '_wp_gallery_random_sort' ] );
63
		unset( $options[ self::$type . '_wp_gallery_size' ] );
64
65
		return $options;
66
67
	}
68
69
	/**
70
	 * {@inheritdoc}
71
	 */
72
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
73
74
		$options = (array) $options;
75
76
		$options[ self::$type . '_type' ]              = 'images';
77
		$options[ self::$type . '_field_template' ]    = 'rows';
78
		$options[ self::$type . '_wp_gallery_output' ] = 0;
79
80
		parent::input( $name, $value, $options, $pod, $id );
81
82
	}
83
84
	/**
85
	 * Take over the avatar served from WordPress
86
	 *
87
	 * @param string            $avatar      Default Avatar Image output from WordPress.
88
	 * @param int|string|object $id_or_email A user ID, email address, or comment object.
89
	 * @param int               $size        Size of the avatar image.
90
	 * @param string            $default     URL to a default image to use if no avatar is available.
91
	 * @param string            $alt         Alternate text to use in image tag. Defaults to blank.
92
	 *
93
	 * @return string <img> tag for the user's avatar
94
	 */
95
	public function get_avatar( $avatar, $id_or_email, $size, $default = '', $alt = '' ) {
96
97
		// Don't replace for the Avatars section of the Discussion settings page.
98
		if ( is_admin() ) {
99
			$current_screen = get_current_screen();
100
101
			if ( ! is_null( $current_screen ) && 'options-discussion' === $current_screen->id && 32 === $size ) {
102
				return $avatar;
103
			}
104
		}
105
106
		$user_id = 0;
107
108
		if ( is_numeric( $id_or_email ) && 0 < $id_or_email ) {
109
			$user_id = (int) $id_or_email;
110
		} elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && 0 < $id_or_email->user_id ) {
111
			$user_id = (int) $id_or_email->user_id;
112
		} elseif ( is_object( $id_or_email ) && isset( $id_or_email->ID ) && isset( $id_or_email->user_login ) && 0 < $id_or_email->ID ) {
113
			$user_id = (int) $id_or_email->ID;
114
		} elseif ( ! is_object( $id_or_email ) && false !== strpos( $id_or_email, '@' ) ) {
115
			$_user = get_user_by( 'email', $id_or_email );
116
117
			if ( ! empty( $_user ) ) {
118
				$user_id = (int) $_user->ID;
119
			}
120
		}
121
122
		// Include PodsMeta if not already included.
123
		pods_meta();
124
125
		if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) {
126
			$avatar_cached = pods_cache_get( $user_id . '-' . $size, 'pods_avatars' );
127
128
			if ( ! empty( $avatar_cached ) ) {
129
				$avatar = $avatar_cached;
130
			} else {
131
				$avatar_field = pods_transient_get( 'pods_avatar_field' );
132
133
				$user = current( PodsMeta::$user );
134
135
				if ( empty( $avatar_field ) ) {
136
					foreach ( $user['fields'] as $field ) {
137
						if ( 'avatar' === $field['type'] ) {
138
							$avatar_field = $field['name'];
139
140
							pods_transient_set( 'pods_avatar_field', $avatar_field );
141
142
							break;
143
						}
144
					}
145
				} elseif ( ! isset( $user['fields'][ $avatar_field ] ) ) {
146
					$avatar_field = false;
147
				}
148
149
				if ( ! empty( $avatar_field ) ) {
150
					$user_avatar = get_user_meta( $user_id, $avatar_field . '.ID', true );
151
152
					if ( ! empty( $user_avatar ) ) {
153
						$attributes = array(
154
							'alt'   => '',
155
							'class' => 'avatar avatar-' . $size . ' photo',
156
						);
157
158
						if ( ! empty( $alt ) ) {
159
							$attributes['alt'] = $alt;
160
						}
161
162
						$user_avatar = pods_image( $user_avatar, array( $size, $size ), 0, $attributes );
163
164
						if ( ! empty( $user_avatar ) ) {
165
							$avatar = $user_avatar;
166
167
							pods_cache_set( $user_id . '-' . $size, $avatar, 'pods_avatars' );
168
						}
169
					}
170
				}
171
			}
172
		}
173
174
		return $avatar;
175
176
	}
177
178
}
179