Completed
Push — develop ( ea95d2...572bc7 )
by Zack
18:12
created

GravityView_Field_Consent   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A field_output() 0 38 5
A field_options() 0 26 2
1
<?php
2
/**
3
 * @file class-gravityview-field-consent.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 * @since 2.10
7
 */
8
9
/**
10
 * @since 2.10
11
 */
12
class GravityView_Field_Consent extends GravityView_Field {
13
14
	var $name = 'consent';
15
16
	var $is_searchable = true;
17
18
	var $search_operators = array( 'is', 'isnot' );
19
20
	var $_gf_field_class_name = 'GF_Field_Consent';
21
22
	var $group = 'standard';
23
24
	var $icon = 'dashicons-text-page';
25
26
	public function __construct() {
27
28
		$this->label = esc_html__( 'Consent', 'gravityview' );
29
30
		parent::__construct();
31
32
		add_filter( 'gravityview/template/field/consent/output', array( $this, 'field_output' ), 10, 2 );
33
	}
34
35
	/**
36
	 * Returns the value of the consent field based on the field settings.
37
	 *
38
	 * @param string $output Existing default $display_value for the field
39
	 * @param \GV\Template_Context $context
40
	 *
41
	 * @return string
42
	 */
43
	public function field_output( $output, $context ) {
44
45
		$configuration = $context->field->as_configuration();
46
47
		/** @var GF_Field_Consent $consent_field */
48
		$consent_field = $context->field->field;
0 ignored issues
show
Bug introduced by
The property field does not seem to exist in GV\Field.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
50
		switch ( \GV\Utils::get( $configuration, 'choice_display' ) ) {
51
			case 'tick':
52
				return $consent_field->checked_indicator_markup;
53
			case 'label':
54
55
				$revision_id = absint( trim( $context->value[ $context->field->ID . '.3' ] ) );
56
57
				// Gravity Forms performs a DB query for consent output. Let's reduce queries
58
				// and cache each version we find.
59
				static $_consent_field_cache = array();
60
				$_cache_key = "{$consent_field->formId}_{$consent_field->ID}_{$revision_id}";
61
62
				// We have a cache hit!
63
				if ( ! empty( $_consent_field_cache[ $_cache_key ] ) ) {
64
					return $_consent_field_cache[ $_cache_key ];
65
				}
66
67
				$description = $consent_field->get_field_description_from_revision( $revision_id );
68
69
				// There was no "description" value set when submitted. Use the checkbox value instead.
70
				if ( ! $description ) {
71
					$description = $consent_field->checkboxLabel;
72
				}
73
74
				$_consent_field_cache[ $_cache_key ] = $description;
75
76
				return $description;
77
		}
78
79
		return $output;
80
	}
81
82
	/**
83
	 * Add `choice_display` setting to the field
84
	 *
85
	 * @param array $field_options
86
	 * @param string $template_id
87
	 * @param string $field_id
88
	 * @param string $context
89
	 * @param string $input_type
90
	 *
91
	 * @since 1.17
92
	 *
93
	 * @return array
94
	 */
95
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
96
97
		// Set the $_field_id var
98
		$field_options = parent::field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id );
99
100
		if( floor( $field_id ) !== floatval( $field_id ) ) {
101
			$default = 'tick';
102
		} else {
103
			$default = 'both';
104
		}
105
106
		$field_options['choice_display'] = array(
107
			'type'    => 'radio',
108
			'class'   => 'vertical',
109
			'label'   => __( 'What should be displayed:', 'gravityview' ),
110
			'value'   => $default,
111
			'desc'    => '',
112
			'choices' => array(
113
				'both' => __( 'Consent image with description', 'gravityview' ),
114
				'tick' => __( 'Consent image', 'gravityview' ),
115
				'label' => __( 'Consent description', 'gravityview' ),
116
			),
117
		);
118
119
		return $field_options;
120
	}
121
}
122
123
new GravityView_Field_Consent;
124