2017-07-26 14:57:52 +03:00
< ? php
namespace um\core ;
2023-06-09 12:28:15 +03:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
2017-07-26 14:57:52 +03:00
2018-03-26 01:27:46 +03:00
if ( ! class_exists ( 'um\core\Permalinks' ) ) {
2017-11-14 17:50:37 +02:00
2018-03-16 14:20:37 +02:00
/**
* Class Permalinks
* @package um\core
*/
class Permalinks {
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
/**
* @var
*/
var $core ;
/**
* @var
*/
var $current_url ;
/**
* Permalinks constructor.
*/
2023-06-09 12:28:15 +03:00
public function __construct () {
2018-03-16 14:20:37 +02:00
add_action ( 'init' , array ( & $this , 'set_current_url' ), 0 );
add_action ( 'init' , array ( & $this , 'check_for_querystrings' ), 1 );
add_action ( 'init' , array ( & $this , 'activate_account_via_email_link' ), 1 );
}
/**
* Set current URL variable
*/
2023-06-09 12:28:15 +03:00
public function set_current_url () {
2018-03-16 14:20:37 +02:00
$this -> current_url = $this -> get_current_url ();
}
/**
* Get query as array
*
* @return array
*/
2023-06-09 12:28:15 +03:00
public function get_query_array () {
2018-03-16 14:20:37 +02:00
$parts = parse_url ( $this -> get_current_url () );
if ( isset ( $parts [ 'query' ] ) ) {
parse_str ( $parts [ 'query' ], $query );
return $query ;
}
return array ();
}
/**
* Get current URL anywhere
*
* @param bool $no_query_params
*
2023-06-09 12:28:15 +03:00
* @return string
2018-03-16 14:20:37 +02:00
*/
2023-06-09 12:28:15 +03:00
public function get_current_url ( $no_query_params = false ) {
2018-05-31 17:21:45 +03:00
//use WP native function for fill $_SERVER variables by correct values
wp_fix_server_vars ();
2019-04-14 21:26:04 +03:00
//check if WP-CLI there isn't set HTTP_HOST, use localhost instead
if ( defined ( 'WP_CLI' ) && WP_CLI ) {
$host = isset ( $_SERVER [ 'HTTP_HOST' ] ) ? $_SERVER [ 'HTTP_HOST' ] : 'localhost' ;
2023-06-09 12:28:15 +03:00
} else {
if ( isset ( $_SERVER [ 'HTTP_HOST' ] ) ) {
$host = $_SERVER [ 'HTTP_HOST' ];
} else {
$host = 'localhost' ;
}
}
2019-12-22 22:28:41 +08:00
2019-04-10 18:19:56 +03:00
$page_url = ( is_ssl () ? 'https://' : 'http://' ) . $host . $_SERVER [ 'REQUEST_URI' ];
2018-03-16 14:20:37 +02:00
2023-06-09 12:28:15 +03:00
if ( false !== $no_query_params ) {
2018-03-16 14:20:37 +02:00
$page_url = strtok ( $page_url , '?' );
}
/**
2023-06-09 12:28:15 +03:00
* Filters current page URL.
2018-03-16 14:20:37 +02:00
*
2023-06-09 12:28:15 +03:00
* @param {string} $page_url Current page URL.
* @param {bool} $no_query_params Ignore $_GET attributes in URL. false !== ignore.
*
* @return {string} Current page URL.
*
* @since 1.3.x
* @hook um_get_current_page_url
*
* @example <caption>Add your custom $_GET attribute to all links.</caption>
* function my_um_get_current_page_url( $page_url, $no_query_params ) {
* $page_url = add_query_arg( '{attr_value}', '{attr_key}', $page_url ); // replace to your custom value and key.
2018-03-16 14:20:37 +02:00
* return $page_url;
* }
2023-06-09 12:28:15 +03:00
* add_filter( 'um_get_current_page_url', 'my_um_get_current_page_url', 10, 2 );
2018-03-16 14:20:37 +02:00
*/
2023-06-09 12:28:15 +03:00
return apply_filters ( 'um_get_current_page_url' , $page_url , $no_query_params );
2018-03-16 14:20:37 +02:00
}
/**
* Activates an account via email
*/
2021-06-29 02:51:54 +03:00
public function activate_account_via_email_link () {
if ( isset ( $_REQUEST [ 'act' ] ) && 'activate_via_email' === sanitize_key ( $_REQUEST [ 'act' ] ) && isset ( $_REQUEST [ 'hash' ] ) && is_string ( $_REQUEST [ 'hash' ] ) && strlen ( $_REQUEST [ 'hash' ] ) == 40 &&
2023-06-09 12:28:15 +03:00
isset ( $_REQUEST [ 'user_id' ] ) && is_numeric ( $_REQUEST [ 'user_id' ] ) ) { // valid token
2018-03-16 14:20:37 +02:00
$user_id = absint ( $_REQUEST [ 'user_id' ] );
delete_option ( " um_cache_userdata_ { $user_id } " );
2020-09-01 23:34:17 +03:00
$account_secret_hash = get_user_meta ( $user_id , 'account_secret_hash' , true );
2021-06-29 02:51:54 +03:00
if ( empty ( $account_secret_hash ) || strtolower ( sanitize_text_field ( $_REQUEST [ 'hash' ] ) ) !== strtolower ( $account_secret_hash ) ) {
2020-03-02 16:47:56 +02:00
wp_die ( __ ( 'This activation link is expired or have already been used.' , 'ultimate-member' ) );
}
2018-03-16 14:20:37 +02:00
2021-04-09 15:25:01 +03:00
$account_secret_hash_expiry = get_user_meta ( $user_id , 'account_secret_hash_expiry' , true );
if ( ! empty ( $account_secret_hash_expiry ) && time () > $account_secret_hash_expiry ) {
wp_die ( __ ( 'This activation link is expired.' , 'ultimate-member' ) );
}
2020-09-01 23:34:17 +03:00
um_fetch_user ( $user_id );
2018-03-16 14:20:37 +02:00
UM () -> user () -> approve ();
2020-09-01 23:34:17 +03:00
um_reset_user ();
$user_role = UM () -> roles () -> get_priority_user_role ( $user_id );
$user_role_data = UM () -> roles () -> role_data ( $user_role );
2018-03-16 14:20:37 +02:00
// log in automatically
2020-09-01 23:34:17 +03:00
$login = ! empty ( $user_role_data [ 'login_email_activate' ] ); // Role setting "Login user after validating the activation link?"
2018-03-16 14:20:37 +02:00
if ( ! is_user_logged_in () && $login ) {
2020-03-02 16:47:56 +02:00
$user = get_userdata ( $user_id );
2018-03-16 14:20:37 +02:00
// update wp user
wp_set_current_user ( $user_id , $user -> user_login );
wp_set_auth_cookie ( $user_id );
ob_start ();
do_action ( 'wp_login' , $user -> user_login , $user );
ob_end_clean ();
}
/**
* UM hook
*
* @type action
* @title um_after_email_confirmation
* @description Action on user activation
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_email_confirmation', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_after_email_confirmation', 'my_after_email_confirmation', 10, 1 );
* function my_after_email_confirmation( $user_id ) {
* // your code here
* }
* ?>
*/
do_action ( 'um_after_email_confirmation' , $user_id );
2020-09-01 23:34:17 +03:00
$redirect = empty ( $user_role_data [ 'url_email_activate' ] ) ? um_get_core_page ( 'login' , 'account_active' ) : trim ( $user_role_data [ 'url_email_activate' ] ); // Role setting "URL redirect after e-mail activation"
$redirect = apply_filters ( 'um_after_email_confirmation_redirect' , $redirect , $user_id , $login );
2018-03-16 14:20:37 +02:00
exit ( wp_redirect ( $redirect ) );
}
}
/**
* Makes an activate link for any user
*
2020-09-01 23:34:17 +03:00
* @return bool|string
2018-03-16 14:20:37 +02:00
*/
2023-06-09 12:28:15 +03:00
public function activate_url () {
2020-09-01 23:34:17 +03:00
if ( ! um_user ( 'account_secret_hash' ) ) {
2018-03-16 14:20:37 +02:00
return false ;
}
/**
* UM hook
*
* @type filter
* @title um_activate_url
* @description Change activate user URL
* @input_vars
* [{"var":"$url","type":"string","desc":"Activate URL"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_activate_url', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_activate_url', 'my_activate_url', 10, 1 );
* function my_activate_url( $url ) {
* // your code here
* return $url;
* }
* ?>
*/
$url = apply_filters ( 'um_activate_url' , home_url () );
$url = add_query_arg ( 'act' , 'activate_via_email' , $url );
2020-09-01 23:34:17 +03:00
$url = add_query_arg ( 'hash' , um_user ( 'account_secret_hash' ), $url );
$url = add_query_arg ( 'user_id' , um_user ( 'ID' ), $url );
2018-03-16 14:20:37 +02:00
return $url ;
}
/**
* Checks for UM query strings
*/
2023-06-09 12:28:15 +03:00
public function check_for_querystrings () {
2018-03-16 14:20:37 +02:00
if ( isset ( $_REQUEST [ 'message' ] ) ) {
UM () -> shortcodes () -> message_mode = true ;
}
}
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
/**
* Add a query param to url
*
* @param $key
* @param $value
*
* @return string
*/
2023-06-09 12:28:15 +03:00
public function add_query ( $key , $value ) {
2021-05-07 13:42:20 +03:00
$this -> current_url = add_query_arg ( $key , $value , $this -> get_current_url () );
2018-03-16 14:20:37 +02:00
return $this -> current_url ;
}
/**
* Remove a query param from url
*
* @param $key
* @param $value
*
* @return string
*/
2023-06-09 12:28:15 +03:00
public function remove_query ( $key , $value ) {
2018-03-16 14:20:37 +02:00
$this -> current_url = remove_query_arg ( $key , $this -> current_url );
return $this -> current_url ;
}
2017-07-26 14:57:52 +03:00
2018-02-18 13:27:46 +02:00
/**
* @param $slug
*
* @return int|null|string
*/
2023-06-09 12:28:15 +03:00
public function slug_exists_user_id ( $slug ) {
2018-02-18 13:27:46 +02:00
global $wpdb ;
$permalink_base = UM () -> options () -> get ( 'permalink_base' );
$user_id = $wpdb -> get_var (
2023-06-06 01:15:10 +03:00
" SELECT user_id
FROM { $wpdb -> usermeta }
WHERE meta_key = 'um_user_profile_url_slug_ { $permalink_base } ' AND
2018-02-18 13:27:46 +02:00
meta_value = ' { $slug } '
ORDER BY umeta_id ASC
LIMIT 1 "
);
if ( ! empty ( $user_id ) ) {
return $user_id ;
}
2018-02-12 13:31:54 +02:00
2018-02-18 13:27:46 +02:00
return false ;
}
2018-02-12 13:31:54 +02:00
2018-03-16 14:20:37 +02:00
/**
* Get Profile Permalink
*
* @param string $slug
2023-06-09 12:28:15 +03:00
*
* @return string
2018-03-16 14:20:37 +02:00
*/
2023-06-09 12:28:15 +03:00
public function profile_permalink ( $slug ) {
/**
* Filters user profile URL externally with own logic.
*
* @since 2.6.3
* @hook um_external_profile_url
*
* @param {bool|string} $profile_url Profile URL.
* @param {string} $slug User profile slug.
*
* @return {string} Profile URL.
*
* @example <caption>Change profile URL to your custom link and ignore native profile permalink handlers.</caption>
* function my_um_external_profile_url( $profile_url, $slug ) {
* $profile_url = '{some your custom URL}'; // replace to your custom link.
* return $profile_url;
* }
* add_filter( 'um_external_profile_url', 'my_um_external_profile_url', 10, 2 );
*/
$external_profile_url = apply_filters ( 'um_external_profile_url' , false , $slug );
if ( false !== $external_profile_url ) {
return ! empty ( $external_profile_url ) ? $external_profile_url : '' ;
}
2018-03-16 14:20:37 +02:00
2023-06-09 12:28:15 +03:00
$page_id = UM () -> config () -> permalinks [ 'user' ];
2018-03-16 14:20:37 +02:00
$profile_url = get_permalink ( $page_id );
/**
2023-06-09 12:28:15 +03:00
* Filters the base URL of the UM profile page.
2018-03-16 14:20:37 +02:00
*
2023-06-09 12:28:15 +03:00
* @since 1.3.x
* @deprecated 2.6.3 Use <a href="https://developer.wordpress.org/reference/hooks/post_link/" target="_blank" title="'post_link' hook article on developer.wordpress.org">'post_link'</a> instead.
* @hook um_localize_permalink_filter
* @todo fully remove since 2.7.0
*
* @param {string} $profile_url Profile URL.
* @param {int} $page_id Profile Page ID.
*
* @return {string} Profile URL.
*
* @example <caption>Change profile base URL to your custom link.</caption>
2018-03-16 14:20:37 +02:00
* function my_localize_permalink( $profile_url, $page_id ) {
2023-06-09 12:28:15 +03:00
* $profile_url = '{some your custom URL}'; // replace to your custom link.
2018-03-16 14:20:37 +02:00
* return $profile_url;
* }
2023-06-09 12:28:15 +03:00
* add_filter( 'um_localize_permalink_filter', 'my_localize_permalink', 10, 2 );
2018-03-16 14:20:37 +02:00
*/
$profile_url = apply_filters ( 'um_localize_permalink_filter' , $profile_url , $page_id );
2023-06-09 12:28:15 +03:00
if ( UM () -> is_permalinks ) {
$profile_url = trailingslashit ( untrailingslashit ( $profile_url ) );
$profile_url .= trailingslashit ( strtolower ( $slug ) );
2018-03-16 14:20:37 +02:00
} else {
2023-06-09 12:28:15 +03:00
$profile_url = add_query_arg ( 'um_user' , strtolower ( $slug ), $profile_url );
2018-03-16 14:20:37 +02:00
}
2023-06-06 01:15:10 +03:00
/**
2023-06-09 12:28:15 +03:00
* Filters user profile URL.
2023-06-06 01:15:10 +03:00
*
2023-06-09 12:28:15 +03:00
* @since 2.6.3
* @hook um_profile_permalink
*
* @param {string} $profile_url Profile URL.
* @param {int} $page_id Profile Page ID.
* @param {string} $slug User profile slug.
*
* @return {string} Profile URL.
*
* @example <caption>Change profile URL to your custom link.</caption>
* function my_um_profile_permalink( $profile_url, $page_id, $slug ) {
* $profile_url = '{some your custom URL}'; // replace to your custom link.
2023-06-06 01:15:10 +03:00
* return $profile_url;
* }
2023-06-09 12:28:15 +03:00
* add_filter( 'um_profile_permalink', 'my_um_profile_permalink', 10, 3 );
2023-06-06 01:15:10 +03:00
*/
2023-06-09 12:28:15 +03:00
$profile_url = apply_filters ( 'um_profile_permalink' , $profile_url , $page_id , $slug );
2023-06-06 01:15:10 +03:00
2018-10-15 21:36:34 +03:00
return ! empty ( $profile_url ) ? $profile_url : '' ;
2018-03-16 14:20:37 +02:00
}
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
/**
* Generate profile slug
*
* @param string $full_name
* @param string $first_name
* @param string $last_name
* @return string
*/
2023-06-09 12:28:15 +03:00
public function profile_slug ( $full_name , $first_name , $last_name ) {
2018-03-16 14:20:37 +02:00
$permalink_base = UM () -> options () -> get ( 'permalink_base' );
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
$user_in_url = '' ;
2017-07-26 14:57:52 +03:00
2023-06-09 12:28:15 +03:00
$full_name = str_replace ( " ' " , " " , $full_name );
$full_name = str_replace ( " & " , " " , $full_name );
$full_name = str_replace ( " / " , " " , $full_name );
2017-07-26 14:57:52 +03:00
2023-06-09 12:28:15 +03:00
switch ( $permalink_base ) {
2018-03-16 14:20:37 +02:00
case 'name' : // dotted
$full_name_slug = $full_name ;
$difficulties = 0 ;
2017-07-26 14:57:52 +03:00
2023-06-09 12:28:15 +03:00
if ( strpos ( $full_name , '.' ) > - 1 ) {
$full_name = str_replace ( " . " , " _ " , $full_name );
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
}
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
$full_name = strtolower ( str_replace ( " " , " . " , $full_name ) );
2017-07-26 14:57:52 +03:00
2023-06-09 12:28:15 +03:00
if ( strpos ( $full_name , '_.' ) > - 1 ) {
$full_name = str_replace ( '_.' , '_' , $full_name );
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
}
2017-07-26 14:57:52 +03:00
2023-06-09 12:28:15 +03:00
$full_name_slug = str_replace ( '-' , '.' , $full_name_slug );
$full_name_slug = str_replace ( ' ' , '.' , $full_name_slug );
$full_name_slug = str_replace ( '..' , '.' , $full_name_slug );
2017-07-26 14:57:52 +03:00
2023-06-09 12:28:15 +03:00
if ( strpos ( $full_name , '.' ) > - 1 ) {
$full_name = str_replace ( '.' , ' ' , $full_name );
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
}
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
$user_in_url = rawurlencode ( $full_name_slug );
2017-07-26 14:57:52 +03:00
2018-03-16 14:20:37 +02:00
break ;
case 'name_dash' : // dashed
$difficulties = 0 ;
$full_name_slug = strtolower ( $full_name );
// if last name has dashed replace with underscore
2023-06-09 12:28:15 +03:00
if ( strpos ( $last_name , '-' ) > - 1 && strpos ( $full_name , '-' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
2023-06-09 12:28:15 +03:00
$full_name = str_replace ( '-' , '_' , $full_name );
2018-03-16 14:20:37 +02:00
}
// if first name has dashed replace with underscore
2023-06-09 12:28:15 +03:00
if ( strpos ( $first_name , '-' ) > - 1 && strpos ( $full_name , '-' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
2023-06-09 12:28:15 +03:00
$full_name = str_replace ( '-' , '_' , $full_name );
2018-03-16 14:20:37 +02:00
}
// if name has space, replace with dash
2023-06-09 12:28:15 +03:00
$full_name_slug = str_replace ( ' ' , '-' , $full_name_slug );
2018-03-16 14:20:37 +02:00
// if name has period
2023-06-09 12:28:15 +03:00
if ( strpos ( $last_name , '.' ) > - 1 && strpos ( $full_name , '.' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
}
2023-06-09 12:28:15 +03:00
$full_name_slug = str_replace ( '.' , '-' , $full_name_slug );
$full_name_slug = str_replace ( '--' , '-' , $full_name_slug );
2018-03-16 14:20:37 +02:00
2023-06-09 12:28:15 +03:00
$user_in_url = rawurlencode ( $full_name_slug );
2018-03-16 14:20:37 +02:00
break ;
case 'name_plus' : // plus
$difficulties = 0 ;
$full_name_slug = strtolower ( $full_name );
// if last name has dashed replace with underscore
2023-06-09 12:28:15 +03:00
if ( strpos ( $last_name , '+' ) > - 1 && strpos ( $full_name , '+' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
2023-06-09 12:28:15 +03:00
$full_name = str_replace ( '-' , '_' , $full_name );
2018-03-16 14:20:37 +02:00
}
// if first name has dashed replace with underscore
2023-06-09 12:28:15 +03:00
if ( strpos ( $first_name , '+' ) > - 1 && strpos ( $full_name , '+' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
2023-06-09 12:28:15 +03:00
$full_name = str_replace ( '-' , '_' , $full_name );
2018-03-16 14:20:37 +02:00
}
2023-06-09 12:28:15 +03:00
if ( strpos ( $last_name , '-' ) > - 1 || strpos ( $first_name , '-' ) > - 1 || strpos ( $full_name , '-' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
}
// if name has space, replace with dash
2023-06-09 12:28:15 +03:00
$full_name_slug = str_replace ( ' ' , '+' , $full_name_slug );
$full_name_slug = str_replace ( '-' , '+' , $full_name_slug );
2018-03-16 14:20:37 +02:00
// if name has period
2023-06-09 12:28:15 +03:00
if ( strpos ( $last_name , '.' ) > - 1 && strpos ( $full_name , '.' ) > - 1 ) {
2018-03-16 14:20:37 +02:00
$difficulties ++ ;
}
2023-06-09 12:28:15 +03:00
$full_name_slug = str_replace ( '.' , '+' , $full_name_slug );
$full_name_slug = str_replace ( '++' , '+' , $full_name_slug );
2018-03-16 14:20:37 +02:00
$user_in_url = $full_name_slug ;
break ;
}
2023-06-09 12:28:15 +03:00
return $user_in_url ;
2018-03-16 14:20:37 +02:00
}
/**
* Get action url for admin use
*
* @param $action
* @param $subaction
*
2023-07-24 12:57:18 +03:00
* @deprecated 2.6.9
*
2018-03-16 14:20:37 +02:00
* @return mixed|string|void
*/
2023-06-09 12:28:15 +03:00
public function admin_act_url ( $action , $subaction ) {
2023-07-24 12:57:18 +03:00
_deprecated_function ( __METHOD__ , '2.6.9' );
$url = add_query_arg (
array (
'um_adm_action' => $action ,
'sub' => $subaction ,
'user_id' => um_user ( 'ID' ),
'_wpnonce' => wp_create_nonce ( $action ),
)
);
2018-03-16 14:20:37 +02:00
return $url ;
}
2023-06-09 12:28:15 +03:00
/**
* SEO canonical href bugfix
*
* @deprecated since version 2.1.7
*
* @todo remove since 2.7.0
* @see function um_profile_remove_wpseo()
*/
public function um_rel_canonical_ () {
_deprecated_function ( __METHOD__ , '2.1.7' , 'um_profile_remove_wpseo()' );
global $wp_the_query ;
if ( ! is_singular () )
return ;
/**
* UM hook
*
* @type filter
* @title um_allow_canonical__filter
* @description Allow canonical
* @input_vars
* [{"var":"$allow_canonical","type":"bool","desc":"Allow?"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_allow_canonical__filter', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_allow_canonical__filter', 'my_allow_canonical', 10, 1 );
* function my_allow_canonical( $allow_canonical ) {
* // your code here
* return $allow_canonical;
* }
* ?>
*/
$enable_canonical = apply_filters ( " um_allow_canonical__filter " , true );
if ( ! $enable_canonical )
return ;
if ( ! $id = $wp_the_query -> get_queried_object_id () )
return ;
if ( UM () -> config () -> permalinks [ 'user' ] == $id ) {
$link = esc_url ( $this -> get_current_url () );
echo " <link rel='canonical' href=' $link ' /> \n " ;
return ;
}
$link = get_permalink ( $id );
if ( $page = get_query_var ( 'cpage' ) ) {
$link = get_comments_pagenum_link ( $page );
echo " <link rel='canonical' href=' $link ' /> \n " ;
}
}
2018-03-16 14:20:37 +02:00
}
2021-06-29 02:51:54 +03:00
}