MS_Controller_Frontend

Creates the controller for Membership/User registration.

Defined (1)

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

/app/controller/class-ms-controller-frontend.php  
  1. class MS_Controller_Frontend extends MS_Controller { 
  2.  
  3. /** 
  4. * Signup/register process step constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const STEP_CHOOSE_MEMBERSHIP = 'choose_membership'; 
  9. const STEP_REGISTER_FORM = 'register'; 
  10. const STEP_REGISTER_FORM_ALT = 'register_form'; 
  11. const STEP_REGISTER_SUBMIT = 'register_submit'; 
  12. const STEP_PAYMENT_TABLE = 'payment_table'; 
  13. const STEP_GATEWAY_FORM = 'gateway_form'; 
  14. const STEP_PROCESS_PURCHASE = 'process_purchase'; 
  15.  
  16. /** 
  17. * AJAX action constants. 
  18. * @since 1.0.0 
  19. * @var string 
  20. */ 
  21. const ACTION_EDIT_PROFILE = 'edit_profile'; 
  22. const ACTION_VIEW_INVOICES = 'view_invoices'; 
  23. const ACTION_VIEW_ACTIVITIES = 'view_activities'; 
  24. const ACTION_VIEW_RESETPASS = 'rp'; 
  25.  
  26. /** 
  27. * Whether Membership2 will handle the registration process or not. 
  28. * This should not be changed directly but via filter ms_frontend_handle_registration 
  29. * @since 1.0.0 
  30. * @var bool 
  31. */ 
  32. static public $handle_registration = true; 
  33.  
  34. /** 
  35. * User registration errors. 
  36. * This variable is used by the class MS_View_Shortcode_RegisterUser. 
  37. * @since 1.0.0 
  38. * @var string 
  39. */ 
  40. static public $register_errors; 
  41.  
  42. /** 
  43. * Allowed actions to execute in template_redirect hook. 
  44. * @since 1.0.0 
  45. * @var string 
  46. */ 
  47. private $allowed_actions = array( 'signup_process', 'register_user' ); 
  48.  
  49. /** 
  50. * Prepare for Member registration. 
  51. * @since 1.0.0 
  52. */ 
  53. public function __construct() { 
  54. parent::__construct(); 
  55.  
  56. if ( MS_Plugin::is_enabled() ) { 
  57. do_action( 'ms_controller_frontend_construct', $this ); 
  58.  
  59. // Process actions like register new account. 
  60. $this->add_action( 'template_redirect', 'process_actions', 1 ); 
  61.  
  62. // Check if the current page is a Membership Page. 
  63. $this->add_action( 'template_redirect', 'check_for_membership_pages', 2 ); 
  64.  
  65. // Propagates SSL cookies when user logs in. 
  66. $this->add_action( 'wp_login', 'propagate_ssl_cookie', 10, 2 ); 
  67.  
  68. // Enqueue scripts. 
  69. $this->add_action( 'wp_enqueue_scripts', 'enqueue_scripts' ); 
  70.  
  71. // Add classes for all memberships the user is registered to. 
  72. $this->add_filter( 'body_class', 'body_class' ); 
  73.  
  74. // Clears the shortcode memory at the beginning of the_content 
  75. $this->add_filter( 'the_content', 'clear_content_memory', 1 ); 
  76.  
  77. // Compact code for output on the front end. 
  78. add_filter( 
  79. 'ms_compact_code',  
  80. array( 'MS_Helper_Html', 'compact_code' ) 
  81. ); 
  82.  
  83. /** 
  84. * This allows WordPress to provide the default register form. 
  85. * Set the filter response to FALSE to stop Membership2 from 
  86. * handling the registration process. WordPress or other plugins can 
  87. * register users in that case. 
  88. * @since 1.0.0 
  89. */ 
  90. self::$handle_registration = apply_filters( 
  91. 'ms_frontend_handle_registration',  
  92. true 
  93. ); 
  94.  
  95. if ( self::$handle_registration ) { 
  96. // Set the registration URL to the 'Register' Membership Page. 
  97. $this->add_filter( 'wp_signup_location', 'signup_location', 999 ); 
  98. $this->add_filter( 'register_url', 'signup_location', 999 ); 
  99.  
  100. // Redirect users to their Account page after login. 
  101. $this->add_filter( 'login_redirect', 'login_redirect', 10, 3 ); 
  102.  
  103. /** 
  104. * Handle URI actions for registration. 
  105. * Matches returned 'action' to method to execute. 
  106. * Related Action Hooks: 
  107. * - template_redirect 
  108. * @since 1.0.0 
  109. */ 
  110. public function process_actions() { 
  111. $action = $this->get_action(); 
  112.  
  113. /** 
  114. * If $action is set, then call relevant method. 
  115. * Methods: 
  116. * @see $allowed_actions property 
  117. */ 
  118. if ( ! empty( $action ) 
  119. && method_exists( $this, $action ) 
  120. && in_array( $action, $this->allowed_actions ) 
  121. ) { 
  122. $this->$action(); 
  123.  
  124. /** 
  125. * Check pages for the presence of Membership special pages. 
  126. * Related Action Hooks: 
  127. * - template_redirect 
  128. * @since 1.0.0 
  129. */ 
  130. public function check_for_membership_pages() { 
  131. global $post, $wp_query; 
  132.  
  133. // For invoice page purchase process 
  134. $fields = array( 'gateway', 'ms_relationship_id', 'step' ); 
  135.  
  136. if ( ! empty( $post ) 
  137. && isset( $post->post_type ) 
  138. && $post->post_type == MS_Model_Invoice::get_post_type() 
  139. && self::validate_required( $fields ) 
  140. && self::STEP_PROCESS_PURCHASE == $_POST['step'] 
  141. ) { 
  142. do_action( 
  143. 'ms_controller_frontend_signup_process_purchase',  
  144. $this 
  145. ); 
  146.  
  147. $the_page = MS_Model_Pages::current_page(); 
  148.  
  149. if ( $the_page ) { 
  150. // Fix the main query flags for best theme support: 
  151. // Our Membership-Pages are always single pages... 
  152.  
  153. $wp_query->is_single = false; 
  154. $wp_query->is_page = true; 
  155. $wp_query->is_singular = true; 
  156. $wp_query->is_home = false; 
  157. $wp_query->is_frontpage = false; 
  158. $wp_query->tax_query = null; 
  159.  
  160. $the_type = MS_Model_Pages::get_page_type( $the_page ); 
  161. switch ( $the_type ) { 
  162. case MS_Model_Pages::MS_PAGE_MEMBERSHIPS: 
  163. if ( ! MS_Model_Member::is_logged_in() ) { 
  164. wp_safe_redirect( 
  165. MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER ) 
  166. ); 
  167. exit; 
  168. if ( MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL == $this->get_action() ) { 
  169. $this->membership_cancel(); 
  170. } else { 
  171. $this->signup_process(); 
  172. break; 
  173.  
  174. case MS_Model_Pages::MS_PAGE_REGISTER: 
  175. $step = $this->get_signup_step(); 
  176. if ( self::STEP_CHOOSE_MEMBERSHIP == $step && MS_Model_Member::is_logged_in() ) { 
  177. wp_safe_redirect( 
  178. MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_MEMBERSHIPS ) 
  179. ); 
  180. exit; 
  181. if ( MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL == $this->get_action() ) { 
  182. $this->membership_cancel(); 
  183. } else { 
  184. $this->signup_process(); 
  185. break; 
  186.  
  187. case MS_Model_Pages::MS_PAGE_ACCOUNT: 
  188. $this->user_account_manager(); 
  189. break; 
  190.  
  191. case MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT: 
  192. // Set up the protection shortcode. 
  193. $scode = MS_Plugin::instance()->controller->controllers['membership_shortcode']; 
  194. $scode->page_is_protected(); 
  195. break; 
  196.  
  197. case MS_Model_Pages::MS_PAGE_REG_COMPLETE: 
  198. // Do nothing... 
  199. break; 
  200.  
  201. default: 
  202. // Do nothing... 
  203. break; 
  204.  
  205. /** 
  206. * Appends classes to the HTML body that identify all memberships that the 
  207. * current user is registered to. This allows webdesigners to adjust layout 
  208. * or hide elements based on the membership a user has. 
  209. * @since 1.0.0 
  210. * @param array $class Class-names to attach to the body. 
  211. * @return array Modified class-names to attach to the body. 
  212. */ 
  213. public function body_class( $classes ) { 
  214. $member = MS_Model_Member::get_current_member(); 
  215. if ( ! $member->is_logged_in() ) { 
  216. $classes[] = 'ms-guest'; 
  217. } else { 
  218. $classes[] = 'ms-member'; 
  219. $classes[] = 'ms-member-' . $member->id; 
  220.  
  221. $info = MS_Plugin::instance()->controller->get_access_info(); 
  222. foreach ( $info['memberships'] as $membership_id ) { 
  223. $classes[] = 'ms-' . absint( $membership_id ); 
  224.  
  225. return $classes; 
  226.  
  227. /** 
  228. * Clears the shortcode memory at the beginning of each call to the_content. 
  229. * This is required when there are several parts of the page that are 
  230. * rendered via the_content, e.g. a main content and a footer area (this 
  231. * is a theme-specific scenario). Or if the page contains an excerpt and a 
  232. * main content block, ... 
  233. * @since 1.0.0 
  234. * @param string $content The page content 
  235. * @return string Value of $content (unmodified) 
  236. */ 
  237. public function clear_content_memory( $content ) { 
  238. global $wp_current_filter; 
  239. $reset = false; 
  240.  
  241. // Correctly handle nesting. 
  242. foreach ( $wp_current_filter as $filter ) { 
  243. if ( 'the_content' === $filter ) { 
  244. if ( $reset ) { 
  245. /** 
  246. * the_content is called inside the_content. 
  247. * Don't reset again! 
  248. * This can happen for example: A shortcode parses the 
  249. * return code via apply_filters( 'the_content' ) 
  250. */ 
  251. $reset = false; 
  252. break; 
  253. } else { 
  254. $reset = true; 
  255.  
  256. if ( $reset ) { 
  257. MS_Helper_Shortcode::reset_shortcode_usage(); 
  258.  
  259. return $content; 
  260.  
  261. /** 
  262. * Handle entire signup process. 
  263. * @since 1.0.0 
  264. */ 
  265. public function signup_process() { 
  266. $step = $this->get_signup_step(); 
  267. $member = MS_Model_Member::get_current_member(); 
  268.  
  269. do_action( 'ms_frontend_register-' . $step ); 
  270.  
  271. switch ( $step ) { 
  272. /** 
  273. * Initial state. 
  274. */ 
  275. case self::STEP_CHOOSE_MEMBERSHIP: 
  276. // Nothing, simply display Membership page. 
  277. break; 
  278.  
  279. /** 
  280. * If not registered. 
  281. */ 
  282. case self::STEP_REGISTER_FORM: 
  283. case self::STEP_REGISTER_FORM_ALT: 
  284. $this->add_filter( 'the_content', 'register_form', 1 ); 
  285. break; 
  286.  
  287. /** 
  288. * Process user registration. 
  289. */ 
  290. case self::STEP_REGISTER_SUBMIT: 
  291. $this->register_user(); 
  292. break; 
  293.  
  294. /** 
  295. * Show payment table. 
  296. * The payment table is only available if the current user has 
  297. * permission to subscribe to the specified membership. 
  298. */ 
  299. case self::STEP_PAYMENT_TABLE: 
  300. $add_filter = false; 
  301. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  302. $membership_id = $_REQUEST['membership_id']; 
  303. if ( $member->can_subscribe_to( $membership_id ) ) { 
  304. $add_filter = true; 
  305.  
  306. if ( $add_filter ) { 
  307. $this->add_filter( 'the_content', 'payment_table', 1 ); 
  308. } else { 
  309. wp_safe_redirect( 
  310. esc_url_raw( 
  311. add_query_arg( 
  312. array( 'step' => self::STEP_CHOOSE_MEMBERSHIP ) 
  313. ); 
  314. break; 
  315.  
  316. /** 
  317. * Show gateway extra form. 
  318. * Handled by MS_Controller_Gateway. 
  319. */ 
  320. case self::STEP_GATEWAY_FORM: 
  321. do_action( 
  322. 'ms_controller_frontend_signup_gateway_form',  
  323. $this 
  324. ); 
  325. break; 
  326.  
  327. /** 
  328. * Process the purchase action. 
  329. * Handled by MS_Controller_Gateway. 
  330. */ 
  331. case self::STEP_PROCESS_PURCHASE: 
  332. do_action( 
  333. 'ms_controller_frontend_signup_process_purchase',  
  334. $this 
  335. ); 
  336. break; 
  337.  
  338. default: 
  339. MS_Helper_Debug::log( "No handler for step: $step" ); 
  340. break; 
  341.  
  342. /** 
  343. * Get signup process step (multi step form). 
  344. * @since 1.0.0 
  345. * @return string The current signup step after validation. 
  346. */ 
  347. private function get_signup_step() { 
  348. static $Valid_Steps = null; 
  349. static $Login_Steps = null; 
  350.  
  351. if ( empty( $Valid_Steps ) ) { 
  352. $Valid_Steps = apply_filters( 
  353. 'ms_controller_frontend_signup_steps',  
  354. array( 
  355. self::STEP_CHOOSE_MEMBERSHIP,  
  356. self::STEP_REGISTER_FORM,  
  357. self::STEP_REGISTER_FORM_ALT,  
  358. self::STEP_REGISTER_SUBMIT,  
  359. self::STEP_PAYMENT_TABLE,  
  360. self::STEP_GATEWAY_FORM,  
  361. self::STEP_PROCESS_PURCHASE,  
  362. ); 
  363.  
  364. // These steps are only available to logged-in users. 
  365. $Login_Steps = apply_filters( 
  366. 'ms_controller_frontend_signup_steps_private',  
  367. array( 
  368. self::STEP_PAYMENT_TABLE,  
  369. self::STEP_GATEWAY_FORM,  
  370. self::STEP_PROCESS_PURCHASE,  
  371. ); 
  372.  
  373. lib3()->array->equip_request( 'step', 'membership_id' ); 
  374.  
  375. if ( in_array( $_REQUEST['step'], $Valid_Steps ) ) { 
  376. $step = $_REQUEST['step']; 
  377. } else { 
  378. // Initial step 
  379. $step = self::STEP_CHOOSE_MEMBERSHIP; 
  380.  
  381. if ( self::STEP_PAYMENT_TABLE == $step ) { 
  382. if ( ! MS_Model_Membership::is_valid_membership( $_REQUEST['membership_id'] ) ) { 
  383. $step = self::STEP_CHOOSE_MEMBERSHIP; 
  384.  
  385. if ( self::STEP_CHOOSE_MEMBERSHIP == $step && ! empty( $_GET['membership_id'] ) ) { 
  386. $step = self::STEP_PAYMENT_TABLE; 
  387.  
  388. if ( ! MS_Model_Member::is_logged_in() && in_array( $step, $Login_Steps ) ) { 
  389. $step = self::STEP_REGISTER_FORM_ALT; 
  390.  
  391. return apply_filters( 
  392. 'ms_controller_frontend_get_signup_step',  
  393. $step,  
  394. $this 
  395. ); 
  396.  
  397. /** 
  398. * Returns the URL to user registration page. 
  399. * If Membership2 handles registration we can provide the registration 
  400. * step via function param $step. 
  401. * @since 1.0.0 
  402. * @param string $step Empty uses default step (choose_membership). 
  403. * 'choose_membership' show list of memberships. 
  404. * 'register' shows the registration form. 
  405. * @return string URL to the registration page. 
  406. */ 
  407. static public function get_registration_url( $step = null ) { 
  408. $url = wp_registration_url(); 
  409.  
  410. if ( self::$handle_registration && ! empty( $step ) ) { 
  411. $url = esc_url_raw( add_query_arg( 'step', $step, $url ) ); 
  412.  
  413. return $url; 
  414.  
  415. /** 
  416. * Show register user form. 
  417. * Related Filter Hooks: 
  418. * - the_content 
  419. * @since 1.0.0 
  420. * @param string $content The page content to filter. 
  421. * @return string The filtered content. 
  422. */ 
  423. public function register_form( $content ) { 
  424. // Check if the WordPress settings allow user registration. 
  425. if ( ! MS_Model_Member::can_register() ) { 
  426. return __( 'Registration is currently not allowed.', 'membership2' ); 
  427.  
  428. // Do not parse the form when building the excerpt. 
  429. global $wp_current_filter; 
  430. if ( in_array( 'get_the_excerpt', $wp_current_filter ) ) { 
  431. return ''; 
  432.  
  433. /** 
  434. * Add-ons or other plugins can use this filter to define a completely 
  435. * different registration form. If this filter returns any content, then 
  436. * the default form will not be generated 
  437. * @since 1.0.0 
  438. * @var string 
  439. */ 
  440. $custom_code = apply_filters( 
  441. 'ms_frontend_custom_registration_form',  
  442. '',  
  443. self::$register_errors,  
  444. $this 
  445. ); 
  446.  
  447. if ( $custom_code ) { 
  448. $content = $custom_code; 
  449. } else { 
  450. remove_filter( 'the_content', 'wpautop' ); 
  451.  
  452. $did_form = MS_Helper_Shortcode::has_shortcode( 
  453. MS_Helper_Shortcode::SCODE_REGISTER_USER,  
  454. $content 
  455. ); 
  456.  
  457. if ( ! $did_form ) { 
  458. $scode = sprintf( 
  459. '[%s]',  
  460. MS_Helper_Shortcode::SCODE_REGISTER_USER 
  461. ); 
  462. $reg_form = do_shortcode( $scode ); 
  463.  
  464. if ( ! MS_Model_Member::is_logged_in() ) { 
  465. $content = $reg_form; 
  466. } else { 
  467. $content .= $reg_form; 
  468.  
  469. return apply_filters( 
  470. 'ms_controller_frontend_register_form_content',  
  471. $content,  
  472. $this 
  473. ); 
  474.  
  475. /** 
  476. * Handles register user submit. 
  477. * On validation errors, step back to register form. 
  478. * @since 1.0.0 
  479. */ 
  480. public function register_user() { 
  481. do_action( 'ms_controller_frontend_register_user_before', $this ); 
  482.  
  483. if ( ! $this->verify_nonce() ) { 
  484. return; 
  485.  
  486. try { 
  487. $user = MS_Factory::create( 'MS_Model_Member' ); 
  488.  
  489. // Default WP registration filter 
  490. $fields = apply_filters( 'signup_user_init', $_REQUEST ); 
  491. foreach ( $fields as $field => $value ) { 
  492. $user->$field = $value; 
  493.  
  494. $user->save(); 
  495.  
  496. // Default WP action hook 
  497. do_action( 'signup_finished' ); 
  498.  
  499. $user->signon_user(); 
  500.  
  501. if ( MS_Model_Event::save_event( MS_Model_Event::TYPE_MS_REGISTERED, $user ) ) { 
  502. wp_new_user_notification( $user->id, $user->password ); 
  503.  
  504. do_action( 'ms_controller_frontend_register_user_complete', $user ); 
  505.  
  506. // Go to membership signup payment form. 
  507. if ( empty( $_REQUEST['membership_id'] ) ) { 
  508. $redirect = esc_url_raw( 
  509. add_query_arg( 
  510. array( 
  511. 'step' => self::STEP_CHOOSE_MEMBERSHIP,  
  512. ); 
  513. } else { 
  514. $redirect = esc_url_raw( 
  515. add_query_arg( 
  516. array( 
  517. 'step' => self::STEP_PAYMENT_TABLE,  
  518. 'membership_id' => absint( $_REQUEST['membership_id'] ),  
  519. ); 
  520.  
  521. wp_safe_redirect( $redirect ); 
  522. exit; 
  523. catch( Exception $e ) { 
  524. self::$register_errors = $e->getMessage(); 
  525.  
  526. // step back 
  527. $this->add_action( 'the_content', 'register_form', 1 ); 
  528. do_action( 
  529. 'ms_controller_frontend_register_user_error',  
  530. self::$register_errors 
  531. ); 
  532.  
  533. /** 
  534. * Render membership payment information. 
  535. * Related Filter Hooks: 
  536. * - the_content 
  537. * @since 1.0.0 
  538. * @param string $content The page content to filter. 
  539. * @return string The filtered content. 
  540. */ 
  541. public function payment_table( $content ) { 
  542. $data = array(); 
  543. $subscription = null; 
  544. $member = MS_Model_Member::get_current_member(); 
  545. $membership_id = 0; 
  546.  
  547. lib3()->array->equip_request( 'membership_id', 'move_from_id', 'ms_relationship_id' ); 
  548.  
  549. if ( ! empty( $_POST['ms_relationship_id'] ) ) { 
  550. // Error path, showing payment table again with error msg 
  551. $subscription = MS_Factory::load( 
  552. 'MS_Model_Relationship',  
  553. absint( intval( $_POST['ms_relationship_id'] ) ) 
  554. ); 
  555. $membership = $subscription->get_membership(); 
  556. $membership_id = $membership->id; 
  557.  
  558. if ( ! empty( $_POST['error'] ) ) { 
  559. lib3()->array->strip_slashes( $_POST, 'error' ); 
  560. $data['error'] = $_POST['error']; 
  561. } elseif ( ! empty( $_REQUEST['membership_id'] ) ) { 
  562. // First time loading 
  563. $membership_id = intval( $_REQUEST['membership_id'] ); 
  564. $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id ); 
  565. $move_from_id = absint( $_REQUEST['move_from_id'] ); 
  566. $subscription = MS_Model_Relationship::create_ms_relationship( 
  567. $membership_id,  
  568. $member->id,  
  569. '',  
  570. $move_from_id 
  571. ); 
  572. } else { 
  573. MS_Helper_Debug::log( 'Error: missing POST params' ); 
  574. MS_Helper_Debug::log( $_POST ); 
  575. return $content; 
  576.  
  577. // Invalid membership ID was specified: Redirect to membership list. 
  578. if ( ! $subscription ) { 
  579. MS_Model_Pages::redirect_to( MS_Model_Pages::MS_PAGE_MEMBERSHIPS ); 
  580.  
  581. $invoice = $subscription->get_current_invoice(); 
  582.  
  583. /** 
  584. * Notify Add-ons that we are preparing payment details for a membership 
  585. * subscription. 
  586. * E.g. Coupon discount is applied by this hook. 
  587. * @since 1.0.0 
  588. */ 
  589. $invoice = apply_filters( 
  590. 'ms_signup_payment_details',  
  591. $invoice,  
  592. $subscription,  
  593. $membership 
  594. ); 
  595. $invoice->save(); 
  596.  
  597. $data['invoice'] = $invoice; 
  598. $data['membership'] = $membership; 
  599. $data['member'] = $member; 
  600. $data['ms_relationship'] = $subscription; 
  601.  
  602. $view = MS_Factory::load( 'MS_View_Frontend_Payment' ); 
  603. $view->data = apply_filters( 
  604. 'ms_view_frontend_payment_data',  
  605. $data,  
  606. $membership_id,  
  607. $subscription,  
  608. $member,  
  609. $this 
  610. ); 
  611.  
  612. return apply_filters( 
  613. 'ms_controller_frontend_payment_table',  
  614. $view->to_html(),  
  615. $this 
  616. ); 
  617.  
  618. /** 
  619. * Handles membership_cancel action. 
  620. * @since 1.0.0 
  621. */ 
  622. public function membership_cancel() { 
  623. if ( ! empty( $_REQUEST['membership_id'] ) && $this->verify_nonce( null, 'any' ) ) { 
  624. $membership_id = absint( $_REQUEST['membership_id'] ); 
  625. $member = MS_Model_Member::get_current_member(); 
  626. $member->cancel_membership( $membership_id ); 
  627. $member->save(); 
  628.  
  629. $url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER ); 
  630. wp_safe_redirect( $url ); 
  631. exit; 
  632.  
  633. /** 
  634. * Manage user account actions. 
  635. * @since 1.0.0 
  636. * @internal 
  637. */ 
  638. public function user_account_manager() { 
  639. $action = $this->get_action(); 
  640. $member = MS_Model_Member::get_current_member(); 
  641.  
  642. /** 
  643. * These actions are always executed when any user account page loads. 
  644. * @since 1.0.1.0 
  645. */ 
  646. do_action( 
  647. 'ms_frontend_user_account_manager-' . $action,  
  648. $this 
  649. ); 
  650. do_action( 
  651. 'ms_frontend_user_account_manager',  
  652. $action,  
  653. $this 
  654. ); 
  655.  
  656. if ( $this->verify_nonce() ) { 
  657. /** 
  658. * The following two actions are only executed when a form was 
  659. * submitted on a user account page. 
  660. * @since 1.0.1.0 
  661. */ 
  662. do_action( 
  663. 'ms_frontend_user_account_manager_submit-' . $action,  
  664. $this 
  665. ); 
  666. do_action( 
  667. 'ms_frontend_user_account_manager_submit',  
  668. $action,  
  669. $this 
  670. ); 
  671.  
  672. switch ( $action ) { 
  673. case self::ACTION_EDIT_PROFILE: 
  674. $data = array(); 
  675.  
  676. if ( $this->verify_nonce() ) { 
  677. if ( is_array( $_POST ) ) { 
  678. foreach ( $_POST as $field => $value ) { 
  679. $member->$field = $value; 
  680.  
  681. try { 
  682. $member->validate_member_info(); 
  683. $member->save(); 
  684. wp_safe_redirect( 
  685. esc_url_raw( remove_query_arg( 'action' ) ) 
  686. ); 
  687. exit; 
  688.  
  689. catch ( Exception $e ) { 
  690. $data['errors'] = $e->getMessage(); 
  691. $view = MS_Factory::create( 'MS_View_Frontend_Profile' ); 
  692. $data['member'] = $member; 
  693. $data['action'] = $action; 
  694. $view->data = apply_filters( 'ms_view_frontend_profile_data', $data, $this ); 
  695. $view->add_filter( 'the_content', 'to_html', 1 ); 
  696. break; 
  697.  
  698. case self::ACTION_VIEW_INVOICES: 
  699. $data['invoices'] = MS_Model_Invoice::get_public_invoices( 
  700. $member->id 
  701. ); 
  702.  
  703. $view = MS_Factory::create( 'MS_View_Frontend_Invoices' ); 
  704. $view->data = apply_filters( 
  705. 'ms_view_frontend_frontend_invoices',  
  706. $data,  
  707. $this 
  708. ); 
  709. $view->add_filter( 'the_content', 'to_html', 1 ); 
  710. break; 
  711.  
  712. case self::ACTION_VIEW_ACTIVITIES: 
  713. $data['events'] = MS_Model_Event::get_events( 
  714. array( 
  715. 'author' => $member->id,  
  716. 'posts_per_page' => -1,  
  717. ); 
  718.  
  719. $view = MS_Factory::create( 'MS_View_Frontend_Activities' ); 
  720. $view->data = apply_filters( 
  721. 'ms_view_frontend_frontend_activities',  
  722. $data,  
  723. $this 
  724. ); 
  725. $view->add_filter( 'the_content', 'to_html', 1 ); 
  726. break; 
  727.  
  728. case self::ACTION_VIEW_RESETPASS: 
  729. /** 
  730. * Reset password action. 
  731. * This action is accessed via the password-reset email 
  732. * @see class-ms-controller-dialog.php 
  733. * The action is targeted to the Account-page but actually calls 
  734. * the Login-Shortcode. 
  735. */ 
  736. $view = MS_Factory::create( 'MS_View_Shortcode_Login' ); 
  737. $view->data = array( 'action' => 'resetpass' ); 
  738.  
  739. $view->add_filter( 'the_content', 'to_html', 1 ); 
  740. break; 
  741.  
  742. default: 
  743. // Do nothing... 
  744. break; 
  745.  
  746. /** 
  747. * Get the URL the user used to register for a subscription. 
  748. * Uses the default registration page unless the registration was embedded 
  749. * on another page (e.g. using a shortcode). 
  750. * Related Filter Hooks: 
  751. * - wp_signup_location 
  752. * - register_url 
  753. * @since 1.0.0 
  754. * @param string $url The url to filter. 
  755. * @return The new signup url. 
  756. */ 
  757. public function signup_location( $url ) { 
  758. $url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER ); 
  759.  
  760. return apply_filters( 
  761. 'ms_controller_frontend_signup_location',  
  762. $url,  
  763. $this 
  764. ); 
  765.  
  766. /** 
  767. * Propagates SSL cookies when user logs in. 
  768. * Related Action Hooks: 
  769. * - wp_login 
  770. * @since 1.0.0 
  771. * @param type $login The login info. 
  772. * @param WP_User $user The user to login. 
  773. */ 
  774. public function propagate_ssl_cookie( $login, $user = null ) { 
  775. if ( empty( $user ) || ! is_a( $user, 'WP_User' ) ) { 
  776. $user = get_user_by( 'login', $login ); 
  777.  
  778. if ( is_a( $user, 'WP_User' ) && ! is_ssl() ) { 
  779. wp_set_auth_cookie( $user->ID, true, true ); 
  780.  
  781. do_action( 
  782. 'ms_controller_frontend_propagate_ssl_cookie',  
  783. $login,  
  784. $user,  
  785. $this 
  786. ); 
  787.  
  788. /** 
  789. * Redirect user to account page. 
  790. * Only redirect when no previous redirect_to is set or when going to /wp-admin/. 
  791. * @since 1.0.0 
  792. * @param string $redirect_to URL to redirect to. 
  793. * @param string $request URL the user is coming from. 
  794. * @param object $user Logged user's data. 
  795. * @return string The redirect url. 
  796. */ 
  797. public function login_redirect( $redirect_to, $request, $user ) { 
  798. if ( ! empty( $user->ID ) 
  799. && ! MS_Model_Member::is_admin_user( $user->ID ) 
  800. && ( empty( $redirect_to ) || admin_url() == $redirect_to ) 
  801. ) { 
  802. $redirect_to = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_ACCOUNT ); 
  803.  
  804. return apply_filters( 
  805. 'ms_controller_frontend_login_redirect',  
  806. $redirect_to,  
  807. $request,  
  808. $user,  
  809. $this 
  810. ); 
  811.  
  812. /** 
  813. * Adds CSS and JS for Membership special pages used in the front end. 
  814. * @since 1.0.0 
  815. * @return void 
  816. */ 
  817. public function enqueue_scripts() { 
  818. do_action( 
  819. 'ms_controller_frontend_enqueue_scripts',  
  820. $this->get_signup_step(),  
  821. $this->get_action(),  
  822. $this 
  823. ); 
  824.  
  825. $is_ms_page = MS_Model_Pages::is_membership_page(); 
  826. $is_profile = self::ACTION_EDIT_PROFILE == $this->get_action() 
  827. && MS_Model_Pages::is_membership_page( null, MS_Model_Pages::MS_PAGE_ACCOUNT ); 
  828.  
  829. if ( $is_ms_page ) { 
  830. $data = array( 
  831. 'ms_init' => array( 'shortcode' ),  
  832. 'cancel_msg' => __( 'Are you sure you want to cancel?', 'membership2' ),  
  833. ); 
  834.  
  835. lib3()->ui->css( 'ms-styles' ); 
  836. lib3()->ui->js( 'jquery-validate' ); 
  837. lib3()->ui->js( 'ms-public' ); 
  838. MS_Controller_Plugin::translate_jquery_validator(); 
  839.  
  840. if ( $is_profile ) { 
  841. $data['ms_init'][] = 'frontend_profile'; 
  842.  
  843. lib3()->ui->data( 'ms_data', $data );