ActivatorService.php 4.12 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
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
Michael Iseard's avatar
Michael Iseard committed
8
use Kudos\Helpers\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
	 * @param string|null $old_version Previous version of plugin.
Michael Iseard's avatar
Michael Iseard committed
30
	 *
Michael Iseard's avatar
Michael Iseard committed
31
32
	 * @since    1.0.0
	 */
Michael Iseard's avatar
Michael Iseard committed
33
	public static function activate( $old_version = null ) {
34

Michael Iseard's avatar
Michael Iseard committed
35
36
		$logger = new LoggerService();
		$logger->init();
37
		$twig = new TwigService();
Michael Iseard's avatar
Michael Iseard committed
38
39
		$twig->init();

40
		self::create_donors_table();
41
		self::create_transactions_table();
Michael Iseard's avatar
Michael Iseard committed
42
		self::create_subscriptions_table();
43
		self::set_defaults();
44

Michael Iseard's avatar
Michael Iseard committed
45
46
		if ( $old_version && version_compare( $old_version, '2.0.4', '<' ) ) {
			$logger->info( 'Upgrading to version 2.0.4', [ 'previous_version' => $old_version ] );
47
			$result = UpdateService::sync_campaign_labels();
Michael Iseard's avatar
Michael Iseard committed
48
49
50
			if ( $result ) {
				$logger->info( 'Updated campaign labels from transactions' );
			}
51
52
		}

Michael Iseard's avatar
Michael Iseard committed
53
		$logger->info( 'Kudos Donations plugin activated' );
Michael Iseard's avatar
Michael Iseard committed
54

55
56
57
	}

	/**
58
	 * Creates the donors table
59
	 *
60
	 * @since    1.1.0
61
	 */
62
	private static function create_donors_table() {
63

Michael Iseard's avatar
Michael Iseard committed
64
65
66
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
67
		$table_name      = DonorEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
68
69
70

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
71
72
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
73
74
75
76
77
78
		  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
79
		  customer_id VARCHAR(255),
80
		  secret VARCHAR(255),
81
		  PRIMARY KEY (id)
82
		) $charset_collate";
Michael Iseard's avatar
Michael Iseard committed
83

84
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
85
		dbDelta( $sql );
86

Michael Iseard's avatar
Michael Iseard committed
87
88
	}

89
	/**
90
	 * Creates the transactions table
91
	 *
92
	 * @since    1.0.0
93
	 */
94
	private static function create_transactions_table() {
95

96
97
98
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
99
		$table_name      = TransactionEntity::get_table_name();
100
101
102

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
103
104
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
105
106
107
108
109
110
		  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
111
		  customer_id varchar(255) NOT NULL,
112
113
114
115
		  order_id VARCHAR(255) NOT NULL,
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  refunds BLOB DEFAULT NULL,
116
		  campaign_label VARCHAR(255),
117
		  secret VARCHAR(255),
118
		  PRIMARY KEY (id)
119
		) $charset_collate;";
120

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

124
125
	}

Michael Iseard's avatar
Michael Iseard committed
126
127
128
129
130
131
	/**
	 * Creates the subscription table
	 *
	 * @since    1.1.0
	 */
	private static function create_subscriptions_table() {
132

Michael Iseard's avatar
Michael Iseard committed
133
		global $wpdb;
Michael Iseard's avatar
Michael Iseard committed
134

Michael Iseard's avatar
Michael Iseard committed
135
		$charset_collate = $wpdb->get_charset_collate();
136
		$table_name      = SubscriptionEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
137

Michael Iseard's avatar
Michael Iseard committed
138
		$sql = "CREATE TABLE $table_name (
139
		  id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
140
141
          created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
          last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
142
143
144
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  frequency VARCHAR(255) NOT NULL,
145
		  years MEDIUMINT(2) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
146
147
148
149
		  customer_id VARCHAR(255),
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  status VARCHAR(255),
150
		  secret VARCHAR(255),		  
Michael Iseard's avatar
Michael Iseard committed
151
152
153
		  PRIMARY KEY (id)
		) $charset_collate";

154
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
155
		dbDelta( $sql );
156

Michael Iseard's avatar
Michael Iseard committed
157
	}
Michael Iseard's avatar
Michael Iseard committed
158

159
160
161
162
163
164
165
	/**
	 * Adds default options if not already set
	 *
	 * @since    1.0.0
	 */
	private static function set_defaults() {

Michael Iseard's avatar
Michael Iseard committed
166
		update_option( '_kudos_donations_version', KUDOS_VERSION );
167
168
169

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

171
	}
Michael Iseard's avatar
Michael Iseard committed
172
}