/app/view/shortcode/class-ms-view-shortcode-membershipsignup.php

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