/**
 * Pre process authenticate, to hook realtime transaction checking
 *
 * @param int user id
 * @return none
 */
function mgm_pre_authenticate_user($user_id)
{
    // get member
    $member = mgm_get_member($user_id);
    // check
    $checked = false;
    // skip auto login
    if (defined('MGM_DOING_REGISTER_AUTO_LOGIN')) {
        return $checked;
    }
    // skip is disabled
    if (isset($member->last_payment_check) && $member->last_payment_check == 'disabled') {
        return $checked;
    }
    // check status is not active OR last payment check not done OR last payment check date is not TODAY
    if ($member->status != MGM_STATUS_ACTIVE || !isset($member->last_payment_check_date) || isset($member->last_payment_check_date) && $member->last_payment_check_date != date('Y-m-d')) {
        // define
        if (!defined('DOING_QUERY_REBILL_STATUS')) {
            define('DOING_QUERY_REBILL_STATUS', 'login');
        }
        // check
        if (apply_filters('mgm_module_rebill_status', $user_id, $member)) {
            $checked = true;
        }
        // update
        if ($checked) {
            mgm_update_payment_check_state($user_id, 'login');
        }
    }
    // IF user package is active and expiry date is over, reassign different pack as per package settings
    // This happens when user tries login for the first time after expiry date reached
    // Issue #1044
    if ($member->status == MGM_STATUS_ACTIVE && !empty($member->expire_date) && strtotime($member->expire_date) < time()) {
        apply_filters('mgm_reassign_member_subscription', $user_id, $member, 'EXPIRE', false);
    }
    // return
    return $checked;
}
/**
 * check membership rebill status
 * @todo should only run on users who are using a payment module with rebill support
 * add _mgm_module_has_rebill_status_check='Y' in usermeta
 */
function mgm_check_membership_rebill_status()
{
    global $wpdb;
    // flag
    $paged_fetch = false;
    // check user with meta only
    // if( ($a_users = mgm_get_users_with_meta('_mgm_module_has_rebill_status_check','Y')) === FALSE ){
    // 		// old fetch
    // 		// $a_users = mgm_get_users_for_rebill_status_check('fetch');
    // 		$a_users = mgm_get_users_for_cron_check('fetch', 'rebill_status');
    // 		// flag
    // 		$paged_fetch = true;
    // 	}
    // fetch on meta, with rebill check module only
    $a_users = mgm_get_users_with_meta('_mgm_module_has_rebill_status_check', 'Y');
    // current time
    $current_date = mgm_get_current_datetime('Y-m-d H:i:s');
    // with time part #1023 issue
    // log
    // mgm_log($a_users, __FUNCTION__);
    // loop
    if ($a_users) {
        // objects
        $spacks_obj = mgm_get_class('subscription_packs');
        // $mtypes_obj = mgm_get_class('membership_types');
        $duration_exprs = $spacks_obj->get_duration_exprs();
        $checked = $offset = 0;
        // define
        if (!defined('DOING_QUERY_REBILL_STATUS')) {
            define('DOING_QUERY_REBILL_STATUS', 'cron');
        }
        // loop
        foreach ($a_users as $user) {
            // set limit
            @set_time_limit(300);
            //300s
            @ini_set('memory_limit', 134217728);
            // 128M
            // get member
            $member = mgm_get_member($user->ID);
            // get rebill cycle
            if ((int) $member->pack_id > 0) {
                // get member subscribed  pack
                $pack = $spacks_obj->get_pack($member->pack_id);
                // member pack cycle
                $num_cycles = isset($member->active_num_cycles) && !empty($member->active_num_cycles) ? (int) $member->active_num_cycles : (int) $pack['num_cycles'];
                // lifetime ongoing/fixed cycle ongoing
                if ($num_cycles == 0 || $num_cycles > 1 && (int) $member->rebilled < (int) $num_cycles) {
                    // 100 to dynamic
                    // when current date is later than expire date, we will run rebill check after expire date, not before
                    // payment status available after scheduled transaction date
                    if (strtotime($current_date['date']) > strtotime($member->expire_date)) {
                        // check already run
                        if (!isset($member->last_payment_check_date) || isset($member->last_payment_check_date) && $current_date['date'] != $member->last_payment_check_date) {
                            // apply rebill filter
                            if (apply_filters('mgm_module_rebill_status', $user->ID, $member)) {
                                // success
                                $checked++;
                            }
                        }
                        // update type
                        mgm_update_payment_check_state($user->ID, 'cron');
                    }
                }
            }
            // unset
            unset($member);
            // increase $offset
            $offset++;
        }
    }
    // update
    if ($paged_fetch) {
        // mgm_get_users_for_rebill_status_check('update', $offset);
        mgm_get_users_for_cron_check('update', 'rebill_status', $offset);
    }
}
 private function _members_check_rebill_status()
 {
     global $current_user, $wpdb;
     // extract
     extract($_POST);
     // init
     $message = __('Member rebill status check failed', 'mgm');
     $status = 'error';
     // check
     if (isset($current_user->ID) && $current_user->ID != 0 && is_numeric($current_user->ID)) {
         // users check
         if (isset($members) && !empty($members)) {
             // define
             if (!defined('DOING_QUERY_REBILL_STATUS')) {
                 define('DOING_QUERY_REBILL_STATUS', 'manual');
             }
             // ctr
             $updated = 0;
             // loop users
             foreach ($members as $uid) {
                 //delete user
                 $uid = mgm_escape($uid);
                 // check
                 if ((int) $uid > 0) {
                     // get user
                     $user = new WP_User($uid);
                     // check
                     if (isset($user->ID) && (int) $user->ID > 0) {
                         // member data
                         $member = mgm_get_member($user->ID);
                         // reset disabled, force recheck
                         $member->last_payment_check = '';
                         // check
                         if (apply_filters('mgm_module_rebill_status', $user->ID, $member)) {
                             // update
                             $updated++;
                         }
                         // update
                         mgm_update_payment_check_state($user->ID, 'manual');
                     }
                     // unset
                     unset($user, $member);
                 }
             }
             // message
             $item = $updated > 1 ? 'members' : 'member';
             // set
             if (!$updated) {
                 $message = sprintf(__('Error while updating %s rebill status.', 'mgm'), $item);
             } elseif ($updated && count($members) != $updated) {
                 $message = sprintf(__('Partially updated rebill status of %d %s .', 'mgm'), $updated, $item);
             } else {
                 $message = sprintf(__('Successfully updated rebill status of %d %s.', 'mgm'), $updated, $item);
                 $status = 'success';
             }
         }
     }
     // return
     return array('status' => $status, 'message' => $message);
 }