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

Commit 7076192

Browse files
committed
Merge branch 'develop' into fix/woocommerce-bootstrapping
2 parents b74d8d1 + c69bd04 commit 7076192

7 files changed

+92
-15
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ matrix:
2626
env: WP_VERSION=latest
2727
- php: 5.6
2828
env: WP_VERSION=trunk
29-
- php: 5.6
30-
env: WP_TRAVISCI=phpcs
29+
#- php: 5.6
30+
# env: WP_TRAVISCI=phpcs
3131
- php: 5.3
3232
env: WP_VERSION=latest
3333
dist: precise

includes/class-wc-custom-order-table-cli.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @author Liquid Web
77
*/
88

9+
/**
10+
* Manages the contents of the WooCommerce order table.
11+
*/
912
class WC_Custom_Order_Table_CLI extends WP_CLI_Command {
1013

1114
/**

includes/class-wc-custom-order-table-install.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
* @author Liquid Web
77
*/
88

9+
/**
10+
* Installer for WooCommerce Custom Order Tables.
11+
*
12+
* Usage:
13+
*
14+
* WC_Custom_Order_Table_Install::activate();
15+
*/
916
class WC_Custom_Order_Table_Install {
1017

1118
/**
@@ -18,14 +25,14 @@ class WC_Custom_Order_Table_Install {
1825
*
1926
* @var int
2027
*/
21-
protected static $table_version = 1;
28+
protected static $table_version = 2;
2229

2330
/**
2431
* Actions to perform on plugin activation.
2532
*/
2633
public static function activate() {
2734
// We're already on the latest schema version.
28-
if ( (int) self::$table_version === (int) get_option( self::SCHEMA_VERSION_KEY ) ) {
35+
if ( (int) get_option( self::SCHEMA_VERSION_KEY ) === (int) self::$table_version ) {
2936
return false;
3037
}
3138

@@ -88,7 +95,10 @@ protected static function install_tables() {
8895
date_completed datetime DEFAULT NULL,
8996
date_paid datetime DEFAULT NULL,
9097
cart_hash varchar(32) NOT NULL,
91-
PRIMARY KEY (order_id)
98+
PRIMARY KEY (order_id),
99+
UNIQUE KEY `order_key` (`order_key`),
100+
KEY `customer_id` (`customer_id`),
101+
KEY `order_total` (`total`)
92102
) $collate;
93103
";
94104

includes/class-wc-custom-order-table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @author Liquid Web
77
*/
88

9+
/**
10+
* Core functionality for WooCommerce Custom Order Tables.
11+
*/
912
class WC_Custom_Order_Table {
1013

1114
/**

includes/class-wc-order-data-store-custom-table.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
/**
33
* WooCommerce order data store.
44
*
5-
* Orders are still treated as posts within WordPress, but the data is stored in a separate table.
6-
*
75
* @package WooCommerce_Custom_Order_Tables
86
* @author Liquid Web
97
*/
108

9+
/**
10+
* Extension of the Abstract_WC_Order_Data_Store_CPT class, designed to map data between
11+
* WooCommerce and the custom database table.
12+
*
13+
* Orders are still treated as posts within WordPress, but the data is stored in a separate table.
14+
*/
1115
class WC_Order_Data_Store_Custom_Table extends Abstract_WC_Order_Data_Store_CPT implements WC_Object_Data_Store_Interface, WC_Order_Data_Store_Interface {
1216

1317
/**
@@ -403,12 +407,12 @@ public function get_orders( $args = array() ) {
403407
$wc_customer_query = array();
404408

405409
if ( ! empty( $args['customer'] ) ) {
406-
$values = is_array( $args['customer'] ) ? $args['customer'] : array( $args['customer'] );
410+
$values = is_array( $args['customer'] ) ? $args['customer'] : array( $args['customer'] );
407411
$wc_customer_query = array_merge( $wc_customer_query, $values );
408412
}
409413

410414
if ( ! empty( $args['email'] ) ) {
411-
$values = is_array( $args['email'] ) ? $args['email'] : array( $args['email'] );
415+
$values = is_array( $args['email'] ) ? $args['email'] : array( $args['email'] );
412416
$wc_customer_query = array_merge( $wc_customer_query, $values );
413417
}
414418

@@ -482,13 +486,13 @@ public function get_orders( $args = array() ) {
482486
*/
483487
private function get_orders_generate_customer_meta_query( $values, $relation = 'or' ) {
484488
$meta_query = array(
485-
'relation' => strtoupper( $relation ),
489+
'relation' => strtoupper( $relation ),
486490
'customer_emails' => array(
487491
'key' => '_billing_email',
488492
'value' => array(),
489493
'compare' => 'IN',
490494
),
491-
'customer_ids' => array(
495+
'customer_ids' => array(
492496
'key' => '_customer_user',
493497
'value' => array(),
494498
'compare' => 'IN',
@@ -713,7 +717,7 @@ public function populate_from_meta( &$order, $save = true, $delete = false ) {
713717

714718
if ( is_null( $table_data ) ) {
715719
$original_creating = $this->creating;
716-
$this->creating = true;
720+
$this->creating = true;
717721
}
718722

719723
foreach ( $this->get_postmeta_mapping() as $column => $meta_key ) {
@@ -777,8 +781,8 @@ public function query( $query_vars ) {
777781

778782
if ( ! empty( $args['errors'] ) ) {
779783
$query = (object) array(
780-
'posts' => array(),
781-
'found_posts' => 0,
784+
'posts' => array(),
785+
'found_posts' => 0,
782786
'max_num_pages' => 0,
783787
);
784788
} else {

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
<exclude-pattern>*/node_modules/*</exclude-pattern>
1616
<exclude-pattern>*/vendor/*</exclude-pattern>
17+
<exclude-pattern>*/tests/*</exclude-pattern>
1718
</ruleset>

tests/test-installation.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function test_can_upgrade_table() {
7676
// Get the current schema version, then increment it.
7777
$property = new ReflectionProperty( 'WC_Custom_Order_Table_Install', 'table_version' );
7878
$property->setAccessible( true );
79-
$version = $property->getValue();
79+
$version = $property->getValue();
8080
$property->setValue( $version + 1 );
8181

8282
// Run the activation script again.
@@ -103,4 +103,60 @@ public function test_current_schema_version_is_not_autoloaded() {
103103
'The schema version should not be autoloaded.'
104104
);
105105
}
106+
107+
/**
108+
* Test that the generated database schema contains the expected indexes.
109+
*
110+
* @dataProvider table_index_provider()
111+
*/
112+
public function test_database_indexes( $non_unique, $key_name, $column_name ) {
113+
global $wpdb;
114+
115+
WC_Custom_Order_Table_Install::activate();
116+
117+
$table = wc_custom_order_table()->get_table_name();
118+
$indexes = $wpdb->get_results( "SHOW INDEX FROM $table", ARRAY_A );
119+
$search = array(
120+
'Non_unique' => $non_unique,
121+
'Key_name' => $key_name,
122+
'Column_name' => $column_name,
123+
);
124+
125+
// Find the index by name.
126+
foreach ( $indexes as $index ) {
127+
if ( $index['Key_name'] !== $key_name ) {
128+
continue;
129+
}
130+
131+
$this->assertEquals(
132+
$non_unique,
133+
$index['Non_unique'],
134+
sprintf(
135+
'Did not match expected non-uniqueness (Received %d, expected %d',
136+
$non_unique,
137+
$index['Non_unique']
138+
)
139+
);
140+
141+
$this->assertEquals(
142+
$column_name,
143+
$index['Column_name'],
144+
sprintf( 'Expected index "%s" on column %s.', $key_name, $column_name )
145+
);
146+
147+
// We've checked the index we've come to check, return early.
148+
return;
149+
}
150+
151+
$this->fail( sprintf( 'Could not find an index with name "%s".', $key_name ) );
152+
}
153+
154+
public function table_index_provider() {
155+
return array(
156+
'Primary key' => array( 0, 'PRIMARY', 'order_id' ),
157+
'Order key' => array( 0, 'order_key', 'order_key' ),
158+
'Customer ID' => array( 1, 'customer_id', 'customer_id' ),
159+
'Order total' => array( 1, 'order_total', 'total' ),
160+
);
161+
}
106162
}

0 commit comments

Comments
 (0)