Skip to:
Content

BuddyPress.org

Ticket #4470: 4470.2.patch

File 4470.2.patch, 11.3 KB (added by johnjamesjacoby, 12 years ago)

Use _doing_it_wrong() in place of wp_die() to protect users from misbehaving plugins

  • bp-loader.php

     
    3434 */
    3535class BuddyPress {
    3636
    37         /**
    38          * Note to Plugin and Theme authors:
    39          *
    40          * Do not directly reference the variables below in your code. Their names
    41          * and locations in the BuddyPress class are subject to change at any time.
    42          *
    43          * Most of them have reference functions located in bp-core-functions.php.
    44          * The ones that don't can be accessed via their respective WordPress API's.
    45          *
    46          * Components are encouraged to store their data in the $bp global rather
    47          * than new globals to keep all BuddyPress data in one place.
    48          */
     37        /** Magic *****************************************************************/
    4938
    50         /** Version ***************************************************************/
    51 
    5239        /**
    53          * @var string BuddyPress version
    54          */
    55         public $version = '1.7';
    56 
    57         /**
    58          * @var int Database version of current BuddyPress files
    59          */
    60         public $db_version = 6066;
    61 
    62         /**
    63          * @var int Database version raw from database connection
    64          */
    65         public $db_version_raw = 0;
    66 
    67         /**
    68          * @var string State of BuddyPress installation
    69          */
    70         public $maintenance_mode = '';
    71 
    72         /**
    73          * @var bool Include deprecated BuddyPress files or not
    74          */
    75         public $load_deprecated = true;
    76 
    77         /** Root ******************************************************************/
    78 
    79         /**
    80          * @var int The root blog ID
    81          */
    82         public $root_blog_id = 1;
    83 
    84         /** Paths *****************************************************************/
    85 
    86         /**
    87          * The absolute path and filename of this file.
     40         * BuddyPress uses many variables, most of which can be filtered to customize
     41         * the way that it works. To prevent unauthorized access, these variables
     42         * are stored in a private array that is magically updated using PHP 5.2+
     43         * methods. This is to prevent third party plugins from tampering with
     44         * essential information indirectly, which would cause issues later.
    8845         *
    89          * @since BuddyPress (1.6)
    90          * @var string
     46         * @see BuddyPress::setup_globals()
     47         * @var array
    9148         */
    92         public $file;
     49        private $data;
    9350
    94         /**
    95          * @var string Basename of the BuddyPress plugin directory
    96          */
    97         public $basename = '';
     51        /** Not Magic *************************************************************/
    9852
    9953        /**
    100          * @var string Absolute path to the BuddyPress plugin directory
     54         * @var array Primary BuddyPress navigation
    10155         */
    102         public $plugin_dir = '';
     56        public $bp_nav = array();
    10357
    10458        /**
    105          * @var string Absolute path to the BuddyPress themes directory
     59         * @var array Secondary BuddyPress navigation to $bp_nav
    10660         */
    107         public $themes_dir = '';
     61        public $bp_options_nav = array();
    10862
    10963        /**
    110          * @var string Absolute path to the BuddyPress language directory
     64         * @var array The unfiltered URI broken down into chunks
     65         * @see bp_core_set_uri_globals()
    11166         */
    112         public $lang_dir = '';
     67        public $unfiltered_uri = array();
    11368
    114         /** URLs ******************************************************************/
    115 
    11669        /**
    117          * @var string URL to the BuddyPress plugin directory
     70         * @var array The canonical URI stack
     71         * @see bp_redirect_canonical()
     72         * @see bp_core_new_nav_item()
    11873         */
    119         public $plugin_url = '';
     74        public $canonical_stack = array();
    12075
    12176        /**
    122          * @var string URL to the BuddyPress themes directory
     77         * @var array Additional navigation elements (supplemental)
    12378         */
    124         public $themes_url = '';
     79        public $action_variables = array();
    12580
    126         /** Users *****************************************************************/
    127 
    12881        /**
    129          * @var object Current user
     82         * @var array Required components (core, members)
    13083         */
    131         public $current_user = null;
     84        public $required_components = array();
    13285
    13386        /**
    134          * @var object Displayed user
     87         * @var array Additional active components
    13588         */
    136         public $displayed_user = null;
     89        public $loaded_components = array();
    13790
    138         /** Navigation ************************************************************/
     91        /** Option Overload *******************************************************/
    13992
    14093        /**
    141          * @var array Primary BuddyPress navigation
     94         * @var array Optional Overloads default options retrieved from get_option()
    14295         */
    143         public $bp_nav = array();
     96        public $options = array();
    14497
    145         /**
    146          * @var array Secondary BuddyPress navigation to $bp_nav
    147          */
    148         public $bp_options_nav = array();
     98        /** Singleton *************************************************************/
    14999
    150         /** Toolbar ***************************************************************/
    151 
    152100        /**
    153          * @var string The primary toolbar ID
     101         * @var BuddyPress The one true BuddyPress
    154102         */
    155         public $my_account_menu_id = '';
     103        private static $instance;
    156104
    157         /** URI's *****************************************************************/
    158 
    159105        /**
    160          * @var array The unfiltered URI broken down into chunks
    161          * @see bp_core_set_uri_globals()
     106         * Main BuddyPress Instance
     107         *
     108         * BuddyPress is great
     109         * Please load it only one time
     110         * For this, we thank you
     111         *
     112         * Insures that only one instance of BuddyPress exists in memory at any one
     113         * time. Also prevents needing to define globals all over the place.
     114         *
     115         * @since BuddyPress (1.7)
     116         *
     117         * @staticvar array $instance
     118         * @uses BuddyPress::constants() Setup the constants (mostly deprecated)
     119         * @uses BuddyPress::setup_globals() Setup the globals needed
     120         * @uses BuddyPress::includes() Include the required files
     121         * @uses BuddyPress::setup_actions() Setup the hooks and actions
     122         * @see buddypress()
     123         *
     124         * @return The one true BuddyPress
    162125         */
    163         public $unfiltered_uri = array();
     126        public static function instance() {
     127                if ( ! isset( self::$instance ) ) {
     128                        self::$instance = new BuddyPress;
     129                        self::$instance->constants();
     130                        self::$instance->setup_globals();
     131                        self::$instance->includes();
     132                        self::$instance->setup_actions();
     133                }
     134                return self::$instance;
     135        }
    164136
    165         /**
    166          * @var int The current offset of the URI
    167          * @see bp_core_set_uri_globals()
    168          */
    169         public $unfiltered_uri_offset = 0;
     137        /** Magic Methods *********************************************************/
    170138
    171139        /**
    172          * @var bool Are status headers already sent?
     140         * A dummy constructor to prevent BuddyPress from being loaded more than once.
     141         *
     142         * @since BuddyPress (1.7)
     143         * @see BuddyPress::instance()
     144         * @see buddypress()
    173145         */
    174         public $no_status_set = false;
     146        private function __construct() { /* Do nothing here */ }
    175147
    176148        /**
    177          * @var array The canonical URI stack
    178          * @see bp_redirect_canonical()
    179          * @see bp_core_new_nav_item()
     149         * A dummy magic method to prevent BuddyPress from being cloned
     150         *
     151         * @since BuddyPress (1.7)
    180152         */
    181         public $canonical_stack = array();
     153        public function __clone() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'buddypress' ), '1.7' ); }
    182154
    183         /** Components ************************************************************/
    184 
    185155        /**
    186          * @var string Name of the current BuddyPress component (primary)
     156         * A dummy magic method to prevent BuddyPress from being unserialized
     157         *
     158         * @since BuddyPress (1.7)
    187159         */
    188         public $current_component = '';
     160        public function __wakeup() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'buddypress' ), '1.7' ); }
    189161
    190162        /**
    191          * @var string Name of the current BuddyPress item (secondary)
     163         * Magic method for checking the existence of a certain custom field
     164         *
     165         * @since BuddyPress (1.7)
    192166         */
    193         public $current_item = '';
     167        public function __isset( $key ) { return isset( $this->data[$key] ); }
    194168
    195169        /**
    196          * @var string Name of the current BuddyPress action (tertiary)
     170         * Magic method for getting BuddyPress varibles
     171         *
     172         * @since BuddyPress (1.7)
    197173         */
    198         public $current_action = '';
     174        public function __get( $key ) { return isset( $this->data[$key] ) ? $this->data[$key] : null; }
    199175
    200176        /**
    201          * @var array Additional navigation elements (supplemental)
     177         * Magic method for setting BuddyPress varibles
     178         *
     179         * @since BuddyPress (1.7)
    202180         */
    203         public $action_variables = array();
     181        public function __set( $key, $value ) { $this->data[$key] = $value; }
    204182
    205         /**
    206          * @var bool Displaying custom 2nd level navigation menu (I.E a group)
    207          */
    208         public $is_single_item = false;
     183        /** Private Methods *******************************************************/
    209184
    210         /** Option Overload *******************************************************/
    211 
    212185        /**
    213          * @var array Optional Overloads default options retrieved from get_option()
    214          */
    215         public $options = array();
    216 
    217         /** Functions *************************************************************/
    218 
    219         /**
    220          * The main BuddyPress loader
    221          *
    222          * @since BuddyPress (1.6)
    223          *
    224          * @uses BuddyPress::constants() Setup legacy constants
    225          * @uses BuddyPress::setup_globals() Setup globals needed
    226          * @uses BuddyPress::includes() Includ required files
    227          * @uses BuddyPress::setup_actions() Setup hooks and actions
    228          */
    229         public function __construct() {
    230                 $this->constants();
    231                 $this->setup_globals();
    232                 $this->includes();
    233                 $this->setup_actions();
    234         }
    235 
    236         /**
    237186         * Legacy BuddyPress constants
    238187         *
    239188         * Try to avoid using these. Their values have been moved into variables
     
    315264         */
    316265        private function setup_globals() {
    317266
     267                /** Versions **********************************************************/
     268
     269                $this->version        = '1.7';
     270                $this->db_version     = 6066;
     271                $this->db_version_raw = 0;
     272
     273                /** Loading ***********************************************************/
     274
     275                $this->maintenance_mode = '';
     276                $this->load_deprecated  = true;
     277
     278                /** Toolbar ***********************************************************/
     279
     280                /**
     281                 * @var string The primary toolbar ID
     282                 */
     283                $this->my_account_menu_id = '';
     284
     285                /** URI's *************************************************************/
     286
     287                /**
     288                 * @var int The current offset of the URI
     289                 * @see bp_core_set_uri_globals()
     290                 */
     291                $this->unfiltered_uri_offset = 0;
     292
     293                /**
     294                 * @var bool Are status headers already sent?
     295                 */
     296                $this->no_status_set = false;
     297
     298                /** Components ********************************************************/
     299
     300                /**
     301                 * @var string Name of the current BuddyPress component (primary)
     302                 */
     303                $this->current_component = '';
     304
     305                /**
     306                 * @var string Name of the current BuddyPress item (secondary)
     307                 */
     308                $this->current_item = '';
     309
     310                /**
     311                 * @var string Name of the current BuddyPress action (tertiary)
     312                 */
     313                $this->current_action = '';
     314
     315                /**
     316                 * @var bool Displaying custom 2nd level navigation menu (I.E a group)
     317                 */
     318                $this->is_single_item = false;
     319       
    318320                /** Root **************************************************************/
    319321
    320322                // BuddyPress Root blog ID
     
    481483        }
    482484}
    483485
    484 // "And now for something completely different"
    485 $GLOBALS['bp'] = new BuddyPress;
     486/**
     487 * The main function responsible for returning the one true BuddyPress Instance
     488 * to functions everywhere.
     489 *
     490 * Use this function like you would a global variable, except without needing
     491 * to declare the global.
     492 *
     493 * Example: <?php $bp = buddypress(); ?>
     494 *
     495 * @return The one true BuddyPress Instance
     496 */
     497function buddypress() {
     498        return buddypress::instance();
     499}
    486500
    487 endif;
     501/**
     502 * Hook BuddyPress early onto the 'plugins_loaded' action.
     503 *
     504 * This gives all other plugins the chance to load before BuddyPress, to get
     505 * their actions, filters, and overrides setup without BuddyPress being in the
     506 * way.
     507 */
     508if ( defined( 'BUDDYPRESS_LATE_LOAD' ) ) {
     509        add_action( 'plugins_loaded', 'buddypress', (int) BUDDYPRESS_LATE_LOAD );
    488510
    489 ?>
     511// "And now here's something we hope you'll really like!"
     512} else {
     513        $GLOBALS['bp'] = &buddypress();
     514}
     515
     516endif;