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

229 lines 4.5 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 2.04
17 *
18 * @var int
19 */
20 protected $entry_id = 0;
21
22 /**
23 * @since 2.04
24 *
25 * @var mixed
26 */
27 protected $saved_value = '';
28
29 /**
30 * @since 2.04
31 *
32 * @var mixed
33 */
34 protected $displayed_value = 'frm_not_prepared';
35
36 /**
37 * FrmFieldValue constructor.
38 *
39 * @param stdClass $field
40 * @param stdClass $entry
41 */
42 public function __construct( $field, $entry ) {
43 if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
44 return;
45 }
46
47 $this->entry_id = $entry->id;
48 $this->field = $field;
49 $this->init_saved_value( $entry );
50 }
51
52 /**
53 * Initialize the saved_value property
54 *
55 * @since 2.04
56 *
57 * @param stdClass $entry
58 */
59 protected function init_saved_value( $entry ) {
60 if ( $this->field->type === 'html' ) {
61 $this->saved_value = $this->field->description;
62 } elseif ( isset( $entry->metas[ $this->field->id ] ) ) {
63 $this->saved_value = $entry->metas[ $this->field->id ];
64 } else {
65 $this->saved_value = '';
66 }
67
68 $this->clean_saved_value();
69 }
70
71 /**
72 * Prepare the display value
73 *
74 * @since 2.05
75 *
76 * @param array $atts
77 */
78 public function prepare_displayed_value( $atts = array() ) {
79 $this->displayed_value = $this->saved_value;
80 $this->generate_displayed_value_for_field_type( $atts );
81 $this->filter_displayed_value( $atts );
82 }
83
84 /**
85 * Get a value from the field settings
86 *
87 * @since 2.05.06
88 */
89 public function get_field_option( $value ) {
90 return FrmField::get_option( $this->field, $value );
91 }
92
93 /**
94 * Get the field property's label
95 *
96 * @since 2.04
97 */
98 public function get_field_label() {
99 return $this->field->name;
100 }
101
102 /**
103 * Get the field property's id
104 *
105 * @since 2.05
106 */
107 public function get_field_id() {
108 return $this->field->id;
109 }
110
111 /**
112 * Get the field property's key
113 *
114 * @since 2.04
115 */
116 public function get_field_key() {
117 return $this->field->field_key;
118 }
119
120 /**
121 * Get the field property's type
122 *
123 * @since 2.04
124 */
125 public function get_field_type() {
126 return $this->field->type;
127 }
128
129 /**
130 * Get the saved_value property
131 *
132 * @since 2.04
133 */
134 public function get_saved_value() {
135 return $this->saved_value;
136 }
137
138 /**
139 * Get the displayed_value property
140 *
141 * @since 2.04
142 */
143 public function get_displayed_value() {
144 if ( $this->displayed_value === 'frm_not_prepared' ) {
145 return __( 'The display value has not been prepared. Please use the prepare_display_value() method before calling get_displayed_value().', 'formidable' );
146 }
147
148 return $this->displayed_value;
149 }
150
151 /**
152 * Get the displayed value for different field types
153 *
154 * @since 3.0
155 *
156 * @param array $atts
157 *
158 * @return mixed
159 */
160 protected function generate_displayed_value_for_field_type( $atts ) {
161 if ( ! FrmAppHelper::is_empty_value( $this->displayed_value, '' ) ) {
162 $field_obj = FrmFieldFactory::get_field_object( $this->field );
163
164 $this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
165 }
166 }
167
168 /**
169 * Filter the displayed_value property
170 *
171 * @since 2.04
172 *
173 * @param array $atts
174 */
175 protected function filter_displayed_value( $atts ) {
176 $entry = FrmEntry::getOne( $this->entry_id, true );
177
178 // TODO: maybe change from 'source' to 'run_filters' = 'email'
179 if ( isset( $atts['source'] ) && $atts['source'] === 'entry_formatter' ) {
180 // Deprecated frm_email_value hook
181 $meta = array(
182 'item_id' => $entry->id,
183 'field_id' => $this->field->id,
184 'meta_value' => $this->saved_value,
185 'field_type' => $this->field->type,
186 );
187
188 if ( has_filter( 'frm_email_value' ) ) {
189 _deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
190 $this->displayed_value = apply_filters(
191 'frm_email_value',
192 $this->displayed_value,
193 (object) $meta,
194 $entry,
195 array(
196 'field' => $this->field,
197 )
198 );
199 }
200 }
201
202 // frm_display_{fieldtype}_value_custom hook
203 $this->displayed_value = apply_filters(
204 'frm_display_' . $this->field->type . '_value_custom',
205 $this->displayed_value,
206 array(
207 'field' => $this->field,
208 'entry' => $entry,
209 )
210 );
211 }
212
213 /**
214 * Clean a field's saved value
215 *
216 * @since 2.04
217 */
218 protected function clean_saved_value() {
219 if ( $this->saved_value !== '' ) {
220
221 $this->saved_value = maybe_unserialize( $this->saved_value );
222
223 if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
224 $this->saved_value = '';
225 }
226 }
227 }
228 }
229