| 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 |
protected function init_entry( $entry_id ) { |
| 81 |
$this->entry = FrmEntry::getOne( $entry_id, true ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Set the form_id property |
| 86 |
* |
| 87 |
* @since 2.04 |
| 88 |
*/ |
| 89 |
protected function init_form_id() { |
| 90 |
$this->form_id = (int) $this->entry->form_id; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Set the include_fields property |
| 95 |
* |
| 96 |
* @since 2.04 |
| 97 |
* |
| 98 |
* @param array $atts |
| 99 |
*/ |
| 100 |
protected function init_include_fields( $atts ) { |
| 101 |
|
| 102 |
// For reverse compatibility with the fields parameter |
| 103 |
if ( ! isset( $atts['include_fields'] ) || empty( $atts['include_fields'] ) ) { |
| 104 |
|
| 105 |
if ( isset( $atts['fields'] ) && ! empty( $atts['fields'] ) ) { |
| 106 |
|
| 107 |
if ( ! is_array( $atts['fields'] ) ) { |
| 108 |
$atts['include_fields'] = $atts['fields']; |
| 109 |
} else { |
| 110 |
$atts['include_fields'] = ''; |
| 111 |
|
| 112 |
foreach ( $atts['fields'] as $included_field ) { |
| 113 |
$atts['include_fields'] .= $included_field->id . ','; |
| 114 |
} |
| 115 |
|
| 116 |
$atts['include_fields'] = rtrim( $atts['include_fields'], ',' ); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$this->include_fields = $this->prepare_array_property( 'include_fields', $atts ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Set the exclude_fields property |
| 126 |
* |
| 127 |
* @since 2.04 |
| 128 |
* |
| 129 |
* @param array $atts |
| 130 |
*/ |
| 131 |
protected function init_exclude_fields( $atts ) { |
| 132 |
$this->exclude_fields = $this->prepare_array_property( 'exclude_fields', $atts ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Prepare an array property value, such as include_fields and exclude_fields |
| 137 |
* |
| 138 |
* @since 2.04 |
| 139 |
* |
| 140 |
* @param string $index |
| 141 |
* @param array $atts |
| 142 |
* |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
private function prepare_array_property( $index, $atts ) { |
| 146 |
if ( isset( $atts[ $index ] ) && ! empty( $atts[ $index ] ) ) { |
| 147 |
|
| 148 |
if ( is_array( $atts[ $index ] ) ) { |
| 149 |
$property = $atts[ $index ]; |
| 150 |
} else { |
| 151 |
$property = explode( ',', $atts[ $index ] ); |
| 152 |
} |
| 153 |
} else { |
| 154 |
$property = array(); |
| 155 |
} |
| 156 |
|
| 157 |
return $property; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Set the fields property |
| 162 |
* |
| 163 |
* @since 2.04 |
| 164 |
*/ |
| 165 |
protected function init_fields() { |
| 166 |
$this->fields = FrmField::get_all_for_form( $this->form_id, '', 'exclude', 'exclude' ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Set the field_values property |
| 171 |
* |
| 172 |
* @since 2.04 |
| 173 |
*/ |
| 174 |
protected function init_field_values() { |
| 175 |
foreach ( $this->fields as $field ) { |
| 176 |
if ( $this->is_field_included( $field ) ) { |
| 177 |
$this->add_field_values( $field ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get the field_values property |
| 184 |
* |
| 185 |
* @since 2.04 |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
public function get_field_values() { |
| 190 |
return $this->field_values; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Set the user_info property |
| 195 |
* |
| 196 |
* @since 2.04 |
| 197 |
*/ |
| 198 |
protected function init_user_info() { |
| 199 |
if ( isset( $this->entry->description ) ) { |
| 200 |
$entry_description = (array) $this->entry->description; |
| 201 |
} else { |
| 202 |
$entry_description = array( |
| 203 |
'browser' => '', |
| 204 |
'referrer' => '', |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
$ip = array( |
| 209 |
'label' => __( 'IP Address', 'formidable' ), |
| 210 |
'value' => $this->entry->ip, |
| 211 |
); |
| 212 |
|
| 213 |
$browser = array( |
| 214 |
'label' => __( 'User-Agent (Browser/OS)', 'formidable' ), |
| 215 |
'value' => FrmEntriesHelper::get_browser( $entry_description['browser'] ), |
| 216 |
); |
| 217 |
|
| 218 |
$referrer = array( |
| 219 |
'label' => __( 'Referrer', 'formidable' ), |
| 220 |
'value' => $entry_description['referrer'], |
| 221 |
); |
| 222 |
|
| 223 |
$this->user_info = array( |
| 224 |
'ip' => $ip, |
| 225 |
'browser' => $browser, |
| 226 |
'referrer' => $referrer, |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get the user_info property |
| 232 |
* |
| 233 |
* @since 2.04 |
| 234 |
* |
| 235 |
* @return array |
| 236 |
*/ |
| 237 |
public function get_user_info() { |
| 238 |
return $this->user_info; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Check if a field should be included in the values |
| 243 |
* |
| 244 |
* @since 2.04 |
| 245 |
* |
| 246 |
* @param stdClass $field |
| 247 |
* |
| 248 |
* @return bool |
| 249 |
*/ |
| 250 |
protected function is_field_included( $field ) { |
| 251 |
$is_included = true; |
| 252 |
if ( ! empty( $this->include_fields ) ) { |
| 253 |
$is_included = $this->is_field_in_array( $field, $this->include_fields ); |
| 254 |
} |
| 255 |
|
| 256 |
if ( ! empty( $this->exclude_fields ) ) { |
| 257 |
$is_excluded = $this->is_field_in_array( $field, $this->exclude_fields ); |
| 258 |
if ( $is_excluded ) { |
| 259 |
$is_included = false; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return $is_included; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Check if a field is in the include fields or exclude fields array |
| 268 |
* |
| 269 |
* @since 2.04 |
| 270 |
* |
| 271 |
* @param stdClass $field |
| 272 |
* @param array $array |
| 273 |
* |
| 274 |
* @return bool |
| 275 |
*/ |
| 276 |
protected function is_field_in_array( $field, $array ) { |
| 277 |
return in_array( $field->id, $array ) || in_array( (string) $field->field_key, $array, true ); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Add a field's values to the field_values property |
| 282 |
* |
| 283 |
* @since 2.04 |
| 284 |
* |
| 285 |
* @param stdClass $field |
| 286 |
*/ |
| 287 |
protected function add_field_values( $field ) { |
| 288 |
$this->field_values[ $field->id ] = new FrmFieldValue( $field, $this->entry ); |
| 289 |
} |
| 290 |
} |
| 291 |
|