Completed
Pull Request — master (#1442)
by Zack
08:53 queued 06:21
created

GravityView_Field_Gravatar::add_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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 the email address to use, based on field settings
63
	 *
64
	 * @internal May change in the future! Don't rely on this.
65
	 *
66
	 * @param array $field_settings
67
	 * @param array $entry Gravity Forms entry
68
	 *
69
	 * @return string Email address from field or from entry creator
70
	 */
71
	static public function get_email( $field_settings, $entry ) {
72
73
		// There was no logged in user.
74
		switch ( $field_settings['email_field'] ) {
75
			case 'created_by_email':
76
77
				$created_by = \GV\Utils::get( $entry, 'created_by', null );
78
79
				if ( empty( $created_by ) ) {
80
					return '';
81
				}
82
83
				$user = get_user_by( 'id', $created_by );
84
85
				$email = $user->user_email;
86
				break;
87
			default:
88
				$field_id = \GV\Utils::get( $field_settings, 'email_field' );
89
				$email    = rgar( $entry, $field_id );
90
				break;
91
		}
92
93
		return $email;
94
	}
95
96
	/**
97
	 * @inheritDoc
98
	 */
99
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
100
101
		if ( 'edit' === $context ) {
102
			return $field_options;
103
		}
104
105
		$field_options['email_field'] = array(
106
			'type'    => 'select',
107
			'label'   => __( 'Email to Use', 'gravityview' ),
108
			'value'   => 'created_by_email',
109
			'desc'    => __( 'Which email should be used to generate the Gravatar?', 'gravityview' ),
110
			'choices' => $this->_get_email_field_choices( $form_id ),
111
		);
112
113
		$field_options['default'] = array(
114
			'type'    => 'select',
115
			'label'   => __( 'Default Image', 'gravityview' ),
116
			'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>',
117
			'value'   => get_option( 'avatar_default', 'mystery' ),
118
			'choices' => array(
119
				'mystery'          => __( 'Silhouetted Person', 'gravityview' ),
120
				'gravatar_default' => __( 'Gravatar Icon', 'gravityview' ),
121
				'identicon'        => __( 'Abstract Geometric Patterns', 'gravityview' ),
122
				'monsterid'        => __( 'Monster Faces', 'gravityview' ),
123
				'retro'            => __( 'Arcade-style Faces', 'gravityview' ),
124
				'robohash'         => __( 'Robot Faces', 'gravityview' ),
125
				'blank'            => __( 'Transparent Image', 'gravityview' ),
126
			),
127
		);
128
129
		$field_options['size'] = array(
130
			'type'  => 'number',
131
			'label' => __( 'Size in Pixels', 'gravityview' ),
132
			'value' => 80,
133
			'max'   => 2048,
134
			'min'   => 1,
135
		);
136
137
		return $field_options;
138
	}
139
140
	/**
141
	 * Get email fields for the form, as well as default choices
142
	 *
143
	 * @param int $form_id ID of the form to fetch fields for
144
	 *
145
	 * @return array Array keys are field IDs and value is field label
146
	 */
147
	private function _get_email_field_choices( $form_id = 0 ) {
148
149
		$field_choices = array(
150
			'created_by_email' => __( 'Entry Creator: Email', 'gravityview' ),
151
		);
152
153
		$form = GFAPI::get_form( $form_id );
154
155
		if ( ! $form ) {
156
			return $field_choices;
157
		}
158
159
		$email_fields = GFAPI::get_fields_by_type( $form, array( 'email' ) );
160
161
		foreach ( $email_fields as $email_field ) {
162
			$email_field_id                   = $email_field['id'];
163
			$email_field_label                = GVCommon::get_field_label( $form, $email_field_id );
164
			$email_field_label                = sprintf( __( 'Field: %s', 'gravityview' ), $email_field_label );
165
			$field_choices[ $email_field_id ] = esc_html( $email_field_label );
166
		}
167
168
		return $field_choices;
169
	}
170
171
}
172
173
new GravityView_Field_Gravatar;
174