GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-created-by.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-created-by.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
9 
10  var $name = 'created_by';
11 
12  var $search_operators = array( 'is', 'isnot' );
13 
14  var $group = 'meta';
15 
16  var $_custom_merge_tag = 'created_by';
17 
18  public function __construct() {
19  $this->label = esc_html__( 'Created By', 'gravityview' );
20  $this->default_search_label = __( 'Submitted by:', 'gravityview' );
21  parent::__construct();
22  }
23 
24  /**
25  * Add custom merge tags to merge tag options
26  *
27  * @since 1.16
28  *
29  * @param array $form GF Form array
30  * @param GF_Field[] $fields Array of fields in the form
31  *
32  * @return array Modified merge tags
33  */
34  protected function custom_merge_tags( $form = array(), $fields = array() ) {
35 
36  $merge_tags = array(
37  array(
38  'label' => __('Entry Creator: Display Name', 'gravityview'),
39  'tag' => '{created_by:display_name}'
40  ),
41  array(
42  'label' => __('Entry Creator: Email', 'gravityview'),
43  'tag' => '{created_by:user_email}'
44  ),
45  array(
46  'label' => __('Entry Creator: Username', 'gravityview'),
47  'tag' => '{created_by:user_login}'
48  ),
49  array(
50  'label' => __('Entry Creator: User ID', 'gravityview'),
51  'tag' => '{created_by:ID}'
52  ),
53  array(
54  'label' => __('Entry Creator: Roles', 'gravityview'),
55  'tag' => '{created_by:roles}'
56  ),
57  );
58 
59  return $merge_tags;
60  }
61 
62  /**
63  * Exactly like Gravity Forms' User Meta functionality, but instead shows information on the user who created the entry
64  * instead of the currently logged-in user.
65  *
66  * @see http://docs.gravityview.co/article/281-the-createdby-merge-tag Read how to use the `{created_by}` merge tag
67  *
68  * @since 1.16
69  *
70  * @param array $matches Array of Merge Tag matches found in text by preg_match_all
71  * @param string $text Text to replace
72  * @param array $form Gravity Forms form array
73  * @param array $entry Entry array
74  * @param bool $url_encode Whether to URL-encode output
75  * @param bool $esc_html Whether to apply `esc_html()` to output
76  *
77  * @return string Text, with user variables replaced, if they existed
78  */
79  public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
80 
81  // If there are no matches OR the Entry `created_by` isn't set or is 0 (no user)
82  if( empty( $entry['created_by'] ) ) {
83  return $text;
84  }
85 
86  // Get the creator of the entry
87  $entry_creator = new WP_User( $entry['created_by'] );
88 
89  foreach ( $matches as $match ) {
90 
91  $full_tag = $match[0];
92  $property = $match[1];
93 
94  switch( $property ) {
95  /** @since 1.13.2 */
96  case 'roles':
97  $value = implode( ', ', $entry_creator->roles );
98  break;
99  default:
100  $value = $entry_creator->get( $property );
101  }
102 
103  $value = $url_encode ? urlencode( $value ) : $value;
104 
105  $value = $esc_html ? esc_html( $value ) : $value;
106 
107  $text = str_replace( $full_tag, $value, $text );
108  }
109 
110  unset( $entry_creator );
111 
112  return $text;
113  }
114 
115  function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
116 
117  if( 'edit' === $context ) {
118  return $field_options;
119  }
120 
121  $field_options['name_display'] = array(
122  'type' => 'select',
123  'label' => __( 'User Format', 'gravityview' ),
124  'desc' => __( 'How should the User information be displayed?', 'gravityview'),
125  'choices' => array(
126  'display_name' => __('Display Name (Example: "Ellen Ripley")', 'gravityview'),
127  'user_login' => __('Username (Example: "nostromo")', 'gravityview'),
128  'ID' => __('User ID # (Example: 426)', 'gravityview'),
129  ),
130  'value' => 'display_name'
131  );
132 
133  return $field_options;
134  }
135 
136 }
137 
Modify field settings by extending this class.
custom_merge_tags($form=array(), $fields=array())
Add custom merge tags to merge tag options.
replace_merge_tag($matches=array(), $text= '', $form=array(), $entry=array(), $url_encode=false, $esc_html=false)
Exactly like Gravity Forms&#39; User Meta functionality, but instead shows information on the user who cr...
field_options($field_options, $template_id, $field_id, $context, $input_type)
$field_id
Definition: time.php:17
$entry
Definition: notes.php:27