bp_core_set_uri_globals

Analyze the URI and break it down into BuddyPress-usable chunks.

Description

bp_core_set_uri_globals(); 

BuddyPress can use complete custom friendly URIs without the user having to add new rewrite rules. Custom components are able to use their own custom URI structures with very little work.

The URIs are broken down as follows: - http:// example.com / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ... - OUTSIDE ROOT: http:// example.com / sites / buddypress / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...

Example:


Usage

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

Defined (1)

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

/bp-core/bp-core-catchuri.php  
  1. function bp_core_set_uri_globals() { 
  2. global $current_blog, $wp_rewrite; 
  3.  
  4. // Don't catch URIs on non-root blogs unless multiblog mode is on. 
  5. if ( !bp_is_root_blog() && !bp_is_multiblog_mode() ) 
  6. return false; 
  7.  
  8. $bp =buddypress); 
  9.  
  10. // Define local variables. 
  11. $root_profile = $match = false; 
  12. $key_slugs = $matches = $uri_chunks = array(); 
  13.  
  14. // Fetch all the WP page names for each component. 
  15. if ( empty( $bp->pages ) ) 
  16. $bp->pages = bp_core_get_directory_pages(); 
  17.  
  18. // Ajax or not? 
  19. if ( defined( 'DOING_AJAX' ) && DOING_AJAX || strpos( $_SERVER['REQUEST_URI'], 'wp-load.php' ) ) 
  20. $path = bp_get_referer_path(); 
  21. else 
  22. $path = esc_url( $_SERVER['REQUEST_URI'] ); 
  23.  
  24. /** 
  25. * Filters the BuddyPress global URI path. 
  26. * @since 1.0.0 
  27. * @param string $path Path to set. 
  28. */ 
  29. $path = apply_filters( 'bp_uri', $path ); 
  30.  
  31. // Take GET variables off the URL to avoid problems. 
  32. $path = strtok( $path, '?' ); 
  33.  
  34. // Fetch current URI and explode each part separated by '/' into an array. 
  35. $bp_uri = explode( '/', $path ); 
  36.  
  37. // Loop and remove empties. 
  38. foreach ( (array) $bp_uri as $key => $uri_chunk ) { 
  39. if ( empty( $bp_uri[$key] ) ) { 
  40. unset( $bp_uri[$key] ); 
  41.  
  42. /** 
  43. * If running off blog other than root, any subdirectory names must be 
  44. * removed from $bp_uri. This includes two cases: 
  45. * 1. when WP is installed in a subdirectory,  
  46. * 2. when BP is running on secondary blog of a subdirectory 
  47. * multisite installation. Phew! 
  48. */ 
  49. if ( is_multisite() && !is_subdomain_install() && ( bp_is_multiblog_mode() || 1 != bp_get_root_blog_id() ) ) { 
  50.  
  51. // Blow chunks. 
  52. $chunks = explode( '/', $current_blog->path ); 
  53.  
  54. // If chunks exist... 
  55. if ( !empty( $chunks ) ) { 
  56.  
  57. // ...loop through them... 
  58. foreach( $chunks as $key => $chunk ) { 
  59. $bkey = array_search( $chunk, $bp_uri ); 
  60.  
  61. // ...and unset offending keys 
  62. if ( false !== $bkey ) { 
  63. unset( $bp_uri[$bkey] ); 
  64.  
  65. $bp_uri = array_values( $bp_uri ); 
  66.  
  67. // Get site path items. 
  68. $paths = explode( '/', bp_core_get_site_path() ); 
  69.  
  70. // Take empties off the end of path. 
  71. if ( empty( $paths[count( $paths ) - 1] ) ) 
  72. array_pop( $paths ); 
  73.  
  74. // Take empties off the start of path. 
  75. if ( empty( $paths[0] ) ) 
  76. array_shift( $paths ); 
  77.  
  78. // Reset indexes. 
  79. $bp_uri = array_values( $bp_uri ); 
  80. $paths = array_values( $paths ); 
  81.  
  82. // Unset URI indices if they intersect with the paths. 
  83. foreach ( (array) $bp_uri as $key => $uri_chunk ) { 
  84. if ( isset( $paths[$key] ) && $uri_chunk == $paths[$key] ) { 
  85. unset( $bp_uri[$key] ); 
  86.  
  87. // Reset the keys by merging with an empty array. 
  88. $bp_uri = array_merge( array(), $bp_uri ); 
  89.  
  90. /** 
  91. * If a component is set to the front page, force its name into $bp_uri 
  92. * so that $current_component is populated (unless a specific WP post is being requested 
  93. * via a URL parameter, usually signifying Preview mode). 
  94. */ 
  95. if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && empty( $bp_uri ) && empty( $_GET['p'] ) && empty( $_GET['page_id'] ) ) { 
  96. $post = get_post( get_option( 'page_on_front' ) ); 
  97. if ( !empty( $post ) ) { 
  98. $bp_uri[0] = $post->post_name; 
  99.  
  100. // Keep the unfiltered URI safe. 
  101. $bp->unfiltered_uri = $bp_uri; 
  102.  
  103. // Don't use $bp_unfiltered_uri, this is only for backpat with old plugins. Use $bp->unfiltered_uri. 
  104. $GLOBALS['bp_unfiltered_uri'] = &$bp->unfiltered_uri; 
  105.  
  106. // Get slugs of pages into array. 
  107. foreach ( (array) $bp->pages as $page_key => $bp_page ) 
  108. $key_slugs[$page_key] = trailingslashit( '/' . $bp_page->slug ); 
  109.  
  110. // Bail if keyslugs are empty, as BP is not setup correct. 
  111. if ( empty( $key_slugs ) ) 
  112. return; 
  113.  
  114. // Loop through page slugs and look for exact match to path. 
  115. foreach ( $key_slugs as $key => $slug ) { 
  116. if ( $slug == $path ) { 
  117. $match = $bp->pages->{$key}; 
  118. $match->key = $key; 
  119. $matches[] = 1; 
  120. break; 
  121.  
  122. // No exact match, so look for partials. 
  123. if ( empty( $match ) ) { 
  124.  
  125. // Loop through each page in the $bp->pages global. 
  126. foreach ( (array) $bp->pages as $page_key => $bp_page ) { 
  127.  
  128. // Look for a match (check members first). 
  129. if ( in_array( $bp_page->name, (array) $bp_uri ) ) { 
  130.  
  131. // Match found, now match the slug to make sure. 
  132. $uri_chunks = explode( '/', $bp_page->slug ); 
  133.  
  134. // Loop through uri_chunks. 
  135. foreach ( (array) $uri_chunks as $key => $uri_chunk ) { 
  136.  
  137. // Make sure chunk is in the correct position. 
  138. if ( !empty( $bp_uri[$key] ) && ( $bp_uri[$key] == $uri_chunk ) ) { 
  139. $matches[] = 1; 
  140.  
  141. // No match. 
  142. } else { 
  143. $matches[] = 0; 
  144.  
  145. // Have a match. 
  146. if ( !in_array( 0, (array) $matches ) ) { 
  147. $match = $bp_page; 
  148. $match->key = $page_key; 
  149. break; 
  150. }; 
  151.  
  152. // Unset matches. 
  153. unset( $matches ); 
  154.  
  155. // Unset uri chunks. 
  156. unset( $uri_chunks ); 
  157.  
  158. // URLs with BP_ENABLE_ROOT_PROFILES enabled won't be caught above. 
  159. if ( empty( $matches ) && bp_core_enable_root_profiles() && ! empty( $bp_uri[0] ) ) { 
  160.  
  161. // Switch field based on compat. 
  162. $field = bp_is_username_compatibility_mode() ? 'login' : 'slug'; 
  163.  
  164. /** 
  165. * Filter the portion of the URI that is the displayed user's slug. 
  166. * Eg. example.com/ADMIN (when root profiles is enabled) 
  167. * example.com/members/ADMIN (when root profiles isn't enabled) 
  168. * ADMIN would be the displayed user's slug. 
  169. * @since 2.6.0 
  170. * @param string $member_slug 
  171. */ 
  172. $member_slug = apply_filters( 'bp_core_set_uri_globals_member_slug', $bp_uri[0] ); 
  173.  
  174. // Make sure there's a user corresponding to $bp_uri[0]. 
  175. if ( ! empty( $bp->pages->members ) && $root_profile = get_user_by( $field, $member_slug ) ) { 
  176.  
  177. // Force BP to recognize that this is a members page. 
  178. $matches[] = 1; 
  179. $match = $bp->pages->members; 
  180. $match->key = 'members'; 
  181.  
  182. // Search doesn't have an associated page, so we check for it separately. 
  183. if ( !empty( $bp_uri[0] ) && ( bp_get_search_slug() == $bp_uri[0] ) ) { 
  184. $matches[] = 1; 
  185. $match = new stdClass; 
  186. $match->key = 'search'; 
  187. $match->slug = bp_get_search_slug(); 
  188.  
  189. // This is not a BuddyPress page, so just return. 
  190. if ( empty( $matches ) ) 
  191. return false; 
  192.  
  193. $wp_rewrite->use_verbose_page_rules = false; 
  194.  
  195. // Find the offset. With $root_profile set, we fudge the offset down so later parsing works. 
  196. $slug = !empty ( $match ) ? explode( '/', $match->slug ) : ''; 
  197. $uri_offset = empty( $root_profile ) ? 0 : -1; 
  198.  
  199. // Rejig the offset. 
  200. if ( !empty( $slug ) && ( 1 < count( $slug ) ) ) { 
  201. // Only offset if not on a root profile. Fixes issue when Members page is nested. 
  202. if ( false === $root_profile ) { 
  203. array_pop( $slug ); 
  204. $uri_offset = count( $slug ); 
  205.  
  206. // Global the unfiltered offset to use in bp_core_load_template(). 
  207. // To avoid PHP warnings in bp_core_load_template(), it must always be >= 0. 
  208. $bp->unfiltered_uri_offset = $uri_offset >= 0 ? $uri_offset : 0; 
  209.  
  210. // We have an exact match. 
  211. if ( isset( $match->key ) ) { 
  212.  
  213. // Set current component to matched key. 
  214. $bp->current_component = $match->key; 
  215.  
  216. // If members component, do more work to find the actual component. 
  217. if ( 'members' == $match->key ) { 
  218.  
  219. $after_member_slug = false; 
  220. if ( ! empty( $bp_uri[ $uri_offset + 1 ] ) ) { 
  221. $after_member_slug = $bp_uri[ $uri_offset + 1 ]; 
  222.  
  223. // Are we viewing a specific user? 
  224. if ( $after_member_slug ) { 
  225.  
  226. /** This filter is documented in bp-core/bp-core-catchuri.php */ 
  227. $after_member_slug = apply_filters( 'bp_core_set_uri_globals_member_slug', $after_member_slug ); 
  228.  
  229. // If root profile, we've already queried for the user. 
  230. if ( $root_profile instanceof WP_User ) { 
  231. $bp->displayed_user->id = $root_profile->ID; 
  232.  
  233. // Switch the displayed_user based on compatibility mode. 
  234. $bp->displayed_user->id = (int) bp_core_get_userid( urldecode( $after_member_slug ) ); 
  235.  
  236. } else { 
  237. $bp->displayed_user->id = (int) bp_core_get_userid_from_nicename( $after_member_slug ); 
  238.  
  239. // Is this a member type directory? 
  240. if ( ! bp_displayed_user_id() && $after_member_slug === bp_get_members_member_type_base() && ! empty( $bp_uri[ $uri_offset + 2 ] ) ) { 
  241. $matched_types = bp_get_member_types( array( 
  242. 'has_directory' => true,  
  243. 'directory_slug' => $bp_uri[ $uri_offset + 2 ],  
  244. ) ); 
  245.  
  246. if ( ! empty( $matched_types ) ) { 
  247. $bp->current_member_type = reset( $matched_types ); 
  248. unset( $bp_uri[ $uri_offset + 1 ] ); 
  249.  
  250. // If the slug matches neither a member type nor a specific member, 404. 
  251. if ( ! bp_displayed_user_id() && ! bp_get_current_member_type() && $after_member_slug ) { 
  252. // Prevent components from loading their templates. 
  253. $bp->current_component = ''; 
  254. bp_do_404(); 
  255. return; 
  256.  
  257. // If the displayed user is marked as a spammer, 404 (unless logged-in user is a super admin). 
  258. if ( bp_current_user_can( 'bp_moderate' ) ) { 
  259. bp_core_add_message( __( 'This user has been marked as a spammer. Only site admins can view this profile.', buddypress ), 'warning' ); 
  260. } else { 
  261. bp_do_404(); 
  262. return; 
  263.  
  264. // Bump the offset. 
  265. if ( bp_displayed_user_id() ) { 
  266. if ( isset( $bp_uri[$uri_offset + 2] ) ) { 
  267. $bp_uri = array_merge( array(), array_slice( $bp_uri, $uri_offset + 2 ) ); 
  268. $bp->current_component = $bp_uri[0]; 
  269.  
  270. // No component, so default will be picked later. 
  271. } else { 
  272. $bp_uri = array_merge( array(), array_slice( $bp_uri, $uri_offset + 2 ) ); 
  273. $bp->current_component = ''; 
  274.  
  275. // Reset the offset. 
  276. $uri_offset = 0; 
  277.  
  278. // Determine the current action. 
  279. $current_action = isset( $bp_uri[ $uri_offset + 1 ] ) ? $bp_uri[ $uri_offset + 1 ] : ''; 
  280.  
  281. /** 
  282. * If a BuddyPress directory is set to the WP front page, URLs like example.com/members/?s=foo 
  283. * shouldn't interfere with blog searches. 
  284. */ 
  285. if ( empty( $current_action) && ! empty( $_GET['s'] ) && 'page' == get_option( 'show_on_front' ) && ! empty( $match->id ) ) { 
  286. $page_on_front = (int) get_option( 'page_on_front' ); 
  287. if ( (int) $match->id === $page_on_front ) { 
  288. $bp->current_component = ''; 
  289. return false; 
  290.  
  291. $bp->current_action = $current_action; 
  292.  
  293. // Slice the rest of the $bp_uri array and reset offset. 
  294. $bp_uri = array_slice( $bp_uri, $uri_offset + 2 ); 
  295. $uri_offset = 0; 
  296.  
  297. // Set the entire URI as the action variables, we will unset the current_component and action in a second. 
  298. $bp->action_variables = $bp_uri; 
  299.  
  300. // Reset the keys by merging with an empty array. 
  301. $bp->action_variables = array_merge( array(), $bp->action_variables );