GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-gravatar.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-gravatar.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
8 /**
9  * @since 2.8
10  */
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  var $icon = 'dashicons-id';
22 
23  public function __construct() {
24  $this->label = esc_html__( 'Gravatar', 'gk-gravityview' );
25  $this->description = esc_html__( 'A Gravatar is an image that represents a person online based on their email. Powered by gravatar.com.', 'gk-gravityview' );
26 
27  $this->add_hooks();
28 
29  parent::__construct();
30  }
31 
32  /**
33  * Add filters for this field
34  */
35  public function add_hooks() {
36  add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
37  }
38 
39  /**
40  * Add this field to the default fields in the GV field picker
41  *
42  * @param array $entry_default_fields Array of fields shown by default
43  * @param string|array $form form_ID or form object
44  * @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer'
45  *
46  * @return array
47  */
48  function add_default_field( $entry_default_fields = array(), $form = array(), $zone = '' ) {
49 
50  if ( 'edit' === $zone ) {
51  return $entry_default_fields;
52  }
53 
54  $entry_default_fields[ $this->name ] = array(
55  'label' => $this->label,
56  'desc' => $this->description,
57  'type' => $this->name,
58  'icon' => 'dashicons-id',
59  );
60 
61  return $entry_default_fields;
62  }
63 
64  /**
65  * Get the email address to use, based on field settings
66  *
67  * @internal May change in the future! Don't rely on this.
68  *
69  * @param array $field_settings
70  * @param array $entry Gravity Forms entry
71  *
72  * @return string Email address from field or from entry creator
73  */
74  static public function get_email( $field_settings, $entry ) {
75 
76  // There was no logged in user.
77  switch ( $field_settings['email_field'] ) {
78  case 'created_by_email':
79 
80  $created_by = \GV\Utils::get( $entry, 'created_by', null );
81 
82  if ( empty( $created_by ) ) {
83  return '';
84  }
85 
86  $user = get_user_by( 'id', $created_by );
87 
88  $email = $user->user_email;
89  break;
90  default:
91  $field_id = \GV\Utils::get( $field_settings, 'email_field' );
92  $email = rgar( $entry, $field_id );
93  break;
94  }
95 
96  return $email;
97  }
98 
99  /**
100  * @inheritDoc
101  */
102  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
103 
104  if ( 'edit' === $context ) {
105  return $field_options;
106  }
107 
108  unset( $field_options['new_window'] );
109 
110  $field_options['email_field'] = array(
111  'type' => 'select',
112  'label' => __( 'Email to Use', 'gk-gravityview' ),
113  'value' => 'created_by_email',
114  'desc' => __( 'Which email should be used to generate the Gravatar?', 'gk-gravityview' ),
115  'choices' => $this->_get_email_field_choices( $form_id ),
116  'group' => 'display',
117  );
118 
119  $field_options['default'] = array(
120  'type' => 'select',
121  'label' => __( 'Default Image', 'gk-gravityview' ),
122  'desc' => __( 'Choose the default image to be shown when an email has no Gravatar.', 'gk-gravityview' ) . ' <a href="https://en.gravatar.com/site/implement/images/">' . esc_html( sprintf( __( 'Read more about %s', 'gk-gravityview' ), __( 'Default Image', 'gk-gravityview' ) ) ) . '</a>',
123  'value' => get_option( 'avatar_default', 'mystery' ),
124  'choices' => array(
125  'mystery' => __( 'Silhouetted Person', 'gk-gravityview' ),
126  'gravatar_default' => __( 'Gravatar Icon', 'gk-gravityview' ),
127  'identicon' => __( 'Abstract Geometric Patterns', 'gk-gravityview' ),
128  'monsterid' => __( 'Monster Faces', 'gk-gravityview' ),
129  'retro' => __( 'Arcade-style Faces', 'gk-gravityview' ),
130  'robohash' => __( 'Robot Faces', 'gk-gravityview' ),
131  'blank' => __( 'Transparent Image', 'gk-gravityview' ),
132  ),
133  'group' => 'display',
134  );
135 
136  $field_options['size'] = array(
137  'type' => 'number',
138  'label' => __( 'Size in Pixels', 'gk-gravityview' ),
139  'value' => 80,
140  'max' => 2048,
141  'min' => 1,
142  'merge_tags' => false,
143  'group' => 'display',
144  );
145 
146  return $field_options;
147  }
148 
149  /**
150  * Get email fields for the form, as well as default choices
151  *
152  * @param int $form_id ID of the form to fetch fields for
153  *
154  * @return array Array keys are field IDs and value is field label
155  */
156  private function _get_email_field_choices( $form_id = 0 ) {
157 
158  $field_choices = array(
159  'created_by_email' => __( 'Entry Creator: Email', 'gk-gravityview' ),
160  );
161 
162  $form = GFAPI::get_form( $form_id );
163 
164  if ( ! $form ) {
165  return $field_choices;
166  }
167 
168  $email_fields = GFAPI::get_fields_by_type( $form, array( 'email' ) );
169 
170  foreach ( $email_fields as $email_field ) {
171  $email_field_id = $email_field['id'];
172  $email_field_label = GVCommon::get_field_label( $form, $email_field_id );
173  $email_field_label = sprintf( __( 'Field: %s', 'gk-gravityview' ), $email_field_label );
174  $field_choices[ $email_field_id ] = esc_html( $email_field_label );
175  }
176 
177  return $field_choices;
178  }
179 
180 }
181 
Modify field settings by extending this class.
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
if(empty( $value)) $user
$field_settings['content']
Definition: custom.php:27
if(gravityview() ->plugin->is_GF_25()) $form
add_hooks()
Add filters for this field.
static get_email( $field_settings, $entry)
Get the email address to use, based on field settings.
_get_email_field_choices( $form_id=0)
Get email fields for the form, as well as default choices.
scale description p description
$created_by
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
static get_field_label( $form=array(), $field_id='', $field_value='')
Retrieve the label of a given field id (for a specific form)
$entry
Definition: notes.php:27
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
add_default_field( $entry_default_fields=array(), $form=array(), $zone='')
Add this field to the default fields in the GV field picker.