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

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