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

228 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 } else if ( 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 $this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
164 }
165 }
166
167 /**
168 * Filter the displayed_value property
169 *
170 * @since 2.04
171 *
172 * @param array $atts
173 */
174 protected function filter_displayed_value( $atts ) {
175 $entry = FrmEntry::getOne( $this->entry_id, true );
176
177 // TODO: maybe change from 'source' to 'run_filters' = 'email'
178 if ( isset( $atts['source'] ) && $atts['source'] === 'entry_formatter' ) {
179 // Deprecated frm_email_value hook
180 $meta = array(
181 'item_id' => $entry->id,
182 'field_id' => $this->field->id,
183 'meta_value' => $this->saved_value,
184 'field_type' => $this->field->type,
185 );
186
187 if ( has_filter( 'frm_email_value' ) ) {
188 _deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
189 $this->displayed_value = apply_filters(
190 'frm_email_value',
191 $this->displayed_value,
192 (object) $meta,
193 $entry,
194 array(
195 'field' => $this->field,
196 )
197 );
198 }
199 }
200
201 // frm_display_{fieldtype}_value_custom hook
202 $this->displayed_value = apply_filters(
203 'frm_display_' . $this->field->type . '_value_custom',
204 $this->displayed_value,
205 array(
206 'field' => $this->field,
207 'entry' => $entry,
208 )
209 );
210 }
211
212 /**
213 * Clean a field's saved value
214 *
215 * @since 2.04
216 */
217 protected function clean_saved_value() {
218 if ( $this->saved_value !== '' ) {
219
220 $this->saved_value = maybe_unserialize( $this->saved_value );
221
222 if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
223 $this->saved_value = '';
224 }
225 }
226 }
227 }
228