PodsField_Avatar::get_avatar()   F
last analyzed

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 9 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
 * 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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
	 */
80
	public function get_avatar( $avatar, $id_or_email, $size, $default = '', $alt = '' ) {
81
82
		// Don't replace for the Avatars section of the Discussion settings page.
83
		if ( is_admin() ) {
84
			$current_screen = get_current_screen();
85
86
			if ( null !== $current_screen && 'options-discussion' === $current_screen->id && 32 === $size ) {
87
				return $avatar;
88
			}
89
		}
90
91
		$user_id = 0;
92
93
		if ( is_numeric( $id_or_email ) && 0 < $id_or_email ) {
94
			$user_id = (int) $id_or_email;
95
		} elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && 0 < $id_or_email->user_id ) {
96
			$user_id = (int) $id_or_email->user_id;
97
		} elseif ( is_object( $id_or_email ) && isset( $id_or_email->ID ) && isset( $id_or_email->user_login ) && 0 < $id_or_email->ID ) {
98
			$user_id = (int) $id_or_email->ID;
99
		} elseif ( ! is_object( $id_or_email ) && false !== strpos( $id_or_email, '@' ) ) {
100
			$_user = get_user_by( 'email', $id_or_email );
101
102
			if ( ! empty( $_user ) ) {
103
				$user_id = (int) $_user->ID;
104
			}
105
		}
106
107
		// Include PodsMeta if not already included.
108
		pods_meta();
109
110
		if ( 0 < $user_id && ! empty( PodsMeta::$user ) ) {
111
			$avatar_cached = pods_cache_get( $user_id . '-' . $size, 'pods_avatars' );
112
113
			if ( ! empty( $avatar_cached ) ) {
114
				$avatar = $avatar_cached;
115
			} else {
116
				$avatar_field = pods_transient_get( 'pods_avatar_field' );
117
118
				$user = current( PodsMeta::$user );
119
120
				if ( empty( $avatar_field ) ) {
121
					foreach ( $user['fields'] as $field ) {
122
						if ( 'avatar' === $field['type'] ) {
123
							$avatar_field = $field['name'];
124
125
							pods_transient_set( 'pods_avatar_field', $avatar_field );
126
127
							break;
128
						}
129
					}
130
				} elseif ( ! isset( $user['fields'][ $avatar_field ] ) ) {
131
					$avatar_field = false;
132
				}
133
134
				if ( ! empty( $avatar_field ) ) {
135
					$user_avatar = get_user_meta( $user_id, $avatar_field . '.ID', true );
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
136
137
					if ( ! empty( $user_avatar ) ) {
138
						$attributes = array(
139
							'alt'   => '',
140
							'class' => 'avatar avatar-' . $size . ' photo',
141
						);
142
143
						if ( ! empty( $alt ) ) {
144
							$attributes['alt'] = $alt;
145
						}
146
147
						$user_avatar = pods_image( $user_avatar, array( $size, $size ), 0, $attributes );
148
149
						if ( ! empty( $user_avatar ) ) {
150
							$avatar = $user_avatar;
151
152
							pods_cache_set( $user_id . '-' . $size, $avatar, 'pods_avatars' );
153
						}
154
					}
155
				}//end if
156
			}//end if
157
		}//end if
158
159
		return $avatar;
160
161
	}
162
163
}
164