Leinad4Mind

Untitled

Apr 29th, 2018
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Hide Free Levels to all users with a Membership
  3. */
  4. function pmproc_hide_free_levels($levels){
  5.     if( is_user_logged_in() && pmpro_hasMembershipLevel() ){ //check if user is logged in and has a membership level
  6.     $newlevels = array();
  7.       foreach($levels as $level){
  8.           if(!pmpro_isLevelFree($level)){
  9.             $newlevels[] = $level;
  10.           }
  11.       }  
  12.       return $newlevels;
  13.     }
  14.     return $levels;
  15. }
  16. add_filter("pmpro_levels_array", "pmproc_hide_free_levels");
  17.  
  18. /*
  19.     Hide Renew Level for Free Members
  20. */
  21. function pmproc_hide_renew_free($levels){
  22.     if( is_user_logged_in() && pmpro_hasMembershipLevel("5",$user->ID) ){ //check if user is logged in and has a membership level
  23.     $newlevels = array();
  24.       foreach($levels as $level){
  25.           if($level->id=="4"){
  26.             $newlevels[] = $level;
  27.           }
  28.       }  
  29.       return $newlevels;
  30.     }
  31.     return $levels;
  32. }
  33. add_filter("pmpro_levels_array", "pmproc_hide_renew_free");
  34.  
  35. /*
  36.     Hide Club Members Level for Club Members
  37. */
  38. function pmproc_hide_club_members($levels){
  39.     if( is_user_logged_in() && pmpro_hasMembershipLevel("1",$user->ID) ){ //check if user is logged in and has a membership level
  40.     $newlevels = array();
  41.       foreach($levels as $level){
  42.           if($level->id=="4"){
  43.             $newlevels[] = $level;
  44.           }
  45.       }  
  46.       return $newlevels;
  47.     }
  48.     return $levels;
  49. }
  50. add_filter("pmpro_levels_array", "pmproc_hide_club_members");
  51.  
  52. /*
  53.     Hide Club Members Level for Renewal
  54. */
  55. function pmproc_hide_club_renewal($levels){
  56.     if( is_user_logged_in() && pmpro_hasMembershipLevel("4",$user->ID) ){ //check if user is logged in and has a membership level
  57.     $newlevels = array();
  58.       foreach($levels as $level){
  59.           if($level->id=="4"){
  60.             $newlevels[] = $level;
  61.           }
  62.       }  
  63.       return $newlevels;
  64.     }
  65.     return $levels;
  66. }
  67. add_filter("pmpro_levels_array", "pmproc_hide_club_renewal");
  68.  
  69. /*
  70.     Hide Renew Level for Guests and Login Users without any Membership plan
  71. */
  72. function pmproc_hide_renew_guests($levels){
  73.     if( !pmpro_hasMembershipLevel() ){ //check if user is logged in and has not a membership level
  74.     $newlevels = array();
  75.       foreach($levels as $level){
  76.           if($level->id=="1"){
  77.             $newlevels[] = $level;
  78.           }
  79.       }  
  80.       return $newlevels;
  81.     }
  82.     return $levels;
  83. }
  84. add_filter("pmpro_levels_array", "pmproc_hide_renew_guests");
  85.  
  86.  
  87. /*
  88.     Don't let guests checkout for Renew level.
  89. */
  90. function my_pmpro_registration_guests_checks($okay)
  91. {
  92.     //only check if things are okay so far
  93.     if($okay)
  94.     {
  95.         global $pmpro_level;
  96.                
  97.         if( ( !pmpro_hasMembershipLevel() && $pmpro_level->id == 4 ))
  98.         {
  99.             pmpro_setMessage("You need to be a Club Member to Renew at this price.", "pmpro_error");
  100.                        
  101.             $okay = false;
  102.         }
  103.     }
  104.  
  105.     return $okay;
  106. }
  107. add_action("pmpro_registration_checks", "my_pmpro_registration_guests_checks");
  108.  
  109. /*
  110.     When users cancel (are changed to membership level 0) we give them another "cancelled" level.
  111.     Can be used to downgrade someone to a free level when they cancel.
  112.     Will allow members to the "cancel level" to cancel from that though.
  113. */
  114. function my_pmpro_after_change_membership_level_0($level_id, $user_id)
  115. {
  116.     //set this to the id of the level you want to give members when they cancel
  117.     $cancel_level_id = 5;
  118.    
  119.     //are they cancelling?
  120.     if($level_id == 0 )
  121.     {
  122.         //check if they are cancelling from level $cancel_level_id
  123.         global $wpdb;
  124.         $last_level_id = $wpdb->get_var("SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user_id . "' ORDER BY id DESC");
  125.         if($last_level_id == $cancel_level_id)
  126.             return; //let them cancel
  127.        
  128.         //otherwise give them level $cancel_level_id instead
  129.         pmpro_changeMembershipLevel($cancel_level_id, $user_id);
  130.     }
  131. }
  132. add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level_0", 10, 2);
  133.  
  134. /* Change cancellation to set expiration date for next payment instead of cancelling immediately.
  135.    Assumes orders are generated for each payment (i.e. your webhooks/etc are setup correctly).
  136.    Since 2015-09-21 and PMPro v1.8.5.6 contains code to look up next payment dates via Stripe and PayPal Express APIs.
  137. */
  138. //before cancelling, save the next_payment_timestamp to a global for later use. (Requires PMPro 1.8.5.6 or higher.)
  139. function my_pmpro_before_change_membership_level($level_id, $user_id) {
  140.     //are we on the cancel page?
  141.     global $pmpro_pages, $wpdb, $pmpro_stripe_event, $pmpro_next_payment_timestamp;
  142.     if($level_id == 0 && (is_page($pmpro_pages['cancel']) || (is_admin() && (empty($_REQUEST['from']) || $_REQUEST['from'] != 'profile')))) {
  143.         //get last order
  144.         $order = new MemberOrder();
  145.         $order->getLastMemberOrder($user_id, "success");
  146.        
  147.         //get level to check if it already has an end date
  148.         if(!empty($order) && !empty($order->membership_id))
  149.             $level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1");
  150.        
  151.         //figure out the next payment timestamp
  152.         if(empty($level) || (!empty($level->enddate) && $level->enddate != '0000-00-00 00:00:00')) {
  153.             //level already has an end date. set to false so we really cancel.
  154.             $pmpro_next_payment_timestamp = false;
  155.         } elseif(!empty($order) && $order->gateway == "stripe") {
  156.             //if stripe, try to use the API
  157.             if(!empty($pmpro_stripe_event)) {
  158.                 //cancel initiated from Stripe webhook
  159.                 if(!empty($pmpro_stripe_event->data->object->current_period_end)) {
  160.                     $pmpro_next_payment_timestamp = $pmpro_stripe_event->data->object->current_period_end;
  161.                 }
  162.             } else {
  163.                 //cancel initiated from PMPro
  164.                 $pmpro_next_payment_timestamp = PMProGateway_stripe::pmpro_next_payment("", $user_id, "success");
  165.             }
  166.         } elseif(!empty($order) && $order->gateway == "paypalexpress") {
  167.             //if PayPal, try to use the API
  168.             if(!empty($_POST['next_payment_date']) && $_POST['next_payment_date'] != 'N/A') {
  169.                 //cancel initiated from IPN
  170.                 $pmpro_next_payment_timestamp = strtotime($_POST['next_payment_date'], current_time('timestamp'));
  171.             } else {
  172.                 //cancel initiated from PMPro
  173.                 $pmpro_next_payment_timestamp = PMProGateway_paypalexpress::pmpro_next_payment("", $user_id, "success");
  174.             }
  175.         } else {
  176.             //use built in PMPro function to guess next payment date
  177.             $pmpro_next_payment_timestamp = pmpro_next_payment($user_id);
  178.         }
  179.     }
  180. }
  181. add_action('pmpro_before_change_membership_level', 'my_pmpro_before_change_membership_level', 10, 2);
  182.  
  183. //give users their level back with an expiration
  184. function my_pmpro_after_change_membership_level($level_id, $user_id) { 
  185.     global $pmpro_pages, $wpdb, $pmpro_next_payment_timestamp;
  186.        
  187.     if($pmpro_next_payment_timestamp !== false &&       //this is false if the level already has an enddate
  188.        $level_id == 0 &&                                //make sure we're cancelling
  189.        (is_page($pmpro_pages['cancel']) || (is_admin() && (empty($_REQUEST['from']) || $_REQUEST['from'] != 'profile')))) { //on the cancel page or in admin/adminajax/webhook and not the edit user page
  190.         /*
  191.             okay, let's give the user his old level back with an expiration based on his subscription date
  192.         */
  193.         //get last order
  194.         $order = new MemberOrder();
  195.         $order->getLastMemberOrder($user_id, "cancelled");
  196.            
  197.         //can't do this if we can't find the order
  198.         if(empty($order->id))
  199.             return false;
  200.  
  201.         //get the last level they had      
  202.         $level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1");
  203.        
  204.         //can't do if we can't find an old level
  205.         if(empty($level))
  206.             return false;                              
  207.            
  208.         //last payment date
  209.         $lastdate = date("Y-m-d", $order->timestamp);
  210.                
  211.         /*
  212.             next payment date
  213.         */
  214.         //if stripe or PayPal, try to use the API
  215.         if(!empty($pmpro_next_payment_timestamp)) {
  216.             $nextdate = $pmpro_next_payment_timestamp;
  217.         } else {
  218.             $nextdate = $wpdb->get_var("SELECT UNIX_TIMESTAMP('" . $lastdate . "' + INTERVAL " . $level->cycle_number . " " . $level->cycle_period . ")");
  219.         }
  220.  
  221.         //if the date in the future?
  222.         if($nextdate - time() > 0) {                       
  223.             //give them their level back with the expiration date set
  224.             $old_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1", ARRAY_A);
  225.             $old_level['enddate'] = date("Y-m-d H:i:s", $nextdate);
  226.  
  227.             //disable this hook so we don't loop
  228.             remove_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
  229.             remove_filter('pmpro_cancel_previous_subscriptions', 'my_pmpro_before_change_membership_level', 10, 2);
  230.  
  231.             //disable the action to set the default level on cancels
  232.             remove_action('pmpro_after_change_membership_level', 'pmpro_after_change_membership_level_default_level', 10, 2);
  233.  
  234.             //change level
  235.             pmpro_changeMembershipLevel($old_level, $user_id);
  236.            
  237.             //add the action back just in case
  238.             add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
  239.             add_filter('pmpro_cancel_previous_subscriptions', 'my_pmpro_before_change_membership_level', 10, 2);
  240.  
  241.             //add the action back to set the default level on cancels
  242.             remove_action('pmpro_after_change_membership_level', 'pmpro_after_change_membership_level_default_level', 10, 2);
  243.  
  244.             //change message shown on cancel page
  245.             add_filter("gettext", "my_gettext_cancel_text", 10, 3);
  246.         }
  247.     }
  248.    
  249.     //clear up this global in case we're changing many levels at once (e.g. expiration script running)
  250.     unset($pmpro_next_payment_timestamp);
  251. }
  252. add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
  253.  
  254. //this replaces the cancellation text so people know they'll still have access for a certain amount of time
  255. function my_gettext_cancel_text($translated_text, $text, $domain) {
  256.     if(($domain == "pmpro" || $domain == "paid-memberships-pro") && $text == "Your membership has been cancelled.") {
  257.         global $current_user;
  258.         $translated_text = "Your recurring subscription has been cancelled. Your active membership will expire on " . date(get_option("date_format"), pmpro_next_payment($current_user->ID, "cancelled")) . ".";
  259.     }
  260.    
  261.     return $translated_text;
  262. }
  263.  
  264. //want to update the cancellation email as well
  265. function my_pmpro_email_body($body, $email) {
  266.     if($email->template == "cancel") {
  267.         global $wpdb;
  268.         $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql($email->email) . "' LIMIT 1");
  269.         if(!empty($user_id)) {
  270.             $expiration_date = pmpro_next_payment($user_id);
  271.            
  272.             //if the date in the future?
  273.             if($expiration_date - time() > 0) {                    
  274.                 $body .= "<p>Your access will expire on " . date(get_option("date_format"), $expiration_date) . ".</p>";
  275.             }
  276.         }
  277.     }
  278.    
  279.     return $body;
  280. }
  281. add_filter("pmpro_email_body", "my_pmpro_email_body", 10, 2);
  282.  
  283. /*
  284.     Show a renew link instead of Your Level
  285.     for recurring subscriptions on the levels page.
  286. */
  287. function pmpro_is_level_expiring_soon_yes_on_levels_page($expiring) {
  288.     //only adjust this on the levels page so we don't mess anything up
  289.     global $pmpro_pages;
  290.     if(!is_admin() && !empty($pmpro_pages) && is_page($pmpro_pages['levels']))
  291.         $expiring = true;
  292.    
  293.     return $expiring;
  294. }
  295. add_action('pmpro_is_level_expiring_soon', 'pmpro_is_level_expiring_soon_yes_on_levels_page');
  296.  
  297. /*
  298.     Update the SSLVERSION for CURL to support PayPal Express moving to TLS 1.2
  299.    
  300.     More info: https://devblog.paypal.com/upcoming-security-changes-notice/
  301.    
  302.     Your server will need to be updated to a supported version of OpenSSL for this to work.
  303. */
  304. function my_http_api_curl($handle) {
  305.     curl_setopt( $handle, CURLOPT_SSLVERSION, 6 ); 
  306. }
  307. add_action('http_api_curl', 'my_http_api_curl');
  308.  
  309. /* Cancel subscriptions when a recurring payment fails. As of PMPro version 1.5.9.1, failed payments reported via Stripe webhook, PayPal IPN handler, or Authorize.net silent post kick off the pmpro_subscription_payment_failed action.
  310.    Also make sure to edit the default billing_failure.html email to mention that users are cancelled upon failure. To do so, copy paid-memberships-pro/email/billing_failure.html into themes/{active theme}/paid-memberships-pro/email/ and edit the text to suit your need.
  311.    Note: This cancels on the first failure.
  312. */
  313. function my_pmpro_subscription_payment_failed($order)
  314. {
  315.     //cancel the membership
  316.     pmpro_changeMembershipLevel(0, $order->user_id);
  317. }
  318. add_action("pmpro_subscription_payment_failed", "my_pmpro_subscription_payment_failed");
RAW Paste Data