Files
ultimatemember/core/um-rewrite.php
T

170 lines
3.9 KiB
PHP
Raw Normal View History

2014-12-15 22:38:07 +02:00
<?php
class UM_Rewrite {
function __construct() {
add_filter('query_vars', array(&$this, 'query_vars'), 10, 1 );
2015-03-02 16:46:00 +02:00
add_action('init', array(&$this, 'rewrite_rules'), 100000000 );
2014-12-15 22:38:07 +02:00
2015-02-02 02:10:06 +02:00
add_action('template_redirect', array(&$this, 'redirect_author_page'), 9999 );
2014-12-15 22:38:07 +02:00
add_action('template_redirect', array(&$this, 'locate_user_profile'), 9999 );
}
/***
*** @modify global query vars
***/
function query_vars($public_query_vars) {
$public_query_vars[] = 'um_user';
$public_query_vars[] = 'um_tab';
2015-01-24 23:39:43 +02:00
$public_query_vars[] = 'profiletab';
$public_query_vars[] = 'subnav';
2014-12-15 22:38:07 +02:00
return $public_query_vars;
}
/***
*** @setup rewrite rules
***/
function rewrite_rules(){
2015-03-02 16:46:00 +02:00
2014-12-15 22:38:07 +02:00
global $ultimatemember;
2015-11-05 19:51:31 +08:00
if ( isset( $ultimatemember->permalinks->core['user'] ) && !um_get_option('um_flush_stop') ) {
2014-12-15 22:38:07 +02:00
$user_page_id = $ultimatemember->permalinks->core['user'];
$account_page_id = $ultimatemember->permalinks->core['account'];
2015-02-01 01:30:04 +02:00
$user = get_post($user_page_id);
2014-12-15 22:38:07 +02:00
2015-11-05 19:51:31 +08:00
if ( isset( $user->post_name ) ) {
$user_slug = $user->post_name;
$account = get_post($account_page_id);
$account_slug = $account->post_name;
2016-01-10 22:30:57 +08:00
2015-11-05 19:51:31 +08:00
add_rewrite_rule(
2016-01-10 22:30:57 +08:00
'^'.$user_slug.'/([^/]*)$',
'index.php?page_id='.$user_page_id.'&um_user=$matches[1]',
'top'
2015-11-05 19:51:31 +08:00
);
2016-01-10 22:30:57 +08:00
2015-11-05 19:51:31 +08:00
add_rewrite_rule(
2016-01-10 22:30:57 +08:00
'^'.$account_slug.'/([^/]*)$',
'index.php?page_id='.$account_page_id.'&um_tab=$matches[1]',
'top'
2015-11-05 19:51:31 +08:00
);
2016-01-10 22:30:57 +08:00
if ( function_exists('icl_object_id') || function_exists('icl_get_current_language') && icl_get_current_language() != icl_get_default_language() ) {
if( function_exists('icl_get_current_language') ){
$language_code = icl_get_current_language();
}else if( function_exists('icl_object_id') ){
$language_code = ICL_LANGUAGE_CODE;
}
add_rewrite_rule(
'^'.$language_code.'/'.$user_slug.'/([^/]*)$',
'index.php?page_id='.$user_page_id.'&um_user=$matches[1]',
'bottom'
);
add_rewrite_rule(
'^'.$language_code.'/'.$account_slug.'/([^/]*)$',
'index.php?page_id='.$account_page_id.'&um_tab=$matches[1]',
'bottom'
);
}else{
}
2015-11-05 19:51:31 +08:00
2015-02-04 20:31:39 +02:00
flush_rewrite_rules( true );
2015-11-05 19:51:31 +08:00
2015-02-02 02:10:06 +02:00
}
2015-04-15 16:59:27 +02:00
2014-12-15 22:38:07 +02:00
}
}
2015-02-02 02:10:06 +02:00
/***
*** @author page to user profile redirect
***/
function redirect_author_page() {
if ( um_get_option('author_redirect') && is_author() ) {
$id = get_query_var( 'author' );
um_fetch_user( $id );
exit( wp_redirect( um_user_profile_url() ) );
}
}
2014-12-15 22:38:07 +02:00
/***
*** @locate/display a profile
***/
function locate_user_profile() {
global $post, $ultimatemember;
2014-12-22 01:45:24 +02:00
if ( um_queried_user() && um_is_core_page('user') ) {
2014-12-15 22:38:07 +02:00
if ( um_get_option('permalink_base') == 'user_login' ) {
2015-01-25 19:59:18 +02:00
2014-12-15 22:38:07 +02:00
$user_id = username_exists( um_queried_user() );
2015-01-25 19:59:18 +02:00
// Try nice name
if ( !$user_id ) {
2015-02-08 17:27:04 +02:00
$slug = um_queried_user();
$slug = str_replace('.','-',$slug);
$the_user = get_user_by( 'slug', $slug );
2015-01-25 19:59:18 +02:00
if ( isset( $the_user->ID ) ){
$user_id = $the_user->ID;
}
2014-12-15 22:38:07 +02:00
}
2015-01-25 19:59:18 +02:00
2014-12-15 22:38:07 +02:00
}
if ( um_get_option('permalink_base') == 'user_id' ) {
$user_id = $ultimatemember->user->user_exists_by_id( um_queried_user() );
2015-01-25 19:59:18 +02:00
2014-12-15 22:38:07 +02:00
}
if ( um_get_option('permalink_base') == 'name' ) {
$user_id = $ultimatemember->user->user_exists_by_name( um_queried_user() );
2015-01-25 19:59:18 +02:00
}
/** USER EXISTS SET USER AND CONTINUE **/
if ( $user_id ) {
um_set_requested_user( $user_id );
} else {
2015-02-02 02:10:06 +02:00
2015-01-25 19:59:18 +02:00
exit( wp_redirect( um_get_core_page('user') ) );
2014-12-15 22:38:07 +02:00
}
2015-04-25 21:41:47 +02:00
} else if ( um_is_core_page('user') ) {
2015-02-02 02:10:06 +02:00
if ( is_user_logged_in() ) { // just redirect to their profile
2015-02-13 02:05:04 +02:00
$query = $ultimatemember->permalinks->get_query_array();
$url = um_user_profile_url();
if ( $query ) {
foreach( $query as $key => $val ) {
2015-04-25 21:41:47 +02:00
$url = add_query_arg($key, $val, $url);
2015-02-13 02:05:04 +02:00
}
}
exit( wp_redirect( $url ) );
2015-02-02 02:10:06 +02:00
}
2014-12-15 22:38:07 +02:00
}
}
}