Completed
Push — master ( 02ebbb...59ee4f )
by Stephanie
02:32
created

FrmFieldValue::get_field_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 2.04
5
 */
6
class FrmFieldValue {
7
8
	/**
9
	 * @since 2.04
10
	 *
11
	 * @var stdClass
12
	 */
13
	protected $field = null;
14
15
	/**
16
	 * @since 2.04
17
	 *
18
	 * @var stdClass
19
	 */
20
	protected $entry = null;
21
22
	/**
23
	 * @since 2.04
24
	 *
25
	 * @var string
26
	 */
27
	protected $source = '';
28
29
	/**
30
	 * @since 2.04
31
	 *
32
	 * @var mixed
33
	 */
34
	protected $saved_value = '';
35
36
	/**
37
	 * @since 2.04
38
	 *
39
	 * @var mixed
40
	 */
41
	protected $displayed_value = '';
42
43
	/**
44
	 * FrmFieldValue constructor.
45
	 *
46
	 * @param stdClass $field
47
	 * @param stdClass $entry
48
	 * @param array $atts
49
	 */
50
	public function __construct( $field, $entry, $atts = array() ) {
51
		if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
52
			return;
53
		}
54
55
		$this->field = $field;
56
		$this->entry = $entry;
57
		$this->init_source( $atts );
58
		$this->init_saved_value();
59
		$this->init_displayed_value();
60
	}
61
62
	/**
63
	 * Initialize the source property
64
	 *
65
	 * @since 2.04
66
	 *
67
	 * @param array $atts
68
	 */
69 View Code Duplication
	protected function init_source( $atts ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
		if ( isset( $atts['source'] ) && is_string( $atts['source'] ) && $atts['source'] !== '' ) {
71
			$this->source = (string) $atts['source'];
72
		} else {
73
			$this->source = 'general';
74
		}
75
	}
76
77
	/**
78
	 * Initialize the saved_value property
79
	 *
80
	 * @since 2.04
81
	 */
82
	protected function init_saved_value() {
83
		if ( isset( $this->entry->metas[ $this->field->id ] ) ) {
84
			$this->saved_value = $this->entry->metas[ $this->field->id ];
85
		} else {
86
			$this->saved_value = '';
87
		}
88
89
		$this->clean_saved_value();
90
	}
91
92
	/**
93
	 * Initialize a field's displayed value
94
	 *
95
	 * @since 2.04
96
	 */
97
	protected function init_displayed_value() {
98
		$this->displayed_value = $this->saved_value;
99
100
		$this->filter_displayed_value();
101
	}
102
103
	/**
104
	 * Get the saved_value property
105
	 *
106
	 * @since 2.04
107
	 */
108
	public function get_saved_value() {
109
		return $this->saved_value;
110
	}
111
112
	/**
113
	 * Get a value from the field settings
114
	 * @since 2.05.06
115
	 */
116
	public function get_field_option( $value ) {
117
		return FrmField::get_option( $this->field, $value );
118
	}
119
120
	/**
121
	 * Get the field property's label
122
	 *
123
	 * @since 2.04
124
	 */
125
	public function get_field_label() {
126
		return $this->field->name;
127
	}
128
129
	/**
130
	 * Get the field property's id
131
	 *
132
	 * @since 2.05
133
	 */
134
	public function get_field_id() {
135
		return $this->field->id;
136
	}
137
138
	/**
139
	 * Get the field property's key
140
	 *
141
	 * @since 2.04
142
	 */
143
	public function get_field_key() {
144
		return $this->field->field_key;
145
	}
146
147
	/**
148
	 * Get the field property's type
149
	 *
150
	 * @since 2.04
151
	 */
152
	public function get_field_type() {
153
		return $this->field->type;
154
	}
155
156
	/**
157
	 * Get the displayed_value property
158
	 *
159
	 * @since 2.04
160
	 */
161
	public function get_displayed_value() {
162
		return $this->displayed_value;
163
	}
164
165
	/**
166
	 * Filter the displayed_value property
167
	 *
168
	 * @since 2.04
169
	 */
170
	protected function filter_displayed_value() {
171
172
		if ( $this->source === 'entry_formatter' ) {
173
			// Deprecated frm_email_value hook
174
			$meta                  = array(
175
				'item_id'    => $this->entry->id,
176
				'field_id'   => $this->field->id,
177
				'meta_value' => $this->saved_value,
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
178
				'field_type' => $this->field->type,
179
			);
180
			$this->displayed_value = apply_filters( 'frm_email_value', $this->displayed_value, (object) $meta, $this->entry, array(
181
				'field' => $this->field,
182
			) );
183
			if ( has_filter( 'frm_email_value' ) ) {
184
				_deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
185
			}
186
		}
187
188
		// frm_display_{fieldtype}_value_custom hook
189
		$this->displayed_value = apply_filters( 'frm_display_' . $this->field->type . '_value_custom', $this->displayed_value, array(
190
			'field' => $this->field, 'entry' => $this->entry,
191
		) );
192
	}
193
194
	/**
195
	 * Clean a field's saved value
196
	 *
197
	 * @since 2.04
198
	 */
199
	protected function clean_saved_value() {
200
		if ( $this->saved_value !== '' ) {
201
202
			$this->saved_value = maybe_unserialize( $this->saved_value );
203
204
			if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
205
				$this->saved_value = '';
206
			}
207
		}
208
	}
209
}