Completed
Pull Request — develop (#1440)
by Zack
07:29
created

GravityView_Field_Gravatar::add_default_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 6
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * @file class-gravityview-field-gravatar.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
/**
9
 * @since TODO
10
 */
11
class GravityView_Field_Gravatar extends GravityView_Field {
12
13
	var $name = 'gravatar';
14
15
	var $is_searchable = false;
16
17
	var $group = 'gravityview';
18
19
	var $contexts = array( 'single', 'multiple', 'export' );
20
21
	public function __construct() {
22
		$this->label = esc_html__( 'Gravatar', 'gravityview' );
23
		$this->description = esc_html__( 'A Gravatar is an image that represents a person online based on their email. Powered by gravatar.com.', 'gravityview' );
24
25
		$this->add_hooks();
26
27
		parent::__construct();
28
	}
29
30
	/**
31
	 * Add filters for this field
32
	 */
33
	public function add_hooks() {
34
		add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
35
	}
36
37
	/**
38
	 * Add this field to the default fields in the GV field picker
39
	 *
40
	 * @param array $entry_default_fields Array of fields shown by default
41
	 * @param string|array $form form_ID or form object
42
	 * @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer'
43
	 *
44
	 * @return array
45
	 */
46
	function add_default_field( $entry_default_fields = array(), $form = array(), $zone = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
48
		if ( 'edit' === $zone ) {
49
			return $entry_default_fields;
50
		}
51
52
		$entry_default_fields[ $this->name ] = array(
53
			'label' => $this->label,
54
			'desc'  => $this->description,
55
			'type'  => $this->name,
56
		);
57
58
		return $entry_default_fields;
59
	}
60
61
	/**
62
	 * Get either a Gravatar URL or complete image tag for a specified email address.
63
	 *
64
	 * @source https://gravatar.com/site/implement/images/php/
65
	 *
66
	 * @param string $email The email address. Required.
67
	 * @param int $size Size in pixels, 1 - 2048. Default: 80
68
	 * @param string $default Default imageset to use. Options: '404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank', or a custom URL you provide to your preferred default image. Default: 'mp'
69
	 * @param string $rating Maximum rating (inclusive) Options: 'g', 'pg', 'r', 'x'. Default: 'g'
70
	 * @param bool $img True to return a complete IMG tag False for just the URL. Default: true
71
	 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
72
	 *
73
	 * @return string containing either just a URL or a complete image tag
74
	 */
75
	static public function get_gravatar( $email, $size = 80, $default = 'mp', $rating = 'g', $img = true, $atts = array() ) {
76
77
		$size = (int) $size;
78
79
		if ( $size > 2048 ) {
80
			$size = 2048;
81
		} elseif ( $size < 1 ) {
82
			$size = 1;
83
		}
84
85
		$url = 'https://www.gravatar.com/avatar/';
86
		$url .= md5( strtolower( trim( $email ) ) );
87
		$url .= "?s={$size}&default={$default}&rating={$rating}";
88
		if ( $img ) {
89
			$url = '<img src="' . esc_url( $url ) . '"';
90
91
			foreach ( $atts as $key => $val ) {
92
				$url .= ' ' . esc_attr( $key ) . '="' . esc_attr( $val ) . '"';
93
			}
94
95
			$url .= ' />';
96
		}
97
98
		return $url;
99
	}
100
101
	/**
102
	 * Get the email address to use, based on field settings
103
	 *
104
	 * @internal May change in the future! Don't rely on this.
105
	 *
106
	 * @param array $field_settings
107
	 * @param array $entry Gravity Forms entry
108
	 *
109
	 * @return string Email address from field or from entry creator
110
	 */
111
	static public function get_email( $field_settings, $entry ) {
112
113
		// There was no logged in user.
114
		switch ( $field_settings['email_field'] ) {
115
			case 'created_by_email':
116
117
				$created_by = \GV\Utils::get( $entry, 'created_by', null );
118
119
				if ( empty( $created_by ) ) {
120
					return '';
121
				}
122
123
				$user = get_user_by( 'id', $created_by );
124
125
				$email = $user->user_email;
126
				break;
127
			default:
128
				$field_id = \GV\Utils::get( $field_settings, 'email_field' );
129
				$email    = rgar( $entry, $field_id );
130
				break;
131
		}
132
133
		return $email;
134
	}
135
136
	/**
137
	 * @inheritDoc
138
	 */
139
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
140
141
		if ( 'edit' === $context ) {
142
			return $field_options;
143
		}
144
145
		$field_options['email_field'] = array(
146
			'type'    => 'select',
147
			'label'   => __( 'Email to Use', 'gravityview' ),
148
			'value'   => 'created_by_email',
149
			'desc'    => __( 'Which email should be used to generate the Gravatar?', 'gravityview' ),
150
			'choices' => $this->_get_email_field_choices( $form_id ),
151
		);
152
153
		$field_options['default'] = array(
154
			'type'    => 'select',
155
			'label'   => __( 'Default Image', 'gravityview' ),
156
			'desc'    => __( 'Choose the default image to be shown when an email has no Gravatar.', 'gravityview' ) . ' <a href="https://en.gravatar.com/site/implement/images/">' . esc_html( sprintf( __( 'Read more about %s', 'gravityview' ), __( 'Default Image', 'gravityview' ) ) ) . '</a>',
157
			'value'   => 'mp',
158
			'choices' => array(
159
				'mp'        => __( 'Silhouetted Person', 'gravityview' ),
160
				''          => __( 'Gravatar Icon', 'gravityview' ),
161
				'identicon' => __( 'Abstract Geometric Patterns', 'gravityview' ),
162
				'monsterid' => __( 'Monster Faces', 'gravityview' ),
163
				'retro'     => __( 'Arcade-style Faces', 'gravityview' ),
164
				'robohash'  => __( 'Robot Faces', 'gravityview' ),
165
				'blank'     => __( 'Transparent Image', 'gravityview' ),
166
			),
167
		);
168
169
		$field_options['size'] = array(
170
			'type'  => 'number',
171
			'label' => __( 'Size in Pixels', 'gravityview' ),
172
			'value' => 80,
173
			'max'   => 2048,
174
			'min'   => 1,
175
		);
176
177
		$field_options['rating'] = array(
178
			'type'    => 'radio',
179
			'label'   => __( 'Maximum Rating', 'gravityview' ),
180
			'desc'    => __( 'Gravatar allows users to self-rate their images so that they can indicate if an image is appropriate for a certain audience. Specify one of the following ratings to request images up to and including that rating.', 'gravityview' ) . ' <a href="https://en.gravatar.com/site/implement/images/#rating">' . esc_html( sprintf( __( 'Read more about %s', 'gravityview' ), __( 'Ratings', 'gravityview' ) ) ) . '</a>',
181
			'value'   => 'g',
182
			'choices' => array(
183
				'g'  => 'G',
184
				'pg' => 'PG',
185
				'r'  => 'R',
186
				'x'  => 'X',
187
			),
188
		);
189
190
		return $field_options;
191
	}
192
193
	/**
194
	 * Get email fields for the form, as well as default choices
195
	 *
196
	 * @param int $form_id ID of the form to fetch fields for
197
	 *
198
	 * @return array Array keys are field IDs and value is field label
199
	 */
200
	private function _get_email_field_choices( $form_id = 0 ) {
201
202
		$field_choices = array(
203
			'created_by_email' => __( 'Entry Creator: Email', 'gravityview' ),
204
		);
205
206
		$form = GFAPI::get_form( $form_id );
207
208
		if ( ! $form ) {
209
			return $field_choices;
210
		}
211
212
		$email_fields = GFAPI::get_fields_by_type( $form, array( 'email' ) );
213
214
		foreach ( $email_fields as $email_field ) {
215
			$email_field_id                   = $email_field['id'];
216
			$email_field_label                = GVCommon::get_field_label( $form, $email_field_id );
217
			$email_field_label                = sprintf( __( 'Field: %s', 'gravityview' ), $email_field_label );
218
			$field_choices[ $email_field_id ] = esc_html( $email_field_label );
219
		}
220
221
		return $field_choices;
222
	}
223
224
}
225
226
new GravityView_Field_Gravatar;
227