| 1 |
<?php |
| 2 |
if ( ! defined('ABSPATH') ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmEntriesHelper { |
| 7 |
|
| 8 |
public static function setup_new_vars( $fields, $form = '', $reset = false, $args = array() ) { |
| 9 |
$values = array( |
| 10 |
'name' => '', |
| 11 |
'description' => '', |
| 12 |
'item_key' => '', |
| 13 |
); |
| 14 |
|
| 15 |
$values['fields'] = array(); |
| 16 |
if ( empty($fields) ) { |
| 17 |
return apply_filters('frm_setup_new_entry', $values); |
| 18 |
} |
| 19 |
|
| 20 |
foreach ( (array) $fields as $field ) { |
| 21 |
self::prepare_field_default_value( $field ); |
| 22 |
$new_value = self::get_field_value_for_new_entry( $field, $reset, $args ); |
| 23 |
|
| 24 |
$field_array = array( |
| 25 |
'id' => $field->id, |
| 26 |
'value' => $new_value, |
| 27 |
'default_value' => $field->default_value, |
| 28 |
'name' => $field->name, |
| 29 |
'description' => $field->description, |
| 30 |
'type' => apply_filters('frm_field_type', $field->type, $field, $new_value), |
| 31 |
'options' => $field->options, |
| 32 |
'required' => $field->required, |
| 33 |
'field_key' => $field->field_key, |
| 34 |
'field_order' => $field->field_order, |
| 35 |
'form_id' => $field->form_id, |
| 36 |
'parent_form_id' => isset( $args['parent_form_id'] ) ? $args['parent_form_id'] : $field->form_id, |
| 37 |
'reset_value' => $reset, |
| 38 |
'in_embed_form' => isset( $args['in_embed_form'] ) ? $args['in_embed_form'] : '0', |
| 39 |
); |
| 40 |
|
| 41 |
$opt_defaults = FrmFieldsHelper::get_default_field_opts($field_array['type'], $field, true); |
| 42 |
$opt_defaults['required_indicator'] = ''; |
| 43 |
$opt_defaults['original_type'] = $field->type; |
| 44 |
|
| 45 |
foreach ( $opt_defaults as $opt => $default_opt ) { |
| 46 |
$field_array[ $opt ] = ( isset( $field->field_options[ $opt ] ) && $field->field_options[ $opt ] != '' ) ? $field->field_options[ $opt ] : $default_opt; |
| 47 |
unset($opt, $default_opt); |
| 48 |
} |
| 49 |
|
| 50 |
unset($opt_defaults); |
| 51 |
|
| 52 |
if ( $field_array['custom_html'] == '' ) { |
| 53 |
$field_array['custom_html'] = FrmFieldsHelper::get_default_html($field->type); |
| 54 |
} |
| 55 |
|
| 56 |
$field_array = apply_filters('frm_setup_new_fields_vars', $field_array, $field, $args ); |
| 57 |
$field_array = array_merge( $field->field_options, $field_array ); |
| 58 |
|
| 59 |
$values['fields'][] = $field_array; |
| 60 |
|
| 61 |
if ( ! $form || ! isset($form->id) ) { |
| 62 |
$form = FrmForm::getOne($field->form_id); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$form->options = maybe_unserialize( $form->options ); |
| 67 |
if ( is_array( $form->options ) ) { |
| 68 |
$values = array_merge( $values, $form->options ); |
| 69 |
} |
| 70 |
|
| 71 |
$form_defaults = FrmFormsHelper::get_default_opts(); |
| 72 |
|
| 73 |
$frm_settings = FrmAppHelper::get_settings(); |
| 74 |
$form_defaults['custom_style'] = ( $frm_settings->load_style != 'none' ); |
| 75 |
|
| 76 |
$values = array_merge( $form_defaults, $values ); |
| 77 |
|
| 78 |
return apply_filters( 'frm_setup_new_entry', $values ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @since 2.05 |
| 83 |
*/ |
| 84 |
private static function prepare_field_default_value( &$field ) { |
| 85 |
//If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array |
| 86 |
$return_array = FrmField::is_field_with_multiple_values( $field ); |
| 87 |
|
| 88 |
// Do any shortcodes in default value and allow customization of default value |
| 89 |
$field->default_value = apply_filters( 'frm_get_default_value', $field->default_value, $field, true, $return_array ); |
| 90 |
// Calls FrmProFieldsHelper::get_default_value |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Set the value for each field |
| 95 |
* This function is used when the form is first loaded and on all page turns *for a new entry* |
| 96 |
* |
| 97 |
* @since 2.0.13 |
| 98 |
* |
| 99 |
* @param object $field - this is passed by reference since it is an object |
| 100 |
* @param boolean $reset |
| 101 |
* @param array $args |
| 102 |
* @return string|array $new_value |
| 103 |
*/ |
| 104 |
private static function get_field_value_for_new_entry( $field, $reset, $args ) { |
| 105 |
$new_value = $field->default_value; |
| 106 |
|
| 107 |
if ( ! $reset && self::value_is_posted( $field, $args ) ) { |
| 108 |
self::get_posted_value( $field, $new_value, $args ); |
| 109 |
} else if ( FrmField::is_option_true( $field, 'clear_on_focus' ) ) { |
| 110 |
// If clear on focus is selected, the value should be blank (unless it was posted, of course) |
| 111 |
|
| 112 |
// TODO: move to Pro |
| 113 |
if ( 'address' == $field->type && isset( $new_value['country'] ) ) { |
| 114 |
$new_value = array( 'country' => $new_value['country'] ); |
| 115 |
} else { |
| 116 |
$new_value = ''; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! is_array( $new_value ) ) { |
| 121 |
$new_value = str_replace('"', '"', $new_value); |
| 122 |
} |
| 123 |
|
| 124 |
return $new_value; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Check if a field has a posted value |
| 129 |
* |
| 130 |
* @since 2.01.0 |
| 131 |
* @param object $field |
| 132 |
* @param array $args |
| 133 |
* @return boolean $value_is_posted |
| 134 |
*/ |
| 135 |
public static function value_is_posted( $field, $args ) { |
| 136 |
$value_is_posted = false; |
| 137 |
if ( $_POST ) { |
| 138 |
$repeating = isset( $args['repeating'] ) && $args['repeating']; |
| 139 |
if ( $repeating ) { |
| 140 |
if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] ) ) { |
| 141 |
$value_is_posted = true; |
| 142 |
} |
| 143 |
} else if ( isset( $_POST['item_meta'][ $field->id ] ) ) { |
| 144 |
$value_is_posted = true; |
| 145 |
} |
| 146 |
} |
| 147 |
return $value_is_posted; |
| 148 |
} |
| 149 |
|
| 150 |
public static function setup_edit_vars( $values, $record ) { |
| 151 |
$values['item_key'] = FrmAppHelper::get_post_param( 'item_key', $record->item_key, 'sanitize_title' ); |
| 152 |
$values['form_id'] = $record->form_id; |
| 153 |
$values['is_draft'] = $record->is_draft; |
| 154 |
return apply_filters('frm_setup_edit_entry_vars', $values, $record); |
| 155 |
} |
| 156 |
|
| 157 |
public static function get_admin_params( $form = null ) { |
| 158 |
_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_admin_params' ); |
| 159 |
return FrmForm::set_current_form( $form ); |
| 160 |
} |
| 161 |
|
| 162 |
public static function set_current_form( $form_id ) { |
| 163 |
_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::set_current_form' ); |
| 164 |
return FrmForm::set_current_form( $form_id ); |
| 165 |
} |
| 166 |
|
| 167 |
public static function get_current_form( $form_id = 0 ) { |
| 168 |
_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_current_form' ); |
| 169 |
return FrmForm::get_current_form( $form_id ); |
| 170 |
} |
| 171 |
|
| 172 |
public static function get_current_form_id() { |
| 173 |
_deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::get_current_form_id' ); |
| 174 |
return FrmForm::get_current_form_id(); |
| 175 |
} |
| 176 |
|
| 177 |
public static function maybe_get_entry( &$entry ) { |
| 178 |
_deprecated_function( __FUNCTION__, '2.0.9', 'FrmEntry::maybe_get_entry' ); |
| 179 |
FrmEntry::maybe_get_entry( $entry ); |
| 180 |
} |
| 181 |
|
| 182 |
public static function replace_default_message( $message, $atts ) { |
| 183 |
if ( strpos($message, '[default-message') === false && |
| 184 |
strpos($message, '[default_message') === false && |
| 185 |
! empty( $message ) ) { |
| 186 |
return $message; |
| 187 |
} |
| 188 |
|
| 189 |
if ( empty($message) ) { |
| 190 |
$message = '[default-message]'; |
| 191 |
} |
| 192 |
|
| 193 |
preg_match_all("/\[(default-message|default_message)\b(.*?)(?:(\/))?\]/s", $message, $shortcodes, PREG_PATTERN_ORDER); |
| 194 |
|
| 195 |
foreach ( $shortcodes[0] as $short_key => $tag ) { |
| 196 |
$add_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] ); |
| 197 |
if ( ! empty( $add_atts ) ) { |
| 198 |
$this_atts = array_merge($atts, $add_atts); |
| 199 |
} else { |
| 200 |
$this_atts = $atts; |
| 201 |
} |
| 202 |
|
| 203 |
$default = FrmEntriesController::show_entry_shortcode( $this_atts ); |
| 204 |
|
| 205 |
// Add the default message |
| 206 |
$message = str_replace( $shortcodes[0][ $short_key ], $default, $message ); |
| 207 |
} |
| 208 |
|
| 209 |
return $message; |
| 210 |
} |
| 211 |
|
| 212 |
public static function prepare_display_value( $entry, $field, $atts ) { |
| 213 |
$field_value = isset( $entry->metas[ $field->id ] ) ? $entry->metas[ $field->id ] : false; |
| 214 |
|
| 215 |
if ( FrmAppHelper::pro_is_installed() ) { |
| 216 |
FrmProEntriesHelper::get_dynamic_list_values( $field, $entry, $field_value ); |
| 217 |
} |
| 218 |
|
| 219 |
if ( $field->form_id == $entry->form_id || empty($atts['embedded_field_id']) ) { |
| 220 |
return self::display_value($field_value, $field, $atts); |
| 221 |
} |
| 222 |
|
| 223 |
// this is an embeded form |
| 224 |
$val = ''; |
| 225 |
|
| 226 |
if ( strpos($atts['embedded_field_id'], 'form') === 0 ) { |
| 227 |
//this is a repeating section |
| 228 |
$child_entries = FrmEntry::getAll( array( 'it.parent_item_id' => $entry->id ) ); |
| 229 |
} else { |
| 230 |
// get all values for this field |
| 231 |
$child_values = isset( $entry->metas[ $atts['embedded_field_id'] ] ) ? $entry->metas[ $atts['embedded_field_id'] ] : false; |
| 232 |
|
| 233 |
if ( $child_values ) { |
| 234 |
$child_entries = FrmEntry::getAll( array( 'it.id' => (array) $child_values ) ); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
$field_value = array(); |
| 239 |
|
| 240 |
if ( ! isset($child_entries) || ! $child_entries || ! FrmAppHelper::pro_is_installed() ) { |
| 241 |
return $val; |
| 242 |
} |
| 243 |
|
| 244 |
foreach ( $child_entries as $child_entry ) { |
| 245 |
$atts['item_id'] = $child_entry->id; |
| 246 |
$atts['post_id'] = $child_entry->post_id; |
| 247 |
|
| 248 |
// get the value for this field -- check for post values as well |
| 249 |
$entry_val = FrmProEntryMetaHelper::get_post_or_meta_value($child_entry, $field); |
| 250 |
|
| 251 |
if ( $entry_val ) { |
| 252 |
// foreach entry get display_value |
| 253 |
$field_value[] = self::display_value($entry_val, $field, $atts); |
| 254 |
} |
| 255 |
|
| 256 |
unset($child_entry); |
| 257 |
} |
| 258 |
|
| 259 |
$val = implode(', ', (array) $field_value ); |
| 260 |
return FrmAppHelper::kses( $val, 'all' ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Prepare the saved value for display |
| 265 |
* @return string |
| 266 |
*/ |
| 267 |
public static function display_value( $value, $field, $atts = array() ) { |
| 268 |
|
| 269 |
$defaults = array( |
| 270 |
'type' => '', |
| 271 |
'html' => false, |
| 272 |
'show_filename' => true, |
| 273 |
'truncate' => false, |
| 274 |
'sep' => ', ', |
| 275 |
'post_id' => 0, |
| 276 |
'form_id' => $field->form_id, |
| 277 |
'field' => $field, |
| 278 |
'keepjs' => 0, |
| 279 |
'return_array' => false, |
| 280 |
); |
| 281 |
|
| 282 |
$atts = wp_parse_args( $atts, $defaults ); |
| 283 |
$atts = apply_filters('frm_display_value_atts', $atts, $field, $value); |
| 284 |
|
| 285 |
if ( ! isset($field->field_options['post_field']) ) { |
| 286 |
$field->field_options['post_field'] = ''; |
| 287 |
} |
| 288 |
|
| 289 |
if ( ! isset($field->field_options['custom_field']) ) { |
| 290 |
$field->field_options['custom_field'] = ''; |
| 291 |
} |
| 292 |
|
| 293 |
if ( FrmAppHelper::pro_is_installed() && $atts['post_id'] && ( $field->field_options['post_field'] || $atts['type'] == 'tag' ) ) { |
| 294 |
$atts['pre_truncate'] = $atts['truncate']; |
| 295 |
$atts['truncate'] = true; |
| 296 |
$atts['exclude_cat'] = isset($field->field_options['exclude_cat']) ? $field->field_options['exclude_cat'] : 0; |
| 297 |
|
| 298 |
$value = FrmProEntryMetaHelper::get_post_value($atts['post_id'], $field->field_options['post_field'], $field->field_options['custom_field'], $atts); |
| 299 |
$atts['truncate'] = $atts['pre_truncate']; |
| 300 |
} |
| 301 |
|
| 302 |
if ( $value == '' ) { |
| 303 |
return $value; |
| 304 |
} |
| 305 |
|
| 306 |
$value = apply_filters('frm_display_value_custom', maybe_unserialize($value), $field, $atts); |
| 307 |
$value = apply_filters( 'frm_display_' . $field->type . '_value_custom', $value, compact( 'field', 'atts' ) ); |
| 308 |
|
| 309 |
$new_value = ''; |
| 310 |
|
| 311 |
if ( is_array($value) && $atts['type'] != 'file' ) { |
| 312 |
foreach ( $value as $val ) { |
| 313 |
if ( is_array($val) ) { |
| 314 |
//TODO: add options for display (li or ,) |
| 315 |
$new_value .= implode($atts['sep'], $val); |
| 316 |
if ( $atts['type'] != 'data' ) { |
| 317 |
$new_value .= '<br/>'; |
| 318 |
} |
| 319 |
} |
| 320 |
unset($val); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if ( ! empty($new_value) ) { |
| 325 |
$value = $new_value; |
| 326 |
} else if ( is_array($value) && $atts['type'] != 'file' && ! $atts['return_array'] ) { |
| 327 |
$value = implode($atts['sep'], $value); |
| 328 |
} |
| 329 |
|
| 330 |
if ( $atts['truncate'] && $atts['type'] != 'image' ) { |
| 331 |
$value = FrmAppHelper::truncate($value, 50); |
| 332 |
} |
| 333 |
|
| 334 |
if ( ! $atts['keepjs'] && ! is_array( $value ) ) { |
| 335 |
$value = FrmAppHelper::kses( $value, 'all' ); |
| 336 |
} |
| 337 |
|
| 338 |
return apply_filters('frm_display_value', $value, $field, $atts); |
| 339 |
} |
| 340 |
|
| 341 |
public static function set_posted_value( $field, $value, $args ) { |
| 342 |
// If validating a field with "other" opt, set back to prev value now |
| 343 |
if ( isset( $args['other'] ) && $args['other'] ) { |
| 344 |
$value = $args['temp_value']; |
| 345 |
} |
| 346 |
if ( empty($args['parent_field_id']) ) { |
| 347 |
$_POST['item_meta'][ $field->id ] = $value; |
| 348 |
} else { |
| 349 |
$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] = $value; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
public static function get_posted_value( $field, &$value, $args ) { |
| 354 |
$field_id = is_object( $field ) ? $field->id : $field; |
| 355 |
|
| 356 |
if ( empty($args['parent_field_id']) ) { |
| 357 |
$value = isset( $_POST['item_meta'][ $field_id ] ) ? $_POST['item_meta'][ $field_id ] : ''; |
| 358 |
} else { |
| 359 |
$value = isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) ? $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] : ''; |
| 360 |
} |
| 361 |
FrmAppHelper::sanitize_value( 'wp_kses_post', $value ); |
| 362 |
$value = stripslashes_deep( $value ); |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Check if field has an "Other" option and if any other values are posted |
| 367 |
* |
| 368 |
* @since 2.0 |
| 369 |
* |
| 370 |
* @param object $field |
| 371 |
* @param string|array $value |
| 372 |
* @param array $args |
| 373 |
*/ |
| 374 |
public static function maybe_set_other_validation( $field, &$value, &$args ) { |
| 375 |
$args['other'] = false; |
| 376 |
if ( ! $value || empty( $value ) || ! FrmAppHelper::pro_is_installed() ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
// Get other value for fields in repeating section |
| 381 |
self::set_other_repeating_vals( $field, $value, $args ); |
| 382 |
|
| 383 |
// Check if there are any posted "Other" values |
| 384 |
if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta']['other'][ $field->id ] ) ) { |
| 385 |
|
| 386 |
// Save original value |
| 387 |
$args['temp_value'] = $value; |
| 388 |
$args['other'] = true; |
| 389 |
$other_vals = stripslashes_deep( $_POST['item_meta']['other'][ $field->id ] ); |
| 390 |
|
| 391 |
// Set the validation value now |
| 392 |
self::set_other_validation_val( $value, $other_vals, $field, $args ); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Sets radio or checkbox value equal to "other" value if it is set - FOR REPEATING SECTIONS |
| 398 |
* |
| 399 |
* @since 2.0 |
| 400 |
* |
| 401 |
* @param object $field |
| 402 |
* @param string|array $value |
| 403 |
* @param array $args |
| 404 |
*/ |
| 405 |
public static function set_other_repeating_vals( $field, &$value, &$args ) { |
| 406 |
if ( ! $args['parent_field_id'] ) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
// Check if there are any other posted "other" values for this field |
| 411 |
if ( FrmField::is_option_true( $field, 'other' ) && isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] ) ) { |
| 412 |
// Save original value |
| 413 |
$args['temp_value'] = $value; |
| 414 |
$args['other'] = true; |
| 415 |
|
| 416 |
$other_vals = $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ]; |
| 417 |
|
| 418 |
// Set the validation value now |
| 419 |
self::set_other_validation_val( $value, $other_vals, $field, $args ); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Modify value used for validation |
| 425 |
* This function essentially removes the "Other" radio or checkbox value from the $value being validated. |
| 426 |
* It also adds any text from the free text fields to the value |
| 427 |
* |
| 428 |
* Needs to accommodate for times when other opt is selected, but no other free text is entered |
| 429 |
* |
| 430 |
* @since 2.0 |
| 431 |
* |
| 432 |
* @param string|array $value |
| 433 |
* @param string|array $other_vals (usually of posted values) |
| 434 |
* @param object $field |
| 435 |
* @param array $args |
| 436 |
*/ |
| 437 |
public static function set_other_validation_val( &$value, $other_vals, $field, &$args ) { |
| 438 |
// Checkboxes and multi-select dropdowns |
| 439 |
if ( is_array( $value ) && $field->type == 'checkbox' ) { |
| 440 |
// Combine "Other" values with checked values. "Other" values will override checked box values. |
| 441 |
$value = array_merge( $value, $other_vals ); |
| 442 |
$value = array_filter( $value ); |
| 443 |
if ( count( $value ) == 0 ) { |
| 444 |
$value = ''; |
| 445 |
} |
| 446 |
} else { |
| 447 |
// Radio and dropdowns |
| 448 |
$other_key = array_filter( array_keys($field->options), 'is_string'); |
| 449 |
$other_key = reset( $other_key ); |
| 450 |
|
| 451 |
// Multi-select dropdown |
| 452 |
if ( is_array( $value ) ) { |
| 453 |
$o_key = array_search( $field->options[ $other_key ], $value ); |
| 454 |
|
| 455 |
if ( $o_key !== false ) { |
| 456 |
// Modify the original value so other key will be preserved |
| 457 |
$value[ $other_key ] = $value[ $o_key ]; |
| 458 |
|
| 459 |
// By default, the array keys will be numeric for multi-select dropdowns |
| 460 |
// If going backwards and forwards between pages, the array key will match the other key |
| 461 |
if ( $o_key != $other_key ) { |
| 462 |
unset( $value[ $o_key ] ); |
| 463 |
} |
| 464 |
|
| 465 |
$args['temp_value'] = $value; |
| 466 |
$value[ $other_key ] = reset( $other_vals ); |
| 467 |
} |
| 468 |
} else if ( $field->options[ $other_key ] == $value ) { |
| 469 |
$value = $other_vals; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
public static function enqueue_scripts( $params ) { |
| 475 |
_deprecated_function( __FUNCTION__, '2.0.9', 'FrmFormsController::enqueue_scripts' ); |
| 476 |
FrmFormsController::enqueue_scripts( $params ); |
| 477 |
} |
| 478 |
|
| 479 |
// Add submitted values to a string for spam checking |
| 480 |
public static function entry_array_to_string( $values ) { |
| 481 |
$content = ''; |
| 482 |
foreach ( $values['item_meta'] as $val ) { |
| 483 |
if ( $content != '' ) { |
| 484 |
$content .= "\n\n"; |
| 485 |
} |
| 486 |
|
| 487 |
if ( is_array($val) ) { |
| 488 |
$val = FrmAppHelper::array_flatten( $val ); |
| 489 |
$val = implode( ', ', $val ); |
| 490 |
} |
| 491 |
|
| 492 |
$content .= $val; |
| 493 |
} |
| 494 |
|
| 495 |
return $content; |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Get the browser from the user agent |
| 500 |
* |
| 501 |
* @since 2.04 |
| 502 |
* |
| 503 |
* @param string $u_agent |
| 504 |
* |
| 505 |
* @return string |
| 506 |
*/ |
| 507 |
public static function get_browser( $u_agent ) { |
| 508 |
$bname = __( 'Unknown', 'formidable' ); |
| 509 |
$platform = __( 'Unknown', 'formidable' ); |
| 510 |
$ub = ''; |
| 511 |
|
| 512 |
// Get the operating system |
| 513 |
if ( preg_match( '/windows|win32/i', $u_agent ) ) { |
| 514 |
$platform = 'Windows'; |
| 515 |
} else if ( preg_match( '/android/i', $u_agent ) ) { |
| 516 |
$platform = 'Android'; |
| 517 |
} else if ( preg_match( '/linux/i', $u_agent ) ) { |
| 518 |
$platform = 'Linux'; |
| 519 |
} else if ( preg_match( '/macintosh|mac os x/i', $u_agent ) ) { |
| 520 |
$platform = 'OS X'; |
| 521 |
} |
| 522 |
|
| 523 |
$agent_options = array( |
| 524 |
'Chrome' => 'Google Chrome', |
| 525 |
'Safari' => 'Apple Safari', |
| 526 |
'Opera' => 'Opera', |
| 527 |
'Netscape' => 'Netscape', |
| 528 |
'Firefox' => 'Mozilla Firefox', |
| 529 |
); |
| 530 |
|
| 531 |
// Next get the name of the useragent yes seperately and for good reason |
| 532 |
if ( strpos( $u_agent, 'MSIE' ) !== false && strpos( $u_agent, 'Opera' ) === false ) { |
| 533 |
$bname = 'Internet Explorer'; |
| 534 |
$ub = 'MSIE'; |
| 535 |
} else { |
| 536 |
foreach ( $agent_options as $agent_key => $agent_name ) { |
| 537 |
if ( strpos( $u_agent, $agent_key ) !== false ) { |
| 538 |
$bname = $agent_name; |
| 539 |
$ub = $agent_key; |
| 540 |
break; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
// finally get the correct version number |
| 546 |
$known = array( 'Version', $ub, 'other' ); |
| 547 |
$pattern = '#(?<browser>' . join( '|', $known ) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; |
| 548 |
preg_match_all( $pattern, $u_agent, $matches ); // get the matching numbers |
| 549 |
|
| 550 |
// see how many we have |
| 551 |
$i = count($matches['browser']); |
| 552 |
|
| 553 |
if ( $i > 1 ) { |
| 554 |
//we will have two since we are not using 'other' argument yet |
| 555 |
//see if version is before or after the name |
| 556 |
if ( strripos( $u_agent, 'Version' ) < strripos( $u_agent, $ub ) ) { |
| 557 |
$version = $matches['version'][0]; |
| 558 |
} else { |
| 559 |
$version = $matches['version'][1]; |
| 560 |
} |
| 561 |
} else if ( $i === 1 ) { |
| 562 |
$version = $matches['version'][0]; |
| 563 |
} else { |
| 564 |
$version = ''; |
| 565 |
} |
| 566 |
|
| 567 |
// check if we have a number |
| 568 |
if ( $version == '' ) { |
| 569 |
$version = '?'; |
| 570 |
} |
| 571 |
|
| 572 |
return $bname . ' ' . $version . ' / ' . $platform; |
| 573 |
} |
| 574 |
|
| 575 |
} |
| 576 |
|