Completed
Push — develop ( 0c9afc...d4082e )
by Zack
21:17 queued 17:22
created

GravityView_Field_Is_Approved   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 15.79%

Importance

Changes 0
Metric Value
dl 0
loc 191
ccs 9
cts 57
cp 0.1579
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A add_hooks() 0 5 1
A filter_field_value() 0 17 2
A add_default_field() 0 12 2
A custom_merge_tags() 0 11 1
A replace_merge_tag() 0 24 3
A field_options() 0 25 1
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 200.

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-is-approved.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
class GravityView_Field_Is_Approved extends GravityView_Field {
9
10
	public $name = 'is_approved';
11
12
	public $search_operators = array( 'is', 'isnot' );
13
14
	public $contexts = array( 'single', 'multiple' );
15
16
	public $group = 'meta';
17
18
	public $is_sortable = true;
19
20
	public $is_numeric = true;
21
22
	public $is_searchable = true;
23
24
	/**
25
	 * @var string Approval status is stored in entry meta under this key
26
	 * @since 1.18
27
	 */
28
	var $entry_meta_key = 'is_approved';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $entry_meta_key.

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...
29
30
	/**
31
	 * @var bool Don't add to the "columns to display" list; GravityView adds our own approval column
32
	 * @since 1.18
33
	 */
34
	var $entry_meta_is_default_column = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $entry_meta_is_default_column.

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...
35
36
	public $_custom_merge_tag = 'approval_status';
37
38
	public function __construct() {
39
40
		$this->label = esc_html__( 'Approval Status', 'gravityview' );
41
		$this->description = esc_html__( 'Display the entry\'s current approval status.', 'gravityview' );
42
		$this->default_search_label = __( 'Approval:', 'gravityview' );
43
44
		$this->add_hooks();
45
46
		parent::__construct();
47
	}
48
49
	private function add_hooks() {
50
		add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 );
51
52
		add_filter( 'gravityview_field_entry_value_is_approved_pre_link', array( $this, 'filter_field_value' ), 10, 4 );
53
	}
54
55
	/**
56
	 * Convert entry approval status value to label in the field output. Uses labels from the field setting.
57
	 *
58
	 * @since 1.18
59
	 *
60
	 * @param string $output HTML value output
61
	 * @param array  $entry The GF entry array
62
	 * @param array  $field_settings Settings for the particular GV field
63
	 * @param array  $field Field array, as fetched from GravityView_View::getCurrentField()
0 ignored issues
show
Bug introduced by
There is no parameter named $field. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
	 *
65
	 * @return string The field setting label for the current status. Uses defaults, if not configured.
66
	 */
67 1
	public function filter_field_value( $output = '', $entry = array(), $field_settings = array(), $gv_field_output = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $gv_field_output 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...
68
69 1
		$status = GravityView_Entry_Approval_Status::maybe_convert_status( $output );
70 1
		$status_key = GravityView_Entry_Approval_Status::get_key( $status );
71
72
		// "approved_label", "unapproved_label", "disapproved_label" setting keys
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73 1
		$field_setting_key = sprintf( '%s_label', $status_key );
74
75 1
		$default_label = GravityView_Entry_Approval_Status::get_label( $status );
76
77 1
		$value = \GV\Utils::get( $field_settings, $field_setting_key, $default_label );
78 1
		if ( empty( $value ) ) {
79 1
			$value = $default_label;
80
		}
81
82 1
		return sprintf( '<span class="gv-approval-%s">%s</span>', esc_attr( $status_key ), $value );
83
	}
84
85
	/**
86
	 *
87
	 *
88
	 * @filter `gravityview_entry_default_fields`
89
	 *
90
	 * @param  array $entry_default_fields Array of fields shown by default
91
	 * @param  string|array $form form_ID or form object
92
	 * @param  string $zone Either 'single', 'directory', 'header', 'footer'
93
	 *
94
	 * @return array
95
	 */
96
	function add_default_field( $entry_default_fields, $form, $zone ) {
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...
97
98
		if( 'edit' !== $zone ) {
99
			$entry_default_fields[ $this->name ] = array(
100
				'label' => $this->label,
101
				'desc'  => $this->description,
102
				'type'  => $this->name,
103
			);
104
		}
105
106
		return $entry_default_fields;
107
	}
108
109
	/**
110
	 * Add custom merge tags to merge tag options
111
	 *
112
	 * @since 1.16
113
	 *
114
	 * @param array $form GF Form array
115
	 * @param GF_Field[] $fields Array of fields in the form
116
	 *
117
	 * @return array Modified merge tags
118
	 */
119
	protected function custom_merge_tags( $form = array(), $fields = array() ) {
120
121
		$merge_tags = array(
122
			array(
123
				'label' => __('Approval Status', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
124
				'tag' => '{approval_status}'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
125
			),
126
		);
127
128
		return $merge_tags;
129
	}
130
131
	/**
132
	 * Display the approval status of an entry
133
	 *
134
	 * @see https://docs.gravityview.co/article/389-approvalstatus-merge-tag Read how to use the `{approval_status}` merge tag
135
	 *
136
	 * @since 1.18
137
	 *
138
	 * @param array $matches Array of Merge Tag matches found in text by preg_match_all
139
	 * @param string $text Text to replace
140
	 * @param array $form Gravity Forms form array
141
	 * @param array $entry Entry array
142
	 * @param bool $url_encode Whether to URL-encode output
143
	 * @param bool $esc_html Whether to apply `esc_html()` to output
144
	 *
145
	 * @return string Text, with user variables replaced, if they existed
146
	 */
147
	public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
148
149
		$return = $text;
150
151
		/**
152
		 * @var array $match {
153
		 *      @type string $match[0] Full matched merge tag ("{gv_approval}")
154
		 *      @type string $match[1] Modifier ("value", "label", or empty string)
155
		 * }
156
		 */
157
		foreach ( $matches as $match ) {
158
159
			if ( empty( $entry ) ) {
160
				gravityview()->log->error( 'No entry data available. Returning empty string.' );
161
				$replacement = '';
162
			} else {
163
				$replacement = GravityView_Entry_Approval::get_entry_status( $entry, $match[1] );
164
			}
165
166
			$return = str_replace( $match[0], $replacement, $return );
167
		}
168
169
		return $return;
170
	}
171
172
	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...
173
174
		$field_options['approved_label'] = array(
175
			'type' => 'text',
176
			'label' => __( 'Approved Label', 'gravityview' ),
177
			'desc' => __( 'If the entry is approved, display this value', 'gravityview' ),
178
			'placeholder' => GravityView_Entry_Approval_Status::get_label('approved'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
179
		);
180
181
		$field_options['disapproved_label'] = array(
182
			'type' => 'text',
183
			'label' => __( 'Disapproved Label', 'gravityview' ),
184
			'desc' => __( 'If the entry is not approved, display this value', 'gravityview' ),
185
			'placeholder' => GravityView_Entry_Approval_Status::get_label('disapproved'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
186
		);
187
188
		$field_options['unapproved_label'] = array(
189
			'type' => 'text',
190
			'label' => __( 'Unapproved Label', 'gravityview' ),
191
			'desc' => __( 'If the entry has not yet been approved or disapproved, display this value', 'gravityview' ),
192
			'placeholder' => GravityView_Entry_Approval_Status::get_label('unapproved'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
193
		);
194
195
		return $field_options;
196
	}
197
198
}
199
200
new GravityView_Field_Is_Approved;