bb_repermalink

The BuddyPress bb repermalink function.

Description

bb_repermalink(); 

Usage

  1. if ( !function_exists( 'bb_repermalink' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-core.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bb_repermalink(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-core.php  
  1. function bb_repermalink() { 
  2. global $page; 
  3. $location = bb_get_location(); 
  4. $uri = $_SERVER['REQUEST_URI']; 
  5. if ( isset($_GET['id']) ) 
  6. $id = $_GET['id']; 
  7. else 
  8. $id = bb_get_path(); 
  9. $_original_id = $id; 
  10.  
  11.  
  12.  
  13. switch ($location) { 
  14. case 'front-page': 
  15. $path = null; 
  16. $querystring = null; 
  17. if ($page > 1) { 
  18. if (bb_get_option( 'mod_rewrite' )) { 
  19. $path = 'page/' . $page; 
  20. } else { 
  21. $querystring = array('page' => $page); 
  22. $permalink = bb_get_uri($path, $querystring, BB_URI_CONTEXT_HEADER); 
  23. $issue_404 = true; 
  24. break; 
  25. case 'forum-page': 
  26. if (empty($id)) { 
  27. $permalink = bb_get_uri(null, null, BB_URI_CONTEXT_HEADER); 
  28. break; 
  29. global $forum_id, $forum; 
  30. $forum = bb_get_forum( $id ); 
  31. $forum_id = $forum->forum_id; 
  32. $permalink = get_forum_link( $forum->forum_id, $page ); 
  33. break; 
  34. case 'topic-edit-page': 
  35. case 'topic-page': 
  36. if (empty($id)) { 
  37. $permalink = bb_get_uri(null, null, BB_URI_CONTEXT_HEADER); 
  38. break; 
  39. global $topic_id, $topic; 
  40. $topic = get_topic( $id ); 
  41. $topic_id = $topic->topic_id; 
  42. $permalink = get_topic_link( $topic->topic_id, $page ); 
  43. break; 
  44. case 'profile-page': // This handles the admin side of the profile as well. 
  45. global $user_id, $user, $profile_hooks, $self; 
  46. if ( isset($_GET['id']) ) 
  47. $id = $_GET['id']; 
  48. elseif ( isset($_GET['username']) ) 
  49. $id = $_GET['username']; 
  50. else 
  51. $id = bb_get_path(); 
  52. $_original_id = $id; 
  53.  
  54. if ( !$id ) { 
  55. $user = bb_get_current_user(); // Attempt to go to the current users profile 
  56. } else { 
  57. if ( bb_get_option( 'mod_rewrite' ) === 'slugs') { 
  58. if ( !$user = bb_get_user_by_nicename( $id ) ) { 
  59. $user = bb_get_user( $id ); 
  60. } else { 
  61. if ( !$user = bb_get_user( $id ) ) { 
  62. $user = bb_get_user_by_nicename( $id ); 
  63.  
  64. if ( !$user || ( 1 == $user->user_status && !bb_current_user_can( 'moderate' ) ) ) 
  65. bb_die(__('User not found.'), '', 404); 
  66.  
  67. $user_id = $user->ID; 
  68. $valid = false; 
  69. if ( $tab = isset($_GET['tab']) ? $_GET['tab'] : bb_get_path(2) ) { 
  70. foreach ( $profile_hooks as $valid_tab => $valid_file ) { 
  71. if ( $tab == $valid_tab ) { 
  72. $valid = true; 
  73. $self = $valid_file; 
  74. if ( $valid ) { 
  75. $permalink = get_profile_tab_link( $user->ID, $tab, $page ); 
  76. } else { 
  77. $permalink = get_user_profile_link( $user->ID, $page ); 
  78. unset($self, $tab); 
  79. break; 
  80. case 'favorites-page': 
  81. $permalink = get_favorites_link(); 
  82. break; 
  83. case 'tag-page': // It's not an integer and tags.php pulls double duty. 
  84. $id = ( isset($_GET['tag']) ) ? $_GET['tag'] : false; 
  85. if ( ! $id || ! bb_get_tag( (string) $id ) ) 
  86. $permalink = bb_get_tag_page_link(); 
  87. else { 
  88. global $tag, $tag_name; 
  89. $tag_name = $id; 
  90. $tag = bb_get_tag( (string) $id ); 
  91. $permalink = bb_get_tag_link( 0, $page ); // 0 => grabs $tag from global. 
  92. break; 
  93. case 'view-page': // Not an integer 
  94. if ( isset($_GET['view']) ) 
  95. $id = $_GET['view']; 
  96. else 
  97. $id = bb_get_path(); 
  98. $_original_id = $id; 
  99. global $view; 
  100. $view = $id; 
  101. $permalink = get_view_link( $view, $page ); 
  102. break; 
  103. default: 
  104. return; 
  105. break; 
  106.  
  107. wp_parse_str($_SERVER['QUERY_STRING'], $args); 
  108. $args = urlencode_deep($args); 
  109. if ( $args ) { 
  110. $permalink = add_query_arg($args, $permalink); 
  111. if ( bb_get_option('mod_rewrite') ) { 
  112. $pretty_args = array('id', 'page', 'tag', 'tab', 'username'); // these are already specified in the path 
  113. if ( $location == 'view-page' ) 
  114. $pretty_args[] = 'view'; 
  115. foreach ( $pretty_args as $pretty_arg ) 
  116. $permalink = remove_query_arg( $pretty_arg, $permalink ); 
  117.  
  118. $permalink = apply_filters( 'bb_repermalink_result', $permalink, $location ); 
  119.  
  120. $domain = bb_get_option('domain'); 
  121. $domain = preg_replace('/^https?/', '', $domain); 
  122. $check = preg_replace( '|^.*' . trim($domain, ' /' ) . '|', '', $permalink, 1 ); 
  123. $uri = rtrim( $uri, " \t\n\r\0\x0B?" ); 
  124. $uri = str_replace( '/index.php', '/', $uri ); 
  125.  
  126. global $bb_log; 
  127. $bb_log->debug($uri, 'bb_repermalink() ' . __('REQUEST_URI')); 
  128. $bb_log->debug($check, 'bb_repermalink() ' . __('should be')); 
  129. $bb_log->debug($permalink, 'bb_repermalink() ' . __('full permalink')); 
  130. $bb_log->debug(isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null, 'bb_repermalink() ' . __('PATH_INFO')); 
  131.  
  132. if ( $check != $uri && $check != str_replace(urlencode($_original_id), $_original_id, $uri) ) { 
  133. if ( $issue_404 && rtrim( $check, " \t\n\r\0\x0B/" ) !== rtrim( $uri, " \t\n\r\0\x0B/" ) ) { 
  134. status_header( 404 ); 
  135. bb_load_template( '404.php' ); 
  136. } else { 
  137. wp_redirect( $permalink ); 
  138. exit; 
  139.  
  140. do_action( 'post_permalink', $permalink );