class.swpm-wp-loaded-tasks.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. class SwpmWpLoadedTasks {
  3. public function __construct() {
  4. }
  5. /*
  6. * This is triggered after all plugins, themes and WP has loaded.
  7. * It is triggered after init, plugins_loaded etc.
  8. */
  9. public function do_wp_loaded_tasks() {
  10. $this->synchronise_swpm_logout_for_wp_users();
  11. //IPN listener
  12. $this->swpm_ipn_listener();
  13. //Cancel subscirption action listener
  14. $cancel_sub_action = filter_input( INPUT_POST, 'swpm_do_cancel_sub', FILTER_SANITIZE_NUMBER_INT );
  15. if ( ! empty( $cancel_sub_action ) ) {
  16. $this->do_cancel_sub();
  17. }
  18. }
  19. /*
  20. * Logs out the user from the swpm session if they are logged out of the WP user session
  21. */
  22. public function synchronise_swpm_logout_for_wp_users() {
  23. if ( ! is_user_logged_in() ) {
  24. /* WP user is logged out. So logout the SWPM user (if applicable) */
  25. if ( SwpmMemberUtils::is_member_logged_in() ) {
  26. //Check if force WP user login sync is enabled or not
  27. $force_wp_user_sync = SwpmSettings::get_instance()->get_value( 'force-wp-user-sync' );
  28. if ( empty( $force_wp_user_sync ) ) {
  29. return '';
  30. }
  31. /* Force WP user login sync is enabled. */
  32. /* SWPM user is logged in the system. Log him out. */
  33. SwpmLog::log_auth_debug( 'synchronise_swpm_logout_for_wp_users() - Force wp user login sync is enabled. ', true );
  34. SwpmLog::log_auth_debug( 'WP user session is logged out for this user. So logging out of the swpm session also.', true );
  35. wp_logout();
  36. }
  37. }
  38. }
  39. /* Payment Gateway IPN listener */
  40. public function swpm_ipn_listener() {
  41. //Listen and handle PayPal IPN
  42. $swpm_process_ipn = filter_input( INPUT_GET, 'swpm_process_ipn' );
  43. if ( $swpm_process_ipn == '1' ) {
  44. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm_handle_pp_ipn.php';
  45. exit;
  46. }
  47. //Listen and handle Stripe Buy Now IPN
  48. $swpm_process_stripe_buy_now = filter_input( INPUT_GET, 'swpm_process_stripe_buy_now' );
  49. if ( $swpm_process_stripe_buy_now == '1' ) {
  50. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-stripe-buy-now-ipn.php';
  51. exit;
  52. }
  53. //Listen and handle Stripe SCA Buy Now IPN
  54. $swpm_process_stripe_sca_buy_now = filter_input( INPUT_GET, 'swpm_process_stripe_sca_buy_now' );
  55. if ( $swpm_process_stripe_sca_buy_now == '1' ) {
  56. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-stripe-sca-buy-now-ipn.php';
  57. exit;
  58. }
  59. //Listen and handle Stripe Subscription IPN
  60. $swpm_process_stripe_subscription = filter_input( INPUT_GET, 'swpm_process_stripe_subscription' );
  61. if ( $swpm_process_stripe_subscription == '1' ) {
  62. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-stripe-subscription-ipn.php';
  63. exit;
  64. }
  65. //Listen and handle Stripe SCA Subscription IPN
  66. $swpm_process_stripe_sca_subscription = filter_input( INPUT_GET, 'swpm_process_stripe_sca_subscription' );
  67. $hook = filter_input( INPUT_GET, 'hook', FILTER_SANITIZE_NUMBER_INT );
  68. if ( $swpm_process_stripe_sca_subscription == '1' ) {
  69. //$hook == 1 means it is a background post via webshooks. Otherwise it is direct post to the script after payment (at the time of payment).
  70. if ( $hook ) {
  71. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-stripe-subscription-ipn.php';
  72. } else {
  73. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-stripe-sca-subscription-ipn.php';
  74. }
  75. exit;
  76. }
  77. //Listen and handle Braintree Buy Now IPN
  78. $swpm_process_braintree_buy_now = filter_input( INPUT_GET, 'swpm_process_braintree_buy_now' );
  79. if ( $swpm_process_braintree_buy_now == '1' ) {
  80. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-braintree-buy-now-ipn.php';
  81. exit;
  82. }
  83. if ( wp_doing_ajax() ) {
  84. //Listen and handle smart paypal checkout IPN
  85. include SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-smart-checkout-ipn.php';
  86. add_action( 'wp_ajax_swpm_process_pp_smart_checkout', 'swpm_pp_smart_checkout_ajax_hanlder' );
  87. add_action( 'wp_ajax_nopriv_swpm_process_pp_smart_checkout', 'swpm_pp_smart_checkout_ajax_hanlder' );
  88. //Listed and handle Stripe SCA checkout session create requests
  89. require_once SIMPLE_WP_MEMBERSHIP_PATH . 'ipn/swpm-stripe-sca-buy-now-ipn.php';
  90. }
  91. }
  92. private function do_cancel_sub() {
  93. function msg( $msg, $is_error = true ) {
  94. echo $msg;
  95. echo '<br><br>';
  96. echo SwpmUtils::_( 'You will be redirected to the previous page in a few seconds. If not, please <a href="">click here</a>.' );
  97. echo '<script>function toPrevPage(){window.location = window.location.href;}setTimeout(toPrevPage,5000);</script>';
  98. if ( ! $is_error ) {
  99. wp_die( '', SwpmUtils::_( 'Success!' ), array( 'response' => 200 ) );
  100. }
  101. wp_die();
  102. }
  103. $token = filter_input( INPUT_POST, 'swpm_cancel_sub_token', FILTER_SANITIZE_STRING );
  104. if ( empty( $token ) ) {
  105. //no token
  106. msg( SwpmUtils::_( 'No token provided.' ) );
  107. }
  108. //check nonce
  109. $nonce = filter_input( INPUT_POST, 'swpm_cancel_sub_nonce', FILTER_SANITIZE_STRING );
  110. if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $token ) ) {
  111. //nonce check failed
  112. msg( SwpmUtils::_( 'Nonce check failed.' ) );
  113. }
  114. if ( ! SwpmMemberUtils::is_member_logged_in() ) {
  115. //member not logged in
  116. msg( SwpmUtils::_( 'You are not logged in.' ) );
  117. }
  118. $member_id = SwpmMemberUtils::get_logged_in_members_id();
  119. $subs = new SWPM_Member_Subscriptions( $member_id );
  120. $sub = $subs->find_by_token( $token );
  121. if ( empty( $sub ) ) {
  122. //no subscription found
  123. return false;
  124. }
  125. $res = $subs->cancel( $sub['sub_id'] );
  126. if ( $res !== true ) {
  127. msg( $res );
  128. }
  129. msg( SwpmUtils::_( 'Subscription has been cancelled.' ), false );
  130. }
  131. }