Files
ultimatemember/core/um-rewrite.php
T

184 lines
4.4 KiB
PHP
Raw Normal View History

2014-12-15 22:38:07 +02:00
<?php
class UM_Rewrite {
function __construct() {
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
add_filter('query_vars', array(&$this, 'query_vars'), 10, 1 );
2016-02-26 18:14:07 -08:00
2015-03-02 16:46:00 +02:00
add_action('init', array(&$this, 'rewrite_rules'), 100000000 );
2016-02-26 18:14:07 -08:00
2015-02-02 02:10:06 +02:00
add_action('template_redirect', array(&$this, 'redirect_author_page'), 9999 );
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
add_action('template_redirect', array(&$this, 'locate_user_profile'), 9999 );
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
}
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
/***
*** @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;
}
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
/***
*** @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'] ) ) {
2016-02-26 18:14:07 -08:00
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);
2016-02-26 18:14:07 -08:00
2015-11-05 19:51:31 +08:00
if ( isset( $user->post_name ) ) {
2016-02-26 18:14:07 -08:00
2015-11-05 19:51:31 +08:00
$user_slug = $user->post_name;
$account = get_post($account_page_id);
$account_slug = $account->post_name;
2016-01-10 22:30:57 +08:00
2016-01-12 18:05:24 +08:00
$add_lang_code = '';
2016-02-26 18:14:07 -08:00
2016-01-12 18:05:24 +08:00
if ( function_exists('icl_object_id') || function_exists('icl_get_current_language') ) {
2016-02-26 18:14:07 -08:00
2016-01-10 22:30:57 +08:00
if( function_exists('icl_get_current_language') ){
$language_code = icl_get_current_language();
2016-05-13 10:41:28 +08:00
}else if( function_exists('icl_object_id') && defined('ICL_LANGUAGE_CODE') ){
2016-01-10 22:30:57 +08:00
$language_code = ICL_LANGUAGE_CODE;
}
2016-01-12 19:07:32 +08:00
// User page translated slug
2016-01-12 18:05:24 +08:00
$lang_post_id = icl_object_id( $user->ID, 'post', FALSE, $language_code );
$lang_post_obj = get_post( $lang_post_id );
if( isset( $lang_post_obj->post_name ) ){
$user_slug = $lang_post_obj->post_name;
}
2016-02-26 18:14:07 -08:00
2016-01-12 19:07:32 +08:00
// Account page translated slug
$lang_post_id = icl_object_id( $account->ID, 'post', FALSE, $language_code );
$lang_post_obj = get_post( $lang_post_id );
if( isset( $lang_post_obj->post_name ) ){
$account_slug = $lang_post_obj->post_name;
}
2016-01-12 18:05:24 +08:00
if( $language_code != icl_get_default_language() ){
$add_lang_code = $language_code;
}
2016-02-26 18:14:07 -08:00
2016-01-10 22:30:57 +08:00
}
2016-02-26 18:14:07 -08:00
2016-01-12 19:07:32 +08:00
add_rewrite_rule( $user_slug.'/([^/]+)/?$',
2016-01-12 18:05:24 +08:00
'index.php?page_id='.$user_page_id.'&um_user=$matches[1]&lang='.$add_lang_code,
'top'
);
2016-02-26 18:14:07 -08:00
2016-01-12 19:07:32 +08:00
add_rewrite_rule( $account_slug.'/([^/]+)?$',
2016-01-12 18:05:24 +08:00
'index.php?page_id='.$account_page_id.'&um_tab=$matches[1]&lang='.$add_lang_code,
'top'
);
2016-02-26 18:14:07 -08:00
if( !apply_filters('um_rewrite_flush_rewrite_rules', um_get_option('um_flush_stop') ) )
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
}
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
}
2016-02-26 18:14:07 -08: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() ) );
}
}
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
/***
*** @locate/display a profile
***/
function locate_user_profile() {
global $post, $ultimatemember;
2016-02-26 18:14:07 -08:00
2014-12-22 01:45:24 +02:00
if ( um_queried_user() && um_is_core_page('user') ) {
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
if ( um_get_option('permalink_base') == 'user_login' ) {
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
$user_id = username_exists( um_queried_user() );
2016-02-26 18:14:07 -08:00
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;
}
2016-05-27 14:28:15 +08:00
if( !$user_id ){
$user_id = $ultimatemember->user->user_exists_by_email_as_username( $slug );
}
2014-12-15 22:38:07 +02:00
}
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
}
2016-02-26 18:14:07 -08: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
}
2016-02-26 18:14:07 -08:00
2016-01-13 12:33:21 +08:00
if ( in_array( um_get_option('permalink_base'), array('name','name_dash','name_dot','name_plus') ) ) {
2014-12-15 22:38:07 +02:00
$user_id = $ultimatemember->user->user_exists_by_name( um_queried_user() );
2015-01-25 19:59:18 +02:00
}
2016-02-26 18:14:07 -08:00
2015-01-25 19:59:18 +02:00
/** USER EXISTS SET USER AND CONTINUE **/
2016-02-26 18:14:07 -08:00
2015-01-25 19:59:18 +02:00
if ( $user_id ) {
2016-02-26 18:14:07 -08:00
2015-01-25 19:59:18 +02:00
um_set_requested_user( $user_id );
2016-02-26 18:14:07 -08:00
2016-06-28 16:55:38 +08:00
do_action('um_access_profile', $user_id );
2015-01-25 19:59:18 +02:00
} 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') ) );
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
}
2016-02-26 18:14:07 -08:00
2015-04-25 21:41:47 +02:00
} else if ( um_is_core_page('user') ) {
2016-02-26 18:14:07 -08:00
2015-02-02 02:10:06 +02:00
if ( is_user_logged_in() ) { // just redirect to their profile
2016-02-26 18:14:07 -08:00
2015-02-13 02:05:04 +02:00
$query = $ultimatemember->permalinks->get_query_array();
2016-02-26 18:14:07 -08:00
2015-02-13 02:05:04 +02:00
$url = um_user_profile_url();
2016-02-26 18:14:07 -08:00
2015-02-13 02:05:04 +02:00
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
}
}
2016-02-26 18:14:07 -08:00
2015-02-13 02:05:04 +02:00
exit( wp_redirect( $url ) );
2016-04-02 16:58:20 +08:00
}else{
exit( wp_redirect( home_url() ) );
2015-02-02 02:10:06 +02:00
}
2016-02-26 18:14:07 -08:00
2014-12-15 22:38:07 +02:00
}
}
2016-02-26 18:14:07 -08:00
}