ActivatorService.php 5.85 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

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

Michael Iseard's avatar
Michael Iseard committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * 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
 */
26
class ActivatorService {
Michael Iseard's avatar
Michael Iseard committed
27
28

	/**
29
	 * Runs all activation functions.
Michael Iseard's avatar
Michael Iseard committed
30
	 *
31
	 * @param string|null $old_version Previous version of plugin.
Michael Iseard's avatar
Michael Iseard committed
32
	 *
Michael Iseard's avatar
Michael Iseard committed
33
34
	 * @since    1.0.0
	 */
Michael Iseard's avatar
Michael Iseard committed
35
	public static function activate( $old_version = null ) {
36

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

42
43
		if ( $old_version ) {

Michael Iseard's avatar
Michael Iseard committed
44
45
			if ( version_compare( $old_version, '2.3.0', '<' ) ) {
				global $wpdb;
46
47
48
49

				// Rename setting
				$transaction_table = TransactionEntity::get_table_name();
				$wpdb->query( "ALTER TABLE $transaction_table RENAME COLUMN `campaign_label` TO `campaign_id`" );
Michael Iseard's avatar
Michael Iseard committed
50
				Settings::update_setting( 'show_intro', 1 );
51
52
53
54
55
56
57
58
59
60
61
62

				// Apply mode to Donors
				$mapper = new MapperService(DonorEntity::class);
				$donors = $mapper->get_all_by();
				/** @var DonorEntity $donor */
				foreach ($donors as $donor) {
					$transactions = $donor->get_transactions();
					if($transactions) {
						$donor->set_fields(['mode' => $transactions[0]->mode]);
					}
					$mapper->save($donor);
				}
Michael Iseard's avatar
Michael Iseard committed
63
64
			}

Michael Iseard's avatar
Michael Iseard committed
65
			if ( version_compare( $old_version, '2.2.0', '<' ) ) {
Michael Iseard's avatar
Michael Iseard committed
66
67
				$link = Settings::get_setting( 'privacy_link' );
				Settings::remove_setting( 'subscription_enabled' );
68

Michael Iseard's avatar
Michael Iseard committed
69
70
71
				if ( $link ) {
					Settings::update_setting( 'terms_link', $link );
					Settings::remove_setting( 'privacy_link' );
72
73
74
				}
			}

75
76
			if ( version_compare( $old_version, '2.1.1', '<' ) ) {
				$logger->info( 'Upgrading to version 2.1.1', [ 'previous_version' => $old_version ] );
Michael Iseard's avatar
Michael Iseard committed
77
				Settings::remove_setting( 'action_scheduler' );
78
79
			}

80
81
		}

Michael Iseard's avatar
Michael Iseard committed
82
83
84
85
86
		self::create_donors_table();
		self::create_transactions_table();
		self::create_subscriptions_table();
		self::set_defaults();

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

89
90
91
	}

	/**
92
	 * Creates the donors table
93
	 *
94
	 * @since    1.1.0
95
	 */
96
	private static function create_donors_table() {
97

Michael Iseard's avatar
Michael Iseard committed
98
99
100
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
101
		$table_name      = DonorEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
102
103
104

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
105
106
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
107
108
109
110
111
112
		  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
113
		  customer_id VARCHAR(255),
114
		  secret VARCHAR(255),
115
		  mode VARCHAR(45) NOT NULL,
116
		  PRIMARY KEY (id)
117
		) $charset_collate";
Michael Iseard's avatar
Michael Iseard committed
118

119
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
120
		dbDelta( $sql );
121

Michael Iseard's avatar
Michael Iseard committed
122
123
	}

124
	/**
125
	 * Creates the transactions table
126
	 *
127
	 * @since    1.0.0
128
	 */
129
	private static function create_transactions_table() {
130

131
132
133
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
134
		$table_name      = TransactionEntity::get_table_name();
135
136
137

		$sql = "CREATE TABLE $table_name (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
138
139
		  created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		  last_updated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
140
141
142
143
144
145
		  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
146
		  customer_id varchar(255) NOT NULL,
147
148
149
150
		  order_id VARCHAR(255) NOT NULL,
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  refunds BLOB DEFAULT NULL,
151
		  campaign_id VARCHAR(255),
152
		  secret VARCHAR(255),
153
		  PRIMARY KEY (id)
154
		) $charset_collate;";
155

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

159
160
	}

Michael Iseard's avatar
Michael Iseard committed
161
162
163
164
165
166
	/**
	 * Creates the subscription table
	 *
	 * @since    1.1.0
	 */
	private static function create_subscriptions_table() {
167

Michael Iseard's avatar
Michael Iseard committed
168
		global $wpdb;
Michael Iseard's avatar
Michael Iseard committed
169

Michael Iseard's avatar
Michael Iseard committed
170
		$charset_collate = $wpdb->get_charset_collate();
171
		$table_name      = SubscriptionEntity::get_table_name();
Michael Iseard's avatar
Michael Iseard committed
172

Michael Iseard's avatar
Michael Iseard committed
173
		$sql = "CREATE TABLE $table_name (
174
		  id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
175
176
          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
177
178
179
		  value DECIMAL(7,2) NOT NULL,
		  currency VARCHAR(255),
		  frequency VARCHAR(255) NOT NULL,
180
		  years MEDIUMINT(2) NOT NULL,
Michael Iseard's avatar
Michael Iseard committed
181
182
183
184
		  customer_id VARCHAR(255),
		  transaction_id VARCHAR(255),
		  subscription_id VARCHAR(255),
		  status VARCHAR(255),
185
		  secret VARCHAR(255),		  
Michael Iseard's avatar
Michael Iseard committed
186
187
188
		  PRIMARY KEY (id)
		) $charset_collate";

189
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Michael Iseard's avatar
Michael Iseard committed
190
		dbDelta( $sql );
191

Michael Iseard's avatar
Michael Iseard committed
192
	}
Michael Iseard's avatar
Michael Iseard committed
193

Michael Iseard's avatar
Michael Iseard committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	/**
	 * Adds default options if not already set
	 *
	 * @since    1.0.0
	 */
	private static function set_defaults() {

		update_option( '_kudos_donations_version', KUDOS_VERSION );

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

		$campaigns = new Campaigns();
		$campaigns->add_default();

	}

Michael Iseard's avatar
Michael Iseard committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	/**
	 * Creates the subscription table
	 *
	 * @since    1.1.0
	 */
	private static function create_campaigns_table() {

		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();
		$table_name      = CampaignEntity::get_table_name();

		$sql = "CREATE TABLE $table_name (
	      id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
		  slug VARCHAR(255),
		  name VARCHAR(255),
		  modal_title VARCHAR(255),
		  welcome_text VARCHAR(255),
		  donation_type VARCHAR(255),
		  amount_type VARCHAR(255),
		  fixed_amounts VARCHAR(255),
		  protected BIT,
	      secret VARCHAR(255),
	      PRIMARY KEY (id)
		) $charset_collate";

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

	}
Michael Iseard's avatar
Michael Iseard committed
241
}