| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite; |
| 4 |
|
| 5 |
/** |
| 6 |
* Exit if accessed directly |
| 7 |
*/ |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Installer class |
| 14 |
* |
| 15 |
* @since 0.0.1 |
| 16 |
*/ |
| 17 |
|
| 18 |
class Installer extends Controller { |
| 19 |
|
| 20 |
/** |
| 21 |
* Run the installer |
| 22 |
* |
| 23 |
* @return void |
| 24 |
* @since 0.0.1 |
| 25 |
*/ |
| 26 |
public function run() { |
| 27 |
$this->create_tables(); |
| 28 |
self::enable_setup_wizard(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Table creation schema |
| 33 |
* |
| 34 |
* @since 0.0.1 |
| 35 |
*/ |
| 36 |
private function get_schema() { |
| 37 |
global $wpdb; |
| 38 |
return [ |
| 39 |
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}better_payment( |
| 40 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 41 |
order_id varchar(50) NOT NULL, |
| 42 |
transaction_id varchar(50) DEFAULT '', |
| 43 |
amount decimal(10,2) NOT NULL, |
| 44 |
status varchar(50) DEFAULT NULL, |
| 45 |
source varchar(50) NOT NULL, |
| 46 |
payment_date datetime DEFAULT NULL, |
| 47 |
email varchar(50) NOT NULL DEFAULT '', |
| 48 |
customer_info longtext, |
| 49 |
form_fields_info longtext, |
| 50 |
currency varchar(11) NOT NULL DEFAULT '', |
| 51 |
referer varchar(64) DEFAULT NULL, |
| 52 |
obj_id text, |
| 53 |
PRIMARY KEY (id), |
| 54 |
KEY order_id (order_id), |
| 55 |
KEY status (status) |
| 56 |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ", |
| 57 |
|
| 58 |
// E-commerce subscription <-> order relation. The `source` column |
| 59 |
// names the e-commerce integration that owns the row: woo, |
| 60 |
// fluentcart, surecart, ... |
| 61 |
// E-commerce data ONLY — Better Payment's own (Elementor/campaign) |
| 62 |
// subscription payments never write here. All reads/writes go through |
| 63 |
// Models\SubscriptionRelationModel. |
| 64 |
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}better_payment_subscription_order( |
| 65 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 66 |
subscription_id bigint(20) unsigned NOT NULL, |
| 67 |
order_id bigint(20) unsigned NOT NULL, |
| 68 |
order_item_id bigint(20) unsigned NOT NULL DEFAULT 0, |
| 69 |
type varchar(20) NOT NULL DEFAULT 'new', |
| 70 |
source varchar(50) NOT NULL, |
| 71 |
PRIMARY KEY (id), |
| 72 |
KEY subscription_id (subscription_id), |
| 73 |
KEY order_id (order_id), |
| 74 |
KEY source (source) |
| 75 |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ", |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Create necessary database tables |
| 81 |
* |
| 82 |
* @return void |
| 83 |
* @since 0.0.1 |
| 84 |
*/ |
| 85 |
public function create_tables() { |
| 86 |
global $wpdb; |
| 87 |
$wpdb->hide_errors(); |
| 88 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 89 |
$tables = $this->get_schema(); |
| 90 |
foreach ( $tables as $table ) { |
| 91 |
dbDelta( $table ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Save setup wizard data |
| 97 |
* |
| 98 |
* @since 0.0.2 |
| 99 |
*/ |
| 100 |
public function enable_setup_wizard() |
| 101 |
{ |
| 102 |
if ( !get_option( 'better_payment_setup_wizard' ) ) { |
| 103 |
update_option( 'better_payment_setup_wizard', 'redirect' ); |
| 104 |
|
| 105 |
/** |
| 106 |
* A missing `better_payment_setup_wizard` option is this plugin's |
| 107 |
* definition of a fresh install — it is what decides that the setup |
| 108 |
* wizard is owed a redirect at all. The same test marks the install |
| 109 |
* as eligible for the automatic usage-tracking opt-in, so the two can |
| 110 |
* never disagree about what "new install" means. |
| 111 |
* |
| 112 |
* Deliberately scoped to fresh installs: an existing site that has |
| 113 |
* already been through (or dismissed) the wizard keeps whatever |
| 114 |
* tracking state it has. See |
| 115 |
* Traits\Helper::maybe_auto_enable_usage_tracking(), which consumes |
| 116 |
* this marker exactly once. |
| 117 |
* |
| 118 |
* @since 2.3.2 |
| 119 |
*/ |
| 120 |
self::mark_tracking_auto_optin_pending(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Flag a fresh install for the automatic usage-tracking opt-in. |
| 126 |
* |
| 127 |
* Only ever sets the marker when the site has no tracking state at all. A |
| 128 |
* site that reached consent before this option existed (an upgrade that |
| 129 |
* re-runs activation, say) must not be handed a second, automatic opt-in. |
| 130 |
* |
| 131 |
* @return void |
| 132 |
* @since 2.3.2 |
| 133 |
*/ |
| 134 |
public function mark_tracking_auto_optin_pending() |
| 135 |
{ |
| 136 |
if ( get_option( 'better_payment_tracking_auto_optin' ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$allow_tracking = get_option( 'wpins_allow_tracking' ); |
| 141 |
|
| 142 |
if ( is_array( $allow_tracking ) && isset( $allow_tracking[ basename( BETTER_PAYMENT_FILE, '.php' ) ] ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
update_option( 'better_payment_tracking_auto_optin', 'pending' ); |
| 147 |
} |
| 148 |
} |
| 149 |
|