/app/helper/class-ms-helper-membership.php

  1. <?php 
  2. /** 
  3. * Helper for the Membership class. 
  4. */ 
  5. class MS_Helper_Membership extends MS_Helper { 
  6.  
  7. const MEMBERSHIP_ACTION_SIGNUP = 'membership_signup'; 
  8. const MEMBERSHIP_ACTION_MOVE = 'membership_move'; 
  9. const MEMBERSHIP_ACTION_CANCEL = 'membership_cancel'; 
  10. const MEMBERSHIP_ACTION_RENEW = 'membership_renew'; 
  11. const MEMBERSHIP_ACTION_PAY = 'membership_pay'; 
  12.  
  13. const MEMBERSHIP_MSG_ADDED = 1; 
  14. const MEMBERSHIP_MSG_DELETED = 2; 
  15. const MEMBERSHIP_MSG_UPDATED = 3; 
  16. const MEMBERSHIP_MSG_ACTIVATION_TOGGLED = 4; 
  17. const MEMBERSHIP_MSG_STATUS_TOGGLED = 5; 
  18. const MEMBERSHIP_MSG_BULK_UPDATED = 6; 
  19. const MEMBERSHIP_MSG_NOT_ADDED = -1; 
  20. const MEMBERSHIP_MSG_NOT_DELETED = -2; 
  21. const MEMBERSHIP_MSG_NOT_UPDATED = -3; 
  22. const MEMBERSHIP_MSG_ACTIVATION_NOT_TOGGLED = -4; 
  23. const MEMBERSHIP_MSG_STATUS_NOT_TOGGLED = -5; 
  24. const MEMBERSHIP_MSG_BULK_NOT_UPDATED = -6; 
  25. const MEMBERSHIP_MSG_PARTIALLY_UPDATED = -8; 
  26.  
  27. public static function get_admin_messages( $msg = 0 ) { 
  28. $messages = apply_filters( 
  29. 'ms_helper_membership_get_admin_messages',  
  30. array( 
  31. self::MEMBERSHIP_MSG_ADDED => __( 'You have successfully set up your <b>%s</b> Membership.', 'membership2' ),  
  32. self::MEMBERSHIP_MSG_DELETED => __( 'Membership deleted.', 'membership2' ),  
  33. self::MEMBERSHIP_MSG_UPDATED => __( 'Membership <b>%s</b> updated.', 'membership2' ),  
  34. self::MEMBERSHIP_MSG_ACTIVATION_TOGGLED => __( 'Membership activation toggled.', 'membership2' ),  
  35. self::MEMBERSHIP_MSG_STATUS_TOGGLED => __( 'Membership status toggled.', 'membership2' ),  
  36. self::MEMBERSHIP_MSG_BULK_UPDATED => __( 'Memberships bulk updated.', 'membership2' ),  
  37. self::MEMBERSHIP_MSG_NOT_ADDED => __( 'Membership not added.', 'membership2' ),  
  38. self::MEMBERSHIP_MSG_NOT_DELETED => __( 'Membership not deleted.', 'membership2' ),  
  39. self::MEMBERSHIP_MSG_NOT_UPDATED => __( 'Membership not updated.', 'membership2' ),  
  40. self::MEMBERSHIP_MSG_ACTIVATION_NOT_TOGGLED => __( 'Membership activation not toggled.', 'membership2' ),  
  41. self::MEMBERSHIP_MSG_STATUS_NOT_TOGGLED => __( 'Membership status not toggled.', 'membership2' ),  
  42. self::MEMBERSHIP_MSG_BULK_NOT_UPDATED => __( 'Memberships bulk not updated.', 'membership2' ),  
  43. self::MEMBERSHIP_MSG_PARTIALLY_UPDATED => __( 'Memberships partially updated. Some fields could not be changed after members have signed up.', 'membership2' ),  
  44. ); 
  45.  
  46. if ( array_key_exists( $msg, $messages ) ) { 
  47. return $messages[ $msg ]; 
  48. } else { 
  49. return null; 
  50.  
  51. public static function print_admin_message() { 
  52. $msg = self::get_msg_id(); 
  53.  
  54. $class = ( $msg > 0 ) ? 'updated' : 'error'; 
  55.  
  56. if ( $msg = self::get_admin_messages( $msg ) ) { 
  57. lib3()->ui->admin_message( $msg, $class ); 
  58.  
  59. public static function get_admin_message( $args = null, $membership = null ) { 
  60. $msg = ''; 
  61. $msg_id = self::get_msg_id(); 
  62.  
  63. if ( $msg = self::get_admin_messages( $msg_id ) ) { 
  64. if ( ! empty( $args ) ) { 
  65. $msg = vsprintf( $msg, $args ); 
  66.  
  67. // When the first membership was created show a popup to the user 
  68. $is_first = true; 
  69. if ( $is_first 
  70. && self::MEMBERSHIP_MSG_ADDED == $msg_id 
  71. && ! empty( $membership ) 
  72. ) { 
  73. $url = MS_Controller_Plugin::get_admin_settings_url(); 
  74.  
  75. self::show_setup_note( $membership ); 
  76.  
  77. return apply_filters( 
  78. 'ms_helper_membership_get_admin_message',  
  79. $msg 
  80. ); 
  81.  
  82. public static function get_admin_title() { 
  83. $title = __( 'Memberships', 'membership2' ); 
  84.  
  85. $msg = self::get_msg_id(); 
  86. if ( self::MEMBERSHIP_MSG_ADDED == $msg ) { 
  87. $title = __( 'Congratulations!', 'membership2' ); 
  88. return apply_filters( 'ms_helper_membership_get_admin_title', $title ); 
  89.  
  90. public static function get_msg_id() { 
  91. $msg = ! empty( $_GET['msg'] ) ? (int) $_GET['msg'] : 0; 
  92. return apply_filters( 'ms_helper_membership_get_msg_id', $msg ); 
  93.  
  94. /** 
  95. * Displays a PopUp to the user that shows a sumary of the setup wizard 
  96. * including possible next steps for configuration. 
  97. * 
  98. * @since 1.0.0 
  99. * @param MS_Model_Membership $membership The membership that was created. 
  100. */ 
  101. public static function show_setup_note( $membership ) { 
  102. $popup = array(); 
  103.  
  104. $popup['title'] = sprintf( 
  105. '<i class="dashicons dashicons-yes"></i> %1$s<div class="subtitle">%2$s</div>',  
  106. __( 'Congratulations!', 'membership2' ),  
  107. sprintf( 
  108. __( 'You have successfully set up your <b>%1$s</b> Membership.', 'membership2' ),  
  109. $membership->name 
  110. ); 
  111.  
  112. $setup = MS_Factory::create( 'MS_View_Settings_Page_Setup' ); 
  113.  
  114. $popup['modal'] = true; 
  115. $popup['close'] = false; 
  116. $popup['sticky'] = false; 
  117. $popup['class'] = 'ms-setup-done'; 
  118. $popup['body'] = $setup->to_html(); 
  119. $popup['height'] = $setup->dialog_height(); 
  120.  
  121. $popup['body'] .= sprintf( 
  122. '<div class="buttons">' . 
  123. '<a href="%s" class="button">%s</a> ' . 
  124. '<button type="button" class="button-primary close">%s</button>' . 
  125. '</div>',  
  126. MS_Controller_Plugin::get_admin_url( 'protection' ),  
  127. __( 'Set-up Access Levels', 'membership2' ),  
  128. __( 'Finish', 'membership2' ) 
  129. ); 
  130.  
  131. lib3()->html->popup( $popup ); 
  132.  
  133. $settings = MS_Plugin::instance()->settings; 
  134. $settings->is_first_membership = false; 
  135. if ( ! $membership->is_free ) { 
  136. $settings->is_first_paid_membership = false; 
  137. $settings->save(); 
.