Files
ultimatemember/includes/um-short-functions.php
T

1962 lines
45 KiB
PHP
Raw Normal View History

2014-12-15 22:38:07 +02:00
<?php
2014-12-20 18:02:41 +02:00
2015-11-05 19:51:31 +08:00
function um_mail_content_type( $content_type ) {
2017-10-03 16:29:04 +03:00
2015-11-05 19:51:31 +08:00
return 'text/html';
}
2016-01-09 17:22:42 -08:00
/***
2017-10-03 16:29:04 +03:00
*** @Trim string by char length
***/
function um_trim_string( $s, $length = 20 ) {
2017-10-03 16:29:04 +03:00
$s = strlen( $s ) > $length ? substr( $s, 0, $length ) . "..." : $s;
return $s;
}
/***
2017-10-03 16:29:04 +03:00
*** @Convert urls to clickable links
***/
function um_clickable_links( $s ) {
return preg_replace( '@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" class="um-link" target="_blank">$1</a>', $s );
}
2016-01-09 17:22:42 -08:00
/***
2017-10-03 16:29:04 +03:00
*** @Get where user should be headed after logging
***/
function um_dynamic_login_page_redirect( $redirect_to = '' ) {
2016-03-14 16:34:56 -07:00
$uri = um_get_core_page( 'login' );
2016-03-14 16:34:56 -07:00
2017-10-03 16:29:04 +03:00
if (!$redirect_to) {
$redirect_to = UM()->permalinks()->get_current_url();
}
2016-03-02 21:50:18 +08:00
2016-03-03 22:04:23 +08:00
$redirect_key = urlencode_deep( $redirect_to );
2016-03-02 21:50:18 +08:00
$uri = add_query_arg( 'redirect_to', $redirect_key, $uri );
return $uri;
}
2016-03-02 21:50:18 +08:00
/**
* Set redirect key
2017-10-03 16:29:04 +03:00
*
2016-03-14 16:34:56 -07:00
* @param string $url
2017-10-03 16:29:04 +03:00
*
2016-03-02 21:50:18 +08:00
* @return string $redirect_key
*/
2017-10-03 16:29:04 +03:00
function um_set_redirect_url( $url ) {
2016-03-14 16:34:56 -07:00
2017-10-03 16:29:04 +03:00
if (um_is_session_started() === false) {
session_start();
2016-03-03 22:04:23 +08:00
}
2017-10-03 16:29:04 +03:00
$redirect_key = wp_generate_password( 12, false );
2016-03-02 21:50:18 +08:00
$_SESSION['um_redirect_key'] = array( $redirect_key => $url );
return $redirect_key;
}
/**
* Set redirect key
2017-10-03 16:29:04 +03:00
*
2016-03-14 16:34:56 -07:00
* @param string $url
2017-10-03 16:29:04 +03:00
*
2016-03-02 21:50:18 +08:00
* @return string $redirect_key
*/
2017-10-03 16:29:04 +03:00
function um_get_redirect_url( $key ) {
2016-03-14 16:34:56 -07:00
2017-10-03 16:29:04 +03:00
if (um_is_session_started() === false) {
session_start();
2016-03-03 22:04:23 +08:00
}
2017-10-03 16:29:04 +03:00
if (isset( $_SESSION['um_redirect_key'][$key] )) {
2016-03-14 16:34:56 -07:00
2017-10-03 16:29:04 +03:00
$url = $_SESSION['um_redirect_key'][$key];
2016-03-02 21:50:18 +08:00
return $url;
2017-10-03 16:29:04 +03:00
} else {
2016-03-03 14:38:45 +08:00
2017-10-03 16:29:04 +03:00
if (isset( $_SESSION['um_redirect_key'] )) {
foreach ($_SESSION['um_redirect_key'] as $key => $url) {
2016-03-03 14:38:45 +08:00
return $url;
break;
}
}
2016-03-02 21:50:18 +08:00
}
return;
}
2016-03-03 22:04:23 +08:00
/**
* Checks if session has been started
2017-10-03 16:29:04 +03:00
*
2016-03-03 22:04:23 +08:00
* @return bool
2017-10-03 16:29:04 +03:00
*/
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;
}
2016-03-03 22:04:23 +08:00
}
2016-03-14 16:34:56 -07:00
2017-10-03 16:29:04 +03:00
return false;
2016-03-03 22:04:23 +08:00
}
2016-01-13 12:33:21 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @user clean basename
***/
2016-02-26 18:14:07 -08:00
function um_clean_user_basename( $value ) {
2016-01-13 12:33:21 +08:00
$raw_value = $value;
2017-10-03 16:29:04 +03:00
$value = str_replace( '.', ' ', $value );
$value = str_replace( '-', ' ', $value );
$value = str_replace( '+', ' ', $value );
2016-01-13 12:33:21 +08:00
2017-10-03 16:29:04 +03:00
$value = apply_filters( 'um_clean_user_basename_filter', $value, $raw_value );
2016-03-14 16:34:56 -07:00
2016-01-13 12:33:21 +08:00
return $value;
}
2017-10-03 16:29:04 +03:00
2015-11-05 19:51:31 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @convert template tags
***/
2015-11-05 19:51:31 +08:00
function um_convert_tags( $content, $args = array() ) {
$search = array(
'{display_name}',
'{first_name}',
'{last_name}',
'{gender}',
'{username}',
'{email}',
'{password}',
'{login_url}',
'{login_referrer}',
2015-11-05 19:51:31 +08:00
'{site_name}',
'{site_url}',
'{account_activation_link}',
'{password_reset_link}',
'{admin_email}',
'{user_profile_link}',
'{user_account_link}',
'{submitted_registration}',
'{user_avatar_url}',
);
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
$search = apply_filters( 'um_template_tags_patterns_hook', $search );
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
$replace = array(
2017-10-03 16:29:04 +03:00
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(),
2017-12-11 09:53:38 +02:00
UM()->options()->get( 'site_name' ),
2017-10-03 16:29:04 +03:00
get_bloginfo( 'url' ),
um_user( 'account_activation_link' ),
um_user( 'password_reset_link' ),
2015-11-05 19:51:31 +08:00
um_admin_email(),
um_user_profile_url(),
2017-10-03 16:29:04 +03:00
um_get_core_page( 'account' ),
2015-11-05 19:51:31 +08:00
um_user_submitted_registration(),
um_get_user_avatar_url(),
);
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
$replace = apply_filters( 'um_template_tags_replaces_hook', $replace );
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
$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 );
2015-11-05 19:51:31 +08:00
}
2016-01-09 17:22:42 -08:00
$regex = '~\{(usermeta:[^}]*)\}~';
2017-10-03 16:29:04 +03:00
preg_match_all( $regex, $content, $matches );
// Support for all usermeta keys
if ( ! empty( $matches[1] ) && is_array( $matches[1] ) ) {
foreach ( $matches[1] as $match ) {
2017-10-03 16:29:04 +03:00
$strip_key = str_replace( 'usermeta:', '', $match );
$content = str_replace( '{' . $match . '}', um_user( $strip_key ), $content );
}
}
2015-11-05 19:51:31 +08:00
return $content;
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
}
/**
* @function um_user_ip()
*
* @description This function returns the IP address of user.
*
* @usage <?php $user_ip = um_user_ip(); ?>
*
* @returns Returns the user's IP address.
*
* @example The example below can retrieve the user's IP address
2017-10-03 16:29:04 +03:00
*
* <?php
*
* $user_ip = um_user_ip();
* echo 'User IP address is: ' . $user_ip; // prints the user IP address e.g. 127.0.0.1
*
* ?>
*
*
*/
function um_user_ip() {
$ip = '127.0.0.1';
2017-10-03 16:29:04 +03:00
if (!empty( $_SERVER['HTTP_CLIENT_IP'] )) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
2017-10-03 16:29:04 +03:00
} else if (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'] )) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
2017-10-03 16:29:04 +03:00
} else if (!empty( $_SERVER['REMOTE_ADDR'] )) {
$ip = $_SERVER['REMOTE_ADDR'];
}
2017-10-03 16:29:04 +03:00
return apply_filters( 'um_user_ip', $ip );
2015-05-02 02:49:05 +03:00
}
2015-01-28 17:16:04 +02:00
2015-01-23 19:03:29 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @If conditions are met return true;
***/
2015-01-23 19:03:29 +02:00
function um_field_conditions_are_met( $data ) {
2017-10-03 16:29:04 +03:00
if (!isset( $data['conditions'] )) return true;
2016-01-09 17:22:42 -08:00
2015-01-23 19:03:29 +02:00
$state = 1;
2017-10-03 16:29:04 +03:00
foreach ($data['conditions'] as $k => $arr) {
if ($arr[0] == 'show') {
2016-01-09 17:22:42 -08:00
2015-01-23 19:03:29 +02:00
$val = $arr[3];
$op = $arr[2];
2017-10-03 16:29:04 +03:00
if (strstr( $arr[1], 'role_' ))
$arr[1] = 'role';
$field = um_profile( $arr[1] );
2017-10-03 16:29:04 +03:00
switch ($op) {
case 'equals to':
2017-01-09 16:45:07 +08:00
$field = maybe_unserialize( $field );
2017-01-09 16:45:07 +08:00
2017-10-03 16:29:04 +03:00
if (is_array( $field ))
$state = in_array( $val, $field ) ? 1 : 0;
else
$state = ( $field == $val ) ? 1 : 0;
2017-01-09 16:45:07 +08:00
2017-10-03 16:29:04 +03:00
break;
case 'not equals':
2017-01-09 16:45:07 +08:00
$field = maybe_unserialize( $field );
2017-10-03 16:29:04 +03:00
if (is_array( $field ))
$state = !in_array( $val, $field ) ? 1 : 0;
else
$state = ( $field != $val ) ? 1 : 0;
2017-01-09 16:45:07 +08:00
2017-10-03 16:29:04 +03:00
break;
case 'empty':
2017-10-03 16:29:04 +03:00
$state = ( !$field ) ? 1 : 0;
2017-10-03 16:29:04 +03:00
break;
case 'not empty':
$state = ( $field ) ? 1 : 0;
2017-10-03 16:29:04 +03:00
break;
2017-01-09 16:45:07 +08:00
case 'greater than':
2017-10-03 16:29:04 +03:00
if ($field > $val) {
2017-01-09 16:45:07 +08:00
$state = 1;
2017-10-03 16:29:04 +03:00
} else {
2017-01-09 16:45:07 +08:00
$state = 0;
2017-10-03 16:29:04 +03:00
}
break;
case 'less than':
if ($field < $val) {
2017-01-09 16:45:07 +08:00
$state = 1;
2017-10-03 16:29:04 +03:00
} else {
2017-01-09 16:45:07 +08:00
$state = 0;
2017-10-03 16:29:04 +03:00
}
break;
case 'contains':
if (strstr( $field, $val )) {
$state = 1;
} else {
2017-01-09 16:45:07 +08:00
$state = 0;
2017-10-03 16:29:04 +03:00
}
break;
2015-01-23 19:03:29 +02:00
}
2017-10-03 16:29:04 +03:00
} else if ($arr[0] == 'hide') {
2016-01-09 17:22:42 -08:00
2017-01-09 16:45:07 +08:00
$state = 1;
2015-01-23 19:03:29 +02:00
$val = $arr[3];
$op = $arr[2];
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (strstr( $arr[1], 'role_' ))
2017-01-09 16:45:07 +08:00
$arr[1] = 'role';
$field = um_profile( $arr[1] );
2017-10-03 16:29:04 +03:00
switch ($op) {
case 'equals to':
$field = maybe_unserialize( $field );
2017-10-03 16:29:04 +03:00
if (is_array( $field ))
$state = in_array( $val, $field ) ? 0 : 1;
else
$state = ( $field == $val ) ? 0 : 1;
2017-10-03 16:29:04 +03:00
break;
case 'not equals':
$field = maybe_unserialize( $field );
2017-10-03 16:29:04 +03:00
if (is_array( $field ))
$state = !in_array( $val, $field ) ? 0 : 1;
else
$state = ( $field != $val ) ? 0 : 1;
2017-10-03 16:29:04 +03:00
break;
case 'empty':
2017-10-03 16:29:04 +03:00
$state = ( !$field ) ? 0 : 1;
2017-10-03 16:29:04 +03:00
break;
case 'not empty':
$state = ( $field ) ? 0 : 1;
2017-10-03 16:29:04 +03:00
break;
case 'greater than':
if ($field <= $val) {
$state = 0;
} else {
2017-01-09 16:45:07 +08:00
$state = 1;
}
2017-10-03 16:29:04 +03:00
break;
case 'less than':
if ($field >= $val) {
$state = 0;
} else {
2017-01-09 16:45:07 +08:00
$state = 1;
}
2017-10-03 16:29:04 +03:00
break;
case 'contains':
if (strstr( $field, $val )) {
$state = 0;
} else {
2017-01-09 16:45:07 +08:00
$state = 1;
}
2017-10-03 16:29:04 +03:00
break;
2015-01-23 19:03:29 +02:00
}
}
}
2016-01-09 17:22:42 -08:00
return ( $state ) ? true : false;
2015-01-23 19:03:29 +02:00
}
2015-01-03 15:31:15 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Exit and redirect to home
***/
2015-01-03 15:31:15 +02:00
function um_redirect_home() {
2017-10-03 16:29:04 +03:00
2015-01-03 15:31:15 +02:00
exit( wp_redirect( home_url() ) );
}
2016-01-09 17:22:42 -08:00
function um_js_redirect( $url ) {
2017-10-03 16:29:04 +03:00
if (headers_sent() || empty( $url )) {
//for blank redirects
if ('' == $url) {
$url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
}
$funtext = "echo \"<script data-cfasync='false' type='text/javascript'>window.location = '" . $url . "'</script>\";";
register_shutdown_function( create_function( '', $funtext ) );
if (1 < ob_get_level()) {
while (ob_get_level() > 1) {
ob_end_clean();
}
}
?>
<script data-cfasync='false' type="text/javascript">
window.location = '<?php echo $url; ?>';
</script>
2017-10-03 16:29:04 +03:00
<?php
exit;
} else {
wp_redirect( $url );
}
exit;
}
2015-01-23 19:03:29 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Get limit of words from sentence
***/
2015-04-25 21:41:47 +02:00
function um_get_snippet( $str, $wordCount = 10 ) {
2017-10-03 16:29:04 +03:00
if (str_word_count( $str ) > $wordCount) {
$str = implode(
'',
array_slice(
preg_split(
'/([\s,\.;\?\!]+)/',
$str,
$wordCount * 2 + 1,
PREG_SPLIT_DELIM_CAPTURE
),
0,
$wordCount * 2 - 1
)
);
2015-04-25 21:41:47 +02:00
}
2017-10-03 16:29:04 +03:00
return $str;
2015-01-23 19:03:29 +02:00
}
2015-04-25 21:41:47 +02:00
2015-01-22 02:10:26 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Get submitted user information
***/
2015-02-13 15:26:36 +02:00
function um_user_submitted_registration( $style = false ) {
2015-01-22 02:10:26 +02:00
$output = null;
2016-01-09 17:22:42 -08:00
$data = um_user( 'submitted' );
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($style)
$output .= '<div class="um-admin-infobox">';
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (isset( $data ) && is_array( $data )) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
$data = apply_filters( 'um_email_registration_data', $data );
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
foreach ($data as $k => $v) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!is_array( $v ) && strstr( $v, 'ultimatemember/temp' )) {
2015-11-05 19:51:31 +08:00
$file = basename( $v );
$v = um_user_uploads_uri() . $file;
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!strstr( $k, 'user_pass' ) && !in_array( $k, array( 'g-recaptcha-response', 'request', '_wpnonce', '_wp_http_referer' ) )) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (is_array( $v )) {
$v = implode( ',', $v );
2015-01-22 02:10:26 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($k == 'timestamp') {
$k = __( 'date submitted', 'ultimate-member' );
$v = date( "d M Y H:i", $v );
2015-02-15 20:31:41 +02:00
}
2016-02-25 19:11:00 -08:00
2017-10-03 16:29:04 +03:00
if ($style) {
if (!$v) $v = __( '(empty)', 'ultimate-member' );
2015-02-13 15:26:36 +02:00
$output .= "<p><label>$k</label><span>$v</span></p>";
} else {
$output .= "$k: $v" . "<br />";
2015-02-13 15:26:36 +02:00
}
2016-01-09 17:22:42 -08:00
2015-01-22 02:10:26 +02:00
}
2016-01-09 17:22:42 -08:00
2015-01-22 02:10:26 +02:00
}
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($style)
$output .= '</div>';
2016-01-09 17:22:42 -08:00
2015-01-22 02:10:26 +02:00
return $output;
}
2016-01-09 17:22:42 -08:00
2015-01-03 15:31:15 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Show filtered social link
***/
2015-01-03 15:31:15 +02:00
function um_filtered_social_link( $key, $match ) {
$value = um_profile( $key );
$submatch = str_replace( 'https://', '', $match );
$submatch = str_replace( 'http://', '', $submatch );
2017-10-03 16:29:04 +03:00
if (strstr( $value, $submatch )) {
2015-01-03 15:31:15 +02:00
$value = 'https://' . $value;
2017-10-03 16:29:04 +03:00
} else if (strpos( $value, 'http' ) !== 0) {
2015-01-03 15:31:15 +02:00
$value = $match . $value;
}
2017-10-03 16:29:04 +03:00
$value = str_replace( 'https://https://', 'https://', $value );
$value = str_replace( 'http://https://', 'https://', $value );
$value = str_replace( 'https://http://', 'https://', $value );
2015-01-03 15:31:15 +02:00
return $value;
}
2016-01-09 17:22:42 -08:00
/**
* Get filtered meta value after applying hooks
*
* @param $key
* @param bool $data
* @return mixed|string|void
*/
2015-01-03 15:31:15 +02:00
function um_filtered_value( $key, $data = false ) {
$value = um_user( $key );
2016-01-09 17:22:42 -08:00
if ( ! $data ) {
$data = UM()->builtin()->get_specific_field( $key );
2015-01-03 15:31:15 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
$type = ( isset( $data['type'] ) ) ? $data['type'] : '';
2015-01-03 15:31:15 +02:00
2017-10-03 16:29:04 +03:00
$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 );
2016-01-09 17:22:42 -08:00
2015-01-03 15:31:15 +02:00
return $value;
}
2016-01-09 17:22:42 -08:00
2015-02-08 01:49:10 +02:00
2016-09-27 21:40:01 +08:00
function um_profile_id() {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (um_get_requested_user()) {
2016-09-27 21:40:01 +08:00
return um_get_requested_user();
2017-10-03 16:29:04 +03:00
} else if (is_user_logged_in() && get_current_user_id()) {
2016-09-27 21:40:01 +08:00
return get_current_user_id();
}
2015-11-05 19:51:31 +08:00
2016-09-27 21:40:01 +08:00
return 0;
}
2014-12-30 20:18:29 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Check that temp upload is valid
***/
2014-12-30 20:18:29 +02:00
function um_is_temp_upload( $url ) {
2017-08-07 16:30:12 +03:00
2017-10-03 16:29:04 +03:00
if (filter_var( $url, FILTER_VALIDATE_URL ) === false)
$url = realpath( $url );
2017-08-07 16:30:12 +03:00
2017-10-03 16:29:04 +03:00
if (!$url)
2017-08-07 16:30:12 +03:00
return false;
$url = explode( '/ultimatemember/temp/', $url );
2017-10-03 16:29:04 +03:00
if (isset( $url[1] )) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (strstr( $url[1], '../' ) || strstr( $url[1], '%' ))
2015-03-12 18:22:29 +02:00
return false;
$src = UM()->files()->upload_temp . $url[1];
2017-10-03 16:29:04 +03:00
if (!file_exists( $src ))
2014-12-30 20:18:29 +02:00
return false;
2017-08-07 16:30:12 +03:00
2014-12-30 20:18:29 +02:00
return $src;
}
2017-10-03 16:29:04 +03:00
2014-12-30 20:18:29 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-29 15:51:55 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Check that temp image is valid
***/
2014-12-29 15:51:55 +02:00
function um_is_temp_image( $url ) {
2017-10-03 16:29:04 +03:00
$url = explode( '/ultimatemember/temp/', $url );
if (isset( $url[1] )) {
$src = UM()->files()->upload_temp . $url[1];
2017-10-03 16:29:04 +03:00
if (!file_exists( $src ))
2014-12-29 15:51:55 +02:00
return false;
2017-10-03 16:29:04 +03:00
list( $width, $height, $type, $attr ) = @getimagesize( $src );
if (isset( $width ) && isset( $height ))
2014-12-29 15:51:55 +02:00
return $src;
}
2017-10-03 16:29:04 +03:00
2014-12-29 15:51:55 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2017-11-14 12:36:44 +02:00
/**
* Get a translated core page URL
*
* @param $post_id
* @param $language
* @return bool|false|string
*/
2017-10-03 16:29:04 +03:00
function um_get_url_for_language( $post_id, $language ) {
2017-11-14 12:36:44 +02:00
if ( ! um_is_wpml_active() )
return '';
2017-10-03 16:29:04 +03:00
$lang_post_id = icl_object_id( $post_id, 'page', true, $language );
2016-01-09 17:22:42 -08:00
2017-11-14 12:36:44 +02:00
if ( $lang_post_id != 0 ) {
2015-11-05 19:51:31 +08:00
$url = get_permalink( $lang_post_id );
2017-10-03 16:29:04 +03:00
} else {
2015-11-05 19:51:31 +08:00
// No page found, it's most likely the homepage
global $sitepress;
$url = $sitepress->language_url( $language );
}
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
return $url;
}
2016-01-09 17:22:42 -08:00
2017-11-14 12:36:44 +02:00
/**
* 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;
}
2015-11-05 19:51:31 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @Get core page url
***/
2015-11-05 19:51:31 +08:00
function um_time_diff( $time1, $time2 ) {
return UM()->datetime()->time_diff( $time1, $time2 );
2015-11-05 19:51:31 +08:00
}
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @Get user's last login timestamp
***/
2015-11-05 19:51:31 +08:00
function um_user_last_login_timestamp( $user_id ) {
$value = get_user_meta( $user_id, '_um_last_login', true );
2017-10-03 16:29:04 +03:00
if ($value)
2015-11-05 19:51:31 +08:00
return $value;
2017-10-03 16:29:04 +03:00
2015-11-05 19:51:31 +08:00
return '';
}
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @Get user's last login time
***/
2015-11-05 19:51:31 +08:00
function um_user_last_login_date( $user_id ) {
$value = get_user_meta( $user_id, '_um_last_login', true );
2017-10-03 16:29:04 +03:00
if ($value)
return date_i18n( 'F d, Y', $value );
2015-11-05 19:51:31 +08:00
return '';
}
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @Get user's last login (time diff)
***/
2015-11-05 19:51:31 +08:00
function um_user_last_login( $user_id ) {
$value = get_user_meta( $user_id, '_um_last_login', true );
2017-10-03 16:29:04 +03:00
if ($value) {
$value = um_time_diff( $value, current_time( 'timestamp' ) );
2015-11-05 19:51:31 +08:00
} else {
$value = '';
}
2017-10-03 16:29:04 +03:00
2015-11-05 19:51:31 +08:00
return $value;
}
2016-01-09 17:22:42 -08:00
2014-12-20 18:02:41 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Get core page url
***/
function um_get_core_page( $slug, $updated = false ) {
2015-11-05 19:51:31 +08:00
$url = '';
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (isset( UM()->config()->permalinks[$slug] )) {
$url = get_permalink( UM()->config()->permalinks[$slug] );
if ($updated)
$url = add_query_arg( 'updated', esc_attr( $updated ), $url );
2015-11-05 19:51:31 +08:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (function_exists( 'icl_get_current_language' ) && icl_get_current_language() != icl_get_default_language()) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
$url = um_get_url_for_language( UM()->config()->permalinks[$slug], icl_get_current_language() );
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (get_post_meta( get_the_ID(), '_um_wpml_account', true ) == 1) {
2015-11-05 19:51:31 +08:00
$url = get_permalink( get_the_ID() );
}
2017-10-03 16:29:04 +03:00
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() );
2015-11-05 19:51:31 +08:00
}
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($url) {
$url = apply_filters( 'um_get_core_page_filter', $url, $slug, $updated );
2014-12-22 01:45:24 +02:00
return $url;
2014-12-22 15:09:14 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-20 18:02:41 +02:00
return '';
}
2016-01-09 17:22:42 -08:00
2015-11-05 19:51:31 +08:00
/***
2017-10-03 16:29:04 +03:00
*** @boolean check if we are on UM page
***/
2015-11-05 19:51:31 +08:00
function is_ultimatemember() {
global $post;
2017-10-03 16:29:04 +03:00
if (isset( $post->ID ) && in_array( $post->ID, UM()->config()->permalinks ))
2015-11-05 19:51:31 +08:00
return true;
2017-10-03 16:29:04 +03:00
2015-11-05 19:51:31 +08:00
return false;
}
2016-01-09 17:22:42 -08:00
/**
* 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
2017-10-03 16:29:04 +03:00
*
* @return bool
*/
2014-12-22 01:45:24 +02:00
function um_is_core_page( $page ) {
global $post;
2017-10-03 16:29:04 +03:00
if (isset( $post->ID ) && isset( UM()->config()->permalinks[$page] ) && $post->ID == UM()->config()->permalinks[$page])
2014-12-22 01:45:24 +02:00
return true;
2017-10-03 16:29:04 +03:00
if (isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $page, true ) == 1)
2015-11-05 19:51:31 +08:00
return true;
2016-02-01 21:10:24 +08:00
2017-10-03 16:29:04 +03:00
if (isset( $post->ID )) {
2016-02-05 13:54:12 +08:00
$_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true );
2016-02-01 21:10:24 +08:00
2017-10-03 16:29:04 +03:00
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 ) {
2017-10-03 16:29:04 +03:00
if (isset( $post->ID ) && isset( UM()->config()->permalinks[$core_page] ) && $post->ID == UM()->config()->permalinks[$core_page])
return true;
2017-10-03 16:29:04 +03:00
if (isset( $post->ID ) && get_post_meta( $post->ID, '_um_wpml_' . $core_page, true ) == 1)
return true;
2017-10-03 16:29:04 +03:00
if (isset( $post->ID )) {
$_icl_lang_duplicate_of = get_post_meta( $post->ID, '_icl_lang_duplicate_of', true );
2017-10-03 16:29:04 +03:00
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 ))
2016-02-05 13:54:12 +08:00
return true;
}
2016-02-01 21:10:24 +08:00
2014-12-22 01:45:24 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2015-01-14 00:16:44 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Is core URL
***/
2015-01-14 00:16:44 +02:00
function um_is_core_uri() {
$array = UM()->config()->permalinks;
2017-10-03 16:29:04 +03:00
$current_url = UM()->permalinks()->get_current_url( get_option( 'permalink_structure' ) );
2015-01-28 17:16:04 +02:00
2017-10-03 16:29:04 +03:00
if (!isset( $array ) || !is_array( $array )) return false;
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
foreach ($array as $k => $id) {
2015-01-28 17:16:04 +02:00
$page_url = get_permalink( $id );
2017-10-03 16:29:04 +03:00
if (strstr( $current_url, $page_url ))
2015-01-14 00:16:44 +02:00
return true;
}
2017-10-03 16:29:04 +03:00
2015-01-14 00:16:44 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Check value of queried search in text input
***/
2016-04-16 20:28:20 +08:00
function um_queried_search_value( $filter, $echo = true ) {
$value = '';
2017-10-03 16:29:04 +03:00
if (isset( $_REQUEST['um_search'] )) {
$query = UM()->permalinks()->get_query_array();
2017-10-03 16:29:04 +03:00
if (isset( $query[$filter] ) && $query[$filter] != '') {
$value = stripslashes_deep( $query[$filter] );
2015-11-05 19:51:31 +08:00
}
2014-12-15 22:38:07 +02:00
}
2017-10-03 16:29:04 +03:00
if ($echo) {
2016-04-16 20:28:20 +08:00
echo $value;
2017-10-03 16:29:04 +03:00
return '';
} else {
2016-04-16 20:28:20 +08:00
return $value;
}
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Check whether item in dropdown is selected in query-url
***/
2014-12-15 22:38:07 +02:00
function um_select_if_in_query_params( $filter, $val ) {
$selected = false;
2017-10-03 16:29:04 +03:00
if (isset( $_REQUEST['um_search'] )) {
$query = UM()->permalinks()->get_query_array();
2017-10-03 16:29:04 +03:00
if (isset( $query[$filter] ) && $val == $query[$filter])
$selected = true;
$selected = apply_filters( 'um_selected_if_in_query_params', $selected, $filter, $val );
2014-12-15 22:38:07 +02:00
}
echo $selected ? 'selected="selected"' : '';
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get styling defaults
***/
2014-12-15 22:38:07 +02:00
function um_styling_defaults( $mode ) {
2017-10-03 16:29:04 +03:00
$new_arr = array();
$core_form_meta_all = UM()->config()->core_form_meta_all;
$core_global_meta_all = UM()->config()->core_global_meta_all;
2017-12-11 09:53:38 +02:00
foreach ( $core_form_meta_all as $k => $v ) {
$s = str_replace( $mode . '_', '', $k );
2017-10-03 16:29:04 +03:00
if (strstr( $k, '_um_' . $mode . '_' ) && !in_array( $s, $core_global_meta_all )) {
$a = str_replace( '_um_' . $mode . '_', '', $k );
$b = str_replace( '_um_', '', $k );
2017-12-11 09:53:38 +02:00
$new_arr[$a] = UM()->options()->get( $b );
2017-10-03 16:29:04 +03:00
} else if (in_array( $k, $core_global_meta_all )) {
$a = str_replace( '_um_', '', $k );
2017-12-11 09:53:38 +02:00
$new_arr[$a] = UM()->options()->get( $a );
2014-12-15 22:38:07 +02:00
}
}
return $new_arr;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get meta option default
***/
2014-12-15 22:38:07 +02:00
function um_get_metadefault( $id ) {
$core_form_meta_all = UM()->config()->core_form_meta_all;
2017-10-03 16:29:04 +03:00
return isset( $core_form_meta_all['_um_' . $id] ) ? $core_form_meta_all['_um_' . $id] : '';
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @check if a legitimate password reset request is in action
***/
2014-12-22 01:45:24 +02:00
function um_requesting_password_reset() {
2017-10-03 16:29:04 +03:00
if (um_is_core_page( 'password-reset' ) && isset( $_POST['_um_password_reset'] ) == 1)
2014-12-15 22:38:07 +02:00
return true;
2017-10-03 16:29:04 +03:00
2014-12-15 22:38:07 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @check if a legitimate password change request is in action
***/
2014-12-22 01:45:24 +02:00
function um_requesting_password_change() {
2017-10-03 16:29:04 +03:00
if (um_is_core_page( 'account' ) && isset( $_POST['_um_account'] ) == 1)
2014-12-15 22:38:07 +02:00
return true;
2017-10-03 16:29:04 +03:00
else if (isset( $_POST['_um_password_change'] ) && $_POST['_um_password_change'] == 1)
2016-02-10 14:31:27 -08:00
return true;
2017-10-03 16:29:04 +03:00
2014-12-15 22:38:07 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @boolean for account page editing
***/
2014-12-22 01:45:24 +02:00
function um_submitting_account_page() {
2017-10-03 16:29:04 +03:00
if (isset( $_POST['_um_account'] ) && $_POST['_um_account'] == 1 && is_user_logged_in())
2014-12-22 01:45:24 +02:00
return true;
2017-10-03 16:29:04 +03:00
2014-12-15 22:38:07 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get a user's display name
***/
2014-12-15 22:38:07 +02:00
function um_get_display_name( $user_id ) {
2015-11-05 19:51:31 +08:00
um_fetch_user( $user_id );
2017-10-03 16:29:04 +03:00
$name = um_user( 'display_name' );
2015-11-05 19:51:31 +08:00
um_reset_user();
2017-10-03 16:29:04 +03:00
2015-11-05 19:51:31 +08:00
return $name;
2014-12-15 22:38:07 +02:00
}
2015-11-05 19:51:31 +08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get members to show in directory
***/
2014-12-15 22:38:07 +02:00
function um_members( $argument ) {
2017-10-03 16:29:04 +03:00
return UM()->members()->results[$argument];
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
/**
* @function um_reset_user_clean()
*
2017-10-03 16:29:04 +03:00
* @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 <?php um_reset_user_clean(); ?>
*
* @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
2017-10-03 16:29:04 +03:00
*
* <?php um_reset_user_clean(); ?>
*
*
*/
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
2017-10-03 16:29:04 +03:00
* retrieving the details of a specific user.
*
* @usage <?php um_reset_user(); ?>
*
* @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
2017-10-03 16:29:04 +03:00
*
* <?php um_reset_user(); ?>
*
*
*/
function um_reset_user() {
UM()->user()->reset();
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @gets the queried user
***/
2014-12-15 22:38:07 +02:00
function um_queried_user() {
2017-10-03 16:29:04 +03:00
return get_query_var( 'um_user' );
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Sets the requested user
***/
2014-12-15 22:38:07 +02:00
function um_set_requested_user( $user_id ) {
UM()->user()->target_id = $user_id;
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Gets the requested user
***/
2014-12-15 22:38:07 +02:00
function um_get_requested_user() {
2017-10-03 16:29:04 +03:00
if (!empty( UM()->user()->target_id ))
return UM()->user()->target_id;
2017-10-03 16:29:04 +03:00
2014-12-15 22:38:07 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @remove edit profile args from url
***/
2016-04-05 14:09:33 +08:00
function um_edit_my_profile_cancel_uri( $url = '' ) {
2017-10-03 16:29:04 +03:00
if (empty( $url )) {
2016-04-05 14:09:33 +08:00
$url = remove_query_arg( 'um_action' );
$url = remove_query_arg( 'profiletab', $url );
2017-10-03 16:29:04 +03:00
$url = add_query_arg( 'profiletab', 'main', $url );
2016-04-05 14:09:33 +08:00
}
$url = apply_filters( 'um_edit_profile_cancel_uri', $url );
2014-12-15 22:38:07 +02:00
return $url;
}
2016-01-09 17:22:42 -08:00
2015-03-17 16:03:32 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @boolean for profile edit page
***/
2015-03-17 16:03:32 +02:00
function um_is_on_edit_profile() {
2017-10-03 16:29:04 +03:00
if (isset( $_REQUEST['profiletab'] ) && isset( $_REQUEST['um_action'] )) {
if ($_REQUEST['profiletab'] == 'main' && $_REQUEST['um_action'] == 'edit') {
2015-03-17 16:03:32 +02:00
return true;
}
}
2017-10-03 16:29:04 +03:00
2015-03-17 16:03:32 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @can view field
***/
2014-12-15 22:38:07 +02:00
function um_can_view_field( $data ) {
2017-10-03 16:29:04 +03:00
if (!isset( UM()->fields()->set_mode ))
UM()->fields()->set_mode = '';
2014-12-15 22:38:07 +02:00
2017-10-03 16:29:04 +03:00
if (isset( $data['public'] ) && UM()->fields()->set_mode != 'register') {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!is_user_logged_in() && $data['public'] != '1') return false;
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (is_user_logged_in()) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($data['public'] == '-3' && !um_is_user_himself() && !in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] ))
2015-12-15 16:22:35 +02:00
return false;
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!um_is_user_himself() && $data['public'] == '-1' && !UM()->roles()->um_user_can( 'can_edit_everyone' ))
2014-12-15 22:38:07 +02:00
return false;
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($data['public'] == '-2' && $data['roles'])
if (!in_array( UM()->roles()->um_get_user_role( get_current_user_id() ), $data['roles'] ))
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
return true;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @checks if user can view profile
***/
function um_can_view_profile( $user_id ) {
2017-10-03 16:29:04 +03:00
if (!um_user( 'can_view_all' ) && $user_id != get_current_user_id() && is_user_logged_in()) return false;
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (UM()->roles()->um_current_user_can( 'edit', $user_id )) {
2015-03-18 18:38:22 +02:00
return true;
2015-01-05 01:33:17 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!is_user_logged_in()) {
if (UM()->user()->is_private_profile( $user_id )) {
2014-12-15 22:38:07 +02:00
return false;
} else {
return true;
}
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!um_user( 'can_access_private_profile' ) && UM()->user()->is_private_profile( $user_id )) return false;
2015-04-25 21:41:47 +02:00
2017-10-03 16:29:04 +03:00
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' ) )) {
2014-12-15 22:38:07 +02:00
return false;
}
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
return true;
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @boolean check for not same user
***/
2014-12-15 22:38:07 +02:00
function um_is_user_himself() {
2017-10-03 16:29:04 +03:00
if (um_get_requested_user() && um_get_requested_user() != get_current_user_id())
2014-12-15 22:38:07 +02:00
return false;
2017-10-03 16:29:04 +03:00
2014-12-15 22:38:07 +02:00
return true;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @can edit field
***/
2014-12-15 22:38:07 +02:00
function um_can_edit_field( $data ) {
2017-10-03 16:29:04 +03:00
if (isset( UM()->fields()->editing ) && UM()->fields()->editing == true &&
isset( UM()->fields()->set_mode ) && UM()->fields()->set_mode == 'profile'
) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (is_user_logged_in() && isset( $data['editable'] ) && $data['editable'] == 0) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (isset( $data['public'] ) && $data['public'] == "-2") {
2017-01-09 16:45:07 +08:00
return true;
2017-01-20 23:19:49 +08:00
}
2017-10-03 16:29:04 +03:00
if (um_user( 'can_edit_everyone' )) return true;
if (um_is_user_himself() && !um_user( 'can_edit_everyone' )) {
2017-01-20 23:19:49 +08:00
return true;
}
2017-10-03 16:29:04 +03:00
if (!um_is_user_himself() && !UM()->roles()->um_user_can( 'can_edit_everyone' ))
2014-12-15 22:38:07 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
return true;
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2015-01-11 19:07:55 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @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;
2015-01-11 19:07:55 +02:00
return false;
}
2016-01-09 17:22:42 -08:00
2015-04-07 20:10:23 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Returns the edit profile link
***/
function um_edit_profile_url() {
if (um_is_core_page( 'user' )) {
$url = UM()->permalinks()->get_current_url();
2017-10-03 16:29:04 +03:00
} else {
2016-05-16 20:03:39 +08:00
$url = um_user_profile_url();
}
2017-10-03 16:29:04 +03:00
$url = remove_query_arg( 'profiletab', $url );
$url = remove_query_arg( 'subnav', $url );
2015-04-25 21:41:47 +02:00
$url = add_query_arg( 'profiletab', 'main', $url );
2017-10-03 16:29:04 +03:00
$url = add_query_arg( 'um_action', 'edit', $url );
2015-04-07 20:10:23 +02:00
return $url;
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @checks if user can edit his profile
***/
function um_can_edit_my_profile() {
2017-10-03 16:29:04 +03:00
if (!is_user_logged_in()) return false;
if (!um_user( 'can_edit_profile' )) return false;
2014-12-15 22:38:07 +02:00
return true;
2015-01-05 01:33:17 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @short for admin e-mail
***/
function um_admin_email() {
2017-12-11 09:53:38 +02:00
return UM()->options()->get( 'admin_email' );
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Display a link to profile page
***/
2014-12-15 22:38:07 +02:00
function um_user_profile_url() {
return UM()->user()->get_profile_url( um_user( 'ID' ) );
2014-12-15 22:38:07 +02:00
}
/***
2017-10-03 16:29:04 +03:00
*** @Get all UM roles in array
***/
2014-12-15 22:38:07 +02:00
function um_get_roles() {
return UM()->roles()->get_roles();
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
/**
* @function um_fetch_user()
*
* @description This function sets a user and allow you to retrieve any information for the retrieved user
*
* @usage <?php um_fetch_user( $user_id ); ?>
*
* @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.
2017-10-03 16:29:04 +03:00
*
* <?php
*
* um_fetch_user(5);
* echo um_user('display_name'); // returns the display name of user ID 5
*
* ?>
*
* @example In the following example you can fetch the profile of a logged-in user dynamically.
2017-10-03 16:29:04 +03:00
*
* <?php
*
* um_fetch_user( get_current_user_id() );
* echo um_user('display_name'); // returns the display name of logged-in user
*
* ?>
*
*/
function um_fetch_user( $user_id ) {
UM()->user()->set( $user_id );
}
2016-01-09 17:22:42 -08:00
2014-12-15 22:38:07 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @Load profile key
***/
function um_profile( $key ) {
2017-10-03 16:29:04 +03:00
if (!empty( UM()->user()->profile[$key] )) {
$value = apply_filters( "um_profile_{$key}__filter", UM()->user()->profile[$key] );
2014-12-15 22:38:07 +02:00
} else {
2017-10-03 16:29:04 +03:00
$value = apply_filters( "um_profile_{$key}_empty__filter", false );
2014-12-15 22:38:07 +02:00
}
2016-07-22 22:58:40 +08:00
return $value;
2017-10-03 16:29:04 +03:00
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2015-05-18 14:12:50 +03:00
/***
2017-10-03 16:29:04 +03:00
*** @Get youtube video ID from url
***/
function um_youtube_id_from_url( $url ) {
2016-01-09 17:22:42 -08:00
$pattern =
2015-05-18 14:12:50 +03:00
'%^# 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.
2017-10-03 16:29:04 +03:00
$%x';
$result = preg_match( $pattern, $url, $matches );
2015-05-18 14:12:50 +03:00
if (false !== $result) {
return $matches[1];
}
2017-10-03 16:29:04 +03:00
2015-05-18 14:12:50 +03:00
return false;
}
2016-01-09 17:22:42 -08:00
2014-12-29 15:51:55 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @user uploads uri
***/
2014-12-29 15:51:55 +02:00
function um_user_uploads_uri() {
2017-10-03 16:29:04 +03:00
if (is_ssl()) {
UM()->files()->upload_baseurl = str_replace( "http://", "https://", UM()->files()->upload_baseurl );
}
2017-10-03 16:29:04 +03:00
$uri = UM()->files()->upload_baseurl . um_user( 'ID' ) . '/';
2014-12-29 15:51:55 +02:00
return $uri;
}
2016-01-09 17:22:42 -08:00
2014-12-30 20:18:29 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @user uploads directory
***/
2014-12-30 20:18:29 +02:00
function um_user_uploads_dir() {
2017-10-03 16:29:04 +03:00
$uri = UM()->files()->upload_basedir . um_user( 'ID' ) . '/';
2014-12-30 20:18:29 +02:00
return $uri;
}
2016-01-09 17:22:42 -08:00
2014-12-30 20:18:29 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @find closest number in an array
***/
function um_closest_num( $array, $number ) {
sort( $array );
2014-12-29 21:14:22 +02:00
foreach ($array as $a) {
if ($a >= $number) return $a;
}
2017-10-03 16:29:04 +03:00
return end( $array );
2014-12-29 21:14:22 +02:00
}
2014-12-29 15:51:55 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get cover uri
***/
2014-12-29 15:51:55 +02:00
function um_get_cover_uri( $image, $attrs ) {
$uri = false;
2017-10-03 16:29:04 +03:00
$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' );
2014-12-29 15:51:55 +02:00
}
2017-10-03 16:29:04 +03:00
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' );
2014-12-29 15:51:55 +02:00
}
2017-10-03 16:29:04 +03:00
2014-12-29 15:51:55 +02:00
return $uri;
}
2016-01-09 17:22:42 -08:00
2015-04-07 20:10:23 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get avatar URL instead of image
***/
function um_get_avatar_url( $get_avatar ) {
preg_match( '/src="(.*?)"/i', $get_avatar, $matches );
2015-04-07 20:10:23 +02:00
return $matches[1];
}
2016-01-09 17:22:42 -08:00
2014-12-29 15:51:55 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get avatar uri
***/
2014-12-29 15:51:55 +02:00
function um_get_avatar_uri( $image, $attrs ) {
$uri = false;
2014-12-31 20:04:10 +02:00
$find = false;
2017-10-03 16:29:04 +03:00
$ext = '.' . pathinfo( $image, PATHINFO_EXTENSION );
$cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) );
2016-06-15 14:47:54 +08:00
2017-10-03 16:29:04 +03:00
if (!empty( $cache_time )) {
$cache_time = "?{$cache_time}";
2017-04-07 22:03:31 +08:00
}
2016-06-15 14:47:54 +08:00
2017-10-03 16:29:04 +03:00
if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}" )) {
2017-04-07 22:03:31 +08:00
$uri = um_user_uploads_uri() . "profile_photo-{$attrs}{$ext}{$cache_time}";
2016-01-09 17:22:42 -08:00
2014-12-29 21:14:22 +02:00
} else {
2016-01-09 17:22:42 -08:00
2017-12-11 09:53:38 +02:00
$sizes = UM()->options()->get( 'photo_thumb_sizes' );
2017-10-03 16:29:04 +03:00
if (is_array( $sizes )) $find = um_closest_num( $sizes, $attrs );
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}" )) {
2016-01-09 17:22:42 -08:00
2017-04-07 22:03:31 +08:00
$uri = um_user_uploads_uri() . "profile_photo-{$find}{$ext}{$cache_time}";
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
} else if (file_exists( UM()->files()->upload_basedir . um_user( 'ID' ) . "/profile_photo{$ext}" )) {
2016-01-09 17:22:42 -08:00
2017-04-07 22:03:31 +08:00
$uri = um_user_uploads_uri() . "profile_photo{$ext}{$cache_time}";
2016-01-09 17:22:42 -08:00
2014-12-29 21:14:22 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($attrs == 'original') {
2017-04-07 22:03:31 +08:00
$uri = um_user_uploads_uri() . "profile_photo{$ext}{$cache_time}";
2015-05-02 02:49:05 +03:00
}
2016-01-09 17:22:42 -08:00
2014-12-29 15:51:55 +02:00
}
2017-10-03 16:29:04 +03:00
2014-12-29 15:51:55 +02:00
return $uri;
}
2016-01-09 17:22:42 -08:00
/**
* Default avatar URL
*
* @return string
*/
function um_get_default_avatar_uri() {
2017-12-11 09:53:38 +02:00
$uri = UM()->options()->get( 'default_avatar' );
2017-10-03 16:29:04 +03:00
$uri = !empty( $uri['url'] ) ? $uri['url'] : '';
2017-11-02 18:13:50 +02:00
if ( ! $uri ) {
2015-01-03 15:31:15 +02:00
$uri = um_url . 'assets/img/default_avatar.jpg';
} else {
//http <-> https compatibility default avatar option of SSL was changed
$url_array = parse_url( $uri );
2017-10-03 16:29:04 +03:00
if (is_ssl() && $url_array['scheme'] == 'http') {
$uri = str_replace( 'http://', 'https://', $uri );
2017-10-03 16:29:04 +03:00
} else if (!is_ssl() && $url_array['scheme'] == 'https') {
$uri = str_replace( 'https://', 'http://', $uri );
}
}
2014-12-29 15:51:55 +02:00
return $uri;
}
2016-01-09 17:22:42 -08:00
2015-03-01 00:44:04 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @get user avatar url
***/
2015-03-01 00:44:04 +02:00
function um_get_user_avatar_url() {
2017-10-03 16:29:04 +03:00
if (um_profile( 'profile_photo' )) {
$avatar_uri = um_get_avatar_uri( um_profile( 'profile_photo' ), 32 );
2015-03-01 00:44:04 +02:00
} else {
$avatar_uri = um_get_default_avatar_uri();
}
2017-10-03 16:29:04 +03:00
2015-03-01 00:44:04 +02:00
return $avatar_uri;
}
2016-01-09 17:22:42 -08:00
2015-02-01 01:30:04 +02:00
/***
2017-10-03 16:29:04 +03:00
*** @default cover
***/
2015-02-01 01:30:04 +02:00
function um_get_default_cover_uri() {
2017-12-11 09:53:38 +02:00
$uri = UM()->options()->get( 'default_cover' );
2017-10-03 16:29:04 +03:00
$uri = !empty( $uri['url'] ) ? $uri['url'] : '';
if ($uri) {
$uri = apply_filters( 'um_get_default_cover_uri_filter', $uri );
2015-02-01 01:30:04 +02:00
return $uri;
}
2017-10-03 16:29:04 +03:00
2015-02-01 01:30:04 +02:00
return '';
}
2015-02-08 01:49:10 +02:00
function um_user( $data, $attrs = null ) {
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
switch ($data) {
2016-01-09 17:22:42 -08:00
default:
2016-01-09 17:22:42 -08:00
$value = um_profile( $data );
2016-01-09 17:22:42 -08:00
$value = maybe_unserialize( $value );
2017-10-03 16:29:04 +03:00
if (in_array( $data, array( 'role', 'gender' ) )) {
if (is_array( $value )) {
$value = implode( ",", $value );
2016-08-24 20:14:22 +08:00
}
2017-10-03 16:29:04 +03:00
2016-09-07 17:10:06 +08:00
return $value;
2016-03-01 23:18:03 +08:00
}
return $value;
break;
2016-01-09 17:22:42 -08:00
2017-03-03 16:37:43 +08:00
case 'user_email':
2017-10-03 16:29:04 +03:00
$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' );
2017-03-03 16:37:43 +08:00
}
$value = um_profile( $data );
2017-10-03 16:29:04 +03:00
2017-03-03 16:37:43 +08:00
return $value;
break;
case 'first_name':
case 'last_name':
$name = um_profile( $data );
2017-12-11 09:53:38 +02:00
if ( UM()->options()->get( 'force_display_name_capitlized' ) ) {
2017-10-03 16:29:04 +03:00
$name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) );
}
2017-10-03 16:29:04 +03:00
$name = apply_filters( "um_user_{$data}_case", $name );
return $name;
break;
case 'full_name':
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (um_user( 'first_name' ) && um_user( 'last_name' )) {
$full_name = um_user( 'first_name' ) . ' ' . um_user( 'last_name' );
} else {
2017-10-03 16:29:04 +03:00
$full_name = um_user( 'display_name' );
}
2016-01-09 17:22:42 -08:00
$full_name = UM()->validation()->safe_name_in_url( $full_name );
2016-01-09 17:22:42 -08:00
// update full_name changed
2017-10-03 16:29:04 +03:00
if (um_profile( $data ) !== $full_name) {
update_user_meta( um_user( 'ID' ), 'full_name', $full_name );
}
2016-01-09 17:22:42 -08:00
return $full_name;
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
break;
2016-04-09 14:50:04 +08:00
case 'first_and_last_name_initial':
2017-10-03 16:29:04 +03:00
2016-04-09 14:50:04 +08:00
$f_and_l_initial = '';
2017-10-03 16:29:04 +03:00
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 {
2016-04-09 14:50:04 +08:00
$f_and_l_initial = um_profile( $data );
}
$f_and_l_initial = UM()->validation()->safe_name_in_url( $f_and_l_initial );
2016-04-09 14:50:04 +08:00
2017-12-11 09:53:38 +02:00
if ( UM()->options()->get( 'force_display_name_capitlized' ) ) {
2017-10-03 16:29:04 +03:00
$name = implode( '-', array_map( 'ucfirst', explode( '-', $f_and_l_initial ) ) );
} else {
$name = $f_and_l_initial;
}
2017-10-03 16:29:04 +03:00
2016-04-20 20:18:14 +08:00
return $name;
2016-04-09 14:50:04 +08:00
2017-10-03 16:29:04 +03:00
break;
2016-01-09 17:22:42 -08:00
case 'display_name':
2016-01-04 11:09:41 +08:00
2017-12-11 09:53:38 +02:00
$op = UM()->options()->get( 'display_name' );
2016-01-09 17:22:42 -08:00
$name = '';
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($op == 'default') {
$name = um_profile( 'display_name' );
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($op == 'nickname') {
$name = um_profile( 'nickname' );
2014-12-15 22:38:07 +02:00
}
2017-10-03 16:29:04 +03:00
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 );
}
2017-10-03 16:29:04 +03:00
if (!$name) {
$name = um_user( 'user_login' );
}
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
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 );
}
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($op == 'first_name') {
if (um_user( 'first_name' )) {
$name = um_user( 'first_name' );
} else {
$name = um_profile( $data );
}
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if ($op == 'username') {
$name = um_user( 'user_login' );
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
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 );
}
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
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 );
}
2014-12-15 22:38:07 +02:00
}
2015-12-24 18:11:07 +02:00
2016-04-09 14:50:04 +08:00
2017-12-11 09:53:38 +02:00
if ($op == 'field' && UM()->options()->get( 'display_name_field' ) != '') {
$fields = array_filter( preg_split( '/[,\s]+/', UM()->options()->get( 'display_name_field' ) ) );
$name = '';
2016-04-09 14:50:04 +08:00
2017-10-03 16:29:04 +03:00
foreach ($fields as $field) {
if (um_profile( $field )) {
2016-04-09 14:50:04 +08:00
$name .= um_profile( $field ) . ' ';
2017-10-03 16:29:04 +03:00
} else if (um_user( $field )) {
2016-04-09 14:50:04 +08:00
$name .= um_user( $field ) . ' ';
}
2017-10-03 16:29:04 +03:00
}
2014-12-15 22:38:07 +02:00
}
2016-01-09 17:22:42 -08:00
2017-12-11 09:53:38 +02:00
if ( UM()->options()->get( 'force_display_name_capitlized' ) ) {
2017-10-03 16:29:04 +03:00
$name = implode( '-', array_map( 'ucfirst', explode( '-', $name ) ) );
}
2016-04-09 14:50:04 +08:00
2017-10-03 16:29:04 +03:00
return apply_filters( 'um_user_display_name_filter', $name, um_user( 'ID' ), ( $attrs == 'html' ) ? 1 : 0 );
2016-01-09 17:22:42 -08:00
break;
2016-01-09 17:22:42 -08:00
case 'role_select':
case 'role_radio':
return UM()->roles()->get_role_name( um_user( 'role' ) );
break;
2016-01-09 17:22:42 -08:00
case 'submitted':
2017-10-03 16:29:04 +03:00
$array = um_profile( $data );
if (empty( $array )) return '';
$array = unserialize( $array );
2017-10-03 16:29:04 +03:00
return $array;
break;
2014-12-15 22:38:07 +02:00
case 'password_reset_link':
return UM()->password()->reset_url();
break;
2016-01-09 17:22:42 -08:00
case 'account_activation_link':
return UM()->permalinks()->activate_url();
break;
2014-12-15 22:38:07 +02:00
case 'profile_photo':
2016-02-25 19:11:00 -08:00
$has_profile_photo = false;
2016-05-27 20:12:21 +08:00
$photo_type = 'um-avatar-default';
2017-10-03 16:29:04 +03:00
$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 {
2017-10-03 16:29:04 +03:00
$avatar_uri = um_get_default_avatar_uri();
}
2015-05-05 18:06:39 +03:00
2017-10-03 16:29:04 +03:00
$avatar_uri = apply_filters( 'um_user_avatar_url_filter', $avatar_uri, um_user( 'ID' ) );
2016-02-25 19:11:00 -08:00
2017-10-03 16:29:04 +03:00
if (!$avatar_uri)
return '';
2017-12-11 09:53:38 +02:00
if ( UM()->options()->get( 'use_gravatars' ) && !um_user( 'synced_profile_photo' ) && !$has_profile_photo) {
2017-10-03 16:29:04 +03:00
$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 );
2017-12-11 09:53:38 +02:00
$gravatar_type = UM()->options()->get( 'use_um_gravatar_default_builtin_image' );
$photo_type = 'um-avatar-gravatar';
2017-12-11 09:53:38 +02:00
if ( $gravatar_type == 'default' ) {
if ( UM()->options()->get( 'use_um_gravatar_default_image' ) ) {
2017-10-03 16:29:04 +03:00
$avatar_uri = add_query_arg( 'd', um_get_default_avatar_uri(), $avatar_uri );
2016-03-07 19:12:07 +08:00
}
2017-10-03 16:29:04 +03:00
} else {
$avatar_uri = add_query_arg( 'd', $gravatar_type, $avatar_uri );
}
2015-05-05 18:06:39 +03:00
}
2016-01-09 17:22:42 -08:00
$default_avatar = um_get_default_avatar_uri();
return '<img onerror="this.src=\''.esc_attr($default_avatar).'\';" src="' . $avatar_uri . '" class="func-um_user gravatar avatar avatar-' . $attrs . ' um-avatar ' . $photo_type . '" width="' . $attrs . '" height="' . $attrs . '" alt="' . $image_alt . '" />';
2016-01-09 17:22:42 -08:00
break;
2014-12-15 22:38:07 +02:00
case 'cover_photo':
2017-10-03 16:29:04 +03:00
$is_default = false;
2017-10-03 16:29:04 +03:00
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;
}
2017-10-03 16:29:04 +03:00
$cover_uri = apply_filters( 'um_user_cover_photo_uri__filter', $cover_uri, $is_default, $attrs );
if ($cover_uri)
return '<img src="' . $cover_uri . '" alt="" />';
2016-01-09 17:22:42 -08:00
2017-10-03 16:29:04 +03:00
if (!$cover_uri)
return '';
2016-01-09 17:22:42 -08:00
break;
2016-09-07 17:10:06 +08:00
}
2015-02-08 01:49:10 +02:00
}
2016-01-09 17:22:42 -08:00
/**
* Get server protocol
2017-10-03 16:29:04 +03:00
*
* @return string
*/
function um_get_domain_protocol() {
2017-10-03 16:29:04 +03:00
if (is_ssl()) {
$protocol = 'https://';
} else {
2017-10-03 16:29:04 +03:00
$protocol = 'http://';
}
2016-02-25 19:11:00 -08:00
return $protocol;
}
2016-01-18 20:04:18 +08:00
2016-04-08 10:27:24 +08:00
/**
* Set SSL to media URI
2017-10-03 16:29:04 +03:00
*
2016-04-08 10:27:52 +08:00
* @param string $url
2017-10-03 16:29:04 +03:00
*
2016-04-08 10:27:52 +08:00
* @return string
2016-04-08 10:27:24 +08:00
*/
2017-10-03 16:29:04 +03:00
function um_secure_media_uri( $url ) {
if (is_ssl()) {
$url = str_replace( 'http:', 'https:', $url );
2016-04-08 10:27:24 +08:00
}
return $url;
}
/**
* Check if meta_value exists
2017-10-03 16:29:04 +03:00
*
* UNUSED
*
* @param string $key
2017-10-03 16:29:04 +03:00
* @param mixed $value
*
* @return integer
*/
2017-10-03 16:29:04 +03:00
function um_is_meta_value_exists( $key, $value, $return_user_id = false ) {
global $wpdb;
2016-04-23 21:35:39 +08:00
2017-10-03 16:29:04 +03:00
if (isset( UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] )) {
return UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key];
2016-04-23 21:35:39 +08:00
}
2017-10-03 16:29:04 +03:00
if (!$return_user_id) {
2016-04-01 23:14:34 +08:00
$count = $wpdb->get_var( $wpdb->prepare(
2017-10-03 16:29:04 +03:00
"SELECT COUNT(*) as count FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ",
$key,
$value
2016-04-01 23:14:34 +08:00
) );
2017-10-03 16:29:04 +03:00
UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] = $count;
2016-04-23 21:35:39 +08:00
2016-04-01 23:14:34 +08:00
return $count;
}
2017-10-03 16:29:04 +03:00
$user_id = $wpdb->get_var( $wpdb->prepare(
2017-10-03 16:29:04 +03:00
"SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s ",
$key,
$value
) );
2016-04-01 23:14:34 +08:00
2017-10-03 16:29:04 +03:00
UM()->profile()->arr_user_slugs['is_' . $return_user_id][$key] = $user_id;
return $user_id;
}
/**
* Force strings to UTF-8 encoded
2017-10-03 16:29:04 +03:00
*
* @param mixed $value
2017-10-03 16:29:04 +03:00
*
* @return mixed
*/
2017-10-03 16:29:04 +03:00
function um_force_utf8_string( $value ) {
2017-10-03 16:29:04 +03:00
if (is_array( $value )) {
$arr_value = array();
foreach ($value as $key => $value) {
$utf8_decoded_value = utf8_decode( $value );
2017-10-03 16:29:04 +03:00
if (mb_check_encoding( $utf8_decoded_value, 'UTF-8' )) {
array_push( $arr_value, $utf8_decoded_value );
} else {
array_push( $arr_value, $value );
}
}
2017-10-03 16:29:04 +03:00
return $arr_value;
2017-10-03 16:29:04 +03:00
} else {
2017-10-03 16:29:04 +03:00
$utf8_decoded_value = utf8_decode( $value );
2017-10-03 16:29:04 +03:00
if (mb_check_encoding( $utf8_decoded_value, 'UTF-8' )) {
return $utf8_decoded_value;
}
}
return $value;
}
2016-03-17 15:09:49 -07:00
/**
* Filters the search query.
*
* @param string $search
2017-10-03 16:29:04 +03:00
*
2016-03-17 15:09:49 -07:00
* @return string
*/
2017-10-03 16:29:04 +03:00
function um_filter_search( $search ) {
2016-03-17 15:09:49 -07:00
$search = trim( strip_tags( $search ) );
2017-10-03 16:29:04 +03:00
$search = preg_replace( '/[^a-z \.\@\_\-]+/i', '', $search );
2016-03-17 15:09:49 -07:00
return $search;
}
/**
* Returns the user search query
2017-10-03 16:29:04 +03:00
*
2016-03-17 15:09:49 -07:00
* @return string
*/
function um_get_search_query() {
2017-10-03 16:29:04 +03:00
$query = UM()->permalinks()->get_query_array();
2016-03-17 15:09:49 -07:00
$search = isset( $query['search'] ) ? $query['search'] : '';
return um_filter_search( $search );
2016-03-17 15:09:49 -07:00
}
/**
* Returns the ultimate member search form
2017-10-03 16:29:04 +03:00
*
2016-03-17 15:09:49 -07:00
* @return string
*/
function um_get_search_form() {
2017-10-03 16:29:04 +03:00
2016-03-17 15:09:49 -07:00
return do_shortcode( '[ultimatemember_searchform]' );
}
/**
* Display the search form.
*
* @return string
*/
function um_search_form() {
2017-10-03 16:29:04 +03:00
2016-03-17 15:09:49 -07:00
echo um_get_search_form();
}
2016-03-20 20:02:06 +08:00
/**
* Get localization
2017-10-03 16:29:04 +03:00
*
2016-03-20 20:02:06 +08:00
* @return string
*/
2017-10-03 16:29:04 +03:00
function um_get_locale() {
2016-03-20 20:02:06 +08:00
$lang_code = get_locale();
2017-10-03 16:29:04 +03:00
if (strpos( $lang_code, 'en_' ) > -1 || empty( $lang_code ) || $lang_code == 0) {
2016-03-20 20:02:06 +08:00
return 'en';
}
2017-10-03 16:29:04 +03:00
2016-03-20 20:02:06 +08:00
return $lang_code;
}
/**
* Get current page type
2017-10-03 16:29:04 +03:00
*
* @return string
*/
function um_get_current_page_type() {
2017-10-03 16:29:04 +03:00
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;
2016-06-29 16:43:03 +08:00
}
/**
* Check if running local
2017-10-03 16:29:04 +03:00
*
2016-06-29 16:43:03 +08:00
* @return boolean
*/
function um_core_is_local() {
2017-10-03 16:29:04 +03:00
if ($_SERVER['HTTP_HOST'] == 'localhost'
|| substr( $_SERVER['HTTP_HOST'], 0, 3 ) == '10.'
|| substr( $_SERVER['HTTP_HOST'], 0, 7 ) == '192.168'
) return true;
return false;
2016-08-04 18:36:26 +08:00
}
/**
* 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;
2017-10-03 16:29:04 +03:00
if (defined( 'WPE_APIKEY' )) {
2016-08-04 18:36:26 +08:00
$host = 'WP Engine';
2017-10-03 16:29:04 +03:00
} else if (defined( 'PAGELYBIN' )) {
2016-08-04 18:36:26 +08:00
$host = 'Pagely';
2017-10-03 16:29:04 +03:00
} else if (DB_HOST == 'localhost:/tmp/mysql5.sock') {
2016-08-04 18:36:26 +08:00
$host = 'ICDSoft';
2017-10-03 16:29:04 +03:00
} else if (DB_HOST == 'mysqlv5') {
2016-08-04 18:36:26 +08:00
$host = 'NetworkSolutions';
2017-10-03 16:29:04 +03:00
} else if (strpos( DB_HOST, 'ipagemysql.com' ) !== false) {
2016-08-04 18:36:26 +08:00
$host = 'iPage';
2017-10-03 16:29:04 +03:00
} else if (strpos( DB_HOST, 'ipowermysql.com' ) !== false) {
2016-08-04 18:36:26 +08:00
$host = 'IPower';
2017-10-03 16:29:04 +03:00
} else if (strpos( DB_HOST, '.gridserver.com' ) !== false) {
2016-08-04 18:36:26 +08:00
$host = 'MediaTemple Grid';
2017-10-03 16:29:04 +03:00
} else if (strpos( DB_HOST, '.pair.com' ) !== false) {
2016-08-04 18:36:26 +08:00
$host = 'pair Networks';
2017-10-03 16:29:04 +03:00
} else if (strpos( DB_HOST, '.stabletransit.com' ) !== false) {
2016-08-04 18:36:26 +08:00
$host = 'Rackspace Cloud';
2017-10-03 16:29:04 +03:00
} else if (strpos( DB_HOST, '.sysfix.eu' ) !== false) {
2016-08-04 18:36:26 +08:00
$host = 'SysFix.eu Power Hosting';
2017-10-03 16:29:04 +03:00
} else if (strpos( $_SERVER['SERVER_NAME'], 'Flywheel' ) !== false) {
2016-08-04 18:36:26 +08:00
$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 unknown $v
2017-10-03 16:29:04 +03:00
*
2016-08-04 18:36:26 +08:00
* @return int|string
*/
function um_let_to_num( $v ) {
2017-10-03 16:29:04 +03:00
$l = substr( $v, -1 );
2016-08-04 18:36:26 +08:00
$ret = substr( $v, 0, -1 );
2017-10-03 16:29:04 +03:00
switch (strtoupper( $l )) {
2016-08-04 18:36:26 +08:00
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;
}