/bp-blogs/classes/class-bp-blogs-component.php

  1. <?php 
  2. /** 
  3. * BuddyPress Blogs Loader 
  4. * 
  5. * The blogs component tracks posts and comments to member activity streams,  
  6. * shows blogs the member can post to in their profiles, and caches useful 
  7. * information from those blogs to make querying blogs in bulk more performant. 
  8. * 
  9. * @package BuddyPress 
  10. * @subpackage BlogsCore 
  11. * @since 1.5.0 
  12. */ 
  13.  
  14. // Exit if accessed directly. 
  15. defined( 'ABSPATH' ) || exit; 
  16.  
  17. /** 
  18. * Creates our Blogs component. 
  19. */ 
  20. class BP_Blogs_Component extends BP_Component { 
  21.  
  22. /** 
  23. * Start the blogs component creation process. 
  24. * 
  25. * @since 1.5.0 
  26. */ 
  27. public function __construct() { 
  28. parent::start( 
  29. 'blogs',  
  30. __( 'Site Directory', 'buddypress' ),  
  31. buddypress()->plugin_dir,  
  32. array( 
  33. 'adminbar_myaccount_order' => 30,  
  34. 'search_query_arg' => 'sites_search',  
  35. 'features' => array( 'site-icon' ) 
  36. ); 
  37.  
  38. /** 
  39. * Set up global settings for the blogs component. 
  40. * 
  41. * The BP_BLOGS_SLUG constant is deprecated, and only used here for 
  42. * backwards compatibility. 
  43. * 
  44. * @since 1.5.0 
  45. * 
  46. * @see BP_Component::setup_globals() for description of parameters. 
  47. * 
  48. * @param array $args See {@link BP_Component::setup_globals()}. 
  49. */ 
  50. public function setup_globals( $args = array() ) { 
  51. $bp = buddypress(); 
  52.  
  53. if ( ! defined( 'BP_BLOGS_SLUG' ) ) { 
  54. define ( 'BP_BLOGS_SLUG', $this->id ); 
  55.  
  56. // Global tables for messaging component. 
  57. $global_tables = array( 
  58. 'table_name' => $bp->table_prefix . 'bp_user_blogs',  
  59. 'table_name_blogmeta' => $bp->table_prefix . 'bp_user_blogs_blogmeta',  
  60. ); 
  61.  
  62. $meta_tables = array( 
  63. 'blog' => $bp->table_prefix . 'bp_user_blogs_blogmeta',  
  64. ); 
  65.  
  66. // Fetch the default directory title. 
  67. $default_directory_titles = bp_core_get_directory_page_default_titles(); 
  68. $default_directory_title = $default_directory_titles[$this->id]; 
  69.  
  70. // All globals for blogs component. 
  71. $args = array( 
  72. 'slug' => BP_BLOGS_SLUG,  
  73. 'root_slug' => isset( $bp->pages->blogs->slug ) ? $bp->pages->blogs->slug : BP_BLOGS_SLUG,  
  74. 'has_directory' => is_multisite(), // Non-multisite installs don't need a top-level Sites directory, since there's only one site. 
  75. 'directory_title' => isset( $bp->pages->blogs->title ) ? $bp->pages->blogs->title : $default_directory_title,  
  76. 'notification_callback' => 'bp_blogs_format_notifications',  
  77. 'search_string' => __( 'Search sites...', 'buddypress' ),  
  78. 'autocomplete_all' => defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' ),  
  79. 'global_tables' => $global_tables,  
  80. 'meta_tables' => $meta_tables,  
  81. ); 
  82.  
  83. // Setup the globals. 
  84. parent::setup_globals( $args ); 
  85.  
  86. /** 
  87. * Filters if a blog is public. 
  88. * 
  89. * In case the config is not multisite, the blog_public option is ignored. 
  90. * 
  91. * @since 2.3.0 
  92. * 
  93. * @param int $value Whether or not the blog is public. 
  94. */ 
  95. if ( 0 !== apply_filters( 'bp_is_blog_public', (int) get_option( 'blog_public' ) ) || ! is_multisite() ) { 
  96.  
  97. /** 
  98. * Filters the post types to track for the Blogs component. 
  99. * 
  100. * @since 1.5.0 
  101. * @deprecated 2.3.0 
  102. * 
  103. * @param array $value Array of post types to track. 
  104. */ 
  105. $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ); 
  106.  
  107. foreach ( $post_types as $post_type ) { 
  108. add_post_type_support( $post_type, 'buddypress-activity' ); 
  109.  
  110. /** 
  111. * Include bp-blogs files. 
  112. * 
  113. * @see BP_Component::includes() for description of parameters. 
  114. * 
  115. * @param array $includes See {@link BP_Component::includes()}. 
  116. */ 
  117. public function includes( $includes = array() ) { 
  118.  
  119. // Files to include. 
  120. $includes = array( 
  121. 'cache',  
  122. 'actions',  
  123. 'screens',  
  124. 'template',  
  125. 'filters',  
  126. 'functions',  
  127. ); 
  128.  
  129. if ( bp_is_active( 'activity' ) ) { 
  130. $includes[] = 'activity'; 
  131.  
  132. if ( is_multisite() ) { 
  133. $includes[] = 'widgets'; 
  134.  
  135. // Include the files. 
  136. parent::includes( $includes ); 
  137.  
  138. /** 
  139. * Set up component navigation for bp-blogs. 
  140. * 
  141. * @see BP_Component::setup_nav() for a description of arguments. 
  142. * 
  143. * @param array $main_nav Optional. See BP_Component::setup_nav() for 
  144. * description. 
  145. * @param array $sub_nav Optional. See BP_Component::setup_nav() for 
  146. * description. 
  147. */ 
  148. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  149.  
  150. /** 
  151. * Blog/post/comment menus should not appear on single WordPress setups. 
  152. * Although comments and posts made by users will still show on their 
  153. * activity stream. 
  154. */ 
  155. if ( ! is_multisite() ) { 
  156. return false; 
  157.  
  158. // Determine user to use. 
  159. if ( bp_displayed_user_domain() ) { 
  160. $user_domain = bp_displayed_user_domain(); 
  161. } elseif ( bp_loggedin_user_domain() ) { 
  162. $user_domain = bp_loggedin_user_domain(); 
  163. } else { 
  164. return; 
  165.  
  166. $slug = bp_get_blogs_slug(); 
  167. $parent_url = trailingslashit( $user_domain . $slug ); 
  168.  
  169. // Add 'Sites' to the main navigation. 
  170. $count = (int) bp_get_total_blog_count_for_user(); 
  171. $class = ( 0 === $count ) ? 'no-count' : 'count'; 
  172. $nav_text = sprintf( 
  173. /** translators: %s: Site count for the current user */ 
  174. __( 'Sites %s', 'buddypress' ),  
  175. sprintf( 
  176. '<span class="%s">%s</span>',  
  177. esc_attr( $class ),  
  178. bp_core_number_format( $count ) 
  179. ); 
  180. $main_nav = array( 
  181. 'name' => $nav_text,  
  182. 'slug' => $slug,  
  183. 'position' => 30,  
  184. 'screen_function' => 'bp_blogs_screen_my_blogs',  
  185. 'default_subnav_slug' => 'my-sites',  
  186. 'item_css_id' => $this->id 
  187. ); 
  188.  
  189. $sub_nav[] = array( 
  190. 'name' => __( 'My Sites', 'buddypress' ),  
  191. 'slug' => 'my-sites',  
  192. 'parent_url' => $parent_url,  
  193. 'parent_slug' => $slug,  
  194. 'screen_function' => 'bp_blogs_screen_my_blogs',  
  195. 'position' => 10 
  196. ); 
  197.  
  198. // Setup navigation. 
  199. parent::setup_nav( $main_nav, $sub_nav ); 
  200.  
  201. /** 
  202. * Set up bp-blogs integration with the WordPress admin bar. 
  203. * 
  204. * @since 1.5.0 
  205. * 
  206. * @see BP_Component::setup_admin_bar() for a description of arguments. 
  207. * 
  208. * @param array $wp_admin_nav See BP_Component::setup_admin_bar() 
  209. * for description. 
  210. * @return bool 
  211. */ 
  212. public function setup_admin_bar( $wp_admin_nav = array() ) { 
  213.  
  214. /** 
  215. * Site/post/comment menus should not appear on single WordPress setups. 
  216. * 
  217. * Comments and posts made by users will still show in their activity. 
  218. */ 
  219. if ( ! is_multisite() ) { 
  220. return false; 
  221.  
  222. // Menus for logged in user. 
  223. if ( is_user_logged_in() ) { 
  224.  
  225. // Setup the logged in user variables. 
  226. $blogs_link = trailingslashit( bp_loggedin_user_domain() . bp_get_blogs_slug() ); 
  227.  
  228. // Add the "Sites" sub menu. 
  229. $wp_admin_nav[] = array( 
  230. 'parent' => buddypress()->my_account_menu_id,  
  231. 'id' => 'my-account-' . $this->id,  
  232. 'title' => __( 'Sites', 'buddypress' ),  
  233. 'href' => $blogs_link 
  234. ); 
  235.  
  236. // My Sites. 
  237. $wp_admin_nav[] = array( 
  238. 'parent' => 'my-account-' . $this->id,  
  239. 'id' => 'my-account-' . $this->id . '-my-sites',  
  240. 'title' => __( 'My Sites', 'buddypress' ),  
  241. 'href' => $blogs_link,  
  242. 'position' => 10 
  243. ); 
  244.  
  245. // Create a Site. 
  246. if ( bp_blog_signup_enabled() ) { 
  247. $wp_admin_nav[] = array( 
  248. 'parent' => 'my-account-' . $this->id,  
  249. 'id' => 'my-account-' . $this->id . '-create',  
  250. 'title' => __( 'Create a Site', 'buddypress' ),  
  251. 'href' => trailingslashit( bp_get_blogs_directory_permalink() . 'create' ),  
  252. 'position' => 99 
  253. ); 
  254.  
  255. parent::setup_admin_bar( $wp_admin_nav ); 
  256.  
  257. /** 
  258. * Set up the title for pages and <title>. 
  259. */ 
  260. public function setup_title() { 
  261.  
  262. // Set up the component options navigation for Site. 
  263. if ( bp_is_blogs_component() ) { 
  264. $bp = buddypress(); 
  265.  
  266. if ( bp_is_my_profile() ) { 
  267. if ( bp_is_active( 'xprofile' ) ) { 
  268. $bp->bp_options_title = __( 'My Sites', 'buddypress' ); 
  269.  
  270. // If we are not viewing the logged in user, set up the current 
  271. // users avatar and name. 
  272. } else { 
  273. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  274. 'item_id' => bp_displayed_user_id(),  
  275. 'type' => 'thumb',  
  276. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() ) 
  277. ) ); 
  278. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  279.  
  280. parent::setup_title(); 
  281.  
  282. /** 
  283. * Setup cache groups 
  284. * 
  285. * @since 2.2.0 
  286. */ 
  287. public function setup_cache_groups() { 
  288.  
  289. // Global groups. 
  290. wp_cache_add_global_groups( array( 
  291. 'blog_meta' 
  292. ) ); 
  293.  
  294. parent::setup_cache_groups(); 
.