membershipcron

The Membership 2 membershipcron class.

Defined (1)

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

/app_old/membershipincludes/classes/membershipcron.php  
  1. class membershipcron { 
  2.  
  3. var $build = 1; 
  4.  
  5. var $db; 
  6.  
  7. var $tables = array('membership_levels', 'membership_rules', 'subscriptions', 'subscriptions_levels', 'membership_relationships'); 
  8.  
  9. var $membership_levels; 
  10. var $membership_rules; 
  11. var $membership_relationships; 
  12. var $subscriptions; 
  13. var $subscriptions_levels; 
  14.  
  15. function __construct() { 
  16.  
  17. global $wpdb; 
  18.  
  19. $this->db =& $wpdb; 
  20.  
  21. foreach($this->tables as $table) { 
  22. $this->$table = membership_db_prefix($this->db, $table); 
  23.  
  24. // Set up Actions 
  25. add_action( 'init', array(&$this, 'set_up_schedule') ); 
  26. add_filter( 'cron_schedules', array(&$this, 'add_time_period') ); 
  27.  
  28. if( !$this->get_expiring_relationships_count() >= 50 ) { 
  29. // Schedule for quarter hourly to get number of processing down a bit 
  30. add_action( 'membership_process_quarterhourly_cron', array( &$this, 'transition_user_through_subscriptions' ) ); 
  31. } else { 
  32. // We don't have that many, so let's process hourly instead 
  33. add_action( 'membership_process_hourly_cron', array( &$this, 'transition_user_through_subscriptions' ) ); 
  34.  
  35.  
  36.  
  37.  
  38. function membershipcron() { 
  39. $this->__construct(); 
  40.  
  41. function add_time_period( $periods ) { 
  42.  
  43. if(!is_array($periods)) { 
  44. $periods = array(); 
  45.  
  46. $periods['15mins'] = array( 'interval' => 900, 'display' => __('Every 15 Mins', 'membership') ); 
  47.  
  48. return $periods; 
  49.  
  50. function set_up_schedule() { 
  51.  
  52. if ( !wp_next_scheduled( 'membership_perform_cron_processes_quarterhourly' ) ) { 
  53. wp_schedule_event(time(), '15mins', 'membership_perform_cron_processes_quarterhourly'); 
  54.  
  55. if ( !wp_next_scheduled( 'membership_perform_cron_processes_hourly' ) ) { 
  56. wp_schedule_event(time(), 'hourly', 'membership_perform_cron_processes_hourly'); 
  57.  
  58.  
  59. function get_expiring_relationships_count() { 
  60.  
  61. $sql = $this->db->prepare( "SELECT count(*) FROM {$this->membership_relationships} WHERE sub_id != 0 AND expirydate <= %s ORDER BY expirydate ASC", gmdate( 'Y-m-d H:i:s', time() ) ); 
  62.  
  63. $result = $this->db->get_var( $sql ); 
  64.  
  65. if(empty($result)) { 
  66. return 0; 
  67. } else { 
  68. return $result; 
  69.  
  70.  
  71. function get_expiring_relationships() { 
  72.  
  73. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_relationships} WHERE sub_id != 0 AND expirydate <= %s ORDER BY expirydate ASC LIMIT 0, 25", gmdate( 'Y-m-d H:i:s', time() ) ); 
  74.  
  75. $result = $this->db->get_results( $sql ); 
  76.  
  77. return $result; 
  78.  
  79.  
  80. function transition_user_through_subscriptions() { 
  81.  
  82. $relationships = $this->get_expiring_relationships(); 
  83.  
  84. if( !empty($relationships) ) { 
  85.  
  86. foreach( $relationships as $rel ) { 
  87.  
  88. // Just creating a membership record for this user should automatically 
  89. // start the transition through the subscription 
  90. $member = new M_Membership( $rel->user_id ); 
  91.  
  92.  
  93.  
  94.  
  95. // Quearter hourly processing 
  96. function membership_perform_cron_processes_quarterhourly() { 
  97.  
  98. do_action( 'membership_process_quarterhourly_cron' ); 
  99.  
  100.  
  101. // Hourly processing 
  102. function membership_perform_cron_processes_hourly() { 
  103.  
  104. do_action( 'membership_process_hourly_cron' ); 
  105.  
  106.