PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmFieldValue.php

FrmFieldValue.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26, at classes/models/FrmFieldValue.php

284 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 2.04
8 */
9 class FrmFieldValue {
10
11 /**
12 * @since 2.04
13 *
14 * @var stdClass|null
15 */
16 protected $field;
17
18 /**
19 * @since 4.03
20 *
21 * @var object
22 */
23 protected $entry;
24
25 /**
26 * @since 2.04
27 *
28 * @var int
29 */
30 protected $entry_id = 0;
31
32 /**
33 * @since 2.04
34 *
35 * @var mixed
36 */
37 protected $saved_value = '';
38
39 /**
40 * @since 2.04
41 *
42 * @var mixed
43 */
44 protected $displayed_value = 'frm_not_prepared';
45
46 /**
47 * FrmFieldValue constructor.
48 *
49 * @param stdClass $field
50 * @param stdClass $entry
51 */
52 public function __construct( $field, $entry ) {
53 if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
54 return;
55 }
56
57 $this->entry = $entry;
58 $this->entry_id = $entry->id;
59 $field = apply_filters( 'frm_field_value_object', $field );
60 $this->field = $field;
61 $this->init_saved_value( $entry );
62 }
63
64 /**
65 * Gets entry property.
66 *
67 * @since 5.0.16
68 *
69 * @return stdClass
70 */
71 public function get_entry() {
72 return $this->entry;
73 }
74
75 /**
76 * Initialize the saved_value property
77 *
78 * @since 2.04
79 *
80 * @param stdClass $entry
81 *
82 * @return void
83 */
84 protected function init_saved_value( $entry ) {
85 if ( $this->field->type === 'html' ) {
86 $this->saved_value = $this->field->description;
87 } elseif ( isset( $entry->metas[ $this->field->id ] ) ) {
88 $this->saved_value = $entry->metas[ $this->field->id ];
89 } else {
90 $this->saved_value = '';
91 }
92
93 $this->clean_saved_value();
94 }
95
96 /**
97 * Prepare the display value
98 *
99 * @since 2.05
100 *
101 * @param array $atts
102 *
103 * @return void
104 */
105 public function prepare_displayed_value( $atts = array() ) {
106 $this->displayed_value = $this->saved_value;
107 // This class shouldn't affect values.
108 unset( $atts['class'] );
109 $this->generate_displayed_value_for_field_type( $atts );
110 $this->filter_displayed_value( $atts );
111 }
112
113 /**
114 * Get a value from the field settings
115 *
116 * @since 2.05.06
117 *
118 * @param string $value
119 *
120 * @return mixed
121 */
122 public function get_field_option( $value ) {
123 return FrmField::get_option( $this->field, $value );
124 }
125
126 /**
127 * @since 4.03
128 *
129 * @param string $option
130 *
131 * @return mixed
132 */
133 public function get_field_attr( $option ) {
134 return is_object( $this->field ) ? $this->field->{$option} : '';
135 }
136
137 /**
138 * @since 4.03
139 *
140 * @return stdClass
141 */
142 public function get_field() {
143 return $this->field;
144 }
145
146 /**
147 * Get the field property's label
148 *
149 * @since 2.04
150 *
151 * @return string
152 */
153 public function get_field_label() {
154 return $this->get_field_attr( 'name' );
155 }
156
157 /**
158 * Get the field property's id
159 *
160 * @since 2.05
161 *
162 * @return string
163 */
164 public function get_field_id() {
165 return $this->get_field_attr( 'id' );
166 }
167
168 /**
169 * Get the field property's key
170 *
171 * @since 2.04
172 *
173 * @return string
174 */
175 public function get_field_key() {
176 return $this->get_field_attr( 'field_key' );
177 }
178
179 /**
180 * Get the field property's type
181 *
182 * @since 2.04
183 *
184 * @return string
185 */
186 public function get_field_type() {
187 return $this->get_field_attr( 'type' );
188 }
189
190 /**
191 * Get the saved_value property
192 *
193 * @since 2.04
194 *
195 * @return mixed
196 */
197 public function get_saved_value() {
198 return $this->saved_value;
199 }
200
201 /**
202 * Get the displayed_value property
203 *
204 * @since 2.04
205 *
206 * @return mixed
207 */
208 public function get_displayed_value() {
209 if ( $this->displayed_value === 'frm_not_prepared' ) {
210 return __( 'The display value has not been prepared. Please use the prepare_display_value() method before calling get_displayed_value().', 'formidable' );
211 }
212
213 return $this->displayed_value;
214 }
215
216 /**
217 * Get the displayed value for different field types
218 *
219 * @since 3.0
220 *
221 * @param array $atts
222 *
223 * @return void
224 */
225 protected function generate_displayed_value_for_field_type( $atts ) {
226 if ( ! FrmAppHelper::is_empty_value( $this->displayed_value, '' ) ) {
227 $field_obj = FrmFieldFactory::get_field_object( $this->field );
228
229 $this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
230 }
231 }
232
233 /**
234 * Filter the displayed_value property
235 *
236 * @since 2.04
237 *
238 * @param array $atts
239 *
240 * @return void
241 */
242 protected function filter_displayed_value( $atts ) {
243 if ( ! is_object( $this->entry ) || empty( $this->entry->metas ) ) {
244 $this->entry = FrmEntry::getOne( $this->entry_id, true );
245
246 if ( ! is_object( $this->entry ) ) {
247 return;
248 }
249 }
250
251 // frm_display_{fieldtype}_value_custom hook
252 $this->displayed_value = apply_filters(
253 'frm_display_' . $this->field->type . '_value_custom',
254 $this->displayed_value,
255 array(
256 'field' => $this->field,
257 'entry' => $this->entry,
258 )
259 );
260
261 $this->displayed_value = apply_filters( 'frm_display_value', $this->displayed_value, $this->field, $atts );
262 }
263
264 /**
265 * Clean a field's saved value.
266 *
267 * @since 2.04
268 *
269 * @return void
270 */
271 protected function clean_saved_value() {
272 if ( $this->saved_value !== '' ) {
273 if ( ! is_array( $this->saved_value ) && ! is_object( $this->saved_value ) ) {
274 $field_type = FrmField::get_field_type( $this->field );
275 FrmFieldsHelper::prepare_field_value( $this->saved_value, $field_type );
276 }
277
278 if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
279 $this->saved_value = '';
280 }
281 }
282 }
283 }
284