Skip to:
Content

BuddyPress.org

Ticket #1479: bp-component.php

File bp-component.php, 6.8 KB (added by johnjamesjacoby, 15 years ago)

Universal Component Class

Line 
1<?php
2/*
3Plugin Name: BP-Test
4Plugin URI: http://buddypress.org/
5Description: BP Test Class
6Author: The BuddyPress Community
7Version: Concept
8Author URI: http://buddypress.org/developers/
9Site Wide Only: true
10*/
11
12add_action( 'plugins_loaded', 'bp_test_load' );
13
14function bp_test_load() {
15        global $bp3;
16
17        $friends = new BP_Component (
18                array(
19                        'id' => 'friends',
20                        'slug' => 'friends',
21                        'version' => '1.3',
22                        'table_name' => 'bp_friends',
23                        'object' => 'user',
24                        'active' => true,
25                        'css_class' => 'bp_friends',
26                )
27        );
28
29        $followers = new BP_Component (
30                array(
31                        'id' => 'followers',
32                        'slug' => 'followers',
33                        'version' => '1.3',
34                        'table_name' => 'bp_followers',
35                        'object' => 'user',
36                        'active' => true,
37                        'css_class' => 'bp_followers',
38                )
39        );
40
41        /* Uncomment to see it load */
42        //var_dump( $bp3 );
43}
44
45/* Make sure slug is healthy */
46function bp_component_sanitize_slug( $slug ) {
47        return $slug;
48}
49
50/* Check table_name */
51function bp_component_check_table_name( $table_name ) {
52        return $table_name;
53}
54
55/* Standard component class for all BuddyPress components
56 *
57 * It includes all of the necessary functions and variables to
58 * create, initialize, and control all BuddyPress components.
59 *
60 * To be used by core and external plugins
61 *
62 * @since 1.3
63 *
64 */
65
66class BP_Component {
67        /* Set a unique ID for internal reference
68         *      - Will be checked for duplicates
69         */
70        var $id;
71
72        /* Unique slug to identify component in URI's
73         *      - Will be checked for duplicates
74         */
75        var $slug;
76
77        /* For updating the DB schema */
78        var $version;
79
80        /* Set the table name where component data is saved
81         *      - Table will be created/updated by this class
82         */
83        var $table_name;
84
85        /* Set object type
86         *      - user/group/blog/custom type
87         *  - @uses bp_get_registered_objects
88         */
89        var $object;
90
91        /* Set if component is on or off
92         *      - Defaults to off
93         */
94        var $active;
95
96        /* Best to set this as sanitized component name
97         *      - @uses sanitize_title
98         */
99        var $css_class;
100
101        /* Used internally */
102        var $before_install;
103        var $after_install;
104
105        /* Initialize new component */
106        function bp_component( $args ) {
107                $this->setup_globals( $args );
108        }
109
110        /* Setup component values */
111        function setup_globals( $args ) {
112                global $bp3;
113
114                /* Allow for pre component initialization */
115                $this->init();
116                do_action( $this->id . '_setup_globals' );
117
118                /* Set default args */
119                $defaults = array (
120                        'id' => 'bp_component',
121                        'slug' => 'bp_component',
122                        'version' => BP_CORE_DB_VERSION,
123                        'table_name' => '',
124                        'object' => 'bp_component',
125                        'active' => false,
126                        'css_class' => 'bp_component',
127                        'before_install' => false,
128                        'after_install' => false,
129                );
130
131                /* Parse args and extract */
132                $params = wp_parse_args( $args, $defaults );
133                extract( $params, EXTR_SKIP );
134
135                $this->id = $id;
136                $this->slug = $slug;
137                $this->version = $version;
138                $this->table_name = $wpdb->base_prefix . $table_name;
139                $this->object = $object;
140                $this->active = $active;
141                $this->css_class = $css_class;
142
143                /* Check existing components for conflicting values */
144                if ( $this->check_conflict() ) {
145                        die( 'Dupe!' );
146
147                /* Register this in the active components array */
148                } else {
149                        if ( $this->active )
150                                $bp3->active_components[$this->id] = $this;
151                }
152
153                do_action ( $this->id . '_setup_globals');
154        }
155
156        /* Run after construct but before setup_globals */
157        function init() {
158                do_action( $this->id . '_init' );
159        }
160
161        /* Run before db schema installation */
162        function before_install() {
163                do_action( $this->id . '_before_install' );
164        }
165
166        /* Run after db schema installation */
167        function after_install() {
168                do_action( $this->id . '_after_install' );
169        }
170
171        /* Check if db schema needs to be installed/upgraded */
172        function check_installed() {
173                global $wpdb, $bp3;
174
175                if ( get_site_option( $this->id . '-db-version') < $this->version )
176                        $this->install();
177
178        }
179
180        /* Install if need be */
181        function install( $sql ) {
182                global $wpdb;
183
184                /* Allow components to perform actions before install */
185                $this->before_install();
186
187                if ( !empty( $wpdb->charset ) )
188                        $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
189
190                require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );
191                dbDelta( $sql );
192
193                /* Allow components to perform action after install */
194                $this->after_install();
195
196                /* Set component version in site meta */
197                update_site_option( $this->id . '-db-version', $this->version );
198
199                do_action( $this->id . '_installed' );
200        }
201
202        function setup_nav() {
203                do_action( $this->id . '_setup_nav' );
204        }
205
206        function admin_menu() {
207                global $wpdb, $bp3;
208
209                if ( !is_site_admin() )
210                        return false;
211
212                do_action( $this->id . '_admin_menu' );
213        }
214
215        /* Template files are stored like $template/$slug/$screen
216         *
217         * Examples:    /bp-default/members/index
218         *                              /bp-default/members/my-friends
219         *
220         *                              /bp-default/groups/index
221         *                              /bp-default/groups/my-groups
222         *
223         *                              /bp-default/blogs/index
224         *                              /bp-default/blogs/create
225         *
226         *                              /bp-default/activity/index
227         */
228        function screen( $screen, $single = false ) {
229                /* Not a single item */
230                if ( $single === false ) {
231                        do_action( $this->id . '_screen_' . $screen );
232                        bp_core_load_template( apply_filters( $this->id . '_template_' . $screen, $this->slug . '/' . $screen ) );
233
234                /* Viewing a single item */
235                } else {
236                        do_action( $this->id . '_screen_' . $screen );
237                        bp_core_load_template( apply_filters( $this->id . '_template_' . $screen, $this->slug . '/single/' . $screen ) );
238                }
239        }
240
241        /* Template files are stored like $template/$slug/single/$screen
242         *
243         * Examples:    /bp-default/members/single/my-friends
244         *                              /bp-default/members/single/friends-activity
245         *
246         *                              /bp-default/groups/single/my-groups
247         *                              /bp-default/groups/single/groups-activity
248         *
249         *                              /bp-default/forums/single/edit
250         *                              /bp-default/forums/single/topic
251         *
252         *                              /bp-default/activity/single/my-activity
253         *                              /bp-default/activity/single/friends-activity
254         *                              /bp-default/activity/single/blogs-activity
255         */
256        function single_screen( $screen ) {
257                $this->screen( $screen, true );
258        }
259
260        /* Default for when screen is not set */
261        function single_screen_permalink() {
262                do_action( $this->id . '_screen_single_permalink' );
263        }
264
265        /* Handle removing of data */
266        function remove_data() {
267                do_action( $this->id . '_remove_data', $this );
268        }
269
270        function check_conflict() {
271                global $bp3;
272
273                /* If first item, no dupe */
274                if ( !is_array( $bp3->active_components ) )
275                        return false;
276
277                /* Loop through components and look for dupes */
278                foreach ( $bp3->active_components as $component ) {
279
280                        if ( $this->id == $component->id )
281                                return true;
282
283                        if ( $this->slug == $component->slug )
284                                return true;
285
286                        if ( $this->slug == $component->slug )
287                                return true;
288
289                        if ( ( $this->table_name && $component->table_name) && $this->table_name == $component->table_name )
290                                return true;
291
292                        if ( $this->css_class == $component->css_class )
293                                return true;
294
295                }
296
297                /* No dupes, return false */
298                return false;
299
300        }
301}
302?>