Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 78b306a

Browse files
committed
Add documentation and clean up coding standards for class-wc-custom-order-table-install.php
1 parent 73e65ba commit 78b306a

File tree

1 file changed

+105
-74
lines changed

1 file changed

+105
-74
lines changed
Lines changed: 105 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,121 @@
11
<?php
2+
/**
3+
* Table installation procedure.
4+
*/
25

36
class WC_Custom_Order_Table_Install {
4-
protected $table_version = 1;
57

6-
public function activate() {
7-
$this->maybe_install_tables();
8-
}
8+
/**
9+
* The database table schema version.
10+
*
11+
* @var int
12+
*/
13+
protected $table_version = 1;
914

10-
public function get_latest_table_version() {
11-
return absint( $this->table_version );
12-
}
1315

14-
public function get_installed_table_version() {
15-
return absint( get_option( 'wc_orders_table_version' ) );
16-
}
16+
/**
17+
* Actions to perform on plugin activation.
18+
*/
19+
public function activate() {
20+
$this->maybe_install_tables();
21+
}
1722

18-
protected function maybe_install_tables() {
19-
if( $this->get_installed_table_version() < $this->get_latest_table_version() ) {
20-
$this->install_tables();
21-
}
22-
}
23+
/**
24+
* Retrieve the latest table schema version.
25+
*
26+
* @return int The latest schema version.
27+
*/
28+
public function get_latest_table_version() {
29+
return absint( $this->table_version );
30+
}
2331

24-
protected function install_tables() {
25-
global $wpdb;
32+
/**
33+
* Retrieve the current table version from the options table.
34+
*
35+
* @return int The current schema version.
36+
*/
37+
public function get_installed_table_version() {
38+
return absint( get_option( 'wc_orders_table_version' ) );
39+
}
2640

27-
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
41+
/**
42+
* Install or update the tables if the site is not using the current schema.
43+
*/
44+
protected function maybe_install_tables() {
45+
if ( $this->get_installed_table_version() < $this->get_latest_table_version() ) {
46+
$this->install_tables();
47+
}
48+
}
2849

29-
$collate = '';
50+
/**
51+
* Perform the database delta to create the table.
52+
*
53+
* @global $wpdb
54+
*/
55+
protected function install_tables() {
56+
global $wpdb;
3057

31-
if ( $wpdb->has_cap( 'collation' ) ) {
32-
$collate = $wpdb->get_charset_collate();
33-
}
58+
// Load wp-admin/includes/upgrade.php, which defines dbDelta().
59+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
3460

35-
$table = wc_custom_order_table()->get_table_name();
61+
$collate = '';
3662

37-
$tables = "
38-
CREATE TABLE {$table} (
39-
order_id BIGINT UNSIGNED NOT NULL,
40-
order_key varchar(100) NOT NULL,
41-
customer_id BIGINT UNSIGNED NOT NULL,
42-
billing_first_name varchar(100) NOT NULL,
43-
billing_last_name varchar(100) NOT NULL,
44-
billing_company varchar(100) NOT NULL,
45-
billing_address_1 varchar(200) NOT NULL,
46-
billing_address_2 varchar(200) NOT NULL,
47-
billing_city varchar(100) NOT NULL,
48-
billing_state varchar(100) NOT NULL,
49-
billing_postcode varchar(100) NOT NULL,
50-
billing_country varchar(100) NOT NULL,
51-
billing_email varchar(200) NOT NULL,
52-
billing_phone varchar(200) NOT NULL,
53-
shipping_first_name varchar(100) NOT NULL,
54-
shipping_last_name varchar(100) NOT NULL,
55-
shipping_company varchar(100) NOT NULL,
56-
shipping_address_1 varchar(200) NOT NULL,
57-
shipping_address_2 varchar(200) NOT NULL,
58-
shipping_city varchar(100) NOT NULL,
59-
shipping_state varchar(100) NOT NULL,
60-
shipping_postcode varchar(100) NOT NULL,
61-
shipping_country varchar(100) NOT NULL,
62-
payment_method varchar(100) NOT NULL,
63-
payment_method_title varchar(100) NOT NULL,
63+
if ( $wpdb->has_cap( 'collation' ) ) {
64+
$collate = $wpdb->get_charset_collate();
65+
}
6466

65-
discount_total float NOT NULL DEFAULT 0,
66-
discount_tax float NOT NULL DEFAULT 0,
67-
shipping_total float NOT NULL DEFAULT 0,
68-
shipping_tax float NOT NULL DEFAULT 0,
69-
cart_tax float NOT NULL DEFAULT 0,
70-
total float NOT NULL DEFAULT 0,
71-
version varchar(16) NOT NULL,
72-
currency varchar(3) NOT NULL,
73-
prices_include_tax tinyint(1) NOT NULL,
74-
75-
transaction_id varchar(200) NOT NULL,
76-
customer_ip_address varchar(40) NOT NULL,
77-
customer_user_agent varchar(200) NOT NULL,
78-
created_via varchar(200) NOT NULL,
79-
date_completed datetime DEFAULT NULL,
80-
date_paid datetime DEFAULT NULL,
81-
cart_hash varchar(32) NOT NULL,
82-
83-
PRIMARY KEY (order_id)
84-
) $collate;
67+
$table = wc_custom_order_table()->get_table_name();
68+
$tables = "
69+
CREATE TABLE {$table} (
70+
order_id BIGINT UNSIGNED NOT NULL,
71+
order_key varchar(100) NOT NULL,
72+
customer_id BIGINT UNSIGNED NOT NULL,
73+
billing_first_name varchar(100) NOT NULL,
74+
billing_last_name varchar(100) NOT NULL,
75+
billing_company varchar(100) NOT NULL,
76+
billing_address_1 varchar(200) NOT NULL,
77+
billing_address_2 varchar(200) NOT NULL,
78+
billing_city varchar(100) NOT NULL,
79+
billing_state varchar(100) NOT NULL,
80+
billing_postcode varchar(100) NOT NULL,
81+
billing_country varchar(100) NOT NULL,
82+
billing_email varchar(200) NOT NULL,
83+
billing_phone varchar(200) NOT NULL,
84+
shipping_first_name varchar(100) NOT NULL,
85+
shipping_last_name varchar(100) NOT NULL,
86+
shipping_company varchar(100) NOT NULL,
87+
shipping_address_1 varchar(200) NOT NULL,
88+
shipping_address_2 varchar(200) NOT NULL,
89+
shipping_city varchar(100) NOT NULL,
90+
shipping_state varchar(100) NOT NULL,
91+
shipping_postcode varchar(100) NOT NULL,
92+
shipping_country varchar(100) NOT NULL,
93+
payment_method varchar(100) NOT NULL,
94+
payment_method_title varchar(100) NOT NULL,
95+
discount_total float NOT NULL DEFAULT 0,
96+
discount_tax float NOT NULL DEFAULT 0,
97+
shipping_total float NOT NULL DEFAULT 0,
98+
shipping_tax float NOT NULL DEFAULT 0,
99+
cart_tax float NOT NULL DEFAULT 0,
100+
total float NOT NULL DEFAULT 0,
101+
version varchar(16) NOT NULL,
102+
currency varchar(3) NOT NULL,
103+
prices_include_tax tinyint(1) NOT NULL,
104+
transaction_id varchar(200) NOT NULL,
105+
customer_ip_address varchar(40) NOT NULL,
106+
customer_user_agent varchar(200) NOT NULL,
107+
created_via varchar(200) NOT NULL,
108+
date_completed datetime DEFAULT NULL,
109+
date_paid datetime DEFAULT NULL,
110+
cart_hash varchar(32) NOT NULL,
111+
PRIMARY KEY (order_id)
112+
) $collate;
85113
";
86114

87-
dbDelta( $tables );
88-
update_option('wc_orders_table_version', $this->get_latest_table_version() );
89-
}
90-
}
115+
// Apply the database migration.
116+
dbDelta( $tables );
117+
118+
// Store the table version in the options table.
119+
update_option( 'wc_orders_table_version', $this->get_latest_table_version() );
120+
}
121+
}

0 commit comments

Comments
 (0)