| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Gateways\Paypal\PaypalDB; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Installer |
| 9 |
* |
| 10 |
* @package SpringDevs\Subscription |
| 11 |
*/ |
| 12 |
class Installer { |
| 13 |
|
| 14 |
/** |
| 15 |
* Database schema version. Bump whenever a custom table changes. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
const DB_VERSION = '1.2.0'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Run the installer |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function run() { |
| 27 |
$this->add_version(); |
| 28 |
$this->register_schedules(); |
| 29 |
$this->create_tables(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Create/upgrade custom tables when the stored schema version is outdated. |
| 34 |
* |
| 35 |
* Lets existing installs pick up new tables on update without a manual |
| 36 |
* deactivate/reactivate. A single option read short-circuits once current. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public static function maybe_upgrade() { |
| 41 |
if ( get_option( 'subscrpt_db_version' ) === self::DB_VERSION ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
( new self() )->create_tables(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Add time and version on DB |
| 50 |
*/ |
| 51 |
public function add_version() { |
| 52 |
$installed = get_option( 'subscrpt_installed' ); |
| 53 |
|
| 54 |
if ( ! $installed ) { |
| 55 |
update_option( 'subscrpt_installed', time() ); |
| 56 |
} |
| 57 |
|
| 58 |
update_option( 'subscrpt_version', SUBSCRPT_VERSION ); |
| 59 |
|
| 60 |
update_option( 'subscrpt_manual_renew_cart_notice', 'Subscriptional product added to cart. Please complete the checkout to renew subscription.' ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register cron events. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function register_schedules() { |
| 69 |
if ( ! wp_next_scheduled( 'subscrpt_hourly_cron' ) ) { |
| 70 |
wp_schedule_event( strtotime( 'tomorrow midnight' ), 'hourly', 'subscrpt_hourly_cron' ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Create necessary database tables |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function create_tables() { |
| 80 |
if ( ! function_exists( 'dbDelta' ) ) { |
| 81 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 82 |
} |
| 83 |
|
| 84 |
$this->create_histories_table(); |
| 85 |
$this->create_stats_snapshot_table(); |
| 86 |
$this->create_cancellation_feedback_table(); |
| 87 |
PaypalDB::maybe_create_tables(); |
| 88 |
|
| 89 |
update_option( 'subscrpt_db_version', self::DB_VERSION ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Create the daily stats snapshot table. |
| 94 |
* |
| 95 |
* One row per calendar day holding the subscription status counts and the |
| 96 |
* total monthly recurring revenue (MRR) at snapshot time. Powers MRR/ |
| 97 |
* subscription "over time" charts in reports and the recovery report. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function create_stats_snapshot_table() { |
| 102 |
global $wpdb; |
| 103 |
|
| 104 |
$charset_collate = $wpdb->get_charset_collate(); |
| 105 |
$table_name = $wpdb->prefix . 'subscrpt_stats_snapshot'; |
| 106 |
|
| 107 |
$schema = "CREATE TABLE IF NOT EXISTS `{$table_name}` ( |
| 108 |
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 109 |
`snapshot_date` DATE NOT NULL, |
| 110 |
`active_count` INT(11) NOT NULL DEFAULT 0, |
| 111 |
`pending_count` INT(11) NOT NULL DEFAULT 0, |
| 112 |
`on_hold_count` INT(11) NOT NULL DEFAULT 0, |
| 113 |
`cancelled_count` INT(11) NOT NULL DEFAULT 0, |
| 114 |
`expired_count` INT(11) NOT NULL DEFAULT 0, |
| 115 |
`pe_cancelled_count` INT(11) NOT NULL DEFAULT 0, |
| 116 |
`active_mrr` DECIMAL(14,2) NOT NULL DEFAULT 0, |
| 117 |
`created_at` DATETIME NOT NULL, |
| 118 |
PRIMARY KEY (`id`), |
| 119 |
UNIQUE KEY `snapshot_date` (`snapshot_date`) |
| 120 |
) $charset_collate"; |
| 121 |
|
| 122 |
dbDelta( $schema ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Create the cancellation survey table. |
| 127 |
* |
| 128 |
* One row per subscription — the latest cancellation survey (a re-cancel after |
| 129 |
* reactivation overwrites the previous row rather than accumulating a log, so |
| 130 |
* churn-by-reason reports never double-count a subscription). Stores the |
| 131 |
* customer's stated reason (key + a label snapshot that survives later reason |
| 132 |
* edits/deletes) and optional comment. Consumed for churn tracking — the recovery |
| 133 |
* plugin joins its recovery log to this table on subscription_id. |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function create_cancellation_feedback_table() { |
| 138 |
global $wpdb; |
| 139 |
|
| 140 |
$charset_collate = $wpdb->get_charset_collate(); |
| 141 |
$table_name = $wpdb->prefix . 'subscrpt_cancellation_feedback'; |
| 142 |
|
| 143 |
$schema = "CREATE TABLE IF NOT EXISTS `{$table_name}` ( |
| 144 |
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 145 |
`subscription_id` BIGINT(20) UNSIGNED NOT NULL, |
| 146 |
`customer_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, |
| 147 |
`reason_key` VARCHAR(60) NOT NULL DEFAULT '', |
| 148 |
`reason_label` VARCHAR(191) NOT NULL DEFAULT '', |
| 149 |
`comment` TEXT NULL, |
| 150 |
`created_at` DATETIME NOT NULL, |
| 151 |
PRIMARY KEY (`id`), |
| 152 |
KEY `subscription_id` (`subscription_id`), |
| 153 |
KEY `created_at` (`created_at`) |
| 154 |
) $charset_collate"; |
| 155 |
|
| 156 |
dbDelta( $schema ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Create histories table |
| 161 |
* |
| 162 |
* @return void |
| 163 |
*/ |
| 164 |
public function create_histories_table() { |
| 165 |
global $wpdb; |
| 166 |
|
| 167 |
$charset_collate = $wpdb->get_charset_collate(); |
| 168 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 169 |
|
| 170 |
$schema = "CREATE TABLE IF NOT EXISTS `{$table_name}` ( |
| 171 |
`id` INT(255) NOT NULL AUTO_INCREMENT, |
| 172 |
`subscription_id` INT(100) NOT NULL, |
| 173 |
`order_id` INT(100) NOT NULL, |
| 174 |
`order_item_id` INT(100) NOT NULL, |
| 175 |
`type` VARCHAR(50) NOT NULL, |
| 176 |
PRIMARY KEY (`id`) |
| 177 |
) $charset_collate"; |
| 178 |
|
| 179 |
dbDelta( $schema ); |
| 180 |
} |
| 181 |
} |
| 182 |
|