Completed
Push — master ( 3ac4b1...21157a )
by Zack
05:18
created

GravityView_Field_Created_By::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

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