Completed
Push — develop ( 2cbb28...c90d11 )
by Zack
04:20
created

GravityView_Field_Password::add_form_fields()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 12
c 1
b 0
f 1
nc 4
nop 3
dl 0
loc 24
rs 8.6845
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 106.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
11
12
	var $is_searchable = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_searchable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
13
14
	/** @see GF_Field_Password */
15
	var $_gf_field_class_name = 'GF_Field_Password';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_gf_field_class_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
17
	var $group = 'advanced';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
18
19
	public function __construct() {
20
		$this->label = esc_html__( 'Password', 'gravityview' );
21
22
		$this->add_hooks();
23
24
		parent::__construct();
25
	}
26
27
	/**
28
	 * Add filters to modify the front-end label and the Add Field label
29
	 *
30
	 * @since 1.17
31
	 *
32
	 * @return void
33
	 */
34
	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...
35
		add_filter( 'gravityview/common/get_form_fields', array( $this, 'add_form_fields' ), 10, 3 );
36
37
		add_filter( 'gravityview/template/field_label', array( $this, 'field_label' ), 10, 4 );
38
	}
39
40
	/**
41
	 * Use the GV Admin Field label for the Password field instead of the per-input setting
42
	 *
43
	 * @since 1.17
44
	 *
45
	 * @param string $label Field label HTML
46
	 * @param array $field GravityView field array
47
	 * @param array $form Gravity Forms form array
48
	 * @param array $entry Gravity Forms entry array
49
	 *
50
	 * @return string If a custom field label isn't set, return the field label for the password field
51
	 */
52
	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...
53
54
		// If using a custom label, no need to fetch the parent label
55
		if( ! is_numeric( $field['id'] ) || ! empty( $field['custom_label'] ) ) {
56
			return $label;
57
		}
58
59
		$field_object = GFFormsModel::get_field( $form, $field['id'] );
60
61
		if( $field_object && 'password' === $field_object->type ) {
62
			$label = $field['label'];
63
		}
64
65
		return $label;
66
	}
67
68
	/**
69
	 * If a form has list fields, add the columns to the field picker
70
	 *
71
	 * @since 1.17
72
	 *
73
	 * @param array $fields Associative array of fields, with keys as field type
74
	 * @param array $form GF Form array
75
	 * @param bool $include_parent_field Whether to include the parent field when getting a field with inputs
76
	 *
77
	 * @return array $fields with list field columns added, if exist. Unmodified if form has no list fields.
78
	 */
79
	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...
80
81
		foreach ( $fields as $key => $field ) {
82
			if( 'password' === $field['type'] ) {
83
84
				// The Enter Password input
85
				if( floor( $key ) === floatval( $key ) ) {
86
					$field['label'] = $field['parent']->label;
87
					$field['adminOnly'] = $field['parent']->adminOnly;
88
					$field['adminLabel'] = $field['parent']->adminLabel;
89
90
					// Don't show as a child input
91
					unset( $field['parent'] );
92
93
					$fields[ $key ] = $field;
94
				} else {
95
					// The Confirm Password input
96
					unset( $fields[ $key ] );
97
				}
98
			}
99
		}
100
101
		return $fields;
102
	}
103
104
}
105
106
new GravityView_Field_Password;
107