| 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 FrmEntryValues { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var stdClass |
| 13 |
*/ |
| 14 |
protected $entry = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
protected $form_id; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
protected $fields = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* @var FrmFieldValue[] |
| 28 |
*/ |
| 29 |
protected $field_values = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $user_info = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $include_fields = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
protected $exclude_fields = array(); |
| 45 |
|
| 46 |
/** |
| 47 |
* FrmEntryValues constructor |
| 48 |
* |
| 49 |
* @since 2.04 |
| 50 |
* |
| 51 |
* @param int|string $entry_id |
| 52 |
* @param array $atts |
| 53 |
*/ |
| 54 |
public function __construct( $entry_id, $atts = array() ) { |
| 55 |
if ( isset( $atts['entry'] ) && is_object( $atts['entry'] ) && ! empty( $atts['entry']->metas ) ) { |
| 56 |
$this->entry = $atts['entry']; |
| 57 |
} else { |
| 58 |
$this->init_entry( $entry_id ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( $this->entry === null || $this->entry === false ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$this->init_form_id(); |
| 66 |
$this->init_include_fields( $atts ); |
| 67 |
$this->init_exclude_fields( $atts ); |
| 68 |
$this->init_fields(); |
| 69 |
$this->init_field_values(); |
| 70 |
$this->init_user_info(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Set the entry property |
| 75 |
* |
| 76 |
* @since 2.04 |
| 77 |
* |
| 78 |
* @param int|string $entry_id |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
protected function init_entry( $entry_id ) { |
| 83 |
$this->entry = FrmEntry::getOne( $entry_id, true ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets entry property. |
| 88 |
* |
| 89 |
* @since 5.0.16 |
| 90 |
* @return stdClass |
| 91 |
*/ |
| 92 |
public function get_entry() { |
| 93 |
return $this->entry; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Set the form_id property |
| 98 |
* |
| 99 |
* @since 2.04 |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
protected function init_form_id() { |
| 104 |
$this->form_id = (int) $this->entry->form_id; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Set the include_fields property |
| 109 |
* |
| 110 |
* @since 2.04 |
| 111 |
* |
| 112 |
* @param array $atts |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
protected function init_include_fields( $atts ) { |
| 117 |
|
| 118 |
// For reverse compatibility with the fields parameter |
| 119 |
if ( ! isset( $atts['include_fields'] ) || empty( $atts['include_fields'] ) ) { |
| 120 |
|
| 121 |
if ( isset( $atts['fields'] ) && ! empty( $atts['fields'] ) ) { |
| 122 |
|
| 123 |
if ( ! is_array( $atts['fields'] ) ) { |
| 124 |
$atts['include_fields'] = $atts['fields']; |
| 125 |
} else { |
| 126 |
$atts['include_fields'] = ''; |
| 127 |
|
| 128 |
foreach ( $atts['fields'] as $included_field ) { |
| 129 |
$atts['include_fields'] .= $included_field->id . ','; |
| 130 |
} |
| 131 |
|
| 132 |
$atts['include_fields'] = rtrim( $atts['include_fields'], ',' ); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
$this->include_fields = $this->prepare_array_property( 'include_fields', $atts ); |
| 138 |
|
| 139 |
/** |
| 140 |
* Allows modifying the IDs of include_fields used in the entry values. |
| 141 |
* |
| 142 |
* @since 5.0.04 |
| 143 |
* |
| 144 |
* @param array $field_ids The list of field IDs. |
| 145 |
* @param array $atts The arguments. See {@see FrmEntriesController::show_entry_shortcode()}. |
| 146 |
*/ |
| 147 |
$this->include_fields = apply_filters( 'frm_entry_values_include_fields', $this->include_fields, $atts ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Set the exclude_fields property |
| 152 |
* |
| 153 |
* @since 2.04 |
| 154 |
* |
| 155 |
* @param array $atts |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
protected function init_exclude_fields( $atts ) { |
| 160 |
$this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts ); |
| 161 |
|
| 162 |
/** |
| 163 |
* Allows modifying the IDs of exclude_fields used in the entry values. |
| 164 |
* |
| 165 |
* @since 5.0.04 |
| 166 |
* |
| 167 |
* @param array $field_ids The list of field IDs. |
| 168 |
* @param array $atts The arguments. See {@see FrmEntriesController::show_entry_shortcode()}. |
| 169 |
*/ |
| 170 |
$this->exclude_fields = apply_filters( 'frm_entry_values_exclude_fields', $this->exclude_fields, $atts ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Prepare an array property value, such as include_fields and exclude_fields |
| 175 |
* |
| 176 |
* @since 2.04 |
| 177 |
* |
| 178 |
* @param string $index |
| 179 |
* @param array $atts |
| 180 |
* |
| 181 |
* @return array |
| 182 |
*/ |
| 183 |
private function prepare_array_property( $index, $atts ) { |
| 184 |
if ( isset( $atts[ $index ] ) && ! empty( $atts[ $index ] ) ) { |
| 185 |
|
| 186 |
if ( is_array( $atts[ $index ] ) ) { |
| 187 |
$property = $atts[ $index ]; |
| 188 |
} else { |
| 189 |
$property = explode( ',', $atts[ $index ] ); |
| 190 |
} |
| 191 |
} else { |
| 192 |
$property = array(); |
| 193 |
} |
| 194 |
|
| 195 |
return $property; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Set the fields property |
| 200 |
* |
| 201 |
* @since 2.04 |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
protected function init_fields() { |
| 206 |
$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' ); |
| 207 |
|
| 208 |
/** |
| 209 |
* Allows modifying the list of all field in the form that is used in the entry values. |
| 210 |
* |
| 211 |
* @since 5.0.04 |
| 212 |
* |
| 213 |
* @param array $fields The list of fields. |
| 214 |
* @param array $args The arguments. Contains `form_id`, `entry`. |
| 215 |
*/ |
| 216 |
$this->fields = apply_filters( |
| 217 |
'frm_entry_values_fields', |
| 218 |
$this->fields, |
| 219 |
array( |
| 220 |
'form_id' => $this->form_id, |
| 221 |
'entry' => $this->entry, |
| 222 |
) |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Set the field_values property |
| 228 |
* |
| 229 |
* @since 2.04 |
| 230 |
* |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
protected function init_field_values() { |
| 234 |
foreach ( $this->fields as $field ) { |
| 235 |
if ( $this->is_field_included( $field ) ) { |
| 236 |
$this->add_field_values( $field ); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get the field_values property |
| 243 |
* |
| 244 |
* @since 2.04 |
| 245 |
* |
| 246 |
* @return array |
| 247 |
*/ |
| 248 |
public function get_field_values() { |
| 249 |
return $this->field_values; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Set the user_info property |
| 254 |
* |
| 255 |
* @since 2.04 |
| 256 |
* |
| 257 |
* @return void |
| 258 |
*/ |
| 259 |
protected function init_user_info() { |
| 260 |
if ( isset( $this->entry->description ) ) { |
| 261 |
$entry_description = (array) $this->entry->description; |
| 262 |
} else { |
| 263 |
$entry_description = array( |
| 264 |
'browser' => '', |
| 265 |
'referrer' => '', |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
$ip = array( |
| 270 |
'label' => __( 'IP Address', 'formidable' ), |
| 271 |
'value' => $this->entry->ip, |
| 272 |
); |
| 273 |
$browser = array( |
| 274 |
'label' => __( 'User-Agent (Browser/OS)', 'formidable' ), |
| 275 |
'value' => isset( $entry_description['browser'] ) ? FrmEntriesHelper::get_browser( $entry_description['browser'] ) : '', |
| 276 |
); |
| 277 |
$referrer = array( |
| 278 |
'label' => __( 'Referrer', 'formidable' ), |
| 279 |
'value' => isset( $entry_description['referrer'] ) ? $entry_description['referrer'] : '', |
| 280 |
); |
| 281 |
|
| 282 |
/** |
| 283 |
* Allow the referrer to be modified. |
| 284 |
* |
| 285 |
* @since 5.5.1 |
| 286 |
* |
| 287 |
* @param array $referrer |
| 288 |
* @param array @entry_description |
| 289 |
*/ |
| 290 |
$referrer = apply_filters( 'frm_user_info_referrer', $referrer, $entry_description, $this->entry ); |
| 291 |
|
| 292 |
$this->user_info = array( |
| 293 |
'ip' => $ip, |
| 294 |
'browser' => $browser, |
| 295 |
'referrer' => $referrer, |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Get the user_info property |
| 301 |
* |
| 302 |
* @since 2.04 |
| 303 |
* |
| 304 |
* @return array |
| 305 |
*/ |
| 306 |
public function get_user_info() { |
| 307 |
return $this->user_info; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Check if a field should be included in the values |
| 312 |
* |
| 313 |
* @since 2.04 |
| 314 |
* |
| 315 |
* @param stdClass $field |
| 316 |
* |
| 317 |
* @return bool |
| 318 |
*/ |
| 319 |
protected function is_field_included( $field ) { |
| 320 |
$is_included = true; |
| 321 |
if ( ! empty( $this->include_fields ) ) { |
| 322 |
$is_included = $this->is_field_in_array( $field, $this->include_fields ); |
| 323 |
} |
| 324 |
|
| 325 |
if ( ! empty( $this->exclude_fields ) ) { |
| 326 |
$is_excluded = $this->is_field_in_array( $field, $this->exclude_fields ); |
| 327 |
if ( $is_excluded ) { |
| 328 |
$is_included = false; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return $is_included; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Check if a field is in the include fields or exclude fields array |
| 337 |
* |
| 338 |
* @since 2.04 |
| 339 |
* |
| 340 |
* @param stdClass $field |
| 341 |
* @param array $array |
| 342 |
* |
| 343 |
* @return bool |
| 344 |
*/ |
| 345 |
protected function is_field_in_array( $field, $array ) { |
| 346 |
return in_array( $field->id, $array ) || in_array( (string) $field->field_key, $array, true ); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Add a field's values to the field_values property |
| 351 |
* |
| 352 |
* @since 2.04 |
| 353 |
* |
| 354 |
* @param stdClass $field |
| 355 |
* |
| 356 |
* @return void |
| 357 |
*/ |
| 358 |
protected function add_field_values( $field ) { |
| 359 |
$this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry ); |
| 360 |
} |
| 361 |
} |
| 362 |
|