MS_Addon_BuddyPress

The Membership 2 MS Addon BuddyPress class.

Defined (1)

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

/app/addon/buddypress/class-ms-addon-buddypress.php  
  1. class MS_Addon_BuddyPress extends MS_Addon { 
  2.  
  3. /** 
  4. * The Add-on ID 
  5. * @since 1.0.0 
  6. */ 
  7. const ID = 'buddypress'; 
  8.  
  9. /** 
  10. * The flag to determine if we want to use the BuddyPress registration page 
  11. * or default M2 registration page. 
  12. * @since 1.0.1.0 
  13. * @var bool 
  14. */ 
  15. protected $buddypress_registration = true; 
  16.  
  17. /** 
  18. * Checks if the current Add-on is enabled 
  19. * @since 1.0.0 
  20. * @return bool 
  21. */ 
  22. static public function is_active() { 
  23. if ( ! self::buddypress_active() 
  24. && MS_Model_Addon::is_enabled( self::ID ) 
  25. ) { 
  26. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  27. $model->disable( self::ID ); 
  28.  
  29. return MS_Model_Addon::is_enabled( self::ID ); 
  30.  
  31. /** 
  32. * Returns the Add-on ID (self::ID). 
  33. * @since 1.0.1.0 
  34. * @return string 
  35. */ 
  36. public function get_id() { 
  37. return self::ID; 
  38.  
  39. /** 
  40. * Initializes the Add-on. Always executed. 
  41. * @since 1.0.0 
  42. */ 
  43. public function init() { 
  44.  
  45. if ( self::buddypress_active() ) { 
  46. $this->collission_check(); 
  47.  
  48. if ( self::is_active() ) { 
  49. $this->buddypress_registration = lib3()->is_true( 
  50. $this->get_setting( 'buddypress_registration' ) 
  51. ); 
  52.  
  53. $this->add_filter( 
  54. 'ms_controller_protection_tabs',  
  55. 'rule_tabs' 
  56. ); 
  57.  
  58. MS_Factory::load( 'MS_Addon_BuddyPress_Rule' ); 
  59.  
  60. /** 
  61. * Using the BuddyPress registration form is optional. 
  62. * These actions are only needed when the BuddyPress registration 
  63. * form is used instead of the M2 registration form. 
  64. */ 
  65. if ( $this->buddypress_registration ) { 
  66. $this->add_filter( 
  67. 'ms_frontend_custom_registration_form',  
  68. 'registration_form' 
  69. ); 
  70.  
  71. $this->add_action( 
  72. 'ms_controller_frontend_register_user_before',  
  73. 'prepare_create_user' 
  74. ); 
  75.  
  76. $this->add_action( 
  77. 'ms_controller_frontend_register_user_complete',  
  78. 'save_custom_fields' 
  79. ); 
  80.  
  81. // Disable BuddyPress Email activation. 
  82. add_filter( 
  83. 'bp_core_signup_send_activation_key',  
  84. '__return_false' 
  85. ); 
  86.  
  87. add_filter( 
  88. 'bp_registration_needs_activation',  
  89. '__return_false' 
  90. ); 
  91.  
  92. $this->add_action( 
  93. 'bp_core_signup_user',  
  94. 'disable_validation' 
  95. ); 
  96. } else { 
  97. $this->buddypress_registration = false; 
  98.  
  99. /** 
  100. * Checks, if some BuddyPress pages overlap with M2 membership pages. 
  101. * In some cases people used the same page-ID for both BuddyPress 
  102. * registration and M2 registration. This will cause problems and must be 
  103. * resolved to have M2 and BuddyPress work symbiotically. 
  104. * @since 1.0.1.1 
  105. */ 
  106. protected function collission_check() { 
  107. $buddy_pages = MS_Factory::get_option( 'bp-pages' ); 
  108.  
  109. if ( ! is_array( $buddy_pages ) ) { 
  110. // Okay, no BuddyPress pages set up yet. 
  111. return; 
  112.  
  113. $duplicates = array(); 
  114. foreach ( $buddy_pages as $type => $page_id ) { 
  115. $collission = MS_Model_Pages::get_page_by( 'id', $page_id ); 
  116. if ( $collission ) { 
  117. $title = $collission->post_title; 
  118. if ( ! $title ) { 
  119. $title = $collission->post_name; 
  120.  
  121. $duplicates[] = sprintf( '%s - %s', $page_id, $title ); 
  122.  
  123. if ( count( $duplicates ) ) { 
  124. $msg = sprintf( 
  125. '%s<br><br>%s',  
  126. sprintf( 
  127. __( 'BuddyPress uses a page that is also used as a Membership page by Membership 2.<br>Please assign a different page for either %sMembership 2%s or %sBuddyPress%s to avoid conflicts.', 'membership2' ),  
  128. '<a href="' . MS_Controller_Plugin::get_admin_url( 'settings' ) . '">',  
  129. '</a>',  
  130. '<a href="' . admin_url( 'admin.php?page=bp-page-settings' ) . '">',  
  131. '</a>' 
  132. ),  
  133. implode( '<br>', $duplicates ) 
  134. ); 
  135. lib3()->ui->admin_message( $msg, 'error' ); 
  136.  
  137. /** 
  138. * Registers the Add-On 
  139. * @since 1.0.0 
  140. * @param array $list The Add-Ons list. 
  141. * @return array The updated Add-Ons list. 
  142. */ 
  143. public function register( $list ) { 
  144. $list[ self::ID ] = (object) array( 
  145. 'name' => __( 'BuddyPress Integration', 'membership2' ),  
  146. 'description' => __( 'Integrate BuddyPress with Membership 2.', 'membership2' ),  
  147. 'icon' => 'dashicons dashicons-groups',  
  148. 'details' => array( 
  149. array( 
  150. 'type' => MS_Helper_Html::TYPE_HTML_TEXT,  
  151. 'title' => __( 'Protection Rules', 'membership2' ),  
  152. 'desc' => __( 'Adds BuddyPress rules in the "Protection Rules" page.', 'membership2' ),  
  153. ),  
  154. array( 
  155. 'id' => 'buddypress_registration',  
  156. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  157. 'title' => __( 'Use BuddyPress Registration', 'membership2' ),  
  158. 'desc' => 
  159. __( 'Enable this option to use the BuddyPress registration page instead of the Membership 2 registration page.', 'membership2' ) . 
  160. '<br />' . 
  161. __( 'New users are automatically activated by Membership 2 and no confirmation email is sent to the user!', 'membership2' ),  
  162. 'value' => $this->buddypress_registration,  
  163. 'ajax_data' => array( 
  164. 'action' => $this->ajax_action(),  
  165. 'field' => 'buddypress_registration',  
  166. ),  
  167. ),  
  168. ),  
  169. ); 
  170.  
  171. if ( ! self::buddypress_active() ) { 
  172. $list[ self::ID ]->description .= sprintf( 
  173. '<br /><b>%s</b>',  
  174. __( 'Activate BuddyPress to use this Add-on', 'membership2' ) 
  175. ); 
  176. $list[ self::ID ]->action = '-'; 
  177.  
  178. return $list; 
  179.  
  180. /** 
  181. * Returns true, when the BuddyPress plugin is activated. 
  182. * @since 1.0.0 
  183. * @return bool 
  184. */ 
  185. static public function buddypress_active() { 
  186. global $bp; 
  187.  
  188. return ( ! empty( $bp ) && function_exists( 'bp_buffer_template_part' ) ); 
  189.  
  190. /** 
  191. * Add buddypress rule tabs in membership level edit. 
  192. * @since 1.0.0 
  193. * @filter ms_controller_membership_get_tabs 
  194. * @param array $tabs The current tabs. 
  195. * @param int $membership_id The membership id to edit 
  196. * @return array The filtered tabs. 
  197. */ 
  198. public function rule_tabs( $tabs ) { 
  199. $rule = MS_Addon_Buddypress_Rule::RULE_ID; 
  200. $tabs[ $rule ] = true; 
  201.  
  202. return $tabs; 
  203.  
  204. /** 
  205. * Display the BuddyPress registration form instead of the default 
  206. * Membership2 registration form. 
  207. * @since 1.0.0 
  208. * @return string HTML code of the registration form or empty string to use 
  209. * the default form. 
  210. */ 
  211. public function registration_form( $code ) { 
  212. global $bp; 
  213.  
  214. if ( self::buddypress_active() ) { 
  215. // Add Membership2 fields to the form so we know what comes next. 
  216. $this->add_action( 'bp_custom_signup_steps', 'membership_fields' ); 
  217.  
  218. // Redirect everything after the submit button to output buffer... 
  219. $this->add_action( 
  220. 'bp_after_registration_submit_buttons',  
  221. 'catch_nonce_field',  
  222. 9999 
  223. ); 
  224.  
  225. // Tell BuddyPress that we want the registration form. 
  226. $bp->signup->step = 'request-details'; 
  227.  
  228. // Get the BuddyPress registration page. 
  229. $code = bp_buffer_template_part( 'members/register', null, false ); 
  230.  
  231. // Don't add <p> tags, the form is already formatted! 
  232. remove_filter( 'the_content', 'wpautop' ); 
  233.  
  234. return $code; 
  235.  
  236. /** 
  237. * Redirects all output to the Buffer, so we can easily discard it later... 
  238. * @since 1.0.0 
  239. */ 
  240. public function catch_nonce_field() { 
  241. ob_start(); 
  242.  
  243. /** 
  244. * Output hidden form fields that are parsed by Membership2 when the 
  245. * registration was completed. 
  246. * This is used to recognize that the registration should be handled by 
  247. * Membership2 and which screen to display next. 
  248. * Note that the form is submitted to Membership2, so we need to 
  249. * handle the background stuff. BuddyPress will not do it for us... 
  250. * @since 1.0.0 
  251. */ 
  252. public function membership_fields() { 
  253. /** 
  254. * Discard the contents of the output buffer. It only contains the 
  255. * BuddyPress nonce fields. 
  256. */ 
  257. ob_end_clean(); 
  258.  
  259. $field_membership = array( 
  260. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  261. 'name' => 'membership_id',  
  262. 'value' => $_REQUEST['membership_id'],  
  263. ); 
  264. $field_action = array( 
  265. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  266. 'name' => 'action',  
  267. 'value' => 'register_user',  
  268. ); 
  269. $field_step = array( 
  270. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  271. 'name' => 'step',  
  272. 'value' => MS_Controller_Frontend::STEP_REGISTER_SUBMIT,  
  273. ); 
  274.  
  275. MS_Helper_Html::html_element( $field_membership ); 
  276. MS_Helper_Html::html_element( $field_action ); 
  277. MS_Helper_Html::html_element( $field_step ); 
  278. wp_nonce_field( $field_action['value'] ); 
  279.  
  280. /** 
  281. * The Registration form was submitted and the nonce-check verified. 
  282. * We have to match the BuddyPress field-names with the 
  283. * Membership2 names. 
  284. * This preparation only ensures that the user can be created. 
  285. * XProfile fields are not handled here... 
  286. * @since 1.0.0 
  287. */ 
  288. public function prepare_create_user() { 
  289. $_REQUEST['first_name'] = $_REQUEST['signup_username']; 
  290. $_REQUEST['last_name'] = ''; 
  291. $_REQUEST['username'] = $_REQUEST['signup_username']; 
  292. $_REQUEST['email'] = $_REQUEST['signup_email']; 
  293. $_REQUEST['password'] = $_REQUEST['signup_password']; 
  294. $_REQUEST['password2'] = $_REQUEST['signup_password_confirm']; 
  295.  
  296. /** 
  297. * After the user was successfully created we now have the opportunity to 
  298. * save the XProfile fields. 
  299. * @see bp-xprofile-screens.php function xprofile_screen_edit_profile() 
  300. * @since 1.0.0 
  301. * @param WP_User $user The new user. 
  302. */ 
  303. public function save_custom_fields( $user ) { 
  304. if ( ! bp_is_active( 'xprofile' ) ) { return; } 
  305.  
  306. // Make sure hidden field is passed and populated 
  307. if ( isset( $_POST['signup_profile_field_ids'] ) 
  308. && ! empty( $_POST['signup_profile_field_ids'] ) 
  309. ) { 
  310. // Let's compact any profile field info into an array 
  311. $profile_field_ids = wp_parse_id_list( $_POST['signup_profile_field_ids'] ); 
  312.  
  313. // Loop through the posted fields formatting any datebox values then add to usermeta 
  314. foreach ( (array) $profile_field_ids as $field_id ) { 
  315. $value = ''; 
  316. $visibility = 'public'; 
  317.  
  318. if ( ! isset( $_POST['field_' . $field_id] ) ) { 
  319. // Build the value of date-fields. 
  320. if ( ! empty( $_POST['field_' . $field_id . '_day'] ) 
  321. && ! empty( $_POST['field_' . $field_id . '_month'] ) 
  322. && ! empty( $_POST['field_' . $field_id . '_year'] ) 
  323. ) { 
  324. // Concatenate the values. 
  325. $date_value = 
  326. $_POST['field_' . $field_id . '_day'] . ' ' . 
  327. $_POST['field_' . $field_id . '_month'] . ' ' . 
  328. $_POST['field_' . $field_id . '_year']; 
  329.  
  330. // Turn the concatenated value into a timestamp. 
  331. $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $date_value ) ); 
  332.  
  333. if ( ! empty( $_POST['field_' . $field_id] ) ) { 
  334. $value = $_POST['field_' . $field_id]; 
  335.  
  336. if ( ! empty( $_POST['field_' . $field_id . '_visibility'] ) ) { 
  337. $visibility = $_POST['field_' . $field_id . '_visibility']; 
  338.  
  339. xprofile_set_field_visibility_level( $field_id, $user->id, $visibility ); 
  340. xprofile_set_field_data( $field_id, $user->id, $value, false ); 
  341.  
  342. /** 
  343. * Automatically confirms new registrations. 
  344. * @since 1.0.0 
  345. * @param int $user_id The new User-ID 
  346. */ 
  347. public function disable_validation( $user_id ) { 
  348. $member = MS_Factory::load( 'MS_Model_Member', $user_id ); 
  349. $member->confirm(); 
  350.