M_Membership

The Membership 2 M Membership class.

Defined (1)

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

/app_old/membershipincludes/classes/class.membership.php  
  1. class M_Membership extends WP_User { 
  2.  
  3. var $db; 
  4.  
  5. var $tables = array('membership_relationships', 'membership_levels', 'subscriptions', 'user_queue', 'member_payments'); 
  6.  
  7. var $membership_relationships, $membership_levels, $subscriptions, $user_queue, $member_payments; 
  8.  
  9. var $subids; 
  10. var $levids; 
  11.  
  12. var $levels = array(); 
  13.  
  14. function M_Membership( $id, $name = '' ) { 
  15.  
  16. global $wpdb; 
  17.  
  18. if($id != 0) { 
  19. parent::__construct( $id, $name ); 
  20.  
  21. $this->db =& $wpdb; 
  22.  
  23. foreach($this->tables as $table) { 
  24. $this->$table = membership_db_prefix($this->db, $table); 
  25.  
  26. $this->transition_through_subscription(); 
  27.  
  28.  
  29. function active_member() { 
  30.  
  31. $active = get_user_meta( $this->ID, membership_db_prefix($this->db, 'membership_active', false), true); 
  32.  
  33. if(empty($active) || $active == 'yes') { 
  34. return apply_filters( 'membership_active_member', true, $this->ID); 
  35. } else { 
  36. return apply_filters( 'membership_active_member', false, $this->ID); 
  37.  
  38. function mark_for_expire( $sub_id ) { 
  39. update_user_meta( $this->ID, '_membership_expire_next', $sub_id); 
  40.  
  41. do_action('membership_mark_for_expire', $sub_id, $this->ID); 
  42.  
  43.  
  44. function is_marked_for_expire($sub_id) { 
  45.  
  46. $markedsub_id = get_user_meta( $this->ID, '_membership_expire_next', true); 
  47.  
  48. if(!empty($markedsub_id) && $markedsub_id == $sub_id) { 
  49. return apply_filters('membership_is_marked_for_expire', true, $this->ID); 
  50. } else { 
  51. return apply_filters('membership_is_marked_for_expire', false, $this->ID); 
  52.  
  53.  
  54. function is_member() { 
  55.  
  56. $sql = $this->db->prepare( "SELECT count(*) FROM {$this->membership_relationships} WHERE user_id = %d", $this->ID ); 
  57.  
  58. $res = $this->db->get_var($sql); 
  59.  
  60. if($res > 0) { 
  61. return apply_filters('membership_is_member', true, $this->ID); 
  62. } else { 
  63. return apply_filters('membership_is_member', false, $this->ID); 
  64.  
  65.  
  66. function has_subscription() { 
  67.  
  68. $sql = $this->db->prepare( "SELECT count(*) FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id != 0", $this->ID ); 
  69.  
  70. $res = $this->db->get_var($sql); 
  71.  
  72. if($res > 0) { 
  73. return apply_filters('membership_has_subscription', true, $this->ID); 
  74. } else { 
  75. return apply_filters('membership_has_subscription', false, $this->ID); 
  76.  
  77.  
  78. function move_to($sub_id, $thislevel_id, $thislevel_order, $nextlevel) { 
  79.  
  80. if($this->on_sub($sub_id)) { 
  81.  
  82. if($nextlevel) { 
  83.  
  84. $this->move_subscription($sub_id, $sub_id, $nextlevel->level_id, $nextlevel->level_order); 
  85.  
  86.  
  87.  
  88. function current_subscription() { 
  89.  
  90. $sql = $this->db->prepare( "SELECT sub_id FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id != 0", $this->ID ); 
  91.  
  92. $res = $this->db->get_results($sql); 
  93.  
  94. if(!empty($res)) { 
  95. return apply_filters('membership_current_subscription', $res, $this->ID); 
  96. } else { 
  97. return apply_filters('membership_current_subscription', false, $this->ID); 
  98.  
  99.  
  100. function started_on_level_in_sub( $sub_id ) { 
  101.  
  102. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $sub_id ); 
  103. $results = $this->db->get_results( $sql ); 
  104.  
  105. if(!empty($results)) { 
  106. foreach($results as $key => $r) { 
  107. return apply_filter('membership_started_on_level_in_sub', $sub_id, mysql2date("U", $rel->updateddate)); 
  108.  
  109.  
  110. function ends_on_level_in_sub( $sub_id ) { 
  111.  
  112. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $sub_id ); 
  113. $results = $this->db->get_results( $sql ); 
  114.  
  115. if(!empty($results)) { 
  116. foreach($results as $key => $r) { 
  117. return apply_filter('membership_ends_on_level_in_sub', $sub_id, mysql2date("U", $rel->expirydate)); 
  118.  
  119.  
  120. function transition_through_subscription() { 
  121.  
  122. do_action('membership_start_transition', $this->ID); 
  123.  
  124. $relationships = $this->get_relationships(); 
  125.  
  126. if($relationships) { 
  127. foreach($relationships as $key => $rel) { 
  128. // Add 6 hours to the expiry date to give a grace period? 
  129. if( strtotime(apply_filters('membership_gateway_exp_window', "+ 6 hours"), mysql2date("U", $rel->expirydate)) <= time() ) { 
  130. // expired, we need to remove the subscription 
  131. if($this->is_marked_for_expire($rel->sub_id)) { 
  132. $this->expire_subscription($rel->sub_id); 
  133. delete_user_meta($this->ID, '_membership_expire_next'); 
  134. continue; 
  135.  
  136. // Need to check if we are on a solo payment and have a valid payment or the next level is free. 
  137. $onsolo = get_user_meta( $this->ID, 'membership_signup_gateway_is_single', true ); 
  138. if(!empty($onsolo) && $onsolo == 'yes') { 
  139. // We are on a solo gateway so need some extra checks 
  140. // Grab the subscription 
  141. $subscription = new M_Subscription($rel->sub_id); 
  142. // Get the next level we will be moving onto 
  143. $nextlevel = $subscription->get_next_level($rel->level_id, $rel->order_instance); 
  144.  
  145. if($nextlevel) { 
  146. // We have a level to move to - let's check it 
  147. if(empty($nextlevel->level_price) || $nextlevel->level_price == 0 ) { 
  148. // The next level is a free one, so I guess we just move to it 
  149. $this->move_to($rel->sub_id, $rel->level_id, $rel->order_instance, $nextlevel); 
  150. } else { 
  151. // The next level is a charged one so we need to make sure we have a payment 
  152. if( $this->has_active_payment( $rel->sub_id, $nextlevel->level_id, $nextlevel->level_order ) ) { 
  153. // We have a current payment for the level we are going to move to 
  154. $this->move_to($rel->sub_id, $rel->level_id, $rel->order_instance, $nextlevel); 
  155. } else { 
  156. // We don't have a payment for this next level so we have to expire it. 
  157. $this->expire_subscription($rel->sub_id); 
  158. } else { 
  159. // We're at the end so need to expire this subscription 
  160. $this->expire_subscription($rel->sub_id); 
  161.  
  162. } else { 
  163. $subscription = new M_Subscription($rel->sub_id); 
  164. $nextlevel = $subscription->get_next_level($rel->level_id, $rel->order_instance); 
  165.  
  166. if($nextlevel) { 
  167. if(empty($nextlevel->level_price)) { 
  168. // this is a non paid level transition so we can go head 
  169. $this->move_to($rel->sub_id, $rel->level_id, $rel->order_instance, $nextlevel); 
  170. } else { 
  171.  
  172. // This is a paid level transition so check for a payment 
  173. // Transition for now cos we are disabling everything when a payment fails 
  174. $this->move_to($rel->sub_id, $rel->level_id, $rel->order_instance, $nextlevel); 
  175. } else { 
  176. // there isn't a next level so expire this subscription 
  177. $this->expire_subscription($rel->sub_id); 
  178.  
  179. } else { 
  180. // not expired we can ignore this for now 
  181. continue; 
  182.  
  183.  
  184. do_action('membership_end_transition', $this->ID); 
  185.  
  186.  
  187. function expire_subscription($sub_id = false) { 
  188.  
  189. if(!apply_filters( 'pre_membership_expire_subscription', true, $sub_id, $this->ID )) { 
  190. return false; 
  191.  
  192. if(!$sub_id) { 
  193. // expire all of the current subscriptions 
  194. $this->db->query( $this->db->prepare( "DELETE FROM {$this->membership_relationships} WHERE user_id = %d", $this->ID )); 
  195. } else { 
  196. // expire just the passed subscription 
  197. $this->db->query( $this->db->prepare( "DELETE FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $sub_id )); 
  198.  
  199.  
  200. // Update users start and expiry meta 
  201. delete_user_meta( $this->ID, 'start_current_' . $sub_id ); 
  202. delete_user_meta( $this->ID, 'expire_current_' . $sub_id ); 
  203. delete_user_meta( $this->ID, 'sent_msgs_' . $sub_id ); 
  204.  
  205. $expiring = get_user_meta( $this->ID, '_membership_expire_next', true); 
  206. if($expiring = $sub_id) { 
  207. delete_user_meta( $this->ID, '_membership_expire_next' ); 
  208.  
  209. do_action( 'membership_expire_subscription', $sub_id, $this->ID); 
  210.  
  211.  
  212. function create_subscription($sub_id, $gateway = 'admin') { 
  213.  
  214. global $blog_id; 
  215.  
  216. if(!$this->active_member()) { 
  217. $this->toggle_activation(); 
  218.  
  219. $subscription = new M_Subscription( $sub_id ); 
  220. $levels = $subscription->get_levels(); 
  221.  
  222. if(!empty($levels)) { 
  223.  
  224. foreach($levels as $key => $level) { 
  225. if($level->level_order == 1) { 
  226. $this->add_subscription($sub_id, $level->level_id, $level->level_order, $gateway); 
  227.  
  228. // Check if a coupon transient already exists 
  229. if(defined('MEMBERSHIP_GLOBAL_TABLES') && MEMBERSHIP_GLOBAL_TABLES === true) { 
  230. if(function_exists('get_site_transient')) { 
  231. $trying = get_site_transient( 'm_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id ); 
  232. } else { 
  233. $trying = get_transient( 'm_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id ); 
  234. } else { 
  235. $trying = get_transient( 'm_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id ); 
  236.  
  237. // If there is a coupon transient do our coupon count magic 
  238. if( $trying != false && is_array($trying) ) { 
  239.  
  240. if( !empty( $trying['coupon_id'] ) ) { 
  241. $coupon = new M_Coupon( $trying['coupon_id'] ); 
  242. // Add one to the coupon count 
  243. $coupon->increment_coupon_used(); 
  244. // Store the coupon details in the usermeta 
  245. update_user_meta( $this->ID, 'm_coupon_' . $sub_id, $trying ); 
  246.  
  247. if(defined('MEMBERSHIP_GLOBAL_TABLES') && MEMBERSHIP_GLOBAL_TABLES === true) { 
  248. if(function_exists('delete_site_transient')) { 
  249. delete_site_transient( 'm_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id ); 
  250. } else { 
  251. delete_transient( 'm_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id ); 
  252. } else { 
  253. delete_transient( 'm_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id ); 
  254.  
  255.  
  256. break; 
  257.  
  258. return true; 
  259.  
  260. } else { 
  261. return false; 
  262.  
  263.  
  264. function remove_active_payment( $sub_id, $level_order, $stamp ) { 
  265.  
  266. $subscription = new M_Subscription($sub_id); 
  267. $level = $subscription->get_level_at_position($level_order); 
  268.  
  269. $sql = $this->db->prepare( "DELETE FROM {$this->member_payments} WHERE member_id = %d AND sub_id = %d AND level_id = %d AND level_order = %d AND paymentmade = %d", $this->ID, $sub_id, $level->id, $level_order, $stamp); 
  270.  
  271. return $this->db->query( $sql ); 
  272.  
  273.  
  274. function record_active_payment( $sub_id, $level_order, $stamp ) { 
  275.  
  276.  
  277. $rel = $this->get_relationship( $sub_id ); 
  278.  
  279. if($rel) { 
  280. $subscription = new M_Subscription($sub_id); 
  281. $level = $subscription->get_level_at_position($level_order); 
  282.  
  283. if($level) { 
  284. $payment = array( 'member_id' => $this->ID,  
  285. 'sub_id' => $sub_id,  
  286. 'level_id' => $level->id,  
  287. 'level_order' => $level_order,  
  288. 'paymentmade' => gmdate( 'Y-m-d H:i:s', $stamp ) 
  289. ); 
  290.  
  291. $expires = mysql2date("U", $rel->expirydate); 
  292. switch($level->level_period_unit) { 
  293. case 'd': $paymentexpires = strtotime('+' . $level->level_period . ' days', $expires); 
  294. break; 
  295. case 'w': $paymentexpires = strtotime('+' . $level->level_period . ' weeks', $expires); 
  296. break; 
  297. case 'm': $paymentexpires = strtotime('+' . $level->level_period . ' months', $expires); 
  298. break; 
  299. case 'y': $paymentexpires = strtotime('+' . $level->level_period . ' years', $expires); 
  300. break; 
  301. $payment['paymentexpires'] = gmdate( 'Y-m-d H:i:s', $paymentexpires); 
  302.  
  303. $this->db->insert( $this->member_payments, $payment); 
  304.  
  305.  
  306.  
  307.  
  308. function has_active_payment( $sub_id, $nextlevel_id, $nextlevel_order ) { 
  309.  
  310. $sql = $this->db->prepare( "SELECT id FROM {$this->member_payments} WHERE member_id = %d AND sub_id = %d AND level_id = %d AND level_order = %d AND paymentexpires >= CURTIME() ORDER BY paymentexpires DESC LIMIT 0, 1", $this->ID, $sub_id, $nextlevel_id, $nextlevel_order); 
  311.  
  312. $row = $this->db->get_var( $sql ); 
  313.  
  314. if(!empty($row)) { 
  315. return true; 
  316. } else { 
  317. return false; 
  318.  
  319.  
  320. function get_subscription_ids() { 
  321.  
  322. if(empty($this->subids)) { 
  323.  
  324. $sql = $this->db->prepare( "SELECT sub_id FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id > 0", $this->ID ); 
  325.  
  326. $this->subids = $this->db->get_col( $sql ); 
  327.  
  328. return $this->subids; 
  329.  
  330. function get_level_ids() { 
  331.  
  332. if(empty($this->levids)) { 
  333.  
  334. $sql = $this->db->prepare( "SELECT level_id, sub_id FROM {$this->membership_relationships} WHERE user_id = %d AND level_id > 0", $this->ID ); 
  335.  
  336. $this->levids = $this->db->get_results( $sql ); 
  337.  
  338.  
  339. return $this->levids; 
  340.  
  341.  
  342. function update_relationship_gateway( $rel_id, $fromgateway, $togateway ) { 
  343.  
  344. if(!empty($rel_id)) { 
  345. $sql = $this->db->prepare( "UPDATE {$this->membership_relationships} SET usinggateway = %s WHERE rel_id = %d AND usinggateway = %s", $togateway, $rel_id, $fromgateway ); 
  346.  
  347. $this->db->query( $sql ); 
  348.  
  349.  
  350. function get_relationship( $sub_id ) { 
  351.  
  352. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $sub_id ); 
  353.  
  354. $result = $this->db->get_row( $sql ); 
  355.  
  356. if(empty($result)) { 
  357. return false; 
  358. } else { 
  359. return $result; 
  360.  
  361.  
  362. function get_relationships() { 
  363.  
  364. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id != 0", $this->ID ); 
  365.  
  366. $result = $this->db->get_results( $sql ); 
  367.  
  368. if(empty($result)) { 
  369. return false; 
  370. } else { 
  371. return $result; 
  372.  
  373.  
  374. function on_level($level_id, $include_subs = false) { 
  375.  
  376. $sql = $this->db->prepare( "SELECT rel_id FROM {$this->membership_relationships} WHERE user_id = %d AND level_id = %d", $this->ID, $level_id ); 
  377.  
  378. if(!$include_subs) { 
  379. $sql .= $this->db->prepare( " AND sub_id = %d", 0 ); 
  380.  
  381. $result = $this->db->get_col( $sql ); 
  382.  
  383. if(empty($result)) { 
  384. return apply_filters('membership_on_level', false, $level_id, $this->ID); 
  385. } else { 
  386. return apply_filters('membership_on_level', true, $level_id, $this->ID); 
  387.  
  388.  
  389. function on_sub($sub_id) { 
  390.  
  391. $sql = $this->db->prepare( "SELECT rel_id FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $sub_id ); 
  392.  
  393. $result = $this->db->get_col( $sql ); 
  394.  
  395. if(empty($result)) { 
  396. return apply_filters('membership_on_sub', false, $sub_id, $this->ID); 
  397. } else { 
  398. return apply_filters('membership_on_sub', true, $sub_id, $this->ID); 
  399.  
  400.  
  401. function add_level($tolevel_id) { 
  402.  
  403. if(!apply_filters( 'pre_membership_add_level', true, $tolevel_id, $this->ID )) { 
  404. return false; 
  405.  
  406. if(!$this->on_level($tolevel_id)) { 
  407. // Add into membership tables 
  408. $this->db->insert($this->membership_relationships, array('user_id' => $this->ID, 'level_id' => $tolevel_id, 'startdate' => current_time('mysql'), 'updateddate' => current_time('mysql'))); 
  409.  
  410. do_action( 'membership_add_level', $tolevel_id, $this->ID ); 
  411.  
  412.  
  413. function drop_level($fromlevel_id) { 
  414.  
  415. if(!apply_filters( 'pre_membership_drop_level', true, $fromlevel_id, $this->ID )) { 
  416. return false; 
  417.  
  418. if($this->on_level($fromlevel_id)) { 
  419.  
  420. $sql = $this->db->prepare( "DELETE FROM {$this->membership_relationships} WHERE user_id = %d AND level_id = %d AND sub_id = 0", $this->ID, $fromlevel_id); 
  421. $this->db->query( $sql ); 
  422.  
  423. do_action( 'membership_drop_level', $fromlevel_id, $this->ID ); 
  424.  
  425.  
  426.  
  427.  
  428. function move_level($fromlevel_id, $tolevel_id) { 
  429.  
  430. if(!apply_filters( 'pre_membership_move_level', true, $fromlevel_id, $tolevel_id, $this->ID )) { 
  431. return false; 
  432.  
  433. if(!$this->on_level($tolevel_id) && $this->on_level($fromlevel_id)) { 
  434.  
  435. $this->db->update( $this->membership_relationships, array('level_id' => $tolevel_id, 'updateddate' => current_time('mysql')), array('level_id' => $fromlevel_id, 'user_id' => $this->ID, 'sub_id' => 0) ); 
  436.  
  437. do_action( 'membership_move_level', $fromlevel_id, $tolevel_id, $this->ID ); 
  438.  
  439.  
  440. function add_subscription($tosub_id, $tolevel_id = false, $to_order = false, $gateway = 'admin') { 
  441.  
  442. if(!apply_filters( 'pre_membership_add_subscription', true, $tosub_id, $tolevel_id, $to_order, $this->ID )) { 
  443. return false; 
  444.  
  445. if(!$this->on_sub($tosub_id)) { 
  446.  
  447. // grab the level information for this position 
  448. $subscription = new M_Subscription( $tosub_id ); 
  449. $level = $subscription->get_level_at($tolevel_id, $to_order); 
  450.  
  451. if($level) { 
  452. $start = current_time('mysql'); 
  453. switch($level->level_period_unit) { 
  454. case 'd': $period = 'days'; break; 
  455. case 'w': $period = 'weeks'; break; 
  456. case 'm': $period = 'months'; break; 
  457. case 'y': $period = 'years'; break; 
  458. default: $period = 'days'; break; 
  459. $expires = gmdate( 'Y-m-d H:i:s', strtotime('+' . $level->level_period . ' ' . $period, strtotime($start) )); 
  460. $this->db->insert($this->membership_relationships, array('user_id' => $this->ID, 'level_id' => $tolevel_id, 'sub_id' => $tosub_id, 'startdate' => $start, 'updateddate' => $start, 'expirydate' => $expires, 'order_instance' => $level->level_order, 'usinggateway' => $gateway )); 
  461.  
  462. // Update users start and expiry meta 
  463. update_user_meta( $this->ID, 'start_current_' . $tosub_id, strtotime($start) ); 
  464. update_user_meta( $this->ID, 'expire_current_' . $tosub_id, strtotime($expires) ); 
  465. update_user_meta( $this->ID, 'using_gateway_' . $tosub_id, $gateway ); 
  466.  
  467. do_action( 'membership_add_subscription', $tosub_id, $tolevel_id, $to_order, $this->ID); 
  468.  
  469.  
  470.  
  471. function get_level_for_sub( $sub_id ) { 
  472.  
  473. $sql = $this->db->prepare( "SELECT level_id FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $sub_id ); 
  474.  
  475. return $this->db->get_var( $sql ); 
  476.  
  477.  
  478. function drop_subscription($fromsub_id) { 
  479.  
  480. if(!apply_filters( 'pre_membership_drop_subscription', true, $fromsub_id, $this->ID )) { 
  481. return false; 
  482.  
  483. if($this->on_sub($fromsub_id)) { 
  484. // Get the level for this subscription before removing it 
  485. $fromlevel_id = $this->get_level_for_sub( $fromsub_id ); 
  486.  
  487. $sql = $this->db->prepare( "DELETE FROM {$this->membership_relationships} WHERE user_id = %d AND sub_id = %d", $this->ID, $fromsub_id); 
  488. $this->db->query( $sql ); 
  489.  
  490. // Update users start and expiry meta 
  491. delete_user_meta( $this->ID, 'start_current_' . $fromsub_id ); 
  492. delete_user_meta( $this->ID, 'expire_current_' . $fromsub_id ); 
  493. delete_user_meta( $this->ID, 'sent_msgs_' . $fromsub_id ); 
  494. delete_user_meta( $this->ID, 'using_gateway_' . $fromsub_id ); 
  495.  
  496. $expiring = get_user_meta( $this->ID, '_membership_expire_next', true); 
  497. if($expiring = $fromsub_id) { 
  498. delete_user_meta( $this->ID, '_membership_expire_next' ); 
  499.  
  500. do_action( 'membership_drop_subscription', $fromsub_id, $fromlevel_id, $this->ID ); 
  501.  
  502.  
  503. function move_subscription($fromsub_id, $tosub_id, $tolevel_id, $to_order) { 
  504.  
  505. if(!apply_filters( 'pre_membership_move_subscription', true, $fromsub_id, $tosub_id, $tolevel_id, $to_order, $this->ID )) { 
  506. return false; 
  507.  
  508. if(!$this->on_level($tolevel_id, true) && $this->on_sub($fromsub_id)) { 
  509. // Get the level for this subscription before removing it 
  510. $fromlevel_id = $this->get_level_for_sub( $fromsub_id ); 
  511.  
  512. // grab the level information for this position 
  513. $subscription = new M_Subscription( $tosub_id ); 
  514. $level = $subscription->get_level_at($tolevel_id, $to_order); 
  515.  
  516. if($level) { 
  517. $start = current_time('mysql'); 
  518. switch($level->level_period_unit) { 
  519. case 'd': $period = 'days'; break; 
  520. case 'w': $period = 'weeks'; break; 
  521. case 'm': $period = 'months'; break; 
  522. case 'y': $period = 'years'; break; 
  523. default: $period = 'days'; break; 
  524. $expires = gmdate( 'Y-m-d H:i:s', strtotime('+' . $level->level_period . ' ' . $period, strtotime($start) )); 
  525.  
  526. // Update users start and expiry meta 
  527. delete_user_meta( $this->ID, 'start_current_' . $fromsub_id ); 
  528. delete_user_meta( $this->ID, 'expire_current_' . $fromsub_id ); 
  529. delete_user_meta( $this->ID, 'sent_msgs_' . $fromsub_id ); 
  530. // get the gateway and then remove it from the usermeta 
  531. $gateway = get_user_meta( $this->ID, 'using_gateway_' . $fromsub_id, true ); 
  532. delete_user_meta( $this->ID, 'using_gateway_' . $fromsub_id ); 
  533.  
  534. update_user_meta( $this->ID, 'start_current_' . $tosub_id, strtotime($start) ); 
  535. update_user_meta( $this->ID, 'expire_current_' . $tosub_id, strtotime($expires) ); 
  536. update_user_meta( $this->ID, 'using_gateway_' . $tosub_id, $gateway ); 
  537.  
  538. $this->db->update( $this->membership_relationships, array('sub_id' => $tosub_id, 'level_id' => $tolevel_id, 'updateddate' => $start, 'expirydate' => $expires, 'order_instance' => $level->level_order), array( 'sub_id' => $fromsub_id, 'user_id' => $this->ID ) ); 
  539.  
  540. do_action( 'membership_move_subscription', $fromsub_id, $fromlevel_id, $tosub_id, $tolevel_id, $to_order, $this->ID ); 
  541.  
  542.  
  543.  
  544. // Member operations 
  545.  
  546. function toggle_activation() { 
  547.  
  548. if(!apply_filters( 'pre_membership_toggleactive_user', true, $this->ID )) { 
  549. return false; 
  550.  
  551. $active = get_user_meta( $this->ID, membership_db_prefix($this->db, 'membership_active', false), true ); 
  552.  
  553. if(empty($active) || $active == 'yes') { 
  554. update_user_meta($this->ID, membership_db_prefix($this->db, 'membership_active', false), 'no'); 
  555.  
  556. do_action('membership_deactivate_user', $this->ID); 
  557.  
  558. } else { 
  559. update_user_meta($this->ID, membership_db_prefix($this->db, 'membership_active', false), 'yes'); 
  560.  
  561. do_action('membership_activate_user', $this->ID); 
  562.  
  563.  
  564. return true; // for now 
  565.  
  566. function deactivate() { 
  567. update_user_meta($this->ID, membership_db_prefix($this->db, 'membership_active', false), 'no'); 
  568.  
  569. // Levels functions 
  570.  
  571. function has_levels() { 
  572. if(!empty($this->levels)) { 
  573. return true; 
  574. } else { 
  575. $levels = $this->get_level_ids(); 
  576.  
  577. if(!empty($levels)) { 
  578. return true; 
  579. } else { 
  580. return false; 
  581.  
  582. function has_level($level_id = false) { 
  583. // Returns true if the user has a level to process 
  584.  
  585. if($level_id && isset($this->levels[$level_id])) { 
  586. return true; 
  587. } else { 
  588. return false; 
  589.  
  590. function has_rule($rulename) { 
  591. // shortcut function 
  592. return $this->has_level_rule($rulename); 
  593.  
  594. function has_level_rule($rulename) { 
  595.  
  596. if(!empty($this->levels)) { 
  597.  
  598. foreach( $this->levels as $key => $level ) { 
  599. if($level->has_rule($rulename)) { 
  600. return true; 
  601.  
  602.  
  603. return false; 
  604.  
  605. function pass_thru( $rulename, $args ) { 
  606.  
  607. if(!empty($this->levels)) { 
  608.  
  609. $functions = array_keys($args); 
  610. $values = array_values($args); 
  611.  
  612. foreach( $this->levels as $key => $level ) { 
  613. if($level->has_positive_rule($rulename)) { 
  614. return $level->positive_pass_thru($rulename, $functions[0], $values[0]); 
  615. } elseif($level->has_negative_rule($rulename)) { 
  616. return $level->negative_pass_thru($rulename, $functions[0], $values[0]); 
  617. } else { 
  618. return false; 
  619.  
  620.  
  621.  
  622.  
  623. function assign_level($level_id, $fullload = true) { 
  624. // Used to force assign a level on a user - mainly for non logged in users 
  625. $this->levels[$level_id] = new M_Level( $level_id, $fullload, array('public', 'core') ); 
  626.  
  627.  
  628. function assign_public_level($level_id, $fullload = true) { 
  629. // Used to force assign a level on a user - mainly for non logged in users 
  630. $this->levels[$level_id] = new M_Level( $level_id, $fullload, array('public', 'core') ); 
  631.  
  632.  
  633. function assign_admin_level($level_id, $fullload) { 
  634. // Used to force assign a level on a user - mainly for non logged in users 
  635. $this->levels[$level_id] = new M_Level( $level_id, $fullload, array('admin', 'core') ); 
  636.  
  637. function assign_core_level($level_id, $fullload) { 
  638. // Used to force assign a level on a user - mainly for non logged in users 
  639. $this->levels[$level_id] = new M_Level( $level_id, $fullload, array('core') ); 
  640.  
  641. function load_levels($fullload = false) { 
  642.  
  643. $levels = $this->get_level_ids(); 
  644.  
  645. if(!empty($levels)) { 
  646. foreach( (array) $levels as $key => $lev ) { 
  647. if(!isset( $this->levels[$lev->level_id] )) { 
  648. $this->levels[$lev->level_id] = new M_Level( $lev->level_id, $fullload, array('public', 'core') ); 
  649.  
  650.  
  651. function load_public_levels($fullload = false) { 
  652.  
  653. $this->load_levels( $fullload ); 
  654.  
  655.  
  656. function load_admin_levels($fullload = false) { 
  657.  
  658. $levels = $this->get_level_ids(); 
  659.  
  660. if(!empty($levels)) { 
  661. foreach( (array) $levels as $key => $lev ) { 
  662. if(!isset( $this->levels[$lev->level_id] )) { 
  663. $this->levels[$lev->level_id] = new M_Level( $lev->level_id, $fullload, array('admin', 'core') ); 
  664.  
  665.  
  666. function load_core_levels($fullload = false) { 
  667.  
  668. $levels = $this->get_level_ids(); 
  669.  
  670. if(!empty($levels)) { 
  671. foreach( (array) $levels as $key => $lev ) { 
  672. if(!isset( $this->levels[$lev->level_id] )) { 
  673. $this->levels[$lev->level_id] = new M_Level( $lev->level_id, $fullload, array('core') ); 
  674.  
  675.  
  676.