Completed
Push — master ( 2ddace...f4e16f )
by Zack
07:25 queued 01:59
created

GravityView_Field_Is_Approved   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 188
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

7 Methods

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

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