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

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