Example #1
0
function ihc_update_user_level_expire($level_data, $l_id, $u_id)
{
    /*  
     * update expire level for a user with the right expire time
     * user this only when user has made the payment
     * @param:
     * - array with level metas
     * - level id
     * - user id
     * @return none
     */
    $current_time = time();
    if (empty($level_data['access_type'])) {
        $level_data['access_type'] = 'unlimited';
    }
    //set end time
    switch ($level_data['access_type']) {
        case 'unlimited':
            $end_time = strtotime('+10 years', $current_time);
            //unlimited will be ten years
            break;
        case 'limited':
            if (!empty($level_data['access_limited_time_type']) && !empty($level_data['access_limited_time_value'])) {
                $multiply = ihc_get_multiply_time_value($level_data['access_limited_time_type']);
                $end_time = $current_time + $multiply * $level_data['access_limited_time_value'];
            }
            break;
        case 'date_interval':
            if (!empty($level_data['access_interval_end'])) {
                $end_time = $level_data['access_interval_end'];
            }
            break;
        case 'regular_period':
            if (!empty($level_data['access_regular_time_type']) && !empty($level_data['access_regular_time_value'])) {
                $multiply = ihc_get_multiply_time_value($level_data['access_regular_time_type']);
                $end_time = $current_time + $multiply * $level_data['access_regular_time_value'];
            }
            break;
    }
    $update_time = date('Y-m-d H:i:s', $current_time);
    $end_time = date('Y-m-d H:i:s', $end_time);
    global $wpdb;
    $table = $wpdb->prefix . 'ihc_user_levels';
    $q = 'UPDATE ' . $table . ' SET update_time="' . $update_time . '", expire_time="' . $end_time . '", status=1 WHERE user_id="' . $u_id . '" AND level_id="' . $l_id . '";';
    $wpdb->query($q);
}
        private function handle_levels_assign($request_levels)
        {
            /*  
             * insert into db when user was start using this level, 
             * if it's free level will assign the expire date, 
             * if not paypal ipn will set the expire time
             * @param string with all level ids separated by comma
             * @return none
             */
            if ($request_levels != -1 && $request_levels !== FALSE) {
                $current_levels = explode(',', $request_levels);
                if (count($current_levels)) {
                    $old_levels = get_user_meta($this->user_id, 'ihc_user_levels', true);
                    foreach ($current_levels as $lid) {
                        if (strpos($old_levels, $lid) === FALSE) {
                            //we got a new level to assign
                            $level_data = ihc_get_level_by_id($lid);
                            //getting details about current level
                            $current_time = time();
                            if (empty($level_data['access_type'])) {
                                $level_data['access_type'] = 'unlimited';
                            }
                            //set start time
                            if ($level_data['access_type'] == 'date_interval' && !empty($level_data['access_interval_start'])) {
                                $start_time = $level_data['access_interval_start'];
                            } else {
                                $start_time = $current_time;
                            }
                            //set end time
                            if ($this->is_public && $level_data['payment_type'] != 'free') {
                                //end time will be expired, updated when payment
                                $end_time = '0000-00-00 00:00:00';
                            } else {
                                //it's admin or free so we set the correct expire time
                                switch ($level_data['access_type']) {
                                    case 'unlimited':
                                        $end_time = strtotime('+10 years', $current_time);
                                        //unlimited will be ten years
                                        break;
                                    case 'limited':
                                        if (!empty($level_data['access_limited_time_type']) && !empty($level_data['access_limited_time_value'])) {
                                            $multiply = ihc_get_multiply_time_value($level_data['access_limited_time_type']);
                                            $end_time = $current_time + $multiply * $level_data['access_limited_time_value'];
                                        }
                                        break;
                                    case 'date_interval':
                                        if (!empty($level_data['access_interval_end'])) {
                                            $end_time = $level_data['access_interval_end'];
                                        }
                                        break;
                                    case 'regular_period':
                                        if (!empty($level_data['access_regular_time_type']) && !empty($level_data['access_regular_time_value'])) {
                                            $multiply = ihc_get_multiply_time_value($level_data['access_regular_time_type']);
                                            $end_time = $current_time + $multiply * $level_data['access_regular_time_value'];
                                        }
                                        break;
                                }
                                $end_time = date('Y-m-d H:i:s', $end_time);
                            }
                            $update_time = date('Y-m-d H:i:s', $current_time);
                            $start_time = date('Y-m-d H:i:s', $start_time);
                            global $wpdb;
                            $table = $wpdb->prefix . 'ihc_user_levels';
                            $exists = $wpdb->get_row('SELECT * FROM ' . $table . ' WHERE user_id="' . $this->user_id . '" AND level_id="' . $lid . '";');
                            if (!empty($exists)) {
                                $wpdb->query('DELETE FROM ' . $table . ' WHERE user_id="' . $this->user_id . '" AND level_id="' . $lid . '";');
                                //assure that pair user_id - level_id entry is not exists
                            }
                            $wpdb->query('INSERT INTO ' . $table . '
												VALUES(null, "' . $this->user_id . '", "' . $lid . '", "' . $start_time . '", "' . $update_time . '", "' . $end_time . '", 1);');
                        }
                    }
                }
            }
        }