GravityView  2.17
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 $is_searchable = true;
13 
14  var $search_operators = array( 'is', 'isnot', 'in', 'not_in' );
15 
16  var $group = 'meta';
17 
18  var $_custom_merge_tag = 'created_by';
19 
20  var $icon = 'dashicons-admin-users';
21 
22  public function __construct() {
23  $this->label = esc_html__( 'Created By (User)', 'gk-gravityview' );
24  $this->description = __('Details of the logged-in user who created the entry (if any).', 'gk-gravityview');
25  $this->default_search_label = __( 'Submitted by:', 'gk-gravityview' );
26  parent::__construct();
27  }
28 
29  /**
30  * Add custom merge tags to merge tag options
31  *
32  * @since 1.16
33  *
34  * @param array $form GF Form array
35  * @param GF_Field[] $fields Array of fields in the form
36  *
37  * @return array Modified merge tags
38  */
39  protected function custom_merge_tags( $form = array(), $fields = array() ) {
40 
41  $merge_tags = array(
42  array(
43  'label' => __('Entry Creator: Display Name', 'gk-gravityview'),
44  'tag' => '{created_by:display_name}'
45  ),
46  array(
47  'label' => __('Entry Creator: Email', 'gk-gravityview'),
48  'tag' => '{created_by:user_email}'
49  ),
50  array(
51  'label' => __('Entry Creator: Username', 'gk-gravityview'),
52  'tag' => '{created_by:user_login}'
53  ),
54  array(
55  'label' => __('Entry Creator: User ID', 'gk-gravityview'),
56  'tag' => '{created_by:ID}'
57  ),
58  array(
59  'label' => __('Entry Creator: Roles', 'gk-gravityview'),
60  'tag' => '{created_by:roles}'
61  ),
62  );
63 
64  return $merge_tags;
65  }
66 
67  /**
68  * Exactly like Gravity Forms' User Meta functionality, but instead shows information on the user who created the entry
69  * instead of the currently logged-in user.
70  *
71  * @see https://docs.gravityview.co/article/281-the-createdby-merge-tag Read how to use the `{created_by}` merge tag
72  *
73  * @since 1.16
74  *
75  * @param array $matches Array of Merge Tag matches found in text by preg_match_all
76  * @param string $text Text to replace
77  * @param array $form Gravity Forms form array
78  * @param array $entry Entry array
79  * @param bool $url_encode Whether to URL-encode output
80  * @param bool $esc_html Whether to apply `esc_html()` to output
81  *
82  * @return string Text, with user variables replaced, if they existed
83  */
84  public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
85 
86  // If there are no matches OR the Entry `created_by` isn't set or is 0 (no user)
87  if( empty( $matches ) || empty( $entry['created_by'] ) ) {
88  return $text;
89  }
90 
91  // Get the creator of the entry
92  $entry_creator = new WP_User( $entry['created_by'] );
93 
94  foreach ( $matches as $match ) {
95 
96  $full_tag = $match[0];
97  $property = $match[1];
98 
99  switch( $property ) {
100  case '':
101  $value = $entry_creator->ID;
102  break;
103  /** @since 1.13.2 */
104  case 'roles':
105  $value = implode( ', ', $entry_creator->roles );
106  break;
107  default:
108  $value = $entry_creator->get( $property );
109  }
110 
111  $value = $url_encode ? urlencode( $value ) : $value;
112 
113  $value = $esc_html ? esc_html( $value ) : $value;
114 
115  $text = str_replace( $full_tag, $value, $text );
116  }
117 
118  unset( $entry_creator );
119 
120  return $text;
121  }
122 
123  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
124 
125  if( 'edit' === $context ) {
126  return $field_options;
127  }
128 
129  $field_options['name_display'] = array(
130  'type' => 'select',
131  'label' => __( 'User Format', 'gk-gravityview' ),
132  'desc' => __( 'How should the User information be displayed?', 'gk-gravityview' ),
133  'choices' => array(
134  // column
135  'ID' => __( 'User ID # (Example: 426)', 'gk-gravityview' ),
136  'user_login' => __( 'Username (Example: "nostromo")', 'gk-gravityview' ),
137  'display_name' => __( 'Display Name (Example: "Ellen Ripley")', 'gk-gravityview' ),
138  'user_email' => __( 'User Email (Example: "[email protected]")', 'gk-gravityview' ),
139  'user_registered' => __( 'User Registered (Example: "2019-10-18 08:30:11")', 'gk-gravityview' ),
140 
141  // meta
142  'nickname' => ucwords( __( 'User nickname', 'gk-gravityview' ) ),
143  'description' => __( 'Description', 'gk-gravityview' ),
144  'first_name' => __( 'First Name', 'gk-gravityview' ),
145  'last_name' => __( 'Last Name', 'gk-gravityview' ),
146 
147  // misc
148  'first_last_name' => __( 'First and Last Name', 'gk-gravityview' ),
149  'last_first_name' => __( 'Last and First Name', 'gk-gravityview' ),
150  ),
151  'value' => 'display_name'
152  );
153 
154  return $field_options;
155  }
156 
157 }
158 
Modify field settings by extending this class.
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, $form_id)
if(gravityview() ->plugin->is_GF_25()) $form
scale description p description
custom_merge_tags( $form=array(), $fields=array())
Add custom merge tags to merge tag options.
if(empty( $created_by)) $form_id
$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