PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/abstraction/class.bp.abstraction.php

http://buddypress-media.googlecode.com/
PHP | 475 lines | 228 code | 135 blank | 112 comment | 42 complexity | f7e0ce0719409f663bb12447c5a223f7 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * BP-MEDIA BUDDYPRESS ABSTRACTION CLASS
  4. * Provides abstraction functions to interface with BuddyPress, simplifying upgrading BP-Media
  5. * when BuddyPress makes changes to their codebase.
  6. *
  7. * @version 0.1.9
  8. * @since 0.1.9
  9. * @package BP-Media
  10. * @subpackage BuddyPress Abstraction
  11. * @license GPL v2.0
  12. * @link http://code.google.com/p/buddypress-media/
  13. *
  14. * ========================================================================================================
  15. */
  16. class BPM_bp {
  17. /**
  18. * Returns the current BuddyPress version
  19. *
  20. * @version 0.1.9
  21. * @since 0.1.9
  22. * @return string $result | current BuddyPress version as string
  23. */
  24. public function getBPVersion(){
  25. return BP_VERSION;
  26. }
  27. /**
  28. * Returns the current logged-in user_id
  29. *
  30. * @version 0.1.9
  31. * @since 0.1.9
  32. * @return int $user_id | current logged-in user_id
  33. */
  34. public function getLoggedInUserID(){
  35. global $bp;
  36. return $bp->loggedin_user->id;
  37. }
  38. /**
  39. * Returns the current displayed user_id
  40. *
  41. * @version 0.1.9
  42. * @since 0.1.9
  43. * @return int $user_id | current displayed user_id
  44. */
  45. public function getDisplayedUserID(){
  46. global $bp;
  47. return $bp->displayed_user->id;
  48. }
  49. /**
  50. * Determines if the system is running BP 1.5 or greater
  51. *
  52. * @version 0.1.9
  53. * @since 0.1.9
  54. * @return bool $result | True if BP 1.5 or greater. False if not.
  55. */
  56. public function isOnePointFive(){
  57. if( BP_DB_VERSION > 3816 ){
  58. return true;
  59. }
  60. else {
  61. return false;
  62. }
  63. }
  64. /**
  65. * Checks if a WordPress page is owned by BP-Media, BuddyPress, or an external plugin
  66. *
  67. * @version 0.1.9
  68. * @since 0.1.9
  69. * @param int $post_id | post_id of the WordPress page to search
  70. * @return bool/array $result | False on failure. Data array on success.
  71. */
  72. public function getPageOwner($post_id) {
  73. global $wpdb, $bpm;
  74. $bp_components = array (
  75. "activate" => __('Activate',"bp-media"),
  76. "activity" => __('Activity',"bp-media"),
  77. "blogs" => __('Blogs',"bp-media"),
  78. "forums" => __('Forums',"bp-media"),
  79. "friends" => __('Friends',"bp-media"),
  80. "groups" => __('Groups',"bp-media"),
  81. "members" => __('Members',"bp-media"),
  82. "messages" => __('Messages',"bp-media"),
  83. "register" => __('Register',"bp-media"),
  84. "settings" => __('Settings',"bp-media"),
  85. "xprofile" => __('Profile',"bp-media")
  86. );
  87. // Make sure that we're working with the root blog, no matter which dashboard the admin screens are being run on
  88. if ( !empty( $wpdb->blogid ) && ( $wpdb->blogid != bp_get_root_blog_id() ) && ( !defined( 'BP_ENABLE_MULTIBLOG' ) ) ) {
  89. switch_to_blog( bp_get_root_blog_id() );
  90. }
  91. // Build the "active components" array
  92. // ==================================================================
  93. $bp_active_components = array(
  94. // The "register" and "activate" components are always active, but
  95. // are not present in BP's active components option
  96. "register"=>1,
  97. "activate"=>1
  98. );
  99. $bp_active_components_db = bp_get_option( 'bp-active-components' );
  100. if( is_array($bp_active_components_db) ){ // Handle empty option
  101. foreach( $bp_active_components_db as $key => $fake_var){
  102. $bp_active_components[$key] = true;
  103. }
  104. unset($key, $fake_var);
  105. }
  106. // Fetch BP's component pages list, and scrub any broken key=>val
  107. // pairs from the data array (as BuddyPress does)
  108. // ==================================================================
  109. $bp_pages = bp_get_option( 'bp-pages' );
  110. if( $bp_pages && is_array( $bp_pages ) ) {
  111. foreach( (array)$bp_pages as $component_name => $page_id ) {
  112. if( empty( $component_name ) || empty( $page_id ) ) {
  113. unset( $bp_pages[$component_name] );
  114. }
  115. }
  116. }
  117. // Determine if other plugins are adding, removing, or not affecting
  118. // the page_id that we're checking
  119. // ==================================================================
  120. // NOTE: A badly designed plugin could also inject itself into BP's router
  121. // using the 'bp_core_get_directory_pages' filter in bp-core-filters.php
  122. // line 168. However, by the time the data reaches this filter, the page_id's
  123. // have already been converted into slugs.
  124. $bp_pages_filtered = apply_filters( 'bp_core_get_directory_page_ids', $bp_pages );
  125. if( array_search($post_id, $bp_pages) !== false){
  126. $in_original = true;
  127. }
  128. else {
  129. $in_original = false;
  130. }
  131. if( array_search($post_id, $bp_pages_filtered) !== false){
  132. $in_filtered = true;
  133. }
  134. else {
  135. $in_filtered = false;
  136. }
  137. // CASE 1: BuddyPress owns the page
  138. // ==================================================================
  139. if( $in_original && $in_filtered ){
  140. $component_slug = array_search($post_id, $bp_pages);
  141. $result = array(
  142. "exists"=>true,
  143. "slug"=>$component_slug,
  144. "plugin_name"=>__('BuddyPress',"bp-media")
  145. );
  146. // Look-up the component's name based on its slug
  147. $component_name = $bp_components[$component_slug];
  148. if($component_name){
  149. $result["component_name"] = $component_name;
  150. $result["module_slug"] = null;
  151. $result["module_id"] = null;
  152. }
  153. else {
  154. $result["component_name"] = __('Unknown',"bp-media");
  155. $result["module_slug"] = null;
  156. $result["module_id"] = null;
  157. }
  158. // Check if the component is active
  159. if( array_key_exists($component_slug, $bp_active_components) ){
  160. $result["active"] = true;
  161. }
  162. else {
  163. $result["active"] = false;
  164. }
  165. return $result;
  166. }
  167. // CASE 2: BP-Media or an external plugin are adding a page
  168. // ==================================================================
  169. elseif( !$in_original && $in_filtered ) {
  170. $component_slug = array_search($post_id, $bp_pages_filtered);
  171. $result = array(
  172. "exists"=>true,
  173. "slug"=>$component_slug,
  174. );
  175. $bpm_page_data = $bpm->navigation->getPageOwner($post_id);
  176. // BP-Media owns the page
  177. // ====================================
  178. if($bpm_page_data){
  179. $result["plugin_name"] = __('BP-Media',"bp-media");
  180. $result["component_name"] = $bpm_page_data["module_name"];
  181. }
  182. // External plugin owns the page
  183. // ====================================
  184. else {
  185. $result["plugin_name"] = __('Other Plugin',"bp-media");
  186. $result["component_name"] = null;
  187. }
  188. // Check if the component is active
  189. if( array_key_exists($component_slug, $bp_active_components) ){
  190. $result["active"] = true;
  191. }
  192. else {
  193. $result["active"] = false;
  194. }
  195. return $result;
  196. }
  197. // CASE 3: An external plugin deactivating a BP component
  198. // ==================================================================
  199. elseif( $in_original && !$in_filtered ) {
  200. $component_slug = array_search($post_id, $bp_pages_filtered);
  201. $result = array(
  202. "exists"=>true,
  203. "active"=>false,
  204. "slug"=>$component_slug,
  205. "plugin_name"=>__('BuddyPress',"bp-media")
  206. );
  207. $component_name = $bp_components[$component_slug];
  208. if($component_name){
  209. $result["component_name"] = $component_name;
  210. }
  211. else {
  212. $result["component_name"] = __('Unknown',"bp-media");
  213. }
  214. }
  215. // CASE 4: The page is not being claimed
  216. // ==================================================================
  217. else {
  218. $result = array(
  219. "exists"=>false,
  220. "active"=>null,
  221. "slug"=>null,
  222. "plugin_name"=>null,
  223. "component_name"=>null,
  224. );
  225. return $result;
  226. }
  227. }
  228. /**
  229. * Checks if a slug is owned by BP-Media, BuddyPress, or an external plugin
  230. *
  231. * @version 0.1.9
  232. * @since 0.1.9
  233. * @param string $location | location of slug "profile" or "tab"
  234. * @return bool/array $result | False on failure. Data array on success.
  235. */
  236. public function getSlugOwner($location, $slug) {
  237. global $wpdb, $bp, $bpm;
  238. $location = strtolower($location);
  239. $slug = strtolower($slug);
  240. // BuddyPress lists all their slug constants in /bp-members/bp-members-signup.php. We
  241. // have to build the array using BP's slug constants in case the site uses a define()
  242. // to override the component's default slug name. (Typically to translate it). Slugs
  243. // are converted to lowercase because web servers ignore capitalization in URL's
  244. $bp_components = array (
  245. strtolower(BP_ACTIVATION_SLUG) => __('Activate',"bp-media"),
  246. strtolower(BP_ACTIVITY_SLUG) => __('Activity',"bp-media"),
  247. strtolower(BP_BLOGS_SLUG) => __('Blogs',"bp-media"),
  248. strtolower(BP_FORUMS_SLUG) => __('Forums',"bp-media"),
  249. strtolower(BP_FRIENDS_SLUG) => __('Friends',"bp-media"),
  250. strtolower(BP_GROUPS_SLUG) => __('Groups',"bp-media"),
  251. strtolower(BP_MEMBERS_SLUG) => __('Members',"bp-media"),
  252. strtolower(BP_MESSAGES_SLUG) => __('Messages',"bp-media"),
  253. strtolower(BP_REGISTER_SLUG) => __('Register',"bp-media"),
  254. strtolower(BP_SEARCH_SLUG) => __('Search',"bp-media"),
  255. strtolower(BP_SETTINGS_SLUG) => __('Settings',"bp-media"),
  256. strtolower(BP_XPROFILE_SLUG) => __('Profile',"bp-media"),
  257. );
  258. $result = array();
  259. // CASE 1: Top-level BuddyPress menu
  260. // ==============================================
  261. if( $location == "profile"){
  262. // Scan the bp_nav array for the slug
  263. // ========================================
  264. $exists_in_bp_nav = false;
  265. foreach( $bp->bp_nav as $key => $data ){
  266. if( $data["slug"] == $slug ){
  267. $exists_in_bp_nav = true;
  268. $matching_slug = $slug;
  269. $component_name = $data["name"];
  270. break;
  271. }
  272. }
  273. unset($key, $data);
  274. // CASE 1A: Exists in the nav array
  275. // ===================================================
  276. if($exists_in_bp_nav){
  277. $result["exists"] = true;
  278. $result["slug"] = $matching_slug;
  279. // BuddyPress owns the slug
  280. // ====================================
  281. if( array_key_exists($slug, $bp_components) ){
  282. $result["plugin_name"] = __('BuddyPress',"bp-media");
  283. $result["component_name"] = $bp_components[$slug];
  284. $result["module_slug"] = null;
  285. $result["module_id"] = null;
  286. }
  287. // External plugin owns the slug
  288. // ====================================
  289. else {
  290. $result["plugin_name"] = __('Other Plugin',"bp-media");
  291. $result["component_name"] = $component_name;
  292. $result["module_slug"] = null;
  293. $result["module_id"] = null;
  294. }
  295. }
  296. // CASE 1B: Does not exist in the nav array
  297. // ===================================================
  298. else {
  299. $bpm_slug_data = $bpm->navigation->getSlugOwner($location, $slug, $slug_error);
  300. // BP-Media owns the slug
  301. // ====================================
  302. if($bpm_slug_data){
  303. $result["plugin_name"] = __('BP-Media',"bp-media");
  304. $result["component_name"] = $bpm_slug_data["module_name"];
  305. $result["module_slug"] = $bpm_slug_data["module_slug"];
  306. $result["module_id"] = $bpm_slug_data["module_id"];
  307. }
  308. // Slug is available for use
  309. // ====================================
  310. else {
  311. $result = array(
  312. "exists"=>false,
  313. "active"=>null,
  314. "slug"=>null,
  315. "module_id"=>null,
  316. "module_slug"=>null,
  317. "plugin_name"=>null,
  318. "component_name"=>null,
  319. );
  320. }
  321. }
  322. return $result;
  323. }
  324. // CASE 2: Checking on BP-Media tab
  325. // ==============================================
  326. else {
  327. $bpm_slug_data = $bpm->navigation->getSlugOwner($location, $slug);
  328. // BP-Media owns the slug
  329. // ====================================
  330. if($bpm_slug_data){
  331. $result["exists"] = true;
  332. $result["slug"] = $slug;
  333. $result["plugin_name"] = __('BP-Media',"bp-media");
  334. $result["component_name"] = $bpm_slug_data["module_name"];
  335. }
  336. return $result;
  337. }
  338. }
  339. } // End of class BPM_bp
  340. ?>