Skip to:
Content

BuddyPress.org

Ticket #225: bp-core-catchuri.php

File bp-core-catchuri.php, 8.9 KB (added by jehy, 16 years ago)

bp-core-catchuri fixed

Line 
1<?php
2/*
3Based on contributions from: Chris Taylor - http://www.stillbreathing.co.uk/
4Modified for BuddyPress by: Andy Peatling - http://apeatling.wordpress.com/
5*/
6
7/**
8 * bp_core_set_uri_globals()
9 *
10 * Analyzes the URI structure and breaks it down into parts for use in code.
11 * The idea is that BuddyPress can use complete custom friendly URI's without the
12 * user having to add new re-write rules.
13 *
14 * Future custom components would then be able to use their own custom URI structure.
15 *
16 * The URI's are broken down as follows:
17 *   - http:// domain.com / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
18 *   - OUTSIDE ROOT: http:// domain.com / sites / buddypress / members / andy / [current_component] / [current_action] / [action_variables] / [action_variables] / ...
19 *
20 *      Example:
21 *    - http://domain.com/members/andy/profile/edit/group/5/
22 *    - $current_component: string 'profile'
23 *    - $current_action: string 'edit'
24 *    - $action_variables: array ['group', 5]
25 *
26 * @package BuddyPress Core
27 */
28function bp_core_set_uri_globals() {
29        global $current_component, $current_action, $action_variables;
30        global $current_userid;
31        global $is_member_page, $is_new_friend;
32       
33        $path = apply_filters( 'bp_uri', $_SERVER['REQUEST_URI'] );
34       
35        // Firstly, take GET variables off the URL to avoid problems,
36        // they are still registered in the global $_GET variable */
37        $noget = substr( $path, 0, strpos( $path, '?' ) );
38        if ( $noget != '' ) $path = $noget;
39       
40        /* Fetch the current URI and explode each part seperated by '/' into an array */
41        $bp_uri = explode( "/", $path );
42       
43        /* Take empties off the end of complete URI */
44        if ( $bp_uri[count($bp_uri) - 1] == "" )
45                array_pop( $bp_uri );
46
47        /* Take empties off the start of complete URI */
48        if ( $bp_uri[0] == "" )
49                array_shift( $bp_uri );
50               
51        /* Get total URI segment count */
52        $bp_uri_count = count( $bp_uri ) - 1;
53        $is_member_page = false;
54       
55        /* Set the indexes, these are incresed by one if we are not on a VHOST install */
56        $component_index = 0;
57        $action_index = $component_index + 1;
58       
59        // If this is a WordPress page, return from the function.
60        if ( is_page( $bp_uri[$component_index] ) )
61                return false;
62       
63        /* Get site path items */
64        $paths = explode( '/', bp_core_get_site_path() );
65
66        /* Take empties off the end of path */
67        if ( $paths[count($paths) - 1] == "" )
68                array_pop( $paths );
69
70        /* Take empties off the start of path */
71        if ( $paths[0] == "" )
72                array_shift( $paths );
73
74        for ( $i = 0; $i < $bp_uri_count; $i++ ) {
75                if ( in_array( $bp_uri[$i], $paths )) {
76                        unset( $bp_uri[$i] );
77                }
78        }
79       
80        /* Reset the keys by merging with an empty array */
81        $bp_uri = array_merge( array(), $bp_uri );     
82
83        /* Catch a member page and set the current member ID */
84        if ( $bp_uri[0] == MEMBERS_SLUG && $bp_uri[1] != '' ) {
85                $is_member_page = true;
86                $is_root_component = true;
87               
88                // We are within a member page, set up user id globals
89                $current_userid = bp_core_get_current_userid( $bp_uri[1] );
90                               
91                unset($bp_uri[0]);
92                unset($bp_uri[1]);
93               
94                // if the get variable 'new' is set this the first visit to a new friends profile.
95                // this means we need to delete friend acceptance notifications, so we set a flag of is_new_friend.
96                if ( isset($_GET['new']) ) {
97                        $is_new_friend = 1;
98                        unset($bp_uri[2]);
99                }
100               
101                /* Reset the keys by merging with an empty array */
102                $bp_uri = array_merge( array(), $bp_uri );
103        }
104       
105        /* This is used to determine where the component and action indexes should start */
106        $root_components = explode( ',', BP_CORE_ROOT_COMPONENTS );
107       
108        if ( !isset($is_root_component) )
109                $is_root_component = in_array( $bp_uri[0], $root_components );
110
111        if ( VHOST == 'no' && !$is_root_component ) {
112                $component_index++;
113                $action_index++;
114        }
115
116        /* Set the current component */
117        $current_component = $bp_uri[$component_index];
118       
119        /* Set the current action */
120        $current_action = $bp_uri[$action_index];
121       
122        /* Set the entire URI as the action variables, we will unset the current_component and action in a second */
123        $action_variables = $bp_uri;
124
125        /* Unset the current_component and action from action_variables */
126        unset($action_variables[$component_index]);
127        unset($action_variables[$action_index]);
128
129        /* Remove the username from action variables if this is not a VHOST install */
130        if ( VHOST == 'no' && !$is_root_component )
131                array_shift($action_variables);
132       
133        /* Reset the keys by merging with an empty array */
134        $action_variables = array_merge( array(), $action_variables );
135
136        //var_dump($current_component, $current_action, $action_variables);
137}
138add_action( 'wp', 'bp_core_set_uri_globals', 1 );
139
140/**
141 * bp_catch_uri()
142 *
143 * Takes either a single page name or array of page names and
144 * loads the first template file that can be found.
145 *
146 * @package BuddyPress Core
147 * @global $bp_path BuddyPress global containing the template file names to use.
148 * @param $pages Template file names to use.
149 * @uses add_action() Hooks a function on to a specific action
150 */
151function bp_catch_uri( $pages, $skip_blog_check = false ) {
152        global $bp_path, $bp_skip_blog_check;
153       
154        $bp_skip_blog_check = $skip_blog_check;
155
156        $bp_path = $pages;
157       
158        remove_action( 'template_redirect', 'redirect_canonical' );
159        add_action( 'template_redirect', 'bp_core_do_catch_uri' );
160}
161
162/**
163 * bp_core_do_catch_uri()
164 *
165 * Loads the first template file found based on the $bp_path global.
166 *
167 * @package BuddyPress Core
168 * @global $bp_path BuddyPress global containing the template file names to use.
169 */
170function bp_core_do_catch_uri() {
171        global $bp_path, $bp, $wpdb;
172        global $current_blog, $bp_skip_blog_check;
173        global $bp_no_status_set;
174       
175        $pages = $bp_path;
176       
177        /* Don't hijack any URLs on blog pages */
178        if ( !$bp_skip_blog_check ) {
179                if ( bp_is_blog_page() )
180                        return false;
181        }
182       
183        /* Make sure this is not reported as a 404 */
184        if ( !$bp_no_status_set ) {
185                status_header( 200 );
186        }
187
188        if ( is_array( $pages ) ) {
189                foreach( $pages as $page ) {
190                        if ( file_exists( TEMPLATEPATH . "/" . $page . ".php" ) ) {
191                                load_template( TEMPLATEPATH . "/" . $page . ".php" );
192                        }
193                }
194        } else {
195                if ( file_exists( TEMPLATEPATH . "/" . $pages . ".php" ) ) {
196                        load_template( TEMPLATEPATH . "/" . $pages . ".php" );
197                } else {
198                        if ( file_exists( TEMPLATEPATH . "/404.php" ) ) {
199                                status_header( 404 );
200                                load_template( TEMPLATEPATH . "/404.php" );
201                        } else if ( file_exists( TEMPLATEPATH . "/home.php" ) ) {
202                                load_template( TEMPLATEPATH . "/home.php" );
203                        } else {
204                                load_template( TEMPLATEPATH . "/index.php" );
205                        }
206                }
207        }
208        die;
209}
210
211function bp_core_catch_no_access() {
212        global $bp, $bp_path, $bp_no_status_set;
213
214        // If bp_core_redirect() and $bp_no_status_set is true,
215        // we are redirecting to an accessable page, so skip this check.
216        if ( $bp_no_status_set )
217                return false;
218               
219        if ( !$bp_path && !bp_is_blog_page() ) {
220                if ( is_user_logged_in() ) {
221                        wp_redirect( $bp['loggedin_domain'] );
222                } else {
223                        wp_redirect( site_url( 'wp-login.php?redirect_to=' . site_url() . $_SERVER['REQUEST_URI'] ) );
224                }
225        }
226}
227add_action( 'wp', 'bp_core_catch_no_access', 10 );
228
229/**
230 * bp_core_catch_profile_uri()
231 *
232 * If the extended profiles component is not installed we still need
233 * to catch the /profile URI's and display whatever we have installed.
234 *
235 */
236function bp_core_catch_profile_uri() {
237        global $bp;
238       
239        if ( !function_exists('xprofile_install') )
240                bp_catch_uri( 'profile/index' );
241}
242
243function bp_core_force_buddypress_theme() {
244        global $current_component, $current_action;
245        global $is_member_page;
246       
247        $member_theme = get_site_option('active-member-theme');
248       
249        if ( $member_theme == '' )
250                $member_theme = 'buddypress-member';
251       
252        // The theme filter does not recognize any globals, where as the stylesheet filter does.
253        // We have to set up the globals to use manually.
254        bp_core_set_uri_globals();
255       
256        if ( function_exists('groups_setup_globals') )
257                $groups_bp = groups_setup_globals(true);
258
259        if(class_exists(BP_Groups_Group))
260        {
261        if ( $current_component == $groups_bp['groups']['slug'] )
262                $is_single_group = BP_Groups_Group::group_exists( $current_action, $groups_bp['groups']['table_name'] );
263        }
264       
265        if ( $is_member_page || ( $current_component == $groups_bp['groups']['slug'] && $is_single_group ) ) {
266                add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
267                add_filter( 'theme_root_uri', 'bp_core_set_member_theme_root_uri' );
268
269                return $member_theme;
270        } else {
271                return get_option('template');
272        }
273       
274        return $theme;
275}
276add_filter( 'template', 'bp_core_force_buddypress_theme' );
277
278function bp_core_force_buddypress_stylesheet() {
279        global $bp, $is_single_group, $is_member_page;
280
281        $member_theme = get_site_option('active-member-theme');
282       
283        if ( $member_theme == '' )
284                $member_theme = 'buddypress-member';
285       
286        if ( $is_member_page || ( $bp['current_component'] == $bp['groups']['slug'] && $is_single_group ) ) {
287                add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
288                add_filter( 'theme_root_uri', 'bp_core_set_member_theme_root_uri' );
289               
290                return $member_theme;
291        } else {
292                return get_option('stylesheet');       
293        }
294}
295add_filter( 'stylesheet', 'bp_core_force_buddypress_stylesheet' );
296
297
298
299?>