Skip to content

Commit a6e5213

Browse files
committed
spotfixes: add secret key on plugin activation; fix syntax of cart_hash unique key definition
1 parent 47bfd3e commit a6e5213

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/AbandonedCarts/CartsTable.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CartsTable extends Service {
2525
*
2626
* @since 2019-10-09
2727
*/
28-
const DB_VERSION = '1.2';
28+
const DB_VERSION = '1.3';
2929

3030
/**
3131
* Option name for abandoned carts db version.
@@ -73,7 +73,7 @@ public function create_table() {
7373
cart_created_ts int(11) unsigned NOT NULL DEFAULT 0,
7474
cart_hash binary(16) NOT NULL DEFAULT 0,
7575
PRIMARY KEY (cart_id),
76-
UNIQUE KEY (cart_hash)
76+
UNIQUE KEY cart_hash (cart_hash)
7777
) {$wpdb->get_charset_collate()}";
7878

7979
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
@@ -91,27 +91,13 @@ public function create_table() {
9191
protected function update_table() {
9292
global $wpdb;
9393

94-
$table_name = $wpdb->prefix . self::TABLE_NAME;
94+
$table_name = self::get_table_name();
9595

96-
// Check if hash key exists, if not, create key and update existing values.
9796
// phpcs:disable WordPress.DB.PreparedSQL -- Okay use of unprepared variable for table name in SQL.
9897
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) ) {
99-
if ( ! $wpdb->get_var( "SHOW KEYS FROM {$table_name} WHERE Key_name = 'cart_hash'" ) ) {
100-
101-
// Update existing entries.
102-
$wpdb->query(
103-
"UPDATE {$table_name}
104-
SET cart_hash = UNHEX(MD5(CONCAT(user_id, user_email)))"
105-
);
106-
107-
// Add unique key constraint.
108-
$wpdb->query(
109-
"ALTER TABLE {$table_name}
110-
ADD UNIQUE KEY(`cart_hash`)"
111-
);
112-
113-
update_option( self::DB_VERSION_OPTION_NAME, self::DB_VERSION );
114-
}
98+
99+
// Any data updates would be performed here.
100+
update_option( self::DB_VERSION_OPTION_NAME, self::DB_VERSION );
115101
}
116102
// phpcs:enable
117103
}

src/Plugin.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,28 @@ private function maybe_activate_woocommerce() {
232232
public function do_activation_process() {
233233
$this->maybe_activate_woocommerce();
234234

235+
$this->create_initial_secret_key();
235236
$this->create_abandoned_carts_table();
236237
$this->create_abandoned_carts_expiration_check();
237238

238239
flush_rewrite_rules();
239240
}
240241

242+
/**
243+
* Create a secret key on plugin activation so JWT tokens can used in the Abandoned Carts REST API.
244+
*
245+
* @author George Gecewicz <[email protected]>
246+
* @since 2019-10-29
247+
*/
248+
private function create_initial_secret_key() {
249+
update_option( 'cc_woo_abandoned_carts_secret_key', wp_generate_password( 64, true, true ) );
250+
}
251+
241252
/**
242253
* Creates the database table for Abandoned Carts.
243254
*
244-
* @author Rebekah Van Epps <george.gecewicz@webdevstudios.com>
245-
* @since 2019-20-24
255+
* @author Rebekah Van Epps <rebekah.vanepps@webdevstudios.com>
256+
* @since 2019-10-24
246257
*/
247258
private function create_abandoned_carts_table() {
248259
( new CartsTable() )->create_table();

0 commit comments

Comments
 (0)