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