Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions inc/class-wp-ultimo.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,10 @@ public function init(): void {
* @return void
*/
public function after_init() {

/**
* Loads admin pages
*
* @todo: Move this to a manager in the future?
* @todo: move this to a manager in the future?
*/
$this->load_admin_pages();

Expand Down
11 changes: 11 additions & 0 deletions inc/database/customers/class-customers-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,16 @@ class Customers_Schema extends Schema {
'sortable' => true,
],

// network_id - Added for multinetwork support
[
'name' => 'network_id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'allow_null' => true,
'searchable' => true,
'sortable' => true,
],

];
}
51 changes: 37 additions & 14 deletions inc/database/customers/class-customers-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Customers_Table extends Table {
* @since 2.0.0
* @var string
*/
protected $version = '2.0.1-revision.20230601';
protected $version = '2.0.2-revision.20250908';

/**
* List of table upgrades.
Expand All @@ -54,20 +54,9 @@ final class Customers_Table extends Table {
'2.0.1-revision.20210508' => 20_210_508,
'2.0.1-revision.20210607' => 20_210_607,
'2.0.1-revision.20230601' => 20_230_601,
'2.0.2-revision.20250908' => 20_250_908,
];

/**
* Customer constructor.
*
* @access public
* @since 2.0.0
* @return void
*/
public function __construct() {

parent::__construct();
}

/**
* Setup the database schema
*
Expand All @@ -80,6 +69,7 @@ protected function set_schema(): void {
$this->schema = "id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
user_id bigint(20) unsigned NOT NULL DEFAULT '0',
type varchar(20) NOT NULL DEFAULT 'customer',
network_id bigint(20) unsigned DEFAULT NULL,
email_verification enum('verified', 'pending', 'none') DEFAULT 'none',
date_modified datetime NULL,
date_registered datetime NULL,
Expand All @@ -89,7 +79,8 @@ protected function set_schema(): void {
ips longtext,
signup_form varchar(40) DEFAULT 'by-admin',
PRIMARY KEY (id),
KEY user_id (user_id)";
KEY user_id (user_id),
KEY network_id (network_id)";
}

/**
Expand Down Expand Up @@ -170,4 +161,36 @@ protected function __20230601(): bool { // phpcs:ignore PHPCompatibility.Functio

return true;
}

/**
* Adds network_id column for multinetwork support.
*
* @since 2.0.2
*/
protected function __20250908(): bool { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore

$result = $this->column_exists('network_id');

// Maybe add column
if (empty($result)) {
$query = "ALTER TABLE {$this->table_name} ADD COLUMN `network_id` bigint(20) unsigned DEFAULT NULL AFTER `type`;";

$result = $this->get_db()->query($query);

if ( ! $this->is_success($result)) {
return false;
}

// Add index for network_id
$index_query = "ALTER TABLE {$this->table_name} ADD INDEX `network_id` (`network_id`);";

$index_result = $this->get_db()->query($index_query);

if ( ! $this->is_success($index_result)) {
return false;
}
}

return true;
Comment on lines +184 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Keep the network_id index idempotent.
Line 192 will never run once the column already exists, so any partial run that skipped the index leaves the table without it. Add a separate index_exists('network_id') guard (or equivalent) after the column block so reruns can create the missing index and preserve expected performance.

-			// Add index for network_id
-			$index_query = "ALTER TABLE {$this->table_name} ADD INDEX `network_id` (`network_id`);";
-
-			$index_result = $this->get_db()->query($index_query);
-
-			if ( ! $this->is_success($index_result)) {
-				return false;
-			}
 		}
 
+		$index_exists = $this->index_exists('network_id');
+
+		if (empty($index_exists)) {
+			$index_query = "ALTER TABLE {$this->table_name} ADD INDEX `network_id` (`network_id`);";
+
+			$index_result = $this->get_db()->query($index_query);
+
+			if ( ! $this->is_success($index_result)) {
+				return false;
+			}
+		}
+
 		return true;

Committable suggestion skipped: line range outside the PR's diff.

}
}
11 changes: 11 additions & 0 deletions inc/database/memberships/class-memberships-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,16 @@ class Memberships_Schema extends Schema {
'pattern' => '%d',
],

// network_id - Added for multinetwork support
[
'name' => 'network_id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'allow_null' => true,
'searchable' => true,
'sortable' => true,
],

];
}
37 changes: 36 additions & 1 deletion inc/database/memberships/class-memberships-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Memberships_Table extends Table {
* @since 2.0.0
* @var string
*/
protected $version = '2.0.1-revision.20230601';
protected $version = '2.0.2-revision.20250908';

/**
* List of table upgrades.
Expand All @@ -52,6 +52,7 @@ final class Memberships_Table extends Table {
*/
protected $upgrades = [
'2.0.1-revision.20230601' => 20_230_601,
'2.0.2-revision.20250908' => 20_250_908,
];

/**
Expand All @@ -68,6 +69,7 @@ protected function set_schema(): void {
user_id bigint(20) unsigned DEFAULT NULL,
migrated_from_id bigint(20) DEFAULT NULL,
plan_id bigint(20) NOT NULL default '0',
network_id bigint(20) unsigned DEFAULT NULL,
addon_products longtext,
currency varchar(10) NOT NULL DEFAULT 'USD',
initial_amount decimal(13,4) default 0,
Expand Down Expand Up @@ -97,6 +99,7 @@ protected function set_schema(): void {
PRIMARY KEY (id),
KEY customer_id (customer_id),
KEY plan_id (plan_id),
KEY network_id (network_id),
KEY status (status),
KEY disabled (disabled)";
}
Expand Down Expand Up @@ -131,4 +134,36 @@ protected function __20230601(): bool { // phpcs:ignore PHPCompatibility.Functio

return true;
}

/**
* Adds network_id column for multinetwork support.
*
* @since 2.0.2
*/
protected function __20250908(): bool { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore

$result = $this->column_exists('network_id');

// Maybe add column
if (empty($result)) {
$query = "ALTER TABLE {$this->table_name} ADD COLUMN `network_id` bigint(20) unsigned DEFAULT NULL AFTER `plan_id`;";

$result = $this->get_db()->query($query);

if ( ! $this->is_success($result)) {
return false;
}

// Add index for network_id
$index_query = "ALTER TABLE {$this->table_name} ADD INDEX `network_id` (`network_id`);";

$index_result = $this->get_db()->query($index_query);

if ( ! $this->is_success($index_result)) {
return false;
}
}

return true;
}
}
11 changes: 11 additions & 0 deletions inc/database/products/class-products-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,16 @@ class Products_Schema extends Schema {
'sortable' => true,
],

// network_id - Added for multinetwork support
[
'name' => 'network_id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'allow_null' => true,
'searchable' => true,
'sortable' => true,
],

];
}
39 changes: 37 additions & 2 deletions inc/database/products/class-products-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Products_Table extends Table {
* @since 2.0.0
* @var string
*/
protected $version = '2.0.1-revision.20230601';
protected $version = '2.0.2-revision.20250908';

/**
* List of table upgrades.
Expand All @@ -54,6 +54,7 @@ final class Products_Table extends Table {
'2.0.1-revision.20210419' => 20_210_419,
'2.0.1-revision.20210607' => 20_210_607,
'2.0.1-revision.20230601' => 20_230_601,
'2.0.2-revision.20250908' => 20_250_908,
];

/**
Expand All @@ -70,6 +71,7 @@ protected function set_schema(): void {
slug tinytext NOT NULL DEFAULT '',
parent_id bigint(20),
migrated_from_id bigint(20) DEFAULT NULL,
network_id bigint(20) unsigned DEFAULT NULL,
description longtext NOT NULL default '',
product_group varchar(20) DEFAULT '',
currency varchar(10) NOT NULL DEFAULT 'USD',
Expand All @@ -87,7 +89,8 @@ protected function set_schema(): void {
type tinytext NOT NULL DEFAULT '',
date_created datetime NULL,
date_modified datetime NULL,
PRIMARY KEY (id)";
PRIMARY KEY (id),
KEY network_id (network_id)";
}

/**
Expand Down Expand Up @@ -167,4 +170,36 @@ protected function __20230601(): bool { // phpcs:ignore PHPCompatibility.Functio

return true;
}

/**
* Adds network_id column for multinetwork support.
*
* @since 2.0.2
*/
protected function __20250908(): bool { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore

$result = $this->column_exists('network_id');

// Maybe add column
if (empty($result)) {
$query = "ALTER TABLE {$this->table_name} ADD COLUMN `network_id` bigint(20) unsigned DEFAULT NULL AFTER `migrated_from_id`;";

$result = $this->get_db()->query($query);

if ( ! $this->is_success($result)) {
return false;
}

// Add index for network_id
$index_query = "ALTER TABLE {$this->table_name} ADD INDEX `network_id` (`network_id`);";

$index_result = $this->get_db()->query($index_query);

if ( ! $this->is_success($index_result)) {
return false;
}
}

return true;
}
}
32 changes: 32 additions & 0 deletions inc/models/class-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ class Customer extends Base_Model implements Billable, Notable {
*/
protected $extra_information;

/**
* Network ID for multinetwork support.
*
* @since 2.3.0
* @var int|null
*/
protected $network_id;

/**
* Query Class to the static query methods.
*
Expand Down Expand Up @@ -156,6 +164,7 @@ public function validation_rules() {
'ips' => 'array',
'extra_information' => 'default:',
'signup_form' => 'default:',
'network_id' => 'integer|nullable',
];
}

Expand Down Expand Up @@ -875,4 +884,27 @@ public function set_signup_form($signup_form): void {

$this->signup_form = $signup_form;
}

/**
* Get the network ID for multinetwork support.
*
* @since 2.3.0
* @return int|null
*/
public function get_network_id() {

return $this->network_id ? absint($this->network_id) : null;
}

/**
* Set the network ID for multinetwork support.
*
* @since 2.3.0
* @param int|null $network_id Network ID.
* @return void
*/
public function set_network_id($network_id): void {

$this->network_id = $network_id ? absint($network_id) : null;
}
}
Loading
Loading