BP_Gifts_Component

Implementation of BP_Component.

Defined (1)

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

/includes/bp-gifts-loader.php  
  1. class BP_Gifts_Component extends BP_Component { 
  2.  
  3. /** 
  4. * Constructor method 
  5. * You can do all sorts of stuff in your constructor, but it's recommended that, at the 
  6. * very least, you call the parent::start() function. This tells the parent BP_Component 
  7. * to begin its setup routine. 
  8. * BP_Component::start() takes three parameters: 
  9. * (1) $id - A unique identifier for the component. Letters, numbers, and underscores 
  10. * only. 
  11. * (2) $name - This is a translatable name for your component, which will be used in 
  12. * various places through the BuddyPress admin screens to identify it. 
  13. * (3) $path - The path to your plugin directory. Primarily, this is used by 
  14. * BP_Component::includes(), to include your plugin's files. See loader.php 
  15. * to see how BP_EXAMPLE_PLUGIN_DIR was defined. 
  16. * @package BuddyPress_Skeleton_Component 
  17. * @since 1.6 
  18. */ 
  19. function __construct() { 
  20. global $bp; 
  21.  
  22. parent::start( 
  23. 'gifts',  
  24. __( 'Gifts', 'bp-gifts' ),  
  25. BP_GIFTS_PLUGIN_DIR 
  26. ); 
  27.  
  28. /** 
  29. * BuddyPress-dependent plugins are loaded too late to depend on BP_Component's 
  30. * hooks, so we must call the function directly. 
  31. */ 
  32. $this->includes(); 
  33.  
  34. /** 
  35. * Put your component into the active components array, so that 
  36. * bp_is_active( 'example' ); 
  37. * returns true when appropriate. We have to do this manually, because non-core 
  38. * components are not saved as active components in the database. 
  39. */ 
  40. $bp->active_components[$this->id] = '1'; 
  41.  
  42. /** 
  43. * Hook the register_post_types() method. If you're using custom post types to store 
  44. * data (which is recommended), you will need to hook your function manually to 
  45. * 'init'. 
  46. */ 
  47. add_action( 'init', array( &$this, 'register_post_types' ) ); 
  48.  
  49. /** 
  50. * Include your component's files 
  51. * BP_Component has a method called includes(), which will automatically load your plugin's 
  52. * files, as long as they are properly named and arranged. BP_Component::includes() loops 
  53. * through the $includes array, defined below, and for each $file in the array, it tries 
  54. * to load files in the following locations: 
  55. * (1) $this->path . '/' . $file - For example, if your $includes array is defined as 
  56. * $includes = array( 'notifications.php', 'filters.php' ); 
  57. * BP_Component::includes() will try to load these files (assuming a typical WP 
  58. * setup): 
  59. * /wp-content/plugins/bp-example/notifications.php 
  60. * /wp-content/plugins/bp-example/filters.php 
  61. * Our includes function, listed below, uses a variation on this method, by specifying 
  62. * the 'includes' directory in our $includes array. 
  63. * (2) $this->path . '/bp-' . $this->id . '/' . $file - Assuming the same $includes array 
  64. * as above, BP will look for the following files: 
  65. * /wp-content/plugins/bp-example/bp-example/notifications.php 
  66. * /wp-content/plugins/bp-example/bp-example/filters.php 
  67. * (3) $this->path . '/bp-' . $this->id . '/' . 'bp-' . $this->id . '-' . $file . '.php' - 
  68. * This is the format that BuddyPress core components use to load their files. Given 
  69. * an $includes array like 
  70. * $includes = array( 'notifications', 'filters' ); 
  71. * BP looks for files at: 
  72. * /wp-content/plugins/bp-example/bp-example/bp-example-notifications.php 
  73. * /wp-content/plugins/bp-example/bp-example/bp-example-filters.php 
  74. * If you'd prefer not to use any of these naming or organizational schemas, you are not 
  75. * required to use parent::includes(); your own includes() method can require the files 
  76. * manually. For example: 
  77. * require( $this->path . '/includes/notifications.php' ); 
  78. * require( $this->path . '/includes/filters.php' ); 
  79. * Notice that this method is called directly in $this->__construct(). While this step is 
  80. * not necessary for BuddyPress core components, plugins are loaded later, and thus their 
  81. * includes() method must be invoked manually. 
  82. * Our example component includes a fairly large number of files. Your component may not 
  83. * need to have versions of all of these files. What follows is a short description of 
  84. * what each file does; for more details, open the file itself and see its inline docs. 
  85. * - -actions.php - Functions hooked to bp_actions, mainly used to catch action 
  86. * requests (save, delete, etc) 
  87. * - -screens.php - Functions hooked to bp_screens. These are the screen functions 
  88. * responsible for the display of your plugin's content. 
  89. * - -filters.php - Functions that are hooked via apply_filters() 
  90. * - -classes.php - Your plugin's classes. Depending on how you organize your 
  91. * plugin, this could mean: a database query class, a custom post 
  92. * type data schema, and so forth 
  93. * - -activity.php - Functions related to the BP Activity Component. This is where 
  94. * you put functions responsible for creating, deleting, and 
  95. * modifying activity items related to your component 
  96. * - -template.php - Template tags. These are functions that are called from your 
  97. * templates, or from your screen functions. If your plugin 
  98. * contains its own version of the WordPress Loop (such as 
  99. * bp_example_has_items()), those functions should go in this file. 
  100. * - -functions.php - Miscellaneous utility functions required by your component. 
  101. * - -notifications.php - Functions related to email notification, as well as the 
  102. * BuddyPress notifications that show up in the admin bar. 
  103. * - -widgets.php - If your plugin includes any sidebar widgets, define them in this 
  104. * file. 
  105. * - -buddybar.php - Functions related to the BuddyBar. 
  106. * - -adminbar.php - Functions related to the WordPress Admin Bar. 
  107. * - -cssjs.php - Here is where you set up and enqueue your CSS and JS. 
  108. * - -ajax.php - Functions used in the process of AJAX requests. 
  109. * @package BuddyPress_Skeleton_Component 
  110. * @since 1.6 
  111. */ 
  112. function includes() { 
  113.  
  114. // Files to include 
  115. $includes = array( 
  116. 'includes/bp-gifts-filters.php',  
  117. 'includes/bp-gifts-classes.php',  
  118. 'includes/bp-gifts-notifications.php',  
  119. 'includes/bp-gifts-cssjs.php',  
  120. 'includes/bp-gifts-ajax.php' 
  121. ); 
  122.  
  123. parent::includes( $includes ); 
  124.  
  125. // As an example of how you might do it manually, let's include the functions used 
  126. // on the WordPress Dashboard conditionally: 
  127. if ( is_admin && !is_network_admin() ) { 
  128. include( BP_GIFTS_PLUGIN_DIR . '/includes/bp-gifts-admin.php' ); 
  129. if ( is_admin() && is_network_admin() ) { 
  130. include( BP_GIFTS_PLUGIN_DIR . '/includes/bp-gifts-admin-network.php' ); 
  131.  
  132. /** 
  133. * Set up your plugin's globals 
  134. * Use the parent::setup_globals() method to set up the key global data for your plugin: 
  135. * - 'slug' - This is the string used to create URLs when your component 
  136. * adds navigation underneath profile URLs. For example,  
  137. * in the URL http://testbp.com/members/boone/example, the 
  138. * 'example' portion of the URL is formed by the 'slug'. 
  139. * Site admins can customize this value by defining 
  140. * BP_EXAMPLE_SLUG in their wp-config.php or bp-custom.php 
  141. * files. 
  142. * - 'root_slug' - This is the string used to create URLs when your component 
  143. * adds navigation to the root of the site. In other words,  
  144. * you only need to define root_slug if your component is a 
  145. * "root component". Eg, in: 
  146. * http://testbp.com/example/test 
  147. * 'example' is a root slug. This should always be defined 
  148. * in terms of $bp->pages; see the example below. Site admins 
  149. * can customize this value by changing the permalink of the 
  150. * corresponding WP page in the Dashboard. NOTE: 
  151. * 'root_slug' requires that 'has_directory' is true. 
  152. * - 'has_directory' - Set this to true if your component requires a top-level 
  153. * directory, such as http://testbp.com/example. When 
  154. * 'has_directory' is true, BP will require that site admins 
  155. * associate a WordPress page with your component. NOTE: 
  156. * When 'has_directory' is true, you must also define your 
  157. * component's 'root_slug'; see previous item. Defaults to 
  158. * false. 
  159. * - 'notification_callback' - The name of the function that is used to format BP 
  160. * admin bar notifications for your component. 
  161. * - 'search_string' - If your component is a root component (has_directory),  
  162. * you can provide a custom string that will be used as the 
  163. * default text in the directory search box. 
  164. * - 'global_tables' - If your component creates custom database tables, store 
  165. * the names of the tables in a $global_tables array, so that 
  166. * they are available to other BP functions. 
  167. * You can also use this function to put data directly into the $bp global. 
  168. * @package BuddyPress_Skeleton_Component 
  169. * @since 1.6 
  170. * @global obj $bp BuddyPress's global object 
  171. */ 
  172. function setup_globals() { 
  173. global $bp; 
  174.  
  175. // Defining the slug in this way makes it possible for site admins to override it 
  176. if ( !defined( 'BP_GIFTS_SLUG' ) ) 
  177. define( 'BP_GIFTS_SLUG', $this->id ); 
  178.  
  179. // Global tables for the example component. Build your table names using 
  180. // $bp->table_prefix (instead of hardcoding 'wp_') to ensure that your component 
  181. // works with $wpdb, multisite, and custom table prefixes. 
  182. $global_tables = array( 
  183. 'table_name' => $bp->table_prefix . 'bp_gifts' 
  184. ); 
  185. $bp->gifts->id = 'gifts'; 
  186.  
  187. $bp->gifts->table_name = $wpdb->base_prefix . 'bp_gifts'; 
  188.  
  189. $bp->gifts->table_name_data = $wpdb->base_prefix . 'bp_gifts_data'; 
  190.  
  191. $bp->gifts->format_notification_function = 'bp_gifts_format_notifications'; 
  192.  
  193. $bp->gifts->slug = BP_GIFTS_SLUG; 
  194. // Set up the $globals array to be passed along to parent::setup_globals() 
  195. $globals = array( 
  196. 'slug' => BP_GIFTS_SLUG,  
  197. 'root_slug' => isset( $bp->pages->{$this->id}->slug ) ? $bp->pages->{$this->id}->slug : BP_GIFTS_SLUG,  
  198. 'has_directory' => true, // Set to false if not required 
  199. 'notification_callback' => 'bp_gifts_format_notifications',  
  200. 'search_string' => __( 'Search Examples...', 'buddypress' ),  
  201. 'global_tables' => $global_tables 
  202. ); 
  203.  
  204. // Let BP_Component::setup_globals() do its work. 
  205. parent::setup_globals( $globals ); 
  206.  
  207. // If your component requires any other data in the $bp global, put it there now. 
  208. //$bp->{$this->id}->misc_data = '123'; 
  209.  
  210. /** 
  211. * Set up your component's navigation. 
  212. * The navigation elements created here are responsible for the main site navigation (eg 
  213. * Profile > Activity > Mentions), as well as the navigation in the BuddyBar. WP Admin Bar 
  214. * navigation is broken out into a separate method; see 
  215. * BP_Gifts_Component::setup_admin_bar(). 
  216. * @global obj $bp 
  217. */ 
  218. /** 
  219. function setup_nav() { 
  220. // Add 'Gifts' to the main navigation 
  221. $main_nav = array( 
  222. 'name' => __( 'Gifts', 'bp-gifts' ),  
  223. 'slug' => bp_get_gifts_slug(),  
  224. 'position' => 80,  
  225. 'screen_function' => 'bp_gifts_screen_one',  
  226. 'default_subnav_slug' => 'screen-one' 
  227. ); 
  228.   
  229. $example_link = trailingslashit( bp_loggedin_user_domain() . bp_get_gifts_slug() ); 
  230.   
  231. // Add a few subnav items under the main Gifts tab 
  232. $sub_nav[] = array( 
  233. 'name' => __( 'Screen One', 'bp-gifts' ),  
  234. 'slug' => 'screen-one',  
  235. 'parent_url' => $gifts_link,  
  236. 'parent_slug' => bp_get_gifts_slug(),  
  237. 'screen_function' => 'bp_gifts_screen_one',  
  238. 'position' => 10 
  239. ); 
  240.   
  241. // Add the subnav items to the friends nav item 
  242. $sub_nav[] = array( 
  243. 'name' => __( 'Screen Two', 'bp-gifts' ),  
  244. 'slug' => 'screen-two',  
  245. 'parent_url' => $gifts_link,  
  246. 'parent_slug' => bp_get_gifts_slug(),  
  247. 'screen_function' => 'bp_gifts_screen_two',  
  248. 'position' => 20 
  249. ); 
  250.   
  251. parent::setup_nav( $main_nav, $sub_nav ); 
  252.   
  253. // If your component needs additional navigation menus that are not handled by 
  254. // BP_Component::setup_nav(), you can register them manually here. For example,  
  255. // if your component needs a subsection under a user's Settings menu, add 
  256. // it like this. See bp_example_screen_settings_menu() for more info 
  257. bp_core_new_subnav_item( array( 
  258. 'name' => __( 'Gifts', 'bp-gifts' ),  
  259. 'slug' => 'gifts-admin',  
  260. 'parent_slug' => bp_get_settings_slug(),  
  261. 'parent_url' => trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() ),  
  262. 'screen_function' => 'bp_gifts_screen_settings_menu',  
  263. 'position' => 40,  
  264. 'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile 
  265. ) ); 
  266. */ 
  267. /** 
  268. * If your component needs to store data, it is highly recommended that you use WordPress 
  269. * custom post types for that data, instead of creating custom database tables. 
  270. * In the future, BuddyPress will have its own bp_register_post_types hook. For the moment,  
  271. * hook to init. See BP_Gifts_Component::__construct(). 
  272. * @package BuddyPress_Skeleton_Component 
  273. * @since 1.6 
  274. * @see http://codex.wordpress.org/Function_Reference/register_post_type 
  275. */ 
  276. /** 
  277. function register_post_types() { 
  278. // Set up some labels for the post type 
  279. $labels = array( 
  280. 'name' => __( 'High Fives', 'bp-gifts' ),  
  281. 'singular' => __( 'High Five', 'bp-gifts' ) 
  282. ); 
  283.   
  284. // Set up the argument array for register_post_type() 
  285. $args = array( 
  286. 'label' => __( 'High Fives', 'bp-gifts' ),  
  287. 'labels' => $labels,  
  288. 'public' => false,  
  289. 'show_ui' => true,  
  290. 'supports' => array( 'title' ) 
  291. ); 
  292.   
  293. // Register the post type. 
  294. // Here we are using $this->id ('example') as the name of the post type. You may 
  295. // choose to use a different name for the post type; if you register more than one,  
  296. // you will have to declare more names. 
  297. register_post_type( $this->id, $args ); 
  298.   
  299. parent::register_post_types(); 
  300.   
  301. function register_taxonomies() { 
  302.   
  303. */