Passed
Push — master ( 883b53...6ec8f5 )
by Brian
04:13
created

WPInv_Subscriptions_DB::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Contains the subscriptions db class.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * The Subscriptions DB Class
11
 *
12
 * @since  1.0.0
13
 */
14
15
class WPInv_Subscriptions_DB extends Wpinv_DB {
16
17
    /**
18
     * Get things started
19
     *
20
     * @access  public
21
     * @since   1.0.0
22
     */
23
    public function __construct() {
24
25
        global $wpdb;
26
27
        $this->table_name  = $wpdb->prefix . 'wpinv_subscriptions';
28
        $this->primary_key = 'id';
29
        $this->version     = '1.0.0';
30
31
    }
32
33
    /**
34
     * Get columns and formats
35
     *
36
     * @access  public
37
     * @since   1.0.0
38
     */
39
    public function get_columns() {
40
        return array(
41
            'id'                => '%d',
42
            'customer_id'       => '%d',
43
            'frequency'         => '%d',
44
            'period'            => '%s',
45
            'initial_amount'    => '%s',
46
            'recurring_amount'  => '%s',
47
            'bill_times'        => '%d',
48
            'transaction_id'    => '%s',
49
            'parent_payment_id' => '%d',
50
            'product_id'        => '%d',
51
            'created'           => '%s',
52
            'expiration'        => '%s',
53
            'trial_period'      => '%s',
54
            'status'            => '%s',
55
            'profile_id'        => '%s',
56
        );
57
    }
58
59
    /**
60
     * Get default column values
61
     *
62
     * @access  public
63
     * @since   1.0.0
64
     */
65
    public function get_column_defaults() {
66
        return array(
67
            'customer_id'       => 0,
68
            'period'            => '',
69
            'initial_amount'    => '',
70
            'recurring_amount'  => '',
71
            'bill_times'        => 0,
72
            'transaction_id'    => '',
73
            'parent_payment_id' => 0,
74
            'product_id'        => 0,
75
            'created'           => date( 'Y-m-d H:i:s' ),
76
            'expiration'        => date( 'Y-m-d H:i:s' ),
77
            'trial_period'      => '',
78
            'status'            => '',
79
            'profile_id'        => '',
80
        );
81
    }
82
83
    /**
84
     * Retrieve all subscriptions for a customer
85
     *
86
     * @access  public
87
     * @since   1.0.0
88
     */
89
    public function get_subscriptions( $args = array() ) {
90
        return getpaid_get_subscriptions( $args );
91
    }
92
93
    /**
94
     * Count the total number of subscriptions in the database
95
     *
96
     * @access  public
97
     * @since   1.0.0
98
     */
99
    public function count( $args = array() ) {
100
        return getpaid_get_subscriptions( $args, 'count' );
101
    }
102
103
    /**
104
     * Create the table
105
     *
106
     * @access  public
107
     * @since   1.0.0
108
     */
109
    public function create_table() {
110
111
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
112
113
        $sql = "CREATE TABLE " . $this->table_name . " (
114
        id bigint(20) NOT NULL AUTO_INCREMENT,
115
        customer_id bigint(20) NOT NULL,
116
        frequency int(11) NOT NULL DEFAULT '1',
117
        period varchar(20) NOT NULL,
118
        initial_amount mediumtext NOT NULL,
119
        recurring_amount mediumtext NOT NULL,
120
        bill_times bigint(20) NOT NULL,
121
        transaction_id varchar(60) NOT NULL,
122
        parent_payment_id bigint(20) NOT NULL,
123
        product_id bigint(20) NOT NULL,
124
        created datetime NOT NULL,
125
        expiration datetime NOT NULL,
126
        trial_period varchar(20) NOT NULL,
127
        status varchar(20) NOT NULL,
128
        profile_id varchar(60) NOT NULL,
129
        PRIMARY KEY  (id),
130
        KEY profile_id (profile_id),
131
        KEY customer (customer_id),
132
        KEY transaction (transaction_id),
133
        KEY customer_and_status ( customer_id, status)
134
        ) CHARACTER SET utf8 COLLATE utf8_general_ci;";
135
136
        dbDelta( $sql );
137
138
        update_option( $this->table_name . '_db_version', $this->version );
139
    }
140
141
}