MS_View_Shortcode_MembershipSignup

The Membership 2 MS View Shortcode MembershipSignup class.

Defined (1)

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

/app/view/shortcode/class-ms-view-shortcode-membershipsignup.php  
  1. class MS_View_Shortcode_MembershipSignup extends MS_View { 
  2.  
  3. /** 
  4. * Return the HTML code. 
  5. * @since 1.0.0 
  6. * @return string 
  7. */ 
  8. public function to_html() { 
  9. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  10. $member = $this->data['member']; 
  11. $subscriptions = $this->data['subscriptions']; 
  12. $memberships = $this->data['memberships']; 
  13.  
  14. ob_start(); 
  15. ?> 
  16. <div class="ms-membership-form-wrapper"> 
  17. <?php 
  18. if ( count( $subscriptions ) > 0 ) { 
  19. foreach ( $subscriptions as $subscription ) { 
  20. $msg = $subscription->get_status_description(); 
  21.  
  22. $membership = MS_Factory::load( 
  23. 'MS_Model_Membership',  
  24. $subscription->membership_id 
  25. ); 
  26.  
  27. switch ( $subscription->status ) { 
  28. case MS_Model_Relationship::STATUS_CANCELED: 
  29. $this->membership_box_html( 
  30. $membership,  
  31. MS_Helper_Membership::MEMBERSHIP_ACTION_RENEW,  
  32. $msg,  
  33. $subscription 
  34. ); 
  35. break; 
  36.  
  37. case MS_Model_Relationship::STATUS_EXPIRED: 
  38. $this->membership_box_html( 
  39. $membership,  
  40. MS_Helper_Membership::MEMBERSHIP_ACTION_RENEW,  
  41. $msg,  
  42. $subscription 
  43. ); 
  44. break; 
  45.  
  46. case MS_Model_Relationship::STATUS_TRIAL: 
  47. case MS_Model_Relationship::STATUS_ACTIVE: 
  48. case MS_Model_Relationship::STATUS_WAITING: 
  49. $this->membership_box_html( 
  50. $membership,  
  51. MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL,  
  52. $msg,  
  53. $subscription 
  54. ); 
  55. break; 
  56.  
  57. case MS_Model_Relationship::STATUS_PENDING: 
  58. if ( $membership->is_free() ) { 
  59. $memberships[] = $membership; 
  60. } else { 
  61. $this->membership_box_html( 
  62. $membership,  
  63. MS_Helper_Membership::MEMBERSHIP_ACTION_PAY,  
  64. $msg,  
  65. $subscription 
  66. ); 
  67. break; 
  68.  
  69. default: 
  70. $this->membership_box_html( 
  71. $membership,  
  72. MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL,  
  73. $msg,  
  74. $subscription 
  75. ); 
  76. break; 
  77.  
  78. if ( $member->has_membership() && ! empty( $memberships ) ) { 
  79. ?> 
  80. <legend class="ms-move-from"> 
  81. <?php _e( 'Available Memberships', 'membership2' ); ?> 
  82. </legend> 
  83. <?php 
  84. ?> 
  85. <div class="ms-form-price-boxes"> 
  86. <?php 
  87. do_action( 
  88. 'ms_view_shortcode_membershipsignup_form_before_memberships',  
  89. $this->data 
  90. ); 
  91.  
  92. foreach ( $memberships as $membership ) { 
  93. if ( ! empty( $membership->_move_from ) ) { 
  94. $action = MS_Helper_Membership::MEMBERSHIP_ACTION_MOVE; 
  95. } else { 
  96. $action = MS_Helper_Membership::MEMBERSHIP_ACTION_SIGNUP; 
  97.  
  98. $this->membership_box_html( 
  99. $membership,  
  100. $action,  
  101. null,  
  102. null 
  103. ); 
  104.  
  105. do_action( 
  106. 'ms_view_shortcode_membershipsignup_form_after_memberships',  
  107. $this->data 
  108. ); 
  109.  
  110. do_action( 'ms_show_prices' ); 
  111. ?> 
  112. </div> 
  113. </div> 
  114.  
  115. <div style="clear:both;"></div> 
  116. <?php 
  117. $html = ob_get_clean(); 
  118. $html = apply_filters( 'ms_compact_code', $html ); 
  119.  
  120. return apply_filters( 
  121. 'ms_shortcode_signup',  
  122. $html,  
  123. $this->data 
  124. ); 
  125.  
  126. /** 
  127. * Generate a standalone "Sign up for Membership" button. 
  128. * @since 1.0.0 
  129. * @param MS_Model_Membership $membership The membership to sign up for. 
  130. * @param string $label The button label. 
  131. * @return string 
  132. */ 
  133. public function signup_form( $membership, $label ) { 
  134. $html = ''; 
  135.  
  136. $url = $this->get_action_url( 
  137. $membership,  
  138. $this->data['action'],  
  139. $this->data['step'] 
  140. ); 
  141.  
  142. $link = array( 
  143. 'url' => $url,  
  144. 'class' => 'ms-pay-button button',  
  145. 'value' => $label,  
  146. ); 
  147. $html = MS_Helper_Html::html_link( $link, true ); 
  148.  
  149. return $html; 
  150.  
  151. /** 
  152. * Returns a URL to trigger the specified membership action. 
  153. * The URL can be used in a link or a form with only a submit button. 
  154. * @since 1.0.0 
  155. * @param string $action 
  156. * @return string The URL. 
  157. */ 
  158. protected function get_action_url( $membership, $action, $step ) { 
  159. if ( empty( $this->data['member'] ) ) { 
  160. $member = MS_Model_Member::get_current_member(); 
  161. } else { 
  162. $member = $this->data['member']; 
  163.  
  164. if ( is_numeric( $membership ) ) { 
  165. $membership = MS_Factory::load( 
  166. 'MS_Model_Membership',  
  167. $membership 
  168. ); 
  169.  
  170. $membership->_move_from = $member->cancel_ids_on_subscription( 
  171. $membership->id 
  172. ); 
  173.  
  174. $fields = $this->prepare_fields( 
  175. $membership->id,  
  176. $action,  
  177. $step,  
  178. $membership->_move_from 
  179. ); 
  180.  
  181. if ( is_user_logged_in() ) { 
  182. $current = MS_Model_Pages::MS_PAGE_MEMBERSHIPS; 
  183. } else { 
  184. $current = MS_Model_Pages::MS_PAGE_REGISTER; 
  185.  
  186. $url = MS_Model_Pages::get_page_url( $current ); 
  187.  
  188. if ( $action == MS_Helper_Membership::MEMBERSHIP_ACTION_SIGNUP ) { 
  189. // Only add the membership_id to the URL. 
  190. $url = esc_url_raw( 
  191. add_query_arg( 
  192. 'membership_id',  
  193. $membership->id,  
  194. $url 
  195. ); 
  196. } else { 
  197. $url = esc_url_raw( 
  198. add_query_arg( 
  199. '_wpnonce',  
  200. wp_create_nonce( $action ),  
  201. $url 
  202. ); 
  203.  
  204. foreach ( $fields as $field ) { 
  205. $url = esc_url_raw( 
  206. add_query_arg( 
  207. $field['id'],  
  208. $field['value'],  
  209. $url 
  210. ); 
  211.  
  212. return apply_filters( 
  213. 'ms_view_shortcode_membershipsignup_action_url',  
  214. $url,  
  215. $action,  
  216. $membership,  
  217. $this 
  218. ); 
  219.  
  220. /** 
  221. * Output the HTML content of a single membership box. 
  222. * This includes the membership name, description, price and the action 
  223. * button (Sign-up, Cancel, etc.) 
  224. * @since 1.0.0 
  225. * @param MS_Model_Membership $membership 
  226. * @param string $action 
  227. * @param string $msg 
  228. * @param MS_Model_Relationship $subscription 
  229. */ 
  230. private function membership_box_html( $membership, $action, $msg = null, $subscription = null ) { 
  231. $fields = $this->prepare_fields( 
  232. $membership->id,  
  233. $action,  
  234. $this->data['step'],  
  235. $membership->_move_from 
  236. ); 
  237. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  238.  
  239. if ( 0 == $membership->price ) { 
  240. $price = __( 'Free', 'membership2' ); 
  241. } else { 
  242. $price = sprintf( 
  243. '%s %s',  
  244. $settings->currency,  
  245. MS_Helper_Billing::format_price( $membership->total_price ) // Includes Tax 
  246. ); 
  247. $price = apply_filters( 'ms_membership_price', $price, $membership ); 
  248.  
  249. if ( is_user_logged_in() ) { 
  250. $current = MS_Model_Pages::MS_PAGE_MEMBERSHIPS; 
  251. } else { 
  252. $current = MS_Model_Pages::MS_PAGE_REGISTER; 
  253.  
  254. $url = MS_Model_Pages::get_page_url( $current ); 
  255.  
  256. $classes = array( 
  257. 'ms-membership-details-wrapper',  
  258. 'ms-signup',  
  259. 'ms-membership-' . $membership->id,  
  260. 'ms-type-' . $membership->type,  
  261. 'ms-payment-' . $membership->payment_type,  
  262. $membership->trial_period_enabled ? 'ms-with-trial' : 'ms-no-trial',  
  263. 'ms-status-' . ( $subscription ? $subscription->status : 'none' ),  
  264. 'ms-subscription-' . ($subscription ? $subscription->id : 'none' ),  
  265. ); 
  266. ?> 
  267. <form action="<?php echo esc_url( $url ); ?>" class="ms-membership-form" method="post"> 
  268. <div id="ms-membership-wrapper-<?php echo esc_attr( $membership->id ); ?>" class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> 
  269. <div class="ms-top-bar"> 
  270. <h4><span class="ms-title"><?php echo esc_html( $membership->name ); ?></span></h4> 
  271. </div> 
  272. <div class="ms-price-details"> 
  273. <div class="ms-description"><?php echo $membership->get_description(); ?></div> 
  274. <div class="ms-price price"><?php echo esc_html( $price ); ?></div> 
  275.  
  276. <?php if ( $msg ) : ?> 
  277. <div class="ms-bottom-msg"><?php echo '' . $msg; ?></div> 
  278. <?php endif; ?> 
  279. </div> 
  280.  
  281. <div class="ms-bottom-bar"> 
  282. <?php 
  283. $class = apply_filters( 
  284. 'ms_view_shortcode_membershipsignup_form_button_class',  
  285. 'ms-signup-button ' . esc_attr( $action ) 
  286. ); 
  287.  
  288. $button = array( 
  289. 'id' => 'submit',  
  290. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  291. 'value' => esc_html( $this->data[ "{$action}_text" ] ),  
  292. 'class' => $class,  
  293. ); 
  294.  
  295. /** 
  296. * Allow customizing the Signup button. 
  297. * Either adjust the array properties or return a valid HTML 
  298. * string that will be directly output. 
  299. * @since 1.0.1.2 
  300. * @param array|string $button 
  301. * @param MS_Model_Membership $membership 
  302. * @param MS_Model_Subscription $subscription 
  303. */ 
  304. $button = apply_filters( 
  305. 'ms_view_shortcode_membershipsignup_button',  
  306. $button,  
  307. $membership,  
  308. $subscription 
  309. ); 
  310.  
  311. if ( MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL === $action ) { 
  312. /** 
  313. * PayPal Standard Gateway uses a special Cancel button. 
  314. * @see MS_Controller_Gateway 
  315. */ 
  316. $button = apply_filters( 
  317. 'ms_view_shortcode_membershipsignup_cancel_button',  
  318. $button,  
  319. $subscription,  
  320. $this 
  321. ); 
  322. } elseif ( MS_Helper_Membership::MEMBERSHIP_ACTION_PAY === $action ) { 
  323. // Paid membership: Display a Cancel button 
  324.  
  325. $cancel_action = MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL; 
  326. $url = $this->get_action_url( 
  327. $membership->id,  
  328. $cancel_action,  
  329. '' // step is not required for cancel 
  330. ); 
  331.  
  332. $link = array( 
  333. 'url' => $url,  
  334. 'class' => 'ms-cancel-button button',  
  335. 'value' => esc_html( $this->data[ "{$cancel_action}_text" ] ),  
  336. ); 
  337. MS_Helper_Html::html_link( $link ); 
  338.  
  339. wp_nonce_field( $fields['action']['value'] ); 
  340.  
  341. foreach ( $fields as $field ) { 
  342. MS_Helper_Html::html_element( $field ); 
  343.  
  344. /** 
  345. * It's possible to add custom fields to the signup box. 
  346. * @since 1.0.1.2 
  347. */ 
  348. do_action( 'ms_shortcode_signup_form_end', $this ); 
  349.  
  350. MS_Helper_Html::html_element( $button ); 
  351. ?> 
  352. </div> 
  353. </div> 
  354. </form> 
  355. <?php 
  356. do_action( 'ms_show_prices' ); 
  357.  
  358. /** 
  359. * Return an array with input field definitions used on the 
  360. * membership-registration page. 
  361. * @since 1.0.0 
  362. * @param int $membership_id 
  363. * @param string $action 
  364. * @param string $step 
  365. * @param string $move_from_id 
  366. * @return array Field definitions 
  367. */ 
  368. protected function prepare_fields( $membership_id, $action, $step, $move_from_id = null ) { 
  369. $fields = array( 
  370. 'membership_id' => array( 
  371. 'id' => 'membership_id',  
  372. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  373. 'value' => $membership_id,  
  374. ),  
  375. 'action' => array( 
  376. 'id' => 'action',  
  377. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  378. 'value' => $action,  
  379. ),  
  380. 'step' => array( 
  381. 'id' => 'step',  
  382. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  383. 'value' => $step,  
  384. ),  
  385. ); 
  386.  
  387. if ( $move_from_id ) { 
  388. if ( is_array( $move_from_id ) ) { 
  389. $move_from_id = implode( ', ', $move_from_id ); 
  390.  
  391. $fields['move_from_id'] = array( 
  392. 'id' => 'move_from_id',  
  393. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  394. 'value' => $move_from_id,  
  395. ); 
  396.  
  397. if ( MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL == $action ) { 
  398. unset( $fields['step'] ); 
  399.  
  400. return $fields;