BP_Profile_Tabs

The BuddyPress Profile Tabs BP Profile Tabs class.

Defined (1)

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

/public/class-bp-profile-tabs.php  
  1. class BP_Profile_Tabs { 
  2.  
  3. /** 
  4. * Plugin version, used for cache-busting of style and script file references. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const VERSION = '1.5.0'; 
  9.  
  10. /** 
  11. * @since 1.5.0 
  12. * @var string 
  13. */ 
  14. protected $plugin_slug = 'bp-profile-tabs'; 
  15.  
  16. /** 
  17. * @since 1.5.0 
  18. * @var string 
  19. */ 
  20. protected $plugin_name = 'BuddyPress Profile Tabs'; 
  21.  
  22. /** 
  23. * Instance of this class. 
  24. * @since 1.5.0 
  25. * @var object 
  26. */ 
  27. protected static $instance = null; 
  28.  
  29. /** 
  30. * Initialize the plugin by setting localization and loading public scripts 
  31. * and styles. 
  32. * @since 1.5.0 
  33. */ 
  34. private function __construct() { 
  35. // Load plugin text domain. 
  36. add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); 
  37.  
  38. // Activate plugin when new blog is added. 
  39. add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); 
  40.  
  41. add_action( 'bp_before_profile_loop_content', array( $this, 'bpt_load' ) ); 
  42.  
  43. add_action( 'bp_before_profile_loop_content', array( $this, 'bp_profile_tabs_top' ) ); 
  44. add_action( 'bp_before_profile_field_content', array( $this, 'bp_profile_tabs_start_div' ) ); 
  45.  
  46. add_action( 'bp_after_profile_field_content', array( $this, 'bp_profile_tabs_end_div' ) ); 
  47. add_action( 'bp_after_profile_loop_content', array( $this, 'bp_profile_tabs_end_div' ) ); 
  48.  
  49. add_action( 'wp_enqueue_scripts', array( $this, 'register_bpt_script' ) ); 
  50.  
  51. add_action( 'init', array( $this, 'get_xprofile_groups' ) ); 
  52.  
  53. $this->tabs_to_filter = ''; 
  54.  
  55. /** 
  56. * Return the plugin slug. 
  57. * @since 1.5.0 
  58. * @return Plugin slug variable. 
  59. */ 
  60. public function get_plugin_slug() { 
  61. return $this->plugin_slug; 
  62.  
  63. /** 
  64. * Return the plugin name. 
  65. * @since 1.5.0 
  66. * @return Plugin name variable. 
  67. */ 
  68. public function get_plugin_name() { 
  69. return $this->plugin_name; 
  70.  
  71. /** 
  72. * Return the version 
  73. * @since 1.5.0 
  74. * @return Version const. 
  75. */ 
  76. public function get_plugin_version() { 
  77. return self::VERSION; 
  78.  
  79.  
  80. /** 
  81. * Return an instance of this class. 
  82. * @since 1.0.0 
  83. * @return object A single instance of this class. 
  84. */ 
  85. public static function get_instance() { 
  86.  
  87. // If the single instance hasn't been set, set it now. 
  88. if ( null === self::$instance ) { 
  89. self::$instance = new self; 
  90.  
  91. return self::$instance; 
  92.  
  93. /** 
  94. * Fired when the plugin is activated. 
  95. * @since 1.5.0 
  96. * @param boolean $network_wide True if WPMU superadmin uses 
  97. * "Network Activate" action, false if 
  98. * WPMU is disabled or plugin is 
  99. * activated on an individual blog. 
  100. */ 
  101. public static function activate( $network_wide ) { 
  102.  
  103. if ( function_exists( 'is_multisite' ) && is_multisite() ) { 
  104.  
  105. if ( $network_wide ) { 
  106.  
  107. // Get all blog ids. 
  108. $blog_ids = self::get_blog_ids(); 
  109.  
  110. foreach ( $blog_ids as $blog_id ) { 
  111.  
  112. switch_to_blog( $blog_id ); 
  113. self::single_activate(); 
  114.  
  115. restore_current_blog(); 
  116. } else { 
  117. self::single_activate(); 
  118. } else { 
  119. self::single_activate(); 
  120.  
  121. /** 
  122. * Fired when the plugin is deactivated. 
  123. * @since 1.5.0 
  124. * @param boolean $network_wide True if WPMU superadmin uses 
  125. * "Network Deactivate" action, false if 
  126. * WPMU is disabled or plugin is 
  127. * deactivated on an individual blog. 
  128. */ 
  129. public static function deactivate( $network_wide ) { 
  130.  
  131. if ( function_exists( 'is_multisite' ) && is_multisite() ) { 
  132.  
  133. if ( $network_wide ) { 
  134.  
  135. // Get all blog ids. 
  136. $blog_ids = self::get_blog_ids(); 
  137.  
  138. foreach ( $blog_ids as $blog_id ) { 
  139.  
  140. switch_to_blog( $blog_id ); 
  141. self::single_deactivate(); 
  142.  
  143. restore_current_blog(); 
  144. } else { 
  145. self::single_deactivate(); 
  146. } else { 
  147. self::single_deactivate(); 
  148.  
  149. /** 
  150. * Fired when a new site is activated with a WPMU environment. 
  151. * @since 1.5.0 
  152. * @param int $blog_id ID of the new blog. 
  153. */ 
  154. public function activate_new_site( $blog_id ) { 
  155.  
  156. if ( 1 !== did_action( 'wpmu_new_blog' ) ) { 
  157. return; 
  158.  
  159. switch_to_blog( $blog_id ); 
  160. self::single_activate(); 
  161. restore_current_blog(); 
  162.  
  163. /** 
  164. * Get all blog ids of blogs in the current network that are: 
  165. * - not archived 
  166. * - not spam 
  167. * - not deleted 
  168. * @since 1.5.0 
  169. * @return array|false The blog ids, false if no matches. 
  170. */ 
  171. private static function get_blog_ids() { 
  172.  
  173. global $wpdb; 
  174.  
  175. // Get an array of blog ids. 
  176. $sql = "SELECT blog_id FROM $wpdb->blogs 
  177. WHERE archived = '0' AND spam = '0' 
  178. AND deleted = '0'"; 
  179.  
  180. return $wpdb->get_col( $sql ); 
  181.  
  182. /** 
  183. * Fired for each blog when the plugin is activated. 
  184. * @since 1.5.0 
  185. * @todo: Define activation functionality at the beginning of this function 
  186. */ 
  187. private static function single_activate() { 
  188.  
  189. $bpt_options = get_option( 'bp_profile_tabs_option' ); // Option name. 
  190.  
  191. // If no option settings set some defaults or import from old settings. 
  192. if ( empty( $bpt_options ) ) { 
  193.  
  194. // Check for old option settings. 
  195. $old_theme_option = get_option( 'bp_profile_tabs_theme_option' ); 
  196. if ( ! empty( $old_theme_option ) ) { 
  197. $bpt_options['bpt_theme'] = $old_theme_option; 
  198. $bpt_options['bpt_cdn'] = 'google'; 
  199. update_option( 'bp_profile_tabs_option', $bpt_options ); 
  200. delete_option( 'bp_profile_tabs_theme_option' ); 
  201.  
  202. // Set default options on new option name if there is no old options. 
  203. if ( empty( $old_theme_option ) ) { 
  204. $bpt_options['bpt_theme'] = 'cupertino'; 
  205. $bpt_options['bpt_cdn'] = 'google'; 
  206. add_option( 'bp_profile_tabs_option', $bpt_options ); 
  207.  
  208. /** 
  209. * Fired for each blog when the plugin is deactivated. 
  210. * @since 1.5.0 
  211. */ 
  212. private static function single_deactivate() { 
  213.  
  214.  
  215. /** 
  216. * Load the plugin text domain for translation. 
  217. * @since 1.5.0 
  218. */ 
  219. public function load_plugin_textdomain() { 
  220.  
  221. $domain = $this->plugin_slug; 
  222. $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); 
  223.  
  224. load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ); 
  225. load_plugin_textdomain( $domain, false, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' ); 
  226.  
  227.  
  228. /** 
  229. * Create the tabs div and un-ordered list for the tab titles from the database. 
  230. * @since 1.5.0 
  231. */ 
  232. public function bp_profile_tabs_top() { 
  233. echo '<div id="tabs"><ul>'; 
  234. foreach ( $this->groups_list as $group ) { 
  235. echo '<li><a href="#tabs-' . esc_attr( $group->id ) . '">'; 
  236. echo esc_attr( $group->name ); 
  237. echo '</a></li>'; 
  238. echo '</ul>'; 
  239.  
  240. /** 
  241. * Creates the start of each tabs div to hold the tab content. 
  242. * @since 1.5.0 
  243. */ 
  244. public function bp_profile_tabs_start_div() { 
  245. if ( bp_is_user_profile() && ! bp_is_user_profile_edit() ) { 
  246. $profile_group_id = bp_get_the_profile_group_id(); 
  247. echo '<div id="tabs-' . esc_attr( $profile_group_id ) . '">'; 
  248.  
  249. /** 
  250. * Ends the profile tabs div created to hold the tab content. 
  251. * @since 1.5.0 
  252. */ 
  253. public function bp_profile_tabs_end_div() { 
  254. if ( bp_is_user_profile() && ! bp_is_user_profile_edit() ) { 
  255. echo '</div>'; 
  256.  
  257. /** 
  258. * Loads the tabs javascript and jQuery UI style. 
  259. * @since 1.5.0 
  260. */ 
  261. public function bpt_load() { 
  262. wp_enqueue_style( $this->plugin_slug . '-jquery-ui-style' ); 
  263. wp_enqueue_script( $this->plugin_slug . '-script' ); 
  264.  
  265. /** 
  266. * Registers the tabs javascript and jQuery UI style. 
  267. * @since 1.5.0 
  268. */ 
  269. public function register_bpt_script() { 
  270. wp_register_script( 
  271. $this->plugin_slug . '-script', plugins_url( 'assets/js/public.js', __FILE__ ),  
  272. array( 
  273. 'jquery',  
  274. 'jquery-ui-tabs',  
  275. ),  
  276. self::VERSION 
  277. ); 
  278.  
  279. $bpt_options = get_option( 'bp_profile_tabs_option' ); 
  280. $protocol = is_ssl() ? 'https' : 'http'; 
  281. $jquery_ui_version = $this->get_jquery_ui_version(); 
  282. if ( 'google' === $bpt_options['bpt_cdn'] ) { 
  283. $jquery_ui_css_url = $protocol . '://ajax.googleapis.com/ajax/libs/jqueryui/' . $jquery_ui_version . '/themes/' . $bpt_options['bpt_theme'] . '/jquery-ui.css'; 
  284. if ( 'microsoft' === $bpt_options['bpt_cdn'] ) { 
  285. $jquery_ui_css_url = $protocol . '://ajax.aspnetcdn.com/ajax/jquery.ui/' . $jquery_ui_version . '/themes/' . $bpt_options['bpt_theme'] . '/jquery-ui.css'; 
  286. if ( 'jquery' === $bpt_options['bpt_cdn'] ) { 
  287. $jquery_ui_css_url = $protocol . '://code.jquery.com/ui/' . $jquery_ui_version . '/themes/' . $bpt_options['bpt_theme'] . '/jquery-ui.css'; 
  288. if ( ! empty( $bpt_options['bpt_custom'] ) ) { 
  289. $jquery_ui_css_url = $bpt_options['bpt_custom']; 
  290. wp_register_style( $this->plugin_slug . '-jquery-ui-style', $jquery_ui_css_url, array(), self::VERSION ); 
  291.  
  292. /** 
  293. * Get the current jQuery version of this WP install 
  294. * @return string 
  295. */ 
  296. public function get_jquery_ui_version() { 
  297.  
  298. global $wp_scripts; 
  299.  
  300. if ( ! $wp_scripts instanceof WP_Scripts ) { 
  301. $wp_scripts = new WP_Scripts(); 
  302.  
  303. $jquery_ui_core = $wp_scripts->query( 'jquery-ui-core' ); 
  304.  
  305. if ( ! $jquery_ui_core instanceof _WP_Dependency ) { 
  306. return $this->ui_version; 
  307.  
  308. if ( ! isset( $jquery_ui_core->ver ) ) { 
  309. return $this->ui_version; 
  310.  
  311. return $jquery_ui_core->ver; 
  312.  
  313. /** 
  314. * Get the xprofile groups from the database 
  315. */ 
  316. public function get_xprofile_groups() { 
  317. if ( class_exists( 'buddypress' ) ) { 
  318. $xprofile_groups = wp_cache_get( 'bpt_xprofile_groups' ); 
  319. if ( false === $xprofile_groups ) { 
  320. global $wpdb; 
  321. $profile_field_groups_query = 'SELECT id, name FROM `' . $wpdb->prefix . 'bp_xprofile_groups` order by group_order'; 
  322. $xprofile_groups = $wpdb->get_results( $profile_field_groups_query ); 
  323. wp_cache_set( 'bpt_xprofile_groups', $xprofile_groups ); 
  324. $this->groups_list = $xprofile_groups; 
  325. $current_user_id = get_current_user_id(); 
  326. if ( isset( $current_user_id ) && $current_user_id > 0 ) { 
  327. $member_type = bp_get_member_type( $current_user_id ); 
  328. if ( ! empty( $member_type ) ) { 
  329. $bpt_options = get_option( 'bp_profile_tabs_option' ); 
  330. $groups_list = $bpt_options[ 'bpt_member_types_' . $member_type ]; 
  331. foreach ( $this->groups_list as $tab_group_index => $tab_group ) { 
  332. if ( ! in_array( $tab_group->id, $groups_list ) ) { 
  333. unset( $this->groups_list[ $tab_group_index ] ); 
  334. $this->tabs_to_filter .= $tab_group->id . ', '; 
  335. // Reference: bp_has_profile -- bp_xprofile-template.php . 
  336. // Reference: bp_parse_args -- bp-core-functions.php . 
  337. add_filter( 'bp_before_has_profile_parse_args', array( $this, 'filter_tabs' ), 10, 1 ); 
  338.  
  339. /** 
  340. * Filter the tabs to show on the member page 
  341. * @param array $args Tabs arguments. 
  342. * @return mixed 
  343. */ 
  344. function filter_tabs( $args ) { 
  345. if ( isset( $args['exclude_groups'] ) ) { 
  346. $args['exclude_groups'] .= $this->tabs_to_filter; 
  347. } else { 
  348. $args['exclude_groups'] = $this->tabs_to_filter; 
  349.  
  350. return $args;