Completed
Push — develop ( 87b48c...882b48 )
by Gennady
23:47 queued 03:49
created

GravityView_Field_Password   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 25.81%

Importance

Changes 0
Metric Value
dl 0
loc 121
ccs 8
cts 31
cp 0.2581
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A get_value() 0 4 1
A add_hooks() 0 5 1
A field_label() 0 15 5
A add_form_fields() 0 26 5
1
<?php
2
/**
3
 * @file class-gravityview-field-password.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
class GravityView_Field_Password extends GravityView_Field {
9
10
	var $name = 'password';
11
12
	var $is_searchable = false;
13
14
	/** @see GF_Field_Password */
15
	var $_gf_field_class_name = 'GF_Field_Password';
16
17
	var $group = 'advanced';
18
19
	public function __construct() {
20
		$this->label = esc_html__( 'Password', 'gravityview' );
21
22
		$this->add_hooks();
23
24
		parent::__construct();
25
	
26
		add_filter( 'gravityview/field/password/value', array( $this, 'get_value' ), 10, 6 );
27
	}
28
29
	/**
30
	 * Filter the value of the field, future.
31
	 *
32
	 * @since 2.0
33
	 *
34
	 * @param mixed			$value	The value of the field.
35
	 * @param \GV\Field		$field	The field as seen by future.
36
	 * @param \GV\View		$view	The view requested in.
37
	 * @param \GV\Source	$source The data source (form).
38
	 * @param \GV\Entry		$entry	The entry.
39
	 * @param \GV\Request	$request The request context.
40
	 *
41
	 * @return mixed $value The filtered value.
42
	 */
43 1
	public function get_value( $value, $field, $view, $source, $entry, $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $view is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $source is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $entry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
		/** Passwords should never be exposed. */
45 1
		return '';
46
	}
47
48
49
	/**
50
	 * Add filters to modify the front-end label and the Add Field label
51
	 *
52
	 * @since 1.17
53
	 *
54
	 * @return void
55
	 */
56
	function add_hooks() {
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...
57
		add_filter( 'gravityview/common/get_form_fields', array( $this, 'add_form_fields' ), 10, 3 );
58
59
		add_filter( 'gravityview/template/field_label', array( $this, 'field_label' ), 10, 4 );
60
	}
61
62
	/**
63
	 * Use the GV Admin Field label for the Password field instead of the per-input setting
64
	 *
65
	 * @since 1.17
66
	 *
67
	 * @param string $label Field label HTML
68
	 * @param array $field GravityView field array
69
	 * @param array $form Gravity Forms form array
70
	 * @param array $entry Gravity Forms entry array
71
	 *
72
	 * @return string If a custom field label isn't set, return the field label for the password field
73
	 */
74 20
	function field_label( $label = '', $field = array(), $form = array(), $entry = array() ){
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
75
76
		// If using a custom label, no need to fetch the parent label
77 20
		if( ! is_numeric( $field['id'] ) || ! empty( $field['custom_label'] ) ) {
78 18
			return $label;
79
		}
80
81 14
		$field_object = GFFormsModel::get_field( $form, $field['id'] );
82
83 14
		if( $field_object && 'password' === $field_object->type ) {
84
			$label = $field['label'];
85
		}
86
87 14
		return $label;
88
	}
89
90
	/**
91
	 * If a form has list fields, add the columns to the field picker
92
	 *
93
	 * @since 1.17
94
	 *
95
	 * @param array $fields Associative array of fields, with keys as field type
96
	 * @param array $form GF Form array
97
	 * @param bool $include_parent_field Whether to include the parent field when getting a field with inputs
98
	 *
99
	 * @return array $fields with list field columns added, if exist. Unmodified if form has no list fields.
100
	 */
101
	function add_form_fields( $fields = array(), $form = array(), $include_parent_field = true ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $include_parent_field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
102
103
		foreach ( $fields as $key => $field ) {
104
			if( 'password' === $field['type'] ) {
105
106
				// The Enter Password input
107
				if( floor( $key ) === floatval( $key ) ) {
108
109
					if( ! empty( $field['parent'] ) ) {
110
						$field['label']      = $field['parent']->label;
111
						$field['adminOnly']  = $field['parent']->adminOnly;
112
						$field['adminLabel'] = $field['parent']->adminLabel;
113
						// Don't show as a child input
114
						unset( $field['parent'] );
115
					}
116
117
					$fields[ $key ] = $field;
118
				} else {
119
					// The Confirm Password input
120
					unset( $fields[ $key ] );
121
				}
122
			}
123
		}
124
125
		return $fields;
126
	}
127
128
}
129
130
new GravityView_Field_Password;
131