From 6aaf72064ebc7ac1c00bee9c605dfc4a13d8610d Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Mon, 18 Dec 2017 10:36:42 +0200 Subject: [PATCH 1/4] - fixed roles options order in dropdown and radio fields ( fix by @LuigiPulcini ); --- includes/core/class-fields.php | 5437 ++++++++++++++++---------------- 1 file changed, 2719 insertions(+), 2718 deletions(-) diff --git a/includes/core/class-fields.php b/includes/core/class-fields.php index e79a724c..ba3780bb 100644 --- a/includes/core/class-fields.php +++ b/includes/core/class-fields.php @@ -1,1531 +1,1352 @@ editing = false; - $this->viewing = false; - $this->timestamp = current_time( 'timestamp' ); + $this->editing = false; + $this->viewing = false; + $this->timestamp = current_time( 'timestamp' ); - } + } - /** - * Standard checkbox field - * - * @param integer $id - * @param string $title - */ - function checkbox( $id, $title ) { - ?> + /** + * Standard checkbox field + * + * @param integer $id + * @param string $title + */ + function checkbox( $id, $title ) { + ?> -
-
- -
+
+
+
+
- builtin()->all_user_fields; + foreach ($fields as $field => $args) { + if (isset( $args['advanced'] ) && $args['advanced'] == 'social') { + $social[$field] = $args; + } + } + foreach ($social as $k => $arr) { + if (um_profile( $k )) { ?> + + + + '; + + $fields = UM()->builtin()->get_specific_fields( $field ); + + $output = null; + + foreach ($fields as $key => $data) { + $output .= UM()->fields()->edit_field( $key, $data ); } + echo $output; - /** - * Shows social links - */ - function show_social_urls() { - $fields = UM()->builtin()->all_user_fields; - foreach ($fields as $field => $args) { - if (isset( $args['advanced'] ) && $args['advanced'] == 'social') { - $social[$field] = $args; - } - } - foreach ($social as $k => $arr) { - if (um_profile( $k )) { ?> - - - - '; + } + + /** + * Get hidden field + * + * @param string $key + * @param string $value + * + * @return string + */ + function disabled_hidden_field( $key, $value ) { + + return ''; + } + + + /** + * Updates a field globally + * + * @param integer $id + * @param array $args + */ + function globally_update_field( $id, $args ) { + $fields = UM()->builtin()->saved_fields; + + $fields[$id] = $args; + + unset( $fields[$id]['in_row'] ); + unset( $fields[$id]['in_sub_row'] ); + unset( $fields[$id]['in_column'] ); + unset( $fields[$id]['in_group'] ); + unset( $fields[$id]['position'] ); + + update_option( 'um_fields', $fields ); + } + + + /** + * Updates a field in form only + * + * @param integer $id + * @param array $args + * @param integer $form_id + */ + function update_field( $id, $args, $form_id ) { + $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); + + if ($args['type'] == 'row') { + if (isset( $fields[$id] )) { + $old_args = $fields[$id]; + foreach ($old_args as $k => $v) { + if (!in_array( $k, array( 'sub_rows', 'cols' ) )) { + unset( $old_args[$k] ); + } } + $args = array_merge( $old_args, $args ); } } - - /** - * Hidden field inside a shortcode - * - * @param string $field - */ - function add_hidden_field( $field ) { - echo '
'; - - $fields = UM()->builtin()->get_specific_fields( $field ); - - $output = null; - - foreach ($fields as $key => $data) { - $output .= UM()->fields()->edit_field( $key, $data ); - } - - echo $output; - - echo '
'; + // custom fields support + if (isset( UM()->builtin()->predefined_fields[$id] ) && isset( UM()->builtin()->predefined_fields[$id]['custom'] )) { + $args = array_merge( UM()->builtin()->predefined_fields[$id], $args ); } - /** - * Get hidden field - * - * @param string $key - * @param string $value - * - * @return string - */ - function disabled_hidden_field( $key, $value ) { + $fields[$id] = $args; - return ''; + // for group field only + if ($args['type'] == 'group') { + $fields[$id]['in_group'] = ''; } + UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); + } - /** - * Updates a field globally - * - * @param integer $id - * @param array $args - */ - function globally_update_field( $id, $args ) { - $fields = UM()->builtin()->saved_fields; - $fields[$id] = $args; + /** + * Deletes a field in form only + * + * @param integer $id + * @param integer $form_id + */ + function delete_field_from_form( $id, $form_id ) { + $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); + if (isset( $fields[$id] )) { + unset( $fields[$id] ); + UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); + } + } - unset( $fields[$id]['in_row'] ); - unset( $fields[$id]['in_sub_row'] ); - unset( $fields[$id]['in_column'] ); - unset( $fields[$id]['in_group'] ); - unset( $fields[$id]['position'] ); + /** + * Deletes a field from custom fields + * + * @param integer $id + */ + function delete_field_from_db( $id ) { + $fields = UM()->builtin()->saved_fields; + if (isset( $fields[$id] )) { + unset( $fields[$id] ); update_option( 'um_fields', $fields ); } + } + /** + * Quickly adds a field from custom fields + * + * @param integer $global_id + * @param integer $form_id + * @param array $position + */ + function add_field_from_list( $global_id, $form_id, $position = array() ) { + $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); + $field_scope = UM()->builtin()->saved_fields; - /** - * Updates a field in form only - * - * @param integer $id - * @param array $args - * @param integer $form_id - */ - function update_field( $id, $args, $form_id ) { - $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); + if (!isset( $fields[$global_id] )) { - if ($args['type'] == 'row') { - if (isset( $fields[$id] )) { - $old_args = $fields[$id]; - foreach ($old_args as $k => $v) { - if (!in_array( $k, array( 'sub_rows', 'cols' ) )) { - unset( $old_args[$k] ); - } - } - $args = array_merge( $old_args, $args ); + $count = 1; + if (isset( $fields ) && !empty( $fields )) $count = count( $fields ) + 1; + + $fields[$global_id] = $field_scope[$global_id]; + $fields[$global_id]['position'] = $count; + + // set position + if ($position) { + foreach ($position as $key => $val) { + $fields[$global_id][$key] = $val; } } - // custom fields support - if (isset( UM()->builtin()->predefined_fields[$id] ) && isset( UM()->builtin()->predefined_fields[$id]['custom'] )) { - $args = array_merge( UM()->builtin()->predefined_fields[$id], $args ); - } - - $fields[$id] = $args; - - // for group field only - if ($args['type'] == 'group') { - $fields[$id]['in_group'] = ''; - } - + // add field to form UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); + } + } + /** + * Quickly adds a field from pre-defined fields + * + * @param integer $global_id + * @param integer $form_id + * @param array $position + */ + function add_field_from_predefined( $global_id, $form_id, $position = array() ) { + $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); + $field_scope = UM()->builtin()->predefined_fields; - /** - * Deletes a field in form only - * - * @param integer $id - * @param integer $form_id - */ - function delete_field_from_form( $id, $form_id ) { - $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); - if (isset( $fields[$id] )) { - unset( $fields[$id] ); - UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); - } - } + if (!isset( $fields[$global_id] )) { + $count = 1; + if (isset( $fields ) && !empty( $fields )) $count = count( $fields ) + 1; - /** - * Deletes a field from custom fields - * - * @param integer $id - */ - function delete_field_from_db( $id ) { - $fields = UM()->builtin()->saved_fields; - if (isset( $fields[$id] )) { - unset( $fields[$id] ); - update_option( 'um_fields', $fields ); - } - } + $fields[$global_id] = $field_scope[$global_id]; + $fields[$global_id]['position'] = $count; - /** - * Quickly adds a field from custom fields - * - * @param integer $global_id - * @param integer $form_id - * @param array $position - */ - function add_field_from_list( $global_id, $form_id, $position = array() ) { - $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); - $field_scope = UM()->builtin()->saved_fields; - - if (!isset( $fields[$global_id] )) { - - $count = 1; - if (isset( $fields ) && !empty( $fields )) $count = count( $fields ) + 1; - - $fields[$global_id] = $field_scope[$global_id]; - $fields[$global_id]['position'] = $count; - - // set position - if ($position) { - foreach ($position as $key => $val) { - $fields[$global_id][$key] = $val; - } + // set position + if ($position) { + foreach ($position as $key => $val) { + $fields[$global_id][$key] = $val; } - - // add field to form - UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); - } - } - - /** - * Quickly adds a field from pre-defined fields - * - * @param integer $global_id - * @param integer $form_id - * @param array $position - */ - function add_field_from_predefined( $global_id, $form_id, $position = array() ) { - $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); - $field_scope = UM()->builtin()->predefined_fields; - - if (!isset( $fields[$global_id] )) { - - $count = 1; - if (isset( $fields ) && !empty( $fields )) $count = count( $fields ) + 1; - - $fields[$global_id] = $field_scope[$global_id]; - $fields[$global_id]['position'] = $count; - - // set position - if ($position) { - foreach ($position as $key => $val) { - $fields[$global_id][$key] = $val; - } - } - - // add field to form - UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); - - // add field to db - //$this->globally_update_field( $global_id, $fields[$global_id] ); - - } - } - - /** - * Duplicates a frield by meta key - * - * @param integer $id - * @param integer $form_id - */ - function duplicate_field( $id, $form_id ) { - $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); - $all_fields = UM()->builtin()->saved_fields; - - $inc = count( $fields ) + 1; - - $duplicate = $fields[$id]; - - $new_metakey = $id . "_" . $inc; - $new_title = $fields[$id]['title'] . " #" . $inc; - $new_position = $inc; - - $duplicate['title'] = $new_title; - $duplicate['metakey'] = $new_metakey; - $duplicate['position'] = $new_position; - - $fields[$new_metakey] = $duplicate; - $all_fields[$new_metakey] = $duplicate; - - // not global attributes - unset( $all_fields[$new_metakey]['in_row'] ); - unset( $all_fields[$new_metakey]['in_sub_row'] ); - unset( $all_fields[$new_metakey]['in_column'] ); - unset( $all_fields[$new_metakey]['in_group'] ); - unset( $all_fields[$new_metakey]['position'] ); + // add field to form UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); - update_option( 'um_fields', $all_fields ); + + // add field to db + //$this->globally_update_field( $global_id, $fields[$global_id] ); } + } - /** - * Print field error - * - * @param string $text - * @param bool $force_show - * - * @return string - */ - function field_error( $text, $force_show = false ) { - if ($force_show) { - $output = '
' . $text . '
'; + /** + * Duplicates a frield by meta key + * + * @param integer $id + * @param integer $form_id + */ + function duplicate_field( $id, $form_id ) { + $fields = UM()->query()->get_attr( 'custom_fields', $form_id ); + $all_fields = UM()->builtin()->saved_fields; - return $output; - } - if (isset( $this->set_id ) && UM()->form()->processing == $this->set_id) { - $output = '
' . $text . '
'; - } else { - $output = ''; - } + $inc = count( $fields ) + 1; - if (!UM()->form()->processing) { - $output = '
' . $text . '
'; - } + $duplicate = $fields[$id]; + + $new_metakey = $id . "_" . $inc; + $new_title = $fields[$id]['title'] . " #" . $inc; + $new_position = $inc; + + $duplicate['title'] = $new_title; + $duplicate['metakey'] = $new_metakey; + $duplicate['position'] = $new_position; + + $fields[$new_metakey] = $duplicate; + $all_fields[$new_metakey] = $duplicate; + + // not global attributes + unset( $all_fields[$new_metakey]['in_row'] ); + unset( $all_fields[$new_metakey]['in_sub_row'] ); + unset( $all_fields[$new_metakey]['in_column'] ); + unset( $all_fields[$new_metakey]['in_group'] ); + unset( $all_fields[$new_metakey]['position'] ); + + UM()->query()->update_attr( 'custom_fields', $form_id, $fields ); + update_option( 'um_fields', $all_fields ); + + } + + /** + * Print field error + * + * @param string $text + * @param bool $force_show + * + * @return string + */ + function field_error( $text, $force_show = false ) { + if ($force_show) { + $output = '
' . $text . '
'; return $output; } - - /** - * Checks if field has a server-side error - * - * @param string $key - * - * @return boolean - */ - function is_error( $key ) { - return UM()->form()->has_error( $key ); + if (isset( $this->set_id ) && UM()->form()->processing == $this->set_id) { + $output = '
' . $text . '
'; + } else { + $output = ''; } - /** - * Returns field error - * - * @param string $key - * - * @return string - */ - function show_error( $key ) { - return UM()->form()->errors[$key]; + if (!UM()->form()->processing) { + $output = '
' . $text . '
'; } - /** - * Display field label - * - * @param string $label - * @param string $key - * @param data $data - * - * @return string - */ - function field_label( $label, $key, $data ) { - $output = null; - $output .= '
'; + return $output; + } - if (isset( $data['icon'] ) && $data['icon'] != '' && isset( $this->field_icons ) && $this->field_icons != 'off' && ( $this->field_icons == 'label' || $this->viewing == true )) { - $output .= '
'; + /** + * Checks if field has a server-side error + * + * @param string $key + * + * @return boolean + */ + function is_error( $key ) { + return UM()->form()->has_error( $key ); + } + + /** + * Returns field error + * + * @param string $key + * + * @return string + */ + function show_error( $key ) { + return UM()->form()->errors[$key]; + } + + /** + * Display field label + * + * @param string $label + * @param string $key + * @param data $data + * + * @return string + */ + function field_label( $label, $key, $data ) { + $output = null; + $output .= '
'; + + if (isset( $data['icon'] ) && $data['icon'] != '' && isset( $this->field_icons ) && $this->field_icons != 'off' && ( $this->field_icons == 'label' || $this->viewing == true )) { + $output .= '
'; + } + + if ($this->viewing == true) { + $label = apply_filters( "um_view_label_{$key}", $label ); + } else { + $label = apply_filters( "um_edit_label_{$key}", $label ); + $label = apply_filters( "um_edit_label_all_fields", $label, $data ); + } + + $output .= ''; + + if (isset( $data['help'] ) && !empty( $data['help'] ) && $this->viewing == false && !strstr( $key, 'confirm_user_pass' )) { + + if (!UM()->mobile()->isMobile()) { + if (!isset( $this->disable_tooltips )) { + $output .= ''; + } } - if ($this->viewing == true) { - $label = apply_filters( "um_view_label_{$key}", $label ); + if (UM()->mobile()->isMobile() || isset( $this->disable_tooltips )) { + $output .= '' . __( $data['help'], UM_TEXTDOMAIN ) . ''; + } + + } + + $output .= '
'; + + return $output; + } + + + /** + * Output field classes + * + * @param string $key + * @param array $data + * @param string $add + * + * @return string + */ + function get_class( $key, $data, $add = null ) { + $classes = null; + + $classes .= 'um-form-field '; + + if ($this->is_error( $key )) { + $classes .= 'um-error '; + } else { + $classes .= 'valid '; + } + + if (!isset( $data['required'] )) { + $classes .= 'not-required '; + } + + if ($data['type'] == 'date') { + $classes .= 'um-datepicker '; + } + + if ($data['type'] == 'time') { + $classes .= 'um-timepicker '; + } + + if (isset( $data['icon'] ) && $data['icon'] && isset( $this->field_icons ) && $this->field_icons == 'field') { + $classes .= 'um-iconed '; + } + + if ($add) { + $classes .= $add . ' '; + } + + return $classes; + } + + + /** + * Gets field value + * + * @param string $key + * @param boolean $default + * @param array $data + * + * @return mixed + */ + function field_value( $key, $default = false, $data = null ) { + if (isset( $_SESSION ) && isset( $_SESSION['um_social_profile'][$key] ) && isset( $this->set_mode ) && $this->set_mode == 'register') + return $_SESSION['um_social_profile'][$key]; + + $type = ( isset( $data['type'] ) ) ? $data['type'] : ''; + + // preview in backend + if (isset( UM()->user()->preview ) && UM()->user()->preview) { + $submitted = um_user( 'submitted' ); + if (isset( $submitted[$key] ) && !empty( $submitted[$key] )) { + return $submitted[$key]; } else { - $label = apply_filters( "um_edit_label_{$key}", $label ); - $label = apply_filters( "um_edit_label_all_fields", $label, $data ); + return 'Undefined'; } - - $output .= ''; - - if (isset( $data['help'] ) && !empty( $data['help'] ) && $this->viewing == false && !strstr( $key, 'confirm_user_pass' )) { - - if (!UM()->mobile()->isMobile()) { - if (!isset( $this->disable_tooltips )) { - $output .= ''; - } - } - - if (UM()->mobile()->isMobile() || isset( $this->disable_tooltips )) { - $output .= '' . __( $data['help'], UM_TEXTDOMAIN ) . ''; - } - - } - - $output .= '
'; - - return $output; } + // normal state + if (isset( UM()->form()->post_form[$key] )) { - /** - * Output field classes - * - * @param string $key - * @param array $data - * @param string $add - * - * @return string - */ - function get_class( $key, $data, $add = null ) { - $classes = null; + if (strstr( $key, 'user_pass' ) && $this->set_mode != 'password') return ''; - $classes .= 'um-form-field '; + return stripslashes_deep( UM()->form()->post_form[$key] ); - if ($this->is_error( $key )) { - $classes .= 'um-error '; - } else { - $classes .= 'valid '; - } + } else if (um_user( $key ) && $this->editing == true) { - if (!isset( $data['required'] )) { - $classes .= 'not-required '; - } + if (strstr( $key, 'user_pass' )) return ''; - if ($data['type'] == 'date') { - $classes .= 'um-datepicker '; - } - - if ($data['type'] == 'time') { - $classes .= 'um-timepicker '; - } - - if (isset( $data['icon'] ) && $data['icon'] && isset( $this->field_icons ) && $this->field_icons == 'field') { - $classes .= 'um-iconed '; - } - - if ($add) { - $classes .= $add . ' '; - } - - return $classes; - } - - - /** - * Gets field value - * - * @param string $key - * @param boolean $default - * @param array $data - * - * @return mixed - */ - function field_value( $key, $default = false, $data = null ) { - if (isset( $_SESSION ) && isset( $_SESSION['um_social_profile'][$key] ) && isset( $this->set_mode ) && $this->set_mode == 'register') - return $_SESSION['um_social_profile'][$key]; - - $type = ( isset( $data['type'] ) ) ? $data['type'] : ''; - - // preview in backend - if (isset( UM()->user()->preview ) && UM()->user()->preview) { - $submitted = um_user( 'submitted' ); - if (isset( $submitted[$key] ) && !empty( $submitted[$key] )) { - return $submitted[$key]; - } else { - return 'Undefined'; - } - } - - // normal state - if (isset( UM()->form()->post_form[$key] )) { - - if (strstr( $key, 'user_pass' ) && $this->set_mode != 'password') return ''; - - return stripslashes_deep( UM()->form()->post_form[$key] ); - - } else if (um_user( $key ) && $this->editing == true) { - - if (strstr( $key, 'user_pass' )) return ''; - - $value = um_user( $key ); - $value = apply_filters( "um_edit_{$key}_field_value", $value, $key ); - $value = apply_filters( "um_edit_{$type}_field_value", $value, $key ); - - return $value; - - } else if (( um_user( $key ) || isset( $data['show_anyway'] ) ) && $this->viewing == true) { - - $value = um_filtered_value( $key, $data ); - - return $value; - - } else if( isset( UM()->user()->profile[$key] ) ){ - - $value = UM()->user()->profile[$key]; - $value = apply_filters( "um_edit_{$key}_field_value", $value, $key ); - return $value; - - } else if ($default) { - - $default = apply_filters( "um_field_default_value", $default, $data, $type ); - $default = apply_filters( "um_field_{$key}_default_value", $default, $data ); - $default = apply_filters( "um_field_{$type}_default_value", $default, $data ); - - return $default; - - } else if ($this->editing == true) { - - return apply_filters( "um_edit_{$key}_field_value", '', $key ); - - } - - return ''; - } - - - /** - * Checks if an option is selected - * - * @param string $key - * @param string $value - * @param array $data - * - * @return boolean - */ - function is_selected( $key, $value, $data ) { - $key = apply_filters( 'um_is_selected_filter_key', $key ); - - if (isset( UM()->form()->post_form[$key] ) && is_array( UM()->form()->post_form[$key] )) { - - if (in_array( $value, UM()->form()->post_form[$key] )) { - return true; - } - - if (in_array( html_entity_decode( $value ), UM()->form()->post_form[$key] )) { - return true; - } - - } else { - - if (!isset( UM()->form()->post_form[$key] )) { - - $field_value = um_user( $key ); - - if ($key == 'role') { - - $role_keys = get_option( 'um_roles' ); - - if (!empty( $role_keys )) { - if (in_array( $field_value, $role_keys )) { - $field_value = 'um_' . $field_value; - } - } - - } - - $field_value = apply_filters( 'um_is_selected_filter_value', $field_value, $key ); - $data = apply_filters( 'um_is_selected_filter_data', $data, $key, $field_value ); - - if ($field_value && $this->editing == true && is_array( $field_value ) && ( in_array( $value, $field_value ) || in_array( html_entity_decode( $value ), $field_value ) )) { - return true; - } - - if ($field_value && $this->editing == true && !is_array( $field_value ) && $field_value == $value) { - return true; - } - - if ($field_value && $this->editing == true && !is_array( $field_value ) && html_entity_decode( $field_value ) == html_entity_decode( $value )) { - return true; - } - - if (strstr( $data['default'], ', ' )) { - $data['default'] = explode( ', ', $data['default'] ); - } - - if (isset( $data['default'] ) && !is_array( $data['default'] ) && $data['default'] == $value) { - return true; - } - - if (isset( $data['default'] ) && is_array( $data['default'] ) && in_array( $value, $data['default'] )) { - return true; - } - - } else { - - if ($value == UM()->form()->post_form[$key]) { - return true; - } - - - } - - } - - return false; - } - - - /** - * Checks if a radio button is selected - * - * @param string $key - * @param string $value - * @param array $data - * - * @return boolean - */ - function is_radio_checked( $key, $value, $data ) { - if (isset( UM()->form()->post_form[$key] )) { - if (is_array( UM()->form()->post_form[$key] ) && in_array( $value, UM()->form()->post_form[$key] )) { - return true; - } else if ($value == UM()->form()->post_form[$key]) { - return true; - } - } else { - if (um_user( $key ) && $this->editing == true) { - - if (strstr( $key, 'role_' )) { - $key = 'role'; - } - - $um_user_value = um_user( $key ); - - if ($key == 'role') { - $um_user_value = strtolower( $um_user_value ); - - $role_keys = get_option( 'um_roles' ); - - if (!empty( $role_keys )) { - if (in_array( $um_user_value, $role_keys )) { - $um_user_value = 'um_' . $um_user_value; - } - } - } - - if ($um_user_value == $value) { - return true; - } - - if (is_array( $um_user_value ) && in_array( $value, $um_user_value )) { - return true; - } - - if (is_array( $um_user_value )) { - foreach ($um_user_value as $u) { - if ($u == html_entity_decode( $value )) { - return true; - } - } - } - - - } else if (isset( $data['default'] ) && $data['default'] == $value) { - return true; - } - } - - return false; - } - - - /** - * Get field icon - * - * @param string $key - * - * @return string - */ - function get_field_icon( $key ) { - $fields = UM()->builtin()->all_user_fields; - if (isset( $fields[$key]['icon'] )) - return $fields[$key]['icon']; - - return ''; - } - - - /** - * Gets selected option value from a callback function - * - * @param string $value - * @param array $data - * @param string $type - * - * @return json - */ - function get_option_value_from_callback( $value, $data, $type ) { - - if (in_array( $type, array( 'select', 'multiselect' ) ) && isset( $data['custom_dropdown_options_source'] ) && !empty( $data['custom_dropdown_options_source'] )) { - - if ( function_exists( $data['custom_dropdown_options_source'] ) ) { - - $arr_options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); - - - if ($type == 'select') { - if (isset( $arr_options[$value] ) && !empty( $arr_options[$value] )) { - return $arr_options[$value]; - } else if (isset( $data['default'] ) && !empty( $data['default'] ) && empty( $arr_options[$value] )) { - return $arr_options[$data['default']]; - } else { - return ''; - } - } - - if ($type == 'multiselect') { - - if (is_array( $value )) { - $values = $value; - } else { - $values = explode( ', ', $value ); - } - - $arr_paired_options = array(); - - foreach ($values as $option) { - if (isset( $arr_options[$option] )) { - $arr_paired_options[] = $arr_options[$option]; - } - } - - return implode( ', ', $arr_paired_options ); - } - - } - - - } + $value = um_user( $key ); + $value = apply_filters( "um_edit_{$key}_field_value", $value, $key ); + $value = apply_filters( "um_edit_{$type}_field_value", $value, $key ); return $value; + + } else if (( um_user( $key ) || isset( $data['show_anyway'] ) ) && $this->viewing == true) { + + $value = um_filtered_value( $key, $data ); + + return $value; + + } else if( isset( UM()->user()->profile[$key] ) ){ + + $value = UM()->user()->profile[$key]; + $value = apply_filters( "um_edit_{$key}_field_value", $value, $key ); + return $value; + + } else if ($default) { + + $default = apply_filters( "um_field_default_value", $default, $data, $type ); + $default = apply_filters( "um_field_{$key}_default_value", $default, $data ); + $default = apply_filters( "um_field_{$type}_default_value", $default, $data ); + + return $default; + + } else if ($this->editing == true) { + + return apply_filters( "um_edit_{$key}_field_value", '', $key ); + } - /** - * Get select options from a callback function - * - * @param array $data - * @param string $type - * - * @return array $arr_options - */ - function get_options_from_callback( $data, $type ) { + return ''; + } - if ( in_array( $type, array( 'select', 'multiselect' ) ) && isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ) { - $arr_options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); - + /** + * Checks if an option is selected + * + * @param string $key + * @param string $value + * @param array $data + * + * @return boolean + */ + function is_selected( $key, $value, $data ) { + $key = apply_filters( 'um_is_selected_filter_key', $key ); + if (isset( UM()->form()->post_form[$key] ) && is_array( UM()->form()->post_form[$key] )) { + + if (in_array( $value, UM()->form()->post_form[$key] )) { + return true; } - return $arr_options; - } - - /** - * Get field type - * - * @param string $key - * - * @return string - */ - function get_field_type( $key ) { - $fields = UM()->builtin()->all_user_fields; - if (isset( $fields[$key]['type'] )) - return $fields[$key]['type']; - - return ''; - } - - /** - * Get field label - * - * @param string $key - * - * @return string - */ - function get_label( $key ) { - $fields = UM()->builtin()->all_user_fields; - if (isset( $fields[$key]['label'] )) - return $fields[$key]['label']; - if (isset( $fields[$key]['title'] )) - return $fields[$key]['title']; - - return ''; - } - - - /** - * Get field title - * - * @param string $key - * - * @return string - */ - function get_field_title( $key ) { - $fields = UM()->builtin()->all_user_fields; - if (isset( $fields[$key]['title'] )) - return $fields[$key]['title']; - if (isset( $fields[$key]['label'] )) - return $fields[$key]['label']; - - return __( 'Custom Field', 'ultimate-member' ); - } - - /** - * Get form fields - * - * @return array - */ - function get_fields() { - $this->fields = array(); - $this->fields = apply_filters( "um_get_form_fields", $this->fields ); - - return $this->fields; - } - - - /** - * Get specific field - * - * @param string $key - * - * @return array - */ - function get_field( $key ) { - $fields = $this->get_fields(); - - if (isset( $fields ) && is_array( $fields ) && isset( $fields[$key] )) { - $array = $fields[$key]; - } else { - if (!isset( UM()->builtin()->predefined_fields[$key] ) && !isset( UM()->builtin()->all_user_fields[$key] )) { - return ''; - } - $array = ( isset( UM()->builtin()->predefined_fields[$key] ) ) ? UM()->builtin()->predefined_fields[$key] : UM()->builtin()->all_user_fields[$key]; + if (in_array( html_entity_decode( $value ), UM()->form()->post_form[$key] )) { + return true; } - $array['classes'] = null; + } else { - if (!isset( $array['placeholder'] )) $array['placeholder'] = null; - if (!isset( $array['required'] )) $array['required'] = null; - if (!isset( $array['validate'] )) $array['validate'] = null; - if (!isset( $array['default'] )) $array['default'] = null; + if (!isset( UM()->form()->post_form[$key] )) { - if (isset( $array['conditions'] ) && is_array( $array['conditions'] ) && !$this->viewing) { - $array['conditional'] = ''; + $field_value = um_user( $key ); - foreach ($array['conditions'] as $cond_id => $cond) { - $array['conditional'] .= ' data-cond-' . $cond_id . '-action="' . $cond[0] . '" data-cond-' . $cond_id . '-field="' . $cond[1] . '" data-cond-' . $cond_id . '-operator="' . $cond[2] . '" data-cond-' . $cond_id . '-value="' . $cond[3] . '"'; - } + if ($key == 'role') { - $array['classes'] .= ' um-is-conditional'; - - } else { - $array['conditional'] = null; - } - - $array['classes'] .= ' um-field-' . $key; - $array['classes'] .= ' um-field-' . $array['type']; - $array['classes'] .= ' um-field-type_' . $array['type']; - - switch ($array['type']) { - - case 'googlemap': - case 'youtube_video': - case 'vimeo_video': - case 'soundcloud_track': - $array['disabled'] = ''; - $array['input'] = 'text'; - break; - - case 'text': - - $array['disabled'] = ''; - - if ($key == 'user_login' && isset( $this->set_mode ) && $this->set_mode == 'account') { - $array['disabled'] = 'disabled="disabled"'; - } - - $array['input'] = 'text'; - - break; - - case 'password': - - $array['input'] = 'password'; - - break; - - case 'number': - - $array['disabled'] = ''; - - break; - - case 'url': - - $array['input'] = 'text'; - - break; - - case 'date': - - $array['input'] = 'text'; - - if (!isset( $array['format'] )) $array['format'] = 'j M Y'; - - switch ($array['format']) { - case 'j M Y': - $js_format = 'd mmm yyyy'; - break; - case 'j F Y': - $js_format = 'd mmmm yyyy'; - break; - case 'M j Y': - $js_format = 'mmm d yyyy'; - break; - case 'F j Y': - $js_format = 'mmmm d yyyy'; - break; - } - - $array['js_format'] = $js_format; - - if (!isset( $array['range'] )) $array['range'] = 'years'; - if (!isset( $array['years'] )) $array['years'] = 100; - if (!isset( $array['years_x'] )) $array['years_x'] = 'past'; - if (!isset( $array['disabled_weekdays'] )) $array['disabled_weekdays'] = ''; - - if (!empty( $array['disabled_weekdays'] )) { - $array['disabled_weekdays'] = '[' . implode( ',', $array['disabled_weekdays'] ) . ']'; - } - - // When date range is strictly defined - if ($array['range'] == 'date_range') { - - $array['date_min'] = str_replace( '/', ',', $array['range_start'] ); - $array['date_max'] = str_replace( '/', ',', $array['range_end'] ); - - } else { - - if ($array['years_x'] == 'past') { - - $date = new \DateTime( date( 'Y-n-d' ) ); - $past = $date->modify( '-' . $array['years'] . ' years' ); - $past = $date->format( 'Y,n,d' ); - - $array['date_min'] = $past; - $array['date_max'] = date( 'Y,n,d' ); - - } else if ($array['years_x'] == 'future') { - - $date = new \DateTime( date( 'Y-n-d' ) ); - $future = $date->modify( '+' . $array['years'] . ' years' ); - $future = $date->format( 'Y,n,d' ); - - $array['date_min'] = date( 'Y,n,d' ); - $array['date_max'] = $future; - - } else { - - $date = new \DateTime( date( 'Y-n-d' ) ); - $date_f = new \DateTime( date( 'Y-n-d' ) ); - $past = $date->modify( '-' . ( $array['years'] / 2 ) . ' years' ); - $past = $date->format( 'Y,n,d' ); - $future = $date_f->modify( '+' . ( $array['years'] / 2 ) . ' years' ); - $future = $date_f->format( 'Y,n,d' ); - - $array['date_min'] = $past; - $array['date_max'] = $future; + $role_keys = get_option( 'um_roles' ); + if (!empty( $role_keys )) { + if (in_array( $field_value, $role_keys )) { + $field_value = 'um_' . $field_value; } - } - break; + } - case 'time': + $field_value = apply_filters( 'um_is_selected_filter_value', $field_value, $key ); + $data = apply_filters( 'um_is_selected_filter_data', $data, $key, $field_value ); - $array['input'] = 'text'; + if ($field_value && $this->editing == true && is_array( $field_value ) && ( in_array( $value, $field_value ) || in_array( html_entity_decode( $value ), $field_value ) )) { + return true; + } - if (!isset( $array['format'] )) $array['format'] = 'g:i a'; + if ($field_value && $this->editing == true && !is_array( $field_value ) && $field_value == $value) { + return true; + } - switch ($array['format']) { - case 'g:i a': - $js_format = 'h:i a'; - break; - case 'g:i A': - $js_format = 'h:i A'; - break; - case 'H:i': - $js_format = 'HH:i'; - break; - } + if ($field_value && $this->editing == true && !is_array( $field_value ) && html_entity_decode( $field_value ) == html_entity_decode( $value )) { + return true; + } - $array['js_format'] = $js_format; + if (strstr( $data['default'], ', ' )) { + $data['default'] = explode( ', ', $data['default'] ); + } - if (!isset( $array['intervals'] )) $array['intervals'] = 60; + if (isset( $data['default'] ) && !is_array( $data['default'] ) && $data['default'] == $value) { + return true; + } - break; + if (isset( $data['default'] ) && is_array( $data['default'] ) && in_array( $value, $data['default'] )) { + return true; + } - case 'textarea': + } else { - if (!isset( $array['height'] )) $array['height'] = '100px'; + if ($value == UM()->form()->post_form[$key]) { + return true; + } - break; - - case 'rating': - - if (!isset( $array['number'] )) $array['number'] = 5; - - break; - - case 'spacing': - - if (!isset( $array['spacing'] )) { - $array['spacing'] = '20px'; - } - - break; - - case 'divider': - - if (isset( $array['width'] )) { - $array['borderwidth'] = $array['width']; - } else { - $array['borderwidth'] = 4; - } - - if (isset( $array['color'] )) { - $array['bordercolor'] = $array['color']; - } else { - $array['bordercolor'] = '#eee'; - } - - if (isset( $array['style'] )) { - $array['borderstyle'] = $array['style']; - } else { - $array['borderstyle'] = 'solid'; - } - - if (!isset( $array['divider_text'] )) { - $array['divider_text'] = ''; - } - - break; - - case 'image': - - if (!isset( $array['crop'] )) $array['crop'] = 0; - - if ($array['crop'] == 0) { - $array['crop_data'] = 0; - } else if ($array['crop'] == 1) { - $array['crop_data'] = 'square'; - } else if ($array['crop'] == 2) { - $array['crop_data'] = 'cover'; - } else { - $array['crop_data'] = 'user'; - } - - if (!isset( $array['modal_size'] )) $array['modal_size'] = 'normal'; - - if ($array['crop'] > 0) { - $array['crop_class'] = 'crop'; - } else { - $array['crop_class'] = ''; - } - - if (!isset( $array['ratio'] )) $array['ratio'] = 1.0; - - if (!isset( $array['min_width'] )) $array['min_width'] = ''; - if (!isset( $array['min_height'] )) $array['min_height'] = ''; - - if ($array['min_width'] == '' && $array['crop'] == 1) $array['min_width'] = 600; - if ($array['min_height'] == '' && $array['crop'] == 1) $array['min_height'] = 600; - - if ($array['min_width'] == '' && $array['crop'] == 3) $array['min_width'] = 600; - if ($array['min_height'] == '' && $array['crop'] == 3) $array['min_height'] = 600; - - if (!isset( $array['invalid_image'] )) $array['invalid_image'] = __( "Please upload a valid image!", 'ultimate-member' ); - if (!isset( $array['allowed_types'] )) { - $array['allowed_types'] = "gif,jpg,jpeg,png"; - } else { - $array['allowed_types'] = implode( ',', $array['allowed_types'] ); - } - if (!isset( $array['upload_text'] )) $array['upload_text'] = ''; - if (!isset( $array['button_text'] )) $array['button_text'] = __( 'Upload', 'ultimate-member' ); - if (!isset( $array['extension_error'] )) $array['extension_error'] = __( "Sorry this is not a valid image.", 'ultimate-member' ); - if (!isset( $array['max_size_error'] )) $array['max_size_error'] = __( "This image is too large!", 'ultimate-member' ); - if (!isset( $array['min_size_error'] )) $array['min_size_error'] = __( "This image is too small!", 'ultimate-member' ); - if (!isset( $array['max_files_error'] )) $array['max_files_error'] = __( "You can only upload one image", 'ultimate-member' ); - if (!isset( $array['max_size'] )) $array['max_size'] = 999999999; - if (!isset( $array['upload_help_text'] )) $array['upload_help_text'] = ''; - if (!isset( $array['icon'] )) $array['icon'] = ''; - - break; - - case 'file': - - if (!isset( $array['modal_size'] )) $array['modal_size'] = 'normal'; - - if (!isset( $array['allowed_types'] )) { - $array['allowed_types'] = "pdf,txt"; - } else { - $array['allowed_types'] = implode( ',', $array['allowed_types'] ); - } - if (!isset( $array['upload_text'] )) $array['upload_text'] = ''; - if (!isset( $array['button_text'] )) $array['button_text'] = __( 'Upload', 'ultimate-member' ); - if (!isset( $array['extension_error'] )) $array['extension_error'] = "Sorry this is not a valid file."; - if (!isset( $array['max_size_error'] )) $array['max_size_error'] = "This file is too large!"; - if (!isset( $array['min_size_error'] )) $array['min_size_error'] = "This file is too small!"; - if (!isset( $array['max_files_error'] )) $array['max_files_error'] = "You can only upload one file"; - if (!isset( $array['max_size'] )) $array['max_size'] = 999999999; - if (!isset( $array['upload_help_text'] )) $array['upload_help_text'] = ''; - if (!isset( $array['icon'] )) $array['icon'] = ''; - - break; - - case 'select': - - break; - - case 'multiselect': - - break; - - case 'group': - - if (!isset( $array['max_entries'] )) $array['max_entries'] = 0; - - break; } - if (!isset( $array['visibility'] )) $array['visibility'] = 'all'; - - $array = apply_filters( "um_get_field__{$key}", $array ); - - - return $array; } - /** - * Gets a field in 'input mode' - * - * @param string $key - * @param array $data - * @param boolean $rule - * - * @return string - */ - function edit_field( $key, $data, $rule = false ) { - global $_um_profile_id; - $output = null; - $disabled = ''; - if (empty( $_um_profile_id )) { - $_um_profile_id = um_user( 'ID' ); + return false; + } + + + /** + * Checks if a radio button is selected + * + * @param string $key + * @param string $value + * @param array $data + * + * @return boolean + */ + function is_radio_checked( $key, $value, $data ) { + if (isset( UM()->form()->post_form[$key] )) { + if (is_array( UM()->form()->post_form[$key] ) && in_array( $value, UM()->form()->post_form[$key] )) { + return true; + } else if ($value == UM()->form()->post_form[$key]) { + return true; } + } else { + if (um_user( $key ) && $this->editing == true) { - - // get whole field data - if (isset( $data ) && is_array( $data )) { - $data = $this->get_field( $key ); - if (is_array( $data )) { - extract( $data ); + if (strstr( $key, 'role_' )) { + $key = 'role'; } - } - if (!isset( $data['type'] )) return; + $um_user_value = um_user( $key ); - if (isset( $data['in_group'] ) && $data['in_group'] != '' && $rule != 'group') return; + if ($key == 'role') { + $um_user_value = strtolower( $um_user_value ); - if ($visibility == 'view' && $this->set_mode != 'register') return; + $role_keys = get_option( 'um_roles' ); - if (( $visibility == 'view' && $this->set_mode == 'register' ) || - ( isset( $data['editable'] ) && $data['editable'] == 0 && $this->set_mode == 'profile' ) - ) { - - um_fetch_user( get_current_user_id() ); - if (!um_user( 'can_edit_everyone' )) - $disabled = ' disabled="disabled" '; - um_fetch_user( $_um_profile_id ); - if (isset( $data['public'] ) && $data['public'] == '-2' && $data['roles']) { - if (in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] )) { - $disabled = ''; + if (!empty( $role_keys )) { + if (in_array( $um_user_value, $role_keys )) { + $um_user_value = 'um_' . $um_user_value; + } } } + if ($um_user_value == $value) { + return true; + } + + if (is_array( $um_user_value ) && in_array( $value, $um_user_value )) { + return true; + } + + if (is_array( $um_user_value )) { + foreach ($um_user_value as $u) { + if ($u == html_entity_decode( $value )) { + return true; + } + } + } + + + } else if (isset( $data['default'] ) && $data['default'] == $value) { + return true; + } + } + + return false; + } + + + /** + * Get field icon + * + * @param string $key + * + * @return string + */ + function get_field_icon( $key ) { + $fields = UM()->builtin()->all_user_fields; + if (isset( $fields[$key]['icon'] )) + return $fields[$key]['icon']; + + return ''; + } + + + /** + * Gets selected option value from a callback function + * + * @param string $value + * @param array $data + * @param string $type + * + * @return json + */ + function get_option_value_from_callback( $value, $data, $type ) { + + if (in_array( $type, array( 'select', 'multiselect' ) ) && isset( $data['custom_dropdown_options_source'] ) && !empty( $data['custom_dropdown_options_source'] )) { + + if ( function_exists( $data['custom_dropdown_options_source'] ) ) { + + $arr_options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); + + + if ($type == 'select') { + if (isset( $arr_options[$value] ) && !empty( $arr_options[$value] )) { + return $arr_options[$value]; + } else if (isset( $data['default'] ) && !empty( $data['default'] ) && empty( $arr_options[$value] )) { + return $arr_options[$data['default']]; + } else { + return ''; + } + } + + if ($type == 'multiselect') { + + if (is_array( $value )) { + $values = $value; + } else { + $values = explode( ', ', $value ); + } + + $arr_paired_options = array(); + + foreach ($values as $option) { + if (isset( $arr_options[$option] )) { + $arr_paired_options[] = $arr_options[$option]; + } + } + + return implode( ', ', $arr_paired_options ); + } + } - if (!isset( $data['autocomplete'] )) { - $autocomplete = 'off'; + + } + + return $value; + } + + /** + * Get select options from a callback function + * + * @param array $data + * @param string $type + * + * @return array $arr_options + */ + function get_options_from_callback( $data, $type ) { + + if ( in_array( $type, array( 'select', 'multiselect' ) ) && isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ) { + + $arr_options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); + + + } + + return $arr_options; + } + + /** + * Get field type + * + * @param string $key + * + * @return string + */ + function get_field_type( $key ) { + $fields = UM()->builtin()->all_user_fields; + if (isset( $fields[$key]['type'] )) + return $fields[$key]['type']; + + return ''; + } + + /** + * Get field label + * + * @param string $key + * + * @return string + */ + function get_label( $key ) { + $fields = UM()->builtin()->all_user_fields; + if (isset( $fields[$key]['label'] )) + return $fields[$key]['label']; + if (isset( $fields[$key]['title'] )) + return $fields[$key]['title']; + + return ''; + } + + + /** + * Get field title + * + * @param string $key + * + * @return string + */ + function get_field_title( $key ) { + $fields = UM()->builtin()->all_user_fields; + if (isset( $fields[$key]['title'] )) + return $fields[$key]['title']; + if (isset( $fields[$key]['label'] )) + return $fields[$key]['label']; + + return __( 'Custom Field', 'ultimate-member' ); + } + + /** + * Get form fields + * + * @return array + */ + function get_fields() { + $this->fields = array(); + $this->fields = apply_filters( "um_get_form_fields", $this->fields ); + + return $this->fields; + } + + + /** + * Get specific field + * + * @param string $key + * + * @return array + */ + function get_field( $key ) { + $fields = $this->get_fields(); + + if (isset( $fields ) && is_array( $fields ) && isset( $fields[$key] )) { + $array = $fields[$key]; + } else { + if (!isset( UM()->builtin()->predefined_fields[$key] ) && !isset( UM()->builtin()->all_user_fields[$key] )) { + return ''; } + $array = ( isset( UM()->builtin()->predefined_fields[$key] ) ) ? UM()->builtin()->predefined_fields[$key] : UM()->builtin()->all_user_fields[$key]; + } + + $array['classes'] = null; + + if (!isset( $array['placeholder'] )) $array['placeholder'] = null; + if (!isset( $array['required'] )) $array['required'] = null; + if (!isset( $array['validate'] )) $array['validate'] = null; + if (!isset( $array['default'] )) $array['default'] = null; + + if (isset( $array['conditions'] ) && is_array( $array['conditions'] ) && !$this->viewing) { + $array['conditional'] = ''; + + foreach ($array['conditions'] as $cond_id => $cond) { + $array['conditional'] .= ' data-cond-' . $cond_id . '-action="' . $cond[0] . '" data-cond-' . $cond_id . '-field="' . $cond[1] . '" data-cond-' . $cond_id . '-operator="' . $cond[2] . '" data-cond-' . $cond_id . '-value="' . $cond[3] . '"'; + } + + $array['classes'] .= ' um-is-conditional'; + + } else { + $array['conditional'] = null; + } + + $array['classes'] .= ' um-field-' . $key; + $array['classes'] .= ' um-field-' . $array['type']; + $array['classes'] .= ' um-field-type_' . $array['type']; + + switch ($array['type']) { + + case 'googlemap': + case 'youtube_video': + case 'vimeo_video': + case 'soundcloud_track': + $array['disabled'] = ''; + $array['input'] = 'text'; + break; + + case 'text': + + $array['disabled'] = ''; + + if ($key == 'user_login' && isset( $this->set_mode ) && $this->set_mode == 'account') { + $array['disabled'] = 'disabled="disabled"'; + } + + $array['input'] = 'text'; + + break; + + case 'password': + + $array['input'] = 'password'; + + break; + + case 'number': + + $array['disabled'] = ''; + + break; + + case 'url': + + $array['input'] = 'text'; + + break; + + case 'date': + + $array['input'] = 'text'; + + if (!isset( $array['format'] )) $array['format'] = 'j M Y'; + + switch ($array['format']) { + case 'j M Y': + $js_format = 'd mmm yyyy'; + break; + case 'j F Y': + $js_format = 'd mmmm yyyy'; + break; + case 'M j Y': + $js_format = 'mmm d yyyy'; + break; + case 'F j Y': + $js_format = 'mmmm d yyyy'; + break; + } + + $array['js_format'] = $js_format; + + if (!isset( $array['range'] )) $array['range'] = 'years'; + if (!isset( $array['years'] )) $array['years'] = 100; + if (!isset( $array['years_x'] )) $array['years_x'] = 'past'; + if (!isset( $array['disabled_weekdays'] )) $array['disabled_weekdays'] = ''; + + if (!empty( $array['disabled_weekdays'] )) { + $array['disabled_weekdays'] = '[' . implode( ',', $array['disabled_weekdays'] ) . ']'; + } + + // When date range is strictly defined + if ($array['range'] == 'date_range') { + + $array['date_min'] = str_replace( '/', ',', $array['range_start'] ); + $array['date_max'] = str_replace( '/', ',', $array['range_end'] ); + + } else { + + if ($array['years_x'] == 'past') { + + $date = new \DateTime( date( 'Y-n-d' ) ); + $past = $date->modify( '-' . $array['years'] . ' years' ); + $past = $date->format( 'Y,n,d' ); + + $array['date_min'] = $past; + $array['date_max'] = date( 'Y,n,d' ); + + } else if ($array['years_x'] == 'future') { + + $date = new \DateTime( date( 'Y-n-d' ) ); + $future = $date->modify( '+' . $array['years'] . ' years' ); + $future = $date->format( 'Y,n,d' ); + + $array['date_min'] = date( 'Y,n,d' ); + $array['date_max'] = $future; + + } else { + + $date = new \DateTime( date( 'Y-n-d' ) ); + $date_f = new \DateTime( date( 'Y-n-d' ) ); + $past = $date->modify( '-' . ( $array['years'] / 2 ) . ' years' ); + $past = $date->format( 'Y,n,d' ); + $future = $date_f->modify( '+' . ( $array['years'] / 2 ) . ' years' ); + $future = $date_f->format( 'Y,n,d' ); + + $array['date_min'] = $past; + $array['date_max'] = $future; + + } + + } + + break; + + case 'time': + + $array['input'] = 'text'; + + if (!isset( $array['format'] )) $array['format'] = 'g:i a'; + + switch ($array['format']) { + case 'g:i a': + $js_format = 'h:i a'; + break; + case 'g:i A': + $js_format = 'h:i A'; + break; + case 'H:i': + $js_format = 'HH:i'; + break; + } + + $array['js_format'] = $js_format; + + if (!isset( $array['intervals'] )) $array['intervals'] = 60; + + break; + + case 'textarea': + + if (!isset( $array['height'] )) $array['height'] = '100px'; + + break; + + case 'rating': + + if (!isset( $array['number'] )) $array['number'] = 5; + + break; + + case 'spacing': + + if (!isset( $array['spacing'] )) { + $array['spacing'] = '20px'; + } + + break; + + case 'divider': + + if (isset( $array['width'] )) { + $array['borderwidth'] = $array['width']; + } else { + $array['borderwidth'] = 4; + } + + if (isset( $array['color'] )) { + $array['bordercolor'] = $array['color']; + } else { + $array['bordercolor'] = '#eee'; + } + + if (isset( $array['style'] )) { + $array['borderstyle'] = $array['style']; + } else { + $array['borderstyle'] = 'solid'; + } + + if (!isset( $array['divider_text'] )) { + $array['divider_text'] = ''; + } + + break; + + case 'image': + + if (!isset( $array['crop'] )) $array['crop'] = 0; + + if ($array['crop'] == 0) { + $array['crop_data'] = 0; + } else if ($array['crop'] == 1) { + $array['crop_data'] = 'square'; + } else if ($array['crop'] == 2) { + $array['crop_data'] = 'cover'; + } else { + $array['crop_data'] = 'user'; + } + + if (!isset( $array['modal_size'] )) $array['modal_size'] = 'normal'; + + if ($array['crop'] > 0) { + $array['crop_class'] = 'crop'; + } else { + $array['crop_class'] = ''; + } + + if (!isset( $array['ratio'] )) $array['ratio'] = 1.0; + + if (!isset( $array['min_width'] )) $array['min_width'] = ''; + if (!isset( $array['min_height'] )) $array['min_height'] = ''; + + if ($array['min_width'] == '' && $array['crop'] == 1) $array['min_width'] = 600; + if ($array['min_height'] == '' && $array['crop'] == 1) $array['min_height'] = 600; + + if ($array['min_width'] == '' && $array['crop'] == 3) $array['min_width'] = 600; + if ($array['min_height'] == '' && $array['crop'] == 3) $array['min_height'] = 600; + + if (!isset( $array['invalid_image'] )) $array['invalid_image'] = __( "Please upload a valid image!", 'ultimate-member' ); + if (!isset( $array['allowed_types'] )) { + $array['allowed_types'] = "gif,jpg,jpeg,png"; + } else { + $array['allowed_types'] = implode( ',', $array['allowed_types'] ); + } + if (!isset( $array['upload_text'] )) $array['upload_text'] = ''; + if (!isset( $array['button_text'] )) $array['button_text'] = __( 'Upload', 'ultimate-member' ); + if (!isset( $array['extension_error'] )) $array['extension_error'] = __( "Sorry this is not a valid image.", 'ultimate-member' ); + if (!isset( $array['max_size_error'] )) $array['max_size_error'] = __( "This image is too large!", 'ultimate-member' ); + if (!isset( $array['min_size_error'] )) $array['min_size_error'] = __( "This image is too small!", 'ultimate-member' ); + if (!isset( $array['max_files_error'] )) $array['max_files_error'] = __( "You can only upload one image", 'ultimate-member' ); + if (!isset( $array['max_size'] )) $array['max_size'] = 999999999; + if (!isset( $array['upload_help_text'] )) $array['upload_help_text'] = ''; + if (!isset( $array['icon'] )) $array['icon'] = ''; + + break; + + case 'file': + + if (!isset( $array['modal_size'] )) $array['modal_size'] = 'normal'; + + if (!isset( $array['allowed_types'] )) { + $array['allowed_types'] = "pdf,txt"; + } else { + $array['allowed_types'] = implode( ',', $array['allowed_types'] ); + } + if (!isset( $array['upload_text'] )) $array['upload_text'] = ''; + if (!isset( $array['button_text'] )) $array['button_text'] = __( 'Upload', 'ultimate-member' ); + if (!isset( $array['extension_error'] )) $array['extension_error'] = "Sorry this is not a valid file."; + if (!isset( $array['max_size_error'] )) $array['max_size_error'] = "This file is too large!"; + if (!isset( $array['min_size_error'] )) $array['min_size_error'] = "This file is too small!"; + if (!isset( $array['max_files_error'] )) $array['max_files_error'] = "You can only upload one file"; + if (!isset( $array['max_size'] )) $array['max_size'] = 999999999; + if (!isset( $array['upload_help_text'] )) $array['upload_help_text'] = ''; + if (!isset( $array['icon'] )) $array['icon'] = ''; + + break; + + case 'select': + + break; + + case 'multiselect': + + break; + + case 'group': + + if (!isset( $array['max_entries'] )) $array['max_entries'] = 0; + + break; + + } + + if (!isset( $array['visibility'] )) $array['visibility'] = 'all'; + + $array = apply_filters( "um_get_field__{$key}", $array ); + + + return $array; + } + + /** + * Gets a field in 'input mode' + * + * @param string $key + * @param array $data + * @param boolean $rule + * + * @return string + */ + function edit_field( $key, $data, $rule = false ) { + global $_um_profile_id; + $output = null; + $disabled = ''; + if (empty( $_um_profile_id )) { + $_um_profile_id = um_user( 'ID' ); + } + + + // get whole field data + if (isset( $data ) && is_array( $data )) { + $data = $this->get_field( $key ); + if (is_array( $data )) { + extract( $data ); + } + } + + if (!isset( $data['type'] )) return; + + if (isset( $data['in_group'] ) && $data['in_group'] != '' && $rule != 'group') return; + + if ($visibility == 'view' && $this->set_mode != 'register') return; + + if (( $visibility == 'view' && $this->set_mode == 'register' ) || + ( isset( $data['editable'] ) && $data['editable'] == 0 && $this->set_mode == 'profile' ) + ) { + um_fetch_user( get_current_user_id() ); - if (!um_can_view_field( $data )) return; - if (!um_can_edit_field( $data )) return; + if (!um_user( 'can_edit_everyone' )) + $disabled = ' disabled="disabled" '; um_fetch_user( $_um_profile_id ); - - // fields that need to be disabled in edit mode (profile) - $arr_restricted_fields = array( 'user_email', 'username', 'user_login', 'user_password' ); - - if ( UM()->options()->get( 'editable_primary_email_in_profile' ) == 1 ) { - unset( $arr_restricted_fields[0] ); // remove user_email + if (isset( $data['public'] ) && $data['public'] == '-2' && $data['roles']) { + if (in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] )) { + $disabled = ''; + } } - if (in_array( $key, $arr_restricted_fields ) && $this->editing == true && $this->set_mode == 'profile') { + } + + if (!isset( $data['autocomplete'] )) { + $autocomplete = 'off'; + } + um_fetch_user( get_current_user_id() ); + if (!um_can_view_field( $data )) return; + if (!um_can_edit_field( $data )) return; + um_fetch_user( $_um_profile_id ); + + // fields that need to be disabled in edit mode (profile) + $arr_restricted_fields = array( 'user_email', 'username', 'user_login', 'user_password' ); + + if ( UM()->options()->get( 'editable_primary_email_in_profile' ) == 1 ) { + unset( $arr_restricted_fields[0] ); // remove user_email + } + + if (in_array( $key, $arr_restricted_fields ) && $this->editing == true && $this->set_mode == 'profile') { + return; + } + + // forbidden in edit mode? + if (isset( $data['edit_forbidden'] )) return; + + // required option + if (isset( $data['required_opt'] )) { + $opt = $data['required_opt']; + if ( UM()->options()->get( $opt[0] ) != $opt[1]) { return; } + } - // forbidden in edit mode? - if (isset( $data['edit_forbidden'] )) return; - - // required option - if (isset( $data['required_opt'] )) { - $opt = $data['required_opt']; - if ( UM()->options()->get( $opt[0] ) != $opt[1]) { - return; - } + // required user permission + if (isset( $data['required_perm'] )) { + if (!um_user( $data['required_perm'] )) { + return; } + } - // required user permission - if (isset( $data['required_perm'] )) { - if (!um_user( $data['required_perm'] )) { - return; - } + // do not show passwords + if (isset( UM()->user()->preview ) && UM()->user()->preview) { + if ($data['type'] == 'password') { + return; } + } - // do not show passwords - if (isset( UM()->user()->preview ) && UM()->user()->preview) { - if ($data['type'] == 'password') { - return; + $type = apply_filters( "um_hook_for_field_{$type}", $type ); + + /* Begin by field type */ + switch ($type) { + + /* Default: Integration */ + default: + $mode = ( isset( $this->set_mode ) ) ? $this->set_mode : 'no_mode'; + $output .= apply_filters( "um_edit_field_{$mode}_{$type}", $output, $data ); + break; + + /* Other fields */ + case 'googlemap': + case 'youtube_video': + case 'vimeo_video': + case 'soundcloud_track': + + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); } - } - $type = apply_filters( "um_hook_for_field_{$type}", $type ); + $output .= '
'; - /* Begin by field type */ - switch ($type) { + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - /* Default: Integration */ - default: - $mode = ( isset( $this->set_mode ) ) ? $this->set_mode : 'no_mode'; - $output .= apply_filters( "um_edit_field_{$mode}_{$type}", $output, $data ); - break; + $output .= '
'; - /* Other fields */ - case 'googlemap': - case 'youtube_video': - case 'vimeo_video': - case 'soundcloud_track': + } - $output .= '
'; + $field_name = $key . UM()->form()->form_suffix; + $field_value = htmlspecialchars( $this->field_value( $key, $default, $data ) ); - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } + $output .= ' - $output .= '
'; +
'; - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { + if (!empty( $disabled )) { + $output .= $this->disabled_hidden_field( $field_name, $field_value ); + } - $output .= '
'; + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } - } + $output .= '
'; + break; - $field_name = $key . UM()->form()->form_suffix; - $field_value = htmlspecialchars( $this->field_value( $key, $default, $data ) ); + /* Text */ + case 'text': - $output .= ' - -
'; + $output .= '
'; - if (!empty( $disabled )) { - $output .= $this->disabled_hidden_field( $field_name, $field_value ); - } + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } + $output .= '
'; - $output .= '
'; - break; + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - /* Text */ - case 'text': + $output .= '
'; - $output .= '
'; + } - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } + $field_name = $key . UM()->form()->form_suffix; + $field_value = htmlspecialchars( $this->field_value( $key, $default, $data ) ); - $output .= '
'; + $output .= ' - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { +
'; - $output .= '
'; + if (!empty( $disabled )) { + $output .= $this->disabled_hidden_field( $field_name, $field_value ); + } - } + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } - $field_name = $key . UM()->form()->form_suffix; - $field_value = htmlspecialchars( $this->field_value( $key, $default, $data ) ); + $output .= '
'; + break; - $output .= ' - -
'; + /* Number */ + case 'number': - if (!empty( $disabled )) { - $output .= $this->disabled_hidden_field( $field_name, $field_value ); - } + $output .= '
'; - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } - $output .= '
'; - break; + $output .= '
'; - /* Number */ - case 'number': + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - $output .= '
'; + $output .= '
'; - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } + } - $output .= '
'; + $number_limit = ''; - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { + if (isset( $min )) { + $number_limit .= " min=\"{$min}\" "; + } - $output .= '
'; + if (isset( $max )) { + $number_limit .= " max=\"{$max}\" "; + } - } + $output .= ' - $number_limit = ''; +
'; - if (isset( $min )) { - $number_limit .= " min=\"{$min}\" "; - } + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } - if (isset( $max )) { - $number_limit .= " max=\"{$max}\" "; - } + $output .= '
'; + break; - $output .= ' - -
'; + /* Password */ + case 'password': - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } + $original_key = $key; - $output .= '
'; - break; + if ($key == 'single_user_password') { - /* Password */ - case 'password': - - $original_key = $key; - - if ($key == 'single_user_password') { - - $key = $original_key; - - $output .= '
'; - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $output .= '
'; - - if (isset( $icon ) && $icon && $this->field_icons == 'field') { - - $output .= '
'; - - } - - $output .= ' - -
'; - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - - } else { - - if ($this->set_mode == 'account' || um_is_core_page( 'account' )) { - - $key = 'current_' . $original_key; - $output .= '
'; - - if (isset( $data['label'] )) { - $output .= $this->field_label( __( 'Current Password', 'ultimate-member' ), $key, $data ); - } - - $output .= '
'; - - if (isset( $icon ) && $icon && $this->field_icons == 'field') { - - $output .= '
'; - - } - - $output .= ' - -
'; - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - - } - - $key = $original_key; - - $output .= '
'; - - if ($this->set_mode == 'account' && um_is_core_page( 'account' ) || $this->set_mode == 'password' && um_is_core_page( 'password-reset' )) { - - $output .= $this->field_label( __( 'New Password', 'ultimate-member' ), $key, $data ); - - } else if (isset( $data['label'] )) { - - $output .= $this->field_label( $label, $key, $data ); - - } - - $output .= '
'; - - if (isset( $icon ) && $icon && $this->field_icons == 'field') { - - $output .= '
'; - - } - - $output .= ' - -
'; - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - - if ($this->set_mode != 'login' && isset( $data['force_confirm_pass'] ) && $data['force_confirm_pass'] == 1) { - - $key = 'confirm_' . $original_key; - $output .= '
'; - - if (isset( $data['label'] )) { - $output .= $this->field_label( sprintf( __( 'Confirm %s', 'ultimate-member' ), $data['label'] ), $key, $data ); - } - - $output .= '
'; - - if (isset( $icon ) && $icon && $this->field_icons == 'field') { - - $output .= '
'; - - } - - $output .= ' - -
'; - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - - } - - } - - break; - - /* URL */ - case 'url': - - $output .= '
'; - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $output .= '
'; - - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - - $output .= '
'; - - } - - $output .= ' - -
'; - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - break; - - /* Date */ - case 'date': - - $output .= '
'; - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $output .= '
'; - - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - - $output .= '
'; - - } - - $output .= ' - -
'; - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - break; - - /* Time */ - case 'time': + $key = $original_key; $output .= '
'; @@ -1541,8 +1362,8 @@ } - $output .= ' - + $output .= ' +
'; if ($this->is_error( $key )) { @@ -1550,645 +1371,741 @@ } $output .= '
'; - break; - /* Row */ - case 'row': - $output .= ''; - break; + } else { + + if ($this->set_mode == 'account' || um_is_core_page( 'account' )) { + + $key = 'current_' . $original_key; + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( __( 'Current Password', 'ultimate-member' ), $key, $data ); + } + + $output .= '
'; + + if (isset( $icon ) && $icon && $this->field_icons == 'field') { + + $output .= '
'; + + } + + $output .= ' + +
'; + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + + } + + $key = $original_key; - /* Textarea */ - case 'textarea': $output .= '
'; - if (isset( $data['label'] )) { + if ($this->set_mode == 'account' && um_is_core_page( 'account' ) || $this->set_mode == 'password' && um_is_core_page( 'password-reset' )) { + + $output .= $this->field_label( __( 'New Password', 'ultimate-member' ), $key, $data ); + + } else if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } $output .= '
'; - $field_name = $key; - $field_value = $this->field_value( $key, $default, $data ); - if (isset( $data['html'] ) && $data['html'] != 0 && $key != "description") { + if (isset( $icon ) && $icon && $this->field_icons == 'field') { + $output .= '
'; - $textarea_settings = array( - 'media_buttons' => false, - 'wpautop' => false, - 'editor_class' => $this->get_class( $key, $data ), - 'editor_height' => $height, - 'tinymce' => array( - 'toolbar1' => 'formatselect,bullist,numlist,bold,italic,underline,forecolor,blockquote,hr,removeformat,link,unlink,undo,redo', - 'toolbar2' => '', - ) - ); + } - if (!empty( $disabled )) { - $textarea_settings['tinymce']['readonly'] = true; + $output .= ' + +
'; + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + + if ($this->set_mode != 'login' && isset( $data['force_confirm_pass'] ) && $data['force_confirm_pass'] == 1) { + + $key = 'confirm_' . $original_key; + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( sprintf( __( 'Confirm %s', 'ultimate-member' ), $data['label'] ), $key, $data ); } - $textarea_settings = apply_filters( 'um_form_fields_textarea_settings', $textarea_settings ); + $output .= '
'; - // turn on the output buffer - ob_start(); + if (isset( $icon ) && $icon && $this->field_icons == 'field') { - // echo the editor to the buffer - wp_editor( $field_value, $key, $textarea_settings ); + $output .= '
'; - // add the contents of the buffer to the output variable - $output .= ob_get_clean(); + } - } else $output .= ''; + $output .= ' - $output .= ' -
'; +
'; + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= ''; + + } + + } + + break; + + /* URL */ + case 'url': + + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { + + $output .= '
'; + + } + + $output .= ' + +
'; + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + break; + + /* Date */ + case 'date': + + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { + + $output .= '
'; + + } + + $output .= ' + +
'; + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + break; + + /* Time */ + case 'time': + + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + + if (isset( $icon ) && $icon && $this->field_icons == 'field') { + + $output .= '
'; + + } + + $output .= ' + +
'; + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + break; + + /* Row */ + case 'row': + $output .= ''; + break; + + /* Textarea */ + case 'textarea': + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + $field_name = $key; + $field_value = $this->field_value( $key, $default, $data ); + + if (isset( $data['html'] ) && $data['html'] != 0 && $key != "description") { + + + $textarea_settings = array( + 'media_buttons' => false, + 'wpautop' => false, + 'editor_class' => $this->get_class( $key, $data ), + 'editor_height' => $height, + 'tinymce' => array( + 'toolbar1' => 'formatselect,bullist,numlist,bold,italic,underline,forecolor,blockquote,hr,removeformat,link,unlink,undo,redo', + 'toolbar2' => '', + ) + ); if (!empty( $disabled )) { - $output .= $this->disabled_hidden_field( $field_name, $field_value ); + $textarea_settings['tinymce']['readonly'] = true; } - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } + $textarea_settings = apply_filters( 'um_form_fields_textarea_settings', $textarea_settings ); - $output .= '
'; - break; + // turn on the output buffer + ob_start(); - /* Rating */ - case 'rating': - $output .= '
'; + // echo the editor to the buffer + wp_editor( $field_value, $key, $textarea_settings ); - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } + // add the contents of the buffer to the output variable + $output .= ob_get_clean(); - $output .= '
'; + } else $output .= ''; - $output .= '
'; + $output .= ' +
'; - $output .= '
'; + if (!empty( $disabled )) { + $output .= $this->disabled_hidden_field( $field_name, $field_value ); + } - $output .= '
'; + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } - break; + $output .= ''; + break; - /* Gap/Space */ - case 'spacing': - $output .= '
'; - break; + /* Rating */ + case 'rating': + $output .= '
'; - /* A line divider */ - case 'divider': - $output .= '
'; - if ($divider_text) { - $output .= '
' . $divider_text . '
'; - } - $output .= '
'; - break; + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } - /* Single Image Upload */ - case 'image': - $output .= '
'; + $output .= '
'; - if (in_array( $key, array( 'profile_photo', 'cover_photo' ) )) { - $field_value = ''; - } else { - $field_value = $this->field_value( $key, $default, $data ); - } + $output .= '
'; - $output .= ''; + $output .= '
'; - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } + $output .= '
'; - $modal_label = ( isset( $data['label'] ) ) ? $data['label'] : __( 'Upload Photo', 'ultimate-member' ); + break; - $output .= '
'; + /* Gap/Space */ + case 'spacing': + $output .= '
'; + break; - if ($this->field_value( $key, $default, $data )) { + /* A line divider */ + case 'divider': + $output .= '
'; + if ($divider_text) { + $output .= '
' . $divider_text . '
'; + } + $output .= '
'; + break; - if (!in_array( $key, array( 'profile_photo', 'cover_photo' ) )) { - if (isset( $this->set_mode ) && $this->set_mode == 'register') { - $imgValue = $this->field_value( $key, $default, $data ); - } else { - $imgValue = um_user_uploads_uri() . $this->field_value( $key, $default, $data ); - } - $img = ''; - } else { - $img = ''; - } - - $output .= '
- ' . $img . ' -
' . __( 'Change photo', 'ultimate-member' ) . ''; - - } else { - - $output .= '
- - -
' . $button_text . ''; - - } - - $output .= '
'; - - /* modal hidden */ - $output .= '
'; - - $output .= '
' . $modal_label . '
'; - - $output .= '
'; - - if (isset( $this->set_id )) { - $set_id = $this->set_id; - $set_mode = $this->set_mode; - } else { - $set_id = 0; - $set_mode = ''; - } - - $nonce = wp_create_nonce( 'um_upload_nonce-' . $this->timestamp ); - - $output .= '
'; - $output .= '
' . $button_text . '
'; - - $output .= ''; - - $output .= '
'; - - $output .= '
'; - - /* end */ - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - - break; - - /* Single File Upload */ - case 'file': - $output .= '
'; - - $output .= ''; - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $modal_label = ( isset( $data['label'] ) ) ? $data['label'] : __( 'Upload Photo', 'ultimate-member' ); - - $output .= '
'; - - if ($this->field_value( $key, $default, $data )) { - - $extension = pathinfo( $this->field_value( $key, $default, $data ), PATHINFO_EXTENSION ); - - $output .= '' . __( 'Change file', 'ultimate-member' ) . ''; - - } else { - - $output .= '
-
' . $button_text . ''; - - } - - $output .= '
'; - - /* modal hidden */ - $output .= '
'; - - $output .= '
' . $modal_label . '
'; - - $output .= '
'; - - if (isset( $this->set_id )) { - $set_id = $this->set_id; - $set_mode = $this->set_mode; - } else { - $set_id = 0; - $set_mode = ''; - } - - $output .= '
- - -
'; - - $nonce = wp_create_nonce( 'um_upload_nonce-' . $this->timestamp ); - - $output .= '
' . $button_text . '
'; - - $output .= ''; - - $output .= '
'; - - $output .= '
'; - - /* end */ - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - - break; - - /* Select dropdown */ - case 'select': - - $form_key = str_replace( 'role_select', 'role', $key ); - - $output .= '
'; - - - if (isset( $data['allowclear'] ) && $data['allowclear'] == 0) { - $class = 'um-s2'; - } else { - $class = 'um-s1'; - } - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $output .= '
'; - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - $output .= '
'; - } - - $has_parent_option = false; - $disabled_by_parent_option = ''; - $atts_ajax = ''; - $select_original_option_value = ''; - - if (isset( $data['parent_dropdown_relationship'] ) && !empty( $data['parent_dropdown_relationship'] ) && !UM()->user()->preview) { - - $disabled_by_parent_option = 'disabled = disabled'; - - $has_parent_option = true; - - $parent_dropdown_relationship = apply_filters( "um_custom_dropdown_options_parent__{$form_key}", $data['parent_dropdown_relationship'], $data ); - $atts_ajax .= " data-um-parent='{$parent_dropdown_relationship}' "; - - if (isset( $data['custom_dropdown_options_source'] ) && !empty( $data['custom_dropdown_options_source'] ) && - $has_parent_option && function_exists( $data['custom_dropdown_options_source'] ) && - um_user( $data['parent_dropdown_relationship'] ) - ) { - $options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); - $disabled_by_parent_option = ''; - if (um_user( $form_key )) { - $select_original_option_value = " data-um-original-value='" . um_user( $form_key ) . "' "; - } - } - - } - - if (!empty( $data['custom_dropdown_options_source'] )) { - - $ajax_source = apply_filters( "um_custom_dropdown_options_source__{$form_key}", $data['custom_dropdown_options_source'], $data ); - $atts_ajax .= " data-um-ajax-source='{$ajax_source}' "; - - $ajax_source_url = apply_filters( "um_custom_dropdown_options_source_url__{$form_key}", admin_url( 'admin-ajax.php' ), $data ); - $atts_ajax .= " data-um-ajax-url='{$ajax_source_url}' "; - - - } - - $output .= ''; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $modal_label = ( isset( $data['label'] ) ) ? $data['label'] : __( 'Upload Photo', 'ultimate-member' ); + + $output .= '
'; + + if ($this->field_value( $key, $default, $data )) { + + if (!in_array( $key, array( 'profile_photo', 'cover_photo' ) )) { + if (isset( $this->set_mode ) && $this->set_mode == 'register') { + $imgValue = $this->field_value( $key, $default, $data ); + } else { + $imgValue = um_user_uploads_uri() . $this->field_value( $key, $default, $data ); + } + $img = ''; + } else { + $img = ''; } - // add options - if ( ! empty( $options ) ) { - foreach ( $options as $k => $v ) { + $output .= '
+ ' . $img . ' +
' . __( 'Change photo', 'ultimate-member' ) . ''; - $v = rtrim( $v ); + } else { - $option_value = $v; - $um_field_checkbox_item_title = $v; + $output .= '
+ + +
' . $button_text . ''; + + } + + $output .= '
'; + + /* modal hidden */ + $output .= '
'; + + $output .= '
' . $modal_label . '
'; + + $output .= '
'; + + if (isset( $this->set_id )) { + $set_id = $this->set_id; + $set_mode = $this->set_mode; + } else { + $set_id = 0; + $set_mode = ''; + } + + $nonce = wp_create_nonce( 'um_upload_nonce-' . $this->timestamp ); + + $output .= '
'; + $output .= '
' . $button_text . '
'; + + $output .= ''; + + $output .= '
'; + + $output .= '
'; + + /* end */ + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + + break; + + /* Single File Upload */ + case 'file': + $output .= '
'; + + $output .= ''; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $modal_label = ( isset( $data['label'] ) ) ? $data['label'] : __( 'Upload Photo', 'ultimate-member' ); + + $output .= '
'; + + if ($this->field_value( $key, $default, $data )) { + + $extension = pathinfo( $this->field_value( $key, $default, $data ), PATHINFO_EXTENSION ); + + $output .= '' . __( 'Change file', 'ultimate-member' ) . ''; + + } else { + + $output .= '
+
' . $button_text . ''; + + } + + $output .= '
'; + + /* modal hidden */ + $output .= '
'; + + $output .= '
' . $modal_label . '
'; + + $output .= '
'; + + if (isset( $this->set_id )) { + $set_id = $this->set_id; + $set_mode = $this->set_mode; + } else { + $set_id = 0; + $set_mode = ''; + } + + $output .= '
+ + +
'; + + $nonce = wp_create_nonce( 'um_upload_nonce-' . $this->timestamp ); + + $output .= '
' . $button_text . '
'; + + $output .= ''; + + $output .= '
'; + + $output .= '
'; + + /* end */ + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + + break; + + /* Select dropdown */ + case 'select': + + $form_key = str_replace( 'role_select', 'role', $key ); + + $output .= '
'; - if (!is_numeric( $k ) && in_array( $form_key, array( 'role' ) )) { - $option_value = $k; - $um_field_checkbox_item_title = $v; - } + if (isset( $data['allowclear'] ) && $data['allowclear'] == 0) { + $class = 'um-s2'; + } else { + $class = 'um-s1'; + } - if (isset( $options_pair )) { - $option_value = $k; - $um_field_checkbox_item_title = $v; - } + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } - $option_value = apply_filters( 'um_field_non_utf8_value', $option_value ); + $output .= '
'; + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { + $output .= '
'; + } - $output .= ''; + $disabled_by_parent_option = 'disabled = disabled'; + $has_parent_option = true; + $parent_dropdown_relationship = apply_filters( "um_custom_dropdown_options_parent__{$form_key}", $data['parent_dropdown_relationship'], $data ); + $atts_ajax .= " data-um-parent='{$parent_dropdown_relationship}' "; + + if (isset( $data['custom_dropdown_options_source'] ) && !empty( $data['custom_dropdown_options_source'] ) && + $has_parent_option && function_exists( $data['custom_dropdown_options_source'] ) && + um_user( $data['parent_dropdown_relationship'] ) + ) { + $options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); + $disabled_by_parent_option = ''; + if (um_user( $form_key )) { + $select_original_option_value = " data-um-original-value='" . um_user( $form_key ) . "' "; } } - if ( ! empty( $disabled ) ) { - $output .= $this->disabled_hidden_field( $form_key, $field_value ); + } + + if (!empty( $data['custom_dropdown_options_source'] )) { + + $ajax_source = apply_filters( "um_custom_dropdown_options_source__{$form_key}", $data['custom_dropdown_options_source'], $data ); + $atts_ajax .= " data-um-ajax-source='{$ajax_source}' "; + + $ajax_source_url = apply_filters( "um_custom_dropdown_options_source_url__{$form_key}", admin_url( 'admin-ajax.php' ), $data ); + $atts_ajax .= " data-um-ajax-url='{$ajax_source_url}' "; + + + } + + $output .= ''; - - $output .= '
'; - - - if ($this->is_error( $form_key )) { - $output .= $this->field_error( $this->show_error( $form_key ) ); - } - - $output .= '
'; - break; - - /* Multi-Select dropdown */ - case 'multiselect': - - $max_selections = ( isset( $max_selections ) ) ? absint( $max_selections ) : 0; - - $output .= '
'; - - if (isset( $data['allowclear'] ) && $data['allowclear'] == 0) { - $class = 'um-s2'; - } else { - $class = 'um-s1'; - } - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $field_icon = false; - $field_icon_output = ''; - - $use_keyword = apply_filters( 'um_multiselect_option_value', 0, $data['type'] ); - - $output .= '
'; - if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { - $output .= '
'; - } - - $output .= ''; - - if (!empty( $disabled ) && !empty( $arr_selected )) { - foreach ($arr_selected as $item) { - $output .= $this->disabled_hidden_field( $key . '[]', $item ); - } - } - - $output .= '
'; - - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - break; - - /* Radio */ - case 'radio': - - $form_key = str_replace( 'role_radio', 'role', $key ); - - if (isset( $options )) { - $options = apply_filters( 'um_radio_field_options', $options, $data ); - $options = apply_filters( "um_radio_field_options_{$key}", $options ); - } - - $output .= '
'; - - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $output .= '
'; - - $options = $this->get_available_roles( $form_key, $options ); - - // add options - $i = 0; - $field_value = array(); - - if ( ! empty( $options ) ) { - foreach ( $options as $k => $v ) { - - $v = rtrim( $v ); + if (isset( $options_pair )) { + $option_value = $k; $um_field_checkbox_item_title = $v; - $option_value = $v; - - if (!is_numeric( $k ) && in_array( $form_key, array( 'role' ) )) { - $um_field_checkbox_item_title = $v; - $option_value = $k; - } - - $i++; - if ($i % 2 == 0) { - $col_class = 'right'; - } else { - $col_class = ''; - } - - if ($this->is_radio_checked( $key, $option_value, $data )) { - $active = 'active'; - $class = "um-icon-android-radio-button-on"; - } else { - $active = ''; - $class = "um-icon-android-radio-button-off"; - } - - if (isset( $data['editable'] ) && $data['editable'] == 0) { - $col_class .= " um-field-radio-state-disabled"; - } - - $output .= ''; - - if ($i % 2 == 0) { - $output .= '
'; - } - } - } - if (!empty( $disabled )) { - foreach ($field_value as $item) { - $output .= $this->disabled_hidden_field( $form_key, $item ); + $option_value = apply_filters( 'um_field_non_utf8_value', $option_value ); + + $output .= ''; + + } + } - $output .= '
'; + if ( ! empty( $disabled ) ) { + $output .= $this->disabled_hidden_field( $form_key, $field_value ); + } - $output .= '
'; + $output .= ''; - if ($this->is_error( $form_key )) { - $output .= $this->field_error( $this->show_error( $form_key ) ); - } + $output .= '
'; - $output .= '
'; - break; - /* Checkbox */ - case 'checkbox': + if ($this->is_error( $form_key )) { + $output .= $this->field_error( $this->show_error( $form_key ) ); + } - if (isset( $options )) { - $options = apply_filters( 'um_checkbox_field_options', $options, $data ); - $options = apply_filters( "um_checkbox_field_options_{$key}", $options ); - } + $output .= ''; + break; - $output .= '
'; + /* Multi-Select dropdown */ + case 'multiselect': - if (isset( $data['label'] )) { - $output .= $this->field_label( $label, $key, $data ); - } + $max_selections = ( isset( $max_selections ) ) ? absint( $max_selections ) : 0; - $output .= '
'; + $output .= '
'; - // add options - $i = 0; + if (isset( $data['allowclear'] ) && $data['allowclear'] == 0) { + $class = 'um-s2'; + } else { + $class = 'um-s1'; + } + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $field_icon = false; + $field_icon_output = ''; + + $use_keyword = apply_filters( 'um_multiselect_option_value', 0, $data['type'] ); + + $output .= '
'; + if (isset( $icon ) && $icon && isset( $this->field_icons ) && $this->field_icons == 'field') { + $output .= '
'; + } + + $output .= ''; + + if (!empty( $disabled ) && !empty( $arr_selected )) { + foreach ($arr_selected as $item) { + $output .= $this->disabled_hidden_field( $key . '[]', $item ); + } + } + + $output .= '
'; + + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + break; + + /* Radio */ + case 'radio': + + $form_key = str_replace( 'role_radio', 'role', $key ); + + if (isset( $options )) { + $options = apply_filters( 'um_radio_field_options', $options, $data ); + $options = apply_filters( "um_radio_field_options_{$key}", $options ); + } + + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + + $options = $this->get_available_roles( $form_key, $options ); + + // add options + $i = 0; + $field_value = array(); + + if ( ! empty( $options ) ) { + foreach ( $options as $k => $v ) { + + $v = rtrim( $v ); + + $um_field_checkbox_item_title = $v; + $option_value = $v; + + if (!is_numeric( $k ) && in_array( $form_key, array( 'role' ) )) { + $um_field_checkbox_item_title = $v; + $option_value = $k; + } + $i++; if ($i % 2 == 0) { $col_class = 'right'; @@ -2196,40 +2113,33 @@ $col_class = ''; } - if ($this->is_selected( $key, $v, $data )) { + if ($this->is_radio_checked( $key, $option_value, $data )) { $active = 'active'; - $class = "um-icon-android-checkbox-outline"; + $class = "um-icon-android-radio-button-on"; } else { $active = ''; - $class = "um-icon-android-checkbox-outline-blank"; + $class = "um-icon-android-radio-button-off"; } if (isset( $data['editable'] ) && $data['editable'] == 0) { $col_class .= " um-field-radio-state-disabled"; } - $output .= '
'; - - - if ($this->is_error( $key )) { - $output .= $this->field_error( $this->show_error( $key ) ); - } - - $output .= '
'; - break; - - /* HTML */ - case 'block': - $output .= '
-
' . $content . '
-
'; - break; - - /* Shortcode */ - case 'shortcode': - - $content = str_replace( '{profile_id}', um_profile_id(), $content ); - - $output .= '
-
' . do_shortcode( $content ) . '
-
'; - break; - - /* Unlimited Group */ - case 'group': - - $fields = $this->get_fields_in_group( $key ); - if (!empty( $fields )) { - - $output .= '
-
' . __( $label, UM_TEXTDOMAIN ) . '
'; - $output .= '
'; - - foreach ($fields as $subkey => $subdata) { - $output .= $this->edit_field( $subkey, $subdata, 'group' ); - } - - $output .= '
'; - $output .= '
'; - - } - - break; - - } - - // Custom filter for field output - if (isset( $this->set_mode )) { - $output = apply_filters( "um_{$key}_form_edit_field", $output, $this->set_mode ); - } - - return $output; - } - - - /** - * Filter for user roles - * - * @param $form_key - * @param array $options - * @return array - */ - function get_available_roles( $form_key, $options = array() ) { - if ( $form_key != 'role' ) { - return $options; - } - - // role field - global $wp_roles; - $role_keys = array_map( function( $item ) { - return 'um_' . $item; - }, get_option( 'um_roles' ) ); - $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) ); - - $roles = UM()->roles()->get_roles( false, $exclude_roles ); - - if ( ! empty( $options ) ) { - //fix when customers change options for role (radio/dropdown) fields - foreach ( $roles as $role_key => $role_title ) { - if ( false !== $search_key = array_search( $role_title, $options ) ) { - if ( $role_key !== $search_key ) { - $options[ $role_key ] = $role_title; - unset( $options[ $search_key ] ); - } + if (!empty( $disabled )) { + foreach ($field_value as $item) { + $output .= $this->disabled_hidden_field( $form_key, $item ); } } - $options = array_intersect( $options, $roles ); - } else { - $options = $roles; - } + $output .= '
'; + + $output .= '
'; + + if ($this->is_error( $form_key )) { + $output .= $this->field_error( $this->show_error( $form_key ) ); + } + + $output .= '
'; + break; + + /* Checkbox */ + case 'checkbox': + + if (isset( $options )) { + $options = apply_filters( 'um_checkbox_field_options', $options, $data ); + $options = apply_filters( "um_checkbox_field_options_{$key}", $options ); + } + + $output .= '
'; + + if (isset( $data['label'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + + // add options + $i = 0; + + foreach ($options as $k => $v) { + + $v = rtrim( $v ); + + $i++; + if ($i % 2 == 0) { + $col_class = 'right'; + } else { + $col_class = ''; + } + + if ($this->is_selected( $key, $v, $data )) { + $active = 'active'; + $class = "um-icon-android-checkbox-outline"; + } else { + $active = ''; + $class = "um-icon-android-checkbox-outline-blank"; + } + + if (isset( $data['editable'] ) && $data['editable'] == 0) { + $col_class .= " um-field-radio-state-disabled"; + } + + $output .= ''; + + if ($i % 2 == 0) { + $output .= '
'; + } + + } + + $output .= '
'; + + $output .= '
'; + + + if ($this->is_error( $key )) { + $output .= $this->field_error( $this->show_error( $key ) ); + } + + $output .= '
'; + break; + + /* HTML */ + case 'block': + $output .= '
+
' . $content . '
+
'; + break; + + /* Shortcode */ + case 'shortcode': + + $content = str_replace( '{profile_id}', um_profile_id(), $content ); + + $output .= '
+
' . do_shortcode( $content ) . '
+
'; + break; + + /* Unlimited Group */ + case 'group': + + $fields = $this->get_fields_in_group( $key ); + if (!empty( $fields )) { + + $output .= '
+
' . __( $label, UM_TEXTDOMAIN ) . '
'; + $output .= '
'; + + foreach ($fields as $subkey => $subdata) { + $output .= $this->edit_field( $subkey, $subdata, 'group' ); + } + + $output .= '
'; + $output .= '
'; + + } + + break; + + } + + // Custom filter for field output + if (isset( $this->set_mode )) { + $output = apply_filters( "um_{$key}_form_edit_field", $output, $this->set_mode ); + } + + return $output; + } + + + /** + * Filter for user roles + * + * @param $form_key + * @param array $options + * @return array + */ + function get_available_roles( $form_key, $options = array() ) { + if ( $form_key != 'role' ) { return $options; } - - /** - * Sorts columns array - * - * @param array $arr - * @param string $col - * @param string $dir - * - * @return array $arr - */ - function array_sort_by_column( $arr, $col, $dir = SORT_ASC ) { - $sort_col = array(); - foreach ($arr as $key => $row) { - if (isset( $row[$col] )) { - $sort_col[$key] = $row[$col]; - } - } - - array_multisort( $sort_col, $dir, $arr ); - - return $arr; - } - - - /** - * Get fields in row - * - * @param integer $row_id - * - * @return string - */ - function get_fields_by_row( $row_id ) { - foreach ($this->get_fields as $key => $array) { - if (!isset( $array['in_row'] ) || ( isset( $array['in_row'] ) && $array['in_row'] == $row_id )) { - $results[$key] = $array; - } - } - - return ( isset ( $results ) ) ? $results : ''; - } - - - /** - * Get fields by sub row - * - * @param string $row_fields - * @param integer $subrow_id - * - * @return mixed - */ - function get_fields_in_subrow( $row_fields, $subrow_id ) { - if (!is_array( $row_fields )) return ''; - foreach ($row_fields as $key => $array) { - if (!isset( $array['in_sub_row'] ) || ( isset( $array['in_sub_row'] ) && $array['in_sub_row'] == $subrow_id )) { - $results[$key] = $array; - } - } - - return ( isset ( $results ) ) ? $results : ''; - } - - /** - * Get fields in group - * - * @param integer $group_id - * - * @return mixed - */ - function get_fields_in_group( $group_id ) { - foreach ($this->get_fields as $key => $array) { - if (isset( $array['in_group'] ) && $array['in_group'] == $group_id) { - $results[$key] = $array; - } - } - - return ( isset ( $results ) ) ? $results : ''; - } - - - /** - * Get fields in column - * - * @param array $fields - * @param integer $col_number - * - * @return mixed - */ - function get_fields_in_column( $fields, $col_number ) { - foreach ($fields as $key => $array) { - if (isset( $array['in_column'] ) && $array['in_column'] == $col_number) { - $results[$key] = $array; - } - } - - return ( isset ( $results ) ) ? $results : ''; - } - - - /** - * Display fields - * - * @param string $mode - * @param array $args - * - * @return string - */ - function display( $mode, $args ) { - $output = null; - - $this->global_args = $args; - - UM()->form()->form_suffix = '-' . $this->global_args['form_id']; - - $this->set_mode = $mode; - $this->set_id = $this->global_args['form_id']; - - $this->field_icons = ( isset( $this->global_args['icons'] ) ) ? $this->global_args['icons'] : 'label'; - - // start output here - $this->get_fields = $this->get_fields(); - - if (!empty( $this->get_fields )) { - - // find rows - foreach ($this->get_fields as $key => $array) { - if ($array['type'] == 'row') { - $this->rows[$key] = $array; - unset( $this->get_fields[$key] ); // not needed anymore - } - } - - // rows fallback - if (!isset( $this->rows )) { - $this->rows = array( '_um_row_1' => array( - 'type' => 'row', - 'id' => '_um_row_1', - 'sub_rows' => 1, - 'cols' => 1 - ) - ); - } - - // master rows - foreach ($this->rows as $row_id => $row_array) { - - $row_fields = $this->get_fields_by_row( $row_id ); - if ($row_fields) { - - $output .= $this->new_row_output( $row_id, $row_array ); - - $sub_rows = ( isset( $row_array['sub_rows'] ) ) ? $row_array['sub_rows'] : 1; - for ($c = 0; $c < $sub_rows; $c++) { - - // cols - $cols = ( isset( $row_array['cols'] ) ) ? $row_array['cols'] : 1; - if (strstr( $cols, ':' )) { - $col_split = explode( ':', $cols ); - } else { - $col_split = array( $cols ); - } - $cols_num = $col_split[$c]; - - // sub row fields - $subrow_fields = null; - $subrow_fields = $this->get_fields_in_subrow( $row_fields, $c ); - - if (is_array( $subrow_fields )) { - - $subrow_fields = $this->array_sort_by_column( $subrow_fields, 'position' ); - - if ($cols_num == 1) { - - $output .= '
'; - $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); - if ($col1_fields) { - foreach ($col1_fields as $key => $data) { - $output .= $this->edit_field( $key, $data ); - } - } - $output .= '
'; - - } else if ($cols_num == 2) { - - $output .= '
'; - $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); - if ($col1_fields) { - foreach ($col1_fields as $key => $data) { - $output .= $this->edit_field( $key, $data ); - } - } - $output .= '
'; - - $output .= '
'; - $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); - if ($col2_fields) { - foreach ($col2_fields as $key => $data) { - $output .= $this->edit_field( $key, $data ); - } - } - $output .= '
'; - - } else { - - $output .= '
'; - $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); - if ($col1_fields) { - foreach ($col1_fields as $key => $data) { - $output .= $this->edit_field( $key, $data ); - } - } - $output .= '
'; - - $output .= '
'; - $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); - if ($col2_fields) { - foreach ($col2_fields as $key => $data) { - $output .= $this->edit_field( $key, $data ); - } - } - $output .= '
'; - - $output .= '
'; - $col3_fields = $this->get_fields_in_column( $subrow_fields, 3 ); - if ($col3_fields) { - foreach ($col3_fields as $key => $data) { - $output .= $this->edit_field( $key, $data ); - } - } - $output .= '
'; - - } - - } - - } - - $output .= ''; - - } - - } - - } - - return $output; - } - - /** - * Gets a field in `view mode` - * - * @param string $key - * @param array $data - * @param boolean $rule - * - * @return string - */ - function view_field( $key, $data, $rule = false ) { - $output = null; - - // get whole field data - if (is_array( $data )) { - $data = $this->get_field( $key ); - extract( $data ); - } - - if (!isset( $data['type'] )) return; - - if (isset( $data['in_group'] ) && $data['in_group'] != '' && $rule != 'group') return; - - if ($visibility == 'edit') return; - - if (in_array( $type, array( 'block', 'shortcode', 'spacing', 'divider', 'group' ) )) { - - } else { - - $_field_value = $this->field_value( $key, $default, $data ); - - if ( ! isset($_field_value) || $_field_value == '') return; - } - - if (!um_can_view_field( $data )) return; - - // disable these fields in profile view only - if (in_array( $key, array( 'user_password' ) ) && $this->set_mode == 'profile') { - return; - } - - if (!um_field_conditions_are_met( $data )) return; - - switch ($type) { - - /* Default */ - default: - - $output .= '
'; - - if (isset( $data['label'] ) || isset( $data['icon'] ) && !empty( $data['icon'] )) { - - if (!isset( $data['label'] )) $data['label'] = ''; - - $output .= $this->field_label( $data['label'], $key, $data ); - } - - $res = $this->field_value( $key, $default, $data ); - - if (!empty( $res )) { - $res = stripslashes( $res ); - } - - $data['is_view_field'] = true; - $res = apply_filters( "um_view_field", $res, $data, $type ); - $res = apply_filters( "um_view_field_value_{$type}", $res, $data ); - - $output .= '
'; - $output .= '
' . $res . '
'; - $output .= '
'; - - $output .= '
'; - - break; - - /* HTML */ - case 'block': - $output .= '
-
' . $content . '
-
'; - break; - - /* Shortcode */ - case 'shortcode': - - $content = str_replace( '{profile_id}', um_profile_id(), $content ); - - $output .= '
-
' . do_shortcode( $content ) . '
-
'; - break; - - /* Gap/Space */ - case 'spacing': - $output .= '
'; - break; - - /* A line divider */ - case 'divider': - $output .= '
'; - if ($divider_text) { - $output .= '
' . $divider_text . '
'; - } - $output .= '
'; - break; - - /* Rating */ - case 'rating': - - $output .= '
'; - - if (isset( $data['label'] ) || isset( $data['icon'] ) && !empty( $data['icon'] )) { - $output .= $this->field_label( $label, $key, $data ); - } - - $output .= '
'; - $output .= '
-
-
'; - $output .= '
'; - - $output .= '
'; - - break; - - } - - // Custom filter for field output - if (isset( $this->set_mode )) { - $output = apply_filters( "um_{$key}_form_show_field", $output, $this->set_mode ); - $output = apply_filters( "um_{$type}_form_show_field", $output, $this->set_mode ); - - } - - return $output; - } - - /** - * Display fields ( view mode ) - * - * @param string $mode - * @param array $args - * - * @return string - */ - function display_view( $mode, $args ) { - $output = null; - - $this->global_args = $args; - - UM()->form()->form_suffix = '-' . $this->global_args['form_id']; - - $this->set_mode = $mode; - $this->set_id = $this->global_args['form_id']; - - $this->field_icons = ( isset( $this->global_args['icons'] ) ) ? $this->global_args['icons'] : 'label'; - - // start output here - $this->get_fields = $this->get_fields(); - - if ( UM()->options()->get( 'profile_empty_text' ) ) { - - $emo = UM()->options()->get( 'profile_empty_text_emo' ); - if ($emo) { - $emo = ''; + // role field + global $wp_roles; + $role_keys = array_map( function( $item ) { + return 'um_' . $item; + }, get_option( 'um_roles' ) ); + $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) ); + + $roles = UM()->roles()->get_roles( false, $exclude_roles ); + + if ( ! empty( $options ) ) { + //fix when customers change options for role (radio/dropdown) fields + $intersected_options = array(); + foreach ( $options as $option ) { + if ( false !== $search_key = array_search( $option, $roles ) ) { + $intersected_options[ $search_key ] = $option; } else { - $emo = false; - } - - if (um_is_myprofile()) { - $output .= '

' . $emo . '' . sprintf( __( 'Your profile is looking a little empty. Why not add some information!', 'ultimate-member' ), um_edit_profile_url() ) . '

'; - } else { - $output .= '

' . $emo . '' . __( 'This user has not added any information to their profile yet.', 'ultimate-member' ) . '

'; + $intersected_options[] = $option; } } - if (!empty( $this->get_fields )) { - - // find rows - foreach ($this->get_fields as $key => $array) { - if ($array['type'] == 'row') { - $this->rows[$key] = $array; - unset( $this->get_fields[$key] ); // not needed anymore - } - } - - // rows fallback - if (!isset( $this->rows )) { - $this->rows = array( '_um_row_1' => array( - 'type' => 'row', - 'id' => '_um_row_1', - 'sub_rows' => 1, - 'cols' => 1 - ) - ); - } - - // master rows - foreach ($this->rows as $row_id => $row_array) { - - $row_fields = $this->get_fields_by_row( $row_id ); - - if ($row_fields) { - - $output .= $this->new_row_output( $row_id, $row_array ); - - $sub_rows = ( isset( $row_array['sub_rows'] ) ) ? $row_array['sub_rows'] : 1; - for ($c = 0; $c < $sub_rows; $c++) { - - // cols - $cols = ( isset( $row_array['cols'] ) ) ? $row_array['cols'] : 1; - if (strstr( $cols, ':' )) { - $col_split = explode( ':', $cols ); - } else { - $col_split = array( $cols ); - } - $cols_num = $col_split[$c]; - - // sub row fields - $subrow_fields = null; - $subrow_fields = $this->get_fields_in_subrow( $row_fields, $c ); - - if (is_array( $subrow_fields )) { - - $subrow_fields = $this->array_sort_by_column( $subrow_fields, 'position' ); - - if ($cols_num == 1) { - - $output .= '
'; - $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); - if ($col1_fields) { - foreach ($col1_fields as $key => $data) { - - $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); - - $output .= $this->view_field( $key, $data ); - - - } - } - $output .= '
'; - - } else if ($cols_num == 2) { - - $output .= '
'; - $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); - if ($col1_fields) { - foreach ($col1_fields as $key => $data) { - - $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); - - $output .= $this->view_field( $key, $data ); - - } - } - $output .= '
'; - - $output .= '
'; - $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); - if ($col2_fields) { - foreach ($col2_fields as $key => $data) { - - $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); - - $output .= $this->view_field( $key, $data ); - - } - } - $output .= '
'; - - } else { - - $output .= '
'; - $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); - if ($col1_fields) { - foreach ($col1_fields as $key => $data) { - - $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); - - $output .= $this->view_field( $key, $data ); - - } - } - $output .= '
'; - - $output .= '
'; - $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); - if ($col2_fields) { - foreach ($col2_fields as $key => $data) { - - $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); - - $output .= $this->view_field( $key, $data ); - - } - } - $output .= '
'; - - $output .= '
'; - $col3_fields = $this->get_fields_in_column( $subrow_fields, 3 ); - if ($col3_fields) { - foreach ($col3_fields as $key => $data) { - - $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); - - $output .= $this->view_field( $key, $data ); - - } - } - $output .= '
'; - - } - - } - - } - - $output .= ''; - - } - - } - - } - - return $output; + $options = $intersected_options; + } else { + $options = $roles; } - /** - * Get new row in form - * - * @param string $row_id - * @param array $row_array - * - * @return array - */ - function new_row_output( $row_id, $row_array ) { - $output = null; - extract( $row_array ); - - $padding = ( isset( $padding ) ) ? $padding : ''; - $margin = ( isset( $margin ) ) ? $margin : ''; - $background = ( isset( $background ) ) ? $background : ''; - $text_color = ( isset( $text_color ) ) ? $text_color : ''; - $borderradius = ( isset( $borderradius ) ) ? $borderradius : ''; - $border = ( isset( $border ) ) ? $border : ''; - $bordercolor = ( isset( $bordercolor ) ) ? $bordercolor : ''; - $borderstyle = ( isset( $borderstyle ) ) ? $borderstyle : ''; - $heading = ( isset( $heading ) ) ? $heading : ''; - $css_class = ( isset( $css_class ) ) ? $css_class : ''; - - $css_padding = ''; - $css_margin = ''; - $css_background = ''; - $css_borderradius = ''; - $css_border = ''; - $css_bordercolor = ''; - $css_borderstyle = ''; - $css_heading_background_color = ''; - $css_heading_padding = ''; - $css_heading_text_color = ''; - $css_heading_borderradius = ''; - $css_text_color = ''; - - // row css rules - if ($padding) $css_padding = 'padding: ' . $padding . ';'; - if ($margin) { - $css_margin = 'margin: ' . $margin . ';'; - } else { - $css_margin = 'margin: 0 0 30px 0;'; - } - - if ($background) $css_background = 'background-color: ' . $background . ';'; - if ($borderradius) $css_borderradius = 'border-radius: 0px 0px ' . $borderradius . ' ' . $borderradius . ';'; - if ($border) $css_border = 'border-width: ' . $border . ';'; - if ($bordercolor) $css_bordercolor = 'border-color: ' . $bordercolor . ';'; - if ($borderstyle) $css_borderstyle = 'border-style: ' . $borderstyle . ';'; - if ($text_color) $css_text_color = 'color: ' . $text_color . ' !important;'; - - // show the heading - if ($heading) { - - $heading_background_color = ( isset( $heading_background_color ) ) ? $heading_background_color : ''; - $heading_text_color = ( isset( $heading_text_color ) ) ? $heading_text_color : ''; - - if ($heading_background_color) { - $css_heading_background_color = 'background-color: ' . $heading_background_color . ';'; - $css_heading_padding = 'padding: 10px 15px;'; - } - - if ($heading_text_color) $css_heading_text_color = 'color: ' . $heading_text_color . ';'; - if ($borderradius) $css_heading_borderradius = 'border-radius: ' . $borderradius . ' ' . $borderradius . ' 0px 0px;'; - - $output .= '
'; - - if (isset( $icon )) { - $output .= ''; - } - - $output .= ( !empty( $heading_text ) ? $heading_text : '' ) . '
'; - - } else { - - // no heading - if ($borderradius) $css_borderradius = 'border-radius: ' . $borderradius . ';'; - - } - - $output .= '
'; - - return $output; - } - - - function do_ajax_action() { - if (!is_user_logged_in() || !current_user_can( 'manage_options' )) die( __( 'Please login as administrator', 'ultimate-member' ) ); - - extract( $_POST ); - - $output = null; - - $position = array(); - if (!empty( $in_column )) { - $position['in_row'] = '_um_row_' . ( (int)$in_row + 1 ); - $position['in_sub_row'] = $in_sub_row; - $position['in_column'] = $in_column; - $position['in_group'] = $in_group; - } - - switch ($act_id) { - - case 'um_admin_duplicate_field': - UM()->fields()->duplicate_field( $arg1, $arg2 ); - break; - - case 'um_admin_remove_field_global': - UM()->fields()->delete_field_from_db( $arg1 ); - break; - - case 'um_admin_remove_field': - UM()->fields()->delete_field_from_form( $arg1, $arg2 ); - break; - - case 'um_admin_add_field_from_predefined': - UM()->fields()->add_field_from_predefined( $arg1, $arg2, $position ); - break; - - case 'um_admin_add_field_from_list': - UM()->fields()->add_field_from_list( $arg1, $arg2, $position ); - break; - - } - - if (is_array( $output )) { - print_r( $output ); - } else { - echo $output; - } - die; - - } + return $options; } - } \ No newline at end of file + + + /** + * Sorts columns array + * + * @param array $arr + * @param string $col + * @param string $dir + * + * @return array $arr + */ + function array_sort_by_column( $arr, $col, $dir = SORT_ASC ) { + $sort_col = array(); + foreach ($arr as $key => $row) { + if (isset( $row[$col] )) { + $sort_col[$key] = $row[$col]; + } + } + + array_multisort( $sort_col, $dir, $arr ); + + return $arr; + } + + + /** + * Get fields in row + * + * @param integer $row_id + * + * @return string + */ + function get_fields_by_row( $row_id ) { + foreach ($this->get_fields as $key => $array) { + if (!isset( $array['in_row'] ) || ( isset( $array['in_row'] ) && $array['in_row'] == $row_id )) { + $results[$key] = $array; + } + } + + return ( isset ( $results ) ) ? $results : ''; + } + + + /** + * Get fields by sub row + * + * @param string $row_fields + * @param integer $subrow_id + * + * @return mixed + */ + function get_fields_in_subrow( $row_fields, $subrow_id ) { + if (!is_array( $row_fields )) return ''; + foreach ($row_fields as $key => $array) { + if (!isset( $array['in_sub_row'] ) || ( isset( $array['in_sub_row'] ) && $array['in_sub_row'] == $subrow_id )) { + $results[$key] = $array; + } + } + + return ( isset ( $results ) ) ? $results : ''; + } + + /** + * Get fields in group + * + * @param integer $group_id + * + * @return mixed + */ + function get_fields_in_group( $group_id ) { + foreach ($this->get_fields as $key => $array) { + if (isset( $array['in_group'] ) && $array['in_group'] == $group_id) { + $results[$key] = $array; + } + } + + return ( isset ( $results ) ) ? $results : ''; + } + + + /** + * Get fields in column + * + * @param array $fields + * @param integer $col_number + * + * @return mixed + */ + function get_fields_in_column( $fields, $col_number ) { + foreach ($fields as $key => $array) { + if (isset( $array['in_column'] ) && $array['in_column'] == $col_number) { + $results[$key] = $array; + } + } + + return ( isset ( $results ) ) ? $results : ''; + } + + + /** + * Display fields + * + * @param string $mode + * @param array $args + * + * @return string + */ + function display( $mode, $args ) { + $output = null; + + $this->global_args = $args; + + UM()->form()->form_suffix = '-' . $this->global_args['form_id']; + + $this->set_mode = $mode; + $this->set_id = $this->global_args['form_id']; + + $this->field_icons = ( isset( $this->global_args['icons'] ) ) ? $this->global_args['icons'] : 'label'; + + // start output here + $this->get_fields = $this->get_fields(); + + if (!empty( $this->get_fields )) { + + // find rows + foreach ($this->get_fields as $key => $array) { + if ($array['type'] == 'row') { + $this->rows[$key] = $array; + unset( $this->get_fields[$key] ); // not needed anymore + } + } + + // rows fallback + if (!isset( $this->rows )) { + $this->rows = array( '_um_row_1' => array( + 'type' => 'row', + 'id' => '_um_row_1', + 'sub_rows' => 1, + 'cols' => 1 + ) + ); + } + + // master rows + foreach ($this->rows as $row_id => $row_array) { + + $row_fields = $this->get_fields_by_row( $row_id ); + if ($row_fields) { + + $output .= $this->new_row_output( $row_id, $row_array ); + + $sub_rows = ( isset( $row_array['sub_rows'] ) ) ? $row_array['sub_rows'] : 1; + for ($c = 0; $c < $sub_rows; $c++) { + + // cols + $cols = ( isset( $row_array['cols'] ) ) ? $row_array['cols'] : 1; + if (strstr( $cols, ':' )) { + $col_split = explode( ':', $cols ); + } else { + $col_split = array( $cols ); + } + $cols_num = $col_split[$c]; + + // sub row fields + $subrow_fields = null; + $subrow_fields = $this->get_fields_in_subrow( $row_fields, $c ); + + if (is_array( $subrow_fields )) { + + $subrow_fields = $this->array_sort_by_column( $subrow_fields, 'position' ); + + if ($cols_num == 1) { + + $output .= '
'; + $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); + if ($col1_fields) { + foreach ($col1_fields as $key => $data) { + $output .= $this->edit_field( $key, $data ); + } + } + $output .= '
'; + + } else if ($cols_num == 2) { + + $output .= '
'; + $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); + if ($col1_fields) { + foreach ($col1_fields as $key => $data) { + $output .= $this->edit_field( $key, $data ); + } + } + $output .= '
'; + + $output .= '
'; + $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); + if ($col2_fields) { + foreach ($col2_fields as $key => $data) { + $output .= $this->edit_field( $key, $data ); + } + } + $output .= '
'; + + } else { + + $output .= '
'; + $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); + if ($col1_fields) { + foreach ($col1_fields as $key => $data) { + $output .= $this->edit_field( $key, $data ); + } + } + $output .= '
'; + + $output .= '
'; + $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); + if ($col2_fields) { + foreach ($col2_fields as $key => $data) { + $output .= $this->edit_field( $key, $data ); + } + } + $output .= '
'; + + $output .= '
'; + $col3_fields = $this->get_fields_in_column( $subrow_fields, 3 ); + if ($col3_fields) { + foreach ($col3_fields as $key => $data) { + $output .= $this->edit_field( $key, $data ); + } + } + $output .= '
'; + + } + + } + + } + + $output .= '
'; + + } + + } + + } + + return $output; + } + + /** + * Gets a field in `view mode` + * + * @param string $key + * @param array $data + * @param boolean $rule + * + * @return string + */ + function view_field( $key, $data, $rule = false ) { + $output = null; + + // get whole field data + if (is_array( $data )) { + $data = $this->get_field( $key ); + extract( $data ); + } + + if (!isset( $data['type'] )) return; + + if (isset( $data['in_group'] ) && $data['in_group'] != '' && $rule != 'group') return; + + if ($visibility == 'edit') return; + + if (in_array( $type, array( 'block', 'shortcode', 'spacing', 'divider', 'group' ) )) { + + } else { + + $_field_value = $this->field_value( $key, $default, $data ); + + if ( ! isset($_field_value) || $_field_value == '') return; + } + + if (!um_can_view_field( $data )) return; + + // disable these fields in profile view only + if (in_array( $key, array( 'user_password' ) ) && $this->set_mode == 'profile') { + return; + } + + if (!um_field_conditions_are_met( $data )) return; + + switch ($type) { + + /* Default */ + default: + + $output .= '
'; + + if (isset( $data['label'] ) || isset( $data['icon'] ) && !empty( $data['icon'] )) { + + if (!isset( $data['label'] )) $data['label'] = ''; + + $output .= $this->field_label( $data['label'], $key, $data ); + } + + $res = $this->field_value( $key, $default, $data ); + + if (!empty( $res )) { + $res = stripslashes( $res ); + } + + $data['is_view_field'] = true; + $res = apply_filters( "um_view_field", $res, $data, $type ); + $res = apply_filters( "um_view_field_value_{$type}", $res, $data ); + + $output .= '
'; + $output .= '
' . $res . '
'; + $output .= '
'; + + $output .= '
'; + + break; + + /* HTML */ + case 'block': + $output .= '
+
' . $content . '
+
'; + break; + + /* Shortcode */ + case 'shortcode': + + $content = str_replace( '{profile_id}', um_profile_id(), $content ); + + $output .= '
+
' . do_shortcode( $content ) . '
+
'; + break; + + /* Gap/Space */ + case 'spacing': + $output .= '
'; + break; + + /* A line divider */ + case 'divider': + $output .= '
'; + if ($divider_text) { + $output .= '
' . $divider_text . '
'; + } + $output .= '
'; + break; + + /* Rating */ + case 'rating': + + $output .= '
'; + + if (isset( $data['label'] ) || isset( $data['icon'] ) && !empty( $data['icon'] )) { + $output .= $this->field_label( $label, $key, $data ); + } + + $output .= '
'; + $output .= '
+
+
'; + $output .= '
'; + + $output .= '
'; + + break; + + } + + // Custom filter for field output + if (isset( $this->set_mode )) { + $output = apply_filters( "um_{$key}_form_show_field", $output, $this->set_mode ); + $output = apply_filters( "um_{$type}_form_show_field", $output, $this->set_mode ); + + } + + return $output; + } + + /** + * Display fields ( view mode ) + * + * @param string $mode + * @param array $args + * + * @return string + */ + function display_view( $mode, $args ) { + $output = null; + + $this->global_args = $args; + + UM()->form()->form_suffix = '-' . $this->global_args['form_id']; + + $this->set_mode = $mode; + $this->set_id = $this->global_args['form_id']; + + $this->field_icons = ( isset( $this->global_args['icons'] ) ) ? $this->global_args['icons'] : 'label'; + + // start output here + $this->get_fields = $this->get_fields(); + + if ( UM()->options()->get( 'profile_empty_text' ) ) { + + $emo = UM()->options()->get( 'profile_empty_text_emo' ); + if ($emo) { + $emo = ''; + } else { + $emo = false; + } + + if (um_is_myprofile()) { + $output .= '

' . $emo . '' . sprintf( __( 'Your profile is looking a little empty. Why not add some information!', 'ultimate-member' ), um_edit_profile_url() ) . '

'; + } else { + $output .= '

' . $emo . '' . __( 'This user has not added any information to their profile yet.', 'ultimate-member' ) . '

'; + } + } + + if (!empty( $this->get_fields )) { + + // find rows + foreach ($this->get_fields as $key => $array) { + if ($array['type'] == 'row') { + $this->rows[$key] = $array; + unset( $this->get_fields[$key] ); // not needed anymore + } + } + + // rows fallback + if (!isset( $this->rows )) { + $this->rows = array( '_um_row_1' => array( + 'type' => 'row', + 'id' => '_um_row_1', + 'sub_rows' => 1, + 'cols' => 1 + ) + ); + } + + // master rows + foreach ($this->rows as $row_id => $row_array) { + + $row_fields = $this->get_fields_by_row( $row_id ); + + if ($row_fields) { + + $output .= $this->new_row_output( $row_id, $row_array ); + + $sub_rows = ( isset( $row_array['sub_rows'] ) ) ? $row_array['sub_rows'] : 1; + for ($c = 0; $c < $sub_rows; $c++) { + + // cols + $cols = ( isset( $row_array['cols'] ) ) ? $row_array['cols'] : 1; + if (strstr( $cols, ':' )) { + $col_split = explode( ':', $cols ); + } else { + $col_split = array( $cols ); + } + $cols_num = $col_split[$c]; + + // sub row fields + $subrow_fields = null; + $subrow_fields = $this->get_fields_in_subrow( $row_fields, $c ); + + if (is_array( $subrow_fields )) { + + $subrow_fields = $this->array_sort_by_column( $subrow_fields, 'position' ); + + if ($cols_num == 1) { + + $output .= '
'; + $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); + if ($col1_fields) { + foreach ($col1_fields as $key => $data) { + + $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); + + $output .= $this->view_field( $key, $data ); + + + } + } + $output .= '
'; + + } else if ($cols_num == 2) { + + $output .= '
'; + $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); + if ($col1_fields) { + foreach ($col1_fields as $key => $data) { + + $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); + + $output .= $this->view_field( $key, $data ); + + } + } + $output .= '
'; + + $output .= '
'; + $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); + if ($col2_fields) { + foreach ($col2_fields as $key => $data) { + + $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); + + $output .= $this->view_field( $key, $data ); + + } + } + $output .= '
'; + + } else { + + $output .= '
'; + $col1_fields = $this->get_fields_in_column( $subrow_fields, 1 ); + if ($col1_fields) { + foreach ($col1_fields as $key => $data) { + + $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); + + $output .= $this->view_field( $key, $data ); + + } + } + $output .= '
'; + + $output .= '
'; + $col2_fields = $this->get_fields_in_column( $subrow_fields, 2 ); + if ($col2_fields) { + foreach ($col2_fields as $key => $data) { + + $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); + + $output .= $this->view_field( $key, $data ); + + } + } + $output .= '
'; + + $output .= '
'; + $col3_fields = $this->get_fields_in_column( $subrow_fields, 3 ); + if ($col3_fields) { + foreach ($col3_fields as $key => $data) { + + $data = apply_filters( "um_view_field_output_" . $data['type'], $data ); + + $output .= $this->view_field( $key, $data ); + + } + } + $output .= '
'; + + } + + } + + } + + $output .= ''; + + } + + } + + } + + return $output; + } + + /** + * Get new row in form + * + * @param string $row_id + * @param array $row_array + * + * @return array + */ + function new_row_output( $row_id, $row_array ) { + $output = null; + extract( $row_array ); + + $padding = ( isset( $padding ) ) ? $padding : ''; + $margin = ( isset( $margin ) ) ? $margin : ''; + $background = ( isset( $background ) ) ? $background : ''; + $text_color = ( isset( $text_color ) ) ? $text_color : ''; + $borderradius = ( isset( $borderradius ) ) ? $borderradius : ''; + $border = ( isset( $border ) ) ? $border : ''; + $bordercolor = ( isset( $bordercolor ) ) ? $bordercolor : ''; + $borderstyle = ( isset( $borderstyle ) ) ? $borderstyle : ''; + $heading = ( isset( $heading ) ) ? $heading : ''; + $css_class = ( isset( $css_class ) ) ? $css_class : ''; + + $css_padding = ''; + $css_margin = ''; + $css_background = ''; + $css_borderradius = ''; + $css_border = ''; + $css_bordercolor = ''; + $css_borderstyle = ''; + $css_heading_background_color = ''; + $css_heading_padding = ''; + $css_heading_text_color = ''; + $css_heading_borderradius = ''; + $css_text_color = ''; + + // row css rules + if ($padding) $css_padding = 'padding: ' . $padding . ';'; + if ($margin) { + $css_margin = 'margin: ' . $margin . ';'; + } else { + $css_margin = 'margin: 0 0 30px 0;'; + } + + if ($background) $css_background = 'background-color: ' . $background . ';'; + if ($borderradius) $css_borderradius = 'border-radius: 0px 0px ' . $borderradius . ' ' . $borderradius . ';'; + if ($border) $css_border = 'border-width: ' . $border . ';'; + if ($bordercolor) $css_bordercolor = 'border-color: ' . $bordercolor . ';'; + if ($borderstyle) $css_borderstyle = 'border-style: ' . $borderstyle . ';'; + if ($text_color) $css_text_color = 'color: ' . $text_color . ' !important;'; + + // show the heading + if ($heading) { + + $heading_background_color = ( isset( $heading_background_color ) ) ? $heading_background_color : ''; + $heading_text_color = ( isset( $heading_text_color ) ) ? $heading_text_color : ''; + + if ($heading_background_color) { + $css_heading_background_color = 'background-color: ' . $heading_background_color . ';'; + $css_heading_padding = 'padding: 10px 15px;'; + } + + if ($heading_text_color) $css_heading_text_color = 'color: ' . $heading_text_color . ';'; + if ($borderradius) $css_heading_borderradius = 'border-radius: ' . $borderradius . ' ' . $borderradius . ' 0px 0px;'; + + $output .= '
'; + + if (isset( $icon )) { + $output .= ''; + } + + $output .= ( !empty( $heading_text ) ? $heading_text : '' ) . '
'; + + } else { + + // no heading + if ($borderradius) $css_borderradius = 'border-radius: ' . $borderradius . ';'; + + } + + $output .= '
'; + + return $output; + } + + + function do_ajax_action() { + if (!is_user_logged_in() || !current_user_can( 'manage_options' )) die( __( 'Please login as administrator', 'ultimate-member' ) ); + + extract( $_POST ); + + $output = null; + + $position = array(); + if (!empty( $in_column )) { + $position['in_row'] = '_um_row_' . ( (int)$in_row + 1 ); + $position['in_sub_row'] = $in_sub_row; + $position['in_column'] = $in_column; + $position['in_group'] = $in_group; + } + + switch ($act_id) { + + case 'um_admin_duplicate_field': + UM()->fields()->duplicate_field( $arg1, $arg2 ); + break; + + case 'um_admin_remove_field_global': + UM()->fields()->delete_field_from_db( $arg1 ); + break; + + case 'um_admin_remove_field': + UM()->fields()->delete_field_from_form( $arg1, $arg2 ); + break; + + case 'um_admin_add_field_from_predefined': + UM()->fields()->add_field_from_predefined( $arg1, $arg2, $position ); + break; + + case 'um_admin_add_field_from_list': + UM()->fields()->add_field_from_list( $arg1, $arg2, $position ); + break; + + } + + if (is_array( $output )) { + print_r( $output ); + } else { + echo $output; + } + die; + + } + } +} \ No newline at end of file From 0aacb5504fbaa964b9341710bde7b764ab2e892e Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Mon, 18 Dec 2017 15:16:14 +0200 Subject: [PATCH 2/4] - added info text field; --- includes/admin/core/class-admin-forms.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/admin/core/class-admin-forms.php b/includes/admin/core/class-admin-forms.php index 20b06b77..4aed11a6 100644 --- a/includes/admin/core/class-admin-forms.php +++ b/includes/admin/core/class-admin-forms.php @@ -972,6 +972,11 @@ if ( ! class_exists( 'Admin_Forms' ) ) { } + function render_info_text( $field_data ) { + return $field_data['value']; + } + + /** * Get field value * From cb0de87668f64e3dc709c79e14fea7c5e12b3427 Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Mon, 18 Dec 2017 15:36:04 +0200 Subject: [PATCH 3/4] - added deprecated functions; - added security ABSPATH checking; --- .../admin/core/um-admin-actions-modal.php | 3 + includes/admin/core/um-admin-actions-user.php | 3 + includes/admin/core/um-admin-actions.php | 3 + .../admin/core/um-admin-filters-fields.php | 3 + includes/core/class-ajax-common.php | 21 +- includes/core/um-actions-access.php | 3 + includes/core/um-actions-account.php | 2 + includes/core/um-actions-ajax.php | 2 + includes/core/um-actions-core.php | 2 + includes/core/um-actions-form.php | 3 + includes/core/um-actions-global.php | 3 + includes/core/um-actions-login.php | 3 + includes/core/um-actions-members.php | 4 + includes/core/um-actions-misc.php | 3 + includes/core/um-actions-password.php | 3 + includes/core/um-actions-profile.php | 3 + includes/core/um-actions-register.php | 3 + includes/core/um-actions-save-profile.php | 3 + includes/core/um-actions-user.php | 3 + includes/core/um-actions-wpadmin.php | 5 +- includes/core/um-filters-account.php | 3 + includes/core/um-filters-arguments.php | 3 + includes/core/um-filters-avatars.php | 3 + includes/core/um-filters-commenting.php | 3 + includes/core/um-filters-fields.php | 3 + includes/core/um-filters-files.php | 3 + includes/core/um-filters-language.php | 5 +- includes/core/um-filters-login.php | 3 + includes/core/um-filters-members.php | 5 +- includes/core/um-filters-misc.php | 3 + includes/core/um-filters-navmenu.php | 3 + includes/core/um-filters-profile.php | 3 + includes/core/um-filters-user.php | 4 +- includes/lib/upload/um-image-upload.php | 1 + includes/um-deprecated-functions.php | 263 ++ includes/um-short-functions.php | 3552 +++++++++-------- 36 files changed, 2145 insertions(+), 1793 deletions(-) diff --git a/includes/admin/core/um-admin-actions-modal.php b/includes/admin/core/um-admin-actions-modal.php index 0d10bb54..7b31fb3c 100644 --- a/includes/admin/core/um-admin-actions-modal.php +++ b/includes/admin/core/um-admin-actions-modal.php @@ -1,4 +1,7 @@ - - - - $value ) { $wp_load = "{$value}/wp-load.php"; if ( file_exists( $wp_load ) ) { diff --git a/includes/um-deprecated-functions.php b/includes/um-deprecated-functions.php index 7a5ffeb5..7f43dbe4 100644 --- a/includes/um-deprecated-functions.php +++ b/includes/um-deprecated-functions.php @@ -72,4 +72,267 @@ function um_update_option( $option_id, $value ) { function um_remove_option( $option_id ) { //um_deprecated_function( 'um_remove_option', '2.0', 'UM()->options()->remove' ); UM()->options()->remove( $option_id ); +} + + +/** + * @deprecated 2.0 + * + * @param $content_type + * @return string + */ +function um_mail_content_type( $content_type ) { + return 'text/html'; +} + + +/** + * Convert urls to clickable links + * + * @deprecated 2.0 + * + * @param $s + * @return mixed + */ +function um_clickable_links( $s ) { + return preg_replace( '@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '$1', $s ); +} + + +/** + * Set redirect key + * + * @deprecated 2.0 + * + * @param string $url + * @return string $redirect_key + */ +function um_set_redirect_url( $url ) { + + if (um_is_session_started() === false) { + session_start(); + } + + $redirect_key = wp_generate_password( 12, false ); + + $_SESSION['um_redirect_key'] = array( $redirect_key => $url ); + + return $redirect_key; +} + + +/** + * Set redirect key + * + * @deprecated 2.0 + * + * @param string $key + * @return string $redirect_key + */ +function um_get_redirect_url( $key ) { + + if (um_is_session_started() === false) { + session_start(); + } + + if (isset( $_SESSION['um_redirect_key'][$key] )) { + + $url = $_SESSION['um_redirect_key'][$key]; + + return $url; + + } else { + + if (isset( $_SESSION['um_redirect_key'] )) { + foreach ($_SESSION['um_redirect_key'] as $key => $url) { + + return $url; + + break; + } + } + } + + return; +} + + +/** + * Get user's last login time + * + * @deprecated 2.0 + * + * @param $user_id + * @return string + */ +function um_user_last_login_date( $user_id ) { + $value = get_user_meta( $user_id, '_um_last_login', true ); + if ($value) + return date_i18n( 'F d, Y', $value ); + + return ''; +} + + +/** + * Check if we are on UM page + * + * @deprecated 2.0 + * + * @return bool + */ +function is_ultimatemember() { + global $post; + if ( isset( $post->ID ) && in_array( $post->ID, UM()->config()->permalinks ) ) + return true; + + return false; +} + + +/** + * Is core URL + * + * @deprecated 2.0 + * + * @return bool + */ +function um_is_core_uri() { + $array = UM()->config()->permalinks; + $current_url = UM()->permalinks()->get_current_url( get_option( 'permalink_structure' ) ); + + if (!isset( $array ) || !is_array( $array )) return false; + + foreach ($array as $k => $id) { + $page_url = get_permalink( $id ); + if (strstr( $current_url, $page_url )) + return true; + } + + return false; +} + + +/** + * Check if meta_value exists + * + * @deprecated 2.0 + * + * @param string $key + * @param mixed $value + * @param mixed $return_user_id + * + * @return integer + */ +function um_is_meta_value_exists( $key, $value, $return_user_id = false ) { + global $wpdb; + + if (isset( UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] )) { + return UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key]; + } + + if (!$return_user_id) { + $count = $wpdb->get_var( $wpdb->prepare( + "SELECT COUNT(*) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", + $key, + $value + ) ); + + UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] = $count; + + return $count; + } + + $user_id = $wpdb->get_var( $wpdb->prepare( + "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", + $key, + $value + ) ); + + UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] = $user_id; + + return $user_id; + +} + + +/** + * Get localization + * + * @deprecated 2.0 + * + * @return string + */ +function um_get_locale() { + + $lang_code = get_locale(); + + if (strpos( $lang_code, 'en_' ) > -1 || empty( $lang_code ) || $lang_code == 0) { + return 'en'; + } + + return $lang_code; +} + + +/** + * Get current page type + * + * @deprecated 2.0 + * + * @return string + */ +function um_get_current_page_type() { + global $wp_query; + $loop = 'notfound'; + + if ($wp_query->is_page) { + //$loop = is_front_page() ? 'front' : 'page'; + $loop = 'page'; + } else if ($wp_query->is_home) { + $loop = 'home'; + } else if ($wp_query->is_single) { + $loop = ( $wp_query->is_attachment ) ? 'attachment' : 'single'; + } else if ($wp_query->is_category) { + $loop = 'category'; + } else if ($wp_query->is_tag) { + $loop = 'tag'; + } else if ($wp_query->is_tax) { + $loop = 'tax'; + } else if ($wp_query->is_archive) { + if ($wp_query->is_day) { + $loop = 'day'; + } else if ($wp_query->is_month) { + $loop = 'month'; + } else if ($wp_query->is_year) { + $loop = 'year'; + } else if ($wp_query->is_author) { + $loop = 'author'; + } else { + $loop = 'archive'; + } + } else if ($wp_query->is_search) { + $loop = 'search'; + } else if ($wp_query->is_404) { + $loop = 'notfound'; + } + + return $loop; +} + + +/** + * Check if running local + * + * @deprecated 2.0 + * + * @return boolean + */ +function um_core_is_local() { + if ($_SERVER['HTTP_HOST'] == 'localhost' + || substr( $_SERVER['HTTP_HOST'], 0, 3 ) == '10.' + || substr( $_SERVER['HTTP_HOST'], 0, 7 ) == '192.168' + ) return true; + + return false; } \ No newline at end of file diff --git a/includes/um-short-functions.php b/includes/um-short-functions.php index d1bb2e5d..397b63d5 100644 --- a/includes/um-short-functions.php +++ b/includes/um-short-functions.php @@ -1,1961 +1,1973 @@ $length ? substr( $s, 0, $length ) . "..." : $s; + + return $s; +} + + +/** + * Get where user should be headed after logging + * + * @param string $redirect_to + * + * @return bool|false|mixed|string|void + */ +function um_dynamic_login_page_redirect( $redirect_to = '' ) { + + $uri = um_get_core_page( 'login' ); + + if (!$redirect_to) { + $redirect_to = UM()->permalinks()->get_current_url(); } - /*** - *** @Trim string by char length - ***/ - function um_trim_string( $s, $length = 20 ) { - $s = strlen( $s ) > $length ? substr( $s, 0, $length ) . "..." : $s; + $redirect_key = urlencode_deep( $redirect_to ); - return $s; - } + $uri = add_query_arg( 'redirect_to', $redirect_key, $uri ); - /*** - *** @Convert urls to clickable links - ***/ - function um_clickable_links( $s ) { + return $uri; +} - return preg_replace( '@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '$1', $s ); - } - /*** - *** @Get where user should be headed after logging - ***/ - function um_dynamic_login_page_redirect( $redirect_to = '' ) { - - $uri = um_get_core_page( 'login' ); - - if (!$redirect_to) { - $redirect_to = UM()->permalinks()->get_current_url(); - } - - $redirect_key = urlencode_deep( $redirect_to ); - - $uri = add_query_arg( 'redirect_to', $redirect_key, $uri ); - - return $uri; - } - - /** - * Set redirect key - * - * @param string $url - * - * @return string $redirect_key - */ - function um_set_redirect_url( $url ) { - - if (um_is_session_started() === false) { - session_start(); - } - - $redirect_key = wp_generate_password( 12, false ); - - $_SESSION['um_redirect_key'] = array( $redirect_key => $url ); - - return $redirect_key; - } - - /** - * Set redirect key - * - * @param string $url - * - * @return string $redirect_key - */ - function um_get_redirect_url( $key ) { - - if (um_is_session_started() === false) { - session_start(); - } - - if (isset( $_SESSION['um_redirect_key'][$key] )) { - - $url = $_SESSION['um_redirect_key'][$key]; - - return $url; +/** + * Checks if session has been started + * + * @return bool + */ +function um_is_session_started() { + if ( php_sapi_name() !== 'cli' ) { + if ( version_compare( phpversion(), '5.4.0', '>=' ) ) { + return session_status() === PHP_SESSION_ACTIVE ? true : false; } else { + return session_id() === '' ? false : true; + } + } - if (isset( $_SESSION['um_redirect_key'] )) { - foreach ($_SESSION['um_redirect_key'] as $key => $url) { + return false; +} - return $url; + +/** + * User clean basename + * + * @param $value + * + * @return mixed|void + */ +function um_clean_user_basename( $value ) { + + $raw_value = $value; + $value = str_replace( '.', ' ', $value ); + $value = str_replace( '-', ' ', $value ); + $value = str_replace( '+', ' ', $value ); + + $value = apply_filters( 'um_clean_user_basename_filter', $value, $raw_value ); + + return $value; +} + + +/** + * Convert template tags + * + * @param $content + * @param array $args + * + * @return mixed|string + */ +function um_convert_tags( $content, $args = array() ) { + $search = array( + '{display_name}', + '{first_name}', + '{last_name}', + '{gender}', + '{username}', + '{email}', + '{password}', + '{login_url}', + '{login_referrer}', + '{site_name}', + '{site_url}', + '{account_activation_link}', + '{password_reset_link}', + '{admin_email}', + '{user_profile_link}', + '{user_account_link}', + '{submitted_registration}', + '{user_avatar_url}', + ); + + $search = apply_filters( 'um_template_tags_patterns_hook', $search ); + + $replace = array( + um_user( 'display_name' ), + um_user( 'first_name' ), + um_user( 'last_name' ), + um_user( 'gender' ), + um_user( 'user_login' ), + um_user( 'user_email' ), + um_user( '_um_cool_but_hard_to_guess_plain_pw' ), + um_get_core_page( 'login' ), + um_dynamic_login_page_redirect(), + UM()->options()->get( 'site_name' ), + get_bloginfo( 'url' ), + um_user( 'account_activation_link' ), + um_user( 'password_reset_link' ), + um_admin_email(), + um_user_profile_url(), + um_get_core_page( 'account' ), + um_user_submitted_registration(), + um_get_user_avatar_url(), + ); + + $replace = apply_filters( 'um_template_tags_replaces_hook', $replace ); + + $content = wp_kses_decode_entities( str_replace( $search, $replace, $content ) ); + + if (isset( $args['tags'] ) && isset( $args['tags_replace'] )) { + $content = str_replace( $args['tags'], $args['tags_replace'], $content ); + } + + $regex = '~\{(usermeta:[^}]*)\}~'; + preg_match_all( $regex, $content, $matches ); + + // Support for all usermeta keys + if ( ! empty( $matches[1] ) && is_array( $matches[1] ) ) { + foreach ( $matches[1] as $match ) { + $strip_key = str_replace( 'usermeta:', '', $match ); + $content = str_replace( '{' . $match . '}', um_user( $strip_key ), $content ); + } + } + + return $content; +} + + +/** + * @function um_user_ip() + * + * @description This function returns the IP address of user. + * + * @usage + * + * @return string The user's IP address. + * + * @example The example below can retrieve the user's IP address + * + * + */ +function um_user_ip() { + $ip = '127.0.0.1'; + + if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { + //check ip from share internet + $ip = $_SERVER['HTTP_CLIENT_IP']; + } else if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { + //to check ip is pass from proxy + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } else if ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) { + $ip = $_SERVER['REMOTE_ADDR']; + } + + return apply_filters( 'um_user_ip', $ip ); +} + + +/** + * If conditions are met return true; + * + * @param $data + * + * @return bool + */ +function um_field_conditions_are_met( $data ) { + if (!isset( $data['conditions'] )) return true; + + $state = 1; + + foreach ($data['conditions'] as $k => $arr) { + if ($arr[0] == 'show') { + + $val = $arr[3]; + $op = $arr[2]; + + if (strstr( $arr[1], 'role_' )) + $arr[1] = 'role'; + + $field = um_profile( $arr[1] ); + + switch ($op) { + case 'equals to': + + $field = maybe_unserialize( $field ); + + if (is_array( $field )) + $state = in_array( $val, $field ) ? 1 : 0; + else + $state = ( $field == $val ) ? 1 : 0; break; - } - } - } + case 'not equals': - return; - } + $field = maybe_unserialize( $field ); + if (is_array( $field )) + $state = !in_array( $val, $field ) ? 1 : 0; + else + $state = ( $field != $val ) ? 1 : 0; - /** - * Checks if session has been started - * - * @return bool - */ - function um_is_session_started() { + break; + case 'empty': - if (php_sapi_name() !== 'cli') { - if (version_compare( phpversion(), '5.4.0', '>=' )) { - return session_status() === PHP_SESSION_ACTIVE ? true : false; - } else { - return session_id() === '' ? false : true; - } - } + $state = ( !$field ) ? 1 : 0; - return false; - } + break; + case 'not empty': + $state = ( $field ) ? 1 : 0; - /*** - *** @user clean basename - ***/ - function um_clean_user_basename( $value ) { - - $raw_value = $value; - $value = str_replace( '.', ' ', $value ); - $value = str_replace( '-', ' ', $value ); - $value = str_replace( '+', ' ', $value ); - - $value = apply_filters( 'um_clean_user_basename_filter', $value, $raw_value ); - - return $value; - } - - /*** - *** @convert template tags - ***/ - function um_convert_tags( $content, $args = array() ) { - $search = array( - '{display_name}', - '{first_name}', - '{last_name}', - '{gender}', - '{username}', - '{email}', - '{password}', - '{login_url}', - '{login_referrer}', - '{site_name}', - '{site_url}', - '{account_activation_link}', - '{password_reset_link}', - '{admin_email}', - '{user_profile_link}', - '{user_account_link}', - '{submitted_registration}', - '{user_avatar_url}', - ); - - $search = apply_filters( 'um_template_tags_patterns_hook', $search ); - - $replace = array( - um_user( 'display_name' ), - um_user( 'first_name' ), - um_user( 'last_name' ), - um_user( 'gender' ), - um_user( 'user_login' ), - um_user( 'user_email' ), - um_user( '_um_cool_but_hard_to_guess_plain_pw' ), - um_get_core_page( 'login' ), - um_dynamic_login_page_redirect(), - UM()->options()->get( 'site_name' ), - get_bloginfo( 'url' ), - um_user( 'account_activation_link' ), - um_user( 'password_reset_link' ), - um_admin_email(), - um_user_profile_url(), - um_get_core_page( 'account' ), - um_user_submitted_registration(), - um_get_user_avatar_url(), - ); - - $replace = apply_filters( 'um_template_tags_replaces_hook', $replace ); - - $content = wp_kses_decode_entities( str_replace( $search, $replace, $content ) ); - - if (isset( $args['tags'] ) && isset( $args['tags_replace'] )) { - $content = str_replace( $args['tags'], $args['tags_replace'], $content ); - } - - $regex = '~\{(usermeta:[^}]*)\}~'; - preg_match_all( $regex, $content, $matches ); - - // Support for all usermeta keys - if ( ! empty( $matches[1] ) && is_array( $matches[1] ) ) { - foreach ( $matches[1] as $match ) { - $strip_key = str_replace( 'usermeta:', '', $match ); - $content = str_replace( '{' . $match . '}', um_user( $strip_key ), $content ); - } - } - - return $content; - - } - - /** - * @function um_user_ip() - * - * @description This function returns the IP address of user. - * - * @usage - * - * @returns Returns the user's IP address. - * - * @example The example below can retrieve the user's IP address - * - * - * - * - */ - function um_user_ip() { - $ip = '127.0.0.1'; - - if (!empty( $_SERVER['HTTP_CLIENT_IP'] )) { - //check ip from share internet - $ip = $_SERVER['HTTP_CLIENT_IP']; - } else if (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'] )) { - //to check ip is pass from proxy - $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; - } else if (!empty( $_SERVER['REMOTE_ADDR'] )) { - $ip = $_SERVER['REMOTE_ADDR']; - } - - return apply_filters( 'um_user_ip', $ip ); - } - - /*** - *** @If conditions are met return true; - ***/ - function um_field_conditions_are_met( $data ) { - if (!isset( $data['conditions'] )) return true; - - $state = 1; - - foreach ($data['conditions'] as $k => $arr) { - if ($arr[0] == 'show') { - - $val = $arr[3]; - $op = $arr[2]; - - if (strstr( $arr[1], 'role_' )) - $arr[1] = 'role'; - - $field = um_profile( $arr[1] ); - - switch ($op) { - case 'equals to': - - $field = maybe_unserialize( $field ); - - if (is_array( $field )) - $state = in_array( $val, $field ) ? 1 : 0; - else - $state = ( $field == $val ) ? 1 : 0; - - break; - case 'not equals': - - $field = maybe_unserialize( $field ); - - if (is_array( $field )) - $state = !in_array( $val, $field ) ? 1 : 0; - else - $state = ( $field != $val ) ? 1 : 0; - - break; - case 'empty': - - $state = ( !$field ) ? 1 : 0; - - break; - case 'not empty': - - $state = ( $field ) ? 1 : 0; - - break; - case 'greater than': - if ($field > $val) { - $state = 1; - } else { - $state = 0; - } - break; - case 'less than': - if ($field < $val) { - $state = 1; - } else { - $state = 0; - } - break; - case 'contains': - if (strstr( $field, $val )) { - $state = 1; - } else { - $state = 0; - } - break; - } - } else if ($arr[0] == 'hide') { - - $state = 1; - $val = $arr[3]; - $op = $arr[2]; - - if (strstr( $arr[1], 'role_' )) - $arr[1] = 'role'; - - $field = um_profile( $arr[1] ); - - switch ($op) { - case 'equals to': - - $field = maybe_unserialize( $field ); - - if (is_array( $field )) - $state = in_array( $val, $field ) ? 0 : 1; - else - $state = ( $field == $val ) ? 0 : 1; - - break; - case 'not equals': - - $field = maybe_unserialize( $field ); - - if (is_array( $field )) - $state = !in_array( $val, $field ) ? 0 : 1; - else - $state = ( $field != $val ) ? 0 : 1; - - break; - case 'empty': - - $state = ( !$field ) ? 0 : 1; - - break; - case 'not empty': - - $state = ( $field ) ? 0 : 1; - - break; - case 'greater than': - if ($field <= $val) { - $state = 0; - } else { - $state = 1; - } - break; - case 'less than': - if ($field >= $val) { - $state = 0; - } else { - $state = 1; - } - break; - case 'contains': - if (strstr( $field, $val )) { - $state = 0; - } else { - $state = 1; - } - break; - } - } - } - - return ( $state ) ? true : false; - } - - /*** - *** @Exit and redirect to home - ***/ - function um_redirect_home() { - - exit( wp_redirect( home_url() ) ); - } - - - function um_js_redirect( $url ) { - if (headers_sent() || empty( $url )) { - //for blank redirects - if ('' == $url) { - $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; - } - - $funtext = "echo \"\";"; - register_shutdown_function( create_function( '', $funtext ) ); - - if (1 < ob_get_level()) { - while (ob_get_level() > 1) { - ob_end_clean(); - } - } - - ?> - - $wordCount) { - $str = implode( - '', - array_slice( - preg_split( - '/([\s,\.;\?\!]+)/', - $str, - $wordCount * 2 + 1, - PREG_SPLIT_DELIM_CAPTURE - ), - 0, - $wordCount * 2 - 1 - ) - ); - } - - return $str; - } - - /*** - *** @Get submitted user information - ***/ - function um_user_submitted_registration( $style = false ) { - $output = null; - - $data = um_user( 'submitted' ); - - if ($style) - $output .= '
'; - - if (isset( $data ) && is_array( $data )) { - - $data = apply_filters( 'um_email_registration_data', $data ); - - foreach ($data as $k => $v) { - - if (!is_array( $v ) && strstr( $v, 'ultimatemember/temp' )) { - $file = basename( $v ); - $v = um_user_uploads_uri() . $file; - } - - if (!strstr( $k, 'user_pass' ) && !in_array( $k, array( 'g-recaptcha-response', 'request', '_wpnonce', '_wp_http_referer' ) )) { - - if (is_array( $v )) { - $v = implode( ',', $v ); - } - - if ($k == 'timestamp') { - $k = __( 'date submitted', 'ultimate-member' ); - $v = date( "d M Y H:i", $v ); - } - - if ($style) { - if (!$v) $v = __( '(empty)', 'ultimate-member' ); - $output .= "

$v

"; + break; + case 'greater than': + if ($field > $val) { + $state = 1; } else { - $output .= "$k: $v" . "
"; + $state = 0; } + break; + case 'less than': + if ($field < $val) { + $state = 1; + } else { + $state = 0; + } + break; + case 'contains': + if (strstr( $field, $val )) { + $state = 1; + } else { + $state = 0; + } + break; + } + } else if ($arr[0] == 'hide') { - } + $state = 1; + $val = $arr[3]; + $op = $arr[2]; + if (strstr( $arr[1], 'role_' )) + $arr[1] = 'role'; + + $field = um_profile( $arr[1] ); + + switch ($op) { + case 'equals to': + + $field = maybe_unserialize( $field ); + + if (is_array( $field )) + $state = in_array( $val, $field ) ? 0 : 1; + else + $state = ( $field == $val ) ? 0 : 1; + + break; + case 'not equals': + + $field = maybe_unserialize( $field ); + + if (is_array( $field )) + $state = !in_array( $val, $field ) ? 0 : 1; + else + $state = ( $field != $val ) ? 0 : 1; + + break; + case 'empty': + + $state = ( !$field ) ? 0 : 1; + + break; + case 'not empty': + + $state = ( $field ) ? 0 : 1; + + break; + case 'greater than': + if ($field <= $val) { + $state = 0; + } else { + $state = 1; + } + break; + case 'less than': + if ($field >= $val) { + $state = 0; + } else { + $state = 1; + } + break; + case 'contains': + if (strstr( $field, $val )) { + $state = 0; + } else { + $state = 1; + } + break; + } + } + } + + return ( $state ) ? true : false; +} + + +/** + * Exit and redirect to home + */ +function um_redirect_home() { + exit( wp_redirect( home_url() ) ); +} + + +/** + * @param $url + */ +function um_js_redirect( $url ) { + if (headers_sent() || empty( $url )) { + //for blank redirects + if ('' == $url) { + $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; + } + + $funtext = "echo \"\";"; + register_shutdown_function( create_function( '', $funtext ) ); + + if (1 < ob_get_level()) { + while (ob_get_level() > 1) { + ob_end_clean(); } } - if ($style) - $output .= '
'; + ?> + + $wordCount) { + $str = implode( + '', + array_slice( + preg_split( + '/([\s,\.;\?\!]+)/', + $str, + $wordCount * 2 + 1, + PREG_SPLIT_DELIM_CAPTURE + ), + 0, + $wordCount * 2 - 1 + ) + ); } - /*** - *** @Show filtered social link - ***/ - function um_filtered_social_link( $key, $match ) { - $value = um_profile( $key ); - $submatch = str_replace( 'https://', '', $match ); - $submatch = str_replace( 'http://', '', $submatch ); - if (strstr( $value, $submatch )) { - $value = 'https://' . $value; - } else if (strpos( $value, 'http' ) !== 0) { - $value = $match . $value; + return $str; +} + + +/** + * Get submitted user information + * + * @param bool $style + * + * @return null|string + */ +function um_user_submitted_registration( $style = false ) { + $output = null; + + $data = um_user( 'submitted' ); + + if ($style) + $output .= '
'; + + if (isset( $data ) && is_array( $data )) { + + $data = apply_filters( 'um_email_registration_data', $data ); + + foreach ($data as $k => $v) { + + if (!is_array( $v ) && strstr( $v, 'ultimatemember/temp' )) { + $file = basename( $v ); + $v = um_user_uploads_uri() . $file; + } + + if (!strstr( $k, 'user_pass' ) && !in_array( $k, array( 'g-recaptcha-response', 'request', '_wpnonce', '_wp_http_referer' ) )) { + + if (is_array( $v )) { + $v = implode( ',', $v ); + } + + if ($k == 'timestamp') { + $k = __( 'date submitted', 'ultimate-member' ); + $v = date( "d M Y H:i", $v ); + } + + if ($style) { + if (!$v) $v = __( '(empty)', 'ultimate-member' ); + $output .= "

$v

"; + } else { + $output .= "$k: $v" . "
"; + } + + } + } - $value = str_replace( 'https://https://', 'https://', $value ); - $value = str_replace( 'http://https://', 'https://', $value ); - $value = str_replace( 'https://http://', 'https://', $value ); - - return $value; } - /** - * Get filtered meta value after applying hooks - * - * @param $key - * @param bool $data - * @return mixed|string|void - */ - function um_filtered_value( $key, $data = false ) { - $value = um_user( $key ); + if ($style) + $output .= '
'; - if ( ! $data ) { - $data = UM()->builtin()->get_specific_field( $key ); - } + return $output; +} - $type = ( isset( $data['type'] ) ) ? $data['type'] : ''; - $value = apply_filters( "um_profile_field_filter_hook__", $value, $data, $type ); - $value = apply_filters( "um_profile_field_filter_hook__{$key}", $value, $data ); - $value = apply_filters( "um_profile_field_filter_hook__{$type}", $value, $data ); +/** + * Show filtered social link + * + * @param $key + * @param $match + * + * @return mixed|string|void + */ +function um_filtered_social_link( $key, $match ) { + $value = um_profile( $key ); + $submatch = str_replace( 'https://', '', $match ); + $submatch = str_replace( 'http://', '', $submatch ); + if (strstr( $value, $submatch )) { + $value = 'https://' . $value; + } else if (strpos( $value, 'http' ) !== 0) { + $value = $match . $value; + } + $value = str_replace( 'https://https://', 'https://', $value ); + $value = str_replace( 'http://https://', 'https://', $value ); + $value = str_replace( 'https://http://', 'https://', $value ); - return $value; + return $value; +} + + +/** + * Get filtered meta value after applying hooks + * + * @param $key + * @param bool $data + * @return mixed|string|void + */ +function um_filtered_value( $key, $data = false ) { + $value = um_user( $key ); + + if ( ! $data ) { + $data = UM()->builtin()->get_specific_field( $key ); } + $type = ( isset( $data['type'] ) ) ? $data['type'] : ''; - function um_profile_id() { + $value = apply_filters( "um_profile_field_filter_hook__", $value, $data, $type ); + $value = apply_filters( "um_profile_field_filter_hook__{$key}", $value, $data ); + $value = apply_filters( "um_profile_field_filter_hook__{$type}", $value, $data ); - if (um_get_requested_user()) { - return um_get_requested_user(); - } else if (is_user_logged_in() && get_current_user_id()) { - return get_current_user_id(); - } + return $value; +} - return 0; + +/** + * @return bool|int|null + */ +function um_profile_id() { + + if ( um_get_requested_user() ) { + return um_get_requested_user(); + } else if (is_user_logged_in() && get_current_user_id()) { + return get_current_user_id(); } - /*** - *** @Check that temp upload is valid - ***/ - function um_is_temp_upload( $url ) { + return 0; +} - if (filter_var( $url, FILTER_VALIDATE_URL ) === false) - $url = realpath( $url ); - if (!$url) +/** + * Check that temp upload is valid + * + * @param $url + * + * @return bool|string + */ +function um_is_temp_upload( $url ) { + + if (filter_var( $url, FILTER_VALIDATE_URL ) === false) + $url = realpath( $url ); + + if (!$url) + return false; + + $url = explode( '/ultimatemember/temp/', $url ); + if (isset( $url[1] )) { + + if (strstr( $url[1], '../' ) || strstr( $url[1], '%' )) return false; - $url = explode( '/ultimatemember/temp/', $url ); - if (isset( $url[1] )) { + $src = UM()->files()->upload_temp . $url[1]; + if (!file_exists( $src )) + return false; - if (strstr( $url[1], '../' ) || strstr( $url[1], '%' )) - return false; + return $src; + } - $src = UM()->files()->upload_temp . $url[1]; - if (!file_exists( $src )) - return false; + return false; +} + +/** + * Check that temp image is valid + * + * @param $url + * + * @return bool|string + */ +function um_is_temp_image( $url ) { + $url = explode( '/ultimatemember/temp/', $url ); + if (isset( $url[1] )) { + $src = UM()->files()->upload_temp . $url[1]; + if (!file_exists( $src )) + return false; + list( $width, $height, $type, $attr ) = @getimagesize( $src ); + if (isset( $width ) && isset( $height )) return $src; - } - - return false; } - /*** - *** @Check that temp image is valid - ***/ - function um_is_temp_image( $url ) { - $url = explode( '/ultimatemember/temp/', $url ); - if (isset( $url[1] )) { - $src = UM()->files()->upload_temp . $url[1]; - if (!file_exists( $src )) - return false; - list( $width, $height, $type, $attr ) = @getimagesize( $src ); - if (isset( $width ) && isset( $height )) - return $src; - } - - return false; - } + return false; +} - /** - * Get a translated core page URL - * - * @param $post_id - * @param $language - * @return bool|false|string - */ - function um_get_url_for_language( $post_id, $language ) { - if ( ! um_is_wpml_active() ) - return ''; - - $lang_post_id = icl_object_id( $post_id, 'page', true, $language ); - - if ( $lang_post_id != 0 ) { - $url = get_permalink( $lang_post_id ); - } else { - // No page found, it's most likely the homepage - global $sitepress; - $url = $sitepress->language_url( $language ); - } - - return $url; - } - - - /** - * Check if WPML is active - * - * @return bool|mixed - */ - function um_is_wpml_active() { - if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { - global $sitepress; - - return $sitepress->get_setting( 'setup_complete' ); - } - - return false; - } - - /*** - *** @Get core page url - ***/ - function um_time_diff( $time1, $time2 ) { - return UM()->datetime()->time_diff( $time1, $time2 ); - } - - /*** - *** @Get user's last login timestamp - ***/ - function um_user_last_login_timestamp( $user_id ) { - $value = get_user_meta( $user_id, '_um_last_login', true ); - if ($value) - return $value; - +/** + * Get a translated core page URL + * + * @param $post_id + * @param $language + * @return bool|false|string + */ +function um_get_url_for_language( $post_id, $language ) { + if ( ! um_is_wpml_active() ) return ''; + + $lang_post_id = icl_object_id( $post_id, 'page', true, $language ); + + if ( $lang_post_id != 0 ) { + $url = get_permalink( $lang_post_id ); + } else { + // No page found, it's most likely the homepage + global $sitepress; + $url = $sitepress->language_url( $language ); } - /*** - *** @Get user's last login time - ***/ - function um_user_last_login_date( $user_id ) { - $value = get_user_meta( $user_id, '_um_last_login', true ); - if ($value) - return date_i18n( 'F d, Y', $value ); + return $url; +} - return ''; + +/** + * Check if WPML is active + * + * @return bool|mixed + */ +function um_is_wpml_active() { + if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { + global $sitepress; + + return $sitepress->get_setting( 'setup_complete' ); } - /*** - *** @Get user's last login (time diff) - ***/ - function um_user_last_login( $user_id ) { - $value = get_user_meta( $user_id, '_um_last_login', true ); - if ($value) { - $value = um_time_diff( $value, current_time( 'timestamp' ) ); - } else { - $value = ''; - } + return false; +} + +/** + * Get core page url + * + * @param $time1 + * @param $time2 + * + * @return mixed|void + */ +function um_time_diff( $time1, $time2 ) { + return UM()->datetime()->time_diff( $time1, $time2 ); +} + + +/** + * Get user's last login timestamp + * + * @param $user_id + * + * @return mixed|string + */ +function um_user_last_login_timestamp( $user_id ) { + $value = get_user_meta( $user_id, '_um_last_login', true ); + if ($value) return $value; - } - /*** - *** @Get core page url - ***/ - function um_get_core_page( $slug, $updated = false ) { - $url = ''; - - if (isset( UM()->config()->permalinks[$slug] )) { - $url = get_permalink( UM()->config()->permalinks[$slug] ); - if ($updated) - $url = add_query_arg( 'updated', esc_attr( $updated ), $url ); - } - - if (function_exists( 'icl_get_current_language' ) && icl_get_current_language() != icl_get_default_language()) { - - $url = um_get_url_for_language( UM()->config()->permalinks[$slug], icl_get_current_language() ); - - if (get_post_meta( get_the_ID(), '_um_wpml_account', true ) == 1) { - $url = get_permalink( get_the_ID() ); - } - if (get_post_meta( get_the_ID(), '_um_wpml_user', true ) == 1) { - $url = um_get_url_for_language( UM()->config()->permalinks[$slug], icl_get_current_language() ); - } - } - - if ($url) { - $url = apply_filters( 'um_get_core_page_filter', $url, $slug, $updated ); - - return $url; - } - - return ''; - } - - /*** - *** @boolean check if we are on UM page - ***/ - function is_ultimatemember() { - global $post; - if (isset( $post->ID ) && in_array( $post->ID, UM()->config()->permalinks )) - return true; - - return false; - } + return ''; +} - /** - * Check if we are on a UM Core Page or not - * - * Default um core pages slugs - * 'user', 'login', 'register', 'members', 'logout', 'account', 'password-reset' - * - * @param string $page UM core page slug - * - * @return bool - */ - function um_is_core_page( $page ) { - global $post; - - if (isset( $post->ID ) && isset( UM()->config()->permalinks[$page] ) && $post->ID == UM()->config()->permalinks[$page]) - return true; - if (isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $page, true ) == 1) - return true; - - if (isset( $post->ID )) { - $_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true ); - - if (isset( UM()->config()->permalinks[$page] ) && ( ( $_icl_lang_duplicate_of == UM()->config()->permalinks[$page] && !empty( $_icl_lang_duplicate_of ) ) || UM()->config()->permalinks[$page] == $post->ID )) - return true; - } - - return false; - } - - - function um_is_core_post( $post, $core_page ) { - if (isset( $post->ID ) && isset( UM()->config()->permalinks[$core_page] ) && $post->ID == UM()->config()->permalinks[$core_page]) - return true; - if (isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $core_page, true ) == 1) - return true; - - if (isset( $post->ID )) { - $_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true ); - - if (isset( UM()->config()->permalinks[$core_page] ) && ( ( $_icl_lang_duplicate_of == UM()->config()->permalinks[$core_page] && !empty( $_icl_lang_duplicate_of ) ) || UM()->config()->permalinks[$core_page] == $post->ID )) - return true; - } - - return false; - } - - /*** - *** @Is core URL - ***/ - function um_is_core_uri() { - $array = UM()->config()->permalinks; - $current_url = UM()->permalinks()->get_current_url( get_option( 'permalink_structure' ) ); - - if (!isset( $array ) || !is_array( $array )) return false; - - foreach ($array as $k => $id) { - $page_url = get_permalink( $id ); - if (strstr( $current_url, $page_url )) - return true; - } - - return false; - } - - /*** - *** @Check value of queried search in text input - ***/ - function um_queried_search_value( $filter, $echo = true ) { +/** + * Get user's last login (time diff) + * + * @param $user_id + * + * @return mixed|string|void + */ +function um_user_last_login( $user_id ) { + $value = get_user_meta( $user_id, '_um_last_login', true ); + if ( $value ) { + $value = um_time_diff( $value, current_time( 'timestamp' ) ); + } else { $value = ''; - if (isset( $_REQUEST['um_search'] )) { - $query = UM()->permalinks()->get_query_array(); - if (isset( $query[$filter] ) && $query[$filter] != '') { - $value = stripslashes_deep( $query[$filter] ); - } + } + + return $value; +} + + +/** + * Get core page url + * + * @param $slug + * @param bool $updated + * + * @return bool|false|mixed|string|void + */ +function um_get_core_page( $slug, $updated = false ) { + $url = ''; + + if (isset( UM()->config()->permalinks[$slug] )) { + $url = get_permalink( UM()->config()->permalinks[$slug] ); + if ($updated) + $url = add_query_arg( 'updated', esc_attr( $updated ), $url ); + } + + if (function_exists( 'icl_get_current_language' ) && icl_get_current_language() != icl_get_default_language()) { + + $url = um_get_url_for_language( UM()->config()->permalinks[$slug], icl_get_current_language() ); + + if (get_post_meta( get_the_ID(), '_um_wpml_account', true ) == 1) { + $url = get_permalink( get_the_ID() ); } - - if ($echo) { - echo $value; - - return ''; - } else { - return $value; + if (get_post_meta( get_the_ID(), '_um_wpml_user', true ) == 1) { + $url = um_get_url_for_language( UM()->config()->permalinks[$slug], icl_get_current_language() ); } - } - /*** - *** @Check whether item in dropdown is selected in query-url - ***/ - function um_select_if_in_query_params( $filter, $val ) { - $selected = false; - - if (isset( $_REQUEST['um_search'] )) { - $query = UM()->permalinks()->get_query_array(); - - if (isset( $query[$filter] ) && $val == $query[$filter]) - $selected = true; - - $selected = apply_filters( 'um_selected_if_in_query_params', $selected, $filter, $val ); - } - - echo $selected ? 'selected="selected"' : ''; - } - - /*** - *** @get styling defaults - ***/ - function um_styling_defaults( $mode ) { - - $new_arr = array(); - $core_form_meta_all = UM()->config()->core_form_meta_all; - $core_global_meta_all = UM()->config()->core_global_meta_all; - - foreach ( $core_form_meta_all as $k => $v ) { - $s = str_replace( $mode . '_', '', $k ); - if (strstr( $k, '_um_' . $mode . '_' ) && !in_array( $s, $core_global_meta_all )) { - $a = str_replace( '_um_' . $mode . '_', '', $k ); - $b = str_replace( '_um_', '', $k ); - $new_arr[$a] = UM()->options()->get( $b ); - } else if (in_array( $k, $core_global_meta_all )) { - $a = str_replace( '_um_', '', $k ); - $new_arr[$a] = UM()->options()->get( $a ); - } - } - - return $new_arr; - } - - /*** - *** @get meta option default - ***/ - function um_get_metadefault( $id ) { - $core_form_meta_all = UM()->config()->core_form_meta_all; - - return isset( $core_form_meta_all['_um_' . $id] ) ? $core_form_meta_all['_um_' . $id] : ''; - } - - /*** - *** @check if a legitimate password reset request is in action - ***/ - function um_requesting_password_reset() { - if (um_is_core_page( 'password-reset' ) && isset( $_POST['_um_password_reset'] ) == 1) - return true; - - return false; - } - - /*** - *** @check if a legitimate password change request is in action - ***/ - function um_requesting_password_change() { - if (um_is_core_page( 'account' ) && isset( $_POST['_um_account'] ) == 1) - return true; - else if (isset( $_POST['_um_password_change'] ) && $_POST['_um_password_change'] == 1) - return true; - - return false; - } - - /*** - *** @boolean for account page editing - ***/ - function um_submitting_account_page() { - if (isset( $_POST['_um_account'] ) && $_POST['_um_account'] == 1 && is_user_logged_in()) - return true; - - return false; - } - - /*** - *** @get a user's display name - ***/ - function um_get_display_name( $user_id ) { - um_fetch_user( $user_id ); - $name = um_user( 'display_name' ); - um_reset_user(); - - return $name; - } - - /*** - *** @get members to show in directory - ***/ - function um_members( $argument ) { - return UM()->members()->results[$argument]; - } - - /** - * @function um_reset_user_clean() - * - * @description This function is similar to um_reset_user() with a difference that it will not use the logged-in - * user data after resetting. It is a hard-reset function for all user data. - * - * @usage - * - * @returns Clears the user data. You need to fetch a user manually after using this function. - * - * @example You can reset user data by using the following line in your code - * - * - * - * - */ - function um_reset_user_clean() { - UM()->user()->reset( true ); - } - - /** - * @function um_reset_user() - * - * @description This function resets the current user. You can use it to reset user data after - * retrieving the details of a specific user. - * - * @usage - * - * @returns Clears the user data. If a user is logged in, the user data will be reset to that user's data - * - * @example You can reset user data by using the following line in your code - * - * - * - * - */ - function um_reset_user() { - UM()->user()->reset(); - } - - /*** - *** @gets the queried user - ***/ - function um_queried_user() { - - return get_query_var( 'um_user' ); - } - - /*** - *** @Sets the requested user - ***/ - function um_set_requested_user( $user_id ) { - UM()->user()->target_id = $user_id; - } - - /*** - *** @Gets the requested user - ***/ - function um_get_requested_user() { - if (!empty( UM()->user()->target_id )) - return UM()->user()->target_id; - - return false; - } - - /*** - *** @remove edit profile args from url - ***/ - function um_edit_my_profile_cancel_uri( $url = '' ) { - - if (empty( $url )) { - $url = remove_query_arg( 'um_action' ); - $url = remove_query_arg( 'profiletab', $url ); - $url = add_query_arg( 'profiletab', 'main', $url ); - } - - $url = apply_filters( 'um_edit_profile_cancel_uri', $url ); + if ($url) { + $url = apply_filters( 'um_get_core_page_filter', $url, $slug, $updated ); return $url; } - /*** - *** @boolean for profile edit page - ***/ - function um_is_on_edit_profile() { - if (isset( $_REQUEST['profiletab'] ) && isset( $_REQUEST['um_action'] )) { - if ($_REQUEST['profiletab'] == 'main' && $_REQUEST['um_action'] == 'edit') { - return true; - } - } + return ''; +} - return false; - } - /*** - *** @can view field - ***/ - function um_can_view_field( $data ) { - - if (!isset( UM()->fields()->set_mode )) - UM()->fields()->set_mode = ''; - - if (isset( $data['public'] ) && UM()->fields()->set_mode != 'register') { - - if (!is_user_logged_in() && $data['public'] != '1') return false; - - if (is_user_logged_in()) { - - if ($data['public'] == '-3' && !um_is_user_himself() && !in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] )) - return false; - - if (!um_is_user_himself() && $data['public'] == '-1' && !UM()->roles()->um_user_can( 'can_edit_everyone' )) - return false; - - if ($data['public'] == '-2' && $data['roles']) - if (!in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] )) - return false; - } - - } +/** + * Check if we are on a UM Core Page or not + * + * Default um core pages slugs + * 'user', 'login', 'register', 'members', 'logout', 'account', 'password-reset' + * + * @param string $page UM core page slug + * + * @return bool + */ +function um_is_core_page( $page ) { + global $post; + if (isset( $post->ID ) && isset( UM()->config()->permalinks[$page] ) && $post->ID == UM()->config()->permalinks[$page]) + return true; + if (isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $page, true ) == 1) return true; - } - /*** - *** @checks if user can view profile - ***/ - function um_can_view_profile( $user_id ) { - if (!um_user( 'can_view_all' ) && $user_id != get_current_user_id() && is_user_logged_in()) return false; + if (isset( $post->ID )) { + $_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true ); - if (UM()->roles()->um_current_user_can( 'edit', $user_id )) { + if (isset( UM()->config()->permalinks[$page] ) && ( ( $_icl_lang_duplicate_of == UM()->config()->permalinks[$page] && !empty( $_icl_lang_duplicate_of ) ) || UM()->config()->permalinks[$page] == $post->ID )) return true; - } + } - if (!is_user_logged_in()) { - if (UM()->user()->is_private_profile( $user_id )) { - return false; - } else { - return true; - } - } + return false; +} - if (!um_user( 'can_access_private_profile' ) && UM()->user()->is_private_profile( $user_id )) return false; - - if (UM()->roles()->um_user_can( 'can_view_roles' ) && $user_id != get_current_user_id()) { - if (!in_array( UM()->roles()->um_get_user_role( $user_id ), UM()->roles()->um_user_can( 'can_view_roles' ) )) { - return false; - } - } +/** + * @param $post + * @param $core_page + * + * @return bool + */ +function um_is_core_post( $post, $core_page ) { + if (isset( $post->ID ) && isset( UM()->config()->permalinks[$core_page] ) && $post->ID == UM()->config()->permalinks[$core_page]) + return true; + if (isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $core_page, true ) == 1) return true; + if (isset( $post->ID )) { + $_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true ); + + if (isset( UM()->config()->permalinks[$core_page] ) && ( ( $_icl_lang_duplicate_of == UM()->config()->permalinks[$core_page] && !empty( $_icl_lang_duplicate_of ) ) || UM()->config()->permalinks[$core_page] == $post->ID )) + return true; } - /*** - *** @boolean check for not same user - ***/ - function um_is_user_himself() { - if (um_get_requested_user() && um_get_requested_user() != get_current_user_id()) - return false; + return false; +} - return true; - } - - /*** - *** @can edit field - ***/ - function um_can_edit_field( $data ) { - if (isset( UM()->fields()->editing ) && UM()->fields()->editing == true && - isset( UM()->fields()->set_mode ) && UM()->fields()->set_mode == 'profile' - ) { - - if (is_user_logged_in() && isset( $data['editable'] ) && $data['editable'] == 0) { - - if (isset( $data['public'] ) && $data['public'] == "-2") { - return true; - } - - if (um_user( 'can_edit_everyone' )) return true; - if (um_is_user_himself() && !um_user( 'can_edit_everyone' )) { - return true; - } - - if (!um_is_user_himself() && !UM()->roles()->um_user_can( 'can_edit_everyone' )) - return false; - } +/** + * Check value of queried search in text input + * + * @param $filter + * @param bool $echo + * + * @return mixed|string + */ +function um_queried_search_value( $filter, $echo = true ) { + $value = ''; + if (isset( $_REQUEST['um_search'] )) { + $query = UM()->permalinks()->get_query_array(); + if (isset( $query[$filter] ) && $query[$filter] != '') { + $value = stripslashes_deep( $query[$filter] ); } - - return true; - } + if ($echo) { + echo $value; - /*** - *** @Check if user is in his profile - ***/ - function um_is_myprofile() { - if (get_current_user_id() && get_current_user_id() == um_get_requested_user()) return true; - if (!um_get_requested_user() && um_is_core_page( 'user' ) && get_current_user_id()) return true; - - return false; - } - - - /*** - *** @Returns the edit profile link - ***/ - function um_edit_profile_url() { - if (um_is_core_page( 'user' )) { - $url = UM()->permalinks()->get_current_url(); - } else { - $url = um_user_profile_url(); - } - - $url = remove_query_arg( 'profiletab', $url ); - $url = remove_query_arg( 'subnav', $url ); - $url = add_query_arg( 'profiletab', 'main', $url ); - $url = add_query_arg( 'um_action', 'edit', $url ); - - return $url; - } - - /*** - *** @checks if user can edit his profile - ***/ - function um_can_edit_my_profile() { - if (!is_user_logged_in()) return false; - if (!um_user( 'can_edit_profile' )) return false; - - return true; - } - - /*** - *** @short for admin e-mail - ***/ - function um_admin_email() { - return UM()->options()->get( 'admin_email' ); - } - - - /*** - *** @Display a link to profile page - ***/ - function um_user_profile_url() { - return UM()->user()->get_profile_url( um_user( 'ID' ) ); - } - - /*** - *** @Get all UM roles in array - ***/ - function um_get_roles() { - return UM()->roles()->get_roles(); - } - - /** - * @function um_fetch_user() - * - * @description This function sets a user and allow you to retrieve any information for the retrieved user - * - * @usage - * - * @param $user_id (numeric) (required) A user ID is required. This is the user's ID that you wish to set/retrieve - * - * @returns Sets a specific user and prepares profile data and user permissions and makes them accessible. - * - * @example The example below will set user ID 5 prior to retrieving his profile information. - * - * - * - * @example In the following example you can fetch the profile of a logged-in user dynamically. - * - * - * - */ - function um_fetch_user( $user_id ) { - UM()->user()->set( $user_id ); - } - - /*** - *** @Load profile key - ***/ - function um_profile( $key ) { - - if (!empty( UM()->user()->profile[$key] )) { - $value = apply_filters( "um_profile_{$key}__filter", UM()->user()->profile[$key] ); - } else { - $value = apply_filters( "um_profile_{$key}_empty__filter", false ); - } - + return ''; + } else { return $value; + } + +} + + +/** + * Check whether item in dropdown is selected in query-url + * + * @param $filter + * @param $val + */ +function um_select_if_in_query_params( $filter, $val ) { + $selected = false; + + if (isset( $_REQUEST['um_search'] )) { + $query = UM()->permalinks()->get_query_array(); + + if (isset( $query[$filter] ) && $val == $query[$filter]) + $selected = true; + + $selected = apply_filters( 'um_selected_if_in_query_params', $selected, $filter, $val ); + } + + echo $selected ? 'selected="selected"' : ''; +} + + +/** + * Get styling defaults + * + * @param $mode + * + * @return array + */ +function um_styling_defaults( $mode ) { + + $new_arr = array(); + $core_form_meta_all = UM()->config()->core_form_meta_all; + $core_global_meta_all = UM()->config()->core_global_meta_all; + + foreach ( $core_form_meta_all as $k => $v ) { + $s = str_replace( $mode . '_', '', $k ); + if (strstr( $k, '_um_' . $mode . '_' ) && !in_array( $s, $core_global_meta_all )) { + $a = str_replace( '_um_' . $mode . '_', '', $k ); + $b = str_replace( '_um_', '', $k ); + $new_arr[$a] = UM()->options()->get( $b ); + } else if (in_array( $k, $core_global_meta_all )) { + $a = str_replace( '_um_', '', $k ); + $new_arr[$a] = UM()->options()->get( $a ); + } + } + + return $new_arr; +} + + +/** + * Get meta option default + * + * @param $id + * + * @return string + */ +function um_get_metadefault( $id ) { + $core_form_meta_all = UM()->config()->core_form_meta_all; + + return isset( $core_form_meta_all['_um_' . $id] ) ? $core_form_meta_all['_um_' . $id] : ''; +} + + +/** + * Check if a legitimate password reset request is in action + * + * @return bool + */ +function um_requesting_password_reset() { + if (um_is_core_page( 'password-reset' ) && isset( $_POST['_um_password_reset'] ) == 1) + return true; + + return false; +} + + +/** + * Check if a legitimate password change request is in action + * + * + * @return bool + */ +function um_requesting_password_change() { + if (um_is_core_page( 'account' ) && isset( $_POST['_um_account'] ) == 1) + return true; + else if (isset( $_POST['_um_password_change'] ) && $_POST['_um_password_change'] == 1) + return true; + + return false; +} + + +/** + * boolean for account page editing + * + * @return bool + */ +function um_submitting_account_page() { + if (isset( $_POST['_um_account'] ) && $_POST['_um_account'] == 1 && is_user_logged_in()) + return true; + + return false; +} + + +/** + * Get a user's display name + * + * @param $user_id + * + * @return string + */ +function um_get_display_name( $user_id ) { + um_fetch_user( $user_id ); + $name = um_user( 'display_name' ); + um_reset_user(); + + return $name; +} + + +/** + * Get members to show in directory + * + * @param $argument + * + * @return mixed + */ +function um_members( $argument ) { + return UM()->members()->results[ $argument ]; +} + + +/** + * Clears the user data. You need to fetch a user manually after using this function. + * + * @function um_reset_user_clean() + * + * @description This function is similar to um_reset_user() with a difference that it will not use the logged-in + * user data after resetting. It is a hard-reset function for all user data. + * + * @usage + * + * @example You can reset user data by using the following line in your code + * + * + */ +function um_reset_user_clean() { + UM()->user()->reset( true ); +} + + +/** + * Clears the user data. If a user is logged in, the user data will be reset to that user's data + * + * @function um_reset_user() + * + * @description This function resets the current user. You can use it to reset user data after + * retrieving the details of a specific user. + * + * @usage + * + * @example You can reset user data by using the following line in your code + * + * + */ +function um_reset_user() { + UM()->user()->reset(); +} + + +/** + * Gets the queried user + * + * @return mixed + */ +function um_queried_user() { + return get_query_var( 'um_user' ); +} + + +/** + * Sets the requested user + * + * @param $user_id + */ +function um_set_requested_user( $user_id ) { + UM()->user()->target_id = $user_id; +} + + +/** + * Gets the requested user + * + * @return bool|null + */ +function um_get_requested_user() { + if ( ! empty( UM()->user()->target_id ) ) + return UM()->user()->target_id; + + return false; +} + + +/** + * Remove edit profile args from url + * + * @param string $url + * + * @return mixed|string|void + */ +function um_edit_my_profile_cancel_uri( $url = '' ) { + + if ( empty( $url ) ) { + $url = remove_query_arg( 'um_action' ); + $url = remove_query_arg( 'profiletab', $url ); + $url = add_query_arg( 'profiletab', 'main', $url ); + } + + $url = apply_filters( 'um_edit_profile_cancel_uri', $url ); + + return $url; +} + + +/** + * boolean for profile edit page + * + * @return bool + */ +function um_is_on_edit_profile() { + if (isset( $_REQUEST['profiletab'] ) && isset( $_REQUEST['um_action'] )) { + if ($_REQUEST['profiletab'] == 'main' && $_REQUEST['um_action'] == 'edit') { + return true; + } + } + + return false; +} + + +/** + * Can view field + * + * @param $data + * + * @return bool + */ +function um_can_view_field( $data ) { + + if (!isset( UM()->fields()->set_mode )) + UM()->fields()->set_mode = ''; + + if (isset( $data['public'] ) && UM()->fields()->set_mode != 'register') { + + if (!is_user_logged_in() && $data['public'] != '1') return false; + + if (is_user_logged_in()) { + + if ($data['public'] == '-3' && !um_is_user_himself() && !in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] )) + return false; + + if (!um_is_user_himself() && $data['public'] == '-1' && !UM()->roles()->um_user_can( 'can_edit_everyone' )) + return false; + + if ($data['public'] == '-2' && $data['roles']) + if (!in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] )) + return false; + } } - /*** - *** @Get youtube video ID from url - ***/ - function um_youtube_id_from_url( $url ) { - $pattern = - '%^# Match any youtube URL - (?:https?://)? # Optional scheme. Either http or https - (?:www\.)? # Optional www subdomain - (?: # Group host alternatives - youtu\.be/ # Either youtu.be, - | youtube\.com # or youtube.com - (?: # Group path alternatives - /embed/ # Either /embed/ - | /v/ # or /v/ - | /watch\?v= # or /watch\?v= - ) # End path alternatives. - ) # End host alternatives. - ([\w-]{10,12}) # Allow 10-12 for 11 char youtube id. - $%x'; - $result = preg_match( $pattern, $url, $matches ); - if (false !== $result) { - return $matches[1]; - } + return true; +} + +/** + * Checks if user can view profile + * + * @param $user_id + * + * @return bool + */ +function um_can_view_profile( $user_id ) { + if (!um_user( 'can_view_all' ) && $user_id != get_current_user_id() && is_user_logged_in()) return false; + + if (UM()->roles()->um_current_user_can( 'edit', $user_id )) { + return true; + } + + if (!is_user_logged_in()) { + if (UM()->user()->is_private_profile( $user_id )) { + return false; + } else { + return true; + } + } + + if (!um_user( 'can_access_private_profile' ) && UM()->user()->is_private_profile( $user_id )) return false; + + if (UM()->roles()->um_user_can( 'can_view_roles' ) && $user_id != get_current_user_id()) { + if (!in_array( UM()->roles()->um_get_user_role( $user_id ), UM()->roles()->um_user_can( 'can_view_roles' ) )) { + return false; + } + } + + return true; + +} + + +/** + * boolean check for not same user + * + * @return bool + */ +function um_is_user_himself() { + if (um_get_requested_user() && um_get_requested_user() != get_current_user_id()) return false; - } - /*** - *** @user uploads uri - ***/ - function um_user_uploads_uri() { - if (is_ssl()) { - UM()->files()->upload_baseurl = str_replace( "http://", "https://", UM()->files()->upload_baseurl ); + return true; +} + +/** + * Can edit field + * + * @param $data + * + * @return bool + */ +function um_can_edit_field( $data ) { + if (isset( UM()->fields()->editing ) && UM()->fields()->editing == true && + isset( UM()->fields()->set_mode ) && UM()->fields()->set_mode == 'profile' + ) { + + if (is_user_logged_in() && isset( $data['editable'] ) && $data['editable'] == 0) { + + if (isset( $data['public'] ) && $data['public'] == "-2") { + return true; + } + + if (um_user( 'can_edit_everyone' )) return true; + if (um_is_user_himself() && !um_user( 'can_edit_everyone' )) { + return true; + } + + if (!um_is_user_himself() && !UM()->roles()->um_user_can( 'can_edit_everyone' )) + return false; } - $uri = UM()->files()->upload_baseurl . um_user( 'ID' ) . '/'; - - return $uri; } - /*** - *** @user uploads directory - ***/ - function um_user_uploads_dir() { - $uri = UM()->files()->upload_basedir . um_user( 'ID' ) . '/'; + return true; - return $uri; +} + + +/** + * Check if user is in his profile + * + * @return bool + */ +function um_is_myprofile() { + if (get_current_user_id() && get_current_user_id() == um_get_requested_user()) return true; + if (!um_get_requested_user() && um_is_core_page( 'user' ) && get_current_user_id()) return true; + + return false; +} + + +/** + * Returns the edit profile link + * + * @return mixed|string|void + */ +function um_edit_profile_url() { + if (um_is_core_page( 'user' )) { + $url = UM()->permalinks()->get_current_url(); + } else { + $url = um_user_profile_url(); } - /*** - *** @find closest number in an array - ***/ - function um_closest_num( $array, $number ) { - sort( $array ); - foreach ($array as $a) { - if ($a >= $number) return $a; - } + $url = remove_query_arg( 'profiletab', $url ); + $url = remove_query_arg( 'subnav', $url ); + $url = add_query_arg( 'profiletab', 'main', $url ); + $url = add_query_arg( 'um_action', 'edit', $url ); - return end( $array ); + return $url; +} + + +/** + * checks if user can edit his profile + * + * @return bool + */ +function um_can_edit_my_profile() { + if (!is_user_logged_in()) return false; + if (!um_user( 'can_edit_profile' )) return false; + + return true; +} + + +/** + * Short for admin e-mail + * + * @return mixed|string|void + */ +function um_admin_email() { + return UM()->options()->get( 'admin_email' ); +} + + +/** + * Display a link to profile page + * + * @return string + */ +function um_user_profile_url() { + return UM()->user()->get_profile_url( um_user( 'ID' ) ); +} + + +/** + * Get all UM roles in array + * + * @return array + */ +function um_get_roles() { + return UM()->roles()->get_roles(); +} + + +/** + * Sets a specific user and prepares profile data and user permissions and makes them accessible. + * + * @function um_fetch_user() + * + * @description This function sets a user and allow you to retrieve any information for the retrieved user + * + * @usage + * + * @param $user_id (numeric) (required) A user ID is required. This is the user's ID that you wish to set/retrieve + * + * + * @example The example below will set user ID 5 prior to retrieving his profile information. + * + * + * + * @example In the following example you can fetch the profile of a logged-in user dynamically. + * + * + * + */ +function um_fetch_user( $user_id ) { + UM()->user()->set( $user_id ); +} + + +/** + * Load profile key + * + * @param $key + * + * @return mixed|void + */ +function um_profile( $key ) { + + if (!empty( UM()->user()->profile[$key] )) { + $value = apply_filters( "um_profile_{$key}__filter", UM()->user()->profile[$key] ); + } else { + $value = apply_filters( "um_profile_{$key}_empty__filter", false ); } - /*** - *** @get cover uri - ***/ - function um_get_cover_uri( $image, $attrs ) { - $uri = false; - $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION ); - if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . '/cover_photo' . $ext )) { - $uri = um_user_uploads_uri() . 'cover_photo' . $ext . '?' . current_time( 'timestamp' ); - } - if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . '/cover_photo-' . $attrs . $ext )) { - $uri = um_user_uploads_uri() . 'cover_photo-' . $attrs . $ext . '?' . current_time( 'timestamp' ); - } + return $value; - return $uri; - } +} - /*** - *** @get avatar URL instead of image - ***/ - function um_get_avatar_url( $get_avatar ) { - preg_match( '/src="(.*?)"/i', $get_avatar, $matches ); +/** + * Get youtube video ID from url + * + * @param $url + * + * @return bool + */ +function um_youtube_id_from_url( $url ) { + $pattern = + '%^# Match any youtube URL + (?:https?://)? # Optional scheme. Either http or https + (?:www\.)? # Optional www subdomain + (?: # Group host alternatives + youtu\.be/ # Either youtu.be, + | youtube\.com # or youtube.com + (?: # Group path alternatives + /embed/ # Either /embed/ + | /v/ # or /v/ + | /watch\?v= # or /watch\?v= + ) # End path alternatives. + ) # End host alternatives. + ([\w-]{10,12}) # Allow 10-12 for 11 char youtube id. + $%x'; + $result = preg_match( $pattern, $url, $matches ); + if (false !== $result) { return $matches[1]; } - /*** - *** @get avatar uri - ***/ - function um_get_avatar_uri( $image, $attrs ) { - $uri = false; - $find = false; - $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION ); + return false; +} - $cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) ); - if (!empty( $cache_time )) { - $cache_time = "?{$cache_time}"; - } +/** + * user uploads uri + * + * @return string + */ +function um_user_uploads_uri() { + if (is_ssl()) { + UM()->files()->upload_baseurl = str_replace( "http://", "https://", UM()->files()->upload_baseurl ); + } - if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}" )) { + $uri = UM()->files()->upload_baseurl . um_user( 'ID' ) . '/'; - $uri = um_user_uploads_uri() . "profile_photo-{$attrs}{$ext}{$cache_time}"; + return $uri; +} - } else { - $sizes = UM()->options()->get( 'photo_thumb_sizes' ); - if (is_array( $sizes )) $find = um_closest_num( $sizes, $attrs ); +/** + * user uploads directory + * + * @return string + */ +function um_user_uploads_dir() { + $uri = UM()->files()->upload_basedir . um_user( 'ID' ) . '/'; - if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}" )) { + return $uri; +} - $uri = um_user_uploads_uri() . "profile_photo-{$find}{$ext}{$cache_time}"; - } else if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo{$ext}" )) { +/** + * Find closest number in an array + * + * @param $array + * @param $number + * + * @return mixed + */ +function um_closest_num( $array, $number ) { + sort( $array ); + foreach ($array as $a) { + if ($a >= $number) return $a; + } - $uri = um_user_uploads_uri() . "profile_photo{$ext}{$cache_time}"; + return end( $array ); +} - } - if ($attrs == 'original') { - $uri = um_user_uploads_uri() . "profile_photo{$ext}{$cache_time}"; - } +/** + * get cover uri + * + * @param $image + * @param $attrs + * + * @return bool|string + */ +function um_get_cover_uri( $image, $attrs ) { + $uri = false; + $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION ); + if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . '/cover_photo' . $ext )) { + $uri = um_user_uploads_uri() . 'cover_photo' . $ext . '?' . current_time( 'timestamp' ); + } + if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . '/cover_photo-' . $attrs . $ext )) { + $uri = um_user_uploads_uri() . 'cover_photo-' . $attrs . $ext . '?' . current_time( 'timestamp' ); + } + + return $uri; +} + + +/** + * get avatar URL instead of image + * + * @param $get_avatar + * + * @return mixed + */ +function um_get_avatar_url( $get_avatar ) { + preg_match( '/src="(.*?)"/i', $get_avatar, $matches ); + + return $matches[1]; +} + + +/** + * get avatar uri + * + * @param $image + * @param $attrs + * + * @return bool|string + */ +function um_get_avatar_uri( $image, $attrs ) { + $uri = false; + $find = false; + $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION ); + + $cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) ); + + if (!empty( $cache_time )) { + $cache_time = "?{$cache_time}"; + } + + if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}" )) { + + $uri = um_user_uploads_uri() . "profile_photo-{$attrs}{$ext}{$cache_time}"; + + } else { + + $sizes = UM()->options()->get( 'photo_thumb_sizes' ); + if (is_array( $sizes )) $find = um_closest_num( $sizes, $attrs ); + + if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}" )) { + + $uri = um_user_uploads_uri() . "profile_photo-{$find}{$ext}{$cache_time}"; + + } else if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo{$ext}" )) { + + $uri = um_user_uploads_uri() . "profile_photo{$ext}{$cache_time}"; } + if ($attrs == 'original') { + $uri = um_user_uploads_uri() . "profile_photo{$ext}{$cache_time}"; + } + + } + + return $uri; +} + + +/** + * Default avatar URL + * + * @return string + */ +function um_get_default_avatar_uri() { + $uri = UM()->options()->get( 'default_avatar' ); + $uri = !empty( $uri['url'] ) ? $uri['url'] : ''; + if ( ! $uri ) { + $uri = um_url . 'assets/img/default_avatar.jpg'; + } else { + + //http <-> https compatibility default avatar option of SSL was changed + $url_array = parse_url( $uri ); + + if (is_ssl() && $url_array['scheme'] == 'http') { + $uri = str_replace( 'http://', 'https://', $uri ); + } else if (!is_ssl() && $url_array['scheme'] == 'https') { + $uri = str_replace( 'https://', 'http://', $uri ); + } + } + + return $uri; +} + + +/** + * get user avatar url + * + * @return bool|string + */ +function um_get_user_avatar_url() { + if (um_profile( 'profile_photo' )) { + $avatar_uri = um_get_avatar_uri( um_profile( 'profile_photo' ), 32 ); + } else { + $avatar_uri = um_get_default_avatar_uri(); + } + + return $avatar_uri; +} + + +/** + * default cover + * + * @return mixed|string|void + */ +function um_get_default_cover_uri() { + $uri = UM()->options()->get( 'default_cover' ); + $uri = !empty( $uri['url'] ) ? $uri['url'] : ''; + if ($uri) { + $uri = apply_filters( 'um_get_default_cover_uri_filter', $uri ); + return $uri; } + return ''; +} - /** - * Default avatar URL - * - * @return string - */ - function um_get_default_avatar_uri() { - $uri = UM()->options()->get( 'default_avatar' ); - $uri = !empty( $uri['url'] ) ? $uri['url'] : ''; - if ( ! $uri ) { - $uri = um_url . 'assets/img/default_avatar.jpg'; - } else { - //http <-> https compatibility default avatar option of SSL was changed - $url_array = parse_url( $uri ); +/** + * @param $data + * @param null $attrs + * + * @return string + */ +function um_user( $data, $attrs = null ) { - if (is_ssl() && $url_array['scheme'] == 'http') { - $uri = str_replace( 'http://', 'https://', $uri ); - } else if (!is_ssl() && $url_array['scheme'] == 'https') { - $uri = str_replace( 'https://', 'http://', $uri ); + switch ($data) { + + default: + + $value = um_profile( $data ); + + $value = maybe_unserialize( $value ); + + if (in_array( $data, array( 'role', 'gender' ) )) { + if (is_array( $value )) { + $value = implode( ",", $value ); + } + + return $value; } - } - return $uri; - } + return $value; + break; + + case 'user_email': + + $user_email_in_meta = get_user_meta( um_user( 'ID' ), 'user_email', true ); + if ($user_email_in_meta) { + delete_user_meta( um_user( 'ID' ), 'user_email' ); + } + + $value = um_profile( $data ); + + return $value; + break; + + case 'first_name': + case 'last_name': + + $name = um_profile( $data ); + + if ( UM()->options()->get( 'force_display_name_capitlized' ) ) { + $name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) ); + } + + $name = apply_filters( "um_user_{$data}_case", $name ); + + return $name; + + break; + + case 'full_name': + + if (um_user( 'first_name' ) && um_user( 'last_name' )) { + $full_name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' ); + } else { + $full_name = um_user( 'display_name' ); + } + + $full_name = UM()->validation()->safe_name_in_url( $full_name ); + + // update full_name changed + if (um_profile( $data ) !== $full_name) { + update_user_meta( um_user( 'ID' ), 'full_name', $full_name ); + } + + return $full_name; + + break; + + case 'first_and_last_name_initial': + + $f_and_l_initial = ''; + + if (um_user( 'first_name' ) && um_user( 'last_name' )) { + $initial = um_user( 'last_name' ); + $f_and_l_initial = um_user( 'first_name' ) . ' ' . $initial[0]; + } else { + $f_and_l_initial = um_profile( $data ); + } + + $f_and_l_initial = UM()->validation()->safe_name_in_url( $f_and_l_initial ); + + if ( UM()->options()->get( 'force_display_name_capitlized' ) ) { + $name = implode( '-', array_map( 'ucfirst', explode( '-', $f_and_l_initial ) ) ); + } else { + $name = $f_and_l_initial; + } + + return $name; + + break; + + case 'display_name': + + $op = UM()->options()->get( 'display_name' ); + + $name = ''; - /*** - *** @get user avatar url - ***/ - function um_get_user_avatar_url() { - if (um_profile( 'profile_photo' )) { - $avatar_uri = um_get_avatar_uri( um_profile( 'profile_photo' ), 32 ); - } else { - $avatar_uri = um_get_default_avatar_uri(); - } + if ($op == 'default') { + $name = um_profile( 'display_name' ); + } - return $avatar_uri; - } - - /*** - *** @default cover - ***/ - function um_get_default_cover_uri() { - $uri = UM()->options()->get( 'default_cover' ); - $uri = !empty( $uri['url'] ) ? $uri['url'] : ''; - if ($uri) { - $uri = apply_filters( 'um_get_default_cover_uri_filter', $uri ); - - return $uri; - } - - return ''; - } - - function um_user( $data, $attrs = null ) { - - switch ($data) { - - default: - - $value = um_profile( $data ); - - $value = maybe_unserialize( $value ); - - if (in_array( $data, array( 'role', 'gender' ) )) { - if (is_array( $value )) { - $value = implode( ",", $value ); - } - - return $value; - } - - return $value; - break; - - case 'user_email': - - $user_email_in_meta = get_user_meta( um_user( 'ID' ), 'user_email', true ); - if ($user_email_in_meta) { - delete_user_meta( um_user( 'ID' ), 'user_email' ); - } - - $value = um_profile( $data ); - - return $value; - break; - - case 'first_name': - case 'last_name': - - $name = um_profile( $data ); - - if ( UM()->options()->get( 'force_display_name_capitlized' ) ) { - $name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) ); - } - - $name = apply_filters( "um_user_{$data}_case", $name ); - - return $name; - - break; - - case 'full_name': + if ($op == 'nickname') { + $name = um_profile( 'nickname' ); + } + if ($op == 'full_name') { if (um_user( 'first_name' ) && um_user( 'last_name' )) { - $full_name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' ); + $name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' ); } else { - $full_name = um_user( 'display_name' ); + $name = um_profile( $data ); } - - $full_name = UM()->validation()->safe_name_in_url( $full_name ); - - // update full_name changed - if (um_profile( $data ) !== $full_name) { - update_user_meta( um_user( 'ID' ), 'full_name', $full_name ); - } - - return $full_name; - - break; - - case 'first_and_last_name_initial': - - $f_and_l_initial = ''; - - if (um_user( 'first_name' ) && um_user( 'last_name' )) { - $initial = um_user( 'last_name' ); - $f_and_l_initial = um_user( 'first_name' ) . ' ' . $initial[0]; - } else { - $f_and_l_initial = um_profile( $data ); - } - - $f_and_l_initial = UM()->validation()->safe_name_in_url( $f_and_l_initial ); - - if ( UM()->options()->get( 'force_display_name_capitlized' ) ) { - $name = implode( '-', array_map( 'ucfirst', explode( '-', $f_and_l_initial ) ) ); - } else { - $name = $f_and_l_initial; - } - - return $name; - - break; - - case 'display_name': - - $op = UM()->options()->get( 'display_name' ); - - $name = ''; - - - if ($op == 'default') { - $name = um_profile( 'display_name' ); - } - - if ($op == 'nickname') { - $name = um_profile( 'nickname' ); - } - - if ($op == 'full_name') { - if (um_user( 'first_name' ) && um_user( 'last_name' )) { - $name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' ); - } else { - $name = um_profile( $data ); - } - if (!$name) { - $name = um_user( 'user_login' ); - } - } - - if ($op == 'sur_name') { - if (um_user( 'first_name' ) && um_user( 'last_name' )) { - $name = um_user( 'last_name' ) . ' ' . um_user( 'first_name' ); - } else { - $name = um_profile( $data ); - } - } - - if ($op == 'first_name') { - if (um_user( 'first_name' )) { - $name = um_user( 'first_name' ); - } else { - $name = um_profile( $data ); - } - } - - if ($op == 'username') { + if (!$name) { $name = um_user( 'user_login' ); } + } - if ($op == 'initial_name') { - if (um_user( 'first_name' ) && um_user( 'last_name' )) { - $initial = um_user( 'last_name' ); - $name = um_user( 'first_name' ) . ' ' . $initial[0]; - } else { - $name = um_profile( $data ); - } - } - - if ($op == 'initial_name_f') { - if (um_user( 'first_name' ) && um_user( 'last_name' )) { - $initial = um_user( 'first_name' ); - $name = $initial[0] . ' ' . um_user( 'last_name' ); - } else { - $name = um_profile( $data ); - } - } - - - if ($op == 'field' && UM()->options()->get( 'display_name_field' ) != '') { - $fields = array_filter( preg_split( '/[,\s]+/', UM()->options()->get( 'display_name_field' ) ) ); - $name = ''; - - foreach ($fields as $field) { - if (um_profile( $field )) { - $name .= um_profile( $field ) . ' '; - } else if (um_user( $field )) { - $name .= um_user( $field ) . ' '; - } - - } - } - - if ( UM()->options()->get( 'force_display_name_capitlized' ) ) { - $name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) ); - } - - return apply_filters( 'um_user_display_name_filter', $name, um_user( 'ID' ), ( $attrs == 'html' ) ? 1 : 0 ); - - break; - - case 'role_select': - case 'role_radio': - return UM()->roles()->get_role_name( um_user( 'role' ) ); - break; - - case 'submitted': - $array = um_profile( $data ); - if (empty( $array )) return ''; - $array = unserialize( $array ); - - return $array; - break; - - case 'password_reset_link': - return UM()->password()->reset_url(); - break; - - case 'account_activation_link': - return UM()->permalinks()->activate_url(); - break; - - case 'profile_photo': - - $has_profile_photo = false; - $photo_type = 'um-avatar-default'; - $image_alt = apply_filters( "um_avatar_image_alternate_text", um_user( "display_name" ) ); - - if (um_profile( 'profile_photo' )) { - $avatar_uri = um_get_avatar_uri( um_profile( 'profile_photo' ), $attrs ); - $has_profile_photo = true; - $photo_type = 'um-avatar-uploaded'; - } else if (um_user( 'synced_profile_photo' )) { - $avatar_uri = um_user( 'synced_profile_photo' ); + if ($op == 'sur_name') { + if (um_user( 'first_name' ) && um_user( 'last_name' )) { + $name = um_user( 'last_name' ) . ' ' . um_user( 'first_name' ); } else { - $avatar_uri = um_get_default_avatar_uri(); + $name = um_profile( $data ); } + } - $avatar_uri = apply_filters( 'um_user_avatar_url_filter', $avatar_uri, um_user( 'ID' ) ); + if ($op == 'first_name') { + if (um_user( 'first_name' )) { + $name = um_user( 'first_name' ); + } else { + $name = um_profile( $data ); + } + } + + if ($op == 'username') { + $name = um_user( 'user_login' ); + } + + if ($op == 'initial_name') { + if (um_user( 'first_name' ) && um_user( 'last_name' )) { + $initial = um_user( 'last_name' ); + $name = um_user( 'first_name' ) . ' ' . $initial[0]; + } else { + $name = um_profile( $data ); + } + } + + if ($op == 'initial_name_f') { + if (um_user( 'first_name' ) && um_user( 'last_name' )) { + $initial = um_user( 'first_name' ); + $name = $initial[0] . ' ' . um_user( 'last_name' ); + } else { + $name = um_profile( $data ); + } + } - if (!$avatar_uri) - return ''; + if ($op == 'field' && UM()->options()->get( 'display_name_field' ) != '') { + $fields = array_filter( preg_split( '/[,\s]+/', UM()->options()->get( 'display_name_field' ) ) ); + $name = ''; - if ( UM()->options()->get( 'use_gravatars' ) && !um_user( 'synced_profile_photo' ) && !$has_profile_photo) { - $avatar_hash_id = get_user_meta( um_user( 'ID' ), 'synced_gravatar_hashed_id', true ); - $avatar_uri = um_get_domain_protocol() . 'gravatar.com/avatar/' . $avatar_hash_id; - $avatar_uri = add_query_arg( 's', 400, $avatar_uri ); - $gravatar_type = UM()->options()->get( 'use_um_gravatar_default_builtin_image' ); - $photo_type = 'um-avatar-gravatar'; - if ( $gravatar_type == 'default' ) { - if ( UM()->options()->get( 'use_um_gravatar_default_image' ) ) { - $avatar_uri = add_query_arg( 'd', um_get_default_avatar_uri(), $avatar_uri ); - } - } else { - $avatar_uri = add_query_arg( 'd', $gravatar_type, $avatar_uri ); + foreach ($fields as $field) { + if (um_profile( $field )) { + $name .= um_profile( $field ) . ' '; + } else if (um_user( $field )) { + $name .= um_user( $field ) . ' '; } } + } - $default_avatar = um_get_default_avatar_uri(); + if ( UM()->options()->get( 'force_display_name_capitlized' ) ) { + $name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) ); + } - return '' . $image_alt . ''; + return apply_filters( 'um_user_display_name_filter', $name, um_user( 'ID' ), ( $attrs == 'html' ) ? 1 : 0 ); - break; + break; - case 'cover_photo': + case 'role_select': + case 'role_radio': + return UM()->roles()->get_role_name( um_user( 'role' ) ); + break; - $is_default = false; + case 'submitted': + $array = um_profile( $data ); + if (empty( $array )) return ''; + $array = unserialize( $array ); - if (um_profile( 'cover_photo' )) { - $cover_uri = um_get_cover_uri( um_profile( 'cover_photo' ), $attrs ); - } else if (um_profile( 'synced_cover_photo' )) { - $cover_uri = um_profile( 'synced_cover_photo' ); + return $array; + break; + + case 'password_reset_link': + return UM()->password()->reset_url(); + break; + + case 'account_activation_link': + return UM()->permalinks()->activate_url(); + break; + + case 'profile_photo': + + $has_profile_photo = false; + $photo_type = 'um-avatar-default'; + $image_alt = apply_filters( "um_avatar_image_alternate_text", um_user( "display_name" ) ); + + if (um_profile( 'profile_photo' )) { + $avatar_uri = um_get_avatar_uri( um_profile( 'profile_photo' ), $attrs ); + $has_profile_photo = true; + $photo_type = 'um-avatar-uploaded'; + } else if (um_user( 'synced_profile_photo' )) { + $avatar_uri = um_user( 'synced_profile_photo' ); + } else { + $avatar_uri = um_get_default_avatar_uri(); + } + + $avatar_uri = apply_filters( 'um_user_avatar_url_filter', $avatar_uri, um_user( 'ID' ) ); + + + if (!$avatar_uri) + return ''; + + if ( UM()->options()->get( 'use_gravatars' ) && !um_user( 'synced_profile_photo' ) && !$has_profile_photo) { + $avatar_hash_id = get_user_meta( um_user( 'ID' ), 'synced_gravatar_hashed_id', true ); + $avatar_uri = um_get_domain_protocol() . 'gravatar.com/avatar/' . $avatar_hash_id; + $avatar_uri = add_query_arg( 's', 400, $avatar_uri ); + $gravatar_type = UM()->options()->get( 'use_um_gravatar_default_builtin_image' ); + $photo_type = 'um-avatar-gravatar'; + if ( $gravatar_type == 'default' ) { + if ( UM()->options()->get( 'use_um_gravatar_default_image' ) ) { + $avatar_uri = add_query_arg( 'd', um_get_default_avatar_uri(), $avatar_uri ); + } } else { - $cover_uri = um_get_default_cover_uri(); - $is_default = true; - } - - $cover_uri = apply_filters( 'um_user_cover_photo_uri__filter', $cover_uri, $is_default, $attrs ); - - if ($cover_uri) - return ''; - - if (!$cover_uri) - return ''; - - break; - - - } - - } - - /** - * Get server protocol - * - * @return string - */ - function um_get_domain_protocol() { - - if (is_ssl()) { - $protocol = 'https://'; - } else { - $protocol = 'http://'; - } - - return $protocol; - } - - /** - * Set SSL to media URI - * - * @param string $url - * - * @return string - */ - function um_secure_media_uri( $url ) { - - if (is_ssl()) { - $url = str_replace( 'http:', 'https:', $url ); - } - - return $url; - } - - /** - * Check if meta_value exists - * - * UNUSED - * - * @param string $key - * @param mixed $value - * - * @return integer - */ - function um_is_meta_value_exists( $key, $value, $return_user_id = false ) { - global $wpdb; - - if (isset( UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] )) { - return UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key]; - } - - if (!$return_user_id) { - $count = $wpdb->get_var( $wpdb->prepare( - "SELECT COUNT(*) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", - $key, - $value - ) ); - - UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] = $count; - - return $count; - } - - $user_id = $wpdb->get_var( $wpdb->prepare( - "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ", - $key, - $value - ) ); - - UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] = $user_id; - - return $user_id; - - } - - /** - * Force strings to UTF-8 encoded - * - * @param mixed $value - * - * @return mixed - */ - function um_force_utf8_string( $value ) { - - if (is_array( $value )) { - $arr_value = array(); - foreach ($value as $key => $value) { - $utf8_decoded_value = utf8_decode( $value ); - - if (mb_check_encoding( $utf8_decoded_value, 'UTF-8' )) { - array_push( $arr_value, $utf8_decoded_value ); - } else { - array_push( $arr_value, $value ); + $avatar_uri = add_query_arg( 'd', $gravatar_type, $avatar_uri ); } } - return $arr_value; - } else { + $default_avatar = um_get_default_avatar_uri(); + return '' . $image_alt . ''; + + break; + + case 'cover_photo': + + $is_default = false; + + if (um_profile( 'cover_photo' )) { + $cover_uri = um_get_cover_uri( um_profile( 'cover_photo' ), $attrs ); + } else if (um_profile( 'synced_cover_photo' )) { + $cover_uri = um_profile( 'synced_cover_photo' ); + } else { + $cover_uri = um_get_default_cover_uri(); + $is_default = true; + } + + $cover_uri = apply_filters( 'um_user_cover_photo_uri__filter', $cover_uri, $is_default, $attrs ); + + if ($cover_uri) + return ''; + + if (!$cover_uri) + return ''; + + break; + + + } + +} + + +/** + * Get server protocol + * + * @return string + */ +function um_get_domain_protocol() { + + if (is_ssl()) { + $protocol = 'https://'; + } else { + $protocol = 'http://'; + } + + return $protocol; +} + + +/** + * Set SSL to media URI + * + * @param string $url + * + * @return string + */ +function um_secure_media_uri( $url ) { + + if (is_ssl()) { + $url = str_replace( 'http:', 'https:', $url ); + } + + return $url; +} + + +/** + * Force strings to UTF-8 encoded + * + * @param mixed $value + * + * @return mixed + */ +function um_force_utf8_string( $value ) { + + if (is_array( $value )) { + $arr_value = array(); + foreach ($value as $key => $value) { $utf8_decoded_value = utf8_decode( $value ); if (mb_check_encoding( $utf8_decoded_value, 'UTF-8' )) { - return $utf8_decoded_value; - } - } - - return $value; - } - - /** - * Filters the search query. - * - * @param string $search - * - * @return string - */ - function um_filter_search( $search ) { - $search = trim( strip_tags( $search ) ); - $search = preg_replace( '/[^a-z \.\@\_\-]+/i', '', $search ); - - return $search; - } - - /** - * Returns the user search query - * - * @return string - */ - function um_get_search_query() { - $query = UM()->permalinks()->get_query_array(); - $search = isset( $query['search'] ) ? $query['search'] : ''; - - return um_filter_search( $search ); - } - - /** - * Returns the ultimate member search form - * - * @return string - */ - function um_get_search_form() { - - return do_shortcode( '[ultimatemember_searchform]' ); - } - - /** - * Display the search form. - * - * @return string - */ - function um_search_form() { - - echo um_get_search_form(); - } - - /** - * Get localization - * - * @return string - */ - function um_get_locale() { - - $lang_code = get_locale(); - - if (strpos( $lang_code, 'en_' ) > -1 || empty( $lang_code ) || $lang_code == 0) { - return 'en'; - } - - return $lang_code; - } - - /** - * Get current page type - * - * @return string - */ - function um_get_current_page_type() { - global $wp_query; - $loop = 'notfound'; - - if ($wp_query->is_page) { - //$loop = is_front_page() ? 'front' : 'page'; - $loop = 'page'; - } else if ($wp_query->is_home) { - $loop = 'home'; - } else if ($wp_query->is_single) { - $loop = ( $wp_query->is_attachment ) ? 'attachment' : 'single'; - } else if ($wp_query->is_category) { - $loop = 'category'; - } else if ($wp_query->is_tag) { - $loop = 'tag'; - } else if ($wp_query->is_tax) { - $loop = 'tax'; - } else if ($wp_query->is_archive) { - if ($wp_query->is_day) { - $loop = 'day'; - } else if ($wp_query->is_month) { - $loop = 'month'; - } else if ($wp_query->is_year) { - $loop = 'year'; - } else if ($wp_query->is_author) { - $loop = 'author'; + array_push( $arr_value, $utf8_decoded_value ); } else { - $loop = 'archive'; + array_push( $arr_value, $value ); } - } else if ($wp_query->is_search) { - $loop = 'search'; - } else if ($wp_query->is_404) { - $loop = 'notfound'; + } - return $loop; - } + return $arr_value; + } else { - /** - * Check if running local - * - * @return boolean - */ - function um_core_is_local() { - if ($_SERVER['HTTP_HOST'] == 'localhost' - || substr( $_SERVER['HTTP_HOST'], 0, 3 ) == '10.' - || substr( $_SERVER['HTTP_HOST'], 0, 7 ) == '192.168' - ) return true; + $utf8_decoded_value = utf8_decode( $value ); - return false; - } - - /** - * Get user host - * - * Returns the webhost this site is using if possible - * - * @since 1.3.68 - * @return mixed string $host if detected, false otherwise - */ - function um_get_host() { - $host = false; - - if (defined( 'WPE_APIKEY' )) { - $host = 'WP Engine'; - } else if (defined( 'PAGELYBIN' )) { - $host = 'Pagely'; - } else if (DB_HOST == 'localhost:/tmp/mysql5.sock') { - $host = 'ICDSoft'; - } else if (DB_HOST == 'mysqlv5') { - $host = 'NetworkSolutions'; - } else if (strpos( DB_HOST, 'ipagemysql.com' ) !== false) { - $host = 'iPage'; - } else if (strpos( DB_HOST, 'ipowermysql.com' ) !== false) { - $host = 'IPower'; - } else if (strpos( DB_HOST, '.gridserver.com' ) !== false) { - $host = 'MediaTemple Grid'; - } else if (strpos( DB_HOST, '.pair.com' ) !== false) { - $host = 'pair Networks'; - } else if (strpos( DB_HOST, '.stabletransit.com' ) !== false) { - $host = 'Rackspace Cloud'; - } else if (strpos( DB_HOST, '.sysfix.eu' ) !== false) { - $host = 'SysFix.eu Power Hosting'; - } else if (strpos( $_SERVER['SERVER_NAME'], 'Flywheel' ) !== false) { - $host = 'Flywheel'; - } else { - // Adding a general fallback for data gathering - $host = 'DBH: ' . DB_HOST . ', SRV: ' . $_SERVER['SERVER_NAME']; + if (mb_check_encoding( $utf8_decoded_value, 'UTF-8' )) { + return $utf8_decoded_value; } - - return $host; } - /** - * Let To Num - * - * Does Size Conversions - * - * @since 1.3.68 - * @author Chris Christoff - * - * @param unknown $v - * - * @return int|string - */ - function um_let_to_num( $v ) { - $l = substr( $v, -1 ); - $ret = substr( $v, 0, -1 ); + return $value; +} - switch (strtoupper( $l )) { - case 'P': // fall-through - case 'T': // fall-through - case 'G': // fall-through - case 'M': // fall-through - case 'K': // fall-through - $ret *= 1024; - break; - default: - break; - } - return $ret; +/** + * Filters the search query. + * + * @param string $search + * + * @return string + */ +function um_filter_search( $search ) { + $search = trim( strip_tags( $search ) ); + $search = preg_replace( '/[^a-z \.\@\_\-]+/i', '', $search ); + + return $search; +} + + +/** + * Returns the user search query + * + * @return string + */ +function um_get_search_query() { + $query = UM()->permalinks()->get_query_array(); + $search = isset( $query['search'] ) ? $query['search'] : ''; + + return um_filter_search( $search ); +} + + +/** + * Returns the ultimate member search form + * + * @return string + */ +function um_get_search_form() { + return do_shortcode( '[ultimatemember_searchform]' ); +} + + +/** + * Display the search form. + * + */ +function um_search_form() { + echo um_get_search_form(); +} + + +/** + * Get user host + * + * Returns the webhost this site is using if possible + * + * @since 1.3.68 + * @return mixed string $host if detected, false otherwise + */ +function um_get_host() { + $host = false; + + if (defined( 'WPE_APIKEY' )) { + $host = 'WP Engine'; + } else if (defined( 'PAGELYBIN' )) { + $host = 'Pagely'; + } else if (DB_HOST == 'localhost:/tmp/mysql5.sock') { + $host = 'ICDSoft'; + } else if (DB_HOST == 'mysqlv5') { + $host = 'NetworkSolutions'; + } else if (strpos( DB_HOST, 'ipagemysql.com' ) !== false) { + $host = 'iPage'; + } else if (strpos( DB_HOST, 'ipowermysql.com' ) !== false) { + $host = 'IPower'; + } else if (strpos( DB_HOST, '.gridserver.com' ) !== false) { + $host = 'MediaTemple Grid'; + } else if (strpos( DB_HOST, '.pair.com' ) !== false) { + $host = 'pair Networks'; + } else if (strpos( DB_HOST, '.stabletransit.com' ) !== false) { + $host = 'Rackspace Cloud'; + } else if (strpos( DB_HOST, '.sysfix.eu' ) !== false) { + $host = 'SysFix.eu Power Hosting'; + } else if (strpos( $_SERVER['SERVER_NAME'], 'Flywheel' ) !== false) { + $host = 'Flywheel'; + } else { + // Adding a general fallback for data gathering + $host = 'DBH: ' . DB_HOST . ', SRV: ' . $_SERVER['SERVER_NAME']; } + return $host; +} + + +/** + * Let To Num + * + * Does Size Conversions + * + * @since 1.3.68 + * @author Chris Christoff + * + * @param string $v + * + * @return int|string + */ +function um_let_to_num( $v ) { + $l = substr( $v, -1 ); + $ret = substr( $v, 0, -1 ); + + switch (strtoupper( $l )) { + case 'P': // fall-through + case 'T': // fall-through + case 'G': // fall-through + case 'M': // fall-through + case 'K': // fall-through + $ret *= 1024; + break; + default: + break; + } + + return $ret; +} \ No newline at end of file From ef9c06185b2823dab670279af00701b896d01c5c Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Tue, 19 Dec 2017 12:38:00 +0200 Subject: [PATCH 4/4] - fixed access issues with restriction content + rolled back feature with unselected checkboxes access for all logged in users; - fixed vulnerability on user profile update; - small changes; --- .../admin/core/class-admin-forms-settings.php | 2 + includes/admin/core/class-admin-forms.php | 8 +- includes/core/class-access.php | 123 ++++++++++++------ includes/core/um-actions-profile.php | 12 +- 4 files changed, 99 insertions(+), 46 deletions(-) diff --git a/includes/admin/core/class-admin-forms-settings.php b/includes/admin/core/class-admin-forms-settings.php index 13143d93..5fef3440 100644 --- a/includes/admin/core/class-admin-forms-settings.php +++ b/includes/admin/core/class-admin-forms-settings.php @@ -33,6 +33,7 @@ if ( ! class_exists( 'Admin_Forms_Settings' ) ) { return $field_data['value' . $i]; } else { $value = UM()->options()->get( $field_data['id' . $i] ); + $value = is_string( $value ) ? stripslashes( $value ) : $value; return '' !== $value ? $value : $default; } } else { @@ -40,6 +41,7 @@ if ( ! class_exists( 'Admin_Forms_Settings' ) ) { return $field_data['value'. $i]; } else { $value = UM()->options()->get( $field_data['id' . $i] ); + $value = is_string( $value ) ? stripslashes( $value ) : $value; return isset( $value ) ? $value : $default; } } diff --git a/includes/admin/core/class-admin-forms.php b/includes/admin/core/class-admin-forms.php index 4aed11a6..33cc0626 100644 --- a/includes/admin/core/class-admin-forms.php +++ b/includes/admin/core/class-admin-forms.php @@ -989,10 +989,14 @@ if ( ! class_exists( 'Admin_Forms' ) ) { $default = isset( $field_data['default' . $i] ) ? $field_data['default' . $i] : $default; if ( $field_data['type'] == 'checkbox' || $field_data['type'] == 'multi_checkbox' ) { - return ( isset( $field_data['value' . $i] ) && '' !== $field_data['value' . $i] ) ? $field_data['value' . $i] : $default; + $value = ( isset( $field_data['value' . $i] ) && '' !== $field_data['value' . $i] ) ? $field_data['value' . $i] : $default; } else { - return isset( $field_data['value' . $i] ) ? $field_data['value' . $i] : $default; + $value = isset( $field_data['value' . $i] ) ? $field_data['value' . $i] : $default; } + + $value = is_string( $value ) ? stripslashes( $value ) : $value; + + return $value; } } } \ No newline at end of file diff --git a/includes/core/class-access.php b/includes/core/class-access.php index e5dacc10..6c8182d5 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -126,18 +126,30 @@ if ( ! class_exists( 'Access' ) ) { $custom_restrict = apply_filters( 'um_custom_restriction', true, $restriction ); - if ( ! empty( $restriction['_um_access_roles'] ) ) + if ( empty( $restriction['_um_access_roles'] ) ) { + if ( $custom_restrict ) { + $this->allow_access = true; + return; + } else { + //restrict terms page by 404 for logged in users with wrong role + global $wp_query; + $wp_query->set_404(); + status_header( 404 ); + nocache_headers(); + } + } else { $user_can = $this->user_can( get_current_user_id(), $restriction['_um_access_roles'] ); - if ( isset( $user_can ) && $user_can && $custom_restrict ) { - $this->allow_access = true; - return; - } else { - //restrict terms page by 404 for logged in users with wrong role - global $wp_query; - $wp_query->set_404(); - status_header( 404 ); - nocache_headers(); + if ( isset( $user_can ) && $user_can && $custom_restrict ) { + $this->allow_access = true; + return; + } else { + //restrict terms page by 404 for logged in users with wrong role + global $wp_query; + $wp_query->set_404(); + status_header( 404 ); + nocache_headers(); + } } } } @@ -187,6 +199,7 @@ if ( ! class_exists( 'Access' ) ) { if ( $access == 2 ) { //global settings for accessible home page $home_page_accessible = UM()->options()->get( 'home_page_accessible' ); + if ( $home_page_accessible == 0 ) { //get redirect URL if not set get login page by default $redirect = UM()->options()->get( 'access_redirect' ); @@ -194,6 +207,9 @@ if ( ! class_exists( 'Access' ) ) { $redirect = um_get_core_page( 'login' ); $this->redirect_handler = $this->set_referer( $redirect, 'global' ); + } else { + $this->allow_access = true; + return; } } } @@ -212,6 +228,9 @@ if ( ! class_exists( 'Access' ) ) { $redirect = um_get_core_page( 'login' ); $this->redirect_handler = $this->set_referer( $redirect, 'global' ); + } else { + $this->allow_access = true; + return; } } } @@ -223,11 +242,13 @@ if ( ! class_exists( 'Access' ) ) { //build exclude URLs pages $redirects = array(); - $redirects[] = untrailingslashit( UM()->options()->get( 'access_redirect' ) ); + $redirects[] = trim( untrailingslashit( UM()->options()->get( 'access_redirect' ) ) ); $exclude_uris = UM()->options()->get( 'access_exclude_uris' ); - if ( ! empty( $exclude_uris ) ) + if ( ! empty( $exclude_uris ) ) { + $exclude_uris = array_map( 'trim', $exclude_uris ); $redirects = array_merge( $redirects, $exclude_uris ); + } $redirects = array_unique( $redirects ); @@ -235,16 +256,17 @@ if ( ! class_exists( 'Access' ) ) { $current_url = untrailingslashit( $current_url ); $current_url_slash = trailingslashit( $current_url ); - //get redirect URL if not set get login page by default - $redirect = UM()->options()->get( 'access_redirect' ); - if ( ! $redirect ) - $redirect = um_get_core_page( 'login' ); - - if ( ! isset( $post->ID ) || ! ( in_array( $current_url, $redirects ) || in_array( $current_url_slash, $redirects ) ) ) { + if ( ! ( isset( $post->ID ) && ( in_array( $current_url, $redirects ) || in_array( $current_url_slash, $redirects ) ) ) ) { //if current page not in exclude URLs + //get redirect URL if not set get login page by default + $redirect = UM()->options()->get( 'access_redirect' ); + if ( ! $redirect ) + $redirect = um_get_core_page( 'login' ); + $this->redirect_handler = $this->set_referer( $redirect, 'global' ); } else { $this->redirect_handler = false; + $this->allow_access = true; } } } @@ -285,6 +307,11 @@ if ( ! class_exists( 'Access' ) ) { //check global restrict content options do_action( 'um_access_check_global_settings' ); + /*var_dump($this->allow_access); + var_dump($this->redirect_handler); + var_dump('12345678'); + exit;*/ + $this->check_access(); } @@ -475,9 +502,9 @@ if ( ! class_exists( 'Access' ) ) { if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) { if ( ! isset( $restriction['_um_restrict_by_custom_message'] ) || '0' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = $restricted_global_message; + $post->post_content = stripslashes( $restricted_global_message ); } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? $restriction['_um_restrict_custom_message'] : ''; + $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; } } @@ -492,9 +519,9 @@ if ( ! class_exists( 'Access' ) ) { if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) { if ( ! isset( $restriction['_um_restrict_by_custom_message'] ) || '0' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = $restricted_global_message; + $post->post_content = stripslashes( $restricted_global_message ); } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? $restriction['_um_restrict_custom_message'] : ''; + $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; } $filtered_posts[] = $post; @@ -531,13 +558,19 @@ if ( ! class_exists( 'Access' ) ) { $custom_restrict = apply_filters( 'um_custom_restriction', true, $restriction ); - if ( ! empty( $restriction['_um_access_roles'] ) ) - $user_can = $this->user_can( get_current_user_id(), $restriction['_um_access_roles'] ); + if ( empty( $restriction['_um_access_roles'] ) ) { + if ( $custom_restrict ) { + $filtered_posts[] = $post; + continue; + } + } else { + $user_can = $this->user_can( get_current_user_id(), $restriction['_um_access_roles'] ); - if ( isset( $user_can ) && $user_can && $custom_restrict ) { - $filtered_posts[] = $post; - continue; - } + if ( isset( $user_can ) && $user_can && $custom_restrict ) { + $filtered_posts[] = $post; + continue; + } + } if ( empty( $query->is_singular ) ) { //if not single query when exclude if set _um_access_hide_from_queries @@ -546,9 +579,9 @@ if ( ! class_exists( 'Access' ) ) { if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) { if ( ! isset( $restriction['_um_restrict_by_custom_message'] ) || '0' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = $restricted_global_message; + $post->post_content = stripslashes( $restricted_global_message ); } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? $restriction['_um_restrict_custom_message'] : ''; + $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; } } @@ -563,13 +596,13 @@ if ( ! class_exists( 'Access' ) ) { if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) { if ( ! isset( $restriction['_um_restrict_by_custom_message'] ) || '0' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = $restricted_global_message; + $post->post_content = stripslashes( $restricted_global_message ); if ( 'attachment' == $post->post_type ) { remove_filter( 'the_content', 'prepend_attachment' ); } } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? $restriction['_um_restrict_custom_message'] : ''; + $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; if ( 'attachment' == $post->post_type ) { remove_filter( 'the_content', 'prepend_attachment' ); @@ -607,9 +640,9 @@ if ( ! class_exists( 'Access' ) ) { if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) { if ( ! isset( $restriction['_um_restrict_by_custom_message'] ) || '0' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = $restricted_global_message; + $post->post_content = stripslashes( $restricted_global_message ); } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? $restriction['_um_restrict_custom_message'] : ''; + $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; } } @@ -624,13 +657,13 @@ if ( ! class_exists( 'Access' ) ) { if ( ! isset( $restriction['_um_noaccess_action'] ) || '0' == $restriction['_um_noaccess_action'] ) { if ( ! isset( $restriction['_um_restrict_by_custom_message'] ) || '0' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = $restricted_global_message; + $post->post_content = stripslashes( $restricted_global_message ); if ( 'attachment' == $post->post_type ) { remove_filter( 'the_content', 'prepend_attachment' ); } } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { - $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? $restriction['_um_restrict_custom_message'] : ''; + $post->post_content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; if ( 'attachment' == $post->post_type ) { remove_filter( 'the_content', 'prepend_attachment' ); @@ -711,13 +744,19 @@ if ( ! class_exists( 'Access' ) ) { $custom_restrict = apply_filters( 'um_custom_restriction', true, $restriction ); - if ( ! empty( $restriction['_um_access_roles'] ) ) - $user_can = $this->user_can( get_current_user_id(), $restriction['_um_access_roles'] ); + if ( empty( $restriction['_um_access_roles'] ) ) { + if ( $custom_restrict ) { + $filtered_items[] = $menu_item; + continue; + } + } else { + $user_can = $this->user_can( get_current_user_id(), $restriction['_um_access_roles'] ); - if ( isset( $user_can ) && $user_can && $custom_restrict ) { - $filtered_items[] = $menu_item; - continue; - } + if ( isset( $user_can ) && $user_can && $custom_restrict ) { + $filtered_items[] = $menu_item; + continue; + } + } //if not single query when exclude if set _um_access_hide_from_queries if ( empty( $restriction['_um_access_hide_from_queries'] ) ) { diff --git a/includes/core/um-actions-profile.php b/includes/core/um-actions-profile.php index dfa3dc28..9cc32218 100644 --- a/includes/core/um-actions-profile.php +++ b/includes/core/um-actions-profile.php @@ -104,8 +104,16 @@ if ( ! defined( 'ABSPATH' ) ) exit; $to_update['description'] = $args['submitted']['description']; } - if (!empty( $args['submitted']['role'] )) { - $to_update['role'] = $args['submitted']['role']; + if ( ! empty( $args['submitted']['role'] ) ) { + global $wp_roles; + $role_keys = array_map( function( $item ) { + return 'um_' . $item; + }, get_option( 'um_roles' ) ); + $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) ); + + if ( ! in_array( $args['submitted']['role'], $exclude_roles ) ) { + $to_update['role'] = $args['submitted']['role']; + } } do_action( 'um_user_pre_updating_profile', $to_update );