ActivatorService.php 3.74 KB
Newer Older
Michael Iseard's avatar
Michael Iseard committed
1
2
<?php

3
namespace Kudos\Service;
Michael Iseard's avatar
Michael Iseard committed
4

5
6
7
8
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
use Settings;
Michael Iseard's avatar
Michael Iseard committed
9

Michael Iseard's avatar
Michael Iseard committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Fired during plugin activation
 *
 * @link       https://www.linkedin.com/in/michael-iseard/
 * @since      1.0.0
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 */
24
class ActivatorService {
Michael Iseard's avatar
Michael Iseard committed
25
26

	/**
27
	 * Runs all activation functions
Michael Iseard's avatar
Michael Iseard committed
28
29
30
31
	 *
	 * @since    1.0.0
	 */
	public static function activate() {
32
33
34

		LoggerService::init();
		TwigService::init();
35
		self::create_donors_table();
36
		self::create_transactions_table();
Michael Iseard's avatar
Michael Iseard committed
37
		self::create_subscriptions_table();
38
		self::set_defaults();
39

40
41
42
	}

	/**
43
	 * Creates the donors table
44
	 *
45
	 * @since    1.1.0
46
	 */
47
	private static function create_donors_table() {
48

Michael Iseard's avatar
Michael Iseard committed
49
50
51
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
52
		$table_name = DonorEntity::getTableName();  //get the database table prefix to create my new table
Michael Iseard's avatar
Michael Iseard committed
53
54
55

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
56
57
58
59
60
61
62
63
		  created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
		  last_updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
		  email VARCHAR(320) NOT NULL,
		  name VARCHAR(255) NOT NULL,
		  street VARCHAR(255),
		  postcode VARCHAR(255),
		  city VARCHAR(255),
		  country VARCHAR(255),
Michael Iseard's avatar
Michael Iseard committed
64
		  customer_id VARCHAR(255),
65
		  secret VARCHAR(255),
66
		  PRIMARY KEY (id)
67
		) $charset_collate";
Michael Iseard's avatar
Michael Iseard committed
68

Michael Iseard's avatar
Michael Iseard committed
69
70
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $sql );
71

Michael Iseard's avatar
Michael Iseard committed
72
73
	}

74
	/**
75
	 * Creates the transactions table
76
	 *
77
	 * @since    1.0.0
78
	 */
79
	private static function create_transactions_table() {
80

81
82
83
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
84
		$table_name = TransactionEntity::getTableName();  //get the database table prefix to create my new table
85
86
87

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
88
89
90
91
92
93
94
95
		  created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
		  last_updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  status VARCHAR(255) DEFAULT 'open' NOT NULL,
		  method VARCHAR(255),
		  mode VARCHAR(255) NOT NULL,
		  sequence_type VARCHAR(255) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
96
		  customer_id varchar(255) NOT NULL,
97
98
99
100
101
		  order_id VARCHAR(255) NOT NULL,
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  refunds BLOB DEFAULT NULL,
		  donation_label VARCHAR(255),
102
		  PRIMARY KEY (id)
103
		) $charset_collate;";
104
105
106

		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $sql );
107

108
109
	}

Michael Iseard's avatar
Michael Iseard committed
110
111
112
113
114
115
    /**
     * Creates the subscription table
     *
     * @since    1.1.0
     */
    private static function create_subscriptions_table() {
116

Michael Iseard's avatar
Michael Iseard committed
117
118
119
        global $wpdb;

        $charset_collate = $wpdb->get_charset_collate();
120
        $table_name = SubscriptionEntity::getTableName();  //get the database table prefix to create my new table
Michael Iseard's avatar
Michael Iseard committed
121
122

        $sql = "CREATE TABLE $table_name (
123
		  id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
124
125
          created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
          last_updated datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
126
127
128
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  frequency VARCHAR(255) NOT NULL,
129
		  years MEDIUMINT(2) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
130
131
132
133
134
135
136
137
138
		  customer_id VARCHAR(255),
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  status VARCHAR(255),
		  PRIMARY KEY (id)
		) $charset_collate";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
139

Michael Iseard's avatar
Michael Iseard committed
140
141
    }

142
143
144
145
146
147
148
	/**
	 * Adds default options if not already set
	 *
	 * @since    1.0.0
	 */
	private static function set_defaults() {

149
		update_option('_kudos_donations_version', KUDOS_VERSION);
150
151
152

		$settings = new Settings();
		$settings->add_defaults();
153

154
	}
Michael Iseard's avatar
Michael Iseard committed
155
}