| 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.1.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 |
PaypalDB::maybe_create_tables(); |
| 87 |
|
| 88 |
update_option( 'subscrpt_db_version', self::DB_VERSION ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Create the daily stats snapshot table. |
| 93 |
* |
| 94 |
* One row per calendar day holding the subscription status counts and the |
| 95 |
* total monthly recurring revenue (MRR) at snapshot time. Powers MRR/ |
| 96 |
* subscription "over time" charts in reports and the recovery report. |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function create_stats_snapshot_table() { |
| 101 |
global $wpdb; |
| 102 |
|
| 103 |
$charset_collate = $wpdb->get_charset_collate(); |
| 104 |
$table_name = $wpdb->prefix . 'subscrpt_stats_snapshot'; |
| 105 |
|
| 106 |
$schema = "CREATE TABLE IF NOT EXISTS `{$table_name}` ( |
| 107 |
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 108 |
`snapshot_date` DATE NOT NULL, |
| 109 |
`active_count` INT(11) NOT NULL DEFAULT 0, |
| 110 |
`pending_count` INT(11) NOT NULL DEFAULT 0, |
| 111 |
`on_hold_count` INT(11) NOT NULL DEFAULT 0, |
| 112 |
`cancelled_count` INT(11) NOT NULL DEFAULT 0, |
| 113 |
`expired_count` INT(11) NOT NULL DEFAULT 0, |
| 114 |
`pe_cancelled_count` INT(11) NOT NULL DEFAULT 0, |
| 115 |
`active_mrr` DECIMAL(14,2) NOT NULL DEFAULT 0, |
| 116 |
`created_at` DATETIME NOT NULL, |
| 117 |
PRIMARY KEY (`id`), |
| 118 |
UNIQUE KEY `snapshot_date` (`snapshot_date`) |
| 119 |
) $charset_collate"; |
| 120 |
|
| 121 |
dbDelta( $schema ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Create histories table |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function create_histories_table() { |
| 130 |
global $wpdb; |
| 131 |
|
| 132 |
$charset_collate = $wpdb->get_charset_collate(); |
| 133 |
$table_name = $wpdb->prefix . 'subscrpt_order_relation'; |
| 134 |
|
| 135 |
$schema = "CREATE TABLE IF NOT EXISTS `{$table_name}` ( |
| 136 |
`id` INT(255) NOT NULL AUTO_INCREMENT, |
| 137 |
`subscription_id` INT(100) NOT NULL, |
| 138 |
`order_id` INT(100) NOT NULL, |
| 139 |
`order_item_id` INT(100) NOT NULL, |
| 140 |
`type` VARCHAR(50) NOT NULL, |
| 141 |
PRIMARY KEY (`id`) |
| 142 |
) $charset_collate"; |
| 143 |
|
| 144 |
dbDelta( $schema ); |
| 145 |
} |
| 146 |
} |
| 147 |
|