PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7
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.7, at classes/models/FrmFieldValue.php

289 lines 5.8 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
15 */
16 protected $field = null;
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 * @return stdClass
69 */
70 public function get_entry() {
71 return $this->entry;
72 }
73
74 /**
75 * Initialize the saved_value property
76 *
77 * @since 2.04
78 *
79 * @param stdClass $entry
80 *
81 * @return void
82 */
83 protected function init_saved_value( $entry ) {
84 if ( $this->field->type === 'html' ) {
85 $this->saved_value = $this->field->description;
86 } elseif ( isset( $entry->metas[ $this->field->id ] ) ) {
87 $this->saved_value = $entry->metas[ $this->field->id ];
88 } else {
89 $this->saved_value = '';
90 }
91
92 $this->clean_saved_value();
93 }
94
95 /**
96 * Prepare the display value
97 *
98 * @since 2.05
99 *
100 * @param array $atts
101 *
102 * @return void
103 */
104 public function prepare_displayed_value( $atts = array() ) {
105 $this->displayed_value = $this->saved_value;
106 unset( $atts['class'] ); // This class shouldn't affect values.
107 $this->generate_displayed_value_for_field_type( $atts );
108 $this->filter_displayed_value( $atts );
109 }
110
111 /**
112 * Get a value from the field settings
113 *
114 * @since 2.05.06
115 *
116 * @param string $value
117 */
118 public function get_field_option( $value ) {
119 return FrmField::get_option( $this->field, $value );
120 }
121
122 /**
123 * @since 4.03
124 *
125 * @param string $option
126 */
127 public function get_field_attr( $option ) {
128 return is_object( $this->field ) ? $this->field->{$option} : '';
129 }
130
131 /**
132 * @since 4.03
133 *
134 * @return stdClass
135 */
136 public function get_field() {
137 return $this->field;
138 }
139
140 /**
141 * Get the field property's label
142 *
143 * @since 2.04
144 */
145 public function get_field_label() {
146 return $this->get_field_attr( 'name' );
147 }
148
149 /**
150 * Get the field property's id
151 *
152 * @since 2.05
153 */
154 public function get_field_id() {
155 return $this->get_field_attr( 'id' );
156 }
157
158 /**
159 * Get the field property's key
160 *
161 * @since 2.04
162 */
163 public function get_field_key() {
164 return $this->get_field_attr( 'field_key' );
165 }
166
167 /**
168 * Get the field property's type
169 *
170 * @since 2.04
171 */
172 public function get_field_type() {
173 return $this->get_field_attr( 'type' );
174 }
175
176 /**
177 * Get the saved_value property
178 *
179 * @since 2.04
180 */
181 public function get_saved_value() {
182 return $this->saved_value;
183 }
184
185 /**
186 * Get the displayed_value property
187 *
188 * @since 2.04
189 */
190 public function get_displayed_value() {
191 if ( $this->displayed_value === 'frm_not_prepared' ) {
192 return __( 'The display value has not been prepared. Please use the prepare_display_value() method before calling get_displayed_value().', 'formidable' );
193 }
194
195 return $this->displayed_value;
196 }
197
198 /**
199 * Get the displayed value for different field types
200 *
201 * @since 3.0
202 *
203 * @param array $atts
204 *
205 * @return void
206 */
207 protected function generate_displayed_value_for_field_type( $atts ) {
208 if ( ! FrmAppHelper::is_empty_value( $this->displayed_value, '' ) ) {
209 $field_obj = FrmFieldFactory::get_field_object( $this->field );
210
211 $this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
212 }
213 }
214
215 /**
216 * Filter the displayed_value property
217 *
218 * @since 2.04
219 *
220 * @param array $atts
221 *
222 * @return void
223 */
224 protected function filter_displayed_value( $atts ) {
225 if ( ! is_object( $this->entry ) || empty( $this->entry->metas ) ) {
226 $this->entry = FrmEntry::getOne( $this->entry_id, true );
227 if ( ! is_object( $this->entry ) ) {
228 return;
229 }
230 }
231
232 // TODO: maybe change from 'source' to 'run_filters' = 'email'
233 if ( isset( $atts['source'] ) && $atts['source'] === 'entry_formatter' ) {
234 // Deprecated frm_email_value hook
235 $meta = array(
236 'item_id' => $this->entry->id,
237 'field_id' => $this->field->id,
238 'meta_value' => $this->saved_value,
239 'field_type' => $this->field->type,
240 );
241
242 if ( has_filter( 'frm_email_value' ) ) {
243 _deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
244 $this->displayed_value = apply_filters(
245 'frm_email_value',
246 $this->displayed_value,
247 (object) $meta,
248 $this->entry,
249 array(
250 'field' => $this->field,
251 )
252 );
253 }
254 }
255
256 // frm_display_{fieldtype}_value_custom hook
257 $this->displayed_value = apply_filters(
258 'frm_display_' . $this->field->type . '_value_custom',
259 $this->displayed_value,
260 array(
261 'field' => $this->field,
262 'entry' => $this->entry,
263 )
264 );
265
266 $this->displayed_value = apply_filters( 'frm_display_value', $this->displayed_value, $this->field, $atts );
267 }
268
269 /**
270 * Clean a field's saved value.
271 *
272 * @since 2.04
273 *
274 * @return void
275 */
276 protected function clean_saved_value() {
277 if ( $this->saved_value !== '' ) {
278 if ( ! is_array( $this->saved_value ) && ! is_object( $this->saved_value ) ) {
279 $field_type = FrmField::get_field_type( $this->field );
280 FrmFieldsHelper::prepare_field_value( $this->saved_value, $field_type );
281 }
282
283 if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
284 $this->saved_value = '';
285 }
286 }
287 }
288 }
289