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

266 lines 5.1 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 * @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 // This class shouldn't affect values.
107 unset( $atts['class'] );
108 $this->generate_displayed_value_for_field_type( $atts );
109 $this->filter_displayed_value( $atts );
110 }
111
112 /**
113 * Get a value from the field settings
114 *
115 * @since 2.05.06
116 *
117 * @param string $value
118 */
119 public function get_field_option( $value ) {
120 return FrmField::get_option( $this->field, $value );
121 }
122
123 /**
124 * @since 4.03
125 *
126 * @param string $option
127 */
128 public function get_field_attr( $option ) {
129 return is_object( $this->field ) ? $this->field->{$option} : '';
130 }
131
132 /**
133 * @since 4.03
134 *
135 * @return stdClass
136 */
137 public function get_field() {
138 return $this->field;
139 }
140
141 /**
142 * Get the field property's label
143 *
144 * @since 2.04
145 */
146 public function get_field_label() {
147 return $this->get_field_attr( 'name' );
148 }
149
150 /**
151 * Get the field property's id
152 *
153 * @since 2.05
154 */
155 public function get_field_id() {
156 return $this->get_field_attr( 'id' );
157 }
158
159 /**
160 * Get the field property's key
161 *
162 * @since 2.04
163 */
164 public function get_field_key() {
165 return $this->get_field_attr( 'field_key' );
166 }
167
168 /**
169 * Get the field property's type
170 *
171 * @since 2.04
172 */
173 public function get_field_type() {
174 return $this->get_field_attr( 'type' );
175 }
176
177 /**
178 * Get the saved_value property
179 *
180 * @since 2.04
181 */
182 public function get_saved_value() {
183 return $this->saved_value;
184 }
185
186 /**
187 * Get the displayed_value property
188 *
189 * @since 2.04
190 */
191 public function get_displayed_value() {
192 if ( $this->displayed_value === 'frm_not_prepared' ) {
193 return __( 'The display value has not been prepared. Please use the prepare_display_value() method before calling get_displayed_value().', 'formidable' );
194 }
195
196 return $this->displayed_value;
197 }
198
199 /**
200 * Get the displayed value for different field types
201 *
202 * @since 3.0
203 *
204 * @param array $atts
205 *
206 * @return void
207 */
208 protected function generate_displayed_value_for_field_type( $atts ) {
209 if ( ! FrmAppHelper::is_empty_value( $this->displayed_value, '' ) ) {
210 $field_obj = FrmFieldFactory::get_field_object( $this->field );
211
212 $this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
213 }
214 }
215
216 /**
217 * Filter the displayed_value property
218 *
219 * @since 2.04
220 *
221 * @param array $atts
222 *
223 * @return void
224 */
225 protected function filter_displayed_value( $atts ) {
226 if ( ! is_object( $this->entry ) || empty( $this->entry->metas ) ) {
227 $this->entry = FrmEntry::getOne( $this->entry_id, true );
228 if ( ! is_object( $this->entry ) ) {
229 return;
230 }
231 }
232
233 // frm_display_{fieldtype}_value_custom hook
234 $this->displayed_value = apply_filters(
235 'frm_display_' . $this->field->type . '_value_custom',
236 $this->displayed_value,
237 array(
238 'field' => $this->field,
239 'entry' => $this->entry,
240 )
241 );
242
243 $this->displayed_value = apply_filters( 'frm_display_value', $this->displayed_value, $this->field, $atts );
244 }
245
246 /**
247 * Clean a field's saved value.
248 *
249 * @since 2.04
250 *
251 * @return void
252 */
253 protected function clean_saved_value() {
254 if ( $this->saved_value !== '' ) {
255 if ( ! is_array( $this->saved_value ) && ! is_object( $this->saved_value ) ) {
256 $field_type = FrmField::get_field_type( $this->field );
257 FrmFieldsHelper::prepare_field_value( $this->saved_value, $field_type );
258 }
259
260 if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
261 $this->saved_value = '';
262 }
263 }
264 }
265 }
266