/** * Get a value of a user. * * Ideally an attribute key should be passed, in which case the first value will be returned by default. * If a full meta key is passed, the value will be returned. * If a value of an option is passed, a boolean will be returned depending on if the option is enabled for the user. * * @since 0.1 * */ function wp_crm_get_value($meta_key, $user_id = false, $args = '') { global $current_user, $wp_crm; $args = wp_parse_args($args, array('return' => 'value', 'concat_char' => ', ', 'meta_key' => $meta_key, 'option_key' => '')); if (!$user_id) { $user_id = $current_user->ID; } $quantifiable_attributes = array_keys(WP_CRM_F::get_quantifiable_attributes()); //** Check if meta key exists as key and as label */ $attributes = $wp_crm['data_structure']['attributes']; $full_meta_keys = $wp_crm['data_structure']['full_meta_keys']; $meta_keys = $wp_crm['data_structure']['meta_keys']; //* If passed key is an attribute key (as intended) */ if (is_array($attributes) && in_array($meta_key, array_keys($attributes))) { $args['attribute_key'] = $meta_key; } //* If passed meta_key is actually a label name */ if (is_array($meta_keys) && in_array($meta_key, $meta_keys)) { $meta_value_flip = array_flip($meta_keys); $args['attribute_key'] = $meta_value_flip[$meta_key]; $meta_key = $args['attribute_key']; } //* If a full meta key is passed (for pre-defined values) */ if (is_array($full_meta_keys) && in_array($meta_key, array_keys($full_meta_keys))) { $args['full_meta_key'] = $meta_key; $args['attribute_key'] = $full_meta_keys[$meta_key]; } //** If all else fails, use the passed $meta_key (it may be a main user table key) */ if (empty($args['attribute_key'])) { $args['attribute_key'] = $meta_key; } //** Get the full attribute data once we have the attribute_key */ $attribute = WP_CRM_F::get_attribute($args['attribute_key']); //* Make sure we have a user object */ if (!empty($user_id) && is_numeric($user_id)) { $user_object = (array) wp_crm_get_user($user_id); } //** If we have the full meta key then we can get value easily from get_user_meta() */ if (!empty($args['full_meta_key']) && $args['full_meta_key'] != $args['attribute_key']) { $args['value'] = get_user_meta($user_id, $args['full_meta_key'], true); } else { //** Attribute has options, we return the label of the option */ if (!empty($attribute['has_options'])) { if ($attribute['input_type'] == 'text' || $attribute['input_type'] == 'textarea' || $attribute['input_type'] == 'date') { //** Try to get value by option key. */ $option_key = !empty($args['option_key']) ? $args['option_key'] : (is_array($attribute['option_keys']) ? key($attribute['option_keys']) : false); if (!empty($option_key)) { $args['value'] = get_user_meta($user_id, $args['attribute_key'] . '_option_' . $option_key, true); if (!empty($args['value'])) { $args['label'] = $meta_keys[$args['attribute_key'] . '_option_' . $option_key]; $args['return_option_label'] = true; $args['value'] .= ', ' . $args['label']; } } if (empty($args['value'])) { $args['value'] = WP_CRM_F::get_first_value(!empty($user_object[$args['attribute_key']]) ? $user_object[$args['attribute_key']] : array()); } } else { $options = WP_CRM_F::list_options($user_object, $args['attribute_key']); if (is_array($options)) { $args['value'] = implode($args['concat_char'], $options); } } } else { $args['value'] = WP_CRM_F::get_first_value(!empty($user_object[$args['attribute_key']]) ? $user_object[$args['attribute_key']] : array()); } } //** Check if this should be a boolean response */ if ((empty($args['return_option_label']) || !$args['return_option_label']) && in_array($args['attribute_key'], $quantifiable_attributes)) { if (!empty($args['value']) && $args['value'] == 'on') { $args['value'] = true; } } switch ($args['return']) { case 'value': $result = !empty($args['value']) ? $args['value'] : ''; break; case 'detail': $result = $args; break; default: $result = $args[$args['return']]; break; } return $result; }