Completed
Push — master ( 495eb8...42929f )
by Stephanie
02:55 queued 10s
created

FrmFieldValue::clean_saved_value()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 0
dl 0
loc 11
rs 9.2222
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 4.03
17
	 *
18
	 * @var object
19
	 */
20
	protected $entry;
21
22
	/**
23
	 * @since 2.04
24
	 *
25
	 * @var int
26
	 */
27
	protected $entry_id = 0;
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 = 'frm_not_prepared';
42
43
	/**
44
	 * FrmFieldValue constructor.
45
	 *
46
	 * @param stdClass $field
47
	 * @param stdClass $entry
48
	 */
49
	public function __construct( $field, $entry ) {
50
		if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
51
			return;
52
		}
53
54
		$this->entry    = $entry;
55
		$this->entry_id = $entry->id;
56
		$this->field    = $field;
57
		$this->init_saved_value( $entry );
58
	}
59
60
	/**
61
	 * Initialize the saved_value property
62
	 *
63
	 * @since 2.04
64
	 *
65
	 * @param stdClass $entry
66
	 */
67
	protected function init_saved_value( $entry ) {
68
		if ( $this->field->type === 'html' ) {
69
			$this->saved_value = $this->field->description;
70
		} elseif ( isset( $entry->metas[ $this->field->id ] ) ) {
71
			$this->saved_value = $entry->metas[ $this->field->id ];
72
		} else {
73
			$this->saved_value = '';
74
		}
75
76
		$this->clean_saved_value();
77
	}
78
79
	/**
80
	 * Prepare the display value
81
	 *
82
	 * @since 2.05
83
	 *
84
	 * @param array $atts
85
	 */
86
	public function prepare_displayed_value( $atts = array() ) {
87
		$this->displayed_value = $this->saved_value;
88
		$this->generate_displayed_value_for_field_type( $atts );
89
		$this->filter_displayed_value( $atts );
90
	}
91
92
	/**
93
	 * Get a value from the field settings
94
	 *
95
	 * @since 2.05.06
96
	 */
97
	public function get_field_option( $value ) {
98
		return FrmField::get_option( $this->field, $value );
99
	}
100
101
	/**
102
	 * @since 4.03
103
	 */
104
	public function get_field_attr( $option ) {
105
		return is_object( $this->field ) ? $this->field->{$option} : '';
106
	}
107
108
	/**
109
	 * @since 4.03
110
	 */
111
	public function get_field() {
112
		return $this->field;
113
	}
114
115
	/**
116
	 * Get the field property's label
117
	 *
118
	 * @since 2.04
119
	 */
120
	public function get_field_label() {
121
		return $this->get_field_attr( 'name' );
122
	}
123
124
	/**
125
	 * Get the field property's id
126
	 *
127
	 * @since 2.05
128
	 */
129
	public function get_field_id() {
130
		return $this->get_field_attr( 'id' );
131
	}
132
133
	/**
134
	 * Get the field property's key
135
	 *
136
	 * @since 2.04
137
	 */
138
	public function get_field_key() {
139
		return $this->get_field_attr( 'field_key' );
140
	}
141
142
	/**
143
	 * Get the field property's type
144
	 *
145
	 * @since 2.04
146
	 */
147
	public function get_field_type() {
148
		return $this->get_field_attr( 'type' );
149
	}
150
151
	/**
152
	 * Get the saved_value property
153
	 *
154
	 * @since 2.04
155
	 */
156
	public function get_saved_value() {
157
		return $this->saved_value;
158
	}
159
160
	/**
161
	 * Get the displayed_value property
162
	 *
163
	 * @since 2.04
164
	 */
165
	public function get_displayed_value() {
166
		if ( $this->displayed_value === 'frm_not_prepared' ) {
167
			return __( 'The display value has not been prepared. Please use the prepare_display_value() method before calling get_displayed_value().', 'formidable' );
168
		}
169
170
		return $this->displayed_value;
171
	}
172
173
	/**
174
	 * Get the displayed value for different field types
175
	 *
176
	 * @since 3.0
177
	 *
178
	 * @param array $atts
179
	 *
180
	 * @return mixed
181
	 */
182
	protected function generate_displayed_value_for_field_type( $atts ) {
183
		if ( ! FrmAppHelper::is_empty_value( $this->displayed_value, '' ) ) {
184
			$field_obj = FrmFieldFactory::get_field_object( $this->field );
185
186
			$this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
187
		}
188
	}
189
190
	/**
191
	 * Filter the displayed_value property
192
	 *
193
	 * @since 2.04
194
	 *
195
	 * @param array $atts
196
	 */
197
	protected function filter_displayed_value( $atts ) {
198
		if ( ! is_object( $this->entry ) ) {
199
			$this->entry = FrmEntry::getOne( $this->entry_id, true );
200
			if ( ! is_object( $this->entry ) ) {
201
				return;
202
			}
203
		}
204
205
		// TODO: maybe change from 'source' to 'run_filters' = 'email'
206
		if ( isset( $atts['source'] ) && $atts['source'] === 'entry_formatter' ) {
207
			// Deprecated frm_email_value hook
208
			$meta = array(
209
				'item_id'    => $this->entry->id,
210
				'field_id'   => $this->field->id,
211
				'meta_value' => $this->saved_value,
212
				'field_type' => $this->field->type,
213
			);
214
215
			if ( has_filter( 'frm_email_value' ) ) {
216
				_deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
217
				$this->displayed_value = apply_filters(
218
					'frm_email_value',
219
					$this->displayed_value,
220
					(object) $meta,
221
					$this->entry,
222
					array(
223
						'field' => $this->field,
224
					)
225
				);
226
			}
227
		}
228
229
		// frm_display_{fieldtype}_value_custom hook
230
		$this->displayed_value = apply_filters(
231
			'frm_display_' . $this->field->type . '_value_custom',
232
			$this->displayed_value,
233
			array(
234
				'field' => $this->field,
235
				'entry' => $this->entry,
236
			)
237
		);
238
	}
239
240
	/**
241
	 * Clean a field's saved value
242
	 *
243
	 * @since 2.04
244
	 */
245
	protected function clean_saved_value() {
246
		if ( $this->saved_value !== '' ) {
247
			if ( ! is_array( $this->saved_value ) && ! is_object( $this->saved_value ) ) {
248
				FrmAppHelper::unserialize_or_decode( $this->saved_value );
249
			}
250
251
			if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
252
				$this->saved_value = '';
253
			}
254
		}
255
	}
256
}
257