/app_old/membershipincludes/classes/class.membership.php

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